diff --git a/COPYING.LESSER b/COPYING.LESSER new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/COPYING.LESSER @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/examples/COPYING.LESSER b/examples/COPYING.LESSER new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/examples/COPYING.LESSER @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/examples/Makefile b/examples/Makefile index 58a30cf..df00d79 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -9,16 +9,16 @@ # Wiring Compatable library for the Raspberry Pi # # wiringPi is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by +# 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 General Public License for more details. +# GNU Lesser General Public License for more details. # -# You should have received a copy of the GNU General Public License +# You should have received a copy of the GNU Lesser General Public License # along with wiringPi. If not, see . ################################################################################# @@ -35,11 +35,11 @@ LIBS = -lwiringPi # Should not alter anything below this line ############################################################################### -SRC = test1.c test2.c speed.c lcd.c +SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c -OBJ = test1.o test2.o speed.o lcd.o +OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o -all: test1 test2 speed lcd +all: test1 test2 speed lcd wfi piface test1: test1.o @echo [link] @@ -57,13 +57,21 @@ lcd: lcd.o @echo [link] $(CC) -o $@ lcd.o $(LDFLAGS) $(LIBS) +wfi: wfi.o + @echo [link] + $(CC) -o $@ wfi.o $(LDFLAGS) $(LIBS) -lpthread + +piface: piface.o + @echo [link] + $(CC) -o $@ piface.o $(LDFLAGS) $(LIBS) -lpthread + .c.o: @echo [CC] $< @$(CC) -c $(CFLAGS) $< -o $@ clean: - rm -f $(OBJ) *~ core tags test1 test2 speed lcd + rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface tags: $(SRC) @echo [ctags] diff --git a/examples/lcd.c b/examples/lcd.c index 2cabea7..6024917 100644 --- a/examples/lcd.c +++ b/examples/lcd.c @@ -10,16 +10,16 @@ * 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 General Public License as published by + * 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 General Public License for more details. + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License + * You should have received a copy of the GNU Lesser General Public License * along with wiringPi. If not, see . *********************************************************************** */ diff --git a/examples/piface.c b/examples/piface.c new file mode 100644 index 0000000..3305bf9 --- /dev/null +++ b/examples/piface.c @@ -0,0 +1,53 @@ + +/* + * piface.c: + * Simple test for the PiFace + * + * Read the buttons and output the same to the LEDs + */ + +#include + +#include +#include +#include + +int outputs [4] = { 0,0,0,0 } ; + +void scanButton (int button) +{ + if (digitalRead (button) == LOW) + { + outputs [button] ^= 1 ; + digitalWrite (button, outputs [button]) ; + } + + while (digitalRead (button) == LOW) + delay (1) ; +} + + +int main (void) +{ + int pin, button ; + + printf ("Raspberry Pi wiringPiFace test program\n") ; + + if (wiringPiSetupPiFace () == -1) + exit (1) ; + +// Enable internal pull-ups + + for (pin = 0 ; pin < 8 ; ++pin) + pullUpDnControl (pin, PUD_UP) ; + + + for (;;) + { + for (button = 0 ; button < 4 ; ++button) + scanButton (button) ; + delay (1) ; + } + + return 0 ; +} diff --git a/examples/speed.c b/examples/speed.c index 409c994..2f5d990 100644 --- a/examples/speed.c +++ b/examples/speed.c @@ -52,7 +52,8 @@ int main (void) printf ("Native GPIO method: (%8d iterations)\n", FAST_COUNT) ; - wiringPiGpioMode (WPI_MODE_GPIO) ; + if (wiringPiSetupGpio () == -1) + exit (1) ; pinMode (17, OUTPUT) ; @@ -77,7 +78,8 @@ int main (void) // Switch to SYS mode: - wiringPiSetupSys () ; + if (wiringPiSetupSys () == -1) + exit (1) ; printf ("/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ; diff --git a/examples/wfi.c b/examples/wfi.c new file mode 100644 index 0000000..9efcc2c --- /dev/null +++ b/examples/wfi.c @@ -0,0 +1,158 @@ +/* + * wfi.c: + * Wait for Interrupt test program + * + * Copyright (c) 2012 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 +#include + +// A 'key' which we can lock and unlock - values are 0 through 3 +// This is interpreted internally as a pthread_mutex by wiringPi +// which is hiding some of that to make life simple. + +#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 + +// Debounce time in mS + +#define DEBOUNCE_TIME 100 + + +// globalCounter: +// Global variable to count interrupts +// Should be declared volatile to make sure the compiler doesn't cache it. + +static volatile int globalCounter = 0 ; + + +/* + * waitForIt: + * This is a thread created using the wiringPi simplified threading + * mechanism. It will wait on an interrupt on the button and increment + * a counter. + ********************************************************************************* + */ + +PI_THREAD (waitForIt) +{ + int state = 0 ; + 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) + { + debounceTime = millis () + DEBOUNCE_TIME ; + continue ; + } + +// We have a valid one + + digitalWrite (17, state) ; + state ^= 1 ; + + piLock (COUNT_KEY) ; + ++globalCounter ; + piUnlock (COUNT_KEY) ; + +// Wait for key to be released + + while (digitalRead (0) == LOW) + delay (1) ; + + debounceTime = millis () + DEBOUNCE_TIME ; + } + } +} + + +/* + * setup: + * Demo a crude but effective way to initialise the hardware + ********************************************************************************* + */ + +void setup (void) +{ + +// Use the gpio program to initialise the hardware +// (This is the crude, but effective bit) + + system ("gpio edge 0 falling") ; + system ("gpio export 17 out") ; + system ("gpio export 18 out") ; + +// Setup wiringPi + + wiringPiSetupSys () ; + +// Fire off our interrupt handler + + piThreadCreate (waitForIt) ; + + digitalWrite (17, 0) ; +} + + +/* + * main + ********************************************************************************* + */ + +int main (void) +{ + int lastCounter = 0 ; + int myCounter = 0 ; + + setup () ; + + for (;;) + { + printf ("Waiting ... ") ; fflush (stdout) ; + + while (myCounter == lastCounter) + { + piLock (COUNT_KEY) ; + myCounter = globalCounter ; + piUnlock (COUNT_KEY) ; + delay (5000) ; + } + + printf (" Done. myCounter: %5d\n", myCounter) ; + lastCounter = myCounter ; + } + + return 0 ; +} diff --git a/gpio/COPYING.LESSER b/gpio/COPYING.LESSER new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/gpio/COPYING.LESSER @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/gpio/Makefile b/gpio/Makefile index 043e329..c0740c9 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -9,16 +9,16 @@ # Wiring Compatable library for the Raspberry Pi # # wiringPi is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by +# 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 General Public License for more details. +# GNU Lesser General Public License for more details. # -# You should have received a copy of the GNU General Public License +# You should have received a copy of the GNU Lesser General Public License # along with wiringPi. If not, see . ################################################################################# diff --git a/gpio/gpio.1 b/gpio/gpio.1 index 25c8da5..7da64b2 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -1,15 +1,23 @@ -.TH "GPIO" "14 June 2012" "Command-Line access to Raspberry Pi GPIO" +.TH "GPIO" "14 June 2012" "Command-Line access to Raspberry Pi and PiFace GPIO" .SH NAME -gpio \- Command-line access to Raspberry Pi GPIO +gpio \- Command-line access to Raspberry Pi and PiFace GPIO .SH SYNOPSIS +.TP +.B gpio +.RB [ \-v ] +.TP .B gpio .RB [ \-g ] .RB < read/write/pwm/mode ...> .TP .B gpio -.RB < export/unexport/exports ...> +.RB [ \-p ] +.RB < read/write/mode ...> +.TP +.B gpio +.RB < export/edge/unexport/unexportall/exports ...> .SH DESCRIPTION @@ -19,15 +27,23 @@ on the Raspberry Pi. It's designed for simple testing and diagnostic purposes, but can be used in shell scripts for general if somewhat slow control of the GPIO pins. -Additionally, it can be used to set the exports in the /sys/class/gpio -system directory to allow subsequent programs to use the /sys/class/gpio +Additionally, it can be used to set the exports in the \fI/sys/class/gpio\fR +system directory to allow subsequent programs to use the \fR/sys/class/gpio\fR interface without needing to be run as root. .SH OPTIONS +.TP +.B \-v +Output the current version + .TP .B \-g -Use the BCM_GPIO pins numbers rather than WiringPi pin numbers. +Use the BCM_GPIO pins numbers rather than wiringPi pin numbers. + +.TP +.B \-p +Use the PiFace interface board and its corresponding pin numbers. .TP .B read @@ -44,27 +60,45 @@ Write a PWM value (0-1023) to the given pin. .TP .B mode -Set a pin into input, output or pwm mode. Can also use the literals up, down or tri -to set the internal pull-up, pull-down or tristate controls. +Set a pin into \fIinput\fR, \fIoutput\fR or \fIpwm\fR mode. Can also +use the literals \fIup\fR, \fIdown\fR or \fItri\fR to set the internal +pull-up, pull-down or tristate (off) controls. .TP .B export -Export a GPIO pin in the /sys/class/gpio directory. Use like the mode command above -however only in and out are supported at this time. +Export a GPIO pin in the \fI/sys/class/gpio\fR directory. Use like the +mode command above however only \fIin\fR and \fIout\fR are supported at +this time. Note that the pin number is the \fBBCM_GPIO\fR number and +not the wiringPi number. -Once a GPIO pin has been exported, the -.B gpio -program changes the ownership of the /sys/class/gpio/gpioX/value pseudo file to -that of the user running the -.B gpio -program. This means that you can have a small script of gpio exports to setup -the gpio pins as your program requires without the need to run anything as -root, or with the sudo command. +Once a GPIO pin has been exported, the \fBgpio\fR program changes the +ownership of the \fI/sys/class/gpio/gpioX/value\fR and if present in +later kernels, the \fI/sys/class/gpio/gpioX/edge\fR pseudo files to +that of the user running the \fBgpio\fR program. This means that you +can have a small script of gpio exports to setup the gpio pins as your +program requires without the need to run anything as root, or with the +sudo command. + +.TP +.B edge +This exports a GPIO pin in the \fI/sys/class/gpio\fR directory, set +the direction to input and set the edge interrupt method to \fInone\fR, +\fIrising\fR, \fIfalling\fR or \fIboth\fR. Use like the export command +above and note that \fBBCM_GPIO\fR pin number is used not not wiringPi pin +numbering. + +Like the export commands abovem ownership is set to that of the +calling user, allowing subsequent access from user programs without +requiring root/sudo. .TP .B unexport Un-Export a GPIO pin in the /sys/class/gpio directory. +.TP +.B unexportall +Un-Export all the GPIO pins in the /sys/class/gpio directory. + .TP .B exports Print a list (if any) of all the exported GPIO pins and their current values. @@ -119,9 +153,9 @@ gpio -g read 0 # Read GPIO Pin 0 (SDA0) .SH "NOTES" -When using the export or unexport commands, the pin numbers are -.B always -native GPIO numbers and never wiringPi pin numbers. +When using the \fIexport\fR, \fIedge\fR or \fIunexport\fR commands, the +pin numbers are \fBalways\fR native BCM_GPIO numbers and never wiringPi +pin numbers. .SH "SEE ALSO" diff --git a/gpio/gpio.c b/gpio/gpio.c index bbc4e31..8467b3e 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -7,16 +7,16 @@ * 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 General Public License as published by + * 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 General Public License for more details. + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License + * You should have received a copy of the GNU Lesser General Public License * along with wiringPi. If not, see . *********************************************************************** */ @@ -37,10 +37,10 @@ static int wpMode ; - char *usage = "Usage: gpio -v\n" - " gpio [-g] ...\n" - " gpio ..." ; + " gpio [-g] ...\n" + " gpio [-p] ...\n" + " gpio ..." ; /* @@ -54,7 +54,7 @@ void doExports (void) int fd ; int i, l, first ; char fName [128] ; - char dir, val ; + char buf [16] ; // Rather crude, but who knows what others are up to... @@ -75,19 +75,14 @@ void doExports (void) printf ("%4d: ", i) ; - if ((l = read (fd, &dir, 1)) == 0) - { - printf ("Empty direction file (why?)\n") ; - close (fd) ; - continue ; - } + if ((l = read (fd, buf, 16)) == 0) + sprintf (buf, "%s", "?") ; + + buf [l] = 0 ; + if ((buf [strlen (buf) - 1]) == '\n') + buf [strlen (buf) - 1] = 0 ; - /**/ if (dir == 'o') - printf ("Output ") ; - else if (dir == 'i') - printf ("Input ") ; - else - printf ("Wrong ") ; + printf ("%-3s", buf) ; close (fd) ; @@ -100,19 +95,32 @@ void doExports (void) continue ; } - if ((l = read (fd, &val, 1)) == 0) + if ((l = read (fd, buf, 16)) == 0) + sprintf (buf, "%s", "?") ; + + buf [l] = 0 ; + if ((buf [strlen (buf) - 1]) == '\n') + buf [strlen (buf) - 1] = 0 ; + + printf (" %s", buf) ; + +// Read any edge trigger file + + sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ; + if ((fd = open (fName, O_RDONLY)) == -1) { - printf ("Empty Value file (why?)\n") ; - close (fd) ; + printf ("\n") ; continue ; } - /**/ if (val == '0' ) - printf ("(0)\n") ; - else if (val == '1') - printf ("(1)\n") ; - else - printf ("(?)\n") ; + if ((l = read (fd, buf, 16)) == 0) + sprintf (buf, "%s", "?") ; + + buf [l] = 0 ; + if ((buf [strlen (buf) - 1]) == '\n') + buf [strlen (buf) - 1] = 0 ; + + printf (" %-8s\n", buf) ; close (fd) ; } @@ -161,9 +169,9 @@ void doExport (int argc, char *argv []) exit (1) ; } - /**/ if (strcasecmp (mode, "in") == 0) + /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0)) fprintf (fd, "in\n") ; - else if (strcasecmp (mode, "out") == 0) + else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0)) fprintf (fd, "out\n") ; else { @@ -184,7 +192,113 @@ void doExport (int argc, char *argv []) fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ; exit (1) ; } - + +// Also change ownership of the edge file - if it exists + + sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ; + if (chown (fName, uid, gid) != 0) + { + if (errno != ENOENT) // Silently ignore File not found - older kernel + { + fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ; + exit (1) ; + } + } + +} + + +/* + * doEdge: + * gpio edge pin mode + * Easy access to changing the edge trigger on a GPIO pin + * This uses the /sys/class/gpio device interface. + ********************************************************************************* + */ + +void doEdge (int argc, char *argv []) +{ + FILE *fd ; + int pin ; + char *mode ; + char fName [128] ; + uid_t uid ; + gid_t gid ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + mode = argv [3] ; + +// Export the pin and set direction to input + + if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL) + { + fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ; + exit (1) ; + } + + fprintf (fd, "%d\n", pin) ; + fclose (fd) ; + + sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ; + if ((fd = fopen (fName, "w")) == NULL) + { + fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ; + exit (1) ; + } + + fprintf (fd, "in\n") ; + fclose (fd) ; + + sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ; + if ((fd = fopen (fName, "w")) == NULL) + { + fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ; + exit (1) ; + } + + /**/ if (strcasecmp (mode, "none") == 0) + fprintf (fd, "none\n") ; + else if (strcasecmp (mode, "rising") == 0) + fprintf (fd, "rising\n") ; + else if (strcasecmp (mode, "falling") == 0) + fprintf (fd, "falling\n") ; + else if (strcasecmp (mode, "both") == 0) + fprintf (fd, "both\n") ; + else + { + fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ; + exit (1) ; + } + +// Change ownership so the current user can actually use it! + + uid = getuid () ; + gid = getgid () ; + + sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; + if (chown (fName, uid, gid) != 0) + { + fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ; + exit (1) ; + } + +// Also change ownership of the edge file + + sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ; + if (chown (fName, uid, gid) != 0) + { + fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ; + exit (1) ; + } + + fclose (fd) ; } @@ -218,6 +332,33 @@ void doUnexport (int argc, char *argv []) fclose (fd) ; } + +/* + * doUnexportAll: + * gpio unexportall + * Un-Export all the GPIO pins. + * This uses the /sys/class/gpio device interface. + ********************************************************************************* + */ + +void doUnexportall (int argc, char *argv []) +{ + FILE *fd ; + int pin ; + + for (pin = 0 ; pin < 63 ; ++pin) + { + if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL) + { + fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ; + exit (1) ; + } + fprintf (fd, "%d\n", pin) ; + fclose (fd) ; + } +} + + /* * doMode: * gpio mode pin mode ... @@ -381,11 +522,15 @@ int main (int argc, char *argv []) // Initial test for /sys/class/gpio operations: /**/ if (strcasecmp (argv [1], "exports" ) == 0) - { doExports () ; return 0 ; } + { doExports () ; return 0 ; } else if (strcasecmp (argv [1], "export" ) == 0) - { doExport (argc, argv) ; return 0 ; } + { doExport (argc, argv) ; return 0 ; } + else if (strcasecmp (argv [1], "edge" ) == 0) + { doEdge (argc, argv) ; return 0 ; } + else if (strcasecmp (argv [1], "unexportall") == 0) + { doUnexportall (argc, argv) ; return 0 ; } else if (strcasecmp (argv [1], "unexport") == 0) - { doUnexport (argc, argv) ; return 0 ; } + { doUnexport (argc, argv) ; return 0 ; } // Check for -g argument @@ -402,6 +547,25 @@ int main (int argc, char *argv []) --argc ; wpMode = WPI_MODE_GPIO ; } + +// Check for -p argument for PiFace + + else if (strcasecmp (argv [1], "-p") == 0) + { + if (wiringPiSetupPiFaceForGpioProg () == -1) + { + fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ; + exit (1) ; + } + + for (i = 2 ; i < argc ; ++i) + argv [i - 1] = argv [i] ; + --argc ; + wpMode = WPI_MODE_PIFACE ; + } + +// Default to wiringPi mode + else { if (wiringPiSetup () == -1) diff --git a/wiringPi/COPYING.LESSER b/wiringPi/COPYING.LESSER new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/wiringPi/COPYING.LESSER @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/wiringPi/Makefile b/wiringPi/Makefile index c56a3cf..3c23e96 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -8,16 +8,16 @@ # 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 General Public License as published by +# 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 General Public License for more details. +# GNU Lesser General Public License for more details. # -# You should have received a copy of the GNU General Public License +# You should have received a copy of the GNU Lesser General Public License # along with wiringPi. If not, see . ################################################################################# @@ -35,16 +35,17 @@ LIBS = # Should not alter anything below this line ############################################################################### -SRC = wiringPi.c serial.c wiringShift.c lcd.c +SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c lcd.c piHiPri.c piThread.c -OBJ = wiringPi.o serial.o wiringShift.o lcd.o +OBJ = wiringPi.o wiringPiFace.o wiringSerial.o wiringShift.o lcd.o piHiPri.o piThread.o all: $(TARGET) $(TARGET): $(OBJ) @echo [AR] $(OBJ) @ar rcs $(TARGET) $(OBJ) - @size $(TARGET) + @ranlib $(TARGET) + @size $(TARGET) .c.o: @echo [CC] $< @@ -64,13 +65,16 @@ install: $(TARGET) @echo [install] install -m 0755 -d /usr/local/lib install -m 0755 -d /usr/local/include - install -m 0644 wiringPi.h /usr/local/include - install -m 0644 serial.h /usr/local/include - install -m 0644 wiringShift.h /usr/local/include - install -m 0644 libwiringPi.a /usr/local/lib + install -m 0644 wiringPi.h /usr/local/include + install -m 0644 wiringSerial.h /usr/local/include + install -m 0644 wiringShift.h /usr/local/include + install -m 0644 lcd.h /usr/local/include + install -m 0644 libwiringPi.a /usr/local/lib uninstall: @echo [uninstall] + rm -f /usr/local/include/lcd.h + rm -f /usr/local/include/wiringShift.h rm -f /usr/local/include/wiringPi.h rm -f /usr/local/lib/libwiringPi.a @@ -79,5 +83,9 @@ uninstall: # DO NOT DELETE wiringPi.o: wiringPi.h -serial.o: serial.h +wiringPiFace.o: wiringPi.h +wiringSerial.o: wiringSerial.h wiringShift.o: wiringPi.h wiringShift.h +lcd.o: wiringPi.h lcd.h +piHiPri.o: wiringPi.h +piThread.o: wiringPi.h diff --git a/wiringPi/lcd.c b/wiringPi/lcd.c index dc091bc..6826a60 100644 --- a/wiringPi/lcd.c +++ b/wiringPi/lcd.c @@ -10,16 +10,16 @@ * 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 General Public License as published by + * 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 General Public License for more details. + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License + * You should have received a copy of the GNU Lesser General Public License * along with wiringPi. If not, see . *********************************************************************** */ diff --git a/wiringPi/lcd.h b/wiringPi/lcd.h index 96744f6..094f5f5 100644 --- a/wiringPi/lcd.h +++ b/wiringPi/lcd.h @@ -10,16 +10,16 @@ * 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 General Public License as published by + * 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 General Public License for more details. + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License + * You should have received a copy of the GNU Lesser General Public License * along with wiringPi. If not, see . *********************************************************************** */ diff --git a/wiringPi/piHiPri.c b/wiringPi/piHiPri.c new file mode 100644 index 0000000..e7e06b4 --- /dev/null +++ b/wiringPi/piHiPri.c @@ -0,0 +1,50 @@ +/* + * piHiPri: + * Simple way to get your program running at high priority + * with realtime schedulling. + * + * Copyright (c) 2012 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 + +#include "wiringPi.h" + + +/* + * piHiPri: + * Attempt to set a high priority schedulling for the running program + ********************************************************************************* + */ + +int piHiPri (int pri) +{ + struct sched_param sched ; + + memset (&sched, 0, sizeof(sched)) ; + + if (pri > sched_get_priority_max (SCHED_RR)) + pri = sched_get_priority_max (SCHED_RR) ; + + sched.sched_priority = pri ; + return sched_setscheduler (0, SCHED_RR, &sched) ; +} diff --git a/wiringPi/piThread.c b/wiringPi/piThread.c new file mode 100644 index 0000000..b0499be --- /dev/null +++ b/wiringPi/piThread.c @@ -0,0 +1,63 @@ +/* + * piThread.c: + * Provide a simplified interface to pthreads + * + * Copyright (c) 2012 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 "wiringPi.h" + +static pthread_mutex_t piMutexes [4] ; + + + +/* + * piThreadCreate: + * Create and start a thread + ********************************************************************************* + */ + +int piThreadCreate (void *(*fn)(void *)) +{ + pthread_t myThread ; + + return pthread_create (&myThread, NULL, fn, NULL) ; +} + +/* + * piLock: piUnlock: + * Activate/Deactivate a mutex. + * We're keeping things simple here and only tracking 4 mutexes which + * is more than enough for out entry-level pthread programming + ********************************************************************************* + */ + +void piLock (int key) +{ + pthread_mutex_lock (&piMutexes [key]) ; +} + +void piUnlock (int key) +{ + pthread_mutex_unlock (&piMutexes [key]) ; +} + diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index dcad15e..d99c863 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -11,21 +11,32 @@ * 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 General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * 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 General Public License for more details. + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with wiringPi. If not, see . + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . *********************************************************************** */ // Revisions: +// 19 Jul 2012: +// Moved to the LGPL +// Added an abstraction layer to the main routines to save a tiny +// bit of run-time and make the clode a little cleaner (if a little +// larger) +// Added waitForInterrupt code +// Added piHiPri code +// +// 9 Jul 2012: +// Added in support to use the /sys/class/gpio interface. // 2 Jul 2012: // Fixed a few more bugs to do with range-checking when in GPIO mode. // 11 Jun 2012: @@ -45,6 +56,7 @@ #include #include +#include #include #include #include @@ -55,10 +67,18 @@ #include #include - - #include "wiringPi.h" +// Function stubs + +void (*pinMode) (int pin, int mode) ; +void (*pullUpDnControl) (int pin, int pud) ; +void (*digitalWrite) (int pin, int value) ; +void (*pwmWrite) (int pin, int value) ; +int (*digitalRead) (int pin) ; +int (*waitForInterrupt) (int pin, int mS) ; + + #ifndef TRUE #define TRUE (1==1) #define FALSE (1==2) @@ -77,7 +97,7 @@ #define FSEL_ALT5 0b010 // Access from ARM Running Linux -// Take from Gerts code. Some of this is not in the manual +// Take from Gert/Doms code. Some of this is not in the manual // that I can find )-: #define BCM2708_PERI_BASE 0x20000000 @@ -143,14 +163,10 @@ static volatile uint32_t *clk ; // X / 10 + ((X % 10) * 3) // sysFds: -// Map a file descriptor from the /sys/class/gpio/gpioX/value file +// Map a file descriptor from the /sys/class/gpio/gpioX/value static int sysFds [64] ; -// Mode - -static int gpioPinMode ; - // Doing it the Arduino way with lookup tables... // Yes, it's probably more innefficient than all the bit-twidling, but it // does tend to make it all a bit clearer. At least to me! @@ -158,13 +174,19 @@ static int gpioPinMode ; // pinToGpio: // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin -static int pinToGpio [] = +static int pinToGpio [64] = { 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7 0, 1, // I2C - SDA0, SCL0 8, 7, // SPI - CE1, CE0 10, 9, 11, // SPI - MOSI, MISO, SCLK 14, 15, // UART - Tx, Rx + +// Padding: + + -1, -1, -1,-1,-1,-1,-1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63 } ; // gpioToGPFSEL: @@ -219,6 +241,35 @@ static uint8_t gpioToGPLEV [] = 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14, } ; +#ifdef notYetReady +// gpioToEDS +// (Word) offset to the Event Detect Status + +static uint8_t gpioToEDS [] = +{ + 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, + 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, +} ; + +// gpioToREN +// (Word) offset to the Rising edgde ENable register + +static uint8_t gpioToREN [] = +{ + 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, + 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, +} ; + +// gpioToFEN +// (Word) offset to the Falling edgde ENable register + +static uint8_t gpioToFEN [] = +{ + 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, + 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23, +} ; +#endif + // gpioToPUDCLK // (Word) offset to the Pull Up Down Clock regsiter @@ -265,16 +316,304 @@ static unsigned long long epoch ; /* - * wiringPiGpioMode: - * Set the mode - use Pin numbers (0-16) or GPIO number (seemingly random) + * pinMode: + * Sets the mode of a pin to be input, output or PWM output ********************************************************************************* */ -void wiringPiGpioMode (int mode) +void pinModeGpio (int pin, int mode) { - gpioPinMode = mode ; + static int pwmRunning = FALSE ; + int fSel, shift, alt ; + + pin &= 63 ; + + fSel = gpioToGPFSEL [pin] ; + shift = gpioToShift [pin] ; + + /**/ if (mode == INPUT) + *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input + else if (mode == OUTPUT) + *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ; + else if (mode == PWM_OUTPUT) + { + if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin + return ; + +// Set pin to PWM mode + + *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; + +// We didn't initialise the PWM hardware at setup time - because it's possible that +// something else is using the PWM - e.g. the Audio systems! So if we use PWM +// here, then we're assuming that nothing else is, otherwise things are going +// to sound a bit funny... + + if (!pwmRunning) + { + +// Gert/Doms Values + *(clk + PWMCLK_DIV) = 0x5A000000 | (32<<12) ; // set pwm div to 32 (19.2/3 = 600KHz) + *(clk + PWMCLK_CNTL) = 0x5A000011 ; // Source=osc and enable + digitalWrite (pin, LOW) ; + *(pwm + PWM_CONTROL) = 0 ; // Disable PWM + delayMicroseconds (10) ; + *(pwm + PWM0_RANGE) = 0x400 ; + delayMicroseconds (10) ; + *(pwm + PWM1_RANGE) = 0x400 ; + delayMicroseconds (10) ; + +// Enable PWMs + + *(pwm + PWM0_DATA) = 512 ; + *(pwm + PWM1_DATA) = 512 ; + + *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; + } + + } + +// When we change mode of any pin, we remove the pull up/downs + + pullUpDnControl (pin, PUD_OFF) ; } +void pinModeWPi (int pin, int mode) +{ + pinModeGpio (pinToGpio [pin & 63], mode) ; +} + +void pinModeSys (int pin, int mode) +{ + return ; +} + + +#ifdef notYetReady +/* + * pinED01: + * pinED10: + * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0 + * Pin must already be in input mode with appropriate pull up/downs set. + ********************************************************************************* + */ + +void pinEnableED01Pi (int pin) +{ + pin = pinToGpio [pin & 63] ; +} +#endif + + + +/* + * digitalWrite: + * Set an output bit + ********************************************************************************* + */ + +void digitalWriteWPi (int pin, int value) +{ + int gpioPin = pinToGpio [pin & 63] ; + + if (value == LOW) + *(gpio + gpioToGPCLR [gpioPin]) = 1 << gpioPin ; + else + *(gpio + gpioToGPSET [gpioPin]) = 1 << gpioPin ; +} + +void digitalWriteGpio (int pin, int value) +{ + pin &= 63 ; + + if (value == LOW) + *(gpio + gpioToGPCLR [pin]) = 1 << pin ; + else + *(gpio + gpioToGPSET [pin]) = 1 << pin ; +} + +void digitalWriteSys (int pin, int value) +{ + pin &= 63 ; + + if (sysFds [pin] != -1) + { + if (value == LOW) + write (sysFds [pin], "0\n", 2) ; + else + write (sysFds [pin], "1\n", 2) ; + } +} + + +/* + * pwnWrite: + * Set an output PWM value + ********************************************************************************* + */ + +void pwmWriteWPi (int pin, int value) +{ + int port, gpioPin ; + + gpioPin = pinToGpio [pin & 63] ; + port = gpioToPwmPort [gpioPin] ; + + *(pwm + port) = value & 0x3FF ; +} + +void pwmWriteGpio (int pin, int value) +{ + int port, gpioPin ; + + gpioPin = pin & 63 ; + port = gpioToPwmPort [gpioPin] ; + + *(pwm + port) = value & 0x3FF ; +} + + +void pwmWriteSys (int pin, int value) +{ + return ; +} + + +/* + * digitalRead: + * Read the value of a given Pin, returning HIGH or LOW + ********************************************************************************* + */ + +int digitalReadWPi (int pin) +{ + int gpioPin ; + + pin &= 63 ; + + gpioPin = pinToGpio [pin] ; + + if ((*(gpio + gpioToGPLEV [gpioPin]) & (1 << gpioPin)) != 0) + return HIGH ; + else + return LOW ; +} + +int digitalReadGpio (int pin) +{ + pin &= 63 ; + + if ((*(gpio + gpioToGPLEV [pin]) & (1 << pin)) != 0) + return HIGH ; + else + return LOW ; +} + +int digitalReadSys (int pin) +{ + char c ; + + pin &= 63 ; + + if (sysFds [pin] == -1) + return 0 ; + + lseek (sysFds [pin], 0L, SEEK_SET) ; + read (sysFds [pin], &c, 1) ; + return (c == '0') ? 0 : 1 ; +} + + +/* + * pullUpDownCtrl: + * Control the internal pull-up/down resistors on a GPIO pin + * The Arduino only has pull-ups and these are enabled by writing 1 + * to a port when in input mode - this paradigm doesn't quite apply + * here though. + ********************************************************************************* + */ + +void pullUpDnControlWPi (int pin, int pud) +{ + pin = pinToGpio [pin & 63] ; + + *(gpio + 37) = pud ; + delayMicroseconds (10) ; + *(gpio + gpioToPUDCLK [pin]) = 1 << pin ; + delayMicroseconds (10) ; + + *(gpio + 37) = 0 ; + *(gpio + gpioToPUDCLK [pin]) = 0 ; +} + +void pullUpDnControlGpio (int pin, int pud) +{ + pin &= 63 ; + + *(gpio + 37) = pud ; + delayMicroseconds (10) ; + *(gpio + gpioToPUDCLK [pin]) = 1 << pin ; + delayMicroseconds (10) ; + + *(gpio + 37) = 0 ; + *(gpio + gpioToPUDCLK [pin]) = 0 ; +} + +void pullUpDnControlSys (int pin, int pud) +{ + return ; +} + + +/* + * waitForInterrupt: + * Wait for Interrupt on a GPIO pin. + * This is actually done via the /sys/class/gpio interface regardless of + * the wiringPi access mode in-use. Maybe sometime it might get a better + * way for a bit more efficiency. + ********************************************************************************* + */ + +int waitForInterruptSys (int pin, int mS) +{ + int fd, x ; + char buf [8] ; + 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 ; + polls.events = POLLPRI ; // Urgent data! + +// Wait for it ... + + return poll (&polls, 1, mS) ; +} + +int waitForInterruptWPi (int pin, int mS) +{ + return waitForInterruptSys (pinToGpio [pin & 63], mS) ; +} + +int waitForInterruptGpio (int pin, int mS) +{ + return waitForInterruptSys (pin, mS) ; +} + + /* * wiringPiSetup: @@ -296,9 +635,12 @@ int wiringPiSetup (void) uint32_t *pads ; #endif -// Set Pin mode by default - - wiringPiGpioMode (WPI_MODE_PINS) ; + pinMode = pinModeWPi ; + pullUpDnControl = pullUpDnControlWPi ; + digitalWrite = digitalWriteWPi ; + pwmWrite = pwmWriteWPi ; + digitalRead = digitalReadWPi ; + waitForInterrupt = waitForInterruptWPi ; // Open the master /dev/memory device @@ -417,7 +759,13 @@ int wiringPiSetupGpio (void) if (x != 0) return x ; - wiringPiGpioMode (WPI_MODE_GPIO) ; + pinMode = pinModeGpio ; + pullUpDnControl = pullUpDnControlGpio ; + digitalWrite = digitalWriteGpio ; + pwmWrite = pwmWriteGpio ; + digitalRead = digitalReadGpio ; + waitForInterrupt = waitForInterruptGpio ; + return 0 ; } @@ -433,26 +781,28 @@ int wiringPiSetupGpio (void) int wiringPiSetupSys (void) { - int fd, pin ; + int pin ; struct timeval tv ; char fName [128] ; -// Set GPIO_SYS mode by default - - wiringPiGpioMode (WPI_MODE_GPIO_SYS) ; + pinMode = pinModeSys ; + pullUpDnControl = pullUpDnControlSys ; + digitalWrite = digitalWriteSys ; + pwmWrite = pwmWriteSys ; + digitalRead = digitalReadSys ; + waitForInterrupt = waitForInterruptSys ; // Open and scan the directory, looking for exported GPIOs, and pre-open -// the 'value' part to speed things up for later +// the 'value' interface to speed things up for later for (pin = 0 ; pin < 64 ; ++pin) { - sysFds [pin] = -1 ; sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; - if ((fd = open (fName, O_RDWR)) == -1) - continue ; - sysFds [pin] = fd ; + sysFds [pin] = open (fName, O_RDWR) ; } +// Initialise the epoch for mills() ... + gettimeofday (&tv, NULL) ; epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; @@ -460,228 +810,7 @@ int wiringPiSetupSys (void) } -/* - * pinMode: - * Sets the mode of a pin to be input, output or PWM output - ********************************************************************************* - */ -void pinMode (int pin, int mode) -{ - static int pwmRunning = FALSE ; - - int gpioPin, fSel, shift ; - int alt ; - -// We can't change the mode in GPIO_SYS mode - - if (gpioPinMode == WPI_MODE_GPIO_SYS) - return ; - - if (gpioPinMode == WPI_MODE_PINS) - { - if ((pin < 0) || (pin >= NUM_PINS)) - return ; - gpioPin = pinToGpio [pin] ; - } - else - gpioPin = pin ; - - fSel = gpioToGPFSEL [gpioPin] ; - shift = gpioToShift [gpioPin] ; - - /**/ if (mode == INPUT) - *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input - else if (mode == OUTPUT) - *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ; - else if (mode == PWM_OUTPUT) - { - if ((alt = gpioToPwmALT [gpioPin]) == 0) // Not a PWM pin - return ; - -// Set pin to PWM mode - - *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; - -// We didn't initialise the PWM hardware at setup time - because it's possible that -// something else is using the PWM - e.g. the Audio systems! So if we use PWM -// here, then we're assuming that nothing else is, otherwise things are going -// to sound a bit funny... - - if (!pwmRunning) - { - -// Gert/Doms Values - *(clk + PWMCLK_DIV) = 0x5A000000 | (32<<12) ; // set pwm div to 32 (19.2/3 = 600KHz) - *(clk + PWMCLK_CNTL) = 0x5A000011 ; // Source=osc and enable - digitalWrite (pin, LOW) ; - *(pwm + PWM_CONTROL) = 0 ; // Disable PWM - delayMicroseconds (10) ; - *(pwm + PWM0_RANGE) = 0x400 ; - delayMicroseconds (10) ; - *(pwm + PWM1_RANGE) = 0x400 ; - delayMicroseconds (10) ; - -// Enable PWMs - - *(pwm + PWM0_DATA) = 512 ; - *(pwm + PWM1_DATA) = 512 ; - - *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; - } - - } - -// When we change mode of any pin, we remove the pull up/downs - - pullUpDnControl (pin, PUD_OFF) ; -} - - -/* - * digitalWrite: - * Set an output bit - ********************************************************************************* - */ - -void digitalWrite (int pin, int value) -{ - int gpioPin ; - - if (gpioPinMode == WPI_MODE_PINS) - { - if ((pin < 0) || (pin >= NUM_PINS)) - return ; - gpioPin = pinToGpio [pin] ; - } - else - gpioPin = pin ; - - if (gpioPinMode == WPI_MODE_GPIO_SYS) - { - if (sysFds [gpioPin] != -1) - { - if (value == LOW) - write (sysFds [gpioPin], "0\n", 2) ; - else - write (sysFds [gpioPin], "1\n", 2) ; - } - } - else - { - if (value == LOW) - *(gpio + gpioToGPCLR [gpioPin]) = 1 << gpioPin ; - else - *(gpio + gpioToGPSET [gpioPin]) = 1 << gpioPin ; - } -} - - -/* - * pwnWrite: - * Set an output PWM value - ********************************************************************************* - */ - -void pwmWrite (int pin, int value) -{ - int port, gpioPin ; - -// We can't do this in GPIO_SYS mode - - if (gpioPinMode == WPI_MODE_GPIO_SYS) - return ; - - if (gpioPinMode == WPI_MODE_PINS) - { - if ((pin < 0) || (pin >= NUM_PINS)) - return ; - gpioPin = pinToGpio [pin] ; - } - else - gpioPin = pin ; - - port = gpioToPwmPort [gpioPin] ; - - *(pwm + port) = value & ~0x400 ; -} - - -/* - * digitalRead: - * Read the value of a given Pin, returning HIGH or LOW - ********************************************************************************* - */ - -int digitalRead (int pin) -{ - int gpioPin ; - char c ; - - if (gpioPinMode == WPI_MODE_PINS) - { - if ((pin < 0) || (pin >= NUM_PINS)) - return 0 ; - gpioPin = pinToGpio [pin] ; - } - else - gpioPin = pin ; - - if (gpioPinMode == WPI_MODE_GPIO_SYS) - { - if (sysFds [gpioPin] == -1) - return 0 ; - else - { - lseek (sysFds [gpioPin], 0L, SEEK_SET) ; - read (sysFds [gpioPin], &c, 1) ; - return (c == '0') ? 0 : 1 ; - } - } - else - { - if ((*(gpio + gpioToGPLEV [gpioPin]) & (1 << gpioPin)) != 0) - return HIGH ; - else - return LOW ; - } -} - -/* - * pullUpDownCtrl: - * Control the internal pull-up/down resistors on a GPIO pin - * The Arduino only has pull-ups and these are enabled by writing 1 - * to a port when in input mode - this paradigm doesn't quite apply - * here though. - ********************************************************************************* - */ - -void pullUpDnControl (int pin, int pud) -{ - int gpioPin ; - -// We can't do this in GPIO_SYS mode - - if (gpioPinMode == WPI_MODE_GPIO_SYS) - return ; - - if (gpioPinMode == WPI_MODE_PINS) - { - if ((pin < 0) || (pin >= NUM_PINS)) - return ; - gpioPin = pinToGpio [pin] ; - } - else - gpioPin = pin ; - - *(gpio + 37) = pud ; - delayMicroseconds (10) ; - *(gpio + gpioToPUDCLK [gpioPin]) = 1 << gpioPin ; - delayMicroseconds (10) ; - - *(gpio + 37) = 0 ; - *(gpio + gpioToPUDCLK [gpioPin]) = 0 ; -} /* diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index fcb7ae6..a81511a 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -7,16 +7,16 @@ * 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 General Public License as published by + * 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 General Public License for more details. + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License + * You should have received a copy of the GNU Lesser General Public License * along with wiringPi. If not, see . *********************************************************************** */ @@ -28,6 +28,7 @@ #define WPI_MODE_PINS 0 #define WPI_MODE_GPIO 1 #define WPI_MODE_GPIO_SYS 2 +#define WPI_MODE_PIFACE 3 #define INPUT 0 #define OUTPUT 1 @@ -48,17 +49,39 @@ extern "C" { #endif -extern int wiringPiSetup (void) ; -extern int wiringPiSetupSys (void) ; -extern int wiringPiSetupGpio (void) ; +// Basic wiringPi functions -extern void wiringPiGpioMode (int mode) ; +extern int wiringPiSetup (void) ; +extern int wiringPiSetupSys (void) ; +extern int wiringPiSetupGpio (void) ; +extern int wiringPiSetupPiFace (void) ; -extern void pullUpDnControl (int pin, int pud) ; -extern void pinMode (int pin, int mode) ; -extern void digitalWrite (int pin, int value) ; -extern void pwmWrite (int pin, int value) ; -extern int digitalRead (int pin) ; +extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only + +extern void (*pinMode) (int pin, int mode) ; +extern void (*pullUpDnControl) (int pin, int pud) ; +extern void (*digitalWrite) (int pin, int value) ; +extern void (*pwmWrite) (int pin, int value) ; +extern int (*digitalRead) (int pin) ; + +// Interrupts + +extern int (*waitForInterrupt) (int pin, int mS) ; + +// Threads + +#define PI_THREAD(X) void *X (void *dummy) + +extern int piThreadCreate (void *(*fn)(void *)) ; +extern void piLock (int key) ; +extern void piUnlock (int key) ; + +// Schedulling priority + +extern int piHiPri (int pri) ; + + +// Extras from arduino land extern void delay (unsigned int howLong) ; extern void delayMicroseconds (unsigned int howLong) ; diff --git a/wiringPi/wiringPiFace.c b/wiringPi/wiringPiFace.c new file mode 100644 index 0000000..2425413 --- /dev/null +++ b/wiringPi/wiringPiFace.c @@ -0,0 +1,355 @@ +/* + * wiringPiFace: + * Arduino compatable (ish) Wiring library for the Raspberry Pi + * Copyright (c) 2012 Gordon Henderson + * + * This file to interface with the PiFace peripheral device which + * has an MCP23S17 GPIO device connected via the SPI bus. + * + *********************************************************************** + * 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 +#include + +#include "wiringPi.h" + + +// The SPI bus parameters +// Variables as they need to be passed as pointers later on + +static char *spiDevice = "/dev/spidev0.0" ; +static uint8_t spiMode = 0 ; +static uint8_t spiBPW = 8 ; +static uint32_t spiSpeed = 5000000 ; +static uint16_t spiDelay = 0; + +// Locals here to keep track of everything + +static int spiFd ; + +// The MCP23S17 doesn't have bit-set operations, so it's +// cheaper to keep a copy here than to read/modify/write it + +uint8_t dataOutRegister = 0 ; +uint8_t pudRegister = 0 ; + +// MCP23S17 Registers + +#define IOCON 0x0A + +#define IODIRA 0x00 +#define IPOLA 0x02 +#define GPINTENA 0x04 +#define DEFVALA 0x06 +#define INTCONA 0x08 +#define GPPUA 0x0C +#define INTFA 0x0E +#define INTCAPA 0x10 +#define GPIOA 0x12 +#define OLATA 0x14 + +#define IODIRB 0x01 +#define IPOLB 0x03 +#define GPINTENB 0x05 +#define DEFVALB 0x07 +#define INTCONB 0x09 +#define GPPUB 0x0D +#define INTFB 0x0F +#define INTCAPB 0x11 +#define GPIOB 0x13 +#define OLATB 0x15 + +// Bits in the IOCON register + +#define IOCON_BANK_MODE 0x80 +#define IOCON_MIRROR 0x40 +#define IOCON_SEQOP 0x20 +#define IOCON_DISSLW 0x10 +#define IOCON_HAEN 0x08 +#define IOCON_ODR 0x04 +#define IOCON_INTPOL 0x02 +#define IOCON_UNUSED 0x01 + +// Default initialisation mode + +#define IOCON_INIT (IOCON_SEQOP) + +// Command codes + +#define CMD_WRITE 0x40 +#define CMD_READ 0x41 + + +/* + * writeByte: + * Write a byte to a register on the MCP23S17 on the SPI bus. + * This is using the synchronous access mechanism. + ********************************************************************************* + */ + +static void writeByte (uint8_t reg, uint8_t data) +{ + uint8_t spiBufTx [3] ; + uint8_t spiBufRx [3] ; + struct spi_ioc_transfer spi ; + + spiBufTx [0] = CMD_WRITE ; + spiBufTx [1] = reg ; + spiBufTx [2] = data ; + + spi.tx_buf = (unsigned long)spiBufTx ; + spi.rx_buf = (unsigned long)spiBufRx ; + spi.len = 3 ; + spi.delay_usecs = spiDelay ; + spi.speed_hz = spiSpeed ; + spi.bits_per_word = spiBPW ; + + ioctl (spiFd, SPI_IOC_MESSAGE(1), &spi) ; +} + +/* + * readByte: + * Read a byte from a register on the MCP23S17 on the SPI bus. + * This is the synchronous access mechanism. + * What appears to happen is that the data returned is at + * the same offset as the number of bytes written to the device. So if we + * write 2 bytes (e.g. command then register number), then the data returned + * will by at the 3rd byte... + ********************************************************************************* + */ + +static uint8_t readByte (uint8_t reg) +{ + uint8_t tx [4] ; + uint8_t rx [4] ; + struct spi_ioc_transfer spi ; + + tx [0] = CMD_READ ; + tx [1] = reg ; + tx [2] = 0 ; + + spi.tx_buf = (unsigned long)tx ; + spi.rx_buf = (unsigned long)rx ; + spi.len = 3 ; + spi.delay_usecs = spiDelay ; + spi.speed_hz = spiSpeed ; + spi.bits_per_word = spiBPW ; + + ioctl (spiFd, SPI_IOC_MESSAGE(1), &spi) ; + + return rx [2] ; +} + + +/* + * digitalWritePiFace: + * Perform the digitalWrite function on the PiFace board + ********************************************************************************* + */ + +void digitalWritePiFace (int pin, int value) +{ + uint8_t mask = 1 << pin ; + + if (value == 0) + dataOutRegister &= (~mask) ; + else + dataOutRegister |= mask ; + + writeByte (GPIOA, dataOutRegister) ; +} + + +void digitalWritePiFaceSpecial (int pin, int value) +{ + uint8_t mask = 1 << pin ; + uint8_t old ; + + old = readByte (GPIOA) ; + + if (value == 0) + old &= (~mask) ; + else + old |= mask ; + + writeByte (GPIOA, old) ; +} + + +/* + * digitalReadPiFace: + * Perform the digitalRead function on the PiFace board + ********************************************************************************* + */ + +int digitalReadPiFace (int pin) +{ + uint8_t mask = 1 << pin ; + + if ((readByte (GPIOB) & mask) != 0) + return HIGH ; + else + return LOW ; +} + + +/* + * pullUpDnControlPiFace: + * Perform the pullUpDnControl function on the PiFace board + ********************************************************************************* + */ + +void pullUpDnControlPiFace (int pin, int pud) +{ + uint8_t mask = 1 << pin ; + + if (pud == PUD_UP) + pudRegister |= mask ; + else + pudRegister &= (~mask) ; + + writeByte (GPPUB, pudRegister) ; + +} + + +void pullUpDnControlPiFaceSpecial (int pin, int pud) +{ + uint8_t mask = 1 << pin ; + uint8_t old ; + + old = readByte (GPPUB) ; + + if (pud == PUD_UP) + old |= mask ; + else + old &= (~mask) ; + + writeByte (GPPUB, old) ; + +} + + + +/* + * Dummy functions that are not used in this mode + ********************************************************************************* + */ + +void pinModePiFace (int pin, int mode) {} +void pwmWritePiFace (int pin, int value) {} +int waitForInterruptPiFace (int pin, int mS) { return 0 ; } + + +/* + * wiringPiSetupPiFace + * Setup the SPI interface and initialise the MCP23S17 chip + ********************************************************************************* + */ + +static int _wiringPiSetupPiFace (void) +{ + if ((spiFd = open (spiDevice, O_RDWR)) < 0) + return -1 ; + +// Set SPI parameters +// Why are we doing a read after write? +// I don't know - just blindliy copying an example elsewhere... -GH- + + if (ioctl (spiFd, SPI_IOC_WR_MODE, &spiMode) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_RD_MODE, &spiMode) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_WR_MAX_SPEED_HZ, &spiSpeed) < 0) + return -1 ; + + if (ioctl (spiFd, SPI_IOC_RD_MAX_SPEED_HZ, &spiSpeed) < 0) + return -1 ; + +// Setup the MCP23S17 + + writeByte (IOCON, IOCON_INIT) ; + + writeByte (IODIRA, 0x00) ; // Port A -> Outputs + writeByte (IODIRB, 0xFF) ; // Port B -> Inputs + + return 0 ; +} + + +int wiringPiSetupPiFace (void) +{ + int x = _wiringPiSetupPiFace () ; + + if (x != 0) + return x ; + + writeByte (GPIOA, 0x00) ; // Set all outptus off + writeByte (GPPUB, 0x00) ; // Disable any pull-ups on port B + + pinMode = pinModePiFace ; + pullUpDnControl = pullUpDnControlPiFace ; + digitalWrite = digitalWritePiFace ; + pwmWrite = pwmWritePiFace ; + digitalRead = digitalReadPiFace ; + waitForInterrupt = waitForInterruptPiFace ; + + return 0 ; +} + + +/* + * wiringPiSetupPiFaceForGpioProg: + * Setup the SPI interface and initialise the MCP23S17 chip + * Special version for the gpio program + ********************************************************************************* + */ + + +int wiringPiSetupPiFaceForGpioProg (void) +{ + int x = _wiringPiSetupPiFace () ; + + if (x != 0) + return x ; + + pinMode = pinModePiFace ; + pullUpDnControl = pullUpDnControlPiFaceSpecial ; + digitalWrite = digitalWritePiFaceSpecial ; + pwmWrite = pwmWritePiFace ; + digitalRead = digitalReadPiFace ; + waitForInterrupt = waitForInterruptPiFace ; + + return 0 ; +} diff --git a/wiringPi/wiringSerial.c b/wiringPi/wiringSerial.c new file mode 100644 index 0000000..c3dae67 --- /dev/null +++ b/wiringPi/wiringSerial.c @@ -0,0 +1,206 @@ +/* + * wiringSerial.c: + * Handle a serial port + *********************************************************************** + * 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 . + *********************************************************************** + */ + +#undef DEBUG + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "wiringSerial.h" + +/* + * serialOpen: + * Open and initialise the serial port, setting all the right + * port parameters - or as many as are required - hopefully! + ********************************************************************************* + */ + +int serialOpen (char *device, int baud) +{ + struct termios options ; + speed_t myBaud ; + int status, fd ; + +#ifdef DEBUG + printf ("openSerialPort: <%s> baud: $d\n", device, baud) ; +#endif + + switch (baud) + { + case 50: myBaud = B50 ; break ; + case 75: myBaud = B75 ; break ; + case 110: myBaud = B110 ; break ; + case 134: myBaud = B134 ; break ; + case 150: myBaud = B150 ; break ; + case 200: myBaud = B200 ; break ; + case 300: myBaud = B300 ; break ; + case 600: myBaud = B600 ; break ; + case 1200: myBaud = B1200 ; break ; + case 1800: myBaud = B1800 ; break ; + case 2400: myBaud = B2400 ; break ; + case 9600: myBaud = B9600 ; break ; + case 19200: myBaud = B19200 ; break ; + case 38400: myBaud = B38400 ; break ; + case 57600: myBaud = B57600 ; break ; + case 115200: myBaud = B115200 ; break ; + case 230400: myBaud = B230400 ; break ; + + default: + return -2 ; + } + + if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1) + return -1 ; + + fcntl (fd, F_SETFL, O_RDWR) ; + +// Get and modify current options: + + tcgetattr (fd, &options) ; + + cfmakeraw (&options) ; + cfsetispeed (&options, myBaud) ; + cfsetospeed (&options, myBaud) ; + + options.c_cflag |= (CLOCAL | CREAD) ; + options.c_cflag &= ~PARENB ; + options.c_cflag &= ~CSTOPB ; + options.c_cflag &= ~CSIZE ; + options.c_cflag |= CS8 ; + options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ; + options.c_oflag &= ~OPOST ; + + options.c_cc [VMIN] = 0 ; + options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds) + + tcsetattr (fd, TCSANOW, &options) ; + + ioctl (fd, TIOCMGET, &status); + + status |= TIOCM_DTR ; + status |= TIOCM_RTS ; + + ioctl (fd, TIOCMSET, &status); + + usleep (10000) ; // 10mS + + return fd ; +} + + +/* + * serialClose: + * Release the serial port + ********************************************************************************* + */ + +void serialClose (int fd) +{ + close (fd) ; +} + + +/* + * serialPutchar: + * Send a single character to the serial port + ********************************************************************************* + */ + +void serialPutchar (int fd, unsigned char c) +{ + write (fd, &c, 1) ; +} + + +/* + * serialPuts: + * Send a string to the serial port + ********************************************************************************* + */ + +void serialPuts (int fd, char *s) +{ + write (fd, s, strlen (s)) ; +} + +/* + * serialPrintf: + * Printf over Serial + ********************************************************************************* + */ + +void serialPrintf (int fd, char *message, ...) +{ + va_list argp ; + char buffer [1024] ; + + va_start (argp, message) ; + vsnprintf (buffer, 1023, message, argp) ; + va_end (argp) ; + + serialPuts (fd, buffer) ; +} + + +/* + * serialDataAvail: + * Return the number of bytes of data avalable to be read in the serial port + ********************************************************************************* + */ + +int serialDataAvail (int fd) +{ + int result ; + + if (ioctl (fd, FIONREAD, &result) == -1) + return -1 ; + + return result ; +} + + +/* + * serialGetchar: + * Get a single character from the serial device. + * Note: Zero is a valid character and this function will time-out after + * 10 seconds. + ********************************************************************************* + */ + +int serialGetchar (int fd) +{ + uint8_t x ; + + if (read (fd, &x, 1) != 1) + return -1 ; + + return ((int)x) & 0xFF ; +} diff --git a/wiringPi/wiringSerial.h b/wiringPi/wiringSerial.h new file mode 100644 index 0000000..609838d --- /dev/null +++ b/wiringPi/wiringSerial.h @@ -0,0 +1,37 @@ +/* + * wiringSerial.h: + * Handle a serial port + *********************************************************************** + * 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 . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int serialOpen (char *device, int baud) ; +extern void serialClose (int fd) ; +extern void serialPutchar (int fd, unsigned char c) ; +extern void serialPuts (int fd, char *s) ; +extern void serialPrintf (int fd, char *message, ...) ; +extern int serialDataAvail (int fd) ; +extern int serialGetchar (int fd) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/wiringShift.c b/wiringPi/wiringShift.c index c684dbb..b9b7a44 100644 --- a/wiringPi/wiringShift.c +++ b/wiringPi/wiringShift.c @@ -8,16 +8,16 @@ * 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 General Public License as published by + * 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 General Public License for more details. + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License + * You should have received a copy of the GNU Lesser General Public License * along with wiringPi. If not, see . *********************************************************************** */ @@ -82,36 +82,3 @@ void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) digitalWrite (cPin, LOW) ; } } - - -/* - * shiftOut: - * Shift data out to a clocked source - ********************************************************************************* - */ - -void shiftOutWithDelay (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val, int delay) -{ - int8_t i; - - if (order == MSBFIRST) - for (i = 7 ; i >= 0 ; --i) - { - digitalWrite (dPin, val & (1 << i)) ; - delayMicroseconds (delay) ; - digitalWrite (cPin, HIGH) ; - delayMicroseconds (delay) ; - digitalWrite (cPin, LOW) ; - delayMicroseconds (delay) ; - } - else - for (i = 0 ; i < 8 ; ++i) - { - digitalWrite (dPin, val & (1 << i)) ; - delayMicroseconds (delay) ; - digitalWrite (cPin, HIGH) ; - delayMicroseconds (delay) ; - digitalWrite (cPin, LOW) ; - delayMicroseconds (delay) ; - } -} diff --git a/wiringPi/wiringShift.h b/wiringPi/wiringShift.h index 3d846e0..a3f4581 100644 --- a/wiringPi/wiringShift.h +++ b/wiringPi/wiringShift.h @@ -8,16 +8,16 @@ * 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 General Public License as published by + * 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 General Public License for more details. + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License + * You should have received a copy of the GNU Lesser General Public License * along with wiringPi. If not, see . *********************************************************************** */ @@ -35,7 +35,6 @@ extern "C" { extern uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order) ; extern void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) ; -extern void shiftOutWithDelay (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val, int delay) ; #ifdef __cplusplus }