chiark / gitweb /
wiringPi Version 2 - First commit (of v2)
[wiringPi.git] / examples / speed.c
index 2f5d9904a7e2c46127eba44f13220ba575ad22d5..0a42b36291473907b133c0e67967900a0938eada 100644 (file)
@@ -1,8 +1,26 @@
-
 /*
  * speed.c:
  *     Simple program to measure the speed of the various GPIO
  *     access mechanisms.
+ *
+ * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
+ ***********************************************************************
+ * This file is part of wiringPi:
+ *     https://projects.drogon.net/raspberry-pi/wiringpi/
+ *
+ *    wiringPi is free software: you can redistribute it and/or modify
+ *    it under the terms of the GNU Lesser General Public License as published by
+ *    the Free Software Foundation, either version 3 of the License, or
+ *    (at your option) any later version.
+ *
+ *    wiringPi is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU Lesser General Public License for more details.
+ *
+ *    You should have received a copy of the GNU Lesser General Public License
+ *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
+ ***********************************************************************
  */
 
 #include <wiringPi.h>
 
 #define        FAST_COUNT      10000000
 #define        SLOW_COUNT       1000000
+#define        PASSES                 5
 
-
-int main (void)
+void speedTest (int pin, int maxCount)
 {
-  int i ;
-  uint32_t start, end, count, sum, perSec ;
-
-  printf ("Raspberry Pi wiringPi speed test program\n") ;
-
-// Start the standard way
-
-  if (wiringPiSetup () == -1)
-    exit (1) ;
-
-  printf ("Native wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
-
-  pinMode (0, OUTPUT) ;
+  int count, sum, perSec, i ;
+  unsigned int start, end ;
 
   sum = 0 ;
-  for (i = 0 ; i < 3 ; ++i)
-  {
-    printf ("  Pass: %d: ", i) ;
-    fflush (stdout) ;
 
+  for (i = 0 ; i < PASSES ; ++i)
+  {
     start = millis () ;
-    for (count = 0 ; count < FAST_COUNT ; ++count)
-      digitalWrite (0, 1) ;
+    for (count = 0 ; count < maxCount ; ++count)
+      digitalWrite (pin, 1) ;
     end = millis () ;
-    printf (" %8dmS\n", end - start) ;
+    printf (" %6d", end - start) ;
+    fflush (stdout) ;
     sum += (end - start) ;
   }
-  digitalWrite (0, 0) ;
-  printf ("   Average: %8dmS", sum / 3) ;
-  perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
-  printf (": %6d/sec\n", perSec) ;
-
 
-  printf ("Native GPIO method: (%8d iterations)\n", FAST_COUNT) ;
+  digitalWrite (pin, 0) ;
+  printf (". Av: %6dmS", sum / PASSES) ;
+  perSec = (int)(double)maxCount / (double)((double)sum / (double)PASSES) * 1000.0 ;
+  printf (": %7d/sec\n", perSec) ;
+}
 
-  if (wiringPiSetupGpio () == -1)
-    exit (1) ;
 
-  pinMode (17, OUTPUT) ;
+int main (void)
+{
+  printf ("Raspberry Pi wiringPi GPIO speed test program\n") ;
+  printf ("=============================================\n") ;
 
-  sum = 0 ;
-  for (i = 0 ; i < 3 ; ++i)
-  {
-    printf ("  Pass: %d: ", i) ;
-    fflush (stdout) ;
+// Start the standard way
 
-    start = millis () ;
-    for (count = 0 ; count < 10000000 ; ++count)
-      digitalWrite (17, 1) ;
-    end = millis () ;
-    printf (" %8dmS\n", end - start) ;
-    sum += (end - start) ;
-  }
-  digitalWrite (17, 0) ;
-  printf ("   Average: %8dmS", sum / 3) ;
-  perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
-  printf (": %6d/sec\n", perSec) ;
+  printf ("\nNative wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
+  wiringPiSetup () ;
+  pinMode (0, OUTPUT) ;
+  speedTest (0, FAST_COUNT) ;
 
+// GPIO
 
-// Switch to SYS mode:
+  printf ("\nNative GPIO method: (%8d iterations)\n", FAST_COUNT) ;
+  wiringPiSetupGpio () ;
+  pinMode (17, OUTPUT) ;
+  speedTest (17, FAST_COUNT) ;
 
-  if (wiringPiSetupSys () == -1)
-    exit (1) ;
+// Phys
 
-  printf ("/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ;
+  printf ("\nPhysical pin GPIO method: (%8d iterations)\n", FAST_COUNT) ;
+  wiringPiSetupPhys () ;
+  pinMode (11, OUTPUT) ;
+  speedTest (11, FAST_COUNT) ;
 
-  sum = 0 ;
-  for (i = 0 ; i < 3 ; ++i)
-  {
-    printf ("  Pass: %d: ", i) ;
-    fflush (stdout) ;
+// Switch to SYS mode:
 
-    start = millis () ;
-    for (count = 0 ; count < SLOW_COUNT ; ++count)
-      digitalWrite (17, 1) ;
-    end = millis () ;
-    printf (" %8dmS\n", end - start) ;
-    sum += (end - start) ;
-  }
-  digitalWrite (17, 0) ;
-  printf ("   Average: %8dmS", sum / 3) ;
-  perSec = (int)(double)SLOW_COUNT / (double)((double)sum / 3.0) * 1000.0 ;
-  printf (": %6d/sec\n", perSec) ;
+  system ("/usr/local/bin/gpio export 17 out") ;
+  printf ("\n/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ;
+  wiringPiSetupSys () ;
+  speedTest (17, SLOW_COUNT) ;
 
   return 0 ;
 }