Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

speed.c 2.7 KiB

12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
12 år sedan
6 månader sedan
12 år sedan
12 år sedan
6 månader sedan
12 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://github.com/WiringPi/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: -> character device ABI
  72. printf ("\n/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ;
  73. wiringPiSetupSys () ;
  74. speedTest (17, SLOW_COUNT) ;
  75. // character device ABI
  76. printf ("\ncharacter device ABI method: (%8d iterations)\n", SLOW_COUNT) ;
  77. wiringPiSetupGpioDevice () ;
  78. pinMode (17, OUTPUT) ;
  79. speedTest (17, SLOW_COUNT) ;
  80. return 0 ;
  81. }