Lots of changes here. Added new I2C test code, a new serialTest program,
and developed the new ISR - Interrupt Service Routine
handler - much easier than the old waitForInterrupt code!
Minor tweaks to the gpio program to recognise the environment variable
WIRINGPI_DEBUG too, and removed the printing of the errors from the
main wiringPi setup routines (and added some new ones!)
diff --git a/examples/Makefile b/examples/Makefile
index 738d36c..3607fc8 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -35,10 +35,10 @@
# Should not alter anything below this line
###############################################################################
-SRC = test1.c test2.c speed.c lcd.c wfi.c \
+SRC = test1.c test2.c speed.c lcd.c wfi.c isr.c \
piface.c gertboard.c nes.c \
pwm.c tone.c servo.c \
- delayTest.c serialRead.c okLed.c
+ delayTest.c serialRead.c serialTest.c okLed.c
OBJ = $(SRC:.c=.o)
@@ -69,6 +69,10 @@
@echo [link]
@$(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS)
+isr: isr.o
+ @echo [link]
+ @$(CC) -o $@ isr.o $(LDFLAGS) $(LDLIBS)
+
piface: piface.o
@echo [link]
@$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS) -lpthread
@@ -93,6 +97,10 @@
@echo [link]
@$(CC) -o $@ serialRead.o $(LDFLAGS) $(LDLIBS)
+serialTest: serialTest.o
+ @echo [link]
+ @$(CC) -o $@ serialTest.o $(LDFLAGS) $(LDLIBS)
+
okLed: okLed.o
@echo [link]
@$(CC) -o $@ okLed.o $(LDFLAGS) $(LDLIBS)
diff --git a/examples/isr.c b/examples/isr.c
new file mode 100644
index 0000000..2bef54a
--- /dev/null
+++ b/examples/isr.c
@@ -0,0 +1,99 @@
+/*
+ * isr.c:
+ * Wait for Interrupt test program - ISR method
+ *
+ * How to test:
+ * Use the SoC's pull-up and pull down resistors that are avalable
+ * on input pins. So compile & run this program (via sudo), then
+ * in another terminal:
+ * gpio mode 0 up
+ * gpio mode 0 down
+ * at which point it should trigger an interrupt. Toggle the pin
+ * up/down to generate more interrupts to test.
+ *
+ * Copyright (c) 2013 Gordon Henderson.
+ ***********************************************************************
+ * 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 <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <wiringPi.h>
+
+
+// What GPIO input are we using?
+// This is a wiringPi pin number
+
+#define BUTTON_PIN 0
+
+// globalCounter:
+// Global variable to count interrupts
+// Should be declared volatile to make sure the compiler doesn't cache it.
+
+static volatile int globalCounter = 0 ;
+
+
+/*
+ * myInterrupt:
+ *********************************************************************************
+ */
+
+void myInterrupt (void)
+{
+ ++globalCounter ;
+}
+
+
+/*
+ *********************************************************************************
+ * main
+ *********************************************************************************
+ */
+
+int main (void)
+{
+ int myCounter = 0 ;
+
+ if (wiringPiSetup () < 0)
+ {
+ fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
+ return 1 ;
+ }
+
+ if (wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
+ {
+ fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
+ return 1 ;
+ }
+
+
+ for (;;)
+ {
+ printf ("Waiting ... ") ; fflush (stdout) ;
+
+ while (myCounter == globalCounter)
+ delay (100) ;
+
+ printf (" Done. counter: %5d\n", globalCounter) ;
+ myCounter = globalCounter ;
+ }
+
+ return 0 ;
+}
diff --git a/examples/okLed.c b/examples/okLed.c
index 3bf21e2..02f0b22 100644
--- a/examples/okLed.c
+++ b/examples/okLed.c
@@ -17,6 +17,7 @@
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
+#include <math.h>
#include <wiringPi.h>
#include <softPwm.h>
diff --git a/examples/serialTest.c b/examples/serialTest.c
new file mode 100644
index 0000000..85a1a66
--- /dev/null
+++ b/examples/serialTest.c
@@ -0,0 +1,57 @@
+/*
+ * serialTest.c:
+ * Very simple program to test the serial port. Expects
+ * the port to be looped back to itself
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <wiringPi.h>
+#include <wiringSerial.h>
+
+int main ()
+{
+ int fd ;
+ int count ;
+ unsigned int nextTime ;
+
+ if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0)
+ {
+ fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
+ return 1 ;
+ }
+
+ if (wiringPiSetup () == -1)
+ {
+ fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
+ return 1 ;
+ }
+
+ nextTime = millis () + 300 ;
+
+ for (count = 0 ; count < 256 ; )
+ {
+ if (millis () > nextTime)
+ {
+ printf ("\nOut: %3d: ", count) ;
+ fflush (stdout) ;
+ serialPutchar (fd, count) ;
+ nextTime += 300 ;
+ ++count ;
+ }
+
+ delay (3) ;
+
+ while (serialDataAvail (fd))
+ {
+ printf (" -> %3d", serialGetchar (fd)) ;
+ fflush (stdout) ;
+ }
+ }
+
+ printf ("\n") ;
+ return 0 ;
+}
diff --git a/examples/wfi.c b/examples/wfi.c
index 9efcc2c..6bb6892 100644
--- a/examples/wfi.c
+++ b/examples/wfi.c
@@ -2,7 +2,17 @@
* wfi.c:
* Wait for Interrupt test program
*
- * Copyright (c) 2012 Gordon Henderson.
+ * This program demonstrates the use of the waitForInterrupt()
+ * function in wiringPi. It listens to a button input on
+ * BCM_GPIO pin 17 (wiringPi pin 0)
+ *
+ * The biggest issue with this method is that it really only works
+ * well in Sys mode.
+ *
+ * Jan 2013: This way of doing things is sort of deprecated now, see
+ * the wiringPiISR() function instead and the isr.c test program here.
+ *
+ * Copyright (c) 2012-2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -33,9 +43,8 @@
#define COUNT_KEY 0
// What BCM_GPIO input are we using?
-// GPIO 0 is one of the I2C pins with an on-board pull-up
-#define BUTTON_PIN 0
+#define BUTTON_PIN 17
// Debounce time in mS
@@ -63,13 +72,11 @@
int debounceTime = 0 ;
(void)piHiPri (10) ; // Set this thread to be high priority
- digitalWrite (18, 1) ;
for (;;)
{
if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it
{
-
// Bouncing?
if (millis () < debounceTime)
@@ -80,7 +87,6 @@
// We have a valid one
- digitalWrite (17, state) ;
state ^= 1 ;
piLock (COUNT_KEY) ;
@@ -89,7 +95,7 @@
// Wait for key to be released
- while (digitalRead (0) == LOW)
+ while (digitalRead (BUTTON_PIN) == LOW)
delay (1) ;
debounceTime = millis () + DEBOUNCE_TIME ;
@@ -108,11 +114,9 @@
{
// Use the gpio program to initialise the hardware
-// (This is the crude, but effective bit)
+// (This is the crude, but effective)
- system ("gpio edge 0 falling") ;
- system ("gpio export 17 out") ;
- system ("gpio export 18 out") ;
+ system ("gpio edge 17 falling") ;
// Setup wiringPi
@@ -120,9 +124,8 @@
// Fire off our interrupt handler
- piThreadCreate (waitForIt) ;
+ piThreadCreate (waitForIt) ;
- digitalWrite (17, 0) ;
}
@@ -147,7 +150,7 @@
piLock (COUNT_KEY) ;
myCounter = globalCounter ;
piUnlock (COUNT_KEY) ;
- delay (5000) ;
+ delay (500) ;
}
printf (" Done. myCounter: %5d\n", myCounter) ;