speed.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * speed.c:
  3. * Simple program to measure the speed of the various GPIO
  4. * access mechanisms.
  5. *
  6. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <wiringPi.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdint.h>
  29. #define FAST_COUNT 10000000
  30. #define SLOW_COUNT 1000000
  31. #define PASSES 5
  32. void speedTest (int pin, int maxCount)
  33. {
  34. int count, sum, perSec, i ;
  35. unsigned int start, end ;
  36. sum = 0 ;
  37. for (i = 0 ; i < PASSES ; ++i)
  38. {
  39. start = millis () ;
  40. for (count = 0 ; count < maxCount ; ++count)
  41. digitalWrite (pin, 1) ;
  42. end = millis () ;
  43. printf (" %6d", end - start) ;
  44. fflush (stdout) ;
  45. sum += (end - start) ;
  46. }
  47. digitalWrite (pin, 0) ;
  48. printf (". Av: %6dmS", sum / PASSES) ;
  49. perSec = (int)(double)maxCount / (double)((double)sum / (double)PASSES) * 1000.0 ;
  50. printf (": %7d/sec\n", perSec) ;
  51. }
  52. int main (void)
  53. {
  54. printf ("Raspberry Pi wiringPi GPIO speed test program\n") ;
  55. printf ("=============================================\n") ;
  56. // Start the standard way
  57. printf ("\nNative wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
  58. wiringPiSetup () ;
  59. pinMode (0, OUTPUT) ;
  60. speedTest (0, FAST_COUNT) ;
  61. // GPIO
  62. printf ("\nNative GPIO method: (%8d iterations)\n", FAST_COUNT) ;
  63. wiringPiSetupGpio () ;
  64. pinMode (17, OUTPUT) ;
  65. speedTest (17, FAST_COUNT) ;
  66. // Phys
  67. printf ("\nPhysical pin GPIO method: (%8d iterations)\n", FAST_COUNT) ;
  68. wiringPiSetupPhys () ;
  69. pinMode (11, OUTPUT) ;
  70. speedTest (11, FAST_COUNT) ;
  71. // Switch to SYS mode:
  72. system ("/usr/local/bin/gpio export 17 out") ;
  73. printf ("\n/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ;
  74. wiringPiSetupSys () ;
  75. speedTest (17, SLOW_COUNT) ;
  76. return 0 ;
  77. }