spiSpeed.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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://projects.drogon.net/raspberry-pi/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. #define TRUE (1==1)
  36. #define FALSE (!TRUE)
  37. #define SPI_CHAN 0
  38. #define NUM_TIMES 100
  39. #define MAX_SIZE (1024*1024)
  40. static int myFd ;
  41. void spiSetup (int speed)
  42. {
  43. if ((myFd = wiringPiSPISetup (SPI_CHAN, speed)) < 0)
  44. {
  45. fprintf (stderr, "Can't open the SPI bus: %s\n", strerror (errno)) ;
  46. exit (EXIT_FAILURE) ;
  47. }
  48. }
  49. int main (void)
  50. {
  51. int speed, times, size ;
  52. unsigned int start, end ;
  53. int spiFail ;
  54. unsigned char *myData ;
  55. double timePerTransaction, perfectTimePerTransaction, dataSpeed ;
  56. if ((myData = malloc (MAX_SIZE)) == NULL)
  57. {
  58. fprintf (stderr, "Unable to allocate buffer: %s\n", strerror (errno)) ;
  59. exit (EXIT_FAILURE) ;
  60. }
  61. wiringPiSetup () ;
  62. for (speed = 1 ; speed <= 32 ; speed *= 2)
  63. {
  64. printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
  65. printf ("| MHz | Size | mS/Trans | TpS | Mb/Sec | Latency mS |\n") ;
  66. printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
  67. spiFail = FALSE ;
  68. spiSetup (speed * 1000000) ;
  69. for (size = 1 ; size <= MAX_SIZE ; size *= 2)
  70. {
  71. printf ("| %5d | %6d ", speed, size) ;
  72. start = millis () ;
  73. for (times = 0 ; times < NUM_TIMES ; ++times)
  74. if (wiringPiSPIDataRW (SPI_CHAN, myData, size) == -1)
  75. {
  76. printf ("SPI failure: %s\n", strerror (errno)) ;
  77. spiFail = TRUE ;
  78. break ;
  79. }
  80. end = millis () ;
  81. if (spiFail)
  82. break ;
  83. timePerTransaction = ((double)(end - start) / (double)NUM_TIMES) / 1000.0 ;
  84. dataSpeed = (double)(size * 8) / (1024.0 * 1024.0) / timePerTransaction ;
  85. perfectTimePerTransaction = ((double)(size * 8)) / ((double)(speed * 1000000)) ;
  86. printf ("| %8.3f ", timePerTransaction * 1000.0) ;
  87. printf ("| %8.1f ", 1.0 / timePerTransaction) ;
  88. printf ("| %9.5f ", dataSpeed) ;
  89. printf ("| %8.5f ", (timePerTransaction - perfectTimePerTransaction) * 1000.0) ;
  90. printf ("|\n") ;
  91. }
  92. close (myFd) ;
  93. printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
  94. printf ("\n") ;
  95. }
  96. return 0 ;
  97. }