Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

122 linhas
3.4 KiB

  1. /*
  2. * spiSpeed.c:
  3. * Code to measure the SPI speed/latency.
  4. * Copyright (c) 2014 Gordon Henderson
  5. ***********************************************************************
  6. * This file is part of wiringPi:
  7. * https://github.com/WiringPi/WiringPi/
  8. *
  9. * wiringPi is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * wiringPi is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with wiringPi.
  21. * If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. //#include <fcntl.h>
  31. //#include <sys/ioctl.h>
  32. //#include <linux/spi/spidev.h>
  33. #include <wiringPi.h>
  34. #include <wiringPiSPI.h>
  35. #ifndef TRUE
  36. #define TRUE (1==1)
  37. #define FALSE (!TRUE)
  38. #endif
  39. #define SPI_CHAN 0
  40. #define NUM_TIMES 100
  41. #define MAX_SIZE (1024*1024)
  42. static int myFd ;
  43. void spiSetup (int speed)
  44. {
  45. if ((myFd = wiringPiSPISetup (SPI_CHAN, speed)) < 0)
  46. {
  47. fprintf (stderr, "Can't open the SPI bus: %s\n", strerror (errno)) ;
  48. exit (EXIT_FAILURE) ;
  49. }
  50. }
  51. int main (void)
  52. {
  53. int speed, times, size ;
  54. unsigned int start, end ;
  55. int spiFail ;
  56. unsigned char *myData ;
  57. double timePerTransaction, perfectTimePerTransaction, dataSpeed ;
  58. if ((myData = malloc (MAX_SIZE)) == NULL)
  59. {
  60. fprintf (stderr, "Unable to allocate buffer: %s\n", strerror (errno)) ;
  61. exit (EXIT_FAILURE) ;
  62. }
  63. wiringPiSetup () ;
  64. for (speed = 1 ; speed <= 32 ; speed *= 2)
  65. {
  66. printf ("SPEED: %d\n", speed) ;
  67. printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
  68. printf ("| MHz | Size | mS/Trans | TpS | Mb/Sec | Latency mS |\n") ;
  69. printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
  70. spiFail = FALSE ;
  71. spiSetup (speed * 1000000) ;
  72. for (size = 1 ; size <= MAX_SIZE ; size *= 2)
  73. {
  74. printf ("| %5d | %6d ", speed, size) ;
  75. start = millis () ;
  76. for (times = 0 ; times < NUM_TIMES ; ++times)
  77. if (wiringPiSPIDataRW (SPI_CHAN, myData, size) == -1)
  78. {
  79. printf ("SPI failure: %s\n", strerror (errno)) ;
  80. spiFail = TRUE ;
  81. break ;
  82. }
  83. end = millis () ;
  84. if (spiFail)
  85. break ;
  86. timePerTransaction = ((double)(end - start) / (double)NUM_TIMES) / 1000.0 ;
  87. dataSpeed = (double)(size * 8) / (1024.0 * 1024.0) / timePerTransaction ;
  88. perfectTimePerTransaction = ((double)(size * 8)) / ((double)(speed * 1000000)) ;
  89. printf ("| %8.3f ", timePerTransaction * 1000.0) ;
  90. printf ("| %8.1f ", 1.0 / timePerTransaction) ;
  91. printf ("| %9.5f ", dataSpeed) ;
  92. printf ("| %8.5f ", (timePerTransaction - perfectTimePerTransaction) * 1000.0) ;
  93. printf ("|\n") ;
  94. }
  95. close (myFd) ;
  96. printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
  97. printf ("\n") ;
  98. }
  99. return 0 ;
  100. }