No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

120 líneas
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://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 ("SPEED: %d\n", speed) ;
  65. printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
  66. printf ("| MHz | Size | mS/Trans | TpS | Mb/Sec | Latency mS |\n") ;
  67. printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
  68. spiFail = FALSE ;
  69. spiSetup (speed * 1000000) ;
  70. for (size = 1 ; size <= MAX_SIZE ; size *= 2)
  71. {
  72. printf ("| %5d | %6d ", speed, size) ;
  73. start = millis () ;
  74. for (times = 0 ; times < NUM_TIMES ; ++times)
  75. if (wiringPiSPIDataRW (SPI_CHAN, myData, size) == -1)
  76. {
  77. printf ("SPI failure: %s\n", strerror (errno)) ;
  78. spiFail = TRUE ;
  79. break ;
  80. }
  81. end = millis () ;
  82. if (spiFail)
  83. break ;
  84. timePerTransaction = ((double)(end - start) / (double)NUM_TIMES) / 1000.0 ;
  85. dataSpeed = (double)(size * 8) / (1024.0 * 1024.0) / timePerTransaction ;
  86. perfectTimePerTransaction = ((double)(size * 8)) / ((double)(speed * 1000000)) ;
  87. printf ("| %8.3f ", timePerTransaction * 1000.0) ;
  88. printf ("| %8.1f ", 1.0 / timePerTransaction) ;
  89. printf ("| %9.5f ", dataSpeed) ;
  90. printf ("| %8.5f ", (timePerTransaction - perfectTimePerTransaction) * 1000.0) ;
  91. printf ("|\n") ;
  92. }
  93. close (myFd) ;
  94. printf ("+-------+--------+----------+----------+-----------+------------+\n") ;
  95. printf ("\n") ;
  96. }
  97. return 0 ;
  98. }