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!)
This commit is contained in:
Gordon Henderson
2013-01-14 11:31:56 +00:00
parent 25e4ec570b
commit 13bbba7a22
15 changed files with 744 additions and 91 deletions

View File

@@ -35,10 +35,10 @@ LDLIBS = -lwiringPi
# 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 @@ wfi: wfi.o
@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 @@ serialRead: serialRead.o
@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)

99
examples/isr.c Normal file
View File

@@ -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 ;
}

View File

@@ -17,6 +17,7 @@
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <wiringPi.h>
#include <softPwm.h>

57
examples/serialTest.c Normal file
View File

@@ -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 ;
}

View File

@@ -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 @@ PI_THREAD (waitForIt)
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 @@ PI_THREAD (waitForIt)
// We have a valid one
digitalWrite (17, state) ;
state ^= 1 ;
piLock (COUNT_KEY) ;
@@ -89,7 +95,7 @@ PI_THREAD (waitForIt)
// 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 @@ void setup (void)
{
// 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 @@ void setup (void)
// Fire off our interrupt handler
piThreadCreate (waitForIt) ;
piThreadCreate (waitForIt) ;
digitalWrite (17, 0) ;
}
@@ -147,7 +150,7 @@ int main (void)
piLock (COUNT_KEY) ;
myCounter = globalCounter ;
piUnlock (COUNT_KEY) ;
delay (5000) ;
delay (500) ;
}
printf (" Done. myCounter: %5d\n", myCounter) ;