Big update here.

delayMicrosecondsHard re-written - again.
Added a serialRead example program, and added in the okLed
to the examples too.
Updated/checked some of the GPIO/PWM code.
Added in some experimental servo and tone generating code and
and example or 2.
Tweaks to the gpio command to correctly load the I2C modules too.
This commit is contained in:
Gordon Henderson
2012-12-06 21:49:41 +00:00
parent 183c5a6b5c
commit 25e4ec570b
19 changed files with 874 additions and 127 deletions

View File

@@ -30,16 +30,19 @@ INCLUDE = -I/usr/local/include
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe
LDFLAGS = -L/usr/local/lib
LIBS = -lwiringPi
LDLIBS = -lwiringPi
# Should not alter anything below this line
###############################################################################
SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c gertboard.c nes.c delayTest.c softPwm.c
SRC = test1.c test2.c speed.c lcd.c wfi.c \
piface.c gertboard.c nes.c \
pwm.c tone.c servo.c \
delayTest.c serialRead.c okLed.c
OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o gertboard.o nes.o delayTest.o softPwm.o
OBJ = $(SRC:.c=.o)
BINS = test1 test2 speed lcd wfi piface gertboard nes delayTest softPwm
BINS = $(SRC:.c=)
all:
@cat README.TXT
@@ -48,43 +51,59 @@ all:
test1: test1.o
@echo [link]
$(CC) -o $@ test1.o $(LDFLAGS) $(LIBS)
@$(CC) -o $@ test1.o $(LDFLAGS) $(LDLIBS)
test2: test2.o
@echo [link]
$(CC) -o $@ test2.o $(LDFLAGS) $(LIBS)
@$(CC) -o $@ test2.o $(LDFLAGS) $(LDLIBS)
speed: speed.o
@echo [link]
$(CC) -o $@ speed.o $(LDFLAGS) $(LIBS)
@$(CC) -o $@ speed.o $(LDFLAGS) $(LDLIBS)
lcd: lcd.o
@echo [link]
$(CC) -o $@ lcd.o $(LDFLAGS) $(LIBS)
@$(CC) -o $@ lcd.o $(LDFLAGS) $(LDLIBS)
wfi: wfi.o
@echo [link]
$(CC) -o $@ wfi.o $(LDFLAGS) $(LIBS) -lpthread
@$(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS)
piface: piface.o
@echo [link]
$(CC) -o $@ piface.o $(LDFLAGS) $(LIBS) -lpthread
@$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS) -lpthread
gertboard: gertboard.o
@echo [link]
$(CC) -o $@ gertboard.o $(LDFLAGS) $(LIBS) -lm
@$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS) -lm
nes: nes.o
@echo [link]
$(CC) -o $@ nes.o $(LDFLAGS) $(LIBS) -lm
@$(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS) -lm
softPwm: softPwm.o
pwm: pwm.o
@echo [link]
$(CC) -o $@ softPwm.o $(LDFLAGS) $(LIBS) -lm -lpthread
@$(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS) -lm -lpthread
delayTest: delayTest.o
@echo [link]
$(CC) -o $@ delayTest.o $(LDFLAGS) $(LIBS)
@$(CC) -o $@ delayTest.o $(LDFLAGS) $(LDLIBS)
serialRead: serialRead.o
@echo [link]
@$(CC) -o $@ serialRead.o $(LDFLAGS) $(LDLIBS)
okLed: okLed.o
@echo [link]
@$(CC) -o $@ okLed.o $(LDFLAGS) $(LDLIBS)
tone: tone.o
@echo [link]
@$(CC) -o $@ tone.o $(LDFLAGS) $(LDLIBS)
servo: servo.o
@echo [link]
@$(CC) -o $@ servo.o $(LDFLAGS) $(LDLIBS)
.c.o:
@@ -92,7 +111,7 @@ delayTest: delayTest.o
@$(CC) -c $(CFLAGS) $< -o $@
clean:
rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface gertboard nes delayTest softPwm
rm -f $(OBJ) *~ core tags $(BINS)
tags: $(SRC)
@echo [ctags]

View File

@@ -3,30 +3,24 @@
#include <unistd.h>
#include <wiringPi.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#define CYCLES 1000
#define DELAY 99
int main()
{
int x ;
struct timeval t1, t2 ;
long long t ;
unsigned int max, min ;
unsigned int values [CYCLES] ;
max = 0 ;
min = 1000000 ;
int t ;
int max, min ;
int del ;
int underRuns, overRuns, exactRuns, total ;
int descheds ;
if (wiringPiSetup () == -1)
return 1 ;
piHiPri (10) ;
sleep (1) ;
piHiPri (10) ; sleep (1) ;
// Baseline test
@@ -34,35 +28,56 @@ int main()
gettimeofday (&t2, NULL) ;
t = t2.tv_usec - t1.tv_usec ;
printf ("Baseline test: %lld\n", t);
printf ("Baseline test: %d\n", t);
for (x = 0 ; x < CYCLES ; ++x)
for (del = 1 ; del < 200 ; ++del)
{
gettimeofday (&t1, NULL) ;
delayMicroseconds (DELAY) ;
gettimeofday (&t2, NULL) ;
t = t2.tv_usec - t1.tv_usec ;
if (t > max) max = t ;
if (t < min) min = t ;
values [x] = t ;
}
underRuns = overRuns = exactRuns = total = 0 ;
descheds = 0 ;
max = del ;
min = del ;
printf ("Done: Max: %d, min: %d\n", max, min) ;
for (x = 0 ; x < CYCLES ; ++x)
{
for (;;) // Repeat this if we get a delay over 999uS
{ // -> High probability Linux has deschedulled us
gettimeofday (&t1, NULL) ;
delayMicroseconds (del) ;
gettimeofday (&t2, NULL) ;
for (x = 0 ; x < CYCLES ; ++x)
{
printf ("%4d", values [x]) ;
if (values [x] > DELAY)
printf (".") ;
else if (values [x] < DELAY)
printf ("-") ;
else
printf (" ") ;
if (((x + 1) % 20) == 0)
printf ("\n") ;
if (t2.tv_usec < t1.tv_usec) // Counter wrapped
t = (1000000 + t2.tv_usec) - t1.tv_usec;
else
t = t2.tv_usec - t1.tv_usec ;
if (t > 999)
{
++descheds ;
continue ;
}
else
break ;
}
if (t > max)
{
max = t ;
++overRuns ;
}
else if (t < min)
{
min = t ;
++underRuns ;
}
else
++exactRuns ;
total += t ;
}
printf ("Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d, Descheds: %2d\n",
del, min, max, underRuns, overRuns, exactRuns, total / CYCLES, descheds) ;
fflush (stdout) ;
delay (1) ;
}
printf ("\n") ;
return 0 ;
}

65
examples/okLed.c Normal file
View File

@@ -0,0 +1,65 @@
/*
* okLed:
* Make the OK LED on the Pi Pulsate...
* Copyright (c) 2012 gordon Henderson, but please Share and Enjoy!
*
* Originally posted to the Raspberry Pi forums:
* http://www.raspberrypi.org/phpBB3/viewtopic.php?p=162581#p162581
*
* Compile this and store it somewhere, then kick it off at boot time
* e.g. by putting it in /etc/rc.local and running it in the
* background &
*
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <wiringPi.h>
#include <softPwm.h>
#define OK_LED 16
int main ()
{
int fd, i ;
if ((fd = open ("/sys/class/leds/led0/trigger", O_RDWR)) < 0)
{
fprintf (stderr, "Unable to change LED trigger: %s\n", strerror (errno)) ;
return 1 ;
}
write (fd, "none\n", 5) ;
close (fd) ;
if (wiringPiSetupGpio () < 0)
{
fprintf (stderr, "Unable to setup GPIO: %s\n", strerror (errno)) ;
return 1 ;
}
softPwmCreate (OK_LED, 0, 100) ;
for (;;)
{
for (i = 0 ; i <= 100 ; ++i)
{
softPwmWrite (OK_LED, i) ;
delay (10) ;
}
delay (50) ;
for (i = 100 ; i >= 0 ; --i)
{
softPwmWrite (OK_LED, i) ;
delay (10) ;
}
delay (10) ;
}
return 0 ;
}

31
examples/serialRead.c Normal file
View File

@@ -0,0 +1,31 @@
/*
* serialRead.c:
* Example program to read bytes from the Serial line
*
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringSerial.h>
int main ()
{
int fd ;
if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0)
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
// Loop, getting and printing characters
for (;;)
{
putchar (serialGetchar (fd)) ;
fflush (stdout) ;
}
}

33
examples/servo.c Normal file
View File

@@ -0,0 +1,33 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <softServo.h>
int main ()
{
if (wiringPiSetup () == -1)
{
fprintf (stdout, "oops: %s\n", strerror (errno)) ;
return 1 ;
}
softServoSetup (0, 1, 2, 3, 4, 5, 6, 7) ;
softServoWrite (0, 0) ;
/*
softServoWrite (1, 1000) ;
softServoWrite (2, 1100) ;
softServoWrite (3, 1200) ;
softServoWrite (4, 1300) ;
softServoWrite (5, 1400) ;
softServoWrite (6, 1500) ;
softServoWrite (7, 2200) ;
*/
for (;;)
delay (10) ;
}

37
examples/tone.c Normal file
View File

@@ -0,0 +1,37 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <softTone.h>
#define RANGE 100
#define NUM_LEDS 12
int scale [8] = { 262, 294, 330, 349, 392, 440, 494, 525 } ;
int main ()
{
int i, j ;
char buf [80] ;
if (wiringPiSetup () == -1)
{
fprintf (stdout, "oops: %s\n", strerror (errno)) ;
return 1 ;
}
softToneCreate (3) ;
for (;;)
{
for (i = 0 ; i < 8 ; ++i)
{
printf ("%3d\n", i) ;
softToneWrite (3, scale [i]) ;
delay (500) ;
}
}
}