From e8f6258004e59b1452806e5c45bb6598d0158dd2 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 28 Jan 2013 13:00:47 +0000 Subject: [PATCH] Quite a few changes here. Added in generic 'blink' programs in the examples in C, RTB and Shell. Updated wiringPi with a little big-file on the millis() function and added in a new micros() function too. Updated the examples with standard LGPL headers. Added a new isr-osc.c test program - just for ISR timing purposes. --- examples/Makefile | 23 +++--- examples/README.TXT | 6 +- examples/blink.c | 50 +++++++++++++ examples/blink.rtb | 30 ++++++++ examples/blink.sh | 37 +++++++++ examples/delayTest.c | 24 ++++++ examples/gertboard.c | 19 ++++- examples/header.h | 23 ++++++ examples/isr-osc.c | 118 +++++++++++++++++++++++++++++ examples/nes.c | 23 ++++++ examples/okLed.c | 21 +++++- examples/piface.c | 24 +++++- examples/pwm.c | 24 ++++++ examples/serialRead.c | 21 +++++- examples/serialTest.c | 18 +++++ examples/servo.c | 24 ++++++ examples/speed.c | 20 ++++- examples/test1.c | 22 +++++- examples/test2.c | 23 +++++- examples/tone.c | 34 +++++++-- gpio/Makefile | 4 +- gpio/gpio.1 | 20 +++-- gpio/gpio.c | 77 ++++++++----------- wiringPi/lcd.c | 12 +++ wiringPi/lcd.h | 13 ++-- wiringPi/wiringPi.c | 169 +++++++++++++++++++++++++----------------- wiringPi/wiringPi.h | 4 +- 27 files changed, 726 insertions(+), 157 deletions(-) create mode 100644 examples/blink.c create mode 100644 examples/blink.rtb create mode 100644 examples/blink.sh create mode 100644 examples/header.h create mode 100644 examples/isr-osc.c diff --git a/examples/Makefile b/examples/Makefile index e1d29a0..defd510 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -35,27 +35,26 @@ LDLIBS = -lwiringPi -lpthread -lm # Should not alter anything below this line ############################################################################### -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 \ +SRC = blink.c test1.c test2.c speed.c lcd.c wfi.c isr.c isr-osc.c \ + piface.c gertboard.c nes.c \ + pwm.c tone.c servo.c \ delayTest.c serialRead.c serialTest.c okLed.c OBJ = $(SRC:.c=.o) BINS = $(SRC:.c=) -# Note: -# Please don't waste your time by emailling me or doing a -# pull request with changes to make all these targets. It -# is intentional that I do it this way as it now takes too -# long to compile them all and most people will not run -# them anyway... -GH- - all: @cat README.TXT @echo " $(BINS)" | fmt @echo "" +really-all: $(BINS) + +blink: blink.o + @echo [link] + @$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) + test1: test1.o @echo [link] @$(CC) -o $@ test1.o $(LDFLAGS) $(LDLIBS) @@ -80,6 +79,10 @@ isr: isr.o @echo [link] @$(CC) -o $@ isr.o $(LDFLAGS) $(LDLIBS) +isr-osc: isr-osc.o + @echo [link] + @$(CC) -o $@ isr-osc.o $(LDFLAGS) $(LDLIBS) + piface: piface.o @echo [link] @$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS) diff --git a/examples/README.TXT b/examples/README.TXT index 2bf6f1e..33263b1 100644 --- a/examples/README.TXT +++ b/examples/README.TXT @@ -10,5 +10,9 @@ To compile an individual example, just type make exampleName -Where exampleName is one of: +To really compile everything: + + make really-all + +The individual tests are: diff --git a/examples/blink.c b/examples/blink.c new file mode 100644 index 0000000..bb9f856 --- /dev/null +++ b/examples/blink.c @@ -0,0 +1,50 @@ +/* + * blink.c: + * Standard "blink" program in wiringPi. Blinks an LED connected + * to the first GPIO pin. + * + * Copyright (c) 2012-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 . + *********************************************************************** + */ + +#include +#include + +// LED Pin - wiringPi pin 0 is BCM_GPIO 17. + +#define LED 0 + +int main (void) +{ + printf ("Raspberry Pi blink\n") ; + + if (wiringPiSetup () == -1) + return 1 ; + + pinMode (LED, OUTPUT) ; + + for (;;) + { + digitalWrite (LED, 1) ; // On + delay (500) ; // mS + digitalWrite (LED, 0) ; // Off + delay (500) ; + } + return 0 ; +} diff --git a/examples/blink.rtb b/examples/blink.rtb new file mode 100644 index 0000000..eb7d26c --- /dev/null +++ b/examples/blink.rtb @@ -0,0 +1,30 @@ +// blink.rtb: +// Blink program in Return to Basic +// +// Copyright (c) 2012-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 . + *********************************************************************** +// +PinMode (0, 1) // Output +CYCLE + DigitalWrite (0, 1) // Pin 0 ON + WAIT (0.5) // 0.5 seconds + DigitalWrite (0, 0) + WAIT (0.5) +REPEAT +END diff --git a/examples/blink.sh b/examples/blink.sh new file mode 100644 index 0000000..2aa378a --- /dev/null +++ b/examples/blink.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# +# blink.sh: +# Standard "blink" program in wiringPi. Blinks an LED connected +# to the first GPIO pin. +# +# Copyright (c) 2012-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 . +####################################################################### + +# LED Pin - wiringPi pin 0 is BCM_GPIO 17. + +LED=0 + +gpio mode $PIN out + +while true; do + gpio write $PIN 1 + sleep 0.5 + gpio write $PIN 0 + sleep 0.5 +done diff --git a/examples/delayTest.c b/examples/delayTest.c index d05f3ff..4c8b6ca 100644 --- a/examples/delayTest.c +++ b/examples/delayTest.c @@ -1,3 +1,27 @@ +/* + * delayTest.c: + * Just a little test program I'm using to experiment with + * various timings and latency, etc. + * + * Copyright (c) 2012-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 . + *********************************************************************** + */ #include #include diff --git a/examples/gertboard.c b/examples/gertboard.c index 8f26dd4..f02e27d 100644 --- a/examples/gertboard.c +++ b/examples/gertboard.c @@ -1,4 +1,3 @@ - /* * gertboard.c: * Simple test for the SPI bus on the Gertboard @@ -10,6 +9,24 @@ * copy this value to D/A port 1 and use a 'scope on both D/A ports * to check all's well. * + * Copyright (c) 2012-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 . + *********************************************************************** */ #include diff --git a/examples/header.h b/examples/header.h new file mode 100644 index 0000000..82f723d --- /dev/null +++ b/examples/header.h @@ -0,0 +1,23 @@ +/* + * file.c: + * + * Copyright (c) 2012-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 . + *********************************************************************** + */ + diff --git a/examples/isr-osc.c b/examples/isr-osc.c new file mode 100644 index 0000000..a872ee3 --- /dev/null +++ b/examples/isr-osc.c @@ -0,0 +1,118 @@ +/* + * isr-osc.c: + * Wait for Interrupt test program - ISR method - interrupt oscillator + * + * How to test: + * + * IMPORTANT: To run this test we connect 2 GPIO pins together, but + * before we do that YOU must make sure that they are both setup + * the right way. If they are set to outputs and one is high and one low, + * then you connect the wire, you'll create a short and that won't be good. + * + * Before making the connection, type: + * gpio mode 0 output + * gpio write 0 0 + * gpio mode 1 input + * then you can connect them together. + * + * Run the program, then: + * gpio write 0 1 + * gpio write 0 0 + * + * at which point it will trigger an interrupt and the program will + * then do the up/down toggling for itself and run at full speed, and + * it will report the number of interrupts recieved every second. + * + * Copyright (c) 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 . + *********************************************************************** + */ + +#include +#include +#include +#include +#include + + +// What GPIO input are we using? +// This is a wiringPi pin number + +#define OUT_PIN 0 +#define IN_PIN 1 + +// 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) +{ + digitalWrite (OUT_PIN, 1) ; + ++globalCounter ; + digitalWrite (OUT_PIN, 0) ; +} + + +/* + ********************************************************************************* + * main + ********************************************************************************* + */ + +int main (void) +{ + int myCounter = 0 ; + int lastCounter = 0 ; + + if (wiringPiSetup () < 0) + { + fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ; + return 1 ; + } + + pinMode (OUT_PIN, OUTPUT) ; + pinMode (IN_PIN, INPUT) ; + + if (wiringPiISR (IN_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 (1000) ; + + printf (" Done. counter: %6d: %6d\n", + globalCounter, myCounter - lastCounter) ; + lastCounter = myCounter ; + myCounter = globalCounter ; + } + + return 0 ; +} diff --git a/examples/nes.c b/examples/nes.c index 1a485bd..31908e8 100644 --- a/examples/nes.c +++ b/examples/nes.c @@ -1,3 +1,26 @@ +/* + * nes.c: + * Test program for an old NES controller connected to the Pi. + * + * Copyright (c) 2012-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 . + *********************************************************************** + */ #include #include diff --git a/examples/okLed.c b/examples/okLed.c index 02f0b22..9b3a170 100644 --- a/examples/okLed.c +++ b/examples/okLed.c @@ -1,7 +1,6 @@ /* - * okLed: + * okLed.c: * 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 @@ -10,6 +9,24 @@ * e.g. by putting it in /etc/rc.local and running it in the * background & * + * Copyright (c) 2012-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 . + *********************************************************************** */ #include diff --git a/examples/piface.c b/examples/piface.c index 3305bf9..0f00960 100644 --- a/examples/piface.c +++ b/examples/piface.c @@ -1,9 +1,27 @@ - /* - * piface.c: - * Simple test for the PiFace + * piFace.c: + * Simple test for the PiFace interface board. * * Read the buttons and output the same to the LEDs + * + * Copyright (c) 2012-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 . + *********************************************************************** */ #include diff --git a/examples/pwm.c b/examples/pwm.c index 09b4ae0..c1fc331 100644 --- a/examples/pwm.c +++ b/examples/pwm.c @@ -1,3 +1,27 @@ +/* + * pwm.c: + * Test of the software PWM driver. Needs 12 LEDs connected + * to the Pi. + * + * Copyright (c) 2012-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 . + *********************************************************************** + */ #include #include diff --git a/examples/serialRead.c b/examples/serialRead.c index 34b9bad..9ee11ac 100644 --- a/examples/serialRead.c +++ b/examples/serialRead.c @@ -1,8 +1,25 @@ - /* - * serialRead.c: + * serial.c: * Example program to read bytes from the Serial line * + * Copyright (c) 2012-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 . + *********************************************************************** */ #include diff --git a/examples/serialTest.c b/examples/serialTest.c index 85a1a66..0d6da5f 100644 --- a/examples/serialTest.c +++ b/examples/serialTest.c @@ -3,6 +3,24 @@ * Very simple program to test the serial port. Expects * the port to be looped back to itself * + * Copyright (c) 2012-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 . + *********************************************************************** */ #include diff --git a/examples/servo.c b/examples/servo.c index 0237832..aa1ab05 100644 --- a/examples/servo.c +++ b/examples/servo.c @@ -1,3 +1,27 @@ +/* + * servo.c: + * Test of the softServo code. + * Do not use this code - use the servoBlaster kernel module instead + * + * Copyright (c) 2012-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 . + *********************************************************************** + */ #include #include diff --git a/examples/speed.c b/examples/speed.c index 2f5d990..863317e 100644 --- a/examples/speed.c +++ b/examples/speed.c @@ -1,8 +1,26 @@ - /* * speed.c: * Simple program to measure the speed of the various GPIO * access mechanisms. + * + * Copyright (c) 2012-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 . + *********************************************************************** */ #include diff --git a/examples/test1.c b/examples/test1.c index 7eb0abd..4c75711 100644 --- a/examples/test1.c +++ b/examples/test1.c @@ -1,7 +1,27 @@ - /* * test1.c: * Simple test program to test the wiringPi functions + * This is a sequencer to make a patter appear on 8 LEDs + * connected to the GPIO pins. + * + * Copyright (c) 2012-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 . + *********************************************************************** */ #include diff --git a/examples/test2.c b/examples/test2.c index e34013c..580591e 100644 --- a/examples/test2.c +++ b/examples/test2.c @@ -1,8 +1,25 @@ - /* * test2.c: - * Simple test program to test the wiringPi functions - * PWM test + * This tests the hardware PWM channel. + * + * Copyright (c) 2012-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 . + *********************************************************************** */ #include diff --git a/examples/tone.c b/examples/tone.c index 8b1fcd7..0e8a47d 100644 --- a/examples/tone.c +++ b/examples/tone.c @@ -1,3 +1,27 @@ +/* + * tone.c: + * Test of the softTone module in wiringPi + * Plays a scale out on pin 3 - connect pizeo disc to pin 3 & 0v + * + * Copyright (c) 2012-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 . + *********************************************************************** + */ #include #include @@ -6,15 +30,13 @@ #include #include -#define RANGE 100 -#define NUM_LEDS 12 +#define PIN 3 int scale [8] = { 262, 294, 330, 349, 392, 440, 494, 525 } ; int main () { - int i, j ; - char buf [80] ; + int i ; if (wiringPiSetup () == -1) { @@ -22,14 +44,14 @@ int main () return 1 ; } - softToneCreate (3) ; + softToneCreate (PIN) ; for (;;) { for (i = 0 ; i < 8 ; ++i) { printf ("%3d\n", i) ; - softToneWrite (3, scale [i]) ; + softToneWrite (PIN, scale [i]) ; delay (500) ; } } diff --git a/gpio/Makefile b/gpio/Makefile index 623096c..a043962 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -70,8 +70,8 @@ install: .PHONEY: uninstall uninstall: @echo "[UnInstall]" - rm -f /usr/local/bin/gpio - rm -f /usr/local/man/man1/gpio.1 + @rm -f /usr/local/bin/gpio + @rm -f /usr/local/man/man1/gpio.1 .PHONEY: depend depend: diff --git a/gpio/gpio.1 b/gpio/gpio.1 index a83cf9f..e816e22 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -38,7 +38,7 @@ group value range .PP .B gpio -.B load \ i2c/spi +.B load \ i2c/spi ... .PP .B gpio .B gbr @@ -168,9 +168,18 @@ Change the PWM mode to balanced (the default) or mark:space ratio (traditional) Change the PWM range register. The default is 1024. .TP -.B load i2c/spi -This loads the i2c or the spi drivers into the system and changes the permissions on -the associated /dev/ entries so that the current user has access to them. +.B load i2c [baudrate] +This loads the i2c or drivers into the kernel and changes the permissions +on the associated /dev/ entries so that the current user has access to +them. Optionally it will set the I2C baudrate to that supplied (or as +close as the Pi can manage) The default speed is 100Kb/sec. + +.TP +.B load spi [buffer size in KB] +This loads the the spi drivers into the kernel and changes the permissions +on the associated /dev/ entries so that the current user has access to +them. Optionally it will set the SPI buffer size to that supplied. The +default is 4KB. .TP .B gbr @@ -275,4 +284,5 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. .SH TRADEMARKS AND ACKNOWLEDGEMENTS -Raspberry Pi is a trademark of the Raspberry Pi Foundation. +Raspberry Pi is a trademark of the Raspberry Pi Foundation. See +http://raspberrypi.org/ for full details. diff --git a/gpio/gpio.c b/gpio/gpio.c index 326dd2d..5eef5d8 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -42,7 +42,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "1.8" +#define VERSION "1.10" static int wpMode ; @@ -129,7 +129,7 @@ static int moduleLoaded (char *modName) static void _doLoadUsage (char *argv []) { - fprintf (stderr, "Usage: %s load [bufferSize in KB for spi]\n", argv [0]) ; + fprintf (stderr, "Usage: %s load [SPI bufferSize in KB | I2C baudrate in Kb/sec]\n", argv [0]) ; exit (1) ; } @@ -138,12 +138,12 @@ static void doLoad (int argc, char *argv []) char *module1, *module2 ; char cmd [80] ; char *file1, *file2 ; - char spiBuf [32] ; + char args1 [32], args2 [32] ; if (argc < 3) _doLoadUsage (argv) ; - spiBuf [0] = 0 ; + args1 [0] = args2 [0] = 0 ; /**/ if (strcasecmp (argv [2], "spi") == 0) { @@ -152,10 +152,9 @@ static void doLoad (int argc, char *argv []) file1 = "/dev/spidev0.0" ; file2 = "/dev/spidev0.1" ; if (argc == 4) - sprintf (spiBuf, " bufsize=%d", atoi (argv [3]) * 1024) ; + sprintf (args1, " bufsize=%d", atoi (argv [3]) * 1024) ; else if (argc > 4) _doLoadUsage (argv) ; - } else if (strcasecmp (argv [2], "i2c") == 0) { @@ -163,19 +162,23 @@ static void doLoad (int argc, char *argv []) module2 = "i2c_bcm2708" ; file1 = "/dev/i2c-0" ; file2 = "/dev/i2c-1" ; + if (argc == 4) + sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ; + else if (argc > 4) + _doLoadUsage (argv) ; } else _doLoadUsage (argv) ; if (!moduleLoaded (module1)) { - sprintf (cmd, "modprobe %s%s", module1, spiBuf) ; + sprintf (cmd, "modprobe %s%s", module1, args1) ; system (cmd) ; } if (!moduleLoaded (module2)) { - sprintf (cmd, "modprobe %s", module2) ; + sprintf (cmd, "modprobe %s%s", module2, args2) ; system (cmd) ; } @@ -200,55 +203,39 @@ static void doLoad (int argc, char *argv []) static char *pinNames [] = { - "GPIO 0", - "GPIO 1", - "GPIO 2", - "GPIO 3", - "GPIO 4", - "GPIO 5", - "GPIO 6", - "GPIO 7", - "SDA ", - "SCL ", - "CE0 ", - "CE1 ", - "MOSI ", - "MISO ", - "SCLK ", - "TxD ", - "RxD ", - "GPIO 8", - "GPIO 9", - "GPIO10", - "GPIO11", + "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7", + "SDA ", "SCL ", + "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ", + "TxD ", "RxD ", + "GPIO 8", "GPIO 9", "GPIO10", "GPIO11", +} ; + +static char *alts [] = +{ + "IN ", "OUT ", "ALT0", "ALT1", "ALT2", "ALT3", "ALT4", "ALT5", "XXXX" } ; static void doReadall (void) { int pin ; - printf ("+----------+------+--------+-------+\n") ; - printf ("| wiringPi | GPIO | Name | Value |\n") ; - printf ("+----------+------+--------+-------+\n") ; + printf ("+----------+------+--------+------+------+\n") ; + printf ("| wiringPi | GPIO | Name | Mode | Value |\n") ; + printf ("+----------+------+--------+------+------+\n") ; - for (pin = 0 ; pin < NUM_PINS ; ++pin) - printf ("| %6d | %3d | %s | %s |\n", - pin, wpiPinToGpio (pin), - pinNames [pin], - digitalRead (pin) == HIGH ? "High" : "Low ") ; - - printf ("+----------+------+--------+-------+\n") ; - - if (piBoardRev () == 1) - return ; + for (pin = 0 ; pin < 64 ; ++pin) + { + if (wpiPinToGpio (pin) == -1) + continue ; - for (pin = 17 ; pin <= 20 ; ++pin) - printf ("| %6d | %3d | %s | %s |\n", + printf ("| %6d | %3d | %s | %s | %s |\n", pin, wpiPinToGpio (pin), pinNames [pin], + alts [getAlt (pin)], digitalRead (pin) == HIGH ? "High" : "Low ") ; + } - printf ("+----------+------+--------+-------+\n") ; + printf ("+----------+------+--------+------+------+\n") ; } diff --git a/wiringPi/lcd.c b/wiringPi/lcd.c index aa58cab..f123db2 100644 --- a/wiringPi/lcd.c +++ b/wiringPi/lcd.c @@ -174,6 +174,18 @@ void lcdClear (int fd) } +/* + * lcdSendCommand: + * Send any arbitary command to the display + ********************************************************************************* + */ + +void lcdSendCommand (int fd, uint8_t command) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + putCommand (lcd, command) ; +} + /* * lcdPosition: * Update the position of the cursor on the display diff --git a/wiringPi/lcd.h b/wiringPi/lcd.h index ecd1d25..beebb75 100644 --- a/wiringPi/lcd.h +++ b/wiringPi/lcd.h @@ -30,12 +30,13 @@ extern "C" { #endif -extern void lcdHome (int fd) ; -extern void lcdClear (int fd) ; -extern void lcdPosition (int fd, int x, int y) ; -extern void lcdPutchar (int fd, uint8_t data) ; -extern void lcdPuts (int fd, char *string) ; -extern void lcdPrintf (int fd, char *message, ...) ; +extern void lcdHome (int fd) ; +extern void lcdClear (int fd) ; +extern void lcdSendCommand (int fd, uint8_t command) ; +extern void lcdPosition (int fd, int x, int y) ; +extern void lcdPutchar (int fd, uint8_t data) ; +extern void lcdPuts (int fd, char *string) ; +extern void lcdPrintf (int fd, char *message, ...) ; extern int lcdInit (int rows, int cols, int bits, int rs, int strb, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 9655db2..bb22de6 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -51,9 +51,6 @@ // Added in the 2 UART pins // Change maxPins to numPins to more accurately reflect purpose -// Pad drive current fiddling - -#undef DEBUG_PADS #include #include @@ -68,15 +65,16 @@ #include #include #include -#include #include #include +#include #include "wiringPi.h" // Function stubs void (*pinMode) (int pin, int mode) ; +int (*getAlt) (int pin) ; void (*pullUpDnControl) (int pin, int pud) ; void (*digitalWrite) (int pin, int value) ; void (*digitalWriteByte) (int value) ; @@ -84,7 +82,6 @@ void (*pwmWrite) (int pin, int value) ; void (*setPadDrive) (int group, int value) ; int (*digitalRead) (int pin) ; int (*waitForInterrupt) (int pin, int mS) ; -void (*delayMicroseconds) (unsigned int howLong) ; void (*pwmSetMode) (int mode) ; void (*pwmSetRange) (unsigned int range) ; void (*pwmSetClock) (int divisor) ; @@ -177,7 +174,7 @@ static volatile uint32_t *timerIrqRaw ; // Time for easy calculations -static unsigned long long epoch ; +static uint64_t epochMilli, epochMicro ; // Misc @@ -586,6 +583,38 @@ void pinModeSys (int pin, int mode) } +/* + * getAlt: + * Returns the ALT bits for a given port. Only really of-use + * for the gpio readall command (I think) + ********************************************************************************* + */ + +int getAltGpio (int pin) +{ + int fSel, shift, alt ; + + pin &= 63 ; + + fSel = gpioToGPFSEL [pin] ; + shift = gpioToShift [pin] ; + + alt = (*(gpio + fSel) >> shift) & 7 ; + + return alt ; +} + +int getAltWPi (int pin) +{ + return getAltGpio (pinToGpio [pin & 63]) ; +} + +int getAltSys (int pin) +{ + return 0 ; +} + + /* * pwmControl: * Allow the user to control some of the PWM functions @@ -627,7 +656,7 @@ void pwmSetRangeSys (unsigned int range) void pwmSetClockWPi (int divisor) { - unsigned int pwm_control ; + uint32_t pwm_control ; divisor &= 4095 ; if (wiringPiDebug) @@ -811,10 +840,11 @@ void setPadDriveWPi (int group, int value) wrVal = BCM_PASSWORD | 0x18 | (value & 7) ; *(pads + group + 11) = wrVal ; -#ifdef DEBUG_PADS - printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ; - printf ("Read : %08X\n", *(pads + group + 11)) ; -#endif + if (wiringPiDebug) + { + printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ; + printf ("Read : %08X\n", *(pads + group + 11)) ; + } } void setPadDriveGpio (int group, int value) @@ -913,22 +943,12 @@ void pullUpDnControlSys (int pin, int pud) int waitForInterruptSys (int pin, int mS) { int fd, x ; - char buf [8] ; + uint8_t c ; struct pollfd polls ; if ((fd = sysFds [pin & 63]) == -1) return -2 ; -// Do a dummy read - - x = read (fd, buf, 6) ; - if (x < 0) - return x ; - -// And seek - - lseek (fd, 0, SEEK_SET) ; - // Setup poll structure polls.fd = fd ; @@ -936,7 +956,14 @@ int waitForInterruptSys (int pin, int mS) // Wait for it ... - return poll (&polls, 1, mS) ; + x = poll (&polls, 1, mS) ; + +// Do a dummy read to clear the interrupt +// A one character read appars to be enough. + + (void)read (fd, &c, 1) ; + + return x ; } int waitForInterruptWPi (int pin, int mS) @@ -986,6 +1013,8 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) char *modeS ; char pinS [8] ; pid_t pid ; + int count, i ; + uint8_t c ; pin &= 63 ; @@ -1027,12 +1056,18 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) } // Now pre-open the /sys/class node - it may already be open if -// we had set it up earlier, but this will do no harm. +// we are in Sys mode, but this will do no harm. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; if ((sysFds [pin] = open (fName, O_RDWR)) < 0) return -1 ; +// Clear any initial pending interrupt + + ioctl (sysFds [pin], FIONREAD, &count) ; + for (i = 0 ; i < count ; ++i) + read (sysFds [pin], &c, 1) ; + isrFunctions [pin] = function ; pthread_create (&threadId, NULL, interruptHandler, &pin) ; @@ -1043,6 +1078,22 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) } +/* + * initialiseEpoch: + * Initialise our start-of-time variable to be the current unix + * time in milliseconds. + ********************************************************************************* + */ + +static void initialiseEpoch (void) +{ + struct timeval tv ; + + gettimeofday (&tv, NULL) ; + epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ; + epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ; +} + /* * delay: * Wait for some number of milli seconds @@ -1078,28 +1129,8 @@ void delay (unsigned int howLong) ********************************************************************************* */ -void delayMicrosecondsSys (unsigned int howLong) -{ - struct timespec sleeper, dummy ; - - sleeper.tv_sec = 0 ; - sleeper.tv_nsec = (long)(howLong * 1000) ; - - nanosleep (&sleeper, &dummy) ; -} - void delayMicrosecondsHard (unsigned int howLong) { -#ifdef HARD_TIMER - volatile unsigned int dummy ; - - *(timer + TIMER_LOAD) = howLong ; - *(timer + TIMER_IRQ_CLR) = 0 ; - - dummy = *timerIrqRaw ; - while (dummy == 0) - dummy = *timerIrqRaw ; -#else struct timeval tNow, tLong, tEnd ; gettimeofday (&tNow, NULL) ; @@ -1109,10 +1140,9 @@ void delayMicrosecondsHard (unsigned int howLong) while (timercmp (&tNow, &tEnd, <)) gettimeofday (&tNow, NULL) ; -#endif } -void delayMicrosecondsWPi (unsigned int howLong) +void delayMicroseconds (unsigned int howLong) { struct timespec sleeper ; @@ -1138,13 +1168,30 @@ void delayMicrosecondsWPi (unsigned int howLong) unsigned int millis (void) { struct timeval tv ; - unsigned long long t1 ; + uint64_t now ; gettimeofday (&tv, NULL) ; + now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ; - t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + return (uint32_t)(now - epochMilli) ; +} + + +/* + * micros: + * Return a number of microseconds as an unsigned int. + ********************************************************************************* + */ - return (uint32_t)(t1 - epoch) ; +unsigned int micros (void) +{ + struct timeval tv ; + uint64_t now ; + + gettimeofday (&tv, NULL) ; + now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ; + + return (uint32_t)(now - epochMicro) ; } @@ -1161,8 +1208,6 @@ int wiringPiSetup (void) { int fd ; int boardRev ; - //uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ; - struct timeval tv ; if (geteuid () != 0) { @@ -1180,6 +1225,7 @@ int wiringPiSetup (void) printf ("wiringPi: wiringPiSetup called\n") ; pinMode = pinModeWPi ; + getAlt = getAltWPi ; pullUpDnControl = pullUpDnControlWPi ; digitalWrite = digitalWriteWPi ; digitalWriteByte = digitalWriteByteGpio ; // Same code @@ -1187,7 +1233,6 @@ int wiringPiSetup (void) setPadDrive = setPadDriveWPi ; digitalRead = digitalReadWPi ; waitForInterrupt = waitForInterruptWPi ; - delayMicroseconds = delayMicrosecondsWPi ; pwmSetMode = pwmSetModeWPi ; pwmSetRange = pwmSetRangeWPi ; pwmSetClock = pwmSetClockWPi ; @@ -1268,11 +1313,6 @@ int wiringPiSetup (void) return -1 ; } -#ifdef DEBUG_PADS - printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ; - printf (" -> %08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ; -#endif - // The system timer timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ; @@ -1295,10 +1335,7 @@ int wiringPiSetup (void) *(timer + TIMER_PRE_DIV) = 0x00000F9 ; timerIrqRaw = timer + TIMER_IRQ_RAW ; -// Initialise our epoch for millis() - - gettimeofday (&tv, NULL) ; - epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + initialiseEpoch () ; wiringPiMode = WPI_MODE_PINS ; @@ -1332,6 +1369,7 @@ int wiringPiSetupGpio (void) printf ("wiringPi: wiringPiSetupGpio called\n") ; pinMode = pinModeGpio ; + getAlt = getAltGpio ; pullUpDnControl = pullUpDnControlGpio ; digitalWrite = digitalWriteGpio ; digitalWriteByte = digitalWriteByteGpio ; @@ -1339,7 +1377,6 @@ int wiringPiSetupGpio (void) setPadDrive = setPadDriveGpio ; digitalRead = digitalReadGpio ; waitForInterrupt = waitForInterruptGpio ; - delayMicroseconds = delayMicrosecondsWPi ; // Same pwmSetMode = pwmSetModeWPi ; pwmSetRange = pwmSetRangeWPi ; pwmSetClock = pwmSetClockWPi ; @@ -1363,7 +1400,6 @@ int wiringPiSetupSys (void) { int boardRev ; int pin ; - struct timeval tv ; char fName [128] ; if (getenv ("WIRINGPI_DEBUG") != NULL) @@ -1373,6 +1409,7 @@ int wiringPiSetupSys (void) printf ("wiringPi: wiringPiSetupSys called\n") ; pinMode = pinModeSys ; + getAlt = getAltSys ; pullUpDnControl = pullUpDnControlSys ; digitalWrite = digitalWriteSys ; digitalWriteByte = digitalWriteByteSys ; @@ -1380,7 +1417,6 @@ int wiringPiSetupSys (void) setPadDrive = setPadDriveSys ; digitalRead = digitalReadSys ; waitForInterrupt = waitForInterruptSys ; - delayMicroseconds = delayMicrosecondsSys ; pwmSetMode = pwmSetModeSys ; pwmSetRange = pwmSetRangeSys ; pwmSetClock = pwmSetClockSys ; @@ -1401,10 +1437,7 @@ int wiringPiSetupSys (void) sysFds [pin] = open (fName, O_RDWR) ; } -// Initialise the epoch for mills() ... - - gettimeofday (&tv, NULL) ; - epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + initialiseEpoch () ; wiringPiMode = WPI_MODE_GPIO_SYS ; diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 7626d28..47d8cc5 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -81,13 +81,13 @@ extern int wpiPinToGpio (int wpiPin) ; extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only extern void (*pinMode) (int pin, int mode) ; +extern int (*getAlt) (int pin) ; extern void (*pullUpDnControl) (int pin, int pud) ; extern void (*digitalWrite) (int pin, int value) ; extern void (*digitalWriteByte) (int value) ; extern void (*pwmWrite) (int pin, int value) ; extern void (*setPadDrive) (int group, int value) ; extern int (*digitalRead) (int pin) ; -extern void (*delayMicroseconds) (unsigned int howLong) ; extern void (*pwmSetMode) (int mode) ; extern void (*pwmSetRange) (unsigned int range) ; extern void (*pwmSetClock) (int divisor) ; @@ -111,7 +111,9 @@ extern int piHiPri (int pri) ; // Extras from arduino land extern void delay (unsigned int howLong) ; +extern void delayMicroseconds (unsigned int howLong) ; extern unsigned int millis (void) ; +extern unsigned int micros (void) ; #ifdef __cplusplus } -- 2.30.2