From bf0ad86017f9a90c503c61b7f03744f310e80383 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 16 Aug 2012 15:04:43 +0100 Subject: [PATCH 01/97] Initial move to GIT --- COPYING.LESSER | 165 ++++++ examples/COPYING.LESSER | 165 ++++++ examples/Makefile | 96 ++++ examples/delayTest.c | 68 +++ examples/gertboard.c | 47 ++ examples/gertboard.png | Bin 0 -> 4834 bytes examples/lcd.c | 129 +++++ examples/nes.c | 44 ++ examples/piface.c | 53 ++ examples/speed.c | 105 ++++ examples/test1.c | 91 ++++ examples/test2.c | 41 ++ examples/wfi.c | 158 ++++++ gpio/COPYING.LESSER | 165 ++++++ gpio/Makefile | 73 +++ gpio/gpio.1 | 179 +++++++ gpio/gpio.c | 755 +++++++++++++++++++++++++++ gpio/test.sh | 23 + wiringPi/.wiringPiFace.c.swp | Bin 0 -> 16384 bytes wiringPi/COPYING.LESSER | 165 ++++++ wiringPi/Makefile | 101 ++++ wiringPi/README | 9 + wiringPi/gertboard.c | 184 +++++++ wiringPi/gertboard.h | 39 ++ wiringPi/lcd.c | 365 +++++++++++++ wiringPi/lcd.h | 37 ++ wiringPi/piHiPri.c | 50 ++ wiringPi/piNes.c | 115 ++++ wiringPi/piNes.h | 37 ++ wiringPi/piThread.c | 63 +++ wiringPi/wiringPi.c | 981 +++++++++++++++++++++++++++++++++++ wiringPi/wiringPi.h | 95 ++++ wiringPi/wiringPiFace.c | 355 +++++++++++++ wiringPi/wiringSerial.c | 218 ++++++++ wiringPi/wiringSerial.h | 38 ++ wiringPi/wiringShift.c | 84 +++ wiringPi/wiringShift.h | 41 ++ 37 files changed, 5334 insertions(+) create mode 100644 COPYING.LESSER create mode 100644 examples/COPYING.LESSER create mode 100644 examples/Makefile create mode 100644 examples/delayTest.c create mode 100644 examples/gertboard.c create mode 100644 examples/gertboard.png create mode 100644 examples/lcd.c create mode 100644 examples/nes.c create mode 100644 examples/piface.c create mode 100644 examples/speed.c create mode 100644 examples/test1.c create mode 100644 examples/test2.c create mode 100644 examples/wfi.c create mode 100644 gpio/COPYING.LESSER create mode 100644 gpio/Makefile create mode 100644 gpio/gpio.1 create mode 100644 gpio/gpio.c create mode 100755 gpio/test.sh create mode 100644 wiringPi/.wiringPiFace.c.swp create mode 100644 wiringPi/COPYING.LESSER create mode 100644 wiringPi/Makefile create mode 100644 wiringPi/README create mode 100644 wiringPi/gertboard.c create mode 100644 wiringPi/gertboard.h create mode 100644 wiringPi/lcd.c create mode 100644 wiringPi/lcd.h create mode 100644 wiringPi/piHiPri.c create mode 100644 wiringPi/piNes.c create mode 100644 wiringPi/piNes.h create mode 100644 wiringPi/piThread.c create mode 100644 wiringPi/wiringPi.c create mode 100644 wiringPi/wiringPi.h create mode 100644 wiringPi/wiringPiFace.c create mode 100644 wiringPi/wiringSerial.c create mode 100644 wiringPi/wiringSerial.h create mode 100644 wiringPi/wiringShift.c create mode 100644 wiringPi/wiringShift.h 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 new file mode 100644 index 0000000..c1b2182 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,96 @@ +# +# Makefile: +# wiringPi - Wiring Compatable library for the Raspberry Pi +# https://projects.drogon.net/wiring-pi +# +# Copyright (c) 2012 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# + + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LIBS = -lwiringPi + +# Should not alter anything below this line +############################################################################### + +SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c gertboard.c nes.c delayTest.c + +OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o gertboard.o nes.o delayTest.o + +all: test1 test2 speed lcd wfi piface gertboard nes + +test1: test1.o + @echo [link] + $(CC) -o $@ test1.o $(LDFLAGS) $(LIBS) + +test2: test2.o + @echo [link] + $(CC) -o $@ test2.o $(LDFLAGS) $(LIBS) + +speed: speed.o + @echo [link] + $(CC) -o $@ speed.o $(LDFLAGS) $(LIBS) + +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 + +gertboard: gertboard.o + @echo [link] + $(CC) -o $@ gertboard.o $(LDFLAGS) $(LIBS) -lm + +nes: nes.o + @echo [link] + $(CC) -o $@ nes.o $(LDFLAGS) $(LIBS) -lm + + +delayTest: delayTest.o + @echo [link] + $(CC) -o $@ delayTest.o $(LDFLAGS) $(LIBS) + + +.c.o: + @echo [CC] $< + @$(CC) -c $(CFLAGS) $< -o $@ + +clean: + rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface gertboard nes delayTest + +tags: $(SRC) + @echo [ctags] + @ctags $(SRC) + +depend: + makedepend -Y $(SRC) + +# DO NOT DELETE diff --git a/examples/delayTest.c b/examples/delayTest.c new file mode 100644 index 0000000..8c95522 --- /dev/null +++ b/examples/delayTest.c @@ -0,0 +1,68 @@ + +#include +#include +#include + +#include +#include +#include + +#define CYCLES 1000 +#define DELAY 99 + +int main() +{ + int x ; + struct timeval t1, t2 ; + long long t ; + unsigned int max, min ; + + unsigned int values [CYCLES] ; + + max = 0 ; + min = 1000000 ; + + if (wiringPiSetup () == -1) + return 1 ; + + piHiPri (10) ; + sleep (1) ; + +// Baseline test + + gettimeofday (&t1, NULL) ; + gettimeofday (&t2, NULL) ; + + t = t2.tv_usec - t1.tv_usec ; + printf ("Baseline test: %lld\n", t); + + for (x = 0 ; x < CYCLES ; ++x) + { + gettimeofday (&t1, NULL) ; + delayMicroseconds (DELAY) ; + gettimeofday (&t2, NULL) ; + + t = t2.tv_usec - t1.tv_usec ; + if (t > max) max = t ; + if (t < min) min = t ; + values [x] = t ; + } + + printf ("Done: Max: %d, min: %d\n", max, min) ; + + for (x = 0 ; x < CYCLES ; ++x) + { + printf ("%4d", values [x]) ; + if (values [x] > DELAY) + printf (".") ; + else if (values [x] < DELAY) + printf ("-") ; + else + printf (" ") ; + if (((x + 1) % 20) == 0) + printf ("\n") ; + } + printf ("\n") ; + + return 0 ; +} diff --git a/examples/gertboard.c b/examples/gertboard.c new file mode 100644 index 0000000..8344d48 --- /dev/null +++ b/examples/gertboard.c @@ -0,0 +1,47 @@ + +/* + * gertboard.c: + * Simple test for the SPI bus on the Gertboard + * + * Hardware setup: + * D/A port 0 jumpered to A/D port 0. + * + * We output a sine wave on D/A port 0 and sample A/D port 0. We then + * copy this value to D/A port 1 and use a 'scope on both D/A ports + * to check all's well. + * + */ + +#include +#include +//#include +#include + +#include +#include + +int main (void) +{ + int angle ; + int h1 ; + uint32_t x1 ; + + printf ("Raspberry Pi Gertboard SPI test program\n") ; + + if (gertboardSPISetup () == -1) + return 1 ; + + for (;;) + { + for (angle = 0 ; angle < 360 ; ++angle) + { + h1 = (int)rint (sin ((double)angle * M_PI / 180.0) * 127.0 + 128.0) ; + gertboardAnalogWrite (0, h1) ; + + x1 = gertboardAnalogRead (0) ; + gertboardAnalogWrite (1, x1 >> 2) ; // 10-bit A/D, 8-bit D/A + } + } + + return 0 ; +} diff --git a/examples/gertboard.png b/examples/gertboard.png new file mode 100644 index 0000000000000000000000000000000000000000..03c5cdd63dbb163330d19400cb9d896211e86ae7 GIT binary patch literal 4834 zcmV<85*_V{P)Px#Fi=cXMfm*q0Q~p>ytw%M0JyvW_yG900Js4B006uIczk&Hpa1{>`1k+-00000 z0000003YmlivR!s32;bRa{vGf5&!@T5&_cPe*6Fc00(qQO+^RX2n!P-G8A#+egFUx zI7vi7RCwC$olTD9s1krZnpKbF1$wJ`5_jm9SN$X(qnC4zTIC&PJG-}?l=1|vx&z2+ zl`>0lmj^_E0RzTARqgDT&df?@772Vr1cbvFH}H#W&2zM!B+G2jWw!pux6B65neB8t zCpP$=*oe#VfdDXIT0#)HPVMzW4=|`V2Lj*z{P7Jy zJ@p&>a3Ch%lPUxB8-N43sH&eYw+q1S3^xM;@RkPQ08;(dRL#9CghUWkjDa|T1>urQ z17cXr7X$(wAc%S5yANWXdhgE~R32%_sxAeS5HFJ9ihLmos|3u}cB;=sxUAg=_X zU=c(ML;{J#$f4^Eh?WGPWTqenBL!j}Ckvu0Z$K`$n|`+-N-8gJdJn$8yrF~?2$v27 ztH-dNlOQmHFbm>TjM-{ef&BQE0XaDv0fV9dE+`qJTB_1-_|SXb;SIr@B&b%X><^@&Y4>wioqL?-2+X4Pn5Qy>KAa(z3E3 zCjvR8Bm(eW5>d7hgM3GT5k`HZ(piZR2qP2D*@)}4d>e@+vY(BlB=X%?E&_Qo-EW{; zUSCuM@q4ZD7kl?wK1k4OO+X+7alKXqazfpGO^+j1GW(As)b(l|I|t*4>aL8DzrhQB z)W)u>q$_X65M>qmg_;FUBEQDS{>%7UzYfT2D0I_TqavEV*9nG6I(E|=9`g5mD zrCV9_4K2g%Xb8j9FpjH3*=gsd3O$ z+g0A!`+5+CanUckZ@$!(HvkzB`(SZ3n^)aojRJWE`Y)tF48*V#2oQ+!f=3W769m!y zulkE4qUxi#2LcWRzcV0^0Wm!2&JxNimx^NROTAaK4ZiQM$gC=gZk7XJW>^Q>Ei#G?% z!8oD?VaAhwHOLuVUJc^LkvHoATM+8CfO;IQ@6&OF@rR3YP>{AddDD6hMqv}p4o%c9 z25J7T-2NIE`KZu~o` zR^x5$J*$?ke`BIh7DT8${R)Cp`u*?{fe54>gA#(OZ{Cj%(zcC&R`aw*e|!-Ag=j<@ zf>b^~x&2!?rMNE1HuE$f4GM-p)K_9O7*w<74vbw8h^af;##n8kpcJWkKRig=_#jx@ z5kyNwgE(tBZm;D)pa^n&V;3QaX`e=r)oVFukbJLon2$yZtr}q`s+RI3=}_^Gf)8ns(c8T%iReRHJ;@{hS!C?2 z)8*|Mt13yUUcK-Q(St^jpy`n)(wylfEa%(1XOd4&u4lqo~fV6gYZhxY44X%CN@&*#!IMeXkOdU3@VjSdTxAc z7$DheRT(?;vn_e%(m4{;klDoCePm^0_dyzAV?kn_8_yxr(6MSs$TYO5&N3^yJ=}N> zl^V^UQe$K_2;Vbg%4_S#q0*Wp{0tD81&LLQ=jtc&qALqo?g(;~K@CdA?j6U;qOtp3 z0WTT5gdHWqpi&Y1#w|BAhe~brK{n5zp^e2!qP{g`z8UTf7#8O+jdp z8JJ8dH!vj-o(BngEt&tFC=i!~%tPOmP?8|-;)_}kUT4CkhL%t(NY&W$&Jcz!^X1^o_Hi|1`i z6eYrItZwtjE{i=*L6>A@r&Ulw39z59C84%{d}YD|fD~0rW>xFwfUx5D=nv&Q{H^4< z!|-@0XStD`?Fqu6(ljMhHnfCVZRG(-u!`4q5dVunwOGNX#U3EK^1WG62Hf&19k|t` z{KaLORD`Ftp?PinszGEV5qjvwiU?%rz*~f=3^Ln!E(T#xsjU)XkUj$GLXg4gToR;k z2p_odJpQfBxbeJr2=CQ=0@8O82t*)O=Mo?cDjRy*tht8fP^qmG5HPFbgdqNkYhbmj zNd-n~L7=vN>p`?c`XMh7*9p{t@MN!51QBIHu?k}{HbSa3xPk>5=svH`C1Yx>z8WObVKuM5ZDof6$|iGOBU-aemV%BA8->f zw`4TW{+B1}#!|-4b_S`_7+Gr$^}ps3$dXfU@m#eTE%#K56_rEGSy&AS&1Li_I*gNQ zL@>FgF{}Pw=lY$MTD}#8rVE&}?a_2uh)z*AJesSvwFBL;&odxx2mq+xiwZbfie4CA5n218Z9pp+uBRamEoc<1)?}|sF z?qlp`jK7QyS!MPR0L$*SfQ+RMVf`keVo}0A*U+QFbYYhHSUx5Ey204xSUp<(Ix59w z$E|8%sv)kQm}F@Zc4L+kYvO*{T|azXHN@<~C-g>*ojAY&2eyYKRSG`vl;#IFE=ae{r0ivGd>G;3zFCIF@}Sp}=Ja$(Ouu76uO%UoCH^i2;wN0@ z#wS8m@ogXaL_@QL=%^AhA-XgOpNh^edzl-b0*QvyF4X8T=4ML0hO&#Jc`Q)DYuGl^ z({{r?ZakYRzH24vtCniM+cY75flVs{h3QiXQ}ge-ecX6H)fd-(iRqP-gs4G0*8J(M zJ&L$^RV}g60SFPmb4IS4suj2nFr}fW5lR1T@?s~eedFEd=%ly+&f5r9bL+j zPZ(WBlgqLXgv_Q{URS&EJfj9!Ms#seCZW!NhYFKXhCaSRD=vGr8*gr~mkH46e{ko< ztLE$elkQuT!J5`Z)sokEjLWr+F8i8^{VsLmCFJi)HFVLHgxT}nLv$5mHp|YJoomDh zWR)Awpm4i0C3pYY&iwtvRDJyGN{2jg7haAzDSNdWU#gxiN7`P?l$u?sMjZ=st6Cfi zGZs{7-j%-7vRAwDym-5_p>qu_OV!gda0$yF_Qgezbp1q4mQHpum2mCI-p)AqlQU*_((H}q60pUKk6j-{+~rLQG^pW#p<`Q5xVbHvcmdc3n)cAqI>s%h4N44-GK zr|Q?W8po7+E7nh6r(Hw*S+|WUdpii8dh5Ebfr99@{aT2o%SO*a4f zT$$yNEBi{$VgTZ=74JKaP2ct1muu|`UJF;q+LonTg~Kd*KMsiFd*gw03Z#3gtzLQ= zv@}U|!dyxZgqCk}Yhoja2tf3`;=gj3wO@+H%a2@e(t-pImA87ouUdNMY)0T;#9=5ApjG@m5bcs5cfmlu;PqOj z?sOy~nnZx^a8J|i3ggH7;UHWy9`H|_iv1o=wI1un|NnqI){Wo(@t#%d*I74yfg68l zi7avB?*pX5jbCVVAG>b+5;y)1BvR+b--BvZx$(#LwHCPX#~w#kxbcS^*&lBF!;OEq z@eeot;l@AQ_=g+6(v3glU0fe-{KJiZxbY7+{^7j~vk@f4wA5FDZuN!}uUTf>R@y8iQR;(L;$dP^Hy7326 zt@j(*5AOYbIwnZF7^FF*YKavfpDMRzOnLt2(>rg?a**m@jEBy*^SRiXO|xAj?*?+a ze58q-mw`M-+e!GLi!hFn0cm#F+7w8TAgtFp+AhsAJi`g#_4x$P&+7ezw@ejuO%`5p zX5z0JM5M~a|4u7L_lKvV@zP;eBd?Zi0mNowenzou{q^N`hKpA43uqm`AqYNBucue_ z*Mfitg7>%8ASvw~?|vJ|l4=09K`9+Udo3(xKw2M!Zal9+@Nu~rsAj~V7lE`s2<;(= z?sxy7ck?2dHTI;8y`!(Moere6(gR;0x{fypUj{^gxnnDVw1Pxx0M7Jx3Zfp*Y1P7f zZkPWmabDD`rFZkP7oj%LjB2JYPPHvS=g9CJfE9&($Mmh2AHC2+fAG9V_e+gaSeJX(4;4-7SfuorBTO}JX1Gz7hzPJs{d2i&4Z4g~AvY(s;n zeFdV$pe6B&f72kCjaDseuhwrnb4jp3iL|yC^CW`n1T^%ry<8-bSRR*41_Zw)S)A?P z7zAyk72u1}wz2_14Q_pH#x$*!h}YguB7T<<*+@%WJH%F!2Em_4AjU=r#P78l*G&T5 zQhc`8G9WF8QMH;#wIUEl+?L0Y7$oeq&R(yjY($%hKaS|kTXZwVQ^R=>v=0H+u(4v} z9dX?lLY~WXsLg}WnH+biaYOD8jw8xX8pn}EBYS#4Ji^HSKVc*y-#-4=i~s-t07*qo IM6N<$f;V*&MF0Q* literal 0 HcmV?d00001 diff --git a/examples/lcd.c b/examples/lcd.c new file mode 100644 index 0000000..6024917 --- /dev/null +++ b/examples/lcd.c @@ -0,0 +1,129 @@ +/* + * lcd.c: + * Text-based LCD driver. + * This is designed to drive the parallel interface LCD drivers + * based in the Hitachi HD44780U controller and compatables. + * + * 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 + +#include +#include +#include + +#include +#include + +int main (void) +{ + int i, j ; + int fd1, fd2 ; + + char message1 [256] ; + char message2 [256] ; + char buf1 [30] ; + char buf2 [30] ; + + struct tm *t ; + time_t tim ; + + printf ("Raspberry Pi LCD test program\n") ; + + if (wiringPiSetup () == -1) + exit (1) ; + + fd1 = lcdInit (4, 20, 4, 8, 9, 4,5,6,7,0,0,0,0) ; + fd2 = lcdInit (2, 16, 4, 8, 10, 4,5,6,7,0,0,0,0) ; + +//fd1 = lcdInit (4, 20, 8, 8, 9, 0,1,2,3,4,5,6,7) ; +//fd2 = lcdInit (2, 16, 8, 8, 10, 0,1,2,3,4,5,6,7) ; + + if (fd1 == -1) + { + printf ("lcdInit 1 failed\n") ; + return 1 ; + } + + if (fd2 == -1) + { + printf ("lcdInit 2 failed\n") ; + return 1 ; + } + + sleep (1) ; + + lcdPosition (fd1, 0, 0) ; lcdPuts (fd1, " Gordon Henderson") ; + lcdPosition (fd1, 0, 1) ; lcdPuts (fd1, " --------------") ; +/* + lcdPosition (fd1, 0, 2) ; lcdPuts (fd1, " 00:00:00") ; + lcdPosition (fd1, 0, 3) ; lcdPuts (fd1, " DD:MM:YY") ; +*/ + + lcdPosition (fd2, 0, 0) ; lcdPuts (fd2, "Gordon Henderson") ; + lcdPosition (fd2, 0, 1) ; lcdPuts (fd2, "----------------") ; + + sleep (2) ; + + sprintf (message1, "%s", " http://projects.drogon.net/ ") ; + sprintf (message2, "%s", " This is a long message to go into the smaller display just for a demonstration of what we can do. ") ; + + for (;;) + { + i = 0 ; + j = 0 ; + for (;;) + { + strncpy (buf1, &message1 [i], 20) ; + buf1 [20] = 0 ; + lcdPosition (fd1, 0, 1) ; + lcdPuts (fd1, buf1) ; + ++i ; + if (i == strlen (message1) - 20) + i = 0 ; + + strncpy (buf2, &message2 [j], 16) ; + buf2 [16] = 0 ; + lcdPosition (fd2, 0, 1) ; + lcdPuts (fd2, buf2) ; + ++j ; + if (j == strlen (message2) - 16) + j = 0 ; + + tim = time (NULL) ; + t = localtime (&tim) ; + + sprintf (buf1, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ; + lcdPosition (fd1, 5, 2) ; + lcdPuts (fd1, buf1) ; + + sprintf (buf1, "%02d/%02d/%02d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ; + lcdPosition (fd1, 4, 3) ; + lcdPuts (fd1, buf1) ; + + delay (250) ; + } + } + + return 0 ; +} diff --git a/examples/nes.c b/examples/nes.c new file mode 100644 index 0000000..1a485bd --- /dev/null +++ b/examples/nes.c @@ -0,0 +1,44 @@ + +#include +#include +#include + +#include +#include + +#define BLANK "| " + +int main () +{ + int joystick ; + unsigned int buttons ; + + if (wiringPiSetup () == -1) + { + fprintf (stdout, "oops: %s\n", strerror (errno)) ; + return 1 ; + } + + if ((joystick = setupNesJoystick (2, 1, 0)) == -1) + { + fprintf (stdout, "Unable to setup joystick\n") ; + return 1 ; + } + + for (;;) + { + buttons = readNesJoystick (joystick) ; + + if ((buttons & NES_UP) != 0) printf ("| UP " ) ; else printf (BLANK) ; + if ((buttons & NES_DOWN) != 0) printf ("| DOWN " ) ; else printf (BLANK) ; + if ((buttons & NES_LEFT) != 0) printf ("| LEFT " ) ; else printf (BLANK) ; + if ((buttons & NES_RIGHT) != 0) printf ("|RIGHT " ) ; else printf (BLANK) ; + if ((buttons & NES_SELECT) != 0) printf ("|SELECT" ) ; else printf (BLANK) ; + if ((buttons & NES_START) != 0) printf ("|START " ) ; else printf (BLANK) ; + if ((buttons & NES_A) != 0) printf ("| A " ) ; else printf (BLANK) ; + if ((buttons & NES_B) != 0) printf ("| B " ) ; else printf (BLANK) ; + printf ("|\n") ; + } + + return 0 ; +} 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 new file mode 100644 index 0000000..2f5d990 --- /dev/null +++ b/examples/speed.c @@ -0,0 +1,105 @@ + +/* + * speed.c: + * Simple program to measure the speed of the various GPIO + * access mechanisms. + */ + +#include + +#include +#include +#include + +#define FAST_COUNT 10000000 +#define SLOW_COUNT 1000000 + + +int main (void) +{ + int i ; + uint32_t start, end, count, sum, perSec ; + + printf ("Raspberry Pi wiringPi speed test program\n") ; + +// Start the standard way + + if (wiringPiSetup () == -1) + exit (1) ; + + printf ("Native wiringPi method: (%8d iterations)\n", FAST_COUNT) ; + + pinMode (0, OUTPUT) ; + + sum = 0 ; + for (i = 0 ; i < 3 ; ++i) + { + printf (" Pass: %d: ", i) ; + fflush (stdout) ; + + start = millis () ; + for (count = 0 ; count < FAST_COUNT ; ++count) + digitalWrite (0, 1) ; + end = millis () ; + printf (" %8dmS\n", end - start) ; + sum += (end - start) ; + } + digitalWrite (0, 0) ; + printf (" Average: %8dmS", sum / 3) ; + perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ; + printf (": %6d/sec\n", perSec) ; + + + printf ("Native GPIO method: (%8d iterations)\n", FAST_COUNT) ; + + if (wiringPiSetupGpio () == -1) + exit (1) ; + + pinMode (17, OUTPUT) ; + + sum = 0 ; + for (i = 0 ; i < 3 ; ++i) + { + printf (" Pass: %d: ", i) ; + fflush (stdout) ; + + start = millis () ; + for (count = 0 ; count < 10000000 ; ++count) + digitalWrite (17, 1) ; + end = millis () ; + printf (" %8dmS\n", end - start) ; + sum += (end - start) ; + } + digitalWrite (17, 0) ; + printf (" Average: %8dmS", sum / 3) ; + perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ; + printf (": %6d/sec\n", perSec) ; + + +// Switch to SYS mode: + + if (wiringPiSetupSys () == -1) + exit (1) ; + + printf ("/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ; + + sum = 0 ; + for (i = 0 ; i < 3 ; ++i) + { + printf (" Pass: %d: ", i) ; + fflush (stdout) ; + + start = millis () ; + for (count = 0 ; count < SLOW_COUNT ; ++count) + digitalWrite (17, 1) ; + end = millis () ; + printf (" %8dmS\n", end - start) ; + sum += (end - start) ; + } + digitalWrite (17, 0) ; + printf (" Average: %8dmS", sum / 3) ; + perSec = (int)(double)SLOW_COUNT / (double)((double)sum / 3.0) * 1000.0 ; + printf (": %6d/sec\n", perSec) ; + + return 0 ; +} diff --git a/examples/test1.c b/examples/test1.c new file mode 100644 index 0000000..7eb0abd --- /dev/null +++ b/examples/test1.c @@ -0,0 +1,91 @@ + +/* + * test1.c: + * Simple test program to test the wiringPi functions + */ + +#include + +#include +#include +#include + + +// Simple sequencer data +// Triplets of LED, On/Off and delay + +uint8_t data [] = +{ + 0, 1, 1, + 1, 1, 1, + 0, 0, 0, 2, 1, 1, + 1, 0, 0, 3, 1, 1, + 2, 0, 0, 4, 1, 1, + 3, 0, 0, 5, 1, 1, + 4, 0, 0, 6, 1, 1, + 5, 0, 0, 7, 1, 1, + 6, 0, 1, + 7, 0, 1, + + 0, 0, 1, // Extra delay + +// Back again + + 7, 1, 1, + 6, 1, 1, + 7, 0, 0, 5, 1, 1, + 6, 0, 0, 4, 1, 1, + 5, 0, 0, 3, 1, 1, + 4, 0, 0, 2, 1, 1, + 3, 0, 0, 1, 1, 1, + 2, 0, 0, 0, 1, 1, + 1, 0, 1, + 0, 0, 1, + + 0, 0, 1, // Extra delay + + 9, 9, 9, // End marker + +} ; + + +int main (void) +{ + int pin ; + int dataPtr ; + int l, s, d ; + + printf ("Raspberry Pi wiringPi test program\n") ; + + if (wiringPiSetup () == -1) + exit (1) ; + + for (pin = 0 ; pin < 8 ; ++pin) + pinMode (pin, OUTPUT) ; + + pinMode (8, INPUT) ; // Pin 8 SDA0 - Has on-board 2k2 pull-up resistor + + dataPtr = 0 ; + + for (;;) + { + l = data [dataPtr++] ; // LED + s = data [dataPtr++] ; // State + d = data [dataPtr++] ; // Duration (10ths) + + if ((l + s + d) == 27) + { + dataPtr = 0 ; + continue ; + } + + digitalWrite (l, s) ; + + if (digitalRead (8) == 0) // Pressed as our switch shorts to ground + delay (d * 10) ; // Faster! + else + delay (d * 100) ; + } + + return 0 ; +} diff --git a/examples/test2.c b/examples/test2.c new file mode 100644 index 0000000..e34013c --- /dev/null +++ b/examples/test2.c @@ -0,0 +1,41 @@ + +/* + * test2.c: + * Simple test program to test the wiringPi functions + * PWM test + */ + +#include + +#include +#include +#include + +int main (void) +{ + int bright ; + + printf ("Raspberry Pi wiringPi PWM test program\n") ; + + if (wiringPiSetup () == -1) + exit (1) ; + + pinMode (1, PWM_OUTPUT) ; + + for (;;) + { + for (bright = 0 ; bright < 1024 ; ++bright) + { + pwmWrite (1, bright) ; + delay (1) ; + } + + for (bright = 1023 ; bright >= 0 ; --bright) + { + pwmWrite (1, bright) ; + delay (1) ; + } + } + + return 0 ; +} 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 new file mode 100644 index 0000000..c0740c9 --- /dev/null +++ b/gpio/Makefile @@ -0,0 +1,73 @@ +# +# Makefile: +# wiringPi - Wiring Compatable library for the Raspberry Pi +# https://projects.drogon.net/wiring-pi +# +# Copyright (c) 2012 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# + + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LIBS = -lwiringPi + +# Should not alter anything below this line +############################################################################### + +SRC = gpio.c + +OBJ = gpio.o + +all: gpio + +gpio: gpio.o + @echo [LD] + @$(CC) -o $@ gpio.o $(LDFLAGS) $(LIBS) + +.c.o: + @echo [CC] $< + @$(CC) -c $(CFLAGS) $< -o $@ + +clean: + rm -f $(OBJ) gpio *~ core tags + +tags: $(SRC) + @echo [ctags] + @ctags $(SRC) + +depend: + makedepend -Y $(SRC) + +install: + cp gpio /usr/local/bin + chown root.root /usr/local/bin/gpio + chmod 4755 /usr/local/bin/gpio + mkdir -p /usr/local/man/man1 + cp gpio.1 /usr/local/man/man1 + +uninstall: + rm -f /usr/local/bin/gpio + rm -f /usr/local/man/man1/gpio.1 + +# DO NOT DELETE diff --git a/gpio/gpio.1 b/gpio/gpio.1 new file mode 100644 index 0000000..7da64b2 --- /dev/null +++ b/gpio/gpio.1 @@ -0,0 +1,179 @@ +.TH "GPIO" "14 June 2012" "Command-Line access to Raspberry Pi and PiFace GPIO" + +.SH NAME +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 [ \-p ] +.RB < read/write/mode ...> +.TP +.B gpio +.RB < export/edge/unexport/unexportall/exports ...> + +.SH DESCRIPTION + +.B GPIO +is a command line tool to allow the user easy access to the GPIO pins +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 \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. + +.TP +.B \-p +Use the PiFace interface board and its corresponding pin numbers. + +.TP +.B read +Read the digital value of the given pin and print 0 or 1 to represent the +respective logic levels. + +.TP +.B write +Write the given value (0 or 1) to the pin. + +.TP +.B pwm +Write a PWM value (0-1023) to the given pin. + +.TP +.B mode +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 \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 \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. + +.SH "WiringPi vs. GPIO Pin numbering" + +.PP +.TS +r r l. +WiringPi GPIO Function +_ +0 17 +1 18 (PWM) +2 21 +3 22 +4 23 +5 24 +6 25 +7 4 +8 0 SDA0 +9 1 SCL0 +10 8 SPI CE0 +11 7 SPI CE1 +12 10 SPI MOSI +13 9 SPI MISO +14 11 SPI SCLK +15 14 TxD +16 15 RxD +.TE + +.SH FILES + +.TP 2.2i +.I gpio +executable + +.SH EXAMPLES +.TP 2.2i +gpio mode 4 output # Set pin 4 to output +.PP +gpio -g mode 23 output # Set GPIO pin 23 to output (same as WiringPi pin 4) +.PP +gpio mode 1 pwm # Set pin 1 to PWM mode +.PP +gpio pwm 1 512 # Set pin 1 to PWM value 512 - half brightness +.PP +gpio export 17 out # Set GPIO Pin 17 to output +.PP +gpio export 0 in # Set GPIO Pin 0 (SDA0) to input. +.PP +gpio -g read 0 # Read GPIO Pin 0 (SDA0) + +.SH "NOTES" + +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" + +.LP +WiringPi's home page +.IP +https://projects.drogon.net/raspberry-pi/wiringpi/ + +.SH AUTHOR + +Gordon Henderson + +.SH "REPORTING BUGS" + +Report bugs to + +.SH COPYRIGHT + +Copyright (c) 2012 Gordon Henderson +This is free software; see the source for copying conditions. There is NO +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. diff --git a/gpio/gpio.c b/gpio/gpio.c new file mode 100644 index 0000000..54dfbc3 --- /dev/null +++ b/gpio/gpio.c @@ -0,0 +1,755 @@ +/* + * gpio.c: + * Set-UID command-line interface to the Raspberry Pi's GPIO + * 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 +#include +#include +#include +#include +#include +#include + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + +#define VERSION "1.1" + +static int wpMode ; + +char *usage = "Usage: gpio -v\n" + " gpio [-g] ...\n" + " gpio [-p] ...\n" + " gpio export/edge/unexport/unexportall/exports ...\n" + " gpio drive \n" + " gpio load spi/i2c" ; + + + +/* + * changeOwner: + * Change the ownership of the file to the real userId of the calling + * program so we can access it. + ********************************************************************************* + */ + +static void changeOwner (char *cmd, char *file) +{ + uid_t uid = getuid () ; + uid_t gid = getgid () ; + + if (chown (file, uid, gid) != 0) + { + if (errno == ENOENT) // Warn that it's not there + fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ; + else + { + fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ; + exit (1) ; + } + } +} + + +/* + * moduleLoaded: + * Return true/false if the supplied module is loaded + ********************************************************************************* + */ + +static int moduleLoaded (char *modName) +{ + int len = strlen (modName) ; + int found = FALSE ; + FILE *fd = fopen ("/proc/modules", "r") ; + char line [80] ; + + if (fd == NULL) + { + fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ; + exit (1) ; + } + + while (fgets (line, 80, fd) != NULL) + { + if (strncmp (line, modName, len) != 0) + continue ; + + found = TRUE ; + break ; + } + + fclose (fd) ; + + return found ; +} + + +/* + * doLoad: + * Load either the spi or i2c modules and change device ownerships, etc. + ********************************************************************************* + */ + +static void _doLoadUsage (char *argv []) +{ + fprintf (stderr, "Usage: %s load \n", argv [0]) ; + exit (1) ; +} + +static void doLoad (int argc, char *argv []) +{ + char *module ; + char cmd [80] ; + char *file1, *file2 ; + + if (argc != 3) + _doLoadUsage (argv) ; + + /**/ if (strcasecmp (argv [2], "spi") == 0) + { + module = "spi_bcm2708" ; + file1 = "/dev/spidev0.0" ; + file2 = "/dev/spidev0.1" ; + } + else if (strcasecmp (argv [2], "i2c") == 0) + { + module = "i2c_bcm2708" ; + file1 = "/dev/i2c-0" ; + file2 = "/dev/i2c-1" ; + } + else + _doLoadUsage (argv) ; + + if (!moduleLoaded (module)) + { + sprintf (cmd, "modprobe %s", module) ; + system (cmd) ; + } + + if (!moduleLoaded (module)) + { + fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ; + exit (1) ; + } + + sleep (1) ; // To let things get settled + + changeOwner (argv [0], file1) ; + changeOwner (argv [0], file2) ; +} + + + +/* + * doExports: + * List all GPIO exports + ********************************************************************************* + */ + +void doExports (void) +{ + int fd ; + int i, l, first ; + char fName [128] ; + char buf [16] ; + +// Rather crude, but who knows what others are up to... + + for (first = 0, i = 0 ; i < 64 ; ++i) + { + +// Try to read the direction + + sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ; + if ((fd = open (fName, O_RDONLY)) == -1) + continue ; + + if (first == 0) + { + ++first ; + printf ("GPIO Pins exported:\n") ; + } + + printf ("%4d: ", i) ; + + 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 ("%-3s", buf) ; + + close (fd) ; + +// Try to Read the value + + sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ; + if ((fd = open (fName, O_RDONLY)) == -1) + { + printf ("No Value file (huh?)\n") ; + 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 ; + + 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 ("\n") ; + 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 ; + + printf (" %-8s\n", buf) ; + + close (fd) ; + } +} + + +/* + * doExport: + * gpio export pin mode + * This uses the /sys/class/gpio device interface. + ********************************************************************************* + */ + +void doExport (int argc, char *argv []) +{ + FILE *fd ; + int pin ; + char *mode ; + char fName [128] ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + mode = argv [3] ; + + 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) ; + } + + /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0)) + fprintf (fd, "in\n") ; + else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0)) + fprintf (fd, "out\n") ; + else + { + fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ; + exit (1) ; + } + + fclose (fd) ; + +// Change ownership so the current user can actually use it! + + sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; + changeOwner (argv [0], fName) ; + + sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ; + changeOwner (argv [0], fName) ; + +} + + +/* + * 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) ; +} + + +/* + * doUnexport: + * gpio unexport pin + * This uses the /sys/class/gpio device interface. + ********************************************************************************* + */ + +void doUnexport (int argc, char *argv []) +{ + FILE *fd ; + int pin ; + + if (argc != 3) + { + fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + 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) ; +} + + +/* + * 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 ... + ********************************************************************************* + */ + +void doMode (int argc, char *argv []) +{ + int pin ; + char *mode ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) + return ; + + mode = argv [3] ; + + /**/ if (strcasecmp (mode, "in") == 0) + pinMode (pin, INPUT) ; + else if (strcasecmp (mode, "out") == 0) + pinMode (pin, OUTPUT) ; + else if (strcasecmp (mode, "pwm") == 0) + pinMode (pin, PWM_OUTPUT) ; + else if (strcasecmp (mode, "up") == 0) + pullUpDnControl (pin, PUD_UP) ; + else if (strcasecmp (mode, "down") == 0) + pullUpDnControl (pin, PUD_DOWN) ; + else if (strcasecmp (mode, "tri") == 0) + pullUpDnControl (pin, PUD_OFF) ; + else + { + fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ; + exit (1) ; + } +} + + +/* + * doPadDrive: + * gpio drive group value + ********************************************************************************* + */ + +void doPadDrive (int argc, char *argv []) +{ + int group, val ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ; + exit (1) ; + } + + group = atoi (argv [2]) ; + val = atoi (argv [3]) ; + + if ((group < 0) || (group > 2)) + { + fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ; + exit (1) ; + } + + if ((val < 0) || (val > 7)) + { + fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ; + exit (1) ; + } + + setPadDrive (group, val) ; +} + + +/* + * doWrite: + * gpio write pin value + ********************************************************************************* + */ + +void doWrite (int argc, char *argv []) +{ + int pin, val ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) + return ; + + val = atoi (argv [3]) ; + + /**/ if (val == 0) + digitalWrite (pin, LOW) ; + else + digitalWrite (pin, HIGH) ; +} + + +/* + * doRead: + * Read a pin and return the value + ********************************************************************************* + */ + +void doRead (int argc, char *argv []) +{ + int pin, val ; + + if (argc != 3) + { + fprintf (stderr, "Usage: %s read pin\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) + { + printf ("0\n") ; + return ; + } + + val = digitalRead (pin) ; + + printf ("%s\n", val == 0 ? "0" : "1") ; +} + + +/* + * doPwm: + * Output a PWM value on a pin + ********************************************************************************* + */ + +void doPwm (int argc, char *argv []) +{ + int pin, val ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s pwm \n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) + return ; + + val = atoi (argv [3]) ; + + pwmWrite (pin, val) ; +} + + +/* + * main: + * Start here + ********************************************************************************* + */ + +int main (int argc, char *argv []) +{ + int i ; + + if (argc == 1) + { + fprintf (stderr, "%s: %s\n", argv [0], usage) ; + return 1 ; + } + + if (strcasecmp (argv [1], "-v") == 0) + { + printf ("gpio version: %s\n", VERSION) ; + printf ("Copyright (c) 2012 Gordon Henderson\n") ; + printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ; + printf ("For details type: %s -warranty\n", argv [0]) ; + return 0 ; + } + + if (strcasecmp (argv [1], "-warranty") == 0) + { + printf ("gpio version: %s\n", VERSION) ; + printf ("Copyright (c) 2012 Gordon Henderson\n") ; + printf ("\n") ; + printf (" This program is free software; you can redistribute it and/or modify\n") ; + printf (" it under the terms of the GNU Leser General Public License as published\n") ; + printf (" by the Free Software Foundation, either version 3 of the License, or\n") ; + printf (" (at your option) any later version.\n") ; + printf ("\n") ; + printf (" This program is distributed in the hope that it will be useful,\n") ; + printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ; + printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ; + printf (" GNU Lesser General Public License for more details.\n") ; + printf ("\n") ; + printf (" You should have received a copy of the GNU Lesser General Public License\n") ; + printf (" along with this program. If not, see .\n") ; + printf ("\n") ; + return 0 ; + } + + if (geteuid () != 0) + { + fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ; + return 1 ; + } + +// Initial test for /sys/class/gpio operations: + + /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports () ; return 0 ; } + else if (strcasecmp (argv [1], "export" ) == 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 ; } + else if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; } + +// Check for -g argument + + if (strcasecmp (argv [1], "-g") == 0) + { + if (wiringPiSetupGpio () == -1) + { + fprintf (stderr, "%s: Unable to initialise GPIO in GPIO mode.\n", argv [0]) ; + exit (1) ; + } + + for (i = 2 ; i < argc ; ++i) + argv [i - 1] = argv [i] ; + --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) + { + fprintf (stderr, "%s: Unable to initialise GPIO in wiringPi mode\n", argv [0]) ; + exit (1) ; + } + wpMode = WPI_MODE_PINS ; + } + +// Check for wiring commands + + /**/ if (strcasecmp (argv [1], "write" ) == 0) + doWrite (argc, argv) ; + else if (strcasecmp (argv [1], "read" ) == 0) + doRead (argc, argv) ; + else if (strcasecmp (argv [1], "mode" ) == 0) + doMode (argc, argv) ; + else if (strcasecmp (argv [1], "pwm" ) == 0) + doPwm (argc, argv) ; + else if (strcasecmp (argv [1], "drive" ) == 0) + doPadDrive (argc, argv) ; + else + { + fprintf (stderr, "%s: Unknown command: %s. (read/write/pwm/mode/drive expected)\n", argv [0], argv [1]) ; + exit (1) ; + } + return 0 ; +} diff --git a/gpio/test.sh b/gpio/test.sh new file mode 100755 index 0000000..7127528 --- /dev/null +++ b/gpio/test.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Simple test - assumes LEDs on Pins 0-7. + +for i in `seq 0 7`; +do + gpio mode $i out +done + +while true; +do + for i in `seq 0 7`; + do + gpio write $i 1 + sleep 0.1 + done + + for i in `seq 0 7`; + do + gpio write $i 0 + sleep 0.1 + done +done diff --git a/wiringPi/.wiringPiFace.c.swp b/wiringPi/.wiringPiFace.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..3eff7deff308d27f398bd919c64bb8119b850554 GIT binary patch literal 16384 zcmeI2Ym6IL6~~7rO|mT^fr3*2M{8mh=+Iv1mY8@Mev~#YJ~(J5J5aDgoMKX+ zHdP{}NJTTsFKeHB?wxbaea;!T^C#3Lb|5>W;M%Py$M)^6of_Yvd~jM(y0&ZB78Shy z)z?m)vR_|CmVP2^Fz2~?Ze6&->ej@SAIh4}v-&kOm#U-@NF#7v5!k8B&P?a`jNid- zd&ly1)l8Ms2&556BalWQjX)ZKGy-V^(g>sxc=JcVceW^(;KohhQ{XKTmvEiI=cl6Y zTk!WVn1BN5q*oe&Gy-V^(g>sxNF$I&AdNs8fiwbX1kwnk5lAEO=81r2D9R3)dxR1I zc>hm1fdA}Lls|*7g45t}a4*;fwt(-xO;Mf&ZO{U@gBN!y%G00;?gzWTFW;&tPl83D zf}6naZ&j490Rub&THs-DA2WX!C4?c59|lGfxnI^%Fn@B z@MX{k8-N1?ydPYIlYarf0_VUtz;obp;7M>AIN&3o4D#STU>o?`c13v^`~rLvoClu= zPXix3432|+fPq`UYugm%RqzV90Dc0_f=>e%JOJ(m6JQtk=go@p5AYlC5;zaefiHqH z;3?n%7kms@;1MtmE^bwn=fSh!vw#B~dR2OiUOoqHZz<$6!EgtwX{ z{hr%8@zkBIXm zo?j}qR_bcA6gB3%f$mdmS_p8@QlU7O-`F=DOUIJVAaPz%ayd3{udZsA!F1c;o|0%{tXC^7wW2o1 zGU4Y&>HcaB!9$%Q?`VPPGhqo|Xr}Pc1DIp0XkxU5<;rrSL{{VnR>W;LYt=<6nHjlj zwOA+dz7g^9La9RXgCp{y+Gs4Upkz8fQX)-8!OUpEl3K4<=_Uw56tS_;s<}etzSdH; zSVG~!FhX;f*Kk$#rc_E6mVvX?#X@s#jBL0kX>g&YRzulrB2z2P`9dv}4h5vrEK6Cw zoMJhwGv7{kVuQA@`=J?C_Ru!ara8I1Z6~QV3UfFhnhn+Rf}pp6ykSz zaX84Mg7j$659CoO8|pIYQ79b>COnd|d?{&>JPKv=i7fOeln(ke^eB|>Pjn-X3gpqi zCh{oM9I}Z#3Weyx*dy6@RquSjtIa>d)+|eRhru~FkTeNk}^rAaQ)?l^OPcgg0 z_!@Wneh+I@QlEUA-j9_R7upGT9`gHedwMc2{jf&3DfAP!PbS}!++uLZ)25{ zcEG%z9hgQqo?WhUu|^#dP7jPoI^!)=mf0dsc9=g4+`>>dSuJSej<78<@2V<^86Dfj zMuh2Ra9IxDbRq25&Imlq3lR$H#u zs*O^Xu?E5b)1te#1D~y^&EwVOCM#4#0v@hwE{-^U zAG0oMBn}RpYz>@GU4dqy8SOAVOmbQ(u)F$b)--AS;!xNL%*jCK?EuGI zhqmDfrdj=Hcw?G-5XJrAP&B+p=IcI%Q30GN41xCEP8Pg#K+;a;2K!E zZOqA42Rj@VQMwHb5D?3%nC-2RDPQfbs%=Kpx;#@B{FD@O5wod;yps3#Py& z9<$mB=GJ}I} z>zeS(wyRPazzrN9DTPr{bf~7xy}S(36~1Q5R}4q55RGa>!C7Ao)4GxNkZ43ZYI<0E zSQKg_)uJQMicAMJ6qx3+Q?%x7%Xe)P=8uXtsf2p+a)c#wEd#q)Buea3NHGdo_{KnV zSZ0IWfm{&f&gm(HZQ!=8_(`^b_&QPj480I}R3V*BiV^n(N7ywyp|K2Q?9m{e9NwsG zd>pss^>B0tv=fy5kJ@nus9Y`L%22WX(7f*8BG7&I%gwuP%8Gh&)3c`?=QC9x{ zViaEA@C&tREYWps<1Trb!7?)aJ;{))&0|E3(Mt!sW}rt>%5KmcmVi2Wwp^g$WWOKNgBt;J4CzFp|Sjijr76r-lXpbhN?36PL`I-k)Ji`51Ts1 zRLco`WJ`uUlNAb=xh5+Vr~-1*vc_l?yfm0(@mm+QNmG>4o`Yc@Cnhcn$<(y$Lz?}Y gzYDDwTT6upT8&z%RBRppaQH%0riCZi5n3$%3;&rqhX4Qo literal 0 HcmV?d00001 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 new file mode 100644 index 0000000..51f4773 --- /dev/null +++ b/wiringPi/Makefile @@ -0,0 +1,101 @@ +# +# Makefile: +# wiringPi - Wiring Compatable library for the Raspberry Pi +# +# 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 . +################################################################################# + + +TARGET=libwiringPi.a + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I. +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LIBS = + +# Should not alter anything below this line +############################################################################### + +SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ + gertboard.c \ + piNes.c \ + lcd.c piHiPri.c piThread.c + +OBJ = wiringPi.o wiringPiFace.o wiringSerial.o wiringShift.o \ + gertboard.o \ + piNes.o \ + lcd.o piHiPri.o piThread.o + +all: $(TARGET) + +$(TARGET): $(OBJ) + @echo [AR] $(OBJ) + @ar rcs $(TARGET) $(OBJ) + @ranlib $(TARGET) + @size $(TARGET) + +.c.o: + @echo [CC] $< + @$(CC) -c $(CFLAGS) $< -o $@ + +clean: + rm -f $(OBJ) $(TARGET) *~ core tags Makefile.bak + +tags: $(SRC) + @echo [ctags] + @ctags $(SRC) + +depend: + makedepend -Y $(SRC) + +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 wiringSerial.h /usr/local/include + install -m 0644 wiringShift.h /usr/local/include + install -m 0644 gertboard.h /usr/local/include + install -m 0644 piNes.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 + + + +# DO NOT DELETE + +wiringPi.o: wiringPi.h +wiringPiFace.o: wiringPi.h +wiringSerial.o: wiringSerial.h +wiringShift.o: wiringPi.h wiringShift.h +gertboard.o: gertboard.h +piNes.o: wiringPi.h piNes.h +lcd.o: wiringPi.h lcd.h +piHiPri.o: wiringPi.h +piThread.o: wiringPi.h diff --git a/wiringPi/README b/wiringPi/README new file mode 100644 index 0000000..052cd41 --- /dev/null +++ b/wiringPi/README @@ -0,0 +1,9 @@ + +WiringPi: An implementation of most of the Arduino Wiring + functions for the Raspberry Pi + +-- And a lot lot more! + +Full details at: + https://projects.drogon.net/raspberry-pi/wiringpi/ + diff --git a/wiringPi/gertboard.c b/wiringPi/gertboard.c new file mode 100644 index 0000000..bd7e60a --- /dev/null +++ b/wiringPi/gertboard.c @@ -0,0 +1,184 @@ +/* + * gertboard.c: + * Access routines for the SPI devices on the Gertboard + * Copyright (c) 2012 Gordon Henderson + * + * The Gertboard has: + * + * An MCP3002 dual-channel A to D convertor connected + * to the SPI bus, selected by chip-select A, and: + * + * An MCP4802 dual-channel D to A convertor connected + * to the SPI bus, selected via chip-select B. + * + *********************************************************************** + * 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 "gertboard.h" + + +// The SPI bus parameters +// Variables as they need to be passed as pointers later on + +static char *spiA2D = "/dev/spidev0.0" ; +static char *spiD2A = "/dev/spidev0.1" ; +static uint8_t spiMode = 0 ; +static uint8_t spiBPW = 8 ; +static uint32_t spiSpeed = 100000 ; // 1MHz +static uint16_t spiDelay = 0; + +// Locals here to keep track of everything + +static int spiFdA2D ; +static int spiFdD2A ; + + +/* + * gertboardAnalogWrite: + * Write an 8-bit data value to the MCP4802 Analog to digital + * convertor on the Gertboard. + ********************************************************************************* + */ + +void gertboardAnalogWrite (int chan, int value) +{ + uint8_t spiBufTx [2] ; + uint8_t spiBufRx [2] ; + struct spi_ioc_transfer spi ; + + uint8_t chanBits, dataBits ; + + if (chan == 0) + chanBits = 0x30 ; + else + chanBits = 0xB0 ; + + chanBits |= ((value >> 4) & 0x0F) ; + dataBits = ((value << 4) & 0xF0) ; + + spiBufTx [0] = chanBits ; + spiBufTx [1] = dataBits ; + + spi.tx_buf = (unsigned long)spiBufTx ; + spi.rx_buf = (unsigned long)spiBufRx ; + spi.len = 2 ; + spi.delay_usecs = spiDelay ; + spi.speed_hz = spiSpeed ; + spi.bits_per_word = spiBPW ; + + ioctl (spiFdD2A, SPI_IOC_MESSAGE(1), &spi) ; +} + + +/* + * gertboardAnalogRead: + * Return the analog value of the given channel (0/1). + * The A/D is a 10-bit device + ********************************************************************************* + */ + +int gertboardAnalogRead (int chan) +{ + uint8_t spiBufTx [4] ; + uint8_t spiBufRx [4] ; + struct spi_ioc_transfer spi ; + + uint8_t chanBits ; + + if (chan == 0) + chanBits = 0b0110100 ; + else + chanBits = 0b0111100 ; + + spiBufTx [0] = chanBits ; + spiBufTx [1] = 0 ; + + spi.tx_buf = (unsigned long)spiBufTx ; + spi.rx_buf = (unsigned long)spiBufRx ; + spi.len = 4 ; + spi.delay_usecs = spiDelay ; + spi.speed_hz = spiSpeed ; + spi.bits_per_word = spiBPW ; + + ioctl (spiFdA2D, SPI_IOC_MESSAGE(1), &spi) ; + + return spiBufRx [0] << 8 | spiBufRx [1] ; +} + + +/* + * setParams: + * Output the SPI bus parameters to the given device + ********************************************************************************* + */ + +static int setParams (int fd) +{ + if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) + return -1 ; + + if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0) + return -1 ; + + if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) + return -1 ; + + if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) + return -1 ; + + if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &spiSpeed) < 0) + return -1 ; + + if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &spiSpeed) < 0) + return -1 ; + + return 0 ; +} + + +/* + * gertboardSPISetup: + * Initialise the SPI bus, etc. + ********************************************************************************* + */ + +int gertboardSPISetup (void) +{ + if ((spiFdA2D = open (spiA2D, O_RDWR)) < 0) + return -1 ; + + if (setParams (spiFdA2D) != 0) + return -1 ; + + if ((spiFdD2A = open (spiD2A, O_RDWR)) < 0) + return -1 ; + + if (setParams (spiFdD2A) != 0) + return -1 ; + + return 0 ; +} diff --git a/wiringPi/gertboard.h b/wiringPi/gertboard.h new file mode 100644 index 0000000..98fd1e7 --- /dev/null +++ b/wiringPi/gertboard.h @@ -0,0 +1,39 @@ +/* + * gertboard.h: + * Access routines for the SPI devices on the Gertboard + * Copyright (c) 2012 Gordon Henderson + * + * The Gertboard has an MCP4802 dual-channel D to A convertor + * connected to the SPI bus, selected via chip-select B. + * + *********************************************************************** + * 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 void gertboardAnalogWrite (int chan, int value) ; +extern int gertboardAnalogRead (int chan) ; +extern int gertboardSPISetup (void) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/lcd.c b/wiringPi/lcd.c new file mode 100644 index 0000000..6826a60 --- /dev/null +++ b/wiringPi/lcd.c @@ -0,0 +1,365 @@ +/* + * lcd.c: + * Text-based LCD driver. + * This is designed to drive the parallel interface LCD drivers + * based in the Hitachi HD44780U controller and compatables. + * + * 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 +#include + +#include "wiringPi.h" +#include "lcd.h" + +// Commands + +#define LCD_CLEAR 0x01 +#define LCD_HOME 0x02 +#define LCD_ENTRY 0x04 +#define LCD_ON_OFF 0x08 +#define LCD_CDSHIFT 0x10 +#define LCD_FUNC 0x20 +#define LCD_CGRAM 0x40 +#define LCD_DGRAM 0x80 + +#define LCD_ENTRY_SH 0x01 +#define LCD_ENTRY_ID 0x02 + +#define LCD_ON_OFF_B 0x01 +#define LCD_ON_OFF_C 0x02 +#define LCD_ON_OFF_D 0x04 + +#define LCD_FUNC_F 0x04 +#define LCD_FUNC_N 0x08 +#define LCD_FUNC_DL 0x10 + +#define LCD_CDSHIFT_RL 0x04 + +struct lcdDataStruct +{ + uint8_t bits, rows, cols ; + uint8_t rsPin, strbPin ; + uint8_t dataPins [8] ; +} ; + +struct lcdDataStruct *lcds [MAX_LCDS] ; + + +/* + * strobe: + * Toggle the strobe (Really the "E") pin to the device. + * According to the docs, data is latched on the falling edge. + ********************************************************************************* + */ + +static void strobe (struct lcdDataStruct *lcd) +{ + digitalWrite (lcd->strbPin, 1) ; delayMicroseconds (1) ; + digitalWrite (lcd->strbPin, 0) ; delayMicroseconds (50) ; +} + + +/* + * sentDataCmd: + * Send an data or command byte to the display. + ********************************************************************************* + */ + +static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data) +{ + uint8_t i, d4 ; + + if (lcd->bits == 4) + { + d4 = (data >> 4) & 0x0F; + for (i = 0 ; i < 4 ; ++i) + { + digitalWrite (lcd->dataPins [i], (d4 & 1)) ; + d4 >>= 1 ; + } + strobe (lcd) ; + + d4 = data & 0x0F ; + for (i = 0 ; i < 4 ; ++i) + { + digitalWrite (lcd->dataPins [i], (d4 & 1)) ; + d4 >>= 1 ; + } + } + else + { + for (i = 0 ; i < 8 ; ++i) + { + digitalWrite (lcd->dataPins [i], (data & 1)) ; + data >>= 1 ; + } + } + strobe (lcd) ; +} + + +/* + * putCommand: + * Send a command byte to the display + ********************************************************************************* + */ + +static void putCommand (struct lcdDataStruct *lcd, uint8_t command) +{ + digitalWrite (lcd->rsPin, 0) ; + sendDataCmd (lcd, command) ; +} + +static void put4Command (struct lcdDataStruct *lcd, uint8_t command) +{ + uint8_t i ; + + digitalWrite (lcd->rsPin, 0) ; + + for (i = 0 ; i < 4 ; ++i) + { + digitalWrite (lcd->dataPins [i], (command & 1)) ; + command >>= 1 ; + } + strobe (lcd) ; +} + + +/* + ********************************************************************************* + * User Code below here + ********************************************************************************* + */ + +/* + * lcdHome: lcdClear: + * Home the cursor or clear the screen. + ********************************************************************************* + */ + +void lcdHome (int fd) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + putCommand (lcd, LCD_HOME) ; +} + +void lcdClear (int fd) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + putCommand (lcd, LCD_CLEAR) ; +} + + +/* + * lcdPosition: + * Update the position of the cursor on the display + ********************************************************************************* + */ + + +void lcdPosition (int fd, int x, int y) +{ + static uint8_t rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ; + struct lcdDataStruct *lcd = lcds [fd] ; + + putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ; +} + + +/* + * lcdPutchar: + * Send a data byte to be displayed on the display + ********************************************************************************* + */ + +void lcdPutchar (int fd, uint8_t data) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + + digitalWrite (lcd->rsPin, 1) ; + sendDataCmd (lcd, data) ; +} + + +/* + * lcdPuts: + * Send a string to be displayed on the display + ********************************************************************************* + */ + +void lcdPuts (int fd, char *string) +{ + while (*string) + lcdPutchar (fd, *string++) ; +} + + +/* + * lcdPrintf: + * Printf to an LCD display + ********************************************************************************* + */ + +void lcdPrintf (int fd, char *message, ...) +{ + va_list argp ; + char buffer [1024] ; + + va_start (argp, message) ; + vsnprintf (buffer, 1023, message, argp) ; + va_end (argp) ; + + lcdPuts (fd, buffer) ; +} + + +/* + * lcdInit: + * Take a lot of parameters and initialise the LCD, and return a handle to + * that LCD, or -1 if any error. + ********************************************************************************* + */ + +int lcdInit (int rows, int cols, int bits, int rs, int strb, + int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) +{ + static int initialised = 0 ; + + uint8_t func ; + int i ; + int lcdFd = -1 ; + struct lcdDataStruct *lcd ; + + if (initialised == 0) + { + initialised = 1 ; + for (i = 0 ; i < MAX_LCDS ; ++i) + lcds [i] = NULL ; + } + +// Simple sanity checks + + if (! ((bits == 4) || (bits == 8))) + return -1 ; + + if ((rows < 0) || (rows > 20)) + return -1 ; + + if ((cols < 0) || (cols > 20)) + return -1 ; + +// Create a new LCD: + + for (i = 0 ; i < MAX_LCDS ; ++i) + { + if (lcds [i] == NULL) + { + lcdFd = i ; + break ; + } + } + + if (lcdFd == -1) + return -1 ; + + lcd = malloc (sizeof (struct lcdDataStruct)) ; + if (lcd == NULL) + return -1 ; + + lcd->rsPin = rs ; + lcd->strbPin = strb ; + lcd->bits = 8 ; // For now - we'll set it properly later. + lcd->rows = rows ; + lcd->cols = cols ; + + lcd->dataPins [0] = d0 ; + lcd->dataPins [1] = d1 ; + lcd->dataPins [2] = d2 ; + lcd->dataPins [3] = d3 ; + lcd->dataPins [4] = d4 ; + lcd->dataPins [5] = d5 ; + lcd->dataPins [6] = d6 ; + lcd->dataPins [7] = d7 ; + + lcds [lcdFd] = lcd ; + + digitalWrite (lcd->rsPin, 0) ; pinMode (lcd->rsPin, OUTPUT) ; + digitalWrite (lcd->strbPin, 0) ; pinMode (lcd->strbPin, OUTPUT) ; + + for (i = 0 ; i < bits ; ++i) + { + digitalWrite (lcd->dataPins [i], 0) ; + pinMode (lcd->dataPins [i], OUTPUT) ; + } + delay (35) ; // mS + + +// 4-bit mode? +// OK. This is a PIG and it's not at all obvious from the documentation I had, +// so I guess some others have worked through either with better documentation +// or more trial and error... Anyway here goes: +// +// It seems that the controller needs to see the FUNC command at least 3 times +// consecutively - in 8-bit mode. If you're only using 8-bit mode, then it appears +// that you can get away with one func-set, however I'd not rely on it... +// +// So to set 4-bit mode, you need to send the commands one nibble at a time, +// the same three times, but send the command to set it into 8-bit mode those +// three times, then send a final 4th command to set it into 4-bit mode, and only +// then can you flip the switch for the rest of the library to work in 4-bit +// mode which sends the commands as 2 x 4-bit values. + + if (bits == 4) + { + func = LCD_FUNC | LCD_FUNC_DL ; // Set 8-bit mode 3 times + put4Command (lcd, func >> 4) ; delay (35) ; + put4Command (lcd, func >> 4) ; delay (35) ; + put4Command (lcd, func >> 4) ; delay (35) ; + func = LCD_FUNC ; // 4th set: 4-bit mode + put4Command (lcd, func >> 4) ; delay (35) ; + lcd->bits = 4 ; + } + else + { + func = LCD_FUNC | LCD_FUNC_DL ; + putCommand (lcd, func ) ; delay (35) ; + putCommand (lcd, func ) ; delay (35) ; + putCommand (lcd, func ) ; delay (35) ; + } + + if (lcd->rows > 1) + { + func |= LCD_FUNC_N ; + putCommand (lcd, func) ; delay (35) ; + } + +// Rest of the initialisation sequence + + putCommand (lcd, LCD_ON_OFF | LCD_ON_OFF_D) ; delay (2) ; + putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ; delay (2) ; + putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ; delay (2) ; + putCommand (lcd, LCD_CLEAR) ; delay (5) ; + + return lcdFd ; +} diff --git a/wiringPi/lcd.h b/wiringPi/lcd.h new file mode 100644 index 0000000..094f5f5 --- /dev/null +++ b/wiringPi/lcd.h @@ -0,0 +1,37 @@ +/* + * lcd.h: + * Text-based LCD driver. + * This is designed to drive the parallel interface LCD drivers + * based in the Hitachi HD44780U controller and compatables. + * + * 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 . + *********************************************************************** + */ + +#define MAX_LCDS 8 + +extern void lcdHome (int fd) ; +extern void lcdClear (int fd) ; +extern void lcdPosition (int fd, int x, int y) ; +extern void lcdPutchar (int fd, uint8_t data) ; +extern void lcdPuts (int fd, char *string) ; +extern void lcdPrintf (int fd, char *message, ...) ; + +extern int lcdInit (int rows, int cols, int bits, int rs, int strb, + int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) ; diff --git a/wiringPi/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/piNes.c b/wiringPi/piNes.c new file mode 100644 index 0000000..1722d31 --- /dev/null +++ b/wiringPi/piNes.c @@ -0,0 +1,115 @@ +/* + * piNes.c: + * Driver for the NES Joystick controller on the Raspberry Pi + * 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 "piNes.h" + +#define MAX_NES_JOYSTICKS 8 + +#define NES_RIGHT 0x01 +#define NES_LEFT 0x02 +#define NES_DOWN 0x04 +#define NES_UP 0x08 +#define NES_START 0x10 +#define NES_SELECT 0x20 +#define NES_B 0x40 +#define NES_A 0x80 + + +// Data to store the pins for each controller + +struct nesPinsStruct +{ + unsigned int cPin, dPin, lPin ; +} ; + +static struct nesPinsStruct nesPins [MAX_NES_JOYSTICKS] ; + +static int joysticks = 0 ; + + +/* + * setupNesJoystick: + * Create a new NES joystick interface, program the pins, etc. + ********************************************************************************* + */ + +int setupNesJoystick (int dPin, int cPin, int lPin) +{ + if (joysticks == MAX_NES_JOYSTICKS) + return -1 ; + + nesPins [joysticks].dPin = dPin ; + nesPins [joysticks].cPin = cPin ; + nesPins [joysticks].lPin = lPin ; + + digitalWrite (lPin, LOW) ; + digitalWrite (cPin, LOW) ; + + pinMode (lPin, OUTPUT) ; + pinMode (cPin, OUTPUT) ; + pinMode (dPin, INPUT) ; + + return joysticks++ ; +} + + +/* + * readNesJoystick: + * Do a single scan of the NES Joystick. + ********************************************************************************* + */ + +unsigned int readNesJoystick (int joystick) +{ + unsigned int value = 0 ; + int i ; + + struct nesPinsStruct *pins = &nesPins [joystick] ; + +// Toggle Latch - which presents the first bit + + digitalWrite (pins->lPin, HIGH) ; + delayMicroseconds (1) ; + digitalWrite (pins->lPin, LOW) ; + delayMicroseconds (1) ; + +// Read first bit + + value = digitalRead (pins->dPin) ; + +// Now get the next 7 bits with the clock + + for (i = 0 ; i < 7 ; ++i) + { + digitalWrite (pins->cPin, HIGH) ; + delayMicroseconds (1) ; + digitalWrite (pins->cPin, LOW) ; + delayMicroseconds (1) ; + value = (value << 1) | digitalRead (pins->dPin) ; + } + + return ~value ; +} diff --git a/wiringPi/piNes.h b/wiringPi/piNes.h new file mode 100644 index 0000000..b76c415 --- /dev/null +++ b/wiringPi/piNes.h @@ -0,0 +1,37 @@ +/* + * piNes.h: + * Driver for the NES Joystick controller on the Raspberry Pi + * 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 . + *********************************************************************** + */ + +#define MAX_NES_JOYSTICKS 8 + +#define NES_RIGHT 0x01 +#define NES_LEFT 0x02 +#define NES_DOWN 0x04 +#define NES_UP 0x08 +#define NES_START 0x10 +#define NES_SELECT 0x20 +#define NES_B 0x40 +#define NES_A 0x80 + +extern int setupNesJoystick (int dPin, int cPin, int lPin) ; +extern unsigned int readNesJoystick (int joystick) ; 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 new file mode 100644 index 0000000..35717ef --- /dev/null +++ b/wiringPi/wiringPi.c @@ -0,0 +1,981 @@ +/* + * wiringPi: + * Arduino compatable (ish) Wiring library for the Raspberry Pi + * Copyright (c) 2012 Gordon Henderson + * + * Thanks to code samples from Gert Jan van Loo and the + * BCM2835 ARM Peripherals manual, however it's missing + * the clock section /grr/mutter/ + *********************************************************************** + * 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 . + *********************************************************************** + */ + +// 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: +// Fixed some typos. +// Added c++ support for the .h file +// Added a new function to allow for using my "pin" numbers, or native +// GPIO pin numbers. +// Removed my busy-loop delay and replaced it with a call to delayMicroseconds +// +// 02 May 2012: +// Added in the 2 UART pins +// Change maxPins to numPins to more accurately reflect purpose + +// Pad drive current fiddling + +#undef DEBUG_PADS + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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) ; +void (*setPadDrive) (int group, int value) ; +int (*digitalRead) (int pin) ; +int (*waitForInterrupt) (int pin, int mS) ; +void (*delayMicroseconds) (unsigned int howLong) ; + + +#ifndef TRUE +#define TRUE (1==1) +#define FALSE (1==2) +#endif + +// BCM Magic + +#define BCM_PASSWORD 0x5A000000 + + +// Port function select bits + +#define FSEL_INPT 0b000 +#define FSEL_OUTP 0b001 +#define FSEL_ALT0 0b100 +#define FSEL_ALT0 0b100 +#define FSEL_ALT1 0b101 +#define FSEL_ALT2 0b110 +#define FSEL_ALT3 0b111 +#define FSEL_ALT4 0b011 +#define FSEL_ALT5 0b010 + +// Access from ARM Running Linux +// Take from Gert/Doms code. Some of this is not in the manual +// that I can find )-: + +#define BCM2708_PERI_BASE 0x20000000 +#define GPIO_PADS (BCM2708_PERI_BASE + 0x100000) +#define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000) +#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) +#define GPIO_TIMER (BCM2708_PERI_BASE + 0x00B000) +#define GPIO_PWM (BCM2708_PERI_BASE + 0x20C000) + +#define PAGE_SIZE (4*1024) +#define BLOCK_SIZE (4*1024) + +// PWM + +#define PWM_CONTROL 0 +#define PWM_STATUS 1 +#define PWM0_RANGE 4 +#define PWM0_DATA 5 +#define PWM1_RANGE 8 +#define PWM1_DATA 9 + +#define PWMCLK_CNTL 40 +#define PWMCLK_DIV 41 + +#define PWM1_MS_MODE 0x8000 // Run in MS mode +#define PWM1_USEFIFO 0x2000 // Data from FIFO +#define PWM1_REVPOLAR 0x1000 // Reverse polarity +#define PWM1_OFFSTATE 0x0800 // Ouput Off state +#define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty +#define PWM1_SERIAL 0x0200 // Run in serial mode +#define PWM1_ENABLE 0x0100 // Channel Enable + +#define PWM0_MS_MODE 0x0080 // Run in MS mode +#define PWM0_USEFIFO 0x0020 // Data from FIFO +#define PWM0_REVPOLAR 0x0010 // Reverse polarity +#define PWM0_OFFSTATE 0x0008 // Ouput Off state +#define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty +#define PWM0_SERIAL 0x0002 // Run in serial mode +#define PWM0_ENABLE 0x0001 // Channel Enable + +// Timer + +#define TIMER_LOAD (0x400 >> 2) +#define TIMER_VALUE (0x404 >> 2) +#define TIMER_CONTROL (0x408 >> 2) +#define TIMER_IRQ_CLR (0x40C >> 2) +#define TIMER_IRQ_RAW (0x410 >> 2) +#define TIMER_IRQ_MASK (0x414 >> 2) +#define TIMER_RELOAD (0x418 >> 2) +#define TIMER_PRE_DIV (0x41C >> 2) +#define TIMER_COUNTER (0x420 >> 2) + +// Locals to hold pointers to the hardware + +static volatile uint32_t *gpio ; +static volatile uint32_t *pwm ; +static volatile uint32_t *clk ; +static volatile uint32_t *pads ; +static volatile uint32_t *timer ; + +static volatile uint32_t *timerIrqRaw ; + +// The BCM2835 has 54 GPIO pins. +// BCM2835 data sheet, Page 90 onwards. +// There are 6 control registers, each control the functions of a block +// of 10 pins. +// Each control register has 10 sets of 3 bits per GPIO pin: +// +// 000 = GPIO Pin X is an input +// 001 = GPIO Pin X is an output +// 100 = GPIO Pin X takes alternate function 0 +// 101 = GPIO Pin X takes alternate function 1 +// 110 = GPIO Pin X takes alternate function 2 +// 111 = GPIO Pin X takes alternate function 3 +// 011 = GPIO Pin X takes alternate function 4 +// 010 = GPIO Pin X takes alternate function 5 +// +// So the 3 bits for port X are: +// X / 10 + ((X % 10) * 3) + +// sysFds: +// Map a file descriptor from the /sys/class/gpio/gpioX/value + +static int sysFds [64] ; + +// 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! + +// pinToGpio: +// Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin + +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: +// Map a BCM_GPIO pin to it's control port. (GPFSEL 0-5) + +static uint8_t gpioToGPFSEL [] = +{ + 0,0,0,0,0,0,0,0,0,0, + 1,1,1,1,1,1,1,1,1,1, + 2,2,2,2,2,2,2,2,2,2, + 3,3,3,3,3,3,3,3,3,3, + 4,4,4,4,4,4,4,4,4,4, + 5,5,5,5,5,5,5,5,5,5, +} ; + +// gpioToShift +// Define the shift up for the 3 bits per pin in each GPFSEL port + +static uint8_t gpioToShift [] = +{ + 0,3,6,9,12,15,18,21,24,27, + 0,3,6,9,12,15,18,21,24,27, + 0,3,6,9,12,15,18,21,24,27, + 0,3,6,9,12,15,18,21,24,27, + 0,3,6,9,12,15,18,21,24,27, +} ; + +// gpioToGPSET: +// (Word) offset to the GPIO Set registers for each GPIO pin + +static uint8_t gpioToGPSET [] = +{ + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, +} ; + +// gpioToGPCLR: +// (Word) offset to the GPIO Clear registers for each GPIO pin + +static uint8_t gpioToGPCLR [] = +{ + 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, + 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, +} ; + +// gpioToGPLEV: +// (Word) offset to the GPIO Input level registers for each GPIO pin + +static uint8_t gpioToGPLEV [] = +{ + 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, + 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 + +#define GPPUD 37 + +static uint8_t gpioToPUDCLK [] = +{ + 38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38, + 39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39, +} ; + +// gpioToPwmALT +// the ALT value to put a GPIO pin into PWM mode + +static uint8_t gpioToPwmALT [] = +{ + 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7 + 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15 + 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23 + 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31 + 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39 + FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47 + 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55 + 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63 +} ; + +static uint8_t gpioToPwmPort [] = +{ + 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7 + 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15 + 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23 + 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31 + 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39 + PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47 + 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55 + 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63 + +} ; + + +// Time for easy calculations + +static unsigned long long epoch ; + +////////////////////////////////////////////////////////////////////////////////// + + +/* + * pinMode: + * Sets the mode of a pin to be input, output or PWM output + ********************************************************************************* + */ + +void pinModeGpio (int pin, int 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) = BCM_PASSWORD | (32<<12) ; // set pwm div to 32 (19.2/3 = 600KHz) + *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // 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 ; + + pwmRunning = TRUE ; + } + + } + +// 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) +{ + pin = pinToGpio [pin & 63] ; + + if (value == LOW) + *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ; + else + *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ; +} + +void digitalWriteGpio (int pin, int value) +{ + pin &= 63 ; + + if (value == LOW) + *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ; + else + *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ; +} + +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 ; + + pin = pinToGpio [pin & 63] ; + port = gpioToPwmPort [pin] ; + + *(pwm + port) = value & 0x3FF ; +} + +void pwmWriteGpio (int pin, int value) +{ + int port ; + + pin = pin & 63 ; + port = gpioToPwmPort [pin] ; + + *(pwm + port) = value & 0x3FF ; +} + +void pwmWriteSys (int pin, int value) +{ + return ; +} + + +/* + * setPadDrive: + * Set the PAD driver value + ********************************************************************************* + */ + +void setPadDriveWPi (int group, int value) +{ + uint32_t wrVal ; + + if ((group < 0) || (group > 2)) + return ; + + wrVal = BCM_PASSWORD | 0x18 | (value & 7) ; + *(pads + group + 11) = wrVal ; + +#ifdef DEBUG_PADS + printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ; + printf ("Read : %08X\n", *(pads + group + 11)) ; +#endif +} + +void setPadDriveGpio (int group, int value) +{ + setPadDriveWPi (group, value) ; +} + +void setPadDriveSys (int group, int value) +{ + return ; +} + + +/* + * digitalRead: + * Read the value of a given Pin, returning HIGH or LOW + ********************************************************************************* + */ + +int digitalReadWPi (int pin) +{ + pin = pinToGpio [pin & 63] ; + + if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0) + return HIGH ; + else + return LOW ; +} + +int digitalReadGpio (int pin) +{ + pin &= 63 ; + + if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 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] ; + pud &= 3 ; + + *(gpio + GPPUD) = pud ; delayMicroseconds (10) ; + *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (10) ; + + *(gpio + GPPUD) = 0 ; + *(gpio + gpioToPUDCLK [pin]) = 0 ; +} + +void pullUpDnControlGpio (int pin, int pud) +{ + pin &= 63 ; + pud &= 3 ; + + *(gpio + GPPUD) = pud ; delayMicroseconds (10) ; + *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (10) ; + + *(gpio + GPPUD) = 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) ; +} + + + + +/* + * delay: + * Wait for some number of milli seconds + ********************************************************************************* + */ + +void delay (unsigned int howLong) +{ + struct timespec sleeper, dummy ; + + sleeper.tv_sec = (time_t)(howLong / 1000) ; + sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ; + + nanosleep (&sleeper, &dummy) ; +} + +/* + * delayMicroseconds: + * This is somewhat intersting. It seems that on the Pi, a single call + * to nanosleep takes some 80 to 130 microseconds anyway, so while + * obeying the standards (may take longer), it's not always what we + * want! + * + * So what I'll do now is if the delay is less than 100uS we'll do it + * in a hard loop, watching a built-in counter on the ARM chip. This is + * somewhat sub-optimal in that it uses 100% CPU, something not an issue + * in a microcontroller, but under a multi-tasking, multi-user OS, it's + * wastefull, however we've no real choice )-: + ********************************************************************************* + */ + +void delayMicrosecondsSys (unsigned int howLong) +{ + struct timespec sleeper, dummy ; + + sleeper.tv_sec = 0 ; + sleeper.tv_nsec = (long)(howLong * 1000) ; + + nanosleep (&sleeper, &dummy) ; +} + +void delayMicrosecondsHard (unsigned int howLong) +{ + *(timer + TIMER_LOAD) = howLong ; + *(timer + TIMER_IRQ_CLR) = 0 ; + + while (*timerIrqRaw == 0) + ; +} + +void delayMicrosecondsWPi (unsigned int howLong) +{ + struct timespec sleeper, dummy ; + + /**/ if (howLong == 0) + return ; + else if (howLong < 100) + delayMicrosecondsHard (howLong) ; + else + { + sleeper.tv_sec = 0 ; + sleeper.tv_nsec = (long)(howLong * 1000) ; + nanosleep (&sleeper, &dummy) ; + } +} + + +/* + * millis: + * Return a number of milliseconds as an unsigned int. + ********************************************************************************* + */ + +unsigned int millis (void) +{ + struct timeval tv ; + unsigned long long t1 ; + + gettimeofday (&tv, NULL) ; + + t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + + return (uint32_t)(t1 - epoch) ; +} + + +/* + * wiringPiSetup: + * Must be called once at the start of your program execution. + * + * Default setup: Initialises the system into wiringPi Pin mode and uses the + * memory mapped hardware directly. + ********************************************************************************* + */ + +int wiringPiSetup (void) +{ + int fd ; + uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ; + struct timeval tv ; + + pinMode = pinModeWPi ; + pullUpDnControl = pullUpDnControlWPi ; + digitalWrite = digitalWriteWPi ; + pwmWrite = pwmWriteWPi ; + setPadDrive = setPadDriveWPi ; + digitalRead = digitalReadWPi ; + waitForInterrupt = waitForInterruptWPi ; + delayMicroseconds = delayMicrosecondsWPi ; + +// Open the master /dev/memory device + + if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) + { + fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; + return -1 ; + } + +// GPIO: + +// Allocate 2 pages - 1 ... + + if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) + { + fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ; + return -1 ; + } + +// ... presumably to make sure we can round it up to a whole page size + + if (((uint32_t)gpioMem % PAGE_SIZE) != 0) + gpioMem += PAGE_SIZE - ((uint32_t)gpioMem % PAGE_SIZE) ; + + gpio = (uint32_t *)mmap((caddr_t)gpioMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_BASE) ; + + if ((int32_t)gpio < 0) + { + fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ; + return -1 ; + } + +// PWM + + if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) + { + fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ; + return -1 ; + } + + if (((uint32_t)pwmMem % PAGE_SIZE) != 0) + pwmMem += PAGE_SIZE - ((uint32_t)pwmMem % PAGE_SIZE) ; + + pwm = (uint32_t *)mmap(pwmMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PWM) ; + + if ((int32_t)pwm < 0) + { + fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ; + return -1 ; + } + +// Clock control (needed for PWM) + + if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) + { + fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ; + return -1 ; + } + + if (((uint32_t)clkMem % PAGE_SIZE) != 0) + clkMem += PAGE_SIZE - ((uint32_t)clkMem % PAGE_SIZE) ; + + clk = (uint32_t *)mmap(clkMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, CLOCK_BASE) ; + + if ((int32_t)clk < 0) + { + fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ; + return -1 ; + } + +// The drive pads + + if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) + { + fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ; + return -1 ; + } + + if (((uint32_t)padsMem % PAGE_SIZE) != 0) + padsMem += PAGE_SIZE - ((uint32_t)padsMem % PAGE_SIZE) ; + + pads = (uint32_t *)mmap(padsMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PADS) ; + + if ((int32_t)pads < 0) + { + fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ; + return -1 ; + } + +#ifdef DEBUG_PADS + printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ; + printf (" -> %08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ; +#endif + +// The system timer + + if ((timerMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) + { + fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ; + return -1 ; + } + + if (((uint32_t)timerMem % PAGE_SIZE) != 0) + timerMem += PAGE_SIZE - ((uint32_t)timerMem % PAGE_SIZE) ; + + timer = (uint32_t *)mmap(timerMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_TIMER) ; + + if ((int32_t)timer < 0) + { + fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ; + return -1 ; + } + +// Set the timer to free-running, 1MHz. +// 0xF9 is 249, the timer divide is base clock / (divide+1) +// so base clock is 250MHz / 250 = 1MHz. + + *(timer + TIMER_CONTROL) = 0x0000280 ; + *(timer + TIMER_PRE_DIV) = 0x00000F9 ; + timerIrqRaw = timer + TIMER_IRQ_RAW ; + +// Initialise our epoch for millis() + + gettimeofday (&tv, NULL) ; + epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + + return 0 ; +} + + +/* + * wiringPiSetupGpio: + * Must be called once at the start of your program execution. + * + * GPIO setup: Initialises the system into GPIO Pin mode and uses the + * memory mapped hardware directly. + ********************************************************************************* + */ + +int wiringPiSetupGpio (void) +{ + int x = wiringPiSetup () ; + + if (x != 0) + return x ; + + pinMode = pinModeGpio ; + pullUpDnControl = pullUpDnControlGpio ; + digitalWrite = digitalWriteGpio ; + pwmWrite = pwmWriteGpio ; + setPadDrive = setPadDriveGpio ; + digitalRead = digitalReadGpio ; + waitForInterrupt = waitForInterruptGpio ; + delayMicroseconds = delayMicrosecondsWPi ; // Same + + return 0 ; +} + + +/* + * wiringPiSetupSys: + * Must be called once at the start of your program execution. + * + * Initialisation (again), however this time we are using the /sys/class/gpio + * interface to the GPIO systems - slightly slower, but always usable as + * a non-root user, assuming the devices are already exported and setup correctly. + */ + +int wiringPiSetupSys (void) +{ + int pin ; + struct timeval tv ; + char fName [128] ; + + pinMode = pinModeSys ; + pullUpDnControl = pullUpDnControlSys ; + digitalWrite = digitalWriteSys ; + pwmWrite = pwmWriteSys ; + setPadDrive = setPadDriveSys ; + digitalRead = digitalReadSys ; + waitForInterrupt = waitForInterruptSys ; + delayMicroseconds = delayMicrosecondsSys ; + +// Open and scan the directory, looking for exported GPIOs, and pre-open +// the 'value' interface to speed things up for later + + for (pin = 0 ; pin < 64 ; ++pin) + { + sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; + sysFds [pin] = open (fName, O_RDWR) ; + } + +// Initialise the epoch for mills() ... + + gettimeofday (&tv, NULL) ; + epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + + return 0 ; +} + + diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h new file mode 100644 index 0000000..5b9c8c7 --- /dev/null +++ b/wiringPi/wiringPi.h @@ -0,0 +1,95 @@ +/* + * wiringPi: + * Arduino compatable (ish) Wiring library for the Raspberry Pi + * 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 . + *********************************************************************** + */ + +// Handy defines + +#define NUM_PINS 17 + +#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 +#define PWM_OUTPUT 2 + +#define LOW 0 +#define HIGH 1 + +#define PUD_OFF 0 +#define PUD_DOWN 1 +#define PUD_UP 2 + +// Function prototypes +// c++ wrappers thanks to a commend by Nick Lott +// (and others on the Raspberry Pi forums) + +#ifdef __cplusplus +extern "C" { +#endif + +// Basic wiringPi functions + +extern int wiringPiSetup (void) ; +extern int wiringPiSetupSys (void) ; +extern int wiringPiSetupGpio (void) ; +extern int wiringPiSetupPiFace (void) ; + +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 void (*setPadDrive) (int group, int value) ; +extern int (*digitalRead) (int pin) ; +extern void (*delayMicroseconds) (unsigned int howLong) ; + +// 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) ; +//extern void delayMicrosecondsHard (unsigned int howLong) ; +extern unsigned int millis (void) ; + +#ifdef __cplusplus +} +#endif 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..b401894 --- /dev/null +++ b/wiringPi/wiringSerial.c @@ -0,0 +1,218 @@ +/* + * 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 | TCSAFLUSH, &options) ; + + ioctl (fd, TIOCMGET, &status); + + status |= TIOCM_DTR ; + status |= TIOCM_RTS ; + + ioctl (fd, TIOCMSET, &status); + + usleep (10000) ; // 10mS + + return fd ; +} + + +/* + * serialFlush: + * Flush the serial buffers (both tx & rx) + ********************************************************************************* + */ + +void serialFlush (int fd) +{ + tcflush (fd, TCIOFLUSH) ; +} + + +/* + * 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..1a4198c --- /dev/null +++ b/wiringPi/wiringSerial.h @@ -0,0 +1,38 @@ +/* + * 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 serialFlush (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 new file mode 100644 index 0000000..b9b7a44 --- /dev/null +++ b/wiringPi/wiringShift.c @@ -0,0 +1,84 @@ +/* + * wiringShift.c: + * Emulate some of the Arduino wiring functionality. + * + * Copyright (c) 2009-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" +#include "wiringShift.h" + +/* + * shiftIn: + * Shift data in from a clocked source + ********************************************************************************* + */ + +uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order) +{ + uint8_t value = 0 ; + int8_t i ; + + if (order == MSBFIRST) + for (i = 7 ; i >= 0 ; --i) + { + digitalWrite (cPin, HIGH) ; + value |= digitalRead (dPin) << i ; + digitalWrite (cPin, LOW) ; + } + else + for (i = 0 ; i < 8 ; ++i) + { + digitalWrite (cPin, HIGH) ; + value |= digitalRead (dPin) << i ; + digitalWrite (cPin, LOW) ; + } + + return value; +} + + +/* + * shiftOut: + * Shift data out to a clocked source + ********************************************************************************* + */ + +void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) +{ + int8_t i; + + if (order == MSBFIRST) + for (i = 7 ; i >= 0 ; --i) + { + digitalWrite (dPin, val & (1 << i)) ; + digitalWrite (cPin, HIGH) ; + digitalWrite (cPin, LOW) ; + } + else + for (i = 0 ; i < 8 ; ++i) + { + digitalWrite (dPin, val & (1 << i)) ; + digitalWrite (cPin, HIGH) ; + digitalWrite (cPin, LOW) ; + } +} diff --git a/wiringPi/wiringShift.h b/wiringPi/wiringShift.h new file mode 100644 index 0000000..a3f4581 --- /dev/null +++ b/wiringPi/wiringShift.h @@ -0,0 +1,41 @@ +/* + * wiringShift.h: + * Emulate some of the Arduino wiring functionality. + * + * Copyright (c) 2009-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 . + *********************************************************************** + */ + +#define LSBFIRST 0 +#define MSBFIRST 1 + +#ifndef _STDINT_H +# include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +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) ; + +#ifdef __cplusplus +} +#endif From 0d68ceea6b437cc7dd8d507dd815149a736de7d6 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 18 Aug 2012 00:05:38 +0100 Subject: [PATCH 02/97] Updated wiringPi.c to work with some pullUpDown stuff --- wiringPi/.wiringPiFace.c.swp | Bin 16384 -> 0 bytes wiringPi/README | 2 -- wiringPi/wiringPi.c | 46 +++++++++++++---------------------- wiringPi/wiringPi.h | 2 -- 4 files changed, 17 insertions(+), 33 deletions(-) delete mode 100644 wiringPi/.wiringPiFace.c.swp diff --git a/wiringPi/.wiringPiFace.c.swp b/wiringPi/.wiringPiFace.c.swp deleted file mode 100644 index 3eff7deff308d27f398bd919c64bb8119b850554..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI2Ym6IL6~~7rO|mT^fr3*2M{8mh=+Iv1mY8@Mev~#YJ~(J5J5aDgoMKX+ zHdP{}NJTTsFKeHB?wxbaea;!T^C#3Lb|5>W;M%Py$M)^6of_Yvd~jM(y0&ZB78Shy z)z?m)vR_|CmVP2^Fz2~?Ze6&->ej@SAIh4}v-&kOm#U-@NF#7v5!k8B&P?a`jNid- zd&ly1)l8Ms2&556BalWQjX)ZKGy-V^(g>sxc=JcVceW^(;KohhQ{XKTmvEiI=cl6Y zTk!WVn1BN5q*oe&Gy-V^(g>sxNF$I&AdNs8fiwbX1kwnk5lAEO=81r2D9R3)dxR1I zc>hm1fdA}Lls|*7g45t}a4*;fwt(-xO;Mf&ZO{U@gBN!y%G00;?gzWTFW;&tPl83D zf}6naZ&j490Rub&THs-DA2WX!C4?c59|lGfxnI^%Fn@B z@MX{k8-N1?ydPYIlYarf0_VUtz;obp;7M>AIN&3o4D#STU>o?`c13v^`~rLvoClu= zPXix3432|+fPq`UYugm%RqzV90Dc0_f=>e%JOJ(m6JQtk=go@p5AYlC5;zaefiHqH z;3?n%7kms@;1MtmE^bwn=fSh!vw#B~dR2OiUOoqHZz<$6!EgtwX{ z{hr%8@zkBIXm zo?j}qR_bcA6gB3%f$mdmS_p8@QlU7O-`F=DOUIJVAaPz%ayd3{udZsA!F1c;o|0%{tXC^7wW2o1 zGU4Y&>HcaB!9$%Q?`VPPGhqo|Xr}Pc1DIp0XkxU5<;rrSL{{VnR>W;LYt=<6nHjlj zwOA+dz7g^9La9RXgCp{y+Gs4Upkz8fQX)-8!OUpEl3K4<=_Uw56tS_;s<}etzSdH; zSVG~!FhX;f*Kk$#rc_E6mVvX?#X@s#jBL0kX>g&YRzulrB2z2P`9dv}4h5vrEK6Cw zoMJhwGv7{kVuQA@`=J?C_Ru!ara8I1Z6~QV3UfFhnhn+Rf}pp6ykSz zaX84Mg7j$659CoO8|pIYQ79b>COnd|d?{&>JPKv=i7fOeln(ke^eB|>Pjn-X3gpqi zCh{oM9I}Z#3Weyx*dy6@RquSjtIa>d)+|eRhru~FkTeNk}^rAaQ)?l^OPcgg0 z_!@Wneh+I@QlEUA-j9_R7upGT9`gHedwMc2{jf&3DfAP!PbS}!++uLZ)25{ zcEG%z9hgQqo?WhUu|^#dP7jPoI^!)=mf0dsc9=g4+`>>dSuJSej<78<@2V<^86Dfj zMuh2Ra9IxDbRq25&Imlq3lR$H#u zs*O^Xu?E5b)1te#1D~y^&EwVOCM#4#0v@hwE{-^U zAG0oMBn}RpYz>@GU4dqy8SOAVOmbQ(u)F$b)--AS;!xNL%*jCK?EuGI zhqmDfrdj=Hcw?G-5XJrAP&B+p=IcI%Q30GN41xCEP8Pg#K+;a;2K!E zZOqA42Rj@VQMwHb5D?3%nC-2RDPQfbs%=Kpx;#@B{FD@O5wod;yps3#Py& z9<$mB=GJ}I} z>zeS(wyRPazzrN9DTPr{bf~7xy}S(36~1Q5R}4q55RGa>!C7Ao)4GxNkZ43ZYI<0E zSQKg_)uJQMicAMJ6qx3+Q?%x7%Xe)P=8uXtsf2p+a)c#wEd#q)Buea3NHGdo_{KnV zSZ0IWfm{&f&gm(HZQ!=8_(`^b_&QPj480I}R3V*BiV^n(N7ywyp|K2Q?9m{e9NwsG zd>pss^>B0tv=fy5kJ@nus9Y`L%22WX(7f*8BG7&I%gwuP%8Gh&)3c`?=QC9x{ zViaEA@C&tREYWps<1Trb!7?)aJ;{))&0|E3(Mt!sW}rt>%5KmcmVi2Wwp^g$WWOKNgBt;J4CzFp|Sjijr76r-lXpbhN?36PL`I-k)Ji`51Ts1 zRLco`WJ`uUlNAb=xh5+Vr~-1*vc_l?yfm0(@mm+QNmG>4o`Yc@Cnhcn$<(y$Lz?}Y gzYDDwTT6upT8&z%RBRppaQH%0riCZi5n3$%3;&rqhX4Qo diff --git a/wiringPi/README b/wiringPi/README index 052cd41..781510a 100644 --- a/wiringPi/README +++ b/wiringPi/README @@ -2,8 +2,6 @@ WiringPi: An implementation of most of the Arduino Wiring functions for the Raspberry Pi --- And a lot lot more! - Full details at: https://projects.drogon.net/raspberry-pi/wiringpi/ diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 35717ef..fb5e7c6 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -402,7 +402,8 @@ void pinModeGpio (int pin, int mode) // When we change mode of any pin, we remove the pull up/downs - pullUpDnControl (pin, PUD_OFF) ; +// delayMicroseconds (300) ; +// pullUpDnControl (pin, PUD_OFF) ; } void pinModeWPi (int pin, int mode) @@ -479,16 +480,6 @@ void digitalWriteSys (int pin, int value) ********************************************************************************* */ -void pwmWriteWPi (int pin, int value) -{ - int port ; - - pin = pinToGpio [pin & 63] ; - port = gpioToPwmPort [pin] ; - - *(pwm + port) = value & 0x3FF ; -} - void pwmWriteGpio (int pin, int value) { int port ; @@ -499,6 +490,11 @@ void pwmWriteGpio (int pin, int value) *(pwm + port) = value & 0x3FF ; } +void pwmWriteWPi (int pin, int value) +{ + pwmWriteGpio (pinToGpio [pin & 63], value) ; +} + void pwmWriteSys (int pin, int value) { return ; @@ -588,28 +584,21 @@ int digitalReadSys (int pin) ********************************************************************************* */ -void pullUpDnControlWPi (int pin, int pud) -{ - pin = pinToGpio [pin & 63] ; - pud &= 3 ; - - *(gpio + GPPUD) = pud ; delayMicroseconds (10) ; - *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (10) ; - - *(gpio + GPPUD) = 0 ; - *(gpio + gpioToPUDCLK [pin]) = 0 ; -} - void pullUpDnControlGpio (int pin, int pud) { pin &= 63 ; pud &= 3 ; - *(gpio + GPPUD) = pud ; delayMicroseconds (10) ; - *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (10) ; + *(gpio + GPPUD) = pud ; delayMicroseconds (5) ; + *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ; - *(gpio + GPPUD) = 0 ; - *(gpio + gpioToPUDCLK [pin]) = 0 ; + *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ; + *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ; +} + +void pullUpDnControlWPi (int pin, int pud) +{ + pullUpDnControlGpio (pinToGpio [pin & 63], pud) ; } void pullUpDnControlSys (int pin, int pud) @@ -685,6 +674,7 @@ void delay (unsigned int howLong) nanosleep (&sleeper, &dummy) ; } + /* * delayMicroseconds: * This is somewhat intersting. It seems that on the Pi, a single call @@ -977,5 +967,3 @@ int wiringPiSetupSys (void) return 0 ; } - - diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 5b9c8c7..03ab787 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -86,8 +86,6 @@ extern int piHiPri (int pri) ; // Extras from arduino land extern void delay (unsigned int howLong) ; -//extern void delayMicroseconds (unsigned int howLong) ; -//extern void delayMicrosecondsHard (unsigned int howLong) ; extern unsigned int millis (void) ; #ifdef __cplusplus From b23cb44ffae23dc80105c656f817c41d28cea86c Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 18 Aug 2012 00:05:38 +0100 Subject: [PATCH 03/97] Updated wiringPi.c to work with some pullUpDown stuff General tidying up of some of the code round delayMicroseconds --- wiringPi/.wiringPiFace.c.swp | Bin 16384 -> 0 bytes wiringPi/README | 2 -- wiringPi/wiringPi.c | 46 +++++++++++++---------------------- wiringPi/wiringPi.h | 2 -- 4 files changed, 17 insertions(+), 33 deletions(-) delete mode 100644 wiringPi/.wiringPiFace.c.swp diff --git a/wiringPi/.wiringPiFace.c.swp b/wiringPi/.wiringPiFace.c.swp deleted file mode 100644 index 3eff7deff308d27f398bd919c64bb8119b850554..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI2Ym6IL6~~7rO|mT^fr3*2M{8mh=+Iv1mY8@Mev~#YJ~(J5J5aDgoMKX+ zHdP{}NJTTsFKeHB?wxbaea;!T^C#3Lb|5>W;M%Py$M)^6of_Yvd~jM(y0&ZB78Shy z)z?m)vR_|CmVP2^Fz2~?Ze6&->ej@SAIh4}v-&kOm#U-@NF#7v5!k8B&P?a`jNid- zd&ly1)l8Ms2&556BalWQjX)ZKGy-V^(g>sxc=JcVceW^(;KohhQ{XKTmvEiI=cl6Y zTk!WVn1BN5q*oe&Gy-V^(g>sxNF$I&AdNs8fiwbX1kwnk5lAEO=81r2D9R3)dxR1I zc>hm1fdA}Lls|*7g45t}a4*;fwt(-xO;Mf&ZO{U@gBN!y%G00;?gzWTFW;&tPl83D zf}6naZ&j490Rub&THs-DA2WX!C4?c59|lGfxnI^%Fn@B z@MX{k8-N1?ydPYIlYarf0_VUtz;obp;7M>AIN&3o4D#STU>o?`c13v^`~rLvoClu= zPXix3432|+fPq`UYugm%RqzV90Dc0_f=>e%JOJ(m6JQtk=go@p5AYlC5;zaefiHqH z;3?n%7kms@;1MtmE^bwn=fSh!vw#B~dR2OiUOoqHZz<$6!EgtwX{ z{hr%8@zkBIXm zo?j}qR_bcA6gB3%f$mdmS_p8@QlU7O-`F=DOUIJVAaPz%ayd3{udZsA!F1c;o|0%{tXC^7wW2o1 zGU4Y&>HcaB!9$%Q?`VPPGhqo|Xr}Pc1DIp0XkxU5<;rrSL{{VnR>W;LYt=<6nHjlj zwOA+dz7g^9La9RXgCp{y+Gs4Upkz8fQX)-8!OUpEl3K4<=_Uw56tS_;s<}etzSdH; zSVG~!FhX;f*Kk$#rc_E6mVvX?#X@s#jBL0kX>g&YRzulrB2z2P`9dv}4h5vrEK6Cw zoMJhwGv7{kVuQA@`=J?C_Ru!ara8I1Z6~QV3UfFhnhn+Rf}pp6ykSz zaX84Mg7j$659CoO8|pIYQ79b>COnd|d?{&>JPKv=i7fOeln(ke^eB|>Pjn-X3gpqi zCh{oM9I}Z#3Weyx*dy6@RquSjtIa>d)+|eRhru~FkTeNk}^rAaQ)?l^OPcgg0 z_!@Wneh+I@QlEUA-j9_R7upGT9`gHedwMc2{jf&3DfAP!PbS}!++uLZ)25{ zcEG%z9hgQqo?WhUu|^#dP7jPoI^!)=mf0dsc9=g4+`>>dSuJSej<78<@2V<^86Dfj zMuh2Ra9IxDbRq25&Imlq3lR$H#u zs*O^Xu?E5b)1te#1D~y^&EwVOCM#4#0v@hwE{-^U zAG0oMBn}RpYz>@GU4dqy8SOAVOmbQ(u)F$b)--AS;!xNL%*jCK?EuGI zhqmDfrdj=Hcw?G-5XJrAP&B+p=IcI%Q30GN41xCEP8Pg#K+;a;2K!E zZOqA42Rj@VQMwHb5D?3%nC-2RDPQfbs%=Kpx;#@B{FD@O5wod;yps3#Py& z9<$mB=GJ}I} z>zeS(wyRPazzrN9DTPr{bf~7xy}S(36~1Q5R}4q55RGa>!C7Ao)4GxNkZ43ZYI<0E zSQKg_)uJQMicAMJ6qx3+Q?%x7%Xe)P=8uXtsf2p+a)c#wEd#q)Buea3NHGdo_{KnV zSZ0IWfm{&f&gm(HZQ!=8_(`^b_&QPj480I}R3V*BiV^n(N7ywyp|K2Q?9m{e9NwsG zd>pss^>B0tv=fy5kJ@nus9Y`L%22WX(7f*8BG7&I%gwuP%8Gh&)3c`?=QC9x{ zViaEA@C&tREYWps<1Trb!7?)aJ;{))&0|E3(Mt!sW}rt>%5KmcmVi2Wwp^g$WWOKNgBt;J4CzFp|Sjijr76r-lXpbhN?36PL`I-k)Ji`51Ts1 zRLco`WJ`uUlNAb=xh5+Vr~-1*vc_l?yfm0(@mm+QNmG>4o`Yc@Cnhcn$<(y$Lz?}Y gzYDDwTT6upT8&z%RBRppaQH%0riCZi5n3$%3;&rqhX4Qo diff --git a/wiringPi/README b/wiringPi/README index 052cd41..781510a 100644 --- a/wiringPi/README +++ b/wiringPi/README @@ -2,8 +2,6 @@ WiringPi: An implementation of most of the Arduino Wiring functions for the Raspberry Pi --- And a lot lot more! - Full details at: https://projects.drogon.net/raspberry-pi/wiringpi/ diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 35717ef..fb5e7c6 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -402,7 +402,8 @@ void pinModeGpio (int pin, int mode) // When we change mode of any pin, we remove the pull up/downs - pullUpDnControl (pin, PUD_OFF) ; +// delayMicroseconds (300) ; +// pullUpDnControl (pin, PUD_OFF) ; } void pinModeWPi (int pin, int mode) @@ -479,16 +480,6 @@ void digitalWriteSys (int pin, int value) ********************************************************************************* */ -void pwmWriteWPi (int pin, int value) -{ - int port ; - - pin = pinToGpio [pin & 63] ; - port = gpioToPwmPort [pin] ; - - *(pwm + port) = value & 0x3FF ; -} - void pwmWriteGpio (int pin, int value) { int port ; @@ -499,6 +490,11 @@ void pwmWriteGpio (int pin, int value) *(pwm + port) = value & 0x3FF ; } +void pwmWriteWPi (int pin, int value) +{ + pwmWriteGpio (pinToGpio [pin & 63], value) ; +} + void pwmWriteSys (int pin, int value) { return ; @@ -588,28 +584,21 @@ int digitalReadSys (int pin) ********************************************************************************* */ -void pullUpDnControlWPi (int pin, int pud) -{ - pin = pinToGpio [pin & 63] ; - pud &= 3 ; - - *(gpio + GPPUD) = pud ; delayMicroseconds (10) ; - *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (10) ; - - *(gpio + GPPUD) = 0 ; - *(gpio + gpioToPUDCLK [pin]) = 0 ; -} - void pullUpDnControlGpio (int pin, int pud) { pin &= 63 ; pud &= 3 ; - *(gpio + GPPUD) = pud ; delayMicroseconds (10) ; - *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (10) ; + *(gpio + GPPUD) = pud ; delayMicroseconds (5) ; + *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ; - *(gpio + GPPUD) = 0 ; - *(gpio + gpioToPUDCLK [pin]) = 0 ; + *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ; + *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ; +} + +void pullUpDnControlWPi (int pin, int pud) +{ + pullUpDnControlGpio (pinToGpio [pin & 63], pud) ; } void pullUpDnControlSys (int pin, int pud) @@ -685,6 +674,7 @@ void delay (unsigned int howLong) nanosleep (&sleeper, &dummy) ; } + /* * delayMicroseconds: * This is somewhat intersting. It seems that on the Pi, a single call @@ -977,5 +967,3 @@ int wiringPiSetupSys (void) return 0 ; } - - diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 5b9c8c7..03ab787 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -86,8 +86,6 @@ extern int piHiPri (int pri) ; // Extras from arduino land extern void delay (unsigned int howLong) ; -//extern void delayMicroseconds (unsigned int howLong) ; -//extern void delayMicrosecondsHard (unsigned int howLong) ; extern unsigned int millis (void) ; #ifdef __cplusplus From c213e051c67c179a8176c51f676934712562cff7 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 18 Aug 2012 12:04:46 +0100 Subject: [PATCH 04/97] Added some comments about removing the call to pullUpDnControl() in pinMode () --- wiringPi/wiringPi.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index fb5e7c6..0a4da30 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -401,9 +401,15 @@ void pinModeGpio (int pin, int mode) } // When we change mode of any pin, we remove the pull up/downs - -// delayMicroseconds (300) ; +// Or we used to... Hm. Commented out now because for some wieird reason, +// it seems to block subsequent attempts to set the pull up/downs and I've +// not quite gotten to the bottom of why this happens +// The down-side is that the pull up/downs are rememberd in the SoC between +// power cycles, so it's going to be a good idea to explicitly set them in +// any new code. +// // pullUpDnControl (pin, PUD_OFF) ; + } void pinModeWPi (int pin, int mode) From 4bf5e131e6e513189961f3b4745d19dc9a263da6 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 18 Aug 2012 13:22:36 +0100 Subject: [PATCH 05/97] Added a description! --- description | 1 + 1 file changed, 1 insertion(+) create mode 100644 description diff --git a/description b/description new file mode 100644 index 0000000..7536012 --- /dev/null +++ b/description @@ -0,0 +1 @@ +wiringPi - A 'wiring' like library for the Raspberry Pi From f9bec274ec8d464c17f1f62cfb9314c417aaaa86 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 18 Aug 2012 13:53:01 +0100 Subject: [PATCH 06/97] Tidying up some old debug --- wiringPi/wiringSerial.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/wiringPi/wiringSerial.c b/wiringPi/wiringSerial.c index b401894..28ec598 100644 --- a/wiringPi/wiringSerial.c +++ b/wiringPi/wiringSerial.c @@ -20,8 +20,6 @@ *********************************************************************** */ -#undef DEBUG - #include #include #include @@ -49,10 +47,6 @@ int serialOpen (char *device, int baud) speed_t myBaud ; int status, fd ; -#ifdef DEBUG - printf ("openSerialPort: <%s> baud: $d\n", device, baud) ; -#endif - switch (baud) { case 50: myBaud = B50 ; break ; From c056a9e6413fb65022aca70d697fda7f542127ed Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 18 Aug 2012 14:07:12 +0100 Subject: [PATCH 07/97] Tidying up --- description | 1 - 1 file changed, 1 deletion(-) delete mode 100644 description diff --git a/description b/description deleted file mode 100644 index 7536012..0000000 --- a/description +++ /dev/null @@ -1 +0,0 @@ -wiringPi - A 'wiring' like library for the Raspberry Pi From 8fcaefd557ccd55be0cdb085238e72bf4ecb6c15 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 18 Aug 2012 19:11:02 +0100 Subject: [PATCH 08/97] Updated Makefile so that uninstall works properly --- wiringPi/Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 51f4773..3798ce0 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -82,12 +82,14 @@ install: $(TARGET) uninstall: @echo [uninstall] rm -f /usr/local/include/lcd.h + rm -f /usr/local/include/lpiNes.h + rm -f /usr/local/include/gertboard.h rm -f /usr/local/include/wiringShift.h + rm -f /usr/local/include/wiringSerial.h rm -f /usr/local/include/wiringPi.h rm -f /usr/local/lib/libwiringPi.a - # DO NOT DELETE wiringPi.o: wiringPi.h From c433519cf10b7f012184b83dc7d18193a5064a6e Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 18 Aug 2012 20:07:42 +0100 Subject: [PATCH 09/97] Updated gpio program to fix some minor issues. Added top-level build script for lazy building. --- build | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 build diff --git a/build b/build new file mode 100755 index 0000000..9df047f --- /dev/null +++ b/build @@ -0,0 +1,28 @@ +#!/bin/bash + +if [ x$1 = "xclean" ]; then + echo Cleaning + echo + cd wiringPi + make clean + cd ../gpio + make clean + cd ../examples + make clean + cd .. +else + echo wiringPi Build script - please wait... + echo + cd wiringPi + make + sudo make install + cd ../gpio + make + sudo make install + cd ../examples + make + cd .. +fi + +echo +echo All Done. From f99ffeda401362658a69ea3df98744738ded3554 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 19 Aug 2012 15:12:45 +0100 Subject: [PATCH 10/97] Updated the GPIO command to add in new features for PWM Updated wiringPi to add new PWM controls. --- gpio/Makefile | 2 +- gpio/gpio.1 | 70 ++++++++++++++++++++++++++++++++--------- gpio/gpio.c | 77 ++++++++++++++++++++++++++++++++------------- wiringPi/wiringPi.c | 58 +++++++++++++++++++++++++++++----- wiringPi/wiringPi.h | 8 +++++ 5 files changed, 170 insertions(+), 45 deletions(-) diff --git a/gpio/Makefile b/gpio/Makefile index c0740c9..a92dd5f 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -41,7 +41,7 @@ OBJ = gpio.o all: gpio -gpio: gpio.o +gpio: gpio.o /usr/local/lib/libwiringPi.a @echo [LD] @$(CC) -o $@ gpio.o $(LDFLAGS) $(LIBS) diff --git a/gpio/gpio.1 b/gpio/gpio.1 index 7da64b2..73e43c8 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -4,20 +4,38 @@ gpio \- Command-line access to Raspberry Pi and PiFace GPIO .SH SYNOPSIS -.TP .B gpio -.RB [ \-v ] -.TP +.B \-v +.PP .B gpio -.RB [ \-g ] -.RB < read/write/pwm/mode ...> -.TP +.B [ \-g ] +.B read/write/pwm/mode ... +.PP .B gpio -.RB [ \-p ] -.RB < read/write/mode ...> -.TP +.B [ \-p ] +.B read/write/mode +.B ... +.PP .B gpio -.RB < export/edge/unexport/unexportall/exports ...> +.B unexportall/exports +.PP +.B gpio +.B export/edge/unexport +.B ... +.PP +.B gpio +.B drive +group value +.PP +.B gpio +.B pwm-bal/pwm-ms +.PP +.B gpio +.B pwmr +range +.PP +.B gpio +.B load \ i2c/spi .SH DESCRIPTION @@ -64,6 +82,14 @@ 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 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. + .TP .B export Export a GPIO pin in the \fI/sys/class/gpio\fR directory. Use like the @@ -96,12 +122,26 @@ requiring root/sudo. 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. +.B drive +group value + +Change the pad driver value for the given pad group to the supplied drive +value. Group is 0, 1 or 2 and value is 0-7. Do not use unless you are +absolutely sure you know what you're doing. .TP -.B exports -Print a list (if any) of all the exported GPIO pins and their current values. +.B pwm-bal/pwm-ms +Change the PWM mode to balanced (the default) or mark:space ratio (traditional) + +.TP +.B pwmr +Change the PWM range register. The default is 1024. + +.TP +.B load i2c/spi +This loads the i2c or the spi drivers into the system and changes the permissions on +the associated /dev/ entries so that the current user has access to them. + .SH "WiringPi vs. GPIO Pin numbering" @@ -170,7 +210,7 @@ Gordon Henderson .SH "REPORTING BUGS" -Report bugs to +Report bugs to .SH COPYRIGHT diff --git a/gpio/gpio.c b/gpio/gpio.c index 54dfbc3..1d12aab 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -46,6 +46,8 @@ char *usage = "Usage: gpio -v\n" " gpio [-p] ...\n" " gpio export/edge/unexport/unexportall/exports ...\n" " gpio drive \n" + " gpio pwm-bal/pwm-ms \n" + " gpio pwmr \n" " gpio load spi/i2c" ; @@ -487,18 +489,12 @@ void doMode (int argc, char *argv []) mode = argv [3] ; - /**/ if (strcasecmp (mode, "in") == 0) - pinMode (pin, INPUT) ; - else if (strcasecmp (mode, "out") == 0) - pinMode (pin, OUTPUT) ; - else if (strcasecmp (mode, "pwm") == 0) - pinMode (pin, PWM_OUTPUT) ; - else if (strcasecmp (mode, "up") == 0) - pullUpDnControl (pin, PUD_UP) ; - else if (strcasecmp (mode, "down") == 0) - pullUpDnControl (pin, PUD_DOWN) ; - else if (strcasecmp (mode, "tri") == 0) - pullUpDnControl (pin, PUD_OFF) ; + /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; + else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ; + else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ; + else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ; + else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; + else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; else { fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ; @@ -629,6 +625,39 @@ void doPwm (int argc, char *argv []) } +/* + * doPwmMode: doPwmRange: + * Change the PWM mode and Range values + ********************************************************************************* + */ + +static void doPwmMode (int mode) +{ + pwmSetMode (mode) ; +} + +static void doPwmRange (int argc, char *argv []) +{ + unsigned int range ; + + if (argc != 3) + { + fprintf (stderr, "Usage: %s pwmr \n", argv [0]) ; + exit (1) ; + } + + range = (unsigned int)strtoul (argv [2], NULL, 10) ; + + if (range == 0) + { + fprintf (stderr, "%s: range must be > 0\n", argv [0]) ; + exit (1) ; + } + + pwmSetRange (range) ; +} + + /* * main: * Start here @@ -734,18 +763,22 @@ int main (int argc, char *argv []) wpMode = WPI_MODE_PINS ; } +// Check for PWM operations + + if (wpMode != WPI_MODE_PIFACE) + { + if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; } + if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; } + if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; } + } + // Check for wiring commands - /**/ if (strcasecmp (argv [1], "write" ) == 0) - doWrite (argc, argv) ; - else if (strcasecmp (argv [1], "read" ) == 0) - doRead (argc, argv) ; - else if (strcasecmp (argv [1], "mode" ) == 0) - doMode (argc, argv) ; - else if (strcasecmp (argv [1], "pwm" ) == 0) - doPwm (argc, argv) ; - else if (strcasecmp (argv [1], "drive" ) == 0) - doPadDrive (argc, argv) ; + /**/ if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ; + else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ; + else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ; + else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ; + else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; else { fprintf (stderr, "%s: Unknown command: %s. (read/write/pwm/mode/drive expected)\n", argv [0], argv [1]) ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 0a4da30..bd3c999 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -79,6 +79,8 @@ void (*setPadDrive) (int group, int value) ; int (*digitalRead) (int pin) ; int (*waitForInterrupt) (int pin, int mS) ; void (*delayMicroseconds) (unsigned int howLong) ; +void (*pwmSetMode) (int mode) ; +void (*pwmSetRange) (unsigned int range) ; #ifndef TRUE @@ -377,22 +379,25 @@ void pinModeGpio (int pin, int mode) if (!pwmRunning) { + *(pwm + PWM_CONTROL) = 0 ; // Stop PWM + delayMicroseconds (10) ; + // Gert/Doms Values - *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32<<12) ; // set pwm div to 32 (19.2/3 = 600KHz) + *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32<<12) ; // set pwm div to 32 (19.2/32 = 600KHz) *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // 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) ; + *(pwm + PWM0_RANGE) = 0x400 ; delayMicroseconds (10) ; + *(pwm + PWM1_RANGE) = 0x400 ; delayMicroseconds (10) ; + // Enable PWMs *(pwm + PWM0_DATA) = 512 ; *(pwm + PWM1_DATA) = 512 ; +// Balanced mode (default) + *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; pwmRunning = TRUE ; @@ -423,6 +428,38 @@ void pinModeSys (int pin, int mode) } +/* + * pwmControl: + * Allow the user to control some of the PWM functions + ********************************************************************************* + */ + +void pwmSetModeWPi (int mode) +{ + if (mode == PWM_MODE_MS) + *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ; + else + *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; +} + +void pwmSetModeSys (int mode) +{ + return ; +} + + +void pwmSetRangeWPi (unsigned int range) +{ + *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ; + *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ; +} + +void pwmSetRangeSys (unsigned int range) +{ + return ; +} + + #ifdef notYetReady /* * pinED01: @@ -774,6 +811,8 @@ int wiringPiSetup (void) digitalRead = digitalReadWPi ; waitForInterrupt = waitForInterruptWPi ; delayMicroseconds = delayMicrosecondsWPi ; + pwmSetMode = pwmSetModeWPi ; + pwmSetRange = pwmSetRangeWPi ; // Open the master /dev/memory device @@ -928,6 +967,8 @@ int wiringPiSetupGpio (void) digitalRead = digitalReadGpio ; waitForInterrupt = waitForInterruptGpio ; delayMicroseconds = delayMicrosecondsWPi ; // Same + pwmSetMode = pwmSetModeWPi ; + pwmSetRange = pwmSetRangeWPi ; return 0 ; } @@ -956,6 +997,9 @@ int wiringPiSetupSys (void) digitalRead = digitalReadSys ; waitForInterrupt = waitForInterruptSys ; delayMicroseconds = delayMicrosecondsSys ; + pwmSetMode = pwmSetModeSys ; + pwmSetRange = pwmSetRangeSys ; + // Open and scan the directory, looking for exported GPIOs, and pre-open // the 'value' interface to speed things up for later diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 03ab787..1d21fa0 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -41,6 +41,12 @@ #define PUD_DOWN 1 #define PUD_UP 2 +// PWM + +#define PWM_MODE_MS 0 +#define PWM_MODE_BAL 1 + + // Function prototypes // c++ wrappers thanks to a commend by Nick Lott // (and others on the Raspberry Pi forums) @@ -65,6 +71,8 @@ extern void (*pwmWrite) (int pin, int value) ; extern void (*setPadDrive) (int group, int value) ; extern int (*digitalRead) (int pin) ; extern void (*delayMicroseconds) (unsigned int howLong) ; +extern void (*pwmSetMode) (int mode) ; +extern void (*pwmSetRange) (unsigned int range) ; // Interrupts From 178ea08a0f557a4676a9b16aeca5259806ed0ce6 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 19 Aug 2012 15:17:03 +0100 Subject: [PATCH 11/97] Minor updates to GPIO and README files --- gpio/gpio.c | 98 +++++++++++++++++++------------------------------ wiringPi/README | 4 +- 2 files changed, 41 insertions(+), 61 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 54dfbc3..2b62953 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -42,6 +42,7 @@ static int wpMode ; char *usage = "Usage: gpio -v\n" + " gpio -h\n" " gpio [-g] ...\n" " gpio [-p] ...\n" " gpio export/edge/unexport/unexportall/exports ...\n" @@ -49,7 +50,6 @@ char *usage = "Usage: gpio -v\n" " gpio load spi/i2c" ; - /* * changeOwner: * Change the ownership of the file to the real userId of the calling @@ -171,7 +171,7 @@ static void doLoad (int argc, char *argv []) ********************************************************************************* */ -void doExports (void) +static void doExports (int argc, char *argv []) { int fd ; int i, l, first ; @@ -326,8 +326,6 @@ void doEdge (int argc, char *argv []) int pin ; char *mode ; char fName [128] ; - uid_t uid ; - gid_t gid ; if (argc != 4) { @@ -335,8 +333,7 @@ void doEdge (int argc, char *argv []) exit (1) ; } - pin = atoi (argv [2]) ; - + pin = atoi (argv [2]) ; mode = argv [3] ; // Export the pin and set direction to input @@ -367,40 +364,23 @@ void doEdge (int argc, char *argv []) 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") ; + /**/ 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 () ; +// Change ownership of the value and edge files, so the current user can actually use it! 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 + changeOwner (argv [0], fName) ; 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) ; - } + changeOwner (argv [0], fName) ; fclose (fd) ; } @@ -487,18 +467,12 @@ void doMode (int argc, char *argv []) mode = argv [3] ; - /**/ if (strcasecmp (mode, "in") == 0) - pinMode (pin, INPUT) ; - else if (strcasecmp (mode, "out") == 0) - pinMode (pin, OUTPUT) ; - else if (strcasecmp (mode, "pwm") == 0) - pinMode (pin, PWM_OUTPUT) ; - else if (strcasecmp (mode, "up") == 0) - pullUpDnControl (pin, PUD_UP) ; - else if (strcasecmp (mode, "down") == 0) - pullUpDnControl (pin, PUD_DOWN) ; - else if (strcasecmp (mode, "tri") == 0) - pullUpDnControl (pin, PUD_OFF) ; + /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; + else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ; + else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ; + else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ; + else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; + else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; else { fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ; @@ -645,6 +619,12 @@ int main (int argc, char *argv []) return 1 ; } + if (strcasecmp (argv [1], "-h") == 0) + { + printf ("%s: %s\n", argv [0], usage) ; + return 0 ; + } + if (strcasecmp (argv [1], "-v") == 0) { printf ("gpio version: %s\n", VERSION) ; @@ -683,12 +663,16 @@ int main (int argc, char *argv []) // Initial test for /sys/class/gpio operations: - /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports () ; return 0 ; } - else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; } - else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; } + /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; } + else if (strcasecmp (argv [1], "export" ) == 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 ; } - else if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; } + else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; } + +// Check for drive or load commands: + + if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; } + if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; } // Check for -g argument @@ -696,7 +680,7 @@ int main (int argc, char *argv []) { if (wiringPiSetupGpio () == -1) { - fprintf (stderr, "%s: Unable to initialise GPIO in GPIO mode.\n", argv [0]) ; + fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ; exit (1) ; } @@ -728,7 +712,7 @@ int main (int argc, char *argv []) { if (wiringPiSetup () == -1) { - fprintf (stderr, "%s: Unable to initialise GPIO in wiringPi mode\n", argv [0]) ; + fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ; exit (1) ; } wpMode = WPI_MODE_PINS ; @@ -736,19 +720,13 @@ int main (int argc, char *argv []) // Check for wiring commands - /**/ if (strcasecmp (argv [1], "write" ) == 0) - doWrite (argc, argv) ; - else if (strcasecmp (argv [1], "read" ) == 0) - doRead (argc, argv) ; - else if (strcasecmp (argv [1], "mode" ) == 0) - doMode (argc, argv) ; - else if (strcasecmp (argv [1], "pwm" ) == 0) - doPwm (argc, argv) ; - else if (strcasecmp (argv [1], "drive" ) == 0) - doPadDrive (argc, argv) ; + /**/ if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ; + else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ; + else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ; + else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ; else { - fprintf (stderr, "%s: Unknown command: %s. (read/write/pwm/mode/drive expected)\n", argv [0], argv [1]) ; + fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ; exit (1) ; } return 0 ; diff --git a/wiringPi/README b/wiringPi/README index 781510a..c79754e 100644 --- a/wiringPi/README +++ b/wiringPi/README @@ -1,6 +1,8 @@ WiringPi: An implementation of most of the Arduino Wiring - functions for the Raspberry Pi + functions for the Raspberry Pi, + along with many more features and libraries to support + hardware, etc. on the Raspberry Pi Full details at: https://projects.drogon.net/raspberry-pi/wiringpi/ From 870ba4129ffb48bb66de155de61bdae5b144c858 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 19 Aug 2012 15:38:25 +0100 Subject: [PATCH 12/97] Updated pwmWrite to not mask the value written in-case a call to pumSetRange() happens. ANDing with 0x3FF was wrong anyway as the PWM counter does from zero to the top value, not top-1 as I originally thought. --- wiringPi/wiringPi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index bd3c999..39c34da 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -530,7 +530,7 @@ void pwmWriteGpio (int pin, int value) pin = pin & 63 ; port = gpioToPwmPort [pin] ; - *(pwm + port) = value & 0x3FF ; + *(pwm + port) = value ; } void pwmWriteWPi (int pin, int value) From fbab4b832992c44eb2848b1f7fbb047d1e0daa9a Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 20 Aug 2012 11:08:34 +0100 Subject: [PATCH 13/97] Changes to the piNes driver to relect the new delayMicroseconds() code. Seems the old 4011 in the NES controller isn't that fast afterall! --- wiringPi/piNes.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/wiringPi/piNes.c b/wiringPi/piNes.c index 1722d31..a115050 100644 --- a/wiringPi/piNes.c +++ b/wiringPi/piNes.c @@ -38,6 +38,8 @@ #define NES_A 0x80 +#define PULSE_TIME 25 + // Data to store the pins for each controller struct nesPinsStruct @@ -91,10 +93,8 @@ unsigned int readNesJoystick (int joystick) // Toggle Latch - which presents the first bit - digitalWrite (pins->lPin, HIGH) ; - delayMicroseconds (1) ; - digitalWrite (pins->lPin, LOW) ; - delayMicroseconds (1) ; + digitalWrite (pins->lPin, HIGH) ; delayMicroseconds (PULSE_TIME) ; + digitalWrite (pins->lPin, LOW) ; delayMicroseconds (PULSE_TIME) ; // Read first bit @@ -104,12 +104,10 @@ unsigned int readNesJoystick (int joystick) for (i = 0 ; i < 7 ; ++i) { - digitalWrite (pins->cPin, HIGH) ; - delayMicroseconds (1) ; - digitalWrite (pins->cPin, LOW) ; - delayMicroseconds (1) ; + digitalWrite (pins->cPin, HIGH) ; delayMicroseconds (PULSE_TIME) ; + digitalWrite (pins->cPin, LOW) ; delayMicroseconds (PULSE_TIME) ; value = (value << 1) | digitalRead (pins->dPin) ; } - return ~value ; + return value ^ 0xFF ; } From a20fb1b1540e721ae373c3474e4dc201a95d2c61 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Fri, 24 Aug 2012 17:49:26 +0100 Subject: [PATCH 14/97] Added software PWM module into wiringPi - library code and an example. --- examples/Makefile | 12 +++-- examples/softPwm.c | 44 +++++++++++++++++ wiringPi/Makefile | 5 +- wiringPi/softPwm.c | 116 +++++++++++++++++++++++++++++++++++++++++++++ wiringPi/softPwm.h | 27 +++++++++++ 5 files changed, 198 insertions(+), 6 deletions(-) create mode 100644 examples/softPwm.c create mode 100644 wiringPi/softPwm.c create mode 100644 wiringPi/softPwm.h diff --git a/examples/Makefile b/examples/Makefile index c1b2182..450e0dc 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -35,11 +35,11 @@ LIBS = -lwiringPi # Should not alter anything below this line ############################################################################### -SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c gertboard.c nes.c delayTest.c +SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c gertboard.c nes.c delayTest.c softPwm.c -OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o gertboard.o nes.o delayTest.o +OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o gertboard.o nes.o delayTest.o softPwm.o -all: test1 test2 speed lcd wfi piface gertboard nes +all: test1 test2 speed lcd wfi piface gertboard nes softPwm test1: test1.o @echo [link] @@ -73,6 +73,10 @@ nes: nes.o @echo [link] $(CC) -o $@ nes.o $(LDFLAGS) $(LIBS) -lm +softPwm: softPwm.o + @echo [link] + $(CC) -o $@ softPwm.o $(LDFLAGS) $(LIBS) -lm -lpthread + delayTest: delayTest.o @echo [link] @@ -84,7 +88,7 @@ delayTest: delayTest.o @$(CC) -c $(CFLAGS) $< -o $@ clean: - rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface gertboard nes delayTest + rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface gertboard nes delayTest softPwm tags: $(SRC) @echo [ctags] diff --git a/examples/softPwm.c b/examples/softPwm.c new file mode 100644 index 0000000..e558c4b --- /dev/null +++ b/examples/softPwm.c @@ -0,0 +1,44 @@ + +#include +#include +#include + +#include +#include + +#define RANGE 100 +#define NUM_LEDS 12 + +int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 } ; + +int values [NUM_LEDS] = { 0, 17, 32, 50, 67, 85, 100, 85, 67, 50, 32, 17 } ; + +int main () +{ + int i, j ; + + if (wiringPiSetup () == -1) + { + fprintf (stdout, "oops: %s\n", strerror (errno)) ; + return 1 ; + } + + for (i = 0 ; i < NUM_LEDS ; ++i) + { + softPwmCreate (ledMap [i], 0, RANGE) ; + printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ; + } + + for (;;) + { + for (i = 0 ; i < NUM_LEDS ; ++i) + softPwmWrite (ledMap [i], values [i]) ; + + delay (50) ; + + i = values [0] ; + for (j = 0 ; j < NUM_LEDS - 1 ; ++j) + values [j] = values [j + 1] ; + values [NUM_LEDS - 1] = i ; + } +} diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 3798ce0..b80caea 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -38,12 +38,12 @@ LIBS = SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ gertboard.c \ piNes.c \ - lcd.c piHiPri.c piThread.c + lcd.c piHiPri.c piThread.c softPwm.c OBJ = wiringPi.o wiringPiFace.o wiringSerial.o wiringShift.o \ gertboard.o \ piNes.o \ - lcd.o piHiPri.o piThread.o + lcd.o piHiPri.o piThread.o softPwm.o all: $(TARGET) @@ -76,6 +76,7 @@ install: $(TARGET) install -m 0644 wiringShift.h /usr/local/include install -m 0644 gertboard.h /usr/local/include install -m 0644 piNes.h /usr/local/include + install -m 0644 softPwm.h /usr/local/include install -m 0644 lcd.h /usr/local/include install -m 0644 libwiringPi.a /usr/local/lib diff --git a/wiringPi/softPwm.c b/wiringPi/softPwm.c new file mode 100644 index 0000000..56bf4d8 --- /dev/null +++ b/wiringPi/softPwm.c @@ -0,0 +1,116 @@ +/* + * softPwm.c: + * Provide 2 channels of software driven PWM. + * 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" +#include "softPwm.h" + +#define MAX_PINS 64 + +// The PWM Frequency is derived from the "pulse time" below. Essentially, +// the frequency is a function of the range and this pulse time. +// The total period will be range * pulse time in uS, so a pulse time +// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 uS +// which is a frequency of 100Hz. +// +// It's possible to get a higher frequency by lowering the pulse time, +// however CPU uage will skyrocket as wiringPi uses a hard-loop to time +// periods under 100uS - this is because the Linux timer calls are just +// accurate at all, and have an overhead. +// +// Another way to increase the frequency is to reduce the range - however +// that reduces the overall output accuracy... + +#define PULSE_TIME 100 + +static int marks [MAX_PINS] ; +static int range [MAX_PINS] ; + +int newPin = -1 ; + + +/* + * softPwmThread: + * Thread to do the actual PWM output + ********************************************************************************* + */ + +static PI_THREAD (softPwmThread) +{ + int pin, mark, space ; + + pin = newPin ; + newPin = -1 ; + + piHiPri (50) ; + + for (;;) + { + mark = marks [pin] ; + space = range [pin] - mark ; + + if (mark != 0) + digitalWrite (pin, HIGH) ; + delayMicroseconds (mark * 100) ; + + if (space != 0) + digitalWrite (pin, LOW) ; + delayMicroseconds (space * 100) ; + } + + return NULL ; +} + +void softPwmWrite (int pin, int value) +{ + marks [pin] = value ; +} + + +/* + * softPwmCreate: + * Create a new PWM thread. + ********************************************************************************* + */ + +int softPwmCreate (int pin, int initialValue, int pwmRange) +{ + int res ; + + pinMode (pin, OUTPUT) ; + digitalWrite (pin, LOW) ; + + marks [pin] = initialValue ; + range [pin] = pwmRange ; + + newPin = pin ; + res = piThreadCreate (softPwmThread) ; + + while (newPin != -1) + delay (1) ; + + return res ; +} diff --git a/wiringPi/softPwm.h b/wiringPi/softPwm.h new file mode 100644 index 0000000..a6d8cd4 --- /dev/null +++ b/wiringPi/softPwm.h @@ -0,0 +1,27 @@ +/* + * softPwm.h: + * Provide 2 channels of software driven PWM. + * 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 . + *********************************************************************** + */ + +extern int setupSoftPwm (int pin) ; +extern int softPwmCreate (int pin, int value, int range) ; +extern void softPwmWrite (int pin, int value) ; From c6799ea9980082412daddf7167000e9ecc0d0577 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Fri, 24 Aug 2012 19:07:54 +0100 Subject: [PATCH 15/97] Updating softPwm to fix some typos. --- wiringPi/softPwm.c | 14 ++++++++++++++ wiringPi/softPwm.h | 5 ++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/wiringPi/softPwm.c b/wiringPi/softPwm.c index 56bf4d8..b568dfb 100644 --- a/wiringPi/softPwm.c +++ b/wiringPi/softPwm.c @@ -84,8 +84,22 @@ static PI_THREAD (softPwmThread) return NULL ; } + +/* + * softPwmWrite: + * Write a PWM value to the given pin + ********************************************************************************* + */ + void softPwmWrite (int pin, int value) { + pin &= 63 ; + + /**/ if (value < 0) + value = 0 ; + else if (value > range [pin]) + value = range [pin] ; + marks [pin] = value ; } diff --git a/wiringPi/softPwm.h b/wiringPi/softPwm.h index a6d8cd4..4a17cb0 100644 --- a/wiringPi/softPwm.h +++ b/wiringPi/softPwm.h @@ -22,6 +22,5 @@ *********************************************************************** */ -extern int setupSoftPwm (int pin) ; -extern int softPwmCreate (int pin, int value, int range) ; -extern void softPwmWrite (int pin, int value) ; +extern int softPwmCreate (int pin, int value, int range) ; +extern void softPwmWrite (int pin, int value) ; From 46665739106ef90ae8c792158c35ba0e5acebe8a Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 25 Aug 2012 12:31:14 +0100 Subject: [PATCH 16/97] Added C++ wrappers for some of the newer .h files --- examples/softPwm.c | 25 +++++++++++++++++++++++++ wiringPi/lcd.h | 8 ++++++++ wiringPi/piNes.h | 8 ++++++++ wiringPi/softPwm.h | 8 ++++++++ 4 files changed, 49 insertions(+) diff --git a/examples/softPwm.c b/examples/softPwm.c index e558c4b..09b4ae0 100644 --- a/examples/softPwm.c +++ b/examples/softPwm.c @@ -16,6 +16,7 @@ int values [NUM_LEDS] = { 0, 17, 32, 50, 67, 85, 100, 85, 67, 50, 32, 17 } ; int main () { int i, j ; + char buf [80] ; if (wiringPiSetup () == -1) { @@ -29,6 +30,30 @@ int main () printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ; } + fgets (buf, 80, stdin) ; + +// Bring all up one by one: + + for (i = 0 ; i < NUM_LEDS ; ++i) + for (j = 0 ; j <= 100 ; ++j) + { + softPwmWrite (ledMap [i], j) ; + delay (10) ; + } + + fgets (buf, 80, stdin) ; + +// Down fast + + for (i = 100 ; i > 0 ; --i) + { + for (j = 0 ; j < NUM_LEDS ; ++j) + softPwmWrite (ledMap [j], i) ; + delay (10) ; + } + + fgets (buf, 80, stdin) ; + for (;;) { for (i = 0 ; i < NUM_LEDS ; ++i) diff --git a/wiringPi/lcd.h b/wiringPi/lcd.h index 094f5f5..d4c724c 100644 --- a/wiringPi/lcd.h +++ b/wiringPi/lcd.h @@ -33,5 +33,13 @@ extern void lcdPutchar (int fd, uint8_t data) ; extern void lcdPuts (int fd, char *string) ; extern void lcdPrintf (int fd, char *message, ...) ; +#ifdef __cplusplus +extern "C" { +#endif + extern int lcdInit (int rows, int cols, int bits, int rs, int strb, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/piNes.h b/wiringPi/piNes.h index b76c415..897f181 100644 --- a/wiringPi/piNes.h +++ b/wiringPi/piNes.h @@ -33,5 +33,13 @@ #define NES_B 0x40 #define NES_A 0x80 +#ifdef __cplusplus +extern "C" { +#endif + extern int setupNesJoystick (int dPin, int cPin, int lPin) ; extern unsigned int readNesJoystick (int joystick) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/softPwm.h b/wiringPi/softPwm.h index 4a17cb0..28ad299 100644 --- a/wiringPi/softPwm.h +++ b/wiringPi/softPwm.h @@ -22,5 +22,13 @@ *********************************************************************** */ +#ifdef __cplusplus +extern "C" { +#endif + extern int softPwmCreate (int pin, int value, int range) ; extern void softPwmWrite (int pin, int value) ; + +#ifdef __cplusplus +} +#endif From 99095e3fa0c9e028c2c00d020392b1e22210379a Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 27 Aug 2012 20:56:14 +0100 Subject: [PATCH 17/97] Added new SPI driver helpers. Changed the gertboard code to use it and ran more tests on he Gertboard code. --- examples/gertboard.c | 46 +++++++++++++--- gpio/gpio.1 | 32 +++++++++-- gpio/gpio.c | 94 +++++++++++++++++++++++++++++++-- wiringPi/Makefile | 5 +- wiringPi/gertboard.c | 102 +++++++---------------------------- wiringPi/wiringPiSPI.c | 117 +++++++++++++++++++++++++++++++++++++++++ wiringPi/wiringPiSPI.h | 35 ++++++++++++ 7 files changed, 331 insertions(+), 100 deletions(-) create mode 100644 wiringPi/wiringPiSPI.c create mode 100644 wiringPi/wiringPiSPI.h diff --git a/examples/gertboard.c b/examples/gertboard.c index 8344d48..8f26dd4 100644 --- a/examples/gertboard.c +++ b/examples/gertboard.c @@ -14,33 +14,63 @@ #include #include -//#include #include +#define B_SIZE 200 +#undef DO_TIMING + #include #include int main (void) { - int angle ; - int h1 ; + double angle ; + int i ; uint32_t x1 ; + int buffer [B_SIZE] ; + +#ifdef DO_TIMING + unsigned int now, then ; +#endif printf ("Raspberry Pi Gertboard SPI test program\n") ; - if (gertboardSPISetup () == -1) + if (wiringPiSetupSys () < 0) + return -1 ; + + if (gertboardSPISetup () < 0) return 1 ; +// Generate a Sine Wave + + for (i = 0 ; i < B_SIZE ; ++i) + { + angle = ((double)i / (double)B_SIZE) * M_PI * 2.0 ; + buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ; + } + + for (;;) { - for (angle = 0 ; angle < 360 ; ++angle) - { - h1 = (int)rint (sin ((double)angle * M_PI / 180.0) * 127.0 + 128.0) ; - gertboardAnalogWrite (0, h1) ; +#ifdef DO_TIMING + then = millis () ; +#endif + for (i = 0 ; i < B_SIZE ; ++i) + { + gertboardAnalogWrite (0, buffer [i]) ; + +#ifndef DO_TIMING x1 = gertboardAnalogRead (0) ; gertboardAnalogWrite (1, x1 >> 2) ; // 10-bit A/D, 8-bit D/A +#endif } + +#ifdef DO_TIMING + now = millis () ; + printf ("%4d mS, %9.7f S/sample", now - then, ((double)(now - then) / 1000.0) / (double)B_SIZE) ; + printf (" -> %9.4f samples/sec \n", 1 / (((double)(now - then) / 1000.0) / (double)B_SIZE)) ; +#endif } return 0 ; diff --git a/gpio/gpio.1 b/gpio/gpio.1 index 73e43c8..bc8e36e 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -36,14 +36,23 @@ range .PP .B gpio .B load \ i2c/spi +.PP +.B gpio +.B gbr +channel +.PP +.B gpio +.B gbw +channel value .SH DESCRIPTION .B GPIO -is a command line tool to allow the user easy access to the GPIO pins -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. +is a swiss army knofe of a command line tool to allow the user easy +access to the GPIO pins on the Raspberry Pi and the SPI A/D and D/A +convertors on the Gertboard. 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 \fI/sys/class/gpio\fR system directory to allow subsequent programs to use the \fR/sys/class/gpio\fR @@ -142,6 +151,21 @@ Change the PWM range register. The default is 1024. This loads the i2c or the spi drivers into the system and changes the permissions on the associated /dev/ entries so that the current user has access to them. +.TP +.B gbr +channel + +This reads the analog to digital convertor on the Gertboard on the given +channel. The board jumpers need to be in-place to do this operation. + +.TP +.B gbw +channel value + +This writes the supplied value to the output channel on the Gertboards +SPI digital to analogue convertor. +The board jumpers need to be in-place to do this operation. + .SH "WiringPi vs. GPIO Pin numbering" diff --git a/gpio/gpio.c b/gpio/gpio.c index 5a97aab..b696542 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -1,6 +1,7 @@ /* * gpio.c: - * Set-UID command-line interface to the Raspberry Pi's GPIO + * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry + * Pi's GPIO. * Copyright (c) 2012 Gordon Henderson *********************************************************************** * This file is part of wiringPi: @@ -21,7 +22,6 @@ *********************************************************************** */ -#include #include #include @@ -32,12 +32,15 @@ #include #include +#include +#include + #ifndef TRUE # define TRUE (1==1) # define FALSE (1==2) #endif -#define VERSION "1.1" +#define VERSION "1.2" static int wpMode ; @@ -49,7 +52,9 @@ char *usage = "Usage: gpio -v\n" " gpio drive \n" " gpio pwm-bal/pwm-ms \n" " gpio pwmr \n" - " gpio load spi/i2c" ; + " gpio load spi/i2c\n" + " gpio gbr \n" + " gpio gbw \n" ; /* @@ -518,6 +523,82 @@ static void doPadDrive (int argc, char *argv []) } +/* + * doGbw: + * gpio gbw channel value + ********************************************************************************* + */ + +static void doGbw (int argc, char *argv []) +{ + int channel, value ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s gbr \n", argv [0]) ; + exit (1) ; + } + + channel = atoi (argv [2]) ; + value = atoi (argv [3]) ; + + if ((channel < 0) || (channel > 1)) + { + fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ; + exit (1) ; + } + + if ((value < 0) || (value > 1023)) + { + fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ; + exit (1) ; + } + + if (gertboardSPISetup () == -1) + { + fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ; + exit (1) ; + } + + gertboardAnalogWrite (channel, value) ; +} + + +/* + * doGbr: + * gpio gbr channel + ********************************************************************************* + */ + +static void doGbr (int argc, char *argv []) +{ + int channel ; + + if (argc != 3) + { + fprintf (stderr, "Usage: %s gbr \n", argv [0]) ; + exit (1) ; + } + + channel = atoi (argv [2]) ; + + if ((channel < 0) || (channel > 1)) + { + fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ; + exit (1) ; + } + + if (gertboardSPISetup () == -1) + { + fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ; + exit (1) ; + } + + printf ("%d\n",gertboardAnalogRead (channel)) ; +} + + + /* * doWrite: * gpio write pin value @@ -709,6 +790,11 @@ int main (int argc, char *argv []) if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; } if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; } +// Gertboard commands + + if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; } + if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; } + // Check for -g argument if (strcasecmp (argv [1], "-g") == 0) diff --git a/wiringPi/Makefile b/wiringPi/Makefile index b80caea..dc189cd 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -38,12 +38,12 @@ LIBS = SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ gertboard.c \ piNes.c \ - lcd.c piHiPri.c piThread.c softPwm.c + lcd.c piHiPri.c piThread.c softPwm.c wiringPiSPI.c OBJ = wiringPi.o wiringPiFace.o wiringSerial.o wiringShift.o \ gertboard.o \ piNes.o \ - lcd.o piHiPri.o piThread.o softPwm.o + lcd.o piHiPri.o piThread.o softPwm.o wiringPiSPI.o all: $(TARGET) @@ -78,6 +78,7 @@ install: $(TARGET) install -m 0644 piNes.h /usr/local/include install -m 0644 softPwm.h /usr/local/include install -m 0644 lcd.h /usr/local/include + install -m 0644 wiringPiSPI.h /usr/local/include install -m 0644 libwiringPi.a /usr/local/lib uninstall: diff --git a/wiringPi/gertboard.c b/wiringPi/gertboard.c index bd7e60a..a8795d3 100644 --- a/wiringPi/gertboard.c +++ b/wiringPi/gertboard.c @@ -38,23 +38,16 @@ #include #include +#include "wiringPiSPI.h" + #include "gertboard.h" +// The A-D convertor won't run at more than 1MHz @ 3.3v -// The SPI bus parameters -// Variables as they need to be passed as pointers later on - -static char *spiA2D = "/dev/spidev0.0" ; -static char *spiD2A = "/dev/spidev0.1" ; -static uint8_t spiMode = 0 ; -static uint8_t spiBPW = 8 ; -static uint32_t spiSpeed = 100000 ; // 1MHz -static uint16_t spiDelay = 0; - -// Locals here to keep track of everything - -static int spiFdA2D ; -static int spiFdD2A ; +#define SPI_ADC_SPEED 1000000 +#define SPI_DAC_SPEED 1000000 +#define SPI_A2D 0 +#define SPI_D2A 1 /* @@ -66,10 +59,7 @@ static int spiFdD2A ; void gertboardAnalogWrite (int chan, int value) { - uint8_t spiBufTx [2] ; - uint8_t spiBufRx [2] ; - struct spi_ioc_transfer spi ; - + uint8_t spiData [2] ; uint8_t chanBits, dataBits ; if (chan == 0) @@ -80,17 +70,10 @@ void gertboardAnalogWrite (int chan, int value) chanBits |= ((value >> 4) & 0x0F) ; dataBits = ((value << 4) & 0xF0) ; - spiBufTx [0] = chanBits ; - spiBufTx [1] = dataBits ; + spiData [0] = chanBits ; + spiData [1] = dataBits ; - spi.tx_buf = (unsigned long)spiBufTx ; - spi.rx_buf = (unsigned long)spiBufRx ; - spi.len = 2 ; - spi.delay_usecs = spiDelay ; - spi.speed_hz = spiSpeed ; - spi.bits_per_word = spiBPW ; - - ioctl (spiFdD2A, SPI_IOC_MESSAGE(1), &spi) ; + wiringPiSPIDataRW (SPI_D2A, spiData, 2) ; } @@ -103,60 +86,21 @@ void gertboardAnalogWrite (int chan, int value) int gertboardAnalogRead (int chan) { - uint8_t spiBufTx [4] ; - uint8_t spiBufRx [4] ; - struct spi_ioc_transfer spi ; + uint8_t spiData [2] ; uint8_t chanBits ; if (chan == 0) - chanBits = 0b0110100 ; + chanBits = 0b11010000 ; else - chanBits = 0b0111100 ; + chanBits = 0b11110000 ; - spiBufTx [0] = chanBits ; - spiBufTx [1] = 0 ; + spiData [0] = chanBits ; + spiData [1] = 0 ; - spi.tx_buf = (unsigned long)spiBufTx ; - spi.rx_buf = (unsigned long)spiBufRx ; - spi.len = 4 ; - spi.delay_usecs = spiDelay ; - spi.speed_hz = spiSpeed ; - spi.bits_per_word = spiBPW ; + wiringPiSPIDataRW (SPI_A2D, spiData, 2) ; - ioctl (spiFdA2D, SPI_IOC_MESSAGE(1), &spi) ; - - return spiBufRx [0] << 8 | spiBufRx [1] ; -} - - -/* - * setParams: - * Output the SPI bus parameters to the given device - ********************************************************************************* - */ - -static int setParams (int fd) -{ - if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) - return -1 ; - - if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0) - return -1 ; - - if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) - return -1 ; - - if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) - return -1 ; - - if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &spiSpeed) < 0) - return -1 ; - - if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &spiSpeed) < 0) - return -1 ; - - return 0 ; + return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ; } @@ -168,16 +112,10 @@ static int setParams (int fd) int gertboardSPISetup (void) { - if ((spiFdA2D = open (spiA2D, O_RDWR)) < 0) + if (wiringPiSPISetup (SPI_A2D, SPI_ADC_SPEED) < 0) return -1 ; - if (setParams (spiFdA2D) != 0) - return -1 ; - - if ((spiFdD2A = open (spiD2A, O_RDWR)) < 0) - return -1 ; - - if (setParams (spiFdD2A) != 0) + if (wiringPiSPISetup (SPI_D2A, SPI_DAC_SPEED) < 0) return -1 ; return 0 ; diff --git a/wiringPi/wiringPiSPI.c b/wiringPi/wiringPiSPI.c new file mode 100644 index 0000000..f2e3000 --- /dev/null +++ b/wiringPi/wiringPiSPI.c @@ -0,0 +1,117 @@ +/* + * wiringPiSPI.c: + * Simplified SPI access routines + * 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 +#include + +#include "wiringPiSPI.h" + + +// The SPI bus parameters +// Variables as they need to be passed as pointers later on + +static char *spiDev0 = "/dev/spidev0.0" ; +static char *spiDev1 = "/dev/spidev0.1" ; +static uint8_t spiMode = 0 ; +static uint8_t spiBPW = 8 ; +static uint16_t spiDelay = 0; + +static uint32_t spiSpeeds [2] ; +static int spiFds [2] ; + + +/* + * wiringPiSPIGetFd: + * Return the file-descriptor for the given channel + ********************************************************************************* + */ + +int wiringPiSPIGetFd (int channel) +{ + return spiFds [channel &1] ; +} + + +/* + * wiringPiSPIDataRW: + * Write and Read a block of data over the SPI bus. + * Note the data ia being read into the transmit buffer, so will + * overwrite it! + * This is also a full-duplex operation. + ********************************************************************************* + */ + +int wiringPiSPIDataRW (int channel, unsigned char *data, int len) +{ + struct spi_ioc_transfer spi ; + + channel &= 1 ; + + spi.tx_buf = (unsigned long)data ; + spi.rx_buf = (unsigned long)data ; + spi.len = len ; + spi.delay_usecs = spiDelay ; + spi.speed_hz = spiSpeeds [channel] ; + spi.bits_per_word = spiBPW ; + + return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ; +} + + +/* + * wiringPiSPISetup: + * Open the SPI device, and set it up, etc. + ********************************************************************************* + */ + +int wiringPiSPISetup (int channel, int speed) +{ + int fd ; + + channel &= 1 ; + + if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0) + return -1 ; + + spiSpeeds [channel] = speed ; + spiFds [channel] = fd ; + +// Set SPI parameters. +// Why are we reading it afterwriting it? I've no idea, but for now I'm blindly +// copying example code I've seen online... + + if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) return -1 ; + if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0) return -1 ; + + if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) return -1 ; + if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) return -1 ; + + if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) return -1 ; + if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) return -1 ; + + return fd ; +} diff --git a/wiringPi/wiringPiSPI.h b/wiringPi/wiringPiSPI.h new file mode 100644 index 0000000..f53697d --- /dev/null +++ b/wiringPi/wiringPiSPI.h @@ -0,0 +1,35 @@ +/* + * wiringPiSPI.h: + * Simplified SPI access routines + * 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 . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +int wiringPiSPIGetFd (int channel) ; +int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ; +int wiringPiSPISetup (int channel, int speed) ; + +#ifdef __cplusplus +} +#endif From 728cdc4c236eecb9d484b84df9d505d90b38157c Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Wed, 5 Sep 2012 19:26:58 +0100 Subject: [PATCH 18/97] Updated wiringPi to check the the hardware board revision, to accomodate the changes in pin that are present on the Revision 2 Raspberry Pi board. Added in simple debugging too - set the environment variable WIRINGPI_DEBUG to get some more debug messages out --- wiringPi/wiringPi.c | 99 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 4 deletions(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 39c34da..2a5c533 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -56,6 +56,8 @@ #include #include +#include +#include #include #include #include @@ -169,6 +171,14 @@ static volatile uint32_t *timer ; static volatile uint32_t *timerIrqRaw ; +// Raspberry Pi board revision + +static int boardRevision = -1 ; + +// Debugging + +static int wiringPiDebug = FALSE ; + // The BCM2835 has 54 GPIO pins. // BCM2835 data sheet, Page 90 onwards. // There are 6 control registers, each control the functions of a block @@ -198,8 +208,11 @@ static int sysFds [64] ; // pinToGpio: // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin +// Cope for 2 different board revieions here -static int pinToGpio [64] = +static int *pinToGpio ; + +static int pinToGpioR1 [64] = { 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7 0, 1, // I2C - SDA0, SCL0 @@ -209,7 +222,23 @@ static int pinToGpio [64] = // 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, // ... 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 +} ; + +static int pinToGpioR2 [64] = +{ + 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7 + 2, 3, // I2C - SDA0, SCL0 + 8, 7, // SPI - CE1, CE0 + 10, 9, 11, // SPI - MOSI, MISO, SCLK + 14, 15, // UART - Tx, Rx + 28, 29, 30, 31, // New GPIOs 8 though 11 + +// Padding: + + -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 } ; @@ -800,9 +829,19 @@ unsigned int millis (void) int wiringPiSetup (void) { int fd ; + FILE *cpuFd ; + char line [80] ; + char *c ; + int revision = -1 ; uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ; struct timeval tv ; + if (getenv ("WIRINGPI_DEBUG") != NULL) + wiringPiDebug = TRUE ; + + if (wiringPiDebug) + printf ("wiringPiSetup called\n") ; + pinMode = pinModeWPi ; pullUpDnControl = pullUpDnControlWPi ; digitalWrite = digitalWriteWPi ; @@ -814,6 +853,52 @@ int wiringPiSetup (void) pwmSetMode = pwmSetModeWPi ; pwmSetRange = pwmSetRangeWPi ; +// Find board revision + + if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) + { + fprintf (stderr, "wiringPiSetup: Unable to open /proc/cpuinfo: %s\n", strerror (errno)) ; + return -1 ; + } + + while (fgets (line, 80, cpuFd) != NULL) + if (strncmp (line, "Revision", 8) == 0) + for (c = line ; *c ; ++c) + { + if (!isdigit (*c)) + continue ; + revision = atoi (c) ; + break ; + } + + fclose (cpuFd) ; + if (revision == -1) + { + fprintf (stderr, "wiringPiSetup: Unable to determine board revision\n") ; + errno = 0 ; + return -1 ; + } + + /**/ if ((revision == 2) || (revision == 3)) + boardRevision = 1 ; + else if ((revision == 4) || (revision == 5) || (revision == 6)) + boardRevision = 2 ; + else + { + fprintf (stderr, "wiringPiSetup: Unable to determine board revision: %d\n", revision) ; + errno = 0 ; + return -1 ; + } + + + if (boardRevision == 1) + pinToGpio = pinToGpioR1 ; + else + pinToGpio = pinToGpioR2 ; + + if (wiringPiDebug) + printf ("Revision: %d, board revision: %d\n", revision, boardRevision) ; + // Open the master /dev/memory device if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) @@ -954,9 +1039,12 @@ int wiringPiSetup (void) int wiringPiSetupGpio (void) { - int x = wiringPiSetup () ; + int x ; - if (x != 0) + if (wiringPiDebug) + printf ("wiringPiSetupGpio called\n") ; + + if ((x = wiringPiSetup ()) < 0) return x ; pinMode = pinModeGpio ; @@ -989,6 +1077,9 @@ int wiringPiSetupSys (void) struct timeval tv ; char fName [128] ; + if (wiringPiDebug) + printf ("wiringPiSetupSys called\n") ; + pinMode = pinModeSys ; pullUpDnControl = pullUpDnControlSys ; digitalWrite = digitalWriteSys ; From 30d79dab76770a139748c0d8a8280d25241eb4d1 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Wed, 5 Sep 2012 21:01:12 +0100 Subject: [PATCH 19/97] A-Ha! discovered that overvolted Raspberry Pi's have 10000 added to their hardware revision number... So have adapted wiringPi to cope. --- wiringPi/wiringPi.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 2a5c533..b89309d 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -840,7 +840,7 @@ int wiringPiSetup (void) wiringPiDebug = TRUE ; if (wiringPiDebug) - printf ("wiringPiSetup called\n") ; + printf ("wiringPi: wiringPiSetup called\n") ; pinMode = pinModeWPi ; pullUpDnControl = pullUpDnControlWPi ; @@ -879,6 +879,15 @@ int wiringPiSetup (void) return -1 ; } +// If you have overvolted the Pi, then it appears that the revision +// has 100000 added to it! + + if (wiringPiDebug) + if (revision > 1000) + printf ("wiringPi: This Pi has/is overvolted!\n") ; + + revision %= 100 ; + /**/ if ((revision == 2) || (revision == 3)) boardRevision = 1 ; else if ((revision == 4) || (revision == 5) || (revision == 6)) @@ -897,7 +906,7 @@ int wiringPiSetup (void) pinToGpio = pinToGpioR2 ; if (wiringPiDebug) - printf ("Revision: %d, board revision: %d\n", revision, boardRevision) ; + printf ("wiringPi: Revision: %d, board revision: %d\n", revision, boardRevision) ; // Open the master /dev/memory device @@ -1042,7 +1051,7 @@ int wiringPiSetupGpio (void) int x ; if (wiringPiDebug) - printf ("wiringPiSetupGpio called\n") ; + printf ("wiringPi: wiringPiSetupGpio called\n") ; if ((x = wiringPiSetup ()) < 0) return x ; @@ -1078,7 +1087,7 @@ int wiringPiSetupSys (void) char fName [128] ; if (wiringPiDebug) - printf ("wiringPiSetupSys called\n") ; + printf ("wiringPi: wiringPiSetupSys called\n") ; pinMode = pinModeSys ; pullUpDnControl = pullUpDnControlSys ; From 8cb493937f1128fa0a1ed952214b138b557420f7 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 16 Sep 2012 10:15:32 +0100 Subject: [PATCH 20/97] Improved the PWM functions with help from Chris Hall. --- gpio/gpio.c | 29 ++++++++++++-- wiringPi/wiringPi.c | 97 +++++++++++++++++++++++++++++++++------------ wiringPi/wiringPi.h | 1 + 3 files changed, 98 insertions(+), 29 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index b696542..8b15eea 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -52,9 +52,10 @@ char *usage = "Usage: gpio -v\n" " gpio drive \n" " gpio pwm-bal/pwm-ms \n" " gpio pwmr \n" + " gpio pwmc \n" " gpio load spi/i2c\n" " gpio gbr \n" - " gpio gbw \n" ; + " gpio gbw " ; // No trailing newline needed here. /* @@ -687,8 +688,8 @@ void doPwm (int argc, char *argv []) /* - * doPwmMode: doPwmRange: - * Change the PWM mode and Range values + * doPwmMode: doPwmRange: doPwmClock: + * Change the PWM mode, range and clock divider values ********************************************************************************* */ @@ -718,6 +719,27 @@ static void doPwmRange (int argc, char *argv []) pwmSetRange (range) ; } +static void doPwmClock (int argc, char *argv []) +{ + unsigned int clock ; + + if (argc != 3) + { + fprintf (stderr, "Usage: %s pwmc \n", argv [0]) ; + exit (1) ; + } + + clock = (unsigned int)strtoul (argv [2], NULL, 10) ; + + if ((clock < 1) || (clock > 4095)) + { + fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ; + exit (1) ; + } + + pwmSetClock (clock) ; +} + /* * main: @@ -846,6 +868,7 @@ int main (int argc, char *argv []) if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; } if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; } if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; } + if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; } } // Check for wiring commands diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index b89309d..ac9f530 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -2,6 +2,7 @@ * wiringPi: * Arduino compatable (ish) Wiring library for the Raspberry Pi * Copyright (c) 2012 Gordon Henderson + * Additional code for pwmSetClock by Chris Hall * * Thanks to code samples from Gert Jan van Loo and the * BCM2835 ARM Peripherals manual, however it's missing @@ -83,6 +84,7 @@ int (*waitForInterrupt) (int pin, int mS) ; void (*delayMicroseconds) (unsigned int howLong) ; void (*pwmSetMode) (int mode) ; void (*pwmSetRange) (unsigned int range) ; +void (*pwmSetClock) (int divisor) ; #ifndef TRUE @@ -379,7 +381,6 @@ static unsigned long long epoch ; void pinModeGpio (int pin, int mode) { - static int pwmRunning = FALSE ; int fSel, shift, alt ; pin &= 63 ; @@ -400,38 +401,28 @@ void pinModeGpio (int pin, int 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... +// Page 107 of the BCM Peripherals manual talks about the GPIO clocks, +// but I'm assuming (hoping!) that this applies to other clocks too. - if (!pwmRunning) - { + *(pwm + PWM_CONTROL) = 0 ; // Stop PWM + *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock + delayMicroseconds (110) ; // See comments in pwmSetClockWPi - *(pwm + PWM_CONTROL) = 0 ; // Stop PWM - delayMicroseconds (10) ; - -// Gert/Doms Values - *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32<<12) ; // set pwm div to 32 (19.2/32 = 600KHz) - *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Source=osc and enable + (void)*(pwm + PWM_CONTROL) ; + while ((*(pwm + PWM_CONTROL) & 0x80) != 0) // Wait for clock to be !BUSY + delayMicroseconds (1) ; - delayMicroseconds (10) ; + *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32 << 12) ; // set pwm div to 32 (19.2/32 = 600KHz) + *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // enable clk - *(pwm + PWM0_RANGE) = 0x400 ; delayMicroseconds (10) ; - *(pwm + PWM1_RANGE) = 0x400 ; delayMicroseconds (10) ; +// Default range regsiter of 1024 -// Enable PWMs + *(pwm + PWM0_DATA) = 0 ; *(pwm + PWM0_RANGE) = 1024 ; + *(pwm + PWM1_DATA) = 0 ; *(pwm + PWM1_RANGE) = 1024 ; - *(pwm + PWM0_DATA) = 512 ; - *(pwm + PWM1_DATA) = 512 ; - -// Balanced mode (default) - - *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; - - pwmRunning = TRUE ; - } +// Enable PWMs in balanced mode (default) + *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; } // When we change mode of any pin, we remove the pull up/downs @@ -488,6 +479,57 @@ void pwmSetRangeSys (unsigned int range) return ; } +/* + * pwmSetClockWPi: + * Set/Change the PWM clock. Originally my code, but changed + * (for the better!) by Chris Hall, + * after further study of the manual and testing with a 'scope + ********************************************************************************* + */ + +void pwmSetClockWPi (int divisor) +{ + unsigned int pwm_control ; + divisor &= 4095 ; + + if (wiringPiDebug) + printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ; + + pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL + +// We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY +// stays high. + + *(pwm + PWM_CONTROL) = 0 ; // Stop PWM + +// Stop PWM clock before changing divisor. The delay after this does need to +// this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY +// flag is not working properly in balanced mode. Without the delay when DIV is +// adjusted the clock sometimes switches to very slow, once slow further DIV +// adjustments do nothing and it's difficult to get out of this mode. + + *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock + delayMicroseconds (110) ; // prevents clock going sloooow + + while ((*(pwm + PWM_CONTROL) & 0x80) != 0) // Wait for clock to be !BUSY + delayMicroseconds (1) ; + + *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ; + + *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock + *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL + + if (wiringPiDebug) + printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ; +} + +void pwmSetClockSys (int divisor) +{ + return ; +} + + + #ifdef notYetReady /* @@ -852,6 +894,7 @@ int wiringPiSetup (void) delayMicroseconds = delayMicrosecondsWPi ; pwmSetMode = pwmSetModeWPi ; pwmSetRange = pwmSetRangeWPi ; + pwmSetClock = pwmSetClockWPi ; // Find board revision @@ -1066,6 +1109,7 @@ int wiringPiSetupGpio (void) delayMicroseconds = delayMicrosecondsWPi ; // Same pwmSetMode = pwmSetModeWPi ; pwmSetRange = pwmSetRangeWPi ; + pwmSetClock = pwmSetClockWPi ; return 0 ; } @@ -1099,6 +1143,7 @@ int wiringPiSetupSys (void) delayMicroseconds = delayMicrosecondsSys ; pwmSetMode = pwmSetModeSys ; pwmSetRange = pwmSetRangeSys ; + pwmSetClock = pwmSetClockSys ; // Open and scan the directory, looking for exported GPIOs, and pre-open diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 1d21fa0..503c28a 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -73,6 +73,7 @@ extern int (*digitalRead) (int pin) ; extern void (*delayMicroseconds) (unsigned int howLong) ; extern void (*pwmSetMode) (int mode) ; extern void (*pwmSetRange) (unsigned int range) ; +extern void (*pwmSetClock) (int divisor) ; // Interrupts From 538a066c74bcacc21603c6dabdb5639cee7b7999 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 16 Sep 2012 11:10:55 +0100 Subject: [PATCH 21/97] Added in some new functions to return the Pi board revision and allow access to the wiringPi to BCM_GPIO pin mapping tables. --- wiringPi/wiringPi.c | 175 ++++++++++++++++++++++++++------------------ wiringPi/wiringPi.h | 3 + 2 files changed, 108 insertions(+), 70 deletions(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index ac9f530..b572dde 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -170,13 +170,8 @@ static volatile uint32_t *pwm ; static volatile uint32_t *clk ; static volatile uint32_t *pads ; static volatile uint32_t *timer ; - static volatile uint32_t *timerIrqRaw ; -// Raspberry Pi board revision - -static int boardRevision = -1 ; - // Debugging static int wiringPiDebug = FALSE ; @@ -231,12 +226,12 @@ static int pinToGpioR1 [64] = static int pinToGpioR2 [64] = { - 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7 - 2, 3, // I2C - SDA0, SCL0 - 8, 7, // SPI - CE1, CE0 - 10, 9, 11, // SPI - MOSI, MISO, SCLK - 14, 15, // UART - Tx, Rx - 28, 29, 30, 31, // New GPIOs 8 though 11 + 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7 + 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9 + 8, 7, // SPI - CE1, CE0 wpi 10 - 11 + 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14 + 14, 15, // UART - Tx, Rx wpi 15 - 16 + 28, 29, 30, 31, // New GPIOs 8 though 11 wpi 17 - 20 // Padding: @@ -245,6 +240,7 @@ static int pinToGpioR2 [64] = -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63 } ; + // gpioToGPFSEL: // Map a BCM_GPIO pin to it's control port. (GPFSEL 0-5) @@ -258,6 +254,7 @@ static uint8_t gpioToGPFSEL [] = 5,5,5,5,5,5,5,5,5,5, } ; + // gpioToShift // Define the shift up for the 3 bits per pin in each GPFSEL port @@ -270,6 +267,7 @@ static uint8_t gpioToShift [] = 0,3,6,9,12,15,18,21,24,27, } ; + // gpioToGPSET: // (Word) offset to the GPIO Set registers for each GPIO pin @@ -279,6 +277,7 @@ static uint8_t gpioToGPSET [] = 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, } ; + // gpioToGPCLR: // (Word) offset to the GPIO Clear registers for each GPIO pin @@ -288,6 +287,7 @@ static uint8_t gpioToGPCLR [] = 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, } ; + // gpioToGPLEV: // (Word) offset to the GPIO Input level registers for each GPIO pin @@ -297,6 +297,7 @@ 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 @@ -326,6 +327,7 @@ static uint8_t gpioToFEN [] = } ; #endif + // gpioToPUDCLK // (Word) offset to the Pull Up Down Clock regsiter @@ -337,6 +339,7 @@ static uint8_t gpioToPUDCLK [] = 39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39, } ; + // gpioToPwmALT // the ALT value to put a GPIO pin into PWM mode @@ -370,7 +373,93 @@ static uint8_t gpioToPwmPort [] = static unsigned long long epoch ; -////////////////////////////////////////////////////////////////////////////////// +/* + * Functions + ********************************************************************************* + */ + + +/* + * wpiPinToGpio: + * Translate a wiringPi Pin number to native GPIO pin number. + * (We don't use this here, prefering to just do the lookup directly, + * but it's been requested!) + ********************************************************************************* + */ + +int wpiPinToGpio (int wpiPin) +{ + return pinToGpio [wpiPin & 63] ; +} + + +/* + * piBoardRev: + * Return a number representing the hardware revision of the board. + * Revision is currently 1 or 2. -1 is returned on error. + ********************************************************************************* + */ + +int piBoardRev (void) +{ + FILE *cpuFd ; + char line [80] ; + char *c ; + int r = -1 ; + static int boardRev = -1 ; + +// No point checking twice... + + if (boardRev != -1) + return boardRev ; + + if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) + return -1 ; + + while (fgets (line, 80, cpuFd) != NULL) + if (strncmp (line, "Revision", 8) == 0) + for (c = line ; *c ; ++c) + { + if (!isdigit (*c)) + continue ; + r = atoi (c) ; + break ; + } + + fclose (cpuFd) ; + if (r == -1) + { + fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; + errno = 0 ; + return -1 ; + } + +// If you have overvolted the Pi, then it appears that the revision +// has 100000 added to it! + + if (wiringPiDebug) + if (r > 1000) + printf ("piboardRev: This Pi has/is overvolted!\n") ; + + r %= 100 ; + + /**/ if ((r == 2) || (r == 3)) + boardRev = 1 ; + else if ((r == 4) || (r == 5) || (r == 6)) + boardRev = 2 ; + else + { + fprintf (stderr, "piBoardRev: Unable to determine board revision from %d\n", r) ; + errno = 0 ; + return -1 ; + } + + if (wiringPiDebug) + printf ("piboardRev: Revision: %d, board revision: %d\n", r, boardRev) ; + + return boardRev ; +} + /* @@ -529,8 +618,6 @@ void pwmSetClockSys (int divisor) } - - #ifdef notYetReady /* * pinED01: @@ -589,7 +676,7 @@ void digitalWriteSys (int pin, int value) /* - * pwnWrite: + * pwmWrite: * Set an output PWM value ********************************************************************************* */ @@ -770,8 +857,6 @@ int waitForInterruptGpio (int pin, int mS) } - - /* * delay: * Wait for some number of milli seconds @@ -871,10 +956,7 @@ unsigned int millis (void) int wiringPiSetup (void) { int fd ; - FILE *cpuFd ; - char line [80] ; - char *c ; - int revision = -1 ; + int boardRev ; uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ; struct timeval tv ; @@ -896,61 +978,14 @@ int wiringPiSetup (void) pwmSetRange = pwmSetRangeWPi ; pwmSetClock = pwmSetClockWPi ; -// Find board revision - - if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) - { - fprintf (stderr, "wiringPiSetup: Unable to open /proc/cpuinfo: %s\n", strerror (errno)) ; + if ((boardRev = piBoardRev ()) < 0) return -1 ; - } - while (fgets (line, 80, cpuFd) != NULL) - if (strncmp (line, "Revision", 8) == 0) - for (c = line ; *c ; ++c) - { - if (!isdigit (*c)) - continue ; - revision = atoi (c) ; - break ; - } - - fclose (cpuFd) ; - if (revision == -1) - { - fprintf (stderr, "wiringPiSetup: Unable to determine board revision\n") ; - errno = 0 ; - return -1 ; - } - -// If you have overvolted the Pi, then it appears that the revision -// has 100000 added to it! - - if (wiringPiDebug) - if (revision > 1000) - printf ("wiringPi: This Pi has/is overvolted!\n") ; - - revision %= 100 ; - - /**/ if ((revision == 2) || (revision == 3)) - boardRevision = 1 ; - else if ((revision == 4) || (revision == 5) || (revision == 6)) - boardRevision = 2 ; - else - { - fprintf (stderr, "wiringPiSetup: Unable to determine board revision: %d\n", revision) ; - errno = 0 ; - return -1 ; - } - - - if (boardRevision == 1) + if (boardRev == 1) pinToGpio = pinToGpioR1 ; else pinToGpio = pinToGpioR2 ; - if (wiringPiDebug) - printf ("wiringPi: Revision: %d, board revision: %d\n", revision, boardRevision) ; - // Open the master /dev/memory device if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 503c28a..cab3080 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -62,6 +62,9 @@ extern int wiringPiSetupSys (void) ; extern int wiringPiSetupGpio (void) ; extern int wiringPiSetupPiFace (void) ; +extern int piBoardRev (void) ; +extern int wpiPinToGpio (int wpiPin) ; + extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only extern void (*pinMode) (int pin, int mode) ; From db2edcc9c530eb37414bb6c0d63e7be3c3afc712 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 2 Oct 2012 14:26:15 +0100 Subject: [PATCH 22/97] Updated examples makefile to not actually make any code now. Fixed timing in the LCD library to work with new dealyMicroseconds Minor typos and bigfixes to the gpio command. --- examples/README.TXT | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 examples/README.TXT diff --git a/examples/README.TXT b/examples/README.TXT new file mode 100644 index 0000000..2bf6f1e --- /dev/null +++ b/examples/README.TXT @@ -0,0 +1,14 @@ + +wiringPi Examples +================= + +There are now too many examples to compile them all in a sensible time, +and you probably don't want to compile or run them all anyway, so they +have been separated out. + +To compile an individual example, just type + + make exampleName + +Where exampleName is one of: + From 86a5c68e087ecc7daa7d511968d9c7843a4910eb Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 2 Oct 2012 14:32:12 +0100 Subject: [PATCH 23/97] Updating gpio manual page --- examples/Makefile | 8 +++-- gpio/Makefile | 15 +++++---- gpio/gpio.1 | 52 ++++++++++++++++--------------- gpio/gpio.c | 20 ++++++------ wiringPi/Makefile | 75 +++++++++++++++++++++++++++------------------ wiringPi/lcd.c | 5 ++- wiringPi/lcd.h | 8 ++--- wiringPi/wiringPi.c | 9 ++++-- 8 files changed, 114 insertions(+), 78 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index 450e0dc..16bf7e2 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -39,7 +39,12 @@ SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c gertboard.c nes.c delayTest.c OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o gertboard.o nes.o delayTest.o softPwm.o -all: test1 test2 speed lcd wfi piface gertboard nes softPwm +BINS = test1 test2 speed lcd wfi piface gertboard nes delayTest softPwm + +all: + @cat README.TXT + @echo " $(BINS)" | fmt + @echo "" test1: test1.o @echo [link] @@ -77,7 +82,6 @@ softPwm: softPwm.o @echo [link] $(CC) -o $@ softPwm.o $(LDFLAGS) $(LIBS) -lm -lpthread - delayTest: delayTest.o @echo [link] $(CC) -o $@ delayTest.o $(LDFLAGS) $(LIBS) diff --git a/gpio/Makefile b/gpio/Makefile index a92dd5f..cd56e5e 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -41,7 +41,7 @@ OBJ = gpio.o all: gpio -gpio: gpio.o /usr/local/lib/libwiringPi.a +gpio: gpio.o /usr/local/lib/libwiringPi.a /usr/local/lib/libwiringPi.so.1.0 @echo [LD] @$(CC) -o $@ gpio.o $(LDFLAGS) $(LIBS) @@ -60,11 +60,14 @@ depend: makedepend -Y $(SRC) install: - cp gpio /usr/local/bin - chown root.root /usr/local/bin/gpio - chmod 4755 /usr/local/bin/gpio - mkdir -p /usr/local/man/man1 - cp gpio.1 /usr/local/man/man1 + @echo -n "Installing... " + @cp gpio /usr/local/bin + @chown root.root /usr/local/bin/gpio + @chmod 4755 /usr/local/bin/gpio + @mkdir -p /usr/local/man/man1 + @cp gpio.1 /usr/local/man/man1 + @echo "Done." + uninstall: rm -f /usr/local/bin/gpio diff --git a/gpio/gpio.1 b/gpio/gpio.1 index bc8e36e..ba0183f 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -48,9 +48,9 @@ channel value .SH DESCRIPTION .B GPIO -is a swiss army knofe of a command line tool to allow the user easy +is a swiss army knife of a command line tool to allow the user easy access to the GPIO pins on the Raspberry Pi and the SPI A/D and D/A -convertors on the Gertboard. It's designed for simple testing and +converters on the Gertboard. 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. @@ -122,7 +122,7 @@ the direction to input and set the edge interrupt method to \fInone\fR, 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 +Like the export commands above, ownership is set to that of the calling user, allowing subsequent access from user programs without requiring root/sudo. @@ -155,7 +155,7 @@ the associated /dev/ entries so that the current user has access to them. .B gbr channel -This reads the analog to digital convertor on the Gertboard on the given +This reads the analog to digital converter on the Gertboard on the given channel. The board jumpers need to be in-place to do this operation. .TP @@ -163,7 +163,7 @@ channel. The board jumpers need to be in-place to do this operation. channel value This writes the supplied value to the output channel on the Gertboards -SPI digital to analogue convertor. +SPI digital to analogue converter. The board jumpers need to be in-place to do this operation. @@ -171,26 +171,30 @@ The board jumpers need to be in-place to do this operation. .PP .TS -r r l. -WiringPi GPIO Function +r r r l. +WiringPi GPIO-r1 GPIO-r2 Function _ -0 17 -1 18 (PWM) -2 21 -3 22 -4 23 -5 24 -6 25 -7 4 -8 0 SDA0 -9 1 SCL0 -10 8 SPI CE0 -11 7 SPI CE1 -12 10 SPI MOSI -13 9 SPI MISO -14 11 SPI SCLK -15 14 TxD -16 15 RxD +0 17 17 +1 18 18 (PWM) +2 21 27 +3 22 22 +4 23 23 +5 24 24 +6 25 25 +7 4 4 +8 0 2 I2C: SDA0 +9 1 3 I2C: SCL0 +10 8 8 SPI: CE0 +11 7 7 SPI: CE1 +12 10 10 SPI: MOSI +13 9 9 SPI: MISO +14 11 11 SPI: SCLK +15 14 14 TxD +16 15 16 RxD +17 - 28 +18 - 29 +19 - 30 +20 - 31 .TE .SH FILES diff --git a/gpio/gpio.c b/gpio/gpio.c index 8b15eea..b3534c8 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -40,7 +40,7 @@ # define FALSE (1==2) #endif -#define VERSION "1.2" +#define VERSION "1.3" static int wpMode ; @@ -753,7 +753,7 @@ int main (int argc, char *argv []) if (argc == 1) { - fprintf (stderr, "%s: %s\n", argv [0], usage) ; + fprintf (stderr, "%s\n", usage) ; return 1 ; } @@ -769,6 +769,8 @@ int main (int argc, char *argv []) printf ("Copyright (c) 2012 Gordon Henderson\n") ; printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ; printf ("For details type: %s -warranty\n", argv [0]) ; + printf ("\n") ; + printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ; return 0 ; } @@ -807,9 +809,8 @@ int main (int argc, char *argv []) else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; } else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; } -// Check for drive or load commands: +// Check for load command: - if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; } if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; } // Gertboard commands @@ -861,14 +862,15 @@ int main (int argc, char *argv []) wpMode = WPI_MODE_PINS ; } -// Check for PWM operations +// Check for PWM or Pad Drive operations if (wpMode != WPI_MODE_PIFACE) { - if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; } - if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; } - if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; } - if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; } + if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; } + if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; } + if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; } + if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; } + if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; } } // Check for wiring commands diff --git a/wiringPi/Makefile b/wiringPi/Makefile index dc189cd..2d7967f 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -21,14 +21,17 @@ # along with wiringPi. If not, see . ################################################################################# +DYN_VERS_MAJ=1 +DYN_VERS_MIN=0 -TARGET=libwiringPi.a +STATIC=libwiringPi.a +DYNAMIC=libwiringPi.so.$(DYN_VERS_MAJ).$(DYN_VERS_MIN) #DEBUG = -g -O0 -DEBUG = -O3 +DEBUG = -O2 CC = gcc INCLUDE = -I. -CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe -fPIC LIBS = @@ -45,20 +48,25 @@ OBJ = wiringPi.o wiringPiFace.o wiringSerial.o wiringShift.o \ piNes.o \ lcd.o piHiPri.o piThread.o softPwm.o wiringPiSPI.o -all: $(TARGET) +all: $(STATIC) $(DYNAMIC) -$(TARGET): $(OBJ) - @echo [AR] $(OBJ) - @ar rcs $(TARGET) $(OBJ) - @ranlib $(TARGET) - @size $(TARGET) +$(STATIC): $(OBJ) + @echo [STATIC] + @ar rcs $(STATIC) $(OBJ) + @ranlib $(STATIC) + +# @size $(STATIC) + +$(DYNAMIC): $(OBJ) + @echo [DYNAMIC] + @gcc -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ) .c.o: @echo [CC] $< @$(CC) -c $(CFLAGS) $< -o $@ clean: - rm -f $(OBJ) $(TARGET) *~ core tags Makefile.bak + rm -f $(OBJ) *~ core tags Makefile.bak libwiringPi.* tags: $(SRC) @echo [ctags] @@ -69,27 +77,34 @@ depend: 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 wiringSerial.h /usr/local/include - install -m 0644 wiringShift.h /usr/local/include - install -m 0644 gertboard.h /usr/local/include - install -m 0644 piNes.h /usr/local/include - install -m 0644 softPwm.h /usr/local/include - install -m 0644 lcd.h /usr/local/include - install -m 0644 wiringPiSPI.h /usr/local/include - install -m 0644 libwiringPi.a /usr/local/lib + @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 wiringSerial.h /usr/local/include + @install -m 0644 wiringShift.h /usr/local/include + @install -m 0644 gertboard.h /usr/local/include + @install -m 0644 piNes.h /usr/local/include + @install -m 0644 softPwm.h /usr/local/include + @install -m 0644 lcd.h /usr/local/include + @install -m 0644 wiringPiSPI.h /usr/local/include + @install -m 0644 libwiringPi.a /usr/local/lib + @install -m 0755 libwiringPi.so.1.0 /usr/local/lib + @ln -sf /usr/local/lib/libwiringPi.so.1.0 /usr/local/lib/libwiringPi.so + @ln -sf /usr/local/lib/libwiringPi.so.1.0 /usr/local/lib/libwiringPi.so.1 + @ldconfig uninstall: @echo [uninstall] - rm -f /usr/local/include/lcd.h - rm -f /usr/local/include/lpiNes.h - rm -f /usr/local/include/gertboard.h - rm -f /usr/local/include/wiringShift.h - rm -f /usr/local/include/wiringSerial.h - rm -f /usr/local/include/wiringPi.h - rm -f /usr/local/lib/libwiringPi.a + @rm -f /usr/local/include/wiringPi.h + @rm -f /usr/local/include/wiringSerial.h + @rm -f /usr/local/include/wiringShift.h + @rm -f /usr/local/include/gertboard.h + @rm -f /usr/local/include/piNes.h + @rm -f /usr/local/include/softPwm.h + @rm -f /usr/local/include/lcd.h + @rm -f /usr/local/include/wiringPiSPI.h + @rm -f /usr/local/lib/libwiringPi.* + @ldconfig # DO NOT DELETE @@ -98,8 +113,10 @@ wiringPi.o: wiringPi.h wiringPiFace.o: wiringPi.h wiringSerial.o: wiringSerial.h wiringShift.o: wiringPi.h wiringShift.h -gertboard.o: gertboard.h +gertboard.o: wiringPiSPI.h gertboard.h piNes.o: wiringPi.h piNes.h lcd.o: wiringPi.h lcd.h piHiPri.o: wiringPi.h piThread.o: wiringPi.h +softPwm.o: wiringPi.h softPwm.h +wiringPiSPI.o: wiringPiSPI.h diff --git a/wiringPi/lcd.c b/wiringPi/lcd.c index 6826a60..aa58cab 100644 --- a/wiringPi/lcd.c +++ b/wiringPi/lcd.c @@ -75,7 +75,10 @@ struct lcdDataStruct *lcds [MAX_LCDS] ; static void strobe (struct lcdDataStruct *lcd) { - digitalWrite (lcd->strbPin, 1) ; delayMicroseconds (1) ; + +// Note timing changes for new version of delayMicroseconds () + + digitalWrite (lcd->strbPin, 1) ; delayMicroseconds (50) ; digitalWrite (lcd->strbPin, 0) ; delayMicroseconds (50) ; } diff --git a/wiringPi/lcd.h b/wiringPi/lcd.h index d4c724c..ecd1d25 100644 --- a/wiringPi/lcd.h +++ b/wiringPi/lcd.h @@ -26,6 +26,10 @@ #define MAX_LCDS 8 +#ifdef __cplusplus +extern "C" { +#endif + extern void lcdHome (int fd) ; extern void lcdClear (int fd) ; extern void lcdPosition (int fd, int x, int y) ; @@ -33,10 +37,6 @@ extern void lcdPutchar (int fd, uint8_t data) ; extern void lcdPuts (int fd, char *string) ; extern void lcdPrintf (int fd, char *message, ...) ; -#ifdef __cplusplus -extern "C" { -#endif - extern int lcdInit (int rows, int cols, int bits, int rs, int strb, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index b572dde..ad62082 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -427,6 +427,7 @@ int piBoardRev (void) } fclose (cpuFd) ; + if (r == -1) { fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; @@ -449,9 +450,11 @@ int piBoardRev (void) boardRev = 2 ; else { - fprintf (stderr, "piBoardRev: Unable to determine board revision from %d\n", r) ; - errno = 0 ; - return -1 ; + fprintf (stderr, "WARNING: wiringPi: Unable to determine board revision from \"%d\"\n", r) ; + fprintf (stderr, " -> You may want to check:\n") ; + fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ; + fprintf (stderr, " -> Assuming a Rev 1 board\n") ; + boardRev = 1 ; } if (wiringPiDebug) From 183c5a6b5c16f15b1da7812744f58d19a5446bf6 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 21 Oct 2012 15:25:16 +0100 Subject: [PATCH 24/97] More changes to help reflect usage on Rev 2 / 512MB Raspberry Pi's. Also Makefile tweaks to help improve things somewhat - decided to read the Makefile manual after some 15 years to updated my make grey cells somewhat. New command in the gpio command - readall --- People | 10 ++++++ build | 16 ++++++++++ gpio/Makefile | 30 ++++++++++-------- gpio/gpio.1 | 27 ++++++++++++----- gpio/gpio.c | 74 +++++++++++++++++++++++++++++++++++++++++---- gpio/test.sh | 21 +++++++++++++ wiringPi/Makefile | 36 +++++++++++----------- wiringPi/wiringPi.c | 60 +++++++++++++++++++++++++----------- 8 files changed, 214 insertions(+), 60 deletions(-) create mode 100644 People diff --git a/People b/People new file mode 100644 index 0000000..2c5fa92 --- /dev/null +++ b/People @@ -0,0 +1,10 @@ + +Just a quick note to some people who've provided help, suggestions, +bug-fixes, etc. along the way... + +Nick Lott: (And others) + Hints about making it work with C++ + +Philipp Stefan Neininger: + Minor bug in the Makefile to do with cross compiling + diff --git a/build b/build index 9df047f..740b512 100755 --- a/build +++ b/build @@ -10,15 +10,31 @@ if [ x$1 = "xclean" ]; then cd ../examples make clean cd .. +elif [ x$1 = "xuninstall" ]; then + echo Uninstalling + echo + echo "WiringPi library" + cd wiringPi + sudo make uninstall + echo + echo "GPIO Utility" + cd ../gpio + sudo make uninstall + cd .. else echo wiringPi Build script - please wait... echo + echo "WiringPi library" cd wiringPi make sudo make install + echo + echo "GPIO Utility" cd ../gpio make sudo make install + echo + echo "Examples" cd ../examples make cd .. diff --git a/gpio/Makefile b/gpio/Makefile index cd56e5e..5693c44 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -24,7 +24,7 @@ #DEBUG = -g -O0 -DEBUG = -O3 +DEBUG = -O2 CC = gcc INCLUDE = -I/usr/local/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe @@ -32,45 +32,49 @@ CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L/usr/local/lib LIBS = -lwiringPi -# Should not alter anything below this line +# May not need to alter anything below this line ############################################################################### SRC = gpio.c -OBJ = gpio.o +OBJ = $(SRC:.c=.o) all: gpio -gpio: gpio.o /usr/local/lib/libwiringPi.a /usr/local/lib/libwiringPi.so.1.0 - @echo [LD] +gpio: gpio.o + @echo [Link] @$(CC) -o $@ gpio.o $(LDFLAGS) $(LIBS) .c.o: - @echo [CC] $< + @echo [Compile] $< @$(CC) -c $(CFLAGS) $< -o $@ +.PHONEY: clean clean: - rm -f $(OBJ) gpio *~ core tags + rm -f $(OBJ) gpio *~ core tags *.bak +.PHONEY: tags tags: $(SRC) @echo [ctags] @ctags $(SRC) -depend: - makedepend -Y $(SRC) - +.PHONEY: install install: - @echo -n "Installing... " + @echo "[Install]" @cp gpio /usr/local/bin @chown root.root /usr/local/bin/gpio @chmod 4755 /usr/local/bin/gpio @mkdir -p /usr/local/man/man1 @cp gpio.1 /usr/local/man/man1 - @echo "Done." - +.PHONEY: uninstall uninstall: + @echo "[UnInstall]" rm -f /usr/local/bin/gpio rm -f /usr/local/man/man1/gpio.1 +.PHONEY: depend +depend: + makedepend -Y $(SRC) + # DO NOT DELETE diff --git a/gpio/gpio.1 b/gpio/gpio.1 index ba0183f..be38791 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -1,4 +1,4 @@ -.TH "GPIO" "14 June 2012" "Command-Line access to Raspberry Pi and PiFace GPIO" +.TH "GPIO" "21st October 2012" "Command-Line access to Raspberry Pi and PiFace GPIO" .SH NAME gpio \- Command-line access to Raspberry Pi and PiFace GPIO @@ -17,6 +17,9 @@ gpio \- Command-line access to Raspberry Pi and PiFace GPIO .B ... .PP .B gpio +.B readall +.PP +.B gpio .B unexportall/exports .PP .B gpio @@ -62,7 +65,7 @@ interface without needing to be run as root. .TP .B \-v -Output the current version +Output the current version including the board revision of the Raspberry Pi. .TP .B \-g @@ -73,20 +76,26 @@ Use the BCM_GPIO pins numbers rather than wiringPi pin numbers. Use the PiFace interface board and its corresponding pin numbers. .TP -.B read +.B read Read the digital value of the given pin and print 0 or 1 to represent the respective logic levels. .TP -.B write +.B write Write the given value (0 or 1) to the pin. .TP -.B pwm +.B readall +Output a table of all GPIO pins values. The values represent the actual values read +if the pin is in input mode, or the last value written if the pin is in output +mode. + +.TP +.B pwm Write a PWM value (0-1023) to the given pin. .TP -.B mode +.B mode 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. @@ -238,10 +247,14 @@ Gordon Henderson .SH "REPORTING BUGS" -Report bugs to +Please report bugs to .SH COPYRIGHT Copyright (c) 2012 Gordon Henderson This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +.SH TRADEMARKS AND ACKNOWLEDGEMENTS + +Raspberry Pi is a trademark of the Raspberry Pi Foundation. diff --git a/gpio/gpio.c b/gpio/gpio.c index b3534c8..f019c1f 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -40,7 +40,7 @@ # define FALSE (1==2) #endif -#define VERSION "1.3" +#define VERSION "1.4" static int wpMode ; @@ -48,7 +48,9 @@ char *usage = "Usage: gpio -v\n" " gpio -h\n" " gpio [-g] ...\n" " gpio [-p] ...\n" - " gpio export/edge/unexport/unexportall/exports ...\n" + " gpio readall\n" + " gpio unexportall/exports ...\n" + " gpio export/edge/unexport ...\n" " gpio drive \n" " gpio pwm-bal/pwm-ms \n" " gpio pwmr \n" @@ -172,6 +174,65 @@ static void doLoad (int argc, char *argv []) } +/* + * doReadall: + * Read all the GPIO pins + ********************************************************************************* + */ + +static char *pinNames [] = +{ + "GPIO 0", + "GPIO 1", + "GPIO 2", + "GPIO 3", + "GPIO 4", + "GPIO 5", + "GPIO 6", + "GPIO 7", + "SDA ", + "SCL ", + "CE0 ", + "CE1 ", + "MOSI ", + "MISO ", + "SCLK ", + "TxD ", + "RxD ", + "GPIO 8", + "GPIO 9", + "GPIO10", + "GPIO11", +} ; + +static void doReadall (void) +{ + int pin ; + + printf ("+----------+------+--------+-------+\n") ; + printf ("| wiringPi | GPIO | Name | Value |\n") ; + printf ("+----------+------+--------+-------+\n") ; + + for (pin = 0 ; pin < NUM_PINS ; ++pin) + printf ("| %6d | %3d | %s | %s |\n", + pin, wpiPinToGpio (pin), + pinNames [pin], + digitalRead (pin) == HIGH ? "High" : "Low ") ; + + printf ("+----------+------+--------+-------+\n") ; + + if (piBoardRev () == 1) + return ; + + for (pin = 17 ; pin <= 20 ; ++pin) + printf ("| %6d | %3d | %s | %s |\n", + pin, wpiPinToGpio (pin), + pinNames [pin], + digitalRead (pin) == HIGH ? "High" : "Low ") ; + + printf ("+----------+------+--------+-------+\n") ; +} + /* * doExports: @@ -875,10 +936,11 @@ int main (int argc, char *argv []) // Check for wiring commands - /**/ if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ; - else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ; - else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ; - else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ; + /**/ if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; + else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ; + else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ; + else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ; + else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ; else { fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ; diff --git a/gpio/test.sh b/gpio/test.sh index 7127528..a7c2bb2 100755 --- a/gpio/test.sh +++ b/gpio/test.sh @@ -1,4 +1,25 @@ #!/bin/bash +# +# test.sh: +# Simple test: Assumes LEDs on Pins 0-7 and lights them +# in-turn. +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# # Simple test - assumes LEDs on Pins 0-7. diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 2d7967f..c0a39f9 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -43,40 +43,37 @@ SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ piNes.c \ lcd.c piHiPri.c piThread.c softPwm.c wiringPiSPI.c -OBJ = wiringPi.o wiringPiFace.o wiringSerial.o wiringShift.o \ - gertboard.o \ - piNes.o \ - lcd.o piHiPri.o piThread.o softPwm.o wiringPiSPI.o +OBJ = $(SRC:.c=.o) -all: $(STATIC) $(DYNAMIC) +#all: $(STATIC) $(DYNAMIC) +all: $(DYNAMIC) $(STATIC): $(OBJ) - @echo [STATIC] + @echo [Link (Static)] @ar rcs $(STATIC) $(OBJ) @ranlib $(STATIC) - -# @size $(STATIC) + @size $(STATIC) $(DYNAMIC): $(OBJ) - @echo [DYNAMIC] - @gcc -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ) + @echo [Link] + @$(CC) -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ) .c.o: - @echo [CC] $< + @echo [Compile] $< @$(CC) -c $(CFLAGS) $< -o $@ +.PHONEY: clean clean: rm -f $(OBJ) *~ core tags Makefile.bak libwiringPi.* +.PHONEY: tags tags: $(SRC) @echo [ctags] @ctags $(SRC) -depend: - makedepend -Y $(SRC) - +.PHONEY: install install: $(TARGET) - @echo [install] + @echo "[Install]" @install -m 0755 -d /usr/local/lib @install -m 0755 -d /usr/local/include @install -m 0644 wiringPi.h /usr/local/include @@ -87,14 +84,15 @@ install: $(TARGET) @install -m 0644 softPwm.h /usr/local/include @install -m 0644 lcd.h /usr/local/include @install -m 0644 wiringPiSPI.h /usr/local/include - @install -m 0644 libwiringPi.a /usr/local/lib +# @install -m 0644 libwiringPi.a /usr/local/lib @install -m 0755 libwiringPi.so.1.0 /usr/local/lib @ln -sf /usr/local/lib/libwiringPi.so.1.0 /usr/local/lib/libwiringPi.so @ln -sf /usr/local/lib/libwiringPi.so.1.0 /usr/local/lib/libwiringPi.so.1 @ldconfig +.PHONEY: uninstall uninstall: - @echo [uninstall] + @echo "[UnInstall]" @rm -f /usr/local/include/wiringPi.h @rm -f /usr/local/include/wiringSerial.h @rm -f /usr/local/include/wiringShift.h @@ -107,6 +105,10 @@ uninstall: @ldconfig +.PHONEY: depend +depend: + makedepend -Y $(SRC) + # DO NOT DELETE wiringPi.o: wiringPi.h diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index ad62082..9fe3ab1 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -397,15 +397,31 @@ int wpiPinToGpio (int wpiPin) * piBoardRev: * Return a number representing the hardware revision of the board. * Revision is currently 1 or 2. -1 is returned on error. + * + * Much confusion here )-: + * Seems there ar esome boards with 0000 in them (mistake in manufacture) + * and some board with 0005 in them (another mistake in manufacture). + * So the distinction between boards that I can see is: + * 0000 - Error + * 0001 - Not used + * 0002 - Rev 1 + * 0003 - Rev 1 + * 0004 - Rev 2 + * 0005 - Rev 2 + * 0006 - Rev 2 + * 000f - Rev 2 + 512MB + * + * A small thorn is the olde style overvolting - that will add in + * 1000000 + * ********************************************************************************* */ int piBoardRev (void) { FILE *cpuFd ; - char line [80] ; - char *c ; - int r = -1 ; + char line [120] ; + char *c, lastChar ; static int boardRev = -1 ; // No point checking twice... @@ -416,21 +432,28 @@ int piBoardRev (void) if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) return -1 ; - while (fgets (line, 80, cpuFd) != NULL) + while (fgets (line, 120, cpuFd) != NULL) if (strncmp (line, "Revision", 8) == 0) - for (c = line ; *c ; ++c) - { - if (!isdigit (*c)) - continue ; - r = atoi (c) ; - break ; - } + break ; fclose (cpuFd) ; - if (r == -1) + if (line == NULL) { fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; + fprintf (stderr, " (No \"Revision\" line)\n") ; + errno = 0 ; + return -1 ; + } + + for (c = line ; *c ; ++c) + if (isdigit (*c)) + break ; + + if (!isdigit (*c)) + { + fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; + fprintf (stderr, " (No numeric revision string in: \"%s\"\n", line) ; errno = 0 ; return -1 ; } @@ -439,15 +462,17 @@ int piBoardRev (void) // has 100000 added to it! if (wiringPiDebug) - if (r > 1000) + if (strlen (c) != 4) printf ("piboardRev: This Pi has/is overvolted!\n") ; - r %= 100 ; + lastChar = c [strlen (c) - 2] ; - /**/ if ((r == 2) || (r == 3)) + /**/ if ((lastChar == '2') || (lastChar == '3')) boardRev = 1 ; - else if ((r == 4) || (r == 5) || (r == 6)) + else boardRev = 2 ; + +#ifdef DO_WE_CARE_ABOUT_THIS_NOW else { fprintf (stderr, "WARNING: wiringPi: Unable to determine board revision from \"%d\"\n", r) ; @@ -456,9 +481,10 @@ int piBoardRev (void) fprintf (stderr, " -> Assuming a Rev 1 board\n") ; boardRev = 1 ; } +#endif if (wiringPiDebug) - printf ("piboardRev: Revision: %d, board revision: %d\n", r, boardRev) ; + printf ("piboardRev: Revision string: %s, board revision: %d\n", c, boardRev) ; return boardRev ; } From 25e4ec570b0a1915a38792eb2b9d75d295152c3d Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 6 Dec 2012 21:49:41 +0000 Subject: [PATCH 25/97] Big update here. delayMicrosecondsHard re-written - again. Added a serialRead example program, and added in the okLed to the examples too. Updated/checked some of the GPIO/PWM code. Added in some experimental servo and tone generating code and and example or 2. Tweaks to the gpio command to correctly load the I2C modules too. --- People | 5 + examples/Makefile | 51 ++++++--- examples/delayTest.c | 87 +++++++++------ examples/okLed.c | 65 +++++++++++ examples/{softPwm.c => pwm.c} | 0 examples/serialRead.c | 31 ++++++ examples/servo.c | 33 ++++++ examples/tone.c | 37 +++++++ gpio/gpio.1 | 13 ++- gpio/gpio.c | 69 +++++++++--- wiringPi/Makefile | 69 +++++++----- wiringPi/softServo.c | 202 ++++++++++++++++++++++++++++++++++ wiringPi/softServo.h | 35 ++++++ wiringPi/softTone.c | 119 ++++++++++++++++++++ wiringPi/softTone.h | 38 +++++++ wiringPi/wiringPi.c | 117 +++++++++++++++++--- wiringPi/wiringPi.h | 1 + wiringPi/wiringPiFace.c | 27 +++-- wiringPi/wiringPiSPI.c | 2 +- 19 files changed, 874 insertions(+), 127 deletions(-) create mode 100644 examples/okLed.c rename examples/{softPwm.c => pwm.c} (100%) create mode 100644 examples/serialRead.c create mode 100644 examples/servo.c create mode 100644 examples/tone.c create mode 100644 wiringPi/softServo.c create mode 100644 wiringPi/softServo.h create mode 100644 wiringPi/softTone.c create mode 100644 wiringPi/softTone.h diff --git a/People b/People index 2c5fa92..e0c262c 100644 --- a/People +++ b/People @@ -8,3 +8,8 @@ Nick Lott: (And others) Philipp Stefan Neininger: Minor bug in the Makefile to do with cross compiling +Chris McSweeny + Hints and tips about the use of arithmetic in gettimeofday() + inside the dealyMicrosecondsHard() function. + And spotting a couple of schoolboy errors in the (experimental) + softServo code, prompting me to completely re-write it. diff --git a/examples/Makefile b/examples/Makefile index 16bf7e2..738d36c 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -30,16 +30,19 @@ INCLUDE = -I/usr/local/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L/usr/local/lib -LIBS = -lwiringPi +LDLIBS = -lwiringPi # Should not alter anything below this line ############################################################################### -SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c gertboard.c nes.c delayTest.c softPwm.c +SRC = test1.c test2.c speed.c lcd.c wfi.c \ + piface.c gertboard.c nes.c \ + pwm.c tone.c servo.c \ + delayTest.c serialRead.c okLed.c -OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o gertboard.o nes.o delayTest.o softPwm.o +OBJ = $(SRC:.c=.o) -BINS = test1 test2 speed lcd wfi piface gertboard nes delayTest softPwm +BINS = $(SRC:.c=) all: @cat README.TXT @@ -48,43 +51,59 @@ all: test1: test1.o @echo [link] - $(CC) -o $@ test1.o $(LDFLAGS) $(LIBS) + @$(CC) -o $@ test1.o $(LDFLAGS) $(LDLIBS) test2: test2.o @echo [link] - $(CC) -o $@ test2.o $(LDFLAGS) $(LIBS) + @$(CC) -o $@ test2.o $(LDFLAGS) $(LDLIBS) speed: speed.o @echo [link] - $(CC) -o $@ speed.o $(LDFLAGS) $(LIBS) + @$(CC) -o $@ speed.o $(LDFLAGS) $(LDLIBS) lcd: lcd.o @echo [link] - $(CC) -o $@ lcd.o $(LDFLAGS) $(LIBS) + @$(CC) -o $@ lcd.o $(LDFLAGS) $(LDLIBS) wfi: wfi.o @echo [link] - $(CC) -o $@ wfi.o $(LDFLAGS) $(LIBS) -lpthread + @$(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS) piface: piface.o @echo [link] - $(CC) -o $@ piface.o $(LDFLAGS) $(LIBS) -lpthread + @$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS) -lpthread gertboard: gertboard.o @echo [link] - $(CC) -o $@ gertboard.o $(LDFLAGS) $(LIBS) -lm + @$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS) -lm nes: nes.o @echo [link] - $(CC) -o $@ nes.o $(LDFLAGS) $(LIBS) -lm + @$(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS) -lm -softPwm: softPwm.o +pwm: pwm.o @echo [link] - $(CC) -o $@ softPwm.o $(LDFLAGS) $(LIBS) -lm -lpthread + @$(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS) -lm -lpthread delayTest: delayTest.o @echo [link] - $(CC) -o $@ delayTest.o $(LDFLAGS) $(LIBS) + @$(CC) -o $@ delayTest.o $(LDFLAGS) $(LDLIBS) + +serialRead: serialRead.o + @echo [link] + @$(CC) -o $@ serialRead.o $(LDFLAGS) $(LDLIBS) + +okLed: okLed.o + @echo [link] + @$(CC) -o $@ okLed.o $(LDFLAGS) $(LDLIBS) + +tone: tone.o + @echo [link] + @$(CC) -o $@ tone.o $(LDFLAGS) $(LDLIBS) + +servo: servo.o + @echo [link] + @$(CC) -o $@ servo.o $(LDFLAGS) $(LDLIBS) .c.o: @@ -92,7 +111,7 @@ delayTest: delayTest.o @$(CC) -c $(CFLAGS) $< -o $@ clean: - rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface gertboard nes delayTest softPwm + rm -f $(OBJ) *~ core tags $(BINS) tags: $(SRC) @echo [ctags] diff --git a/examples/delayTest.c b/examples/delayTest.c index 8c95522..d05f3ff 100644 --- a/examples/delayTest.c +++ b/examples/delayTest.c @@ -3,30 +3,24 @@ #include #include -#include -#include #include #define CYCLES 1000 -#define DELAY 99 int main() { int x ; struct timeval t1, t2 ; - long long t ; - unsigned int max, min ; - - unsigned int values [CYCLES] ; - - max = 0 ; - min = 1000000 ; + int t ; + int max, min ; + int del ; + int underRuns, overRuns, exactRuns, total ; + int descheds ; if (wiringPiSetup () == -1) return 1 ; - piHiPri (10) ; - sleep (1) ; + piHiPri (10) ; sleep (1) ; // Baseline test @@ -34,35 +28,56 @@ int main() gettimeofday (&t2, NULL) ; t = t2.tv_usec - t1.tv_usec ; - printf ("Baseline test: %lld\n", t); + printf ("Baseline test: %d\n", t); - for (x = 0 ; x < CYCLES ; ++x) + for (del = 1 ; del < 200 ; ++del) { - gettimeofday (&t1, NULL) ; - delayMicroseconds (DELAY) ; - gettimeofday (&t2, NULL) ; - - t = t2.tv_usec - t1.tv_usec ; - if (t > max) max = t ; - if (t < min) min = t ; - values [x] = t ; - } + underRuns = overRuns = exactRuns = total = 0 ; + descheds = 0 ; + max = del ; + min = del ; - printf ("Done: Max: %d, min: %d\n", max, min) ; + for (x = 0 ; x < CYCLES ; ++x) + { + for (;;) // Repeat this if we get a delay over 999uS + { // -> High probability Linux has deschedulled us + gettimeofday (&t1, NULL) ; + delayMicroseconds (del) ; + gettimeofday (&t2, NULL) ; - for (x = 0 ; x < CYCLES ; ++x) - { - printf ("%4d", values [x]) ; - if (values [x] > DELAY) - printf (".") ; - else if (values [x] < DELAY) - printf ("-") ; - else - printf (" ") ; - if (((x + 1) % 20) == 0) - printf ("\n") ; + if (t2.tv_usec < t1.tv_usec) // Counter wrapped + t = (1000000 + t2.tv_usec) - t1.tv_usec; + else + t = t2.tv_usec - t1.tv_usec ; + if (t > 999) + { + ++descheds ; + continue ; + } + else + break ; + } + + if (t > max) + { + max = t ; + ++overRuns ; + } + else if (t < min) + { + min = t ; + ++underRuns ; + } + else + ++exactRuns ; + + total += t ; + } + printf ("Delay: %3d. Min: %3d, Max: %3d, Unders: %3d, Overs: %3d, Exacts: %3d, Average: %3d, Descheds: %2d\n", + del, min, max, underRuns, overRuns, exactRuns, total / CYCLES, descheds) ; + fflush (stdout) ; + delay (1) ; } - printf ("\n") ; return 0 ; } diff --git a/examples/okLed.c b/examples/okLed.c new file mode 100644 index 0000000..3bf21e2 --- /dev/null +++ b/examples/okLed.c @@ -0,0 +1,65 @@ +/* + * okLed: + * Make the OK LED on the Pi Pulsate... + * Copyright (c) 2012 gordon Henderson, but please Share and Enjoy! + * + * Originally posted to the Raspberry Pi forums: + * http://www.raspberrypi.org/phpBB3/viewtopic.php?p=162581#p162581 + * + * Compile this and store it somewhere, then kick it off at boot time + * e.g. by putting it in /etc/rc.local and running it in the + * background & + * + */ + +#include +#include +#include +#include +#include + +#include +#include + +#define OK_LED 16 + +int main () +{ + int fd, i ; + + if ((fd = open ("/sys/class/leds/led0/trigger", O_RDWR)) < 0) + { + fprintf (stderr, "Unable to change LED trigger: %s\n", strerror (errno)) ; + return 1 ; + } + + write (fd, "none\n", 5) ; + close (fd) ; + + if (wiringPiSetupGpio () < 0) + { + fprintf (stderr, "Unable to setup GPIO: %s\n", strerror (errno)) ; + return 1 ; + } + + softPwmCreate (OK_LED, 0, 100) ; + + for (;;) + { + for (i = 0 ; i <= 100 ; ++i) + { + softPwmWrite (OK_LED, i) ; + delay (10) ; + } + delay (50) ; + + for (i = 100 ; i >= 0 ; --i) + { + softPwmWrite (OK_LED, i) ; + delay (10) ; + } + delay (10) ; + } + + return 0 ; +} diff --git a/examples/softPwm.c b/examples/pwm.c similarity index 100% rename from examples/softPwm.c rename to examples/pwm.c diff --git a/examples/serialRead.c b/examples/serialRead.c new file mode 100644 index 0000000..34b9bad --- /dev/null +++ b/examples/serialRead.c @@ -0,0 +1,31 @@ + +/* + * serialRead.c: + * Example program to read bytes from the Serial line + * + */ + +#include +#include +#include + +#include + +int main () +{ + int fd ; + + if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0) + { + fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; + return 1 ; + } + +// Loop, getting and printing characters + + for (;;) + { + putchar (serialGetchar (fd)) ; + fflush (stdout) ; + } +} diff --git a/examples/servo.c b/examples/servo.c new file mode 100644 index 0000000..0237832 --- /dev/null +++ b/examples/servo.c @@ -0,0 +1,33 @@ + +#include +#include +#include + +#include +#include + +int main () +{ + if (wiringPiSetup () == -1) + { + fprintf (stdout, "oops: %s\n", strerror (errno)) ; + return 1 ; + } + + softServoSetup (0, 1, 2, 3, 4, 5, 6, 7) ; + + softServoWrite (0, 0) ; +/* + softServoWrite (1, 1000) ; + softServoWrite (2, 1100) ; + softServoWrite (3, 1200) ; + softServoWrite (4, 1300) ; + softServoWrite (5, 1400) ; + softServoWrite (6, 1500) ; + softServoWrite (7, 2200) ; +*/ + + for (;;) + delay (10) ; + +} diff --git a/examples/tone.c b/examples/tone.c new file mode 100644 index 0000000..8b1fcd7 --- /dev/null +++ b/examples/tone.c @@ -0,0 +1,37 @@ + +#include +#include +#include + +#include +#include + +#define RANGE 100 +#define NUM_LEDS 12 + +int scale [8] = { 262, 294, 330, 349, 392, 440, 494, 525 } ; + +int main () +{ + int i, j ; + char buf [80] ; + + if (wiringPiSetup () == -1) + { + fprintf (stdout, "oops: %s\n", strerror (errno)) ; + return 1 ; + } + + softToneCreate (3) ; + + for (;;) + { + for (i = 0 ; i < 8 ; ++i) + { + printf ("%3d\n", i) ; + softToneWrite (3, scale [i]) ; + delay (500) ; + } + } + +} diff --git a/gpio/gpio.1 b/gpio/gpio.1 index be38791..c39e5dc 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -9,11 +9,11 @@ gpio \- Command-line access to Raspberry Pi and PiFace GPIO .PP .B gpio .B [ \-g ] -.B read/write/pwm/mode ... +.B read/write/wb/pwm/mode ... .PP .B gpio .B [ \-p ] -.B read/write/mode +.B read/write/wb .B ... .PP .B gpio @@ -82,7 +82,14 @@ respective logic levels. .TP .B write -Write the given value (0 or 1) to the pin. +Write the given value (0 or 1) to the pin. You need to set the pin +to output mode first. + +.TP +.B wb +Write the given byte to the 8 main GPIO pins. You can prefix it with 0x +to specify a hexadecimal number. You need to set pins to output mode +first. .TP .B readall diff --git a/gpio/gpio.c b/gpio/gpio.c index f019c1f..52fcb6f 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -40,14 +40,14 @@ # define FALSE (1==2) #endif -#define VERSION "1.4" +#define VERSION "1.5" static int wpMode ; char *usage = "Usage: gpio -v\n" " gpio -h\n" - " gpio [-g] ...\n" - " gpio [-p] ...\n" + " gpio [-g] ...\n" + " gpio [-p] ...\n" " gpio readall\n" " gpio unexportall/exports ...\n" " gpio export/edge/unexport ...\n" @@ -133,7 +133,7 @@ static void _doLoadUsage (char *argv []) static void doLoad (int argc, char *argv []) { - char *module ; + char *module1, *module2 ; char cmd [80] ; char *file1, *file2 ; @@ -142,28 +142,36 @@ static void doLoad (int argc, char *argv []) /**/ if (strcasecmp (argv [2], "spi") == 0) { - module = "spi_bcm2708" ; + module1 = "spidev" ; + module2 = "spi_bcm2708" ; file1 = "/dev/spidev0.0" ; file2 = "/dev/spidev0.1" ; } else if (strcasecmp (argv [2], "i2c") == 0) { - module = "i2c_bcm2708" ; + module1 = "i2c_dev" ; + module2 = "i2c_bcm2708" ; file1 = "/dev/i2c-0" ; file2 = "/dev/i2c-1" ; } else _doLoadUsage (argv) ; - if (!moduleLoaded (module)) + if (!moduleLoaded (module1)) { - sprintf (cmd, "modprobe %s", module) ; + sprintf (cmd, "modprobe %s", module1) ; system (cmd) ; } - if (!moduleLoaded (module)) + if (!moduleLoaded (module2)) { - fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ; + sprintf (cmd, "modprobe %s", module2) ; + system (cmd) ; + } + + if (!moduleLoaded (module2)) + { + fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ; exit (1) ; } @@ -588,6 +596,7 @@ static void doPadDrive (int argc, char *argv []) /* * doGbw: * gpio gbw channel value + * Gertboard Write - To the Analog output ********************************************************************************* */ @@ -629,6 +638,7 @@ static void doGbw (int argc, char *argv []) /* * doGbr: * gpio gbr channel + * From the analog input ********************************************************************************* */ @@ -682,7 +692,12 @@ static void doWrite (int argc, char *argv []) if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) return ; - val = atoi (argv [3]) ; + /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0)) + val = 1 ; + else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0)) + val = 0 ; + else + val = atoi (argv [3]) ; /**/ if (val == 0) digitalWrite (pin, LOW) ; @@ -690,6 +705,27 @@ static void doWrite (int argc, char *argv []) digitalWrite (pin, HIGH) ; } +/* + * doWriteByte: + * gpio write value + ********************************************************************************* + */ + +static void doWriteByte (int argc, char *argv []) +{ + int val ; + + if (argc != 3) + { + fprintf (stderr, "Usage: %s wb value\n", argv [0]) ; + exit (1) ; + } + + val = (int)strtol (argv [2], NULL, 0) ; + + digitalWriteByte (val) ; +} + /* * doRead: @@ -936,11 +972,12 @@ int main (int argc, char *argv []) // Check for wiring commands - /**/ if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; - else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ; - else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ; - else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ; - else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ; + /**/ if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; + else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ; + else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ; + else if (strcasecmp (argv [1], "wb") == 0) doWriteByte (argc, argv) ; + else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ; + else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ; else { fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index c0a39f9..e18a654 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -24,8 +24,12 @@ DYN_VERS_MAJ=1 DYN_VERS_MIN=0 +VERSION=$(DYN_VERS_MAJ).$(DYN_VERS_MIN) +DESTDIR=/usr +PREFIX=/local + STATIC=libwiringPi.a -DYNAMIC=libwiringPi.so.$(DYN_VERS_MAJ).$(DYN_VERS_MIN) +DYNAMIC=libwiringPi.so.$(VERSION) #DEBUG = -g -O0 DEBUG = -O2 @@ -41,21 +45,22 @@ LIBS = SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ gertboard.c \ piNes.c \ - lcd.c piHiPri.c piThread.c softPwm.c wiringPiSPI.c + lcd.c piHiPri.c piThread.c wiringPiSPI.c \ + softPwm.c softServo.c softTone.c OBJ = $(SRC:.c=.o) -#all: $(STATIC) $(DYNAMIC) -all: $(DYNAMIC) +all: $(STATIC) $(DYNAMIC) +#all: $(DYNAMIC) $(STATIC): $(OBJ) - @echo [Link (Static)] + @echo "[Link (Static)]" @ar rcs $(STATIC) $(OBJ) @ranlib $(STATIC) - @size $(STATIC) +# @size $(STATIC) $(DYNAMIC): $(OBJ) - @echo [Link] + @echo "[Link (Dynamic)]" @$(CC) -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ) .c.o: @@ -74,34 +79,38 @@ tags: $(SRC) .PHONEY: install 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 wiringSerial.h /usr/local/include - @install -m 0644 wiringShift.h /usr/local/include - @install -m 0644 gertboard.h /usr/local/include - @install -m 0644 piNes.h /usr/local/include - @install -m 0644 softPwm.h /usr/local/include - @install -m 0644 lcd.h /usr/local/include - @install -m 0644 wiringPiSPI.h /usr/local/include -# @install -m 0644 libwiringPi.a /usr/local/lib - @install -m 0755 libwiringPi.so.1.0 /usr/local/lib - @ln -sf /usr/local/lib/libwiringPi.so.1.0 /usr/local/lib/libwiringPi.so - @ln -sf /usr/local/lib/libwiringPi.so.1.0 /usr/local/lib/libwiringPi.so.1 + @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + @install -m 0755 -d $(DESTDIR)$(PREFIX)/include + @install -m 0644 wiringPi.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 wiringSerial.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 wiringShift.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 gertboard.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 piNes.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 softPwm.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 softServo.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include + @install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib + @install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib + @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so + @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so.1 @ldconfig .PHONEY: uninstall uninstall: @echo "[UnInstall]" - @rm -f /usr/local/include/wiringPi.h - @rm -f /usr/local/include/wiringSerial.h - @rm -f /usr/local/include/wiringShift.h - @rm -f /usr/local/include/gertboard.h - @rm -f /usr/local/include/piNes.h - @rm -f /usr/local/include/softPwm.h - @rm -f /usr/local/include/lcd.h - @rm -f /usr/local/include/wiringPiSPI.h - @rm -f /usr/local/lib/libwiringPi.* + @rm -f $(DESTDIR)$(PREFIX)/include/wiringPi.h + @rm -f $(DESTDIR)$(PREFIX)/include/wiringSerial.h + @rm -f $(DESTDIR)$(PREFIX)/include/wiringShift.h + @rm -f $(DESTDIR)$(PREFIX)/include/gertboard.h + @rm -f $(DESTDIR)$(PREFIX)/include/piNes.h + @rm -f $(DESTDIR)$(PREFIX)/include/softPwm.h + @rm -f $(DESTDIR)$(PREFIX)/include/softServo.h + @rm -f $(DESTDIR)$(PREFIX)/include/softTone.h + @rm -f $(DESTDIR)$(PREFIX)/include/lcd.h + @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h + @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.* @ldconfig diff --git a/wiringPi/softServo.c b/wiringPi/softServo.c new file mode 100644 index 0000000..a6ff1fb --- /dev/null +++ b/wiringPi/softServo.c @@ -0,0 +1,202 @@ +/* + * softServo.c: + * Provide N channels of software driven PWM suitable for RC + * servo motors. + * 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 +#include +#include + +#include "wiringPi.h" +#include "softServo.h" + +// RC Servo motors are a bit of an oddity - designed in the days when +// radio control was experimental and people were tryin to make +// things as simple as possible as it was all very expensive... +// +// So... To drive an RC Servo motor, you need to send it a modified PWM +// signal - it needs anything from 1ms to 2ms - with 1ms meaning +// to move the server fully left, and 2ms meaning to move it fully +// right. Then you need a long gap before sending the next pulse. +// The reason for this is that you send a multiplexed stream of these +// pulses up the radio signal into the reciever which de-multiplexes +// them into the signals for each individual servo. Typically there +// might be 8 channels, so you need at least 8 "slots" of 2mS pulses +// meaning the entire frame must fit into a 16mS slot - which would +// then be repeated... +// +// In practice we have a total slot width of about 20mS - so we're sending 50 +// updates per second to each servo. +// +// In this code, we don't need to be too fussy about the gap as we're not doing +// the multipexing, but it does need to be at least 10mS, and preferably 16 +// from what I've been able to determine. + + +#define MAX_SERVOS 8 + +static int pinMap [MAX_SERVOS] ; // Keep track of our pins +static int pulseWidth [MAX_SERVOS] ; // microseconds + + +/* + * softServoThread: + * Thread to do the actual Servo PWM output + ********************************************************************************* + */ + +static PI_THREAD (softServoThread) +{ + register int i, j, k, m, tmp ; + int lastDelay, pin, servo ; + + int myDelays [MAX_SERVOS] ; + int myPins [MAX_SERVOS] ; + + struct timeval tNow, tStart, tPeriod, tGap, tTotal ; + struct timespec tNs ; + + tTotal.tv_sec = 0 ; + tTotal.tv_usec = 8000 ; + + piHiPri (50) ; + + for (;;) + { + gettimeofday (&tStart, NULL) ; + + memcpy (myDelays, pulseWidth, sizeof (myDelays)) ; + memcpy (myPins, pinMap, sizeof (myPins)) ; + +// Sort the delays (& pins), shortest first + + for (m = MAX_SERVOS / 2 ; m > 0 ; m /= 2 ) + for (j = m ; j < MAX_SERVOS ; ++j) + for (i = j - m ; i >= 0 ; i -= m) + { + k = i + m ; + if (myDelays [k] >= myDelays [i]) + break ; + else // Swap + { + tmp = myDelays [i] ; myDelays [i] = myDelays [k] ; myDelays [k] = tmp ; + tmp = myPins [i] ; myPins [i] = myPins [k] ; myPins [k] = tmp ; + } + } + +// All on + + lastDelay = 0 ; + for (servo = 0 ; servo < MAX_SERVOS ; ++servo) + { + if ((pin = myPins [servo]) == -1) + continue ; + + digitalWrite (pin, HIGH) ; + myDelays [servo] = myDelays [servo] - lastDelay ; + lastDelay += myDelays [servo] ; + } + +// Now loop, turning them all off as required + + for (servo = 0 ; servo < MAX_SERVOS ; ++servo) + { + if ((pin = myPins [servo]) == -1) + continue ; + + delayMicroseconds (myDelays [servo]) ; + digitalWrite (pin, LOW) ; + } + +// Wait until the end of an 8mS time-slot + + gettimeofday (&tNow, NULL) ; + timersub (&tNow, &tStart, &tPeriod) ; + timersub (&tTotal, &tPeriod, &tGap) ; + tNs.tv_sec = tGap.tv_sec ; + tNs.tv_nsec = tGap.tv_usec * 1000 ; + nanosleep (&tNs, NULL) ; + } + + return NULL ; +} + + +/* + * softServoWrite: + * Write a Servo value to the given pin + ********************************************************************************* + */ + +void softServoWrite (int servoPin, int value) +{ + int servo ; + + servoPin &= 63 ; + + /**/ if (value < -250) + value = -250 ; + else if (value > 1250) + value = 1250 ; + + for (servo = 0 ; servo < MAX_SERVOS ; ++servo) + if (pinMap [servo] == servoPin) + pulseWidth [servo] = value + 1000 ; // uS +} + + +/* + * softServoSetup: + * Setup the software servo system + ********************************************************************************* + */ + +int softServoSetup (int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7) +{ + int servo ; + + if (p0 != -1) { pinMode (p0, OUTPUT) ; digitalWrite (p0, LOW) ; } + if (p1 != -1) { pinMode (p1, OUTPUT) ; digitalWrite (p1, LOW) ; } + if (p2 != -1) { pinMode (p2, OUTPUT) ; digitalWrite (p2, LOW) ; } + if (p3 != -1) { pinMode (p3, OUTPUT) ; digitalWrite (p3, LOW) ; } + if (p4 != -1) { pinMode (p4, OUTPUT) ; digitalWrite (p4, LOW) ; } + if (p5 != -1) { pinMode (p5, OUTPUT) ; digitalWrite (p5, LOW) ; } + if (p6 != -1) { pinMode (p6, OUTPUT) ; digitalWrite (p6, LOW) ; } + if (p7 != -1) { pinMode (p7, OUTPUT) ; digitalWrite (p7, LOW) ; } + + pinMap [0] = p0 ; + pinMap [1] = p1 ; + pinMap [2] = p2 ; + pinMap [3] = p3 ; + pinMap [4] = p4 ; + pinMap [5] = p5 ; + pinMap [6] = p6 ; + pinMap [7] = p7 ; + + for (servo = 0 ; servo < MAX_SERVOS ; ++servo) + pulseWidth [servo] = 1500 ; // Mid point + + return piThreadCreate (softServoThread) ; +} diff --git a/wiringPi/softServo.h b/wiringPi/softServo.h new file mode 100644 index 0000000..794cf55 --- /dev/null +++ b/wiringPi/softServo.h @@ -0,0 +1,35 @@ +/* + * softServo.h: + * Provide N channels of software driven PWM suitable for RC + * servo motors. + * 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 . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern void softServoWrite (int pin, int value) ; +extern int softServoSetup (int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/softTone.c b/wiringPi/softTone.c new file mode 100644 index 0000000..d14c2a2 --- /dev/null +++ b/wiringPi/softTone.c @@ -0,0 +1,119 @@ +/* + * softTone.c: + * For that authentic retro sound... + * Er... A little experiment to produce tones out of a Pi using + * one (or 2) GPIO pins and a piezeo "speaker" element. + * (Or a high impedance speaker, but don'y blame me if you blow-up + * the GPIO pins!) + * 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" +#include "softTone.h" + +#define MAX_PINS 64 + +#define PULSE_TIME 100 + +static int frewqs [MAX_PINS] ; + +static int newPin = -1 ; + + +/* + * softToneThread: + * Thread to do the actual PWM output + ********************************************************************************* + */ + +static PI_THREAD (softToneThread) +{ + int pin, frewq, halfPeriod ; + + pin = newPin ; + newPin = -1 ; + + piHiPri (50) ; + + for (;;) + { + frewq = frewqs [pin] ; + if (frewq != 0) + { + halfPeriod = 500000 / frewq ; + + digitalWrite (pin, HIGH) ; + delayMicroseconds (halfPeriod) ; + + digitalWrite (pin, LOW) ; + delayMicroseconds (halfPeriod) ; + } + } + + return NULL ; +} + + +/* + * softToneWrite: + * Write a frequency value to the given pin + ********************************************************************************* + */ + +void softToneWrite (int pin, int frewq) +{ + pin &= 63 ; + + /**/ if (frewq < 0) + frewq = 0 ; + else if (frewq > 5000) // Max 5KHz + frewq = 5000 ; + + frewqs [pin] = frewq ; +} + + +/* + * softToneCreate: + * Create a new tone thread. + ********************************************************************************* + */ + +int softToneCreate (int pin) +{ + int res ; + + pinMode (pin, OUTPUT) ; + digitalWrite (pin, LOW) ; + + frewqs [pin] = 0 ; + + newPin = pin ; + res = piThreadCreate (softToneThread) ; + + while (newPin != -1) + delay (1) ; + + return res ; +} diff --git a/wiringPi/softTone.h b/wiringPi/softTone.h new file mode 100644 index 0000000..80c64fe --- /dev/null +++ b/wiringPi/softTone.h @@ -0,0 +1,38 @@ +/* + * softTone.c: + * For that authentic retro sound... + * Er... A little experiment to produce tones out of a Pi using + * one (or 2) GPIO pins and a piezeo "speaker" element. + * (Or a high impedance speaker, but don'y blame me if you blow-up + * the GPIO pins!) + * 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 . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int softToneCreate (int pin) ; +extern void softToneWrite (int pin, int frewq) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 9fe3ab1..df4d969 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -77,6 +77,7 @@ void (*pinMode) (int pin, int mode) ; void (*pullUpDnControl) (int pin, int pud) ; void (*digitalWrite) (int pin, int value) ; +void (*digitalWriteByte) (int value) ; void (*pwmWrite) (int pin, int value) ; void (*setPadDrive) (int group, int value) ; int (*digitalRead) (int pin) ; @@ -399,15 +400,15 @@ int wpiPinToGpio (int wpiPin) * Revision is currently 1 or 2. -1 is returned on error. * * Much confusion here )-: - * Seems there ar esome boards with 0000 in them (mistake in manufacture) - * and some board with 0005 in them (another mistake in manufacture). + * Seems there are some boards with 0000 in them (mistake in manufacture) + * and some board with 0005 in them (another mistake in manufacture?) * So the distinction between boards that I can see is: * 0000 - Error * 0001 - Not used * 0002 - Rev 1 * 0003 - Rev 1 * 0004 - Rev 2 - * 0005 - Rev 2 + * 0005 - Rev 2 (but error) * 0006 - Rev 2 * 000f - Rev 2 + 512MB * @@ -499,6 +500,8 @@ int piBoardRev (void) void pinModeGpio (int pin, int mode) { +// register int barrier ; + int fSel, shift, alt ; pin &= 63 ; @@ -519,30 +522,40 @@ void pinModeGpio (int pin, int mode) *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; + delayMicroseconds (110) ; // See comments in pwmSetClockWPi + *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; + // Page 107 of the BCM Peripherals manual talks about the GPIO clocks, // but I'm assuming (hoping!) that this applies to other clocks too. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM - *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock - delayMicroseconds (110) ; // See comments in pwmSetClockWPi - (void)*(pwm + PWM_CONTROL) ; - while ((*(pwm + PWM_CONTROL) & 0x80) != 0) // Wait for clock to be !BUSY + *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock + delayMicroseconds (110) ; // See comments in pwmSetClockWPi + + while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY delayMicroseconds (1) ; *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32 << 12) ; // set pwm div to 32 (19.2/32 = 600KHz) *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // enable clk -// Default range regsiter of 1024 + delayMicroseconds (110) ; // See comments in pwmSetClockWPi - *(pwm + PWM0_DATA) = 0 ; *(pwm + PWM0_RANGE) = 1024 ; - *(pwm + PWM1_DATA) = 0 ; *(pwm + PWM1_RANGE) = 1024 ; +// Default range register of 1024 + + *(pwm + PWM0_RANGE) = 1024 ; delayMicroseconds (10) ; + *(pwm + PWM1_RANGE) = 1024 ; delayMicroseconds (10) ; + *(pwm + PWM0_DATA) = 0 ; delayMicroseconds (10) ; + *(pwm + PWM1_DATA) = 0 ; delayMicroseconds (10) ; // Enable PWMs in balanced mode (default) *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; + + delay (100) ; } + // When we change mode of any pin, we remove the pull up/downs // Or we used to... Hm. Commented out now because for some wieird reason, // it seems to block subsequent attempts to set the pull up/downs and I've @@ -629,7 +642,7 @@ void pwmSetClockWPi (int divisor) *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock delayMicroseconds (110) ; // prevents clock going sloooow - while ((*(pwm + PWM_CONTROL) & 0x80) != 0) // Wait for clock to be !BUSY + while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY delayMicroseconds (1) ; *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ; @@ -704,6 +717,50 @@ void digitalWriteSys (int pin, int value) } +/* + * digitalWriteByte: + * Write an 8-bit byte to the first 8 GPIO pins - try to do it as + * fast as possible. + * However it still needs 2 operations to set the bits, so any external + * hardware must not rely on seeing a change as there will be a change + * to set the outputs bits to zero, then another change to set the 1's + ********************************************************************************* + */ + +void digitalWriteByteGpio (int value) +{ + uint32_t pinSet = 0 ; + uint32_t pinClr = 0 ; + int mask = 1 ; + int pin ; + + for (pin = 0 ; pin < 8 ; ++pin) + { + if ((value & mask) == 0) + pinClr |= (1 << pinToGpio [pin]) ; + else + pinSet |= (1 << pinToGpio [pin]) ; + + *(gpio + gpioToGPCLR [0]) = pinClr ; + *(gpio + gpioToGPSET [0]) = pinSet ; + + mask <<= 1 ; + } +} + +void digitalWriteByteSys (int value) +{ + int mask = 1 ; + int pin ; + + for (pin = 0 ; pin < 8 ; ++pin) + { + digitalWriteSys (pinToGpio [pin], value & mask) ; + mask <<= 1 ; + } +} + + /* * pwmWrite: * Set an output PWM value @@ -915,6 +972,9 @@ void delay (unsigned int howLong) * somewhat sub-optimal in that it uses 100% CPU, something not an issue * in a microcontroller, but under a multi-tasking, multi-user OS, it's * wastefull, however we've no real choice )-: + * + * Plan B: It seems all might not be well with that plan, so changing it + * to use gettimeofday () and poll on that instead... ********************************************************************************* */ @@ -930,16 +990,31 @@ void delayMicrosecondsSys (unsigned int howLong) void delayMicrosecondsHard (unsigned int howLong) { +#ifdef HARD_TIMER + volatile unsigned int dummy ; + *(timer + TIMER_LOAD) = howLong ; *(timer + TIMER_IRQ_CLR) = 0 ; - while (*timerIrqRaw == 0) - ; + dummy = *timerIrqRaw ; + while (dummy == 0) + dummy = *timerIrqRaw ; +#else + struct timeval tNow, tLong, tEnd ; + + gettimeofday (&tNow, NULL) ; + tLong.tv_sec = howLong / 1000000 ; + tLong.tv_usec = howLong % 1000000 ; + timeradd (&tNow, &tLong, &tEnd) ; + + while (timercmp (&tNow, &tEnd, <)) + gettimeofday (&tNow, NULL) ; +#endif } void delayMicrosecondsWPi (unsigned int howLong) { - struct timespec sleeper, dummy ; + struct timespec sleeper ; /**/ if (howLong == 0) return ; @@ -949,7 +1024,7 @@ void delayMicrosecondsWPi (unsigned int howLong) { sleeper.tv_sec = 0 ; sleeper.tv_nsec = (long)(howLong * 1000) ; - nanosleep (&sleeper, &dummy) ; + nanosleep (&sleeper, NULL) ; } } @@ -998,6 +1073,7 @@ int wiringPiSetup (void) pinMode = pinModeWPi ; pullUpDnControl = pullUpDnControlWPi ; digitalWrite = digitalWriteWPi ; + digitalWriteByte = digitalWriteByteGpio ; // Same code pwmWrite = pwmWriteWPi ; setPadDrive = setPadDriveWPi ; digitalRead = digitalReadWPi ; @@ -1166,6 +1242,7 @@ int wiringPiSetupGpio (void) pinMode = pinModeGpio ; pullUpDnControl = pullUpDnControlGpio ; digitalWrite = digitalWriteGpio ; + digitalWriteByte = digitalWriteByteGpio ; pwmWrite = pwmWriteGpio ; setPadDrive = setPadDriveGpio ; digitalRead = digitalReadGpio ; @@ -1190,6 +1267,7 @@ int wiringPiSetupGpio (void) int wiringPiSetupSys (void) { + int boardRev ; int pin ; struct timeval tv ; char fName [128] ; @@ -1200,6 +1278,7 @@ int wiringPiSetupSys (void) pinMode = pinModeSys ; pullUpDnControl = pullUpDnControlSys ; digitalWrite = digitalWriteSys ; + digitalWriteByte = digitalWriteByteSys ; pwmWrite = pwmWriteSys ; setPadDrive = setPadDriveSys ; digitalRead = digitalReadSys ; @@ -1209,6 +1288,14 @@ int wiringPiSetupSys (void) pwmSetRange = pwmSetRangeSys ; pwmSetClock = pwmSetClockSys ; + if ((boardRev = piBoardRev ()) < 0) + return -1 ; + + if (boardRev == 1) + pinToGpio = pinToGpioR1 ; + else + pinToGpio = pinToGpioR2 ; + // Open and scan the directory, looking for exported GPIOs, and pre-open // the 'value' interface to speed things up for later diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index cab3080..6a7278e 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -70,6 +70,7 @@ extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio extern void (*pinMode) (int pin, int mode) ; extern void (*pullUpDnControl) (int pin, int pud) ; extern void (*digitalWrite) (int pin, int value) ; +extern void (*digitalWriteByte) (int value) ; extern void (*pwmWrite) (int pin, int value) ; extern void (*setPadDrive) (int group, int value) ; extern int (*digitalRead) (int pin) ; diff --git a/wiringPi/wiringPiFace.c b/wiringPi/wiringPiFace.c index 2425413..ac3c6fa 100644 --- a/wiringPi/wiringPiFace.c +++ b/wiringPi/wiringPiFace.c @@ -182,6 +182,11 @@ void digitalWritePiFace (int pin, int value) writeByte (GPIOA, dataOutRegister) ; } +void digitalWriteBytePiFace (int value) +{ + writeByte (GPIOA, value) ; +} + void digitalWritePiFaceSpecial (int pin, int value) { @@ -318,12 +323,13 @@ int wiringPiSetupPiFace (void) 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 ; + pinMode = pinModePiFace ; + pullUpDnControl = pullUpDnControlPiFace ; + digitalWrite = digitalWritePiFace ; + digitalWriteByte = digitalWriteBytePiFace ; + pwmWrite = pwmWritePiFace ; + digitalRead = digitalReadPiFace ; + waitForInterrupt = waitForInterruptPiFace ; return 0 ; } @@ -344,12 +350,13 @@ int wiringPiSetupPiFaceForGpioProg (void) if (x != 0) return x ; - pinMode = pinModePiFace ; + pinMode = pinModePiFace ; pullUpDnControl = pullUpDnControlPiFaceSpecial ; digitalWrite = digitalWritePiFaceSpecial ; - pwmWrite = pwmWritePiFace ; - digitalRead = digitalReadPiFace ; - waitForInterrupt = waitForInterruptPiFace ; + digitalWriteByte = digitalWriteBytePiFace ; + pwmWrite = pwmWritePiFace ; + digitalRead = digitalReadPiFace ; + waitForInterrupt = waitForInterruptPiFace ; return 0 ; } diff --git a/wiringPi/wiringPiSPI.c b/wiringPi/wiringPiSPI.c index f2e3000..4441498 100644 --- a/wiringPi/wiringPiSPI.c +++ b/wiringPi/wiringPiSPI.c @@ -52,7 +52,7 @@ static int spiFds [2] ; int wiringPiSPIGetFd (int channel) { - return spiFds [channel &1] ; + return spiFds [channel & 1] ; } From 13bbba7a2275f19d597909ecd80a074ae34c94f6 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 14 Jan 2013 11:31:56 +0000 Subject: [PATCH 26/97] Lots of changes here. Added new I2C test code, a new serialTest program, and developed the new ISR - Interrupt Service Routine handler - much easier than the old waitForInterrupt code! Minor tweaks to the gpio program to recognise the environment variable WIRINGPI_DEBUG too, and removed the printing of the errors from the main wiringPi setup routines (and added some new ones!) --- INSTALL | 36 +++++++ examples/Makefile | 12 ++- examples/isr.c | 99 ++++++++++++++++++ examples/okLed.c | 1 + examples/serialTest.c | 57 ++++++++++ examples/wfi.c | 31 +++--- gpio/Makefile | 2 +- gpio/gpio.1 | 13 ++- gpio/gpio.c | 24 ++++- wiringPi/Makefile | 20 ++-- wiringPi/wiringPi.c | 231 ++++++++++++++++++++++++++++++++--------- wiringPi/wiringPi.h | 37 ++++--- wiringPi/wiringPiI2C.c | 122 ++++++++++++++++++++++ wiringPi/wiringPiI2C.h | 41 ++++++++ wiringPi/wiringPiISR.c | 109 +++++++++++++++++++ 15 files changed, 744 insertions(+), 91 deletions(-) create mode 100644 INSTALL create mode 100644 examples/isr.c create mode 100644 examples/serialTest.c create mode 100644 wiringPi/wiringPiI2C.c create mode 100644 wiringPi/wiringPiI2C.h create mode 100644 wiringPi/wiringPiISR.c diff --git a/INSTALL b/INSTALL new file mode 100644 index 0000000..8e0c43c --- /dev/null +++ b/INSTALL @@ -0,0 +1,36 @@ + +How to install wiringPi +======================= + +The easiest way is to use the supplied 'build' script: + + ./build + +that should do a complete install or upgrade of wiringPi for you. + +That will install a dynamic library. + +Some distributions do not have /usr/local/lib in the default LD_LIBRARY_PATH. To +fix this, you need to edit /etc/ld.so.conf and add in a single line: + + /usr/local/lib + +then run the ldconfig command. + + sudo ldconfig + +If you want to install a static library, you may need to do this manually: + + cd wiringPi + make static + sudo make install-static + + +To un-install wiringPi: + + ./build uninstall + +Gordon Henderson + +projects@drogon.net +https://projects.drogon.net/ diff --git a/examples/Makefile b/examples/Makefile index 738d36c..3607fc8 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -35,10 +35,10 @@ LDLIBS = -lwiringPi # Should not alter anything below this line ############################################################################### -SRC = test1.c test2.c speed.c lcd.c wfi.c \ +SRC = test1.c test2.c speed.c lcd.c wfi.c isr.c \ piface.c gertboard.c nes.c \ pwm.c tone.c servo.c \ - delayTest.c serialRead.c okLed.c + delayTest.c serialRead.c serialTest.c okLed.c OBJ = $(SRC:.c=.o) @@ -69,6 +69,10 @@ wfi: wfi.o @echo [link] @$(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS) +isr: isr.o + @echo [link] + @$(CC) -o $@ isr.o $(LDFLAGS) $(LDLIBS) + piface: piface.o @echo [link] @$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS) -lpthread @@ -93,6 +97,10 @@ serialRead: serialRead.o @echo [link] @$(CC) -o $@ serialRead.o $(LDFLAGS) $(LDLIBS) +serialTest: serialTest.o + @echo [link] + @$(CC) -o $@ serialTest.o $(LDFLAGS) $(LDLIBS) + okLed: okLed.o @echo [link] @$(CC) -o $@ okLed.o $(LDFLAGS) $(LDLIBS) diff --git a/examples/isr.c b/examples/isr.c new file mode 100644 index 0000000..2bef54a --- /dev/null +++ b/examples/isr.c @@ -0,0 +1,99 @@ +/* + * isr.c: + * Wait for Interrupt test program - ISR method + * + * How to test: + * Use the SoC's pull-up and pull down resistors that are avalable + * on input pins. So compile & run this program (via sudo), then + * in another terminal: + * gpio mode 0 up + * gpio mode 0 down + * at which point it should trigger an interrupt. Toggle the pin + * up/down to generate more interrupts to test. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include +#include + + +// What GPIO input are we using? +// This is a wiringPi pin number + +#define BUTTON_PIN 0 + +// globalCounter: +// Global variable to count interrupts +// Should be declared volatile to make sure the compiler doesn't cache it. + +static volatile int globalCounter = 0 ; + + +/* + * myInterrupt: + ********************************************************************************* + */ + +void myInterrupt (void) +{ + ++globalCounter ; +} + + +/* + ********************************************************************************* + * main + ********************************************************************************* + */ + +int main (void) +{ + int myCounter = 0 ; + + if (wiringPiSetup () < 0) + { + fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ; + return 1 ; + } + + if (wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0) + { + fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ; + return 1 ; + } + + + for (;;) + { + printf ("Waiting ... ") ; fflush (stdout) ; + + while (myCounter == globalCounter) + delay (100) ; + + printf (" Done. counter: %5d\n", globalCounter) ; + myCounter = globalCounter ; + } + + return 0 ; +} diff --git a/examples/okLed.c b/examples/okLed.c index 3bf21e2..02f0b22 100644 --- a/examples/okLed.c +++ b/examples/okLed.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include diff --git a/examples/serialTest.c b/examples/serialTest.c new file mode 100644 index 0000000..85a1a66 --- /dev/null +++ b/examples/serialTest.c @@ -0,0 +1,57 @@ +/* + * serialTest.c: + * Very simple program to test the serial port. Expects + * the port to be looped back to itself + * + */ + +#include +#include +#include + +#include +#include + +int main () +{ + int fd ; + int count ; + unsigned int nextTime ; + + if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0) + { + fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; + return 1 ; + } + + if (wiringPiSetup () == -1) + { + fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; + return 1 ; + } + + nextTime = millis () + 300 ; + + for (count = 0 ; count < 256 ; ) + { + if (millis () > nextTime) + { + printf ("\nOut: %3d: ", count) ; + fflush (stdout) ; + serialPutchar (fd, count) ; + nextTime += 300 ; + ++count ; + } + + delay (3) ; + + while (serialDataAvail (fd)) + { + printf (" -> %3d", serialGetchar (fd)) ; + fflush (stdout) ; + } + } + + printf ("\n") ; + return 0 ; +} diff --git a/examples/wfi.c b/examples/wfi.c index 9efcc2c..6bb6892 100644 --- a/examples/wfi.c +++ b/examples/wfi.c @@ -2,7 +2,17 @@ * wfi.c: * Wait for Interrupt test program * - * Copyright (c) 2012 Gordon Henderson. + * This program demonstrates the use of the waitForInterrupt() + * function in wiringPi. It listens to a button input on + * BCM_GPIO pin 17 (wiringPi pin 0) + * + * The biggest issue with this method is that it really only works + * well in Sys mode. + * + * Jan 2013: This way of doing things is sort of deprecated now, see + * the wiringPiISR() function instead and the isr.c test program here. + * + * Copyright (c) 2012-2013 Gordon Henderson. *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -33,9 +43,8 @@ #define COUNT_KEY 0 // What BCM_GPIO input are we using? -// GPIO 0 is one of the I2C pins with an on-board pull-up -#define BUTTON_PIN 0 +#define BUTTON_PIN 17 // Debounce time in mS @@ -63,13 +72,11 @@ PI_THREAD (waitForIt) int debounceTime = 0 ; (void)piHiPri (10) ; // Set this thread to be high priority - digitalWrite (18, 1) ; for (;;) { if (waitForInterrupt (BUTTON_PIN, -1) > 0) // Got it { - // Bouncing? if (millis () < debounceTime) @@ -80,7 +87,6 @@ PI_THREAD (waitForIt) // We have a valid one - digitalWrite (17, state) ; state ^= 1 ; piLock (COUNT_KEY) ; @@ -89,7 +95,7 @@ PI_THREAD (waitForIt) // Wait for key to be released - while (digitalRead (0) == LOW) + while (digitalRead (BUTTON_PIN) == LOW) delay (1) ; debounceTime = millis () + DEBOUNCE_TIME ; @@ -108,11 +114,9 @@ void setup (void) { // Use the gpio program to initialise the hardware -// (This is the crude, but effective bit) +// (This is the crude, but effective) - system ("gpio edge 0 falling") ; - system ("gpio export 17 out") ; - system ("gpio export 18 out") ; + system ("gpio edge 17 falling") ; // Setup wiringPi @@ -120,9 +124,8 @@ void setup (void) // Fire off our interrupt handler - piThreadCreate (waitForIt) ; + piThreadCreate (waitForIt) ; - digitalWrite (17, 0) ; } @@ -147,7 +150,7 @@ int main (void) piLock (COUNT_KEY) ; myCounter = globalCounter ; piUnlock (COUNT_KEY) ; - delay (5000) ; + delay (500) ; } printf (" Done. myCounter: %5d\n", myCounter) ; diff --git a/gpio/Makefile b/gpio/Makefile index 5693c44..729a86b 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -30,7 +30,7 @@ INCLUDE = -I/usr/local/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L/usr/local/lib -LIBS = -lwiringPi +LIBS = -lwiringPi -lpthread # May not need to alter anything below this line ############################################################################### diff --git a/gpio/gpio.1 b/gpio/gpio.1 index c39e5dc..a83cf9f 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -57,6 +57,9 @@ converters on the Gertboard. 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. +It can also control the IO's on the PiFace IO board and load the SPI and I2C +kernel modules if required. + 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. @@ -70,6 +73,8 @@ Output the current version including the board revision of the Raspberry Pi. .TP .B \-g Use the BCM_GPIO pins numbers rather than wiringPi pin numbers. +\fINOTE:\fR The BCM_GPIO pin numbers are always used with the +export and edge commands. .TP .B \-p @@ -183,7 +188,7 @@ SPI digital to analogue converter. The board jumpers need to be in-place to do this operation. -.SH "WiringPi vs. GPIO Pin numbering" +.SH "WiringPi vs. BCM_GPIO Pin numbering" .PP .TS @@ -213,6 +218,12 @@ _ 20 - 31 .TE +Note that "r1" and "r2" above refers to the board revision. Normally +wiringPi detects the correct board revision with use for it's own +numbering scheme, but if you are using a Revision 2 board with some +of the pins which change numbers between revisions you will need +to alter your software. + .SH FILES .TP 2.2i diff --git a/gpio/gpio.c b/gpio/gpio.c index 52fcb6f..7b84abd 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -35,12 +35,14 @@ #include #include +extern int wiringPiDebug ; + #ifndef TRUE # define TRUE (1==1) # define FALSE (1==2) #endif -#define VERSION "1.5" +#define VERSION "1.6" static int wpMode ; @@ -127,7 +129,7 @@ static int moduleLoaded (char *modName) static void _doLoadUsage (char *argv []) { - fprintf (stderr, "Usage: %s load \n", argv [0]) ; + fprintf (stderr, "Usage: %s load [bufferSize in KB for spi]\n", argv [0]) ; exit (1) ; } @@ -136,16 +138,24 @@ static void doLoad (int argc, char *argv []) char *module1, *module2 ; char cmd [80] ; char *file1, *file2 ; + char spiBuf [32] ; - if (argc != 3) + if (argc < 3) _doLoadUsage (argv) ; + spiBuf [0] = 0 ; + /**/ if (strcasecmp (argv [2], "spi") == 0) { module1 = "spidev" ; module2 = "spi_bcm2708" ; file1 = "/dev/spidev0.0" ; file2 = "/dev/spidev0.1" ; + if (argc == 4) + sprintf (spiBuf, " bufsize=%d", atoi (argv [3]) * 1024) ; + else if (argc > 4) + _doLoadUsage (argv) ; + } else if (strcasecmp (argv [2], "i2c") == 0) { @@ -159,7 +169,7 @@ static void doLoad (int argc, char *argv []) if (!moduleLoaded (module1)) { - sprintf (cmd, "modprobe %s", module1) ; + sprintf (cmd, "modprobe %s%s", module1, spiBuf) ; system (cmd) ; } @@ -848,6 +858,12 @@ int main (int argc, char *argv []) { int i ; + if (getenv ("WIRINGPI_DEBUG") != NULL) + { + printf ("gpio: wiringPi debug mode enabled\n") ; + wiringPiDebug = TRUE ; + } + if (argc == 1) { fprintf (stderr, "%s\n", usage) ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index e18a654..62e9d9b 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -1,4 +1,4 @@ -# +# ; # Makefile: # wiringPi - Wiring Compatable library for the Raspberry Pi # @@ -45,13 +45,15 @@ LIBS = SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ gertboard.c \ piNes.c \ - lcd.c piHiPri.c piThread.c wiringPiSPI.c \ + lcd.c piHiPri.c piThread.c \ + wiringPiSPI.c wiringPiI2C.c \ softPwm.c softServo.c softTone.c OBJ = $(SRC:.c=.o) -all: $(STATIC) $(DYNAMIC) -#all: $(DYNAMIC) +all: $(DYNAMIC) + +static: $(STATIC) $(STATIC): $(OBJ) @echo "[Link (Static)]" @@ -77,7 +79,7 @@ tags: $(SRC) @ctags $(SRC) .PHONEY: install -install: $(TARGET) +install: $(DYNAMIC) @echo "[Install]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib @install -m 0755 -d $(DESTDIR)$(PREFIX)/include @@ -91,12 +93,17 @@ install: $(TARGET) @install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include @install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include - @install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib + @install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include @install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so.1 @ldconfig +.PHONEY: install-static +install-static: $(STATIC) + @echo "[Install Static]" + @install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib + .PHONEY: uninstall uninstall: @echo "[UnInstall]" @@ -110,6 +117,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/softTone.h @rm -f $(DESTDIR)$(PREFIX)/include/lcd.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h + @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.* @ldconfig diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index df4d969..cfb6705 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -65,10 +65,12 @@ #include #include #include +#include #include #include #include #include +#include #include "wiringPi.h" @@ -173,9 +175,17 @@ static volatile uint32_t *pads ; static volatile uint32_t *timer ; static volatile uint32_t *timerIrqRaw ; +// Time for easy calculations + +static unsigned long long epoch ; + +// Misc + +static int wiringPiMode = WPI_MODE_UNINITIALISED ; + // Debugging -static int wiringPiDebug = FALSE ; +int wiringPiDebug = FALSE ; // The BCM2835 has 54 GPIO pins. // BCM2835 data sheet, Page 90 onwards. @@ -200,6 +210,11 @@ static int wiringPiDebug = FALSE ; static int sysFds [64] ; +// ISR Data + +static void (*isrFunctions [64])(void) ; + + // 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! @@ -370,10 +385,6 @@ static uint8_t gpioToPwmPort [] = } ; -// Time for easy calculations - -static unsigned long long epoch ; - /* * Functions ********************************************************************************* @@ -418,6 +429,15 @@ int wpiPinToGpio (int wpiPin) ********************************************************************************* */ +static void piBoardRevOops (char *why) +{ + fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; + fprintf (stderr, " -> %s\n", why) ; + fprintf (stderr, " -> You may want to check:\n") ; + fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ; + exit (EXIT_FAILURE) ; +} + int piBoardRev (void) { FILE *cpuFd ; @@ -440,24 +460,19 @@ int piBoardRev (void) fclose (cpuFd) ; if (line == NULL) - { - fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; - fprintf (stderr, " (No \"Revision\" line)\n") ; - errno = 0 ; - return -1 ; - } + piBoardRevOops ("No \"Revision\" line") ; + + line [strlen (line) - 1] = 0 ; // Chomp LF + if (wiringPiDebug) + printf ("piboardRev: Revision string: %s\n", line) ; + for (c = line ; *c ; ++c) if (isdigit (*c)) break ; if (!isdigit (*c)) - { - fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; - fprintf (stderr, " (No numeric revision string in: \"%s\"\n", line) ; - errno = 0 ; - return -1 ; - } + piBoardRevOops ("No numeric revision string") ; // If you have overvolted the Pi, then it appears that the revision // has 100000 added to it! @@ -466,26 +481,18 @@ int piBoardRev (void) if (strlen (c) != 4) printf ("piboardRev: This Pi has/is overvolted!\n") ; - lastChar = c [strlen (c) - 2] ; + lastChar = line [strlen (line) - 1] ; + + if (wiringPiDebug) + printf ("piboardRev: lastChar is: '%c' (%d, 0x%02X)\n", lastChar, lastChar, lastChar) ; /**/ if ((lastChar == '2') || (lastChar == '3')) boardRev = 1 ; else boardRev = 2 ; -#ifdef DO_WE_CARE_ABOUT_THIS_NOW - else - { - fprintf (stderr, "WARNING: wiringPi: Unable to determine board revision from \"%d\"\n", r) ; - fprintf (stderr, " -> You may want to check:\n") ; - fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ; - fprintf (stderr, " -> Assuming a Rev 1 board\n") ; - boardRev = 1 ; - } -#endif - if (wiringPiDebug) - printf ("piboardRev: Revision string: %s, board revision: %d\n", c, boardRev) ; + printf ("piBoardRev: Returning revision: %d\n", boardRev) ; return boardRev ; } @@ -741,11 +748,11 @@ void digitalWriteByteGpio (int value) else pinSet |= (1 << pinToGpio [pin]) ; - *(gpio + gpioToGPCLR [0]) = pinClr ; - *(gpio + gpioToGPSET [0]) = pinSet ; - mask <<= 1 ; } + + *(gpio + gpioToGPCLR [0]) = pinClr ; + *(gpio + gpioToGPSET [0]) = pinSet ; } void digitalWriteByteSys (int value) @@ -943,6 +950,99 @@ int waitForInterruptGpio (int pin, int mS) } +/* + * interruptHandler: + * This is a thread and gets started to wait for the interrupt we're + * hoping to catch. It will call the user-function when the interrupt + * fires. + ********************************************************************************* + */ + +static void *interruptHandler (void *arg) +{ + int myPin = *(int *)arg ; + + (void)piHiPri (55) ; // Only effective if we run as root + + for (;;) + if (waitForInterruptSys (myPin, -1) > 0) + isrFunctions [myPin] () ; + + return NULL ; +} + + +/* + * wiringPiISR: + * Take the details and create an interrupt handler that will do a call- + * back to the user supplied function. + ********************************************************************************* + */ + +int wiringPiISR (int pin, int mode, void (*function)(void)) +{ + pthread_t threadId ; + char fName [64] ; + char *modeS ; + char pinS [8] ; + pid_t pid ; + + pin &= 63 ; + + if (wiringPiMode == WPI_MODE_UNINITIALISED) + { + fprintf (stderr, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ; + exit (EXIT_FAILURE) ; + } + else if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + +// Now export the pin and set the right edge +// We're going to use the gpio program to do this, so it assumes +// a full installation of wiringPi. It's a bit 'clunky', but it +// is a way that will work when we're running in "Sys" mode, as +// a non-root user. (without sudo) + + if (mode != INT_EDGE_SETUP) + { + /**/ if (mode == INT_EDGE_FALLING) + modeS = "falling" ; + else if (mode == INT_EDGE_RISING) + modeS = "rising" ; + else + modeS = "both" ; + + sprintf (pinS, "%d", pin) ; + + if ((pid = fork ()) < 0) // Fail + return pid ; + + if (pid == 0) // Child, exec + { + execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ; + return -1 ; // Failure ... + } + else // Parent, wait + wait (NULL) ; + } + +// Now pre-open the /sys/class node - it may already be open if +// we had set it up earlier, but this will do no harm. + + sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; + if ((sysFds [pin] = open (fName, O_RDWR)) < 0) + return -1 ; + + isrFunctions [pin] = function ; + + pthread_create (&threadId, NULL, interruptHandler, &pin) ; + + delay (1) ; + + return 0 ; +} + + /* * delay: * Wait for some number of milli seconds @@ -1064,8 +1164,17 @@ int wiringPiSetup (void) uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ; struct timeval tv ; + if (geteuid () != 0) + { + fprintf (stderr, "Must be root to call wiringPiSetup(). (Did you forget sudo?)\n") ; + exit (EXIT_FAILURE) ; + } + if (getenv ("WIRINGPI_DEBUG") != NULL) + { + printf ("wiringPi: Debug mode enabled\n") ; wiringPiDebug = TRUE ; + } if (wiringPiDebug) printf ("wiringPi: wiringPiSetup called\n") ; @@ -1083,8 +1192,7 @@ int wiringPiSetup (void) pwmSetRange = pwmSetRangeWPi ; pwmSetClock = pwmSetClockWPi ; - if ((boardRev = piBoardRev ()) < 0) - return -1 ; + boardRev = piBoardRev () ; if (boardRev == 1) pinToGpio = pinToGpioR1 ; @@ -1105,7 +1213,8 @@ int wiringPiSetup (void) if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { - fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ; return -1 ; } @@ -1118,7 +1227,8 @@ int wiringPiSetup (void) if ((int32_t)gpio < 0) { - fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ; return -1 ; } @@ -1126,7 +1236,8 @@ int wiringPiSetup (void) if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { - fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ; return -1 ; } @@ -1137,7 +1248,8 @@ int wiringPiSetup (void) if ((int32_t)pwm < 0) { - fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ; return -1 ; } @@ -1145,7 +1257,8 @@ int wiringPiSetup (void) if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { - fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ; return -1 ; } @@ -1156,7 +1269,8 @@ int wiringPiSetup (void) if ((int32_t)clk < 0) { - fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ; return -1 ; } @@ -1164,7 +1278,8 @@ int wiringPiSetup (void) if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { - fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ; return -1 ; } @@ -1175,7 +1290,8 @@ int wiringPiSetup (void) if ((int32_t)pads < 0) { - fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ; return -1 ; } @@ -1188,7 +1304,8 @@ int wiringPiSetup (void) if ((timerMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { - fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ; return -1 ; } @@ -1199,7 +1316,8 @@ int wiringPiSetup (void) if ((int32_t)timer < 0) { - fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ; return -1 ; } @@ -1216,6 +1334,8 @@ int wiringPiSetup (void) gettimeofday (&tv, NULL) ; epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + wiringPiMode = WPI_MODE_PINS ; + return 0 ; } @@ -1233,12 +1353,18 @@ int wiringPiSetupGpio (void) { int x ; - if (wiringPiDebug) - printf ("wiringPi: wiringPiSetupGpio called\n") ; + if (geteuid () != 0) + { + fprintf (stderr, "Must be root to call wiringPiSetupGpio(). (Did you forget sudo?)\n") ; + exit (EXIT_FAILURE) ; + } if ((x = wiringPiSetup ()) < 0) return x ; + if (wiringPiDebug) + printf ("wiringPi: wiringPiSetupGpio called\n") ; + pinMode = pinModeGpio ; pullUpDnControl = pullUpDnControlGpio ; digitalWrite = digitalWriteGpio ; @@ -1252,6 +1378,8 @@ int wiringPiSetupGpio (void) pwmSetRange = pwmSetRangeWPi ; pwmSetClock = pwmSetClockWPi ; + wiringPiMode = WPI_MODE_GPIO ; + return 0 ; } @@ -1272,6 +1400,9 @@ int wiringPiSetupSys (void) struct timeval tv ; char fName [128] ; + if (getenv ("WIRINGPI_DEBUG") != NULL) + wiringPiDebug = TRUE ; + if (wiringPiDebug) printf ("wiringPi: wiringPiSetupSys called\n") ; @@ -1288,15 +1419,13 @@ int wiringPiSetupSys (void) pwmSetRange = pwmSetRangeSys ; pwmSetClock = pwmSetClockSys ; - if ((boardRev = piBoardRev ()) < 0) - return -1 ; + boardRev = piBoardRev () ; if (boardRev == 1) pinToGpio = pinToGpioR1 ; else pinToGpio = pinToGpioR2 ; - // Open and scan the directory, looking for exported GPIOs, and pre-open // the 'value' interface to speed things up for later @@ -1311,5 +1440,7 @@ int wiringPiSetupSys (void) gettimeofday (&tv, NULL) ; epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + wiringPiMode = WPI_MODE_GPIO_SYS ; + return 0 ; } diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 6a7278e..424e3bc 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -23,32 +23,44 @@ // Handy defines +// Deprecated #define NUM_PINS 17 #define WPI_MODE_PINS 0 #define WPI_MODE_GPIO 1 #define WPI_MODE_GPIO_SYS 2 #define WPI_MODE_PIFACE 3 +#define WPI_MODE_UNINITIALISED -1 -#define INPUT 0 -#define OUTPUT 1 -#define PWM_OUTPUT 2 +#define INPUT 0 +#define OUTPUT 1 +#define PWM_OUTPUT 2 -#define LOW 0 -#define HIGH 1 +#define LOW 0 +#define HIGH 1 -#define PUD_OFF 0 -#define PUD_DOWN 1 -#define PUD_UP 2 +#define PUD_OFF 0 +#define PUD_DOWN 1 +#define PUD_UP 2 // PWM -#define PWM_MODE_MS 0 -#define PWM_MODE_BAL 1 +#define PWM_MODE_MS 0 +#define PWM_MODE_BAL 1 + +// Interrupt levels + +#define INT_EDGE_SETUP 0 +#define INT_EDGE_FALLING 1 +#define INT_EDGE_RISING 2 + +// Threads + +#define PI_THREAD(X) void *X (void *dummy) // Function prototypes -// c++ wrappers thanks to a commend by Nick Lott +// c++ wrappers thanks to a comment by Nick Lott // (and others on the Raspberry Pi forums) #ifdef __cplusplus @@ -82,11 +94,10 @@ extern void (*pwmSetClock) (int divisor) ; // Interrupts extern int (*waitForInterrupt) (int pin, int mS) ; +extern int wiringPiISR (int pin, int mode, void (*function)(void)) ; // 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) ; diff --git a/wiringPi/wiringPiI2C.c b/wiringPi/wiringPiI2C.c new file mode 100644 index 0000000..93fe1d3 --- /dev/null +++ b/wiringPi/wiringPiI2C.c @@ -0,0 +1,122 @@ +/* + * wiringPiI2C.c: + * Simplified I2C access routines + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include +#include + +#include "wiringPi.h" +#include "wiringPiI2C.h" + + +/* + * wiringPiI2CRead: + * Simple device read + ********************************************************************************* + */ + +int wiringPiI2CRead (int fd) +{ + return i2c_smbus_read_byte (fd) ; +} + + +/* + * wiringPiI2CReadReg8: wiringPiI2CReadReg16: + * Read an 8 or 16-bit value from a regsiter on the device + ********************************************************************************* + */ + +int wiringPiI2CReadReg8 (int fd, int reg) +{ + return i2c_smbus_read_byte_data (fd, reg) ; +} + +int wiringPiI2CReadReg16 (int fd, int reg) +{ + return i2c_smbus_read_word_data (fd, reg) ; +} + + +/* + * wiringPiI2CWrite: + * Simple device write + ********************************************************************************* + */ + +int wiringPiI2CWrite (int fd, int data) +{ + return i2c_smbus_write_byte (fd, data) ; +} + + +/* + * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16: + * Write an 8 or 16-bit value to the given register + ********************************************************************************* + */ + +int wiringPiI2CWriteReg8 (int fd, int reg, int data) +{ + return i2c_smbus_write_byte_data (fd, reg, data) ; +} + +int wiringPiI2CWriteReg16 (int fd, int reg, int data) +{ + return i2c_smbus_write_word_data (fd, reg, data) ; +} + + +/* + * wiringPiI2CSetup: + * Open the I2C device, and regsiter the target device + ********************************************************************************* + */ + +int wiringPiI2CSetup (int devId) +{ + int rev, fd ; + char *device ; + + if ((rev = piBoardRev ()) < 0) + { + fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ; + exit (1) ; + } + + if (rev == 1) + device = "/dev/i2c-0" ; + else + device = "/dev/i2c-1" ; + + if ((fd = open (device, O_RDWR)) < 0) + return -1 ; + + if (ioctl (fd, I2C_SLAVE, devId) < 0) + return -1 ; + + return fd ; +} diff --git a/wiringPi/wiringPiI2C.h b/wiringPi/wiringPiI2C.h new file mode 100644 index 0000000..6710ff4 --- /dev/null +++ b/wiringPi/wiringPiI2C.h @@ -0,0 +1,41 @@ +/* + * wiringPiI2C.h: + * Simplified I2C access routines + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int wiringPiI2CRead (int fd) ; +extern int wiringPiI2CReadReg8 (int fd, int reg) ; +extern int wiringPiI2CReadReg16 (int fd, int reg) ; + +extern int wiringPiI2CWrite (int fd, int data) ; +extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ; +extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ; + +int wiringPiI2CSetup (int devId) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/wiringPiISR.c b/wiringPi/wiringPiISR.c new file mode 100644 index 0000000..9e847cc --- /dev/null +++ b/wiringPi/wiringPiISR.c @@ -0,0 +1,109 @@ +/* + * wiringPiISR.c: + * Simplified Interrupt Service Routine handling + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#include "wiringPi.h" + + + +static void (*isrFunctions [64])(void) ; +static int isrFds [64] ; + +/* + * interruptHandler: + * This is a thread and gets started to wait for the interrupt we're + * hoping to catch. It will call the user-function when the interrupt + * fires. + ********************************************************************************* + */ + +static void *interruptHandler (void *arg) +{ + int pin = *(int *)arg ; + + (void)piHiPri (55) ; + + for (;;) + { + if (waitForInterrupt (pin, -1) > 0) + isrFunctions [pin] () ; + } + + return NULL ; +} + +/* + * wiringPiISR: + * Take the details and create an interrupt handler that will do a call- + * back to the user supplied function. + ********************************************************************************* + */ + +int wiringPiISR (int pin, int mode, void (*function)(void)) +{ + pthread_t threadId ; + char command [64] ; + + pin &= 63 ; + + if (wiringPiMode == WPI_MODE_UNINITIALISED) + { + fprintf (stderr, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ; + exit (EXIT_FAILURE) ; + } + else if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + + + isrFunctions [pin] = function ; + +// Now export the pin and set the right edge + + if (mode != INT_EDGE_SETUP) + { + /**/ if (mode == INT_EDGE_FALLING) + modes = "falling" ; + else if (mode == INT_EDGE_RISING) + modes = "rising" ; + else + modes = "both" ; + + sprintf (command, "/usr/local/bin/gpio edge %d %s", pin, modes) ; + system (command) ; + } + + sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; + if ((isrFds [pin] = open (fName, O_RDWR)) < 0) + return -1 ; + + { + fprintf ("std + + pthread_create (&threadId, NULL, interruptHandler, &pin) ; +} + + From c121349a7bbf8e9c8b30b03661836effbde2b373 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 15 Jan 2013 15:30:26 +0000 Subject: [PATCH 27/97] Minor changes to messages being printed from wiringPiSetup() Changed built to work marginally better. --- build | 1 + wiringPi/wiringPi.c | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/build b/build index 740b512..9eeb4df 100755 --- a/build +++ b/build @@ -26,6 +26,7 @@ else echo echo "WiringPi library" cd wiringPi + sudo make uninstall make sudo make install echo diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index cfb6705..066f842 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1166,7 +1166,7 @@ int wiringPiSetup (void) if (geteuid () != 0) { - fprintf (stderr, "Must be root to call wiringPiSetup(). (Did you forget sudo?)\n") ; + fprintf (stderr, "wiringPi:\n Must be root to call wiringPiSetup().\n (Did you forget sudo?)\n") ; exit (EXIT_FAILURE) ; } @@ -1203,7 +1203,8 @@ int wiringPiSetup (void) if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) { - fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; + if (wiringPiDebug) + fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; return -1 ; } From 4673e38b1f11813b8d9bb2a2ab775aa2dd5c575e Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 15 Jan 2013 15:45:32 +0000 Subject: [PATCH 28/97] Change to the build script to check for the presence of the I2C development libraries. Bumped gpio version. --- build | 15 +++++++++++++++ gpio/gpio.c | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/build b/build index 9eeb4df..ad1eff0 100755 --- a/build +++ b/build @@ -23,7 +23,22 @@ elif [ x$1 = "xuninstall" ]; then cd .. else echo wiringPi Build script - please wait... + echo + if [ ! -f /usr/include/linux/i2c-dev.h ]; then + echo "* wiringPi needs the I2C Development Libraires installing." + echo "" + echo "If using Debian/Raspbian, then type this command:" + echo " sudo apt-get install libi2c-dev" + echo "then run ./build again." + echo "" + echo "If using another Linux distribution, then you will have to" + echo "work out how to install the I2C Developmen Libraries for your" + echo "system." + echo "" + exit 1 + fi + echo "WiringPi library" cd wiringPi sudo make uninstall diff --git a/gpio/gpio.c b/gpio/gpio.c index 7b84abd..6615133 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -42,7 +42,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "1.6" +#define VERSION "1.7" static int wpMode ; From c82fb8735d1563d00903cb69f73f99d0a44998dc Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 15 Jan 2013 22:38:21 +0000 Subject: [PATCH 29/97] Updated the build script to better try to detect lack of i2c-dev (hopefully!) Also updated all the mmap code in wiringPiSetup() to make it a bit more sane and efficient to a degree. --- People | 6 ++++ build | 55 +++++++++++++++++++++++++-------- wiringPi/wiringPi.c | 75 +++++---------------------------------------- 3 files changed, 56 insertions(+), 80 deletions(-) diff --git a/People b/People index e0c262c..35f8a4c 100644 --- a/People +++ b/People @@ -13,3 +13,9 @@ Chris McSweeny inside the dealyMicrosecondsHard() function. And spotting a couple of schoolboy errors in the (experimental) softServo code, prompting me to completely re-write it. + +Armin (Via projects website) + Some pointers about the i2c-dev.h files. + +Arno Wagner + Suggestions for the mmap calls in wiringPiSetup() diff --git a/build b/build index ad1eff0..cc6804a 100755 --- a/build +++ b/build @@ -1,5 +1,34 @@ #!/bin/bash +i2c-install() +{ + echo "* wiringPi needs the I2C Development Libraires installing." + echo "" + echo "If using Debian/Raspbian, then type this command:" + echo " sudo apt-get install libi2c-dev" + echo "then run ./build again." + echo "" + echo "If using another Linux distribution, then you will have to" + echo "work out how to install the I2C Developmen Libraries for your" + echo "system. (Sorry - I don't know - do let me know though!)" + echo "" + exit 1 +} + +check-make-ok() +{ + if [ $? != 0 ]; then + echo "" + echo "Make Failed..." + echo "Please check the messages and fix any problems. If you're still stuck," + echo "then please email all the output and as many details as you can to" + echo " projects@drogon.net" + echo "" + exit 1 + fi +} + + if [ x$1 = "xclean" ]; then echo Cleaning echo @@ -23,32 +52,32 @@ elif [ x$1 = "xuninstall" ]; then cd .. else echo wiringPi Build script - please wait... - echo + +# Check for I2C being installed... + if [ ! -f /usr/include/linux/i2c-dev.h ]; then - echo "* wiringPi needs the I2C Development Libraires installing." - echo "" - echo "If using Debian/Raspbian, then type this command:" - echo " sudo apt-get install libi2c-dev" - echo "then run ./build again." - echo "" - echo "If using another Linux distribution, then you will have to" - echo "work out how to install the I2C Developmen Libraries for your" - echo "system." - echo "" - exit 1 + i2c-install + fi + grep -q i2c_smbus_read_byte /usr/include/linux/i2c-dev.h + if [ $? = 1 ]; then + i2c-install fi echo "WiringPi library" cd wiringPi sudo make uninstall - make + make + check-make-ok sudo make install + check-make-ok echo echo "GPIO Utility" cd ../gpio make + check-make-ok sudo make install + check-make-ok echo echo "Examples" cd ../examples diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 066f842..36c49f5 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1161,7 +1161,7 @@ int wiringPiSetup (void) { int fd ; int boardRev ; - uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ; + //uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ; struct timeval tv ; if (geteuid () != 0) @@ -1210,23 +1210,8 @@ int wiringPiSetup (void) // GPIO: -// Allocate 2 pages - 1 ... - - if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) - { - if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ; - return -1 ; - } - -// ... presumably to make sure we can round it up to a whole page size - - if (((uint32_t)gpioMem % PAGE_SIZE) != 0) - gpioMem += PAGE_SIZE - ((uint32_t)gpioMem % PAGE_SIZE) ; - - gpio = (uint32_t *)mmap((caddr_t)gpioMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_BASE) ; - - if ((int32_t)gpio < 0) + gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ; + if ((int32_t)gpio == -1) { if (wiringPiDebug) fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ; @@ -1235,19 +1220,8 @@ int wiringPiSetup (void) // PWM - if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) - { - if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ; - return -1 ; - } - - if (((uint32_t)pwmMem % PAGE_SIZE) != 0) - pwmMem += PAGE_SIZE - ((uint32_t)pwmMem % PAGE_SIZE) ; - - pwm = (uint32_t *)mmap(pwmMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PWM) ; - - if ((int32_t)pwm < 0) + pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ; + if ((int32_t)pwm == -1) { if (wiringPiDebug) fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ; @@ -1256,18 +1230,7 @@ int wiringPiSetup (void) // Clock control (needed for PWM) - if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) - { - if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ; - return -1 ; - } - - if (((uint32_t)clkMem % PAGE_SIZE) != 0) - clkMem += PAGE_SIZE - ((uint32_t)clkMem % PAGE_SIZE) ; - - clk = (uint32_t *)mmap(clkMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, CLOCK_BASE) ; - + clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ; if ((int32_t)clk < 0) { if (wiringPiDebug) @@ -1277,18 +1240,7 @@ int wiringPiSetup (void) // The drive pads - if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) - { - if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ; - return -1 ; - } - - if (((uint32_t)padsMem % PAGE_SIZE) != 0) - padsMem += PAGE_SIZE - ((uint32_t)padsMem % PAGE_SIZE) ; - - pads = (uint32_t *)mmap(padsMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PADS) ; - + pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ; if ((int32_t)pads < 0) { if (wiringPiDebug) @@ -1303,18 +1255,7 @@ int wiringPiSetup (void) // The system timer - if ((timerMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) - { - if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ; - return -1 ; - } - - if (((uint32_t)timerMem % PAGE_SIZE) != 0) - timerMem += PAGE_SIZE - ((uint32_t)timerMem % PAGE_SIZE) ; - - timer = (uint32_t *)mmap(timerMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_TIMER) ; - + timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ; if ((int32_t)timer < 0) { if (wiringPiDebug) From be04c1bd52fbc787eb741a2a87fcebe40f571a0d Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 19 Jan 2013 10:31:14 +0000 Subject: [PATCH 30/97] Changed the build system to drop I2C for now. Seems to cause too many issues on non Raspbian systems (and even on some Raspbian systems it would appear ): fixed a timing issue on softTone fixed some issues in wiringPisetup introduced when optimising the mmap calls. --- People | 3 +++ README.TXT | 26 ++++++++++++++++++++++++++ build | 22 +++++++++++----------- examples/Makefile | 17 ++++++++++++----- gpio/Makefile | 2 +- gpio/gpio.c | 2 +- wiringPi/Makefile | 2 +- wiringPi/softServo.c | 9 +++++++++ wiringPi/softTone.c | 4 +++- wiringPi/wiringPi.c | 42 +++++++++++++++++++++++++++++++++--------- wiringPi/wiringPi.h | 1 + 11 files changed, 101 insertions(+), 29 deletions(-) create mode 100644 README.TXT diff --git a/People b/People index 35f8a4c..f5645be 100644 --- a/People +++ b/People @@ -19,3 +19,6 @@ Armin (Via projects website) Arno Wagner Suggestions for the mmap calls in wiringPiSetup() + +CHARLES Thibaut: + A small issue in softTone diff --git a/README.TXT b/README.TXT new file mode 100644 index 0000000..0fce86a --- /dev/null +++ b/README.TXT @@ -0,0 +1,26 @@ + +wiringPi README +=============== + +Please note that the official way to get wiringPi is via git from +git.drogon.net and not GitHub. + +ie. + + git clone git://git.drogon.net/wiringPi + +The version of wiringPi held on GitHub by "Gadgetoid" is used to build the +wiringPython, Ruby, Perl, etc. wrappers for these other languages. This +version may lag the official Drogon release. Pull requests may not be +accepted to Github.... + +Please see + + https://projects.drogon.net/raspberry-pi/wiringpi/ + +for the official documentation, etc. and the best way to submit bug reports, etc. +is by sending an email to projects@drogon.net + +Thanks! + + -Gordon diff --git a/build b/build index cc6804a..2afafa6 100755 --- a/build +++ b/build @@ -2,17 +2,17 @@ i2c-install() { - echo "* wiringPi needs the I2C Development Libraires installing." - echo "" - echo "If using Debian/Raspbian, then type this command:" - echo " sudo apt-get install libi2c-dev" - echo "then run ./build again." - echo "" - echo "If using another Linux distribution, then you will have to" - echo "work out how to install the I2C Developmen Libraries for your" - echo "system. (Sorry - I don't know - do let me know though!)" - echo "" - exit 1 + echo "* wiringPi needs the I2C Development Libraires installing." + echo "" + echo "If using Debian/Raspbian, then type this command:" + echo " sudo apt-get install libi2c-dev" + echo "then run ./build again." + echo "" + echo "If using another Linux distribution, then you will have to" + echo "work out how to install the I2C Developmen Libraries for your" + echo "system. (Sorry - I don't know - do let me know though!)" + echo "" + exit 1 } check-make-ok() diff --git a/examples/Makefile b/examples/Makefile index 3607fc8..e1d29a0 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -30,7 +30,7 @@ INCLUDE = -I/usr/local/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L/usr/local/lib -LDLIBS = -lwiringPi +LDLIBS = -lwiringPi -lpthread -lm # Should not alter anything below this line ############################################################################### @@ -44,6 +44,13 @@ OBJ = $(SRC:.c=.o) BINS = $(SRC:.c=) +# Note: +# Please don't waste your time by emailling me or doing a +# pull request with changes to make all these targets. It +# is intentional that I do it this way as it now takes too +# long to compile them all and most people will not run +# them anyway... -GH- + all: @cat README.TXT @echo " $(BINS)" | fmt @@ -75,19 +82,19 @@ isr: isr.o piface: piface.o @echo [link] - @$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS) -lpthread + @$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS) gertboard: gertboard.o @echo [link] - @$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS) -lm + @$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS) nes: nes.o @echo [link] - @$(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS) -lm + @$(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS) pwm: pwm.o @echo [link] - @$(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS) -lm -lpthread + @$(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS) delayTest: delayTest.o @echo [link] diff --git a/gpio/Makefile b/gpio/Makefile index 729a86b..623096c 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -30,7 +30,7 @@ INCLUDE = -I/usr/local/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L/usr/local/lib -LIBS = -lwiringPi -lpthread +LIBS = -lwiringPi -lpthread -lm # May not need to alter anything below this line ############################################################################### diff --git a/gpio/gpio.c b/gpio/gpio.c index 6615133..326dd2d 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -42,7 +42,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "1.7" +#define VERSION "1.8" static int wpMode ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 62e9d9b..0ba690f 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -46,7 +46,7 @@ SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ gertboard.c \ piNes.c \ lcd.c piHiPri.c piThread.c \ - wiringPiSPI.c wiringPiI2C.c \ + wiringPiSPI.c \ softPwm.c softServo.c softTone.c OBJ = $(SRC:.c=.o) diff --git a/wiringPi/softServo.c b/wiringPi/softServo.c index a6ff1fb..9de9f4f 100644 --- a/wiringPi/softServo.c +++ b/wiringPi/softServo.c @@ -54,6 +54,15 @@ // the multipexing, but it does need to be at least 10mS, and preferably 16 // from what I've been able to determine. +// WARNING: +// This code is really experimental. It was written in response to some people +// asking for a servo driver, however while it works, there is too much +// jitter to successfully drive a small servo - I have tried it with a micro +// servo and it worked, but the servo ran hot due to the jitter in the signal +// being sent to it. +// +// If you want servo control for the Pi, then use the servoblaster kernel +// module. #define MAX_SERVOS 8 diff --git a/wiringPi/softTone.c b/wiringPi/softTone.c index d14c2a2..8463627 100644 --- a/wiringPi/softTone.c +++ b/wiringPi/softTone.c @@ -59,7 +59,9 @@ static PI_THREAD (softToneThread) for (;;) { frewq = frewqs [pin] ; - if (frewq != 0) + if (frewq == 0) + delay (1) ; + else { halfPeriod = 500000 / frewq ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 36c49f5..9655db2 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1204,7 +1204,11 @@ int wiringPiSetup (void) if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) { if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; + { + int serr = errno ; + fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; + errno = serr ; + } return -1 ; } @@ -1214,7 +1218,11 @@ int wiringPiSetup (void) if ((int32_t)gpio == -1) { if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ; + { + int serr = errno ; + fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ; + errno = serr ; + } return -1 ; } @@ -1224,27 +1232,39 @@ int wiringPiSetup (void) if ((int32_t)pwm == -1) { if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ; + { + int serr = errno ; + fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ; + errno = serr ; + } return -1 ; } // Clock control (needed for PWM) clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ; - if ((int32_t)clk < 0) + if ((int32_t)clk == -1) { if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ; + { + int serr = errno ; + fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ; + errno = serr ; + } return -1 ; } // The drive pads pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ; - if ((int32_t)pads < 0) + if ((int32_t)pads == -1) { if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ; + { + int serr = errno ; + fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ; + errno = serr ; + } return -1 ; } @@ -1256,10 +1276,14 @@ int wiringPiSetup (void) // The system timer timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ; - if ((int32_t)timer < 0) + if ((int32_t)timer == -1) { if (wiringPiDebug) - fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ; + { + int serr = errno ; + fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ; + errno = serr ; + } return -1 ; } diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 424e3bc..7626d28 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -53,6 +53,7 @@ #define INT_EDGE_SETUP 0 #define INT_EDGE_FALLING 1 #define INT_EDGE_RISING 2 +#define INT_EDGE_BOTH 3 // Threads From 95342e1f8338aeddbf1c8ce0a1b6d7d0e6f91cbb Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 19 Jan 2013 15:10:48 +0000 Subject: [PATCH 31/97] changed build to not do the I2C checks. --- build | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/build b/build index 2afafa6..666ab87 100755 --- a/build +++ b/build @@ -56,13 +56,13 @@ else # Check for I2C being installed... - if [ ! -f /usr/include/linux/i2c-dev.h ]; then - i2c-install - fi - grep -q i2c_smbus_read_byte /usr/include/linux/i2c-dev.h - if [ $? = 1 ]; then - i2c-install - fi +# if [ ! -f /usr/include/linux/i2c-dev.h ]; then +# i2c-install +# fi +# grep -q i2c_smbus_read_byte /usr/include/linux/i2c-dev.h +# if [ $? = 1 ]; then +# i2c-install +# fi echo "WiringPi library" cd wiringPi From db925cea180cf3f67ee13e0a5a0cd2fc9d3ce564 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 19 Jan 2013 21:46:13 +0000 Subject: [PATCH 32/97] Updated the build & makefile to allow for building the I2C libraries again - but only if the right headers are installed! --- INSTALL | 12 +++++++++ build | 64 ++++++++++++++++++++++------------------------- wiringPi/Makefile | 17 ++++++++++--- 3 files changed, 56 insertions(+), 37 deletions(-) diff --git a/INSTALL b/INSTALL index 8e0c43c..8a6d38e 100644 --- a/INSTALL +++ b/INSTALL @@ -30,6 +30,18 @@ To un-install wiringPi: ./build uninstall + +I2C: + +If your system has the correct i2c-dev libraries and headers installed, +then the I2C helpers will be compiled into wiringPi. If you want to +use the I2C helpers and don't have them installed, then under Raspbian, +issue the command: + + sudo apt-get install libi2c-dev + +Consult the documentation for your system if you are not running Raspbian. + Gordon Henderson projects@drogon.net diff --git a/build b/build index 666ab87..cbb1a4f 100755 --- a/build +++ b/build @@ -1,20 +1,5 @@ #!/bin/bash -i2c-install() -{ - echo "* wiringPi needs the I2C Development Libraires installing." - echo "" - echo "If using Debian/Raspbian, then type this command:" - echo " sudo apt-get install libi2c-dev" - echo "then run ./build again." - echo "" - echo "If using another Linux distribution, then you will have to" - echo "work out how to install the I2C Developmen Libraries for your" - echo "system. (Sorry - I don't know - do let me know though!)" - echo "" - exit 1 -} - check-make-ok() { if [ $? != 0 ]; then @@ -28,7 +13,6 @@ check-make-ok() fi } - if [ x$1 = "xclean" ]; then echo Cleaning echo @@ -38,8 +22,10 @@ if [ x$1 = "xclean" ]; then make clean cd ../examples make clean - cd .. -elif [ x$1 = "xuninstall" ]; then + exit +fi + +if [ x$1 = "xuninstall" ]; then echo Uninstalling echo echo "WiringPi library" @@ -50,27 +36,37 @@ elif [ x$1 = "xuninstall" ]; then cd ../gpio sudo make uninstall cd .. -else - echo wiringPi Build script - please wait... + exit +fi + + + echo "wiringPi Build script" + echo "=====================" echo # Check for I2C being installed... +# ... and if-so, then automatically make the I2C helpers -# if [ ! -f /usr/include/linux/i2c-dev.h ]; then -# i2c-install -# fi -# grep -q i2c_smbus_read_byte /usr/include/linux/i2c-dev.h -# if [ $? = 1 ]; then -# i2c-install -# fi + if [ -f /usr/include/linux/i2c-dev.h ]; then + grep -q i2c_smbus_read_byte /usr/include/linux/i2c-dev.h + if [ $? = 0 ]; then + target=i2c + echo "Building wiringPi with the I2C helper libraries." + else + target=all + echo "The wiringPi I2C helper libraries will not be built." + fi + fi + echo echo "WiringPi library" cd wiringPi sudo make uninstall - make + make $target check-make-ok sudo make install check-make-ok + echo echo "GPIO Utility" cd ../gpio @@ -78,12 +74,12 @@ else check-make-ok sudo make install check-make-ok - echo - echo "Examples" - cd ../examples - make - cd .. -fi + +# echo +# echo "Examples" +# cd ../examples +# make +# cd .. echo echo All Done. diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 0ba690f..c6a4555 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -49,8 +49,12 @@ SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ wiringPiSPI.c \ softPwm.c softServo.c softTone.c +SRC_I2C = wiringPiI2C.c + OBJ = $(SRC:.c=.o) +OBJ_I2C = $(SRC_I2C:.c=.o) + all: $(DYNAMIC) static: $(STATIC) @@ -65,13 +69,17 @@ $(DYNAMIC): $(OBJ) @echo "[Link (Dynamic)]" @$(CC) -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ) +i2c: $(OBJ) $(OBJ_I2C) + @echo "[Link (Dynamic + I2C)]" + @$(CC) -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ) $(OBJ_I2C) + .c.o: @echo [Compile] $< @$(CC) -c $(CFLAGS) $< -o $@ .PHONEY: clean clean: - rm -f $(OBJ) *~ core tags Makefile.bak libwiringPi.* + rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.* .PHONEY: tags tags: $(SRC) @@ -124,7 +132,7 @@ uninstall: .PHONEY: depend depend: - makedepend -Y $(SRC) + makedepend -Y $(SRC) $(SRC_I2C) # DO NOT DELETE @@ -137,5 +145,8 @@ piNes.o: wiringPi.h piNes.h lcd.o: wiringPi.h lcd.h piHiPri.o: wiringPi.h piThread.o: wiringPi.h -softPwm.o: wiringPi.h softPwm.h wiringPiSPI.o: wiringPiSPI.h +softPwm.o: wiringPi.h softPwm.h +softServo.o: wiringPi.h softServo.h +softTone.o: wiringPi.h softTone.h +wiringPiI2C.o: wiringPi.h wiringPiI2C.h From e8f6258004e59b1452806e5c45bb6598d0158dd2 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 28 Jan 2013 13:00:47 +0000 Subject: [PATCH 33/97] Quite a few changes here. Added in generic 'blink' programs in the examples in C, RTB and Shell. Updated wiringPi with a little big-file on the millis() function and added in a new micros() function too. Updated the examples with standard LGPL headers. Added a new isr-osc.c test program - just for ISR timing purposes. --- examples/Makefile | 23 +++--- examples/README.TXT | 6 +- examples/blink.c | 50 +++++++++++++ examples/blink.rtb | 30 ++++++++ examples/blink.sh | 37 +++++++++ examples/delayTest.c | 24 ++++++ examples/gertboard.c | 19 ++++- examples/header.h | 23 ++++++ examples/isr-osc.c | 118 +++++++++++++++++++++++++++++ examples/nes.c | 23 ++++++ examples/okLed.c | 21 +++++- examples/piface.c | 24 +++++- examples/pwm.c | 24 ++++++ examples/serialRead.c | 21 +++++- examples/serialTest.c | 18 +++++ examples/servo.c | 24 ++++++ examples/speed.c | 20 ++++- examples/test1.c | 22 +++++- examples/test2.c | 23 +++++- examples/tone.c | 34 +++++++-- gpio/Makefile | 4 +- gpio/gpio.1 | 20 +++-- gpio/gpio.c | 79 +++++++++----------- wiringPi/lcd.c | 12 +++ wiringPi/lcd.h | 13 ++-- wiringPi/wiringPi.c | 169 +++++++++++++++++++++++++----------------- wiringPi/wiringPi.h | 4 +- 27 files changed, 727 insertions(+), 158 deletions(-) create mode 100644 examples/blink.c create mode 100644 examples/blink.rtb create mode 100644 examples/blink.sh create mode 100644 examples/header.h create mode 100644 examples/isr-osc.c diff --git a/examples/Makefile b/examples/Makefile index e1d29a0..defd510 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -35,27 +35,26 @@ LDLIBS = -lwiringPi -lpthread -lm # Should not alter anything below this line ############################################################################### -SRC = test1.c test2.c speed.c lcd.c wfi.c isr.c \ - piface.c gertboard.c nes.c \ - pwm.c tone.c servo.c \ +SRC = blink.c test1.c test2.c speed.c lcd.c wfi.c isr.c isr-osc.c \ + piface.c gertboard.c nes.c \ + pwm.c tone.c servo.c \ delayTest.c serialRead.c serialTest.c okLed.c OBJ = $(SRC:.c=.o) BINS = $(SRC:.c=) -# Note: -# Please don't waste your time by emailling me or doing a -# pull request with changes to make all these targets. It -# is intentional that I do it this way as it now takes too -# long to compile them all and most people will not run -# them anyway... -GH- - all: @cat README.TXT @echo " $(BINS)" | fmt @echo "" +really-all: $(BINS) + +blink: blink.o + @echo [link] + @$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) + test1: test1.o @echo [link] @$(CC) -o $@ test1.o $(LDFLAGS) $(LDLIBS) @@ -80,6 +79,10 @@ isr: isr.o @echo [link] @$(CC) -o $@ isr.o $(LDFLAGS) $(LDLIBS) +isr-osc: isr-osc.o + @echo [link] + @$(CC) -o $@ isr-osc.o $(LDFLAGS) $(LDLIBS) + piface: piface.o @echo [link] @$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS) diff --git a/examples/README.TXT b/examples/README.TXT index 2bf6f1e..33263b1 100644 --- a/examples/README.TXT +++ b/examples/README.TXT @@ -10,5 +10,9 @@ To compile an individual example, just type make exampleName -Where exampleName is one of: +To really compile everything: + + make really-all + +The individual tests are: diff --git a/examples/blink.c b/examples/blink.c new file mode 100644 index 0000000..bb9f856 --- /dev/null +++ b/examples/blink.c @@ -0,0 +1,50 @@ +/* + * blink.c: + * Standard "blink" program in wiringPi. Blinks an LED connected + * to the first GPIO pin. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include + +// LED Pin - wiringPi pin 0 is BCM_GPIO 17. + +#define LED 0 + +int main (void) +{ + printf ("Raspberry Pi blink\n") ; + + if (wiringPiSetup () == -1) + return 1 ; + + pinMode (LED, OUTPUT) ; + + for (;;) + { + digitalWrite (LED, 1) ; // On + delay (500) ; // mS + digitalWrite (LED, 0) ; // Off + delay (500) ; + } + return 0 ; +} diff --git a/examples/blink.rtb b/examples/blink.rtb new file mode 100644 index 0000000..eb7d26c --- /dev/null +++ b/examples/blink.rtb @@ -0,0 +1,30 @@ +// blink.rtb: +// Blink program in Return to Basic +// +// Copyright (c) 2012-2013 Gordon Henderson. +//********************************************************************** +// This file is part of wiringPi: +// https://projects.drogon.net/raspberry-pi/wiringpi/ +// +// wiringPi is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// wiringPi is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with wiringPi. If not, see . + *********************************************************************** +// +PinMode (0, 1) // Output +CYCLE + DigitalWrite (0, 1) // Pin 0 ON + WAIT (0.5) // 0.5 seconds + DigitalWrite (0, 0) + WAIT (0.5) +REPEAT +END diff --git a/examples/blink.sh b/examples/blink.sh new file mode 100644 index 0000000..2aa378a --- /dev/null +++ b/examples/blink.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# +# blink.sh: +# Standard "blink" program in wiringPi. Blinks an LED connected +# to the first GPIO pin. +# +# Copyright (c) 2012-2013 Gordon Henderson. +####################################################################### +# This file is part of wiringPi: +# https://projects.drogon.net/raspberry-pi/wiringpi/ +# +# wiringPi is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# wiringPi is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with wiringPi. If not, see . +####################################################################### + +# LED Pin - wiringPi pin 0 is BCM_GPIO 17. + +LED=0 + +gpio mode $PIN out + +while true; do + gpio write $PIN 1 + sleep 0.5 + gpio write $PIN 0 + sleep 0.5 +done diff --git a/examples/delayTest.c b/examples/delayTest.c index d05f3ff..4c8b6ca 100644 --- a/examples/delayTest.c +++ b/examples/delayTest.c @@ -1,3 +1,27 @@ +/* + * delayTest.c: + * Just a little test program I'm using to experiment with + * various timings and latency, etc. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ #include #include diff --git a/examples/gertboard.c b/examples/gertboard.c index 8f26dd4..f02e27d 100644 --- a/examples/gertboard.c +++ b/examples/gertboard.c @@ -1,4 +1,3 @@ - /* * gertboard.c: * Simple test for the SPI bus on the Gertboard @@ -10,6 +9,24 @@ * copy this value to D/A port 1 and use a 'scope on both D/A ports * to check all's well. * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** */ #include diff --git a/examples/header.h b/examples/header.h new file mode 100644 index 0000000..82f723d --- /dev/null +++ b/examples/header.h @@ -0,0 +1,23 @@ +/* + * file.c: + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + diff --git a/examples/isr-osc.c b/examples/isr-osc.c new file mode 100644 index 0000000..a872ee3 --- /dev/null +++ b/examples/isr-osc.c @@ -0,0 +1,118 @@ +/* + * isr-osc.c: + * Wait for Interrupt test program - ISR method - interrupt oscillator + * + * How to test: + * + * IMPORTANT: To run this test we connect 2 GPIO pins together, but + * before we do that YOU must make sure that they are both setup + * the right way. If they are set to outputs and one is high and one low, + * then you connect the wire, you'll create a short and that won't be good. + * + * Before making the connection, type: + * gpio mode 0 output + * gpio write 0 0 + * gpio mode 1 input + * then you can connect them together. + * + * Run the program, then: + * gpio write 0 1 + * gpio write 0 0 + * + * at which point it will trigger an interrupt and the program will + * then do the up/down toggling for itself and run at full speed, and + * it will report the number of interrupts recieved every second. + * + * Copyright (c) 2013 Gordon Henderson. projects@drogon.net + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include +#include + + +// What GPIO input are we using? +// This is a wiringPi pin number + +#define OUT_PIN 0 +#define IN_PIN 1 + +// globalCounter: +// Global variable to count interrupts +// Should be declared volatile to make sure the compiler doesn't cache it. + +static volatile int globalCounter = 0 ; + +/* + * myInterrupt: + ********************************************************************************* + */ + +void myInterrupt (void) +{ + digitalWrite (OUT_PIN, 1) ; + ++globalCounter ; + digitalWrite (OUT_PIN, 0) ; +} + + +/* + ********************************************************************************* + * main + ********************************************************************************* + */ + +int main (void) +{ + int myCounter = 0 ; + int lastCounter = 0 ; + + if (wiringPiSetup () < 0) + { + fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ; + return 1 ; + } + + pinMode (OUT_PIN, OUTPUT) ; + pinMode (IN_PIN, INPUT) ; + + if (wiringPiISR (IN_PIN, INT_EDGE_FALLING, &myInterrupt) < 0) + { + fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ; + return 1 ; + } + + for (;;) + { + printf ("Waiting ... ") ; fflush (stdout) ; + + while (myCounter == globalCounter) + delay (1000) ; + + printf (" Done. counter: %6d: %6d\n", + globalCounter, myCounter - lastCounter) ; + lastCounter = myCounter ; + myCounter = globalCounter ; + } + + return 0 ; +} diff --git a/examples/nes.c b/examples/nes.c index 1a485bd..31908e8 100644 --- a/examples/nes.c +++ b/examples/nes.c @@ -1,3 +1,26 @@ +/* + * nes.c: + * Test program for an old NES controller connected to the Pi. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ #include #include diff --git a/examples/okLed.c b/examples/okLed.c index 02f0b22..9b3a170 100644 --- a/examples/okLed.c +++ b/examples/okLed.c @@ -1,7 +1,6 @@ /* - * okLed: + * okLed.c: * Make the OK LED on the Pi Pulsate... - * Copyright (c) 2012 gordon Henderson, but please Share and Enjoy! * * Originally posted to the Raspberry Pi forums: * http://www.raspberrypi.org/phpBB3/viewtopic.php?p=162581#p162581 @@ -10,6 +9,24 @@ * e.g. by putting it in /etc/rc.local and running it in the * background & * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** */ #include diff --git a/examples/piface.c b/examples/piface.c index 3305bf9..0f00960 100644 --- a/examples/piface.c +++ b/examples/piface.c @@ -1,9 +1,27 @@ - /* - * piface.c: - * Simple test for the PiFace + * piFace.c: + * Simple test for the PiFace interface board. * * Read the buttons and output the same to the LEDs + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** */ #include diff --git a/examples/pwm.c b/examples/pwm.c index 09b4ae0..c1fc331 100644 --- a/examples/pwm.c +++ b/examples/pwm.c @@ -1,3 +1,27 @@ +/* + * pwm.c: + * Test of the software PWM driver. Needs 12 LEDs connected + * to the Pi. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ #include #include diff --git a/examples/serialRead.c b/examples/serialRead.c index 34b9bad..9ee11ac 100644 --- a/examples/serialRead.c +++ b/examples/serialRead.c @@ -1,8 +1,25 @@ - /* - * serialRead.c: + * serial.c: * Example program to read bytes from the Serial line * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** */ #include diff --git a/examples/serialTest.c b/examples/serialTest.c index 85a1a66..0d6da5f 100644 --- a/examples/serialTest.c +++ b/examples/serialTest.c @@ -3,6 +3,24 @@ * Very simple program to test the serial port. Expects * the port to be looped back to itself * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** */ #include diff --git a/examples/servo.c b/examples/servo.c index 0237832..aa1ab05 100644 --- a/examples/servo.c +++ b/examples/servo.c @@ -1,3 +1,27 @@ +/* + * servo.c: + * Test of the softServo code. + * Do not use this code - use the servoBlaster kernel module instead + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ #include #include diff --git a/examples/speed.c b/examples/speed.c index 2f5d990..863317e 100644 --- a/examples/speed.c +++ b/examples/speed.c @@ -1,8 +1,26 @@ - /* * speed.c: * Simple program to measure the speed of the various GPIO * access mechanisms. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** */ #include diff --git a/examples/test1.c b/examples/test1.c index 7eb0abd..4c75711 100644 --- a/examples/test1.c +++ b/examples/test1.c @@ -1,7 +1,27 @@ - /* * test1.c: * Simple test program to test the wiringPi functions + * This is a sequencer to make a patter appear on 8 LEDs + * connected to the GPIO pins. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** */ #include diff --git a/examples/test2.c b/examples/test2.c index e34013c..580591e 100644 --- a/examples/test2.c +++ b/examples/test2.c @@ -1,8 +1,25 @@ - /* * test2.c: - * Simple test program to test the wiringPi functions - * PWM test + * This tests the hardware PWM channel. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** */ #include diff --git a/examples/tone.c b/examples/tone.c index 8b1fcd7..0e8a47d 100644 --- a/examples/tone.c +++ b/examples/tone.c @@ -1,3 +1,27 @@ +/* + * tone.c: + * Test of the softTone module in wiringPi + * Plays a scale out on pin 3 - connect pizeo disc to pin 3 & 0v + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ #include #include @@ -6,15 +30,13 @@ #include #include -#define RANGE 100 -#define NUM_LEDS 12 +#define PIN 3 int scale [8] = { 262, 294, 330, 349, 392, 440, 494, 525 } ; int main () { - int i, j ; - char buf [80] ; + int i ; if (wiringPiSetup () == -1) { @@ -22,14 +44,14 @@ int main () return 1 ; } - softToneCreate (3) ; + softToneCreate (PIN) ; for (;;) { for (i = 0 ; i < 8 ; ++i) { printf ("%3d\n", i) ; - softToneWrite (3, scale [i]) ; + softToneWrite (PIN, scale [i]) ; delay (500) ; } } diff --git a/gpio/Makefile b/gpio/Makefile index 623096c..a043962 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -70,8 +70,8 @@ install: .PHONEY: uninstall uninstall: @echo "[UnInstall]" - rm -f /usr/local/bin/gpio - rm -f /usr/local/man/man1/gpio.1 + @rm -f /usr/local/bin/gpio + @rm -f /usr/local/man/man1/gpio.1 .PHONEY: depend depend: diff --git a/gpio/gpio.1 b/gpio/gpio.1 index a83cf9f..e816e22 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -38,7 +38,7 @@ group value range .PP .B gpio -.B load \ i2c/spi +.B load \ i2c/spi ... .PP .B gpio .B gbr @@ -168,9 +168,18 @@ Change the PWM mode to balanced (the default) or mark:space ratio (traditional) Change the PWM range register. The default is 1024. .TP -.B load i2c/spi -This loads the i2c or the spi drivers into the system and changes the permissions on -the associated /dev/ entries so that the current user has access to them. +.B load i2c [baudrate] +This loads the i2c or drivers into the kernel and changes the permissions +on the associated /dev/ entries so that the current user has access to +them. Optionally it will set the I2C baudrate to that supplied (or as +close as the Pi can manage) The default speed is 100Kb/sec. + +.TP +.B load spi [buffer size in KB] +This loads the the spi drivers into the kernel and changes the permissions +on the associated /dev/ entries so that the current user has access to +them. Optionally it will set the SPI buffer size to that supplied. The +default is 4KB. .TP .B gbr @@ -275,4 +284,5 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. .SH TRADEMARKS AND ACKNOWLEDGEMENTS -Raspberry Pi is a trademark of the Raspberry Pi Foundation. +Raspberry Pi is a trademark of the Raspberry Pi Foundation. See +http://raspberrypi.org/ for full details. diff --git a/gpio/gpio.c b/gpio/gpio.c index 326dd2d..5eef5d8 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -42,7 +42,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "1.8" +#define VERSION "1.10" static int wpMode ; @@ -129,7 +129,7 @@ static int moduleLoaded (char *modName) static void _doLoadUsage (char *argv []) { - fprintf (stderr, "Usage: %s load [bufferSize in KB for spi]\n", argv [0]) ; + fprintf (stderr, "Usage: %s load [SPI bufferSize in KB | I2C baudrate in Kb/sec]\n", argv [0]) ; exit (1) ; } @@ -138,12 +138,12 @@ static void doLoad (int argc, char *argv []) char *module1, *module2 ; char cmd [80] ; char *file1, *file2 ; - char spiBuf [32] ; + char args1 [32], args2 [32] ; if (argc < 3) _doLoadUsage (argv) ; - spiBuf [0] = 0 ; + args1 [0] = args2 [0] = 0 ; /**/ if (strcasecmp (argv [2], "spi") == 0) { @@ -152,10 +152,9 @@ static void doLoad (int argc, char *argv []) file1 = "/dev/spidev0.0" ; file2 = "/dev/spidev0.1" ; if (argc == 4) - sprintf (spiBuf, " bufsize=%d", atoi (argv [3]) * 1024) ; + sprintf (args1, " bufsize=%d", atoi (argv [3]) * 1024) ; else if (argc > 4) _doLoadUsage (argv) ; - } else if (strcasecmp (argv [2], "i2c") == 0) { @@ -163,19 +162,23 @@ static void doLoad (int argc, char *argv []) module2 = "i2c_bcm2708" ; file1 = "/dev/i2c-0" ; file2 = "/dev/i2c-1" ; + if (argc == 4) + sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ; + else if (argc > 4) + _doLoadUsage (argv) ; } else _doLoadUsage (argv) ; if (!moduleLoaded (module1)) { - sprintf (cmd, "modprobe %s%s", module1, spiBuf) ; + sprintf (cmd, "modprobe %s%s", module1, args1) ; system (cmd) ; } if (!moduleLoaded (module2)) { - sprintf (cmd, "modprobe %s", module2) ; + sprintf (cmd, "modprobe %s%s", module2, args2) ; system (cmd) ; } @@ -200,55 +203,39 @@ static void doLoad (int argc, char *argv []) static char *pinNames [] = { - "GPIO 0", - "GPIO 1", - "GPIO 2", - "GPIO 3", - "GPIO 4", - "GPIO 5", - "GPIO 6", - "GPIO 7", - "SDA ", - "SCL ", - "CE0 ", - "CE1 ", - "MOSI ", - "MISO ", - "SCLK ", - "TxD ", - "RxD ", - "GPIO 8", - "GPIO 9", - "GPIO10", - "GPIO11", + "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7", + "SDA ", "SCL ", + "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ", + "TxD ", "RxD ", + "GPIO 8", "GPIO 9", "GPIO10", "GPIO11", +} ; + +static char *alts [] = +{ + "IN ", "OUT ", "ALT0", "ALT1", "ALT2", "ALT3", "ALT4", "ALT5", "XXXX" } ; static void doReadall (void) { int pin ; - printf ("+----------+------+--------+-------+\n") ; - printf ("| wiringPi | GPIO | Name | Value |\n") ; - printf ("+----------+------+--------+-------+\n") ; + printf ("+----------+------+--------+------+------+\n") ; + printf ("| wiringPi | GPIO | Name | Mode | Value |\n") ; + printf ("+----------+------+--------+------+------+\n") ; - for (pin = 0 ; pin < NUM_PINS ; ++pin) - printf ("| %6d | %3d | %s | %s |\n", + for (pin = 0 ; pin < 64 ; ++pin) + { + if (wpiPinToGpio (pin) == -1) + continue ; + + printf ("| %6d | %3d | %s | %s | %s |\n", pin, wpiPinToGpio (pin), pinNames [pin], + alts [getAlt (pin)], digitalRead (pin) == HIGH ? "High" : "Low ") ; + } - printf ("+----------+------+--------+-------+\n") ; - - if (piBoardRev () == 1) - return ; - - for (pin = 17 ; pin <= 20 ; ++pin) - printf ("| %6d | %3d | %s | %s |\n", - pin, wpiPinToGpio (pin), - pinNames [pin], - digitalRead (pin) == HIGH ? "High" : "Low ") ; - - printf ("+----------+------+--------+-------+\n") ; + printf ("+----------+------+--------+------+------+\n") ; } diff --git a/wiringPi/lcd.c b/wiringPi/lcd.c index aa58cab..f123db2 100644 --- a/wiringPi/lcd.c +++ b/wiringPi/lcd.c @@ -174,6 +174,18 @@ void lcdClear (int fd) } +/* + * lcdSendCommand: + * Send any arbitary command to the display + ********************************************************************************* + */ + +void lcdSendCommand (int fd, uint8_t command) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + putCommand (lcd, command) ; +} + /* * lcdPosition: * Update the position of the cursor on the display diff --git a/wiringPi/lcd.h b/wiringPi/lcd.h index ecd1d25..beebb75 100644 --- a/wiringPi/lcd.h +++ b/wiringPi/lcd.h @@ -30,12 +30,13 @@ extern "C" { #endif -extern void lcdHome (int fd) ; -extern void lcdClear (int fd) ; -extern void lcdPosition (int fd, int x, int y) ; -extern void lcdPutchar (int fd, uint8_t data) ; -extern void lcdPuts (int fd, char *string) ; -extern void lcdPrintf (int fd, char *message, ...) ; +extern void lcdHome (int fd) ; +extern void lcdClear (int fd) ; +extern void lcdSendCommand (int fd, uint8_t command) ; +extern void lcdPosition (int fd, int x, int y) ; +extern void lcdPutchar (int fd, uint8_t data) ; +extern void lcdPuts (int fd, char *string) ; +extern void lcdPrintf (int fd, char *message, ...) ; extern int lcdInit (int rows, int cols, int bits, int rs, int strb, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 9655db2..bb22de6 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -51,9 +51,6 @@ // Added in the 2 UART pins // Change maxPins to numPins to more accurately reflect purpose -// Pad drive current fiddling - -#undef DEBUG_PADS #include #include @@ -68,15 +65,16 @@ #include #include #include -#include #include #include +#include #include "wiringPi.h" // Function stubs void (*pinMode) (int pin, int mode) ; +int (*getAlt) (int pin) ; void (*pullUpDnControl) (int pin, int pud) ; void (*digitalWrite) (int pin, int value) ; void (*digitalWriteByte) (int value) ; @@ -84,7 +82,6 @@ void (*pwmWrite) (int pin, int value) ; void (*setPadDrive) (int group, int value) ; int (*digitalRead) (int pin) ; int (*waitForInterrupt) (int pin, int mS) ; -void (*delayMicroseconds) (unsigned int howLong) ; void (*pwmSetMode) (int mode) ; void (*pwmSetRange) (unsigned int range) ; void (*pwmSetClock) (int divisor) ; @@ -177,7 +174,7 @@ static volatile uint32_t *timerIrqRaw ; // Time for easy calculations -static unsigned long long epoch ; +static uint64_t epochMilli, epochMicro ; // Misc @@ -586,6 +583,38 @@ void pinModeSys (int pin, int mode) } +/* + * getAlt: + * Returns the ALT bits for a given port. Only really of-use + * for the gpio readall command (I think) + ********************************************************************************* + */ + +int getAltGpio (int pin) +{ + int fSel, shift, alt ; + + pin &= 63 ; + + fSel = gpioToGPFSEL [pin] ; + shift = gpioToShift [pin] ; + + alt = (*(gpio + fSel) >> shift) & 7 ; + + return alt ; +} + +int getAltWPi (int pin) +{ + return getAltGpio (pinToGpio [pin & 63]) ; +} + +int getAltSys (int pin) +{ + return 0 ; +} + + /* * pwmControl: * Allow the user to control some of the PWM functions @@ -627,7 +656,7 @@ void pwmSetRangeSys (unsigned int range) void pwmSetClockWPi (int divisor) { - unsigned int pwm_control ; + uint32_t pwm_control ; divisor &= 4095 ; if (wiringPiDebug) @@ -811,10 +840,11 @@ void setPadDriveWPi (int group, int value) wrVal = BCM_PASSWORD | 0x18 | (value & 7) ; *(pads + group + 11) = wrVal ; -#ifdef DEBUG_PADS - printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ; - printf ("Read : %08X\n", *(pads + group + 11)) ; -#endif + if (wiringPiDebug) + { + printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ; + printf ("Read : %08X\n", *(pads + group + 11)) ; + } } void setPadDriveGpio (int group, int value) @@ -913,22 +943,12 @@ void pullUpDnControlSys (int pin, int pud) int waitForInterruptSys (int pin, int mS) { int fd, x ; - char buf [8] ; + uint8_t c ; struct pollfd polls ; if ((fd = sysFds [pin & 63]) == -1) return -2 ; -// Do a dummy read - - x = read (fd, buf, 6) ; - if (x < 0) - return x ; - -// And seek - - lseek (fd, 0, SEEK_SET) ; - // Setup poll structure polls.fd = fd ; @@ -936,7 +956,14 @@ int waitForInterruptSys (int pin, int mS) // Wait for it ... - return poll (&polls, 1, mS) ; + x = poll (&polls, 1, mS) ; + +// Do a dummy read to clear the interrupt +// A one character read appars to be enough. + + (void)read (fd, &c, 1) ; + + return x ; } int waitForInterruptWPi (int pin, int mS) @@ -986,6 +1013,8 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) char *modeS ; char pinS [8] ; pid_t pid ; + int count, i ; + uint8_t c ; pin &= 63 ; @@ -1027,12 +1056,18 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) } // Now pre-open the /sys/class node - it may already be open if -// we had set it up earlier, but this will do no harm. +// we are in Sys mode, but this will do no harm. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; if ((sysFds [pin] = open (fName, O_RDWR)) < 0) return -1 ; +// Clear any initial pending interrupt + + ioctl (sysFds [pin], FIONREAD, &count) ; + for (i = 0 ; i < count ; ++i) + read (sysFds [pin], &c, 1) ; + isrFunctions [pin] = function ; pthread_create (&threadId, NULL, interruptHandler, &pin) ; @@ -1043,6 +1078,22 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) } +/* + * initialiseEpoch: + * Initialise our start-of-time variable to be the current unix + * time in milliseconds. + ********************************************************************************* + */ + +static void initialiseEpoch (void) +{ + struct timeval tv ; + + gettimeofday (&tv, NULL) ; + epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ; + epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ; +} + /* * delay: * Wait for some number of milli seconds @@ -1078,28 +1129,8 @@ void delay (unsigned int howLong) ********************************************************************************* */ -void delayMicrosecondsSys (unsigned int howLong) -{ - struct timespec sleeper, dummy ; - - sleeper.tv_sec = 0 ; - sleeper.tv_nsec = (long)(howLong * 1000) ; - - nanosleep (&sleeper, &dummy) ; -} - void delayMicrosecondsHard (unsigned int howLong) { -#ifdef HARD_TIMER - volatile unsigned int dummy ; - - *(timer + TIMER_LOAD) = howLong ; - *(timer + TIMER_IRQ_CLR) = 0 ; - - dummy = *timerIrqRaw ; - while (dummy == 0) - dummy = *timerIrqRaw ; -#else struct timeval tNow, tLong, tEnd ; gettimeofday (&tNow, NULL) ; @@ -1109,10 +1140,9 @@ void delayMicrosecondsHard (unsigned int howLong) while (timercmp (&tNow, &tEnd, <)) gettimeofday (&tNow, NULL) ; -#endif } -void delayMicrosecondsWPi (unsigned int howLong) +void delayMicroseconds (unsigned int howLong) { struct timespec sleeper ; @@ -1138,13 +1168,30 @@ void delayMicrosecondsWPi (unsigned int howLong) unsigned int millis (void) { struct timeval tv ; - unsigned long long t1 ; + uint64_t now ; gettimeofday (&tv, NULL) ; + now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ; - t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + return (uint32_t)(now - epochMilli) ; +} - return (uint32_t)(t1 - epoch) ; + +/* + * micros: + * Return a number of microseconds as an unsigned int. + ********************************************************************************* + */ + +unsigned int micros (void) +{ + struct timeval tv ; + uint64_t now ; + + gettimeofday (&tv, NULL) ; + now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ; + + return (uint32_t)(now - epochMicro) ; } @@ -1161,8 +1208,6 @@ int wiringPiSetup (void) { int fd ; int boardRev ; - //uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ; - struct timeval tv ; if (geteuid () != 0) { @@ -1180,6 +1225,7 @@ int wiringPiSetup (void) printf ("wiringPi: wiringPiSetup called\n") ; pinMode = pinModeWPi ; + getAlt = getAltWPi ; pullUpDnControl = pullUpDnControlWPi ; digitalWrite = digitalWriteWPi ; digitalWriteByte = digitalWriteByteGpio ; // Same code @@ -1187,7 +1233,6 @@ int wiringPiSetup (void) setPadDrive = setPadDriveWPi ; digitalRead = digitalReadWPi ; waitForInterrupt = waitForInterruptWPi ; - delayMicroseconds = delayMicrosecondsWPi ; pwmSetMode = pwmSetModeWPi ; pwmSetRange = pwmSetRangeWPi ; pwmSetClock = pwmSetClockWPi ; @@ -1268,11 +1313,6 @@ int wiringPiSetup (void) return -1 ; } -#ifdef DEBUG_PADS - printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ; - printf (" -> %08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ; -#endif - // The system timer timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ; @@ -1295,10 +1335,7 @@ int wiringPiSetup (void) *(timer + TIMER_PRE_DIV) = 0x00000F9 ; timerIrqRaw = timer + TIMER_IRQ_RAW ; -// Initialise our epoch for millis() - - gettimeofday (&tv, NULL) ; - epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + initialiseEpoch () ; wiringPiMode = WPI_MODE_PINS ; @@ -1332,6 +1369,7 @@ int wiringPiSetupGpio (void) printf ("wiringPi: wiringPiSetupGpio called\n") ; pinMode = pinModeGpio ; + getAlt = getAltGpio ; pullUpDnControl = pullUpDnControlGpio ; digitalWrite = digitalWriteGpio ; digitalWriteByte = digitalWriteByteGpio ; @@ -1339,7 +1377,6 @@ int wiringPiSetupGpio (void) setPadDrive = setPadDriveGpio ; digitalRead = digitalReadGpio ; waitForInterrupt = waitForInterruptGpio ; - delayMicroseconds = delayMicrosecondsWPi ; // Same pwmSetMode = pwmSetModeWPi ; pwmSetRange = pwmSetRangeWPi ; pwmSetClock = pwmSetClockWPi ; @@ -1363,7 +1400,6 @@ int wiringPiSetupSys (void) { int boardRev ; int pin ; - struct timeval tv ; char fName [128] ; if (getenv ("WIRINGPI_DEBUG") != NULL) @@ -1373,6 +1409,7 @@ int wiringPiSetupSys (void) printf ("wiringPi: wiringPiSetupSys called\n") ; pinMode = pinModeSys ; + getAlt = getAltSys ; pullUpDnControl = pullUpDnControlSys ; digitalWrite = digitalWriteSys ; digitalWriteByte = digitalWriteByteSys ; @@ -1380,7 +1417,6 @@ int wiringPiSetupSys (void) setPadDrive = setPadDriveSys ; digitalRead = digitalReadSys ; waitForInterrupt = waitForInterruptSys ; - delayMicroseconds = delayMicrosecondsSys ; pwmSetMode = pwmSetModeSys ; pwmSetRange = pwmSetRangeSys ; pwmSetClock = pwmSetClockSys ; @@ -1401,10 +1437,7 @@ int wiringPiSetupSys (void) sysFds [pin] = open (fName, O_RDWR) ; } -// Initialise the epoch for mills() ... - - gettimeofday (&tv, NULL) ; - epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; + initialiseEpoch () ; wiringPiMode = WPI_MODE_GPIO_SYS ; diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 7626d28..47d8cc5 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -81,13 +81,13 @@ extern int wpiPinToGpio (int wpiPin) ; extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only extern void (*pinMode) (int pin, int mode) ; +extern int (*getAlt) (int pin) ; extern void (*pullUpDnControl) (int pin, int pud) ; extern void (*digitalWrite) (int pin, int value) ; extern void (*digitalWriteByte) (int value) ; extern void (*pwmWrite) (int pin, int value) ; extern void (*setPadDrive) (int group, int value) ; extern int (*digitalRead) (int pin) ; -extern void (*delayMicroseconds) (unsigned int howLong) ; extern void (*pwmSetMode) (int mode) ; extern void (*pwmSetRange) (unsigned int range) ; extern void (*pwmSetClock) (int divisor) ; @@ -111,7 +111,9 @@ extern int piHiPri (int pri) ; // Extras from arduino land extern void delay (unsigned int howLong) ; +extern void delayMicroseconds (unsigned int howLong) ; extern unsigned int millis (void) ; +extern unsigned int micros (void) ; #ifdef __cplusplus } From 4ba36e247ac4a6f2c948509ddbda0289cb9fad4e Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 28 Jan 2013 16:06:34 +0000 Subject: [PATCH 34/97] Fixed a minor formatting issue in gpio readall --- gpio/gpio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 5eef5d8..59f93d2 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -219,9 +219,9 @@ static void doReadall (void) { int pin ; - printf ("+----------+------+--------+------+------+\n") ; + printf ("+----------+------+--------+------+-------+\n") ; printf ("| wiringPi | GPIO | Name | Mode | Value |\n") ; - printf ("+----------+------+--------+------+------+\n") ; + printf ("+----------+------+--------+------+-------+\n") ; for (pin = 0 ; pin < 64 ; ++pin) { @@ -235,7 +235,7 @@ static void doReadall (void) digitalRead (pin) == HIGH ? "High" : "Low ") ; } - printf ("+----------+------+--------+------+------+\n") ; + printf ("+----------+------+--------+------+-------+\n") ; } From 56c77b5a2ef654d916910159bf6f3367c0b11609 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Fri, 1 Feb 2013 20:19:22 +0000 Subject: [PATCH 35/97] Added a clock mode to enable the GPIo pins to be set with one of the GPCLK drivers to generate a frequency up to 19.2MHz. Tidied some code up and added the clock functions into the gpio utility. --- gpio/gpio.1 | 10 +- gpio/gpio.c | 47 +++++-- wiringPi/wiringPi.c | 317 +++++++++++++++++++++++++++----------------- wiringPi/wiringPi.h | 6 + 4 files changed, 247 insertions(+), 133 deletions(-) diff --git a/gpio/gpio.1 b/gpio/gpio.1 index e816e22..ec54f13 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -9,7 +9,7 @@ gpio \- Command-line access to Raspberry Pi and PiFace GPIO .PP .B gpio .B [ \-g ] -.B read/write/wb/pwm/mode ... +.B read/write/wb/pwm/clock/mode ... .PP .B gpio .B [ \-p ] @@ -104,7 +104,13 @@ mode. .TP .B pwm -Write a PWM value (0-1023) to the given pin. +Write a PWM value (0-1023) to the given pin. The pin needs to be put +into PWM mode first. + +.TP +.B clock +Set the output frequency on the given pin. The pin needs to be put into +clock mode first. .TP .B mode diff --git a/gpio/gpio.c b/gpio/gpio.c index 59f93d2..6c0bf89 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -42,13 +42,13 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "1.10" +#define VERSION "1.11" static int wpMode ; char *usage = "Usage: gpio -v\n" " gpio -h\n" - " gpio [-g] ...\n" + " gpio [-g] ...\n" " gpio [-p] ...\n" " gpio readall\n" " gpio unexportall/exports ...\n" @@ -541,15 +541,16 @@ void doMode (int argc, char *argv []) mode = argv [3] ; - /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; - else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ; - else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ; - else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ; - else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; - else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; + /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; + else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ; + else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ; + else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ; + else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ; + else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; + else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; else { - fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ; + fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ; exit (1) ; } } @@ -754,6 +755,33 @@ void doRead (int argc, char *argv []) } +/* + * doClock: + * Output a clock on a pin + ********************************************************************************* + */ + +void doClock (int argc, char *argv []) +{ + int pin, freq ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s clock \n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) + return ; + + freq = atoi (argv [3]) ; + + gpioClockSet (pin, freq) ; +} + + /* * doPwm: * Output a PWM value on a pin @@ -980,6 +1008,7 @@ int main (int argc, char *argv []) else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ; else if (strcasecmp (argv [1], "wb") == 0) doWriteByte (argc, argv) ; else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ; + else if (strcasecmp (argv [1], "clock") == 0) doClock (argc, argv) ; else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ; else { diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index bb22de6..12b1af1 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -79,6 +79,7 @@ void (*pullUpDnControl) (int pin, int pud) ; void (*digitalWrite) (int pin, int value) ; void (*digitalWriteByte) (int value) ; void (*pwmWrite) (int pin, int value) ; +void (*gpioClockSet) (int pin, int value) ; void (*setPadDrive) (int group, int value) ; int (*digitalRead) (int pin) ; int (*waitForInterrupt) (int pin, int mS) ; @@ -97,6 +98,24 @@ void (*pwmSetClock) (int divisor) ; #define BCM_PASSWORD 0x5A000000 +// The BCM2835 has 54 GPIO pins. +// BCM2835 data sheet, Page 90 onwards. +// There are 6 control registers, each control the functions of a block +// of 10 pins. +// Each control register has 10 sets of 3 bits per GPIO pin - the ALT values +// +// 000 = GPIO Pin X is an input +// 001 = GPIO Pin X is an output +// 100 = GPIO Pin X takes alternate function 0 +// 101 = GPIO Pin X takes alternate function 1 +// 110 = GPIO Pin X takes alternate function 2 +// 111 = GPIO Pin X takes alternate function 3 +// 011 = GPIO Pin X takes alternate function 4 +// 010 = GPIO Pin X takes alternate function 5 +// +// So the 3 bits for port X are: +// X / 10 + ((X % 10) * 3) + // Port function select bits #define FSEL_INPT 0b000 @@ -110,20 +129,21 @@ void (*pwmSetClock) (int divisor) ; #define FSEL_ALT5 0b010 // Access from ARM Running Linux -// Take from Gert/Doms code. Some of this is not in the manual +// Taken from Gert/Doms code. Some of this is not in the manual // that I can find )-: -#define BCM2708_PERI_BASE 0x20000000 -#define GPIO_PADS (BCM2708_PERI_BASE + 0x100000) -#define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000) -#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) -#define GPIO_TIMER (BCM2708_PERI_BASE + 0x00B000) -#define GPIO_PWM (BCM2708_PERI_BASE + 0x20C000) +#define BCM2708_PERI_BASE 0x20000000 +#define GPIO_PADS (BCM2708_PERI_BASE + 0x00100000) +#define CLOCK_BASE (BCM2708_PERI_BASE + 0x00101000) +#define GPIO_BASE (BCM2708_PERI_BASE + 0x00200000) +#define GPIO_TIMER (BCM2708_PERI_BASE + 0x0000B000) +#define GPIO_PWM (BCM2708_PERI_BASE + 0x0020C000) #define PAGE_SIZE (4*1024) #define BLOCK_SIZE (4*1024) // PWM +// Word offsets into the PWM control region #define PWM_CONTROL 0 #define PWM_STATUS 1 @@ -132,17 +152,11 @@ void (*pwmSetClock) (int divisor) ; #define PWM1_RANGE 8 #define PWM1_DATA 9 +// Clock regsiter offsets + #define PWMCLK_CNTL 40 #define PWMCLK_DIV 41 -#define PWM1_MS_MODE 0x8000 // Run in MS mode -#define PWM1_USEFIFO 0x2000 // Data from FIFO -#define PWM1_REVPOLAR 0x1000 // Reverse polarity -#define PWM1_OFFSTATE 0x0800 // Ouput Off state -#define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty -#define PWM1_SERIAL 0x0200 // Run in serial mode -#define PWM1_ENABLE 0x0100 // Channel Enable - #define PWM0_MS_MODE 0x0080 // Run in MS mode #define PWM0_USEFIFO 0x0020 // Data from FIFO #define PWM0_REVPOLAR 0x0010 // Reverse polarity @@ -151,7 +165,16 @@ void (*pwmSetClock) (int divisor) ; #define PWM0_SERIAL 0x0002 // Run in serial mode #define PWM0_ENABLE 0x0001 // Channel Enable +#define PWM1_MS_MODE 0x8000 // Run in MS mode +#define PWM1_USEFIFO 0x2000 // Data from FIFO +#define PWM1_REVPOLAR 0x1000 // Reverse polarity +#define PWM1_OFFSTATE 0x0800 // Ouput Off state +#define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty +#define PWM1_SERIAL 0x0200 // Run in serial mode +#define PWM1_ENABLE 0x0100 // Channel Enable + // Timer +// Word offsets #define TIMER_LOAD (0x400 >> 2) #define TIMER_VALUE (0x404 >> 2) @@ -184,24 +207,6 @@ static int wiringPiMode = WPI_MODE_UNINITIALISED ; int wiringPiDebug = FALSE ; -// The BCM2835 has 54 GPIO pins. -// BCM2835 data sheet, Page 90 onwards. -// There are 6 control registers, each control the functions of a block -// of 10 pins. -// Each control register has 10 sets of 3 bits per GPIO pin: -// -// 000 = GPIO Pin X is an input -// 001 = GPIO Pin X is an output -// 100 = GPIO Pin X takes alternate function 0 -// 101 = GPIO Pin X takes alternate function 1 -// 110 = GPIO Pin X takes alternate function 2 -// 111 = GPIO Pin X takes alternate function 3 -// 011 = GPIO Pin X takes alternate function 4 -// 010 = GPIO Pin X takes alternate function 5 -// -// So the 3 bits for port X are: -// X / 10 + ((X % 10) * 3) - // sysFds: // Map a file descriptor from the /sys/class/gpio/gpioX/value @@ -341,11 +346,14 @@ static uint8_t gpioToFEN [] = #endif -// gpioToPUDCLK -// (Word) offset to the Pull Up Down Clock regsiter +// GPPUD: +// GPIO Pin pull up/down register #define GPPUD 37 +// gpioToPUDCLK +// (Word) offset to the Pull Up Down Clock regsiter + static uint8_t gpioToPUDCLK [] = { 38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38, @@ -368,6 +376,9 @@ static uint8_t gpioToPwmALT [] = 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63 } ; +// gpioToPwmPort +// The port value to put a GPIO pin into PWM mode + static uint8_t gpioToPwmPort [] = { 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7 @@ -381,6 +392,55 @@ static uint8_t gpioToPwmPort [] = } ; +// gpioToGpClkALT: +// ALT value to put a GPIO pin into GP Clock mode. +// On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21 +// for clocks 0 and 1 respectivey, however I'll include the full +// list for completeness - maybe one day... + +#define GPIO_CLOCK_SOURCE 1 + +// gpioToGpClkALT0: + +static uint8_t gpioToGpClkALT0 [] = +{ + 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7 + 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15 + 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23 + 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31 + FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39 + 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47 + 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55 + 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63 +} ; + +// gpioToClk: +// (word) Offsets to the clock Control and Divisor register + +static uint8_t gpioToClkCon [] = +{ + -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7 + -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15 + -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23 + -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31 + 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39 + -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47 + -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55 + -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63 +} ; + +static uint8_t gpioToClkDiv [] = +{ + -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7 + -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15 + -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23 + -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31 + 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39 + -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47 + -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55 + -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63 +} ; + /* * Functions @@ -496,93 +556,6 @@ int piBoardRev (void) -/* - * pinMode: - * Sets the mode of a pin to be input, output or PWM output - ********************************************************************************* - */ - -void pinModeGpio (int pin, int mode) -{ -// register int barrier ; - - 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) ; - - delayMicroseconds (110) ; // See comments in pwmSetClockWPi - *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; - -// Page 107 of the BCM Peripherals manual talks about the GPIO clocks, -// but I'm assuming (hoping!) that this applies to other clocks too. - - *(pwm + PWM_CONTROL) = 0 ; // Stop PWM - - *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock - delayMicroseconds (110) ; // See comments in pwmSetClockWPi - - while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY - delayMicroseconds (1) ; - - *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32 << 12) ; // set pwm div to 32 (19.2/32 = 600KHz) - *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // enable clk - - delayMicroseconds (110) ; // See comments in pwmSetClockWPi - -// Default range register of 1024 - - *(pwm + PWM0_RANGE) = 1024 ; delayMicroseconds (10) ; - *(pwm + PWM1_RANGE) = 1024 ; delayMicroseconds (10) ; - *(pwm + PWM0_DATA) = 0 ; delayMicroseconds (10) ; - *(pwm + PWM1_DATA) = 0 ; delayMicroseconds (10) ; - -// Enable PWMs in balanced mode (default) - - *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; - - delay (100) ; - } - - -// When we change mode of any pin, we remove the pull up/downs -// Or we used to... Hm. Commented out now because for some wieird reason, -// it seems to block subsequent attempts to set the pull up/downs and I've -// not quite gotten to the bottom of why this happens -// The down-side is that the pull up/downs are rememberd in the SoC between -// power cycles, so it's going to be a good idea to explicitly set them in -// any new code. -// -// pullUpDnControl (pin, PUD_OFF) ; - -} - -void pinModeWPi (int pin, int mode) -{ - pinModeGpio (pinToGpio [pin & 63], mode) ; -} - -void pinModeSys (int pin, int mode) -{ - return ; -} - - /* * getAlt: * Returns the ALT bits for a given port. Only really of-use @@ -824,6 +797,44 @@ void pwmWriteSys (int pin, int value) } +/* + * gpioClockSet: + * Set the freuency on a GPIO clock pin + ********************************************************************************* + */ + +void gpioClockSetGpio (int pin, int freq) +{ + int divi, divr, divf ; + + pin &= 63 ; + + divi = 19200000 / freq ; + divr = 19200000 % freq ; + divf = (int)((double)divr * 4096.0 / 19200000.0) ; + + if (divi > 4095) + divi = 4095 ; + + *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock + while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait + ; + + *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers + *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock +} + +void gpioClockSetWPi (int pin, int freq) +{ + gpioClockSetGpio (pinToGpio [pin & 63], freq) ; +} + +void gpioClockSetSys (int pin, int freq) +{ + return ; +} + + /* * setPadDrive: * Set the PAD driver value @@ -931,6 +942,65 @@ void pullUpDnControlSys (int pin, int pud) } +/* + * pinMode: + * Sets the mode of a pin to be input, output or PWM output + ********************************************************************************* + */ + +void pinModeGpio (int pin, int mode) +{ +// register int barrier ; + + 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) ; + delayMicroseconds (110) ; // See comments in pwmSetClockWPi + + pwmSetModeWPi (PWM_MODE_BAL) ; // Pi default mode + pwmSetRangeWPi (1024) ; // Default range of 1024 + pwmSetClockWPi (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM + } + else if (mode == GPIO_CLOCK) + { + if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin + return ; + +// Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz + + *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; + delayMicroseconds (110) ; + gpioClockSetGpio (pin, 100000) ; + } +} + +void pinModeWPi (int pin, int mode) +{ + pinModeGpio (pinToGpio [pin & 63], mode) ; +} + +void pinModeSys (int pin, int mode) +{ + return ; +} + + /* * waitForInterrupt: * Wait for Interrupt on a GPIO pin. @@ -1229,6 +1299,7 @@ int wiringPiSetup (void) pullUpDnControl = pullUpDnControlWPi ; digitalWrite = digitalWriteWPi ; digitalWriteByte = digitalWriteByteGpio ; // Same code + gpioClockSet = gpioClockSetWPi ; pwmWrite = pwmWriteWPi ; setPadDrive = setPadDriveWPi ; digitalRead = digitalReadWPi ; @@ -1373,6 +1444,7 @@ int wiringPiSetupGpio (void) pullUpDnControl = pullUpDnControlGpio ; digitalWrite = digitalWriteGpio ; digitalWriteByte = digitalWriteByteGpio ; + gpioClockSet = gpioClockSetGpio ; pwmWrite = pwmWriteGpio ; setPadDrive = setPadDriveGpio ; digitalRead = digitalReadGpio ; @@ -1413,6 +1485,7 @@ int wiringPiSetupSys (void) pullUpDnControl = pullUpDnControlSys ; digitalWrite = digitalWriteSys ; digitalWriteByte = digitalWriteByteSys ; + gpioClockSet = gpioClockSetSys ; pwmWrite = pwmWriteSys ; setPadDrive = setPadDriveSys ; digitalRead = digitalReadSys ; diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 47d8cc5..18c6da5 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -32,13 +32,18 @@ #define WPI_MODE_PIFACE 3 #define WPI_MODE_UNINITIALISED -1 +// Pin modes + #define INPUT 0 #define OUTPUT 1 #define PWM_OUTPUT 2 +#define GPIO_CLOCK 3 #define LOW 0 #define HIGH 1 +// Pull up/down/none + #define PUD_OFF 0 #define PUD_DOWN 1 #define PUD_UP 2 @@ -85,6 +90,7 @@ extern int (*getAlt) (int pin) ; extern void (*pullUpDnControl) (int pin, int pud) ; extern void (*digitalWrite) (int pin, int value) ; extern void (*digitalWriteByte) (int value) ; +extern void (*gpioClockSet) (int pin, int freq) ; extern void (*pwmWrite) (int pin, int value) ; extern void (*setPadDrive) (int group, int value) ; extern int (*digitalRead) (int pin) ; From 4c8862eac9089b7d7395686b4a13dec40ca5be6a Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 2 Feb 2013 20:33:51 +0000 Subject: [PATCH 36/97] Botched the ALT order in gpio )-: (Or maybe Broadcom did!!!) --- gpio/gpio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 6c0bf89..8d61957 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -212,7 +212,7 @@ static char *pinNames [] = static char *alts [] = { - "IN ", "OUT ", "ALT0", "ALT1", "ALT2", "ALT3", "ALT4", "ALT5", "XXXX" + "IN ", "OUT ", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3" } ; static void doReadall (void) From 98bcb20d9391ebde24f9eb1244f0d238fb1a1dab Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 7 Feb 2013 21:53:49 +0000 Subject: [PATCH 37/97] Slight change to the gpio program to fix SPI buffer size when loading the module. Typo in gpio man page Bug fixed in board revision detection (which would never happen anyway, however) --- People | 3 ++ gpio/gpio.1 | 2 +- gpio/gpio.c | 4 +- wiringPi/wiringPi.c | 17 ++++--- wiringPi/wiringPiISR.c | 109 ----------------------------------------- 5 files changed, 15 insertions(+), 120 deletions(-) delete mode 100644 wiringPi/wiringPiISR.c diff --git a/People b/People index f5645be..8be8b6d 100644 --- a/People +++ b/People @@ -22,3 +22,6 @@ Arno Wagner CHARLES Thibaut: A small issue in softTone + +Xian Stannard + Fixing some typos in the man page! diff --git a/gpio/gpio.1 b/gpio/gpio.1 index ec54f13..ec65519 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -182,7 +182,7 @@ close as the Pi can manage) The default speed is 100Kb/sec. .TP .B load spi [buffer size in KB] -This loads the the spi drivers into the kernel and changes the permissions +This loads the spi drivers into the kernel and changes the permissions on the associated /dev/ entries so that the current user has access to them. Optionally it will set the SPI buffer size to that supplied. The default is 4KB. diff --git a/gpio/gpio.c b/gpio/gpio.c index 8d61957..e71e432 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -42,7 +42,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "1.11" +#define VERSION "1.12" static int wpMode ; @@ -152,7 +152,7 @@ static void doLoad (int argc, char *argv []) file1 = "/dev/spidev0.0" ; file2 = "/dev/spidev0.1" ; if (argc == 4) - sprintf (args1, " bufsize=%d", atoi (argv [3]) * 1024) ; + sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ; else if (argc > 4) _doLoadUsage (argv) ; } diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 12b1af1..a68ae33 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -475,9 +475,11 @@ int wpiPinToGpio (int wpiPin) * 0001 - Not used * 0002 - Rev 1 * 0003 - Rev 1 - * 0004 - Rev 2 - * 0005 - Rev 2 (but error) + * 0004 - Rev 2 (Early reports? + * 0005 - Rev 2 (but error?) * 0006 - Rev 2 + * 0008 - Rev 2 - Model A + * 000e - Rev 2 + 512MB * 000f - Rev 2 + 512MB * * A small thorn is the olde style overvolting - that will add in @@ -502,13 +504,11 @@ int piBoardRev (void) char *c, lastChar ; static int boardRev = -1 ; -// No point checking twice... - - if (boardRev != -1) + if (boardRev != -1) // No point checking twice return boardRev ; if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) - return -1 ; + piBoardRevOops ("Unable to open /proc/cpuinfo") ; while (fgets (line, 120, cpuFd) != NULL) if (strncmp (line, "Revision", 8) == 0) @@ -516,10 +516,11 @@ int piBoardRev (void) fclose (cpuFd) ; - if (line == NULL) + if (strncmp (line, "Revision", 8) != 0) piBoardRevOops ("No \"Revision\" line") ; - line [strlen (line) - 1] = 0 ; // Chomp LF + for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c) + *c = 0 ; if (wiringPiDebug) printf ("piboardRev: Revision string: %s\n", line) ; diff --git a/wiringPi/wiringPiISR.c b/wiringPi/wiringPiISR.c deleted file mode 100644 index 9e847cc..0000000 --- a/wiringPi/wiringPiISR.c +++ /dev/null @@ -1,109 +0,0 @@ -/* - * wiringPiISR.c: - * Simplified Interrupt Service Routine handling - * Copyright (c) 2013 Gordon Henderson - *********************************************************************** - * This file is part of wiringPi: - * https://projects.drogon.net/raspberry-pi/wiringpi/ - * - * wiringPi is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * wiringPi is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with wiringPi. - * If not, see . - *********************************************************************** - */ - -#include -#include -#include - -#include "wiringPi.h" - - - -static void (*isrFunctions [64])(void) ; -static int isrFds [64] ; - -/* - * interruptHandler: - * This is a thread and gets started to wait for the interrupt we're - * hoping to catch. It will call the user-function when the interrupt - * fires. - ********************************************************************************* - */ - -static void *interruptHandler (void *arg) -{ - int pin = *(int *)arg ; - - (void)piHiPri (55) ; - - for (;;) - { - if (waitForInterrupt (pin, -1) > 0) - isrFunctions [pin] () ; - } - - return NULL ; -} - -/* - * wiringPiISR: - * Take the details and create an interrupt handler that will do a call- - * back to the user supplied function. - ********************************************************************************* - */ - -int wiringPiISR (int pin, int mode, void (*function)(void)) -{ - pthread_t threadId ; - char command [64] ; - - pin &= 63 ; - - if (wiringPiMode == WPI_MODE_UNINITIALISED) - { - fprintf (stderr, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ; - exit (EXIT_FAILURE) ; - } - else if (wiringPiMode == WPI_MODE_PINS) - pin = pinToGpio [pin] ; - - - isrFunctions [pin] = function ; - -// Now export the pin and set the right edge - - if (mode != INT_EDGE_SETUP) - { - /**/ if (mode == INT_EDGE_FALLING) - modes = "falling" ; - else if (mode == INT_EDGE_RISING) - modes = "rising" ; - else - modes = "both" ; - - sprintf (command, "/usr/local/bin/gpio edge %d %s", pin, modes) ; - system (command) ; - } - - sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; - if ((isrFds [pin] = open (fName, O_RDWR)) < 0) - return -1 ; - - { - fprintf ("std - - pthread_create (&threadId, NULL, interruptHandler, &pin) ; -} - - From da38443cb257a3bbbe4ad7f54ee3f569710a2fe7 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 13 May 2013 19:43:26 +0100 Subject: [PATCH 38/97] wiringPi Version 2 - First commit (of v2) --- INSTALL | 12 - README.TXT | 2 +- build | 53 +- devLib/Makefile | 130 ++ devLib/ds1302.c | 240 +++ wiringPi/lcd.h => devLib/ds1302.h | 30 +- devLib/font.h | 2577 ++++++++++++++++++++++++++ {wiringPi => devLib}/gertboard.c | 56 +- {wiringPi => devLib}/gertboard.h | 10 +- {wiringPi => devLib}/lcd.c | 205 +- devLib/lcd.h | 52 + devLib/lcd128x64.c | 673 +++++++ devLib/lcd128x64.h | 39 + devLib/maxdetect.c | 165 ++ devLib/maxdetect.h | 40 + devLib/piFace.c | 112 ++ devLib/piFace.h | 32 + devLib/piFaceOld.c | 178 ++ {wiringPi => devLib}/piNes.c | 0 {wiringPi => devLib}/piNes.h | 0 examples/Gertboard/7segments.c | 221 +++ examples/Gertboard/Makefile | 74 + examples/Gertboard/buttons.c | 83 + examples/{ => Gertboard}/gertboard.c | 62 +- examples/Gertboard/record.c | 60 + examples/Gertboard/temperature.c | 78 + examples/Gertboard/voltmeter.c | 73 + examples/Gertboard/vumeter.c | 152 ++ examples/Makefile | 55 +- examples/PiFace/Makefile | 85 + examples/PiFace/blink.c | 59 + examples/PiFace/buttons.c | 103 + examples/PiFace/ladder.c | 337 ++++ examples/PiFace/metro.c | 111 ++ examples/PiFace/motor.c | 120 ++ examples/PiFace/reaction.c | 194 ++ examples/blink.c | 8 +- examples/{test1.c => blink12.c} | 54 +- examples/{test2.c => blink8.c} | 37 +- examples/clock.c | 201 ++ examples/ds1302.c | 238 +++ examples/gertboard.png | Bin 4834 -> 0 bytes examples/isr.c | 63 +- examples/lcd.c | 248 ++- examples/okLed.c | 13 +- examples/pwm.c | 79 +- examples/{piface.c => rht03.c} | 59 +- examples/softPwm.c | 89 + examples/{tone.c => softTone.c} | 9 +- examples/speed.c | 114 +- gpio/Makefile | 14 +- gpio/extensions.c | 329 ++++ gpio/extensions.h | 26 + gpio/gpio.1 | 91 +- gpio/gpio.c | 384 +++- gpio/pintest | 193 ++ pins/Makefile | 18 + pins/pins.pdf | Bin 0 -> 9833 bytes pins/pins.tex | 116 ++ wiringPi/Makefile | 102 +- wiringPi/README | 9 - wiringPi/drc.c | 203 ++ wiringPi/drc.h | 34 + wiringPi/mcp23008.c | 149 ++ wiringPi/mcp23008.h | 33 + wiringPi/mcp23017.c | 195 ++ wiringPi/mcp23017.h | 33 + wiringPi/mcp23s08.c | 189 ++ wiringPi/mcp23s08.h | 33 + wiringPi/mcp23s17.c | 236 +++ wiringPi/mcp23s17.h | 33 + wiringPi/mcp23x08.h | 73 + wiringPi/mcp23x0817.h | 87 + wiringPi/mcp3002.c | 76 + wiringPi/mcp3002.h | 33 + wiringPi/mcp3422.c | 116 ++ wiringPi/mcp3422.h | 43 + wiringPi/mcp4802.c | 76 + wiringPi/mcp4802.h | 33 + wiringPi/pcf8574.c | 126 ++ wiringPi/pcf8574.h | 33 + wiringPi/piHiPri.c | 7 +- wiringPi/softPwm.c | 4 +- wiringPi/softTone.c | 24 +- wiringPi/softTone.h | 2 +- wiringPi/sr595.c | 109 ++ wiringPi/sr595.h | 34 + wiringPi/wiringPi.c | 1180 +++++++----- wiringPi/wiringPi.h | 98 +- wiringPi/wiringPiFace.c | 362 ---- wiringPi/wiringPiI2C.c | 158 +- wiringPi/wiringPiI2C.h | 15 +- wiringPi/wiringPiSPI.c | 28 +- wiringPi/wiringSerial.c | 17 +- wiringPi/wiringSerial.h | 16 +- wiringPi/wiringShift.c | 1 - wiringPi/wiringShift.h | 4 +- 97 files changed, 11243 insertions(+), 1617 deletions(-) create mode 100644 devLib/Makefile create mode 100644 devLib/ds1302.c rename wiringPi/lcd.h => devLib/ds1302.h (58%) create mode 100644 devLib/font.h rename {wiringPi => devLib}/gertboard.c (68%) rename {wiringPi => devLib}/gertboard.h (86%) rename {wiringPi => devLib}/lcd.c (64%) create mode 100644 devLib/lcd.h create mode 100644 devLib/lcd128x64.c create mode 100644 devLib/lcd128x64.h create mode 100755 devLib/maxdetect.c create mode 100755 devLib/maxdetect.h create mode 100644 devLib/piFace.c create mode 100644 devLib/piFace.h create mode 100644 devLib/piFaceOld.c rename {wiringPi => devLib}/piNes.c (100%) rename {wiringPi => devLib}/piNes.h (100%) create mode 100644 examples/Gertboard/7segments.c create mode 100644 examples/Gertboard/Makefile create mode 100644 examples/Gertboard/buttons.c rename examples/{ => Gertboard}/gertboard.c (61%) create mode 100644 examples/Gertboard/record.c create mode 100644 examples/Gertboard/temperature.c create mode 100644 examples/Gertboard/voltmeter.c create mode 100644 examples/Gertboard/vumeter.c create mode 100644 examples/PiFace/Makefile create mode 100644 examples/PiFace/blink.c create mode 100644 examples/PiFace/buttons.c create mode 100755 examples/PiFace/ladder.c create mode 100644 examples/PiFace/metro.c create mode 100644 examples/PiFace/motor.c create mode 100644 examples/PiFace/reaction.c rename examples/{test1.c => blink12.c} (70%) rename examples/{test2.c => blink8.c} (66%) create mode 100644 examples/clock.c create mode 100644 examples/ds1302.c delete mode 100644 examples/gertboard.png rename examples/{piface.c => rht03.c} (60%) mode change 100644 => 100755 create mode 100644 examples/softPwm.c rename examples/{tone.c => softTone.c} (92%) create mode 100644 gpio/extensions.c create mode 100644 gpio/extensions.h create mode 100755 gpio/pintest create mode 100644 pins/Makefile create mode 100644 pins/pins.pdf create mode 100644 pins/pins.tex delete mode 100644 wiringPi/README create mode 100644 wiringPi/drc.c create mode 100644 wiringPi/drc.h create mode 100644 wiringPi/mcp23008.c create mode 100644 wiringPi/mcp23008.h create mode 100644 wiringPi/mcp23017.c create mode 100644 wiringPi/mcp23017.h create mode 100644 wiringPi/mcp23s08.c create mode 100644 wiringPi/mcp23s08.h create mode 100644 wiringPi/mcp23s17.c create mode 100644 wiringPi/mcp23s17.h create mode 100644 wiringPi/mcp23x08.h create mode 100644 wiringPi/mcp23x0817.h create mode 100644 wiringPi/mcp3002.c create mode 100644 wiringPi/mcp3002.h create mode 100644 wiringPi/mcp3422.c create mode 100644 wiringPi/mcp3422.h create mode 100644 wiringPi/mcp4802.c create mode 100644 wiringPi/mcp4802.h create mode 100644 wiringPi/pcf8574.c create mode 100644 wiringPi/pcf8574.h create mode 100644 wiringPi/sr595.c create mode 100644 wiringPi/sr595.h delete mode 100644 wiringPi/wiringPiFace.c diff --git a/INSTALL b/INSTALL index 8a6d38e..8e0c43c 100644 --- a/INSTALL +++ b/INSTALL @@ -30,18 +30,6 @@ To un-install wiringPi: ./build uninstall - -I2C: - -If your system has the correct i2c-dev libraries and headers installed, -then the I2C helpers will be compiled into wiringPi. If you want to -use the I2C helpers and don't have them installed, then under Raspbian, -issue the command: - - sudo apt-get install libi2c-dev - -Consult the documentation for your system if you are not running Raspbian. - Gordon Henderson projects@drogon.net diff --git a/README.TXT b/README.TXT index 0fce86a..7789b2e 100644 --- a/README.TXT +++ b/README.TXT @@ -16,7 +16,7 @@ accepted to Github.... Please see - https://projects.drogon.net/raspberry-pi/wiringpi/ + http://wiringpi.com/ for the official documentation, etc. and the best way to submit bug reports, etc. is by sending an email to projects@drogon.net diff --git a/build b/build index cbb1a4f..048ebb3 100755 --- a/build +++ b/build @@ -14,28 +14,28 @@ check-make-ok() } if [ x$1 = "xclean" ]; then - echo Cleaning - echo cd wiringPi - make clean + echo -n "wiringPi: " ; make clean + cd ../devLib + echo -n "DevLib: " ; make clean cd ../gpio - make clean + echo -n "gpio: " ; make clean cd ../examples - make clean + echo -n "Examples: " ; make clean + cd Gertboard + echo -n "Gertboard: " ; make clean + cd ../PiFace + echo -n "PiFace: " ; make clean exit fi if [ x$1 = "xuninstall" ]; then - echo Uninstalling - echo - echo "WiringPi library" cd wiringPi - sudo make uninstall - echo - echo "GPIO Utility" + echo -n "wiringPi: " ; sudo make uninstall + cd ../devLib + echo -n "DevLib: " ; sudo make uninstall cd ../gpio - sudo make uninstall - cd .. + echo -n "gpio: " ; sudo make uninstall exit fi @@ -44,25 +44,20 @@ fi echo "=====================" echo -# Check for I2C being installed... -# ... and if-so, then automatically make the I2C helpers - - if [ -f /usr/include/linux/i2c-dev.h ]; then - grep -q i2c_smbus_read_byte /usr/include/linux/i2c-dev.h - if [ $? = 0 ]; then - target=i2c - echo "Building wiringPi with the I2C helper libraries." - else - target=all - echo "The wiringPi I2C helper libraries will not be built." - fi - fi - echo - echo "WiringPi library" + echo "WiringPi Library" cd wiringPi sudo make uninstall - make $target + make + check-make-ok + sudo make install + check-make-ok + + echo + echo "WiringPi Devices Library" + cd ../devLib + sudo make uninstall + make check-make-ok sudo make install check-make-ok diff --git a/devLib/Makefile b/devLib/Makefile new file mode 100644 index 0000000..a106d93 --- /dev/null +++ b/devLib/Makefile @@ -0,0 +1,130 @@ +# Makefile: +# wiringPi device - Wiring Compatable library for the Raspberry Pi +# +# Copyright (c) 2012-2013 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# https://projects.drogon.net/raspberry-pi/wiringpi/ +# +# wiringPi is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# wiringPi is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with wiringPi. If not, see . +################################################################################# + +DYN_VERS_MAJ=2 +DYN_VERS_MIN=0 + +VERSION=$(DYN_VERS_MAJ).$(DYN_VERS_MIN) +DESTDIR=/usr +PREFIX=/local + +STATIC=libwiringPiDev.a +DYNAMIC=libwiringPiDev.so.$(VERSION) + +#DEBUG = -g -O0 +DEBUG = -O2 +CC = gcc +INCLUDE = -I. +CFLAGS = $(DEBUG) -Wformat=2 -Wall $(INCLUDE) -Winline -pipe -fPIC + +LIBS = + +############################################################################### + +SRC = ds1302.c maxdetect.c piNes.c \ + gertboard.c piFace.c \ + lcd128x64.c lcd.c + +OBJ = $(SRC:.c=.o) + +all: $(DYNAMIC) + +static: $(STATIC) + +$(STATIC): $(OBJ) + @echo "[Link (Static)]" + @ar rcs $(STATIC) $(OBJ) + @ranlib $(STATIC) +# @size $(STATIC) + +$(DYNAMIC): $(OBJ) + @echo "[Link (Dynamic)]" + @$(CC) -shared -Wl,-soname,libwiringPiDev.so -o libwiringPiDev.so.$(VERSION) -lpthread $(OBJ) + +.c.o: + @echo [Compile] $< + @$(CC) -c $(CFLAGS) $< -o $@ + +.PHONEY: clean +clean: + @echo "[Clean]" + @rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPiDev.* + +.PHONEY: tags +tags: $(SRC) + @echo [ctags] + @ctags $(SRC) + + +.PHONEY: install-headers +install-headers: + @echo "[Install Headers]" + @install -m 0755 -d $(DESTDIR)$(PREFIX)/include + @install -m 0644 ds1302.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 maxdetect.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 piNes.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 gertboard.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 piFace.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 lcd128x64.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include + +.PHONEY: install +install: $(DYNAMIC) install-headers + @echo "[Install Dynamic Lib]" + @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + @install -m 0755 libwiringPiDev.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) + @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) $(DESTDIR)/lib/libwiringPiDev.so + @ldconfig + +.PHONEY: install-static +install-static: $(STATIC) install-headers + @echo "[Install Static Lib]" + @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + @install -m 0755 libwiringPiDev.a $(DESTDIR)$(PREFIX)/lib + +.PHONEY: uninstall +uninstall: + @echo "[UnInstall]" + @rm -f $(DESTDIR)$(PREFIX)/include/ds1302.h + @rm -f $(DESTDIR)$(PREFIX)/include/maxdetect.h + @rm -f $(DESTDIR)$(PREFIX)/include/piNes.h + @rm -f $(DESTDIR)$(PREFIX)/include/gertboard.h + @rm -f $(DESTDIR)$(PREFIX)/include/piFace.h + @rm -f $(DESTDIR)$(PREFIX)/include/lcd128x64.h + @rm -f $(DESTDIR)$(PREFIX)/include/lcd.h + @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.* + @ldconfig + + +.PHONEY: depend +depend: + makedepend -Y $(SRC) + +# DO NOT DELETE + +ds1302.o: ds1302.h +maxdetect.o: maxdetect.h +piNes.o: piNes.h +gertboard.o: gertboard.h +piFace.o: piFace.h +lcd128x64.o: font.h lcd128x64.h +lcd.o: lcd.h diff --git a/devLib/ds1302.c b/devLib/ds1302.c new file mode 100644 index 0000000..cf64de7 --- /dev/null +++ b/devLib/ds1302.c @@ -0,0 +1,240 @@ +/* + * ds1302.c: + * Real Time clock + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include + +#include + +#include "ds1302.h" + +// Register defines + +#define RTC_SECS 0 +#define RTC_MINS 1 +#define RTC_HOURS 2 +#define RTC_DATE 3 +#define RTC_MONTH 4 +#define RTC_DAY 5 +#define RTC_YEAR 6 +#define RTC_WP 7 +#define RTC_TC 8 +#define RTC_BM 31 + + +// Locals + +static int dPin, cPin, sPin ; + +/* + * dsShiftIn: + * Shift a number in from the chip, LSB first. Note that the data is + * sampled on the trailing edge of the last clock, so it's valid immediately. + ********************************************************************************* + */ + +static unsigned int dsShiftIn (void) +{ + uint8_t value = 0 ; + int i ; + + pinMode (dPin, INPUT) ; delayMicroseconds (1) ; + + for (i = 0 ; i < 8 ; ++i) + { + value |= (digitalRead (dPin) << i) ; + digitalWrite (cPin, HIGH) ; delayMicroseconds (1) ; + digitalWrite (cPin, LOW) ; delayMicroseconds (1) ; + } + + return value; +} + + +/* + * dsShiftOut: + * A normal LSB-first shift-out, just slowed down a bit - the Pi is + * a bit faster than the chip can handle. + ********************************************************************************* + */ + +static void dsShiftOut (unsigned int data) +{ + int i ; + + pinMode (dPin, OUTPUT) ; + + for (i = 0 ; i < 8 ; ++i) + { + digitalWrite (dPin, data & (1 << i)) ; delayMicroseconds (1) ; + digitalWrite (cPin, HIGH) ; delayMicroseconds (1) ; + digitalWrite (cPin, LOW) ; delayMicroseconds (1) ; + } +} + + +/* + * ds1302regRead: ds1302regWrite: + * Read/Write a value to an RTC Register or RAM location on the chip + ********************************************************************************* + */ + +static unsigned int ds1302regRead (const int reg) +{ + unsigned int data ; + + digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ; + dsShiftOut (reg) ; + data = dsShiftIn () ; + digitalWrite (sPin, LOW) ; delayMicroseconds (1) ; + + return data ; +} + +static void ds1302regWrite (const int reg, const unsigned int data) +{ + digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ; + dsShiftOut (reg) ; + dsShiftOut (data) ; + digitalWrite (sPin, LOW) ; delayMicroseconds (1) ; +} + + +/* + * ds1302rtcWrite: ds1302rtcRead: + * Writes/Reads the data to/from the RTC register + ********************************************************************************* + */ + +unsigned int ds1302rtcRead (const int reg) +{ + return ds1302regRead (0x81 | ((reg & 0x1F) << 1)) ; +} + +void ds1302rtcWrite (int reg, unsigned int data) +{ + ds1302regWrite (0x80 | ((reg & 0x1F) << 1), data) ; +} + + +/* + * ds1302ramWrite: ds1302ramRead: + * Writes/Reads the data to/from the RTC register + ********************************************************************************* + */ + +unsigned int ds1302ramRead (const int addr) +{ + return ds1302regRead (0xC1 | ((addr & 0x1F) << 1)) ; +} + +void ds1302ramWrite (const int addr, const unsigned int data) +{ + ds1302regWrite ( 0xC0 | ((addr & 0x1F) << 1), data) ; +} + +/* + * ds1302clockRead: + * Read all 8 bytes of the clock in a single operation + ********************************************************************************* + */ + +void ds1302clockRead (int clockData [8]) +{ + int i ; + unsigned int regVal = 0x81 | ((RTC_BM & 0x1F) << 1) ; + + digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ; + + dsShiftOut (regVal) ; + for (i = 0 ; i < 8 ; ++i) + clockData [i] = dsShiftIn () ; + + digitalWrite (sPin, LOW) ; delayMicroseconds (1) ; +} + + +/* + * ds1302clockWrite: + * Write all 8 bytes of the clock in a single operation + ********************************************************************************* + */ + +void ds1302clockWrite (const int clockData [8]) +{ + int i ; + unsigned int regVal = 0x80 | ((RTC_BM & 0x1F) << 1) ; + + digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ; + + dsShiftOut (regVal) ; + for (i = 0 ; i < 8 ; ++i) + dsShiftOut (clockData [i]) ; + + digitalWrite (sPin, LOW) ; delayMicroseconds (1) ; +} + + +/* + * ds1302trickleCharge: + * Set the bits on the trickle charger. + * Probably best left alone... + ********************************************************************************* + */ + +void ds1302trickleCharge (const int diodes, const int resistors) +{ + if (diodes + resistors == 0) + ds1302rtcWrite (RTC_TC, 0x5C) ; // Disabled + else + ds1302rtcWrite (RTC_TC, 0xA0 | ((diodes & 3) << 2) | (resistors & 3)) ; +} + + + + +/* + * ds1302setup: + * Initialise the chip & remember the pins we're using + ********************************************************************************* + */ + +void ds1302setup (const int clockPin, const int dataPin, const int csPin) +{ + dPin = dataPin ; + cPin = clockPin ; + sPin = csPin ; + + digitalWrite (dPin, LOW) ; + digitalWrite (cPin, LOW) ; + digitalWrite (sPin, LOW) ; + + pinMode (dPin, OUTPUT) ; + pinMode (cPin, OUTPUT) ; + pinMode (sPin, OUTPUT) ; + + ds1302rtcWrite (RTC_WP, 0) ; // Remove write-protect +} diff --git a/wiringPi/lcd.h b/devLib/ds1302.h similarity index 58% rename from wiringPi/lcd.h rename to devLib/ds1302.h index beebb75..e82b3ed 100644 --- a/wiringPi/lcd.h +++ b/devLib/ds1302.h @@ -1,10 +1,8 @@ /* - * lcd.h: - * Text-based LCD driver. - * This is designed to drive the parallel interface LCD drivers - * based in the Hitachi HD44780U controller and compatables. + * ds1302.h: + * Real Time clock * - * Copyright (c) 2012 Gordon Henderson. + * Copyright (c) 2013 Gordon Henderson. *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -24,22 +22,22 @@ *********************************************************************** */ -#define MAX_LCDS 8 - #ifdef __cplusplus extern "C" { #endif -extern void lcdHome (int fd) ; -extern void lcdClear (int fd) ; -extern void lcdSendCommand (int fd, uint8_t command) ; -extern void lcdPosition (int fd, int x, int y) ; -extern void lcdPutchar (int fd, uint8_t data) ; -extern void lcdPuts (int fd, char *string) ; -extern void lcdPrintf (int fd, char *message, ...) ; +extern unsigned int ds1302rtcRead (const int reg) ; +extern void ds1302rtcWrite (const int reg, const unsigned int data) ; -extern int lcdInit (int rows, int cols, int bits, int rs, int strb, - int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) ; +extern unsigned int ds1302ramRead (const int addr) ; +extern void ds1302ramWrite (const int addr, const unsigned int data) ; + +extern void ds1302clockRead (int clockData [8]) ; +extern void ds1302clockWrite (const int clockData [8]) ; + +extern void ds1302trickleCharge (const int diodes, const int resistors) ; + +extern void ds1302setup (const int clockPin, const int dataPin, const int csPin) ; #ifdef __cplusplus } diff --git a/devLib/font.h b/devLib/font.h new file mode 100644 index 0000000..ce99e16 --- /dev/null +++ b/devLib/font.h @@ -0,0 +1,2577 @@ +/**********************************************/ +/* */ +/* Font file generated by cpi2fnt */ +/* ------------------------------ */ +/* Combined with the alpha-numeric */ +/* portion of Greg Harp's old PEARL */ +/* font (from earlier versions of */ +/* linux-m86k) by John Shifflett */ +/* */ +/**********************************************/ + +static const int fontHeight = 8 ; +static const int fontWidth = 8 ; + +static unsigned char font [] = +{ + /* 0 0x00 '^@' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 1 0x01 '^A' */ + 0x7e, /* 01111110 */ + 0x81, /* 10000001 */ + 0xa5, /* 10100101 */ + 0x81, /* 10000001 */ + 0xbd, /* 10111101 */ + 0x99, /* 10011001 */ + 0x81, /* 10000001 */ + 0x7e, /* 01111110 */ + + /* 2 0x02 '^B' */ + 0x7e, /* 01111110 */ + 0xff, /* 11111111 */ + 0xdb, /* 11011011 */ + 0xff, /* 11111111 */ + 0xc3, /* 11000011 */ + 0xe7, /* 11100111 */ + 0xff, /* 11111111 */ + 0x7e, /* 01111110 */ + + /* 3 0x03 '^C' */ + 0x6c, /* 01101100 */ + 0xfe, /* 11111110 */ + 0xfe, /* 11111110 */ + 0xfe, /* 11111110 */ + 0x7c, /* 01111100 */ + 0x38, /* 00111000 */ + 0x10, /* 00010000 */ + 0x00, /* 00000000 */ + + /* 4 0x04 '^D' */ + 0x10, /* 00010000 */ + 0x38, /* 00111000 */ + 0x7c, /* 01111100 */ + 0xfe, /* 11111110 */ + 0x7c, /* 01111100 */ + 0x38, /* 00111000 */ + 0x10, /* 00010000 */ + 0x00, /* 00000000 */ + + /* 5 0x05 '^E' */ + 0x38, /* 00111000 */ + 0x7c, /* 01111100 */ + 0x38, /* 00111000 */ + 0xfe, /* 11111110 */ + 0xfe, /* 11111110 */ + 0xd6, /* 11010110 */ + 0x10, /* 00010000 */ + 0x38, /* 00111000 */ + + /* 6 0x06 '^F' */ + 0x10, /* 00010000 */ + 0x38, /* 00111000 */ + 0x7c, /* 01111100 */ + 0xfe, /* 11111110 */ + 0xfe, /* 11111110 */ + 0x7c, /* 01111100 */ + 0x10, /* 00010000 */ + 0x38, /* 00111000 */ + + /* 7 0x07 '^G' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 8 0x08 '^H' */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xe7, /* 11100111 */ + 0xc3, /* 11000011 */ + 0xc3, /* 11000011 */ + 0xe7, /* 11100111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + + /* 9 0x09 '^I' */ + 0x00, /* 00000000 */ + 0x3c, /* 00111100 */ + 0x66, /* 01100110 */ + 0x42, /* 01000010 */ + 0x42, /* 01000010 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x00, /* 00000000 */ + + /* 10 0x0a '^J' */ + 0xff, /* 11111111 */ + 0xc3, /* 11000011 */ + 0x99, /* 10011001 */ + 0xbd, /* 10111101 */ + 0xbd, /* 10111101 */ + 0x99, /* 10011001 */ + 0xc3, /* 11000011 */ + 0xff, /* 11111111 */ + + /* 11 0x0b '^K' */ + 0x0f, /* 00001111 */ + 0x07, /* 00000111 */ + 0x0f, /* 00001111 */ + 0x7d, /* 01111101 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0x78, /* 01111000 */ + + /* 12 0x0c '^L' */ + 0x3c, /* 00111100 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + + /* 13 0x0d '^M' */ + 0x3f, /* 00111111 */ + 0x33, /* 00110011 */ + 0x3f, /* 00111111 */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x70, /* 01110000 */ + 0xf0, /* 11110000 */ + 0xe0, /* 11100000 */ + + /* 14 0x0e '^N' */ + 0x7f, /* 01111111 */ + 0x63, /* 01100011 */ + 0x7f, /* 01111111 */ + 0x63, /* 01100011 */ + 0x63, /* 01100011 */ + 0x67, /* 01100111 */ + 0xe6, /* 11100110 */ + 0xc0, /* 11000000 */ + + /* 15 0x0f '^O' */ + 0x18, /* 00011000 */ + 0xdb, /* 11011011 */ + 0x3c, /* 00111100 */ + 0xe7, /* 11100111 */ + 0xe7, /* 11100111 */ + 0x3c, /* 00111100 */ + 0xdb, /* 11011011 */ + 0x18, /* 00011000 */ + + /* 16 0x10 '^P' */ + 0x80, /* 10000000 */ + 0xe0, /* 11100000 */ + 0xf8, /* 11111000 */ + 0xfe, /* 11111110 */ + 0xf8, /* 11111000 */ + 0xe0, /* 11100000 */ + 0x80, /* 10000000 */ + 0x00, /* 00000000 */ + + /* 17 0x11 '^Q' */ + 0x02, /* 00000010 */ + 0x0e, /* 00001110 */ + 0x3e, /* 00111110 */ + 0xfe, /* 11111110 */ + 0x3e, /* 00111110 */ + 0x0e, /* 00001110 */ + 0x02, /* 00000010 */ + 0x00, /* 00000000 */ + + /* 18 0x12 '^R' */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + + /* 19 0x13 '^S' */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x00, /* 00000000 */ + 0x66, /* 01100110 */ + 0x00, /* 00000000 */ + + /* 20 0x14 '^T' */ + 0x7f, /* 01111111 */ + 0xdb, /* 11011011 */ + 0xdb, /* 11011011 */ + 0x7b, /* 01111011 */ + 0x1b, /* 00011011 */ + 0x1b, /* 00011011 */ + 0x1b, /* 00011011 */ + 0x00, /* 00000000 */ + + /* 21 0x15 '^U' */ + 0x3e, /* 00111110 */ + 0x61, /* 01100001 */ + 0x3c, /* 00111100 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x86, /* 10000110 */ + 0x7c, /* 01111100 */ + + /* 22 0x16 '^V' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x7e, /* 01111110 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + + /* 23 0x17 '^W' */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0xff, /* 11111111 */ + + /* 24 0x18 '^X' */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 25 0x19 '^Y' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 26 0x1a '^Z' */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0xfe, /* 11111110 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 27 0x1b '^[' */ + 0x00, /* 00000000 */ + 0x30, /* 00110000 */ + 0x60, /* 01100000 */ + 0xfe, /* 11111110 */ + 0x60, /* 01100000 */ + 0x30, /* 00110000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 28 0x1c '^\' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 29 0x1d '^]' */ + 0x00, /* 00000000 */ + 0x24, /* 00100100 */ + 0x66, /* 01100110 */ + 0xff, /* 11111111 */ + 0x66, /* 01100110 */ + 0x24, /* 00100100 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 30 0x1e '^^' */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x7e, /* 01111110 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 31 0x1f '^_' */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0x7e, /* 01111110 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 32 0x20 ' ' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 33 0x21 '!' */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x3c, /* 00111100 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 34 0x22 '"' */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 35 0x23 '#' */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0xfe, /* 11111110 */ + 0x6c, /* 01101100 */ + 0xfe, /* 11111110 */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0x00, /* 00000000 */ + + /* 36 0x24 '$' */ + 0x18, /* 00011000 */ + 0x3e, /* 00111110 */ + 0x60, /* 01100000 */ + 0x3c, /* 00111100 */ + 0x06, /* 00000110 */ + 0x7c, /* 01111100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 37 0x25 '%' */ + 0x00, /* 00000000 */ + 0xc6, /* 11000110 */ + 0xcc, /* 11001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x66, /* 01100110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 38 0x26 '&' */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0x68, /* 01101000 */ + 0x76, /* 01110110 */ + 0xdc, /* 11011100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 39 0x27 ''' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 40 0x28 '(' */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0x00, /* 00000000 */ + + /* 41 0x29 ')' */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0x0c, /* 00001100 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x00, /* 00000000 */ + + /* 42 0x2a '*' */ + 0x00, /* 00000000 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0xff, /* 11111111 */ + 0x3c, /* 00111100 */ + 0x66, /* 01100110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 43 0x2b '+' */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 44 0x2c ',' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + + /* 45 0x2d '-' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 46 0x2e '.' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 47 0x2f '/' */ + 0x03, /* 00000011 */ + 0x06, /* 00000110 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x60, /* 01100000 */ + 0xc0, /* 11000000 */ + 0x00, /* 00000000 */ + + /* 48 0x30 '0' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xde, /* 11011110 */ + 0xfe, /* 11111110 */ + 0xf6, /* 11110110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 49 0x31 '1' */ + 0x18, /* 00011000 */ + 0x78, /* 01111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 50 0x32 '2' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x60, /* 01100000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + + /* 51 0x33 '3' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0x06, /* 00000110 */ + 0x1c, /* 00011100 */ + 0x06, /* 00000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 52 0x34 '4' */ + 0x1c, /* 00011100 */ + 0x3c, /* 00111100 */ + 0x6c, /* 01101100 */ + 0xcc, /* 11001100 */ + 0xfe, /* 11111110 */ + 0x0c, /* 00001100 */ + 0x0c, /* 00001100 */ + 0x00, /* 00000000 */ + + /* 53 0x35 '5' */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0xfc, /* 11111100 */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 54 0x36 '6' */ + 0x38, /* 00111000 */ + 0x60, /* 01100000 */ + 0xc0, /* 11000000 */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 55 0x37 '7' */ + 0xfe, /* 11111110 */ + 0x06, /* 00000110 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x60, /* 01100000 */ + 0x60, /* 01100000 */ + 0x00, /* 00000000 */ + + /* 56 0x38 '8' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 57 0x39 '9' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7e, /* 01111110 */ + 0x06, /* 00000110 */ + 0x0c, /* 00001100 */ + 0x38, /* 00111000 */ + 0x00, /* 00000000 */ + + /* 58 0x3a ':' */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 59 0x3b ';' */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + + /* 60 0x3c '<' */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x60, /* 01100000 */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0x00, /* 00000000 */ + + /* 61 0x3d '=' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 62 0x3e '>' */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0x06, /* 00000110 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x00, /* 00000000 */ + + /* 63 0x3f '?' */ + 0x3c, /* 00111100 */ + 0x66, /* 01100110 */ + 0x06, /* 00000110 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 64 0x40 '@' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xde, /* 11011110 */ + 0xde, /* 11011110 */ + 0xde, /* 11011110 */ + 0xc0, /* 11000000 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 65 0x41 'A' */ + 0x10, /* 00010000 */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 66 0x42 'B' */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xfc, /* 11111100 */ + 0x00, /* 00000000 */ + + /* 67 0x43 'C' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 68 0x44 'D' */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xfc, /* 11111100 */ + 0x00, /* 00000000 */ + + /* 69 0x45 'E' */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xf8, /* 11111000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + + /* 70 0x46 'F' */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xf8, /* 11111000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0x00, /* 00000000 */ + + /* 71 0x47 'G' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc0, /* 11000000 */ + 0xce, /* 11001110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 72 0x48 'H' */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 73 0x49 'I' */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + + /* 74 0x4a 'J' */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 75 0x4b 'K' */ + 0xc6, /* 11000110 */ + 0xcc, /* 11001100 */ + 0xd8, /* 11011000 */ + 0xf0, /* 11110000 */ + 0xd8, /* 11011000 */ + 0xcc, /* 11001100 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 76 0x4c 'L' */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + + /* 77 0x4d 'M' */ + 0x82, /* 10000010 */ + 0xc6, /* 11000110 */ + 0xee, /* 11101110 */ + 0xfe, /* 11111110 */ + 0xd6, /* 11010110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 78 0x4e 'N' */ + 0xc6, /* 11000110 */ + 0xe6, /* 11100110 */ + 0xf6, /* 11110110 */ + 0xde, /* 11011110 */ + 0xce, /* 11001110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 79 0x4f 'O' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 80 0x50 'P' */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xfc, /* 11111100 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0x00, /* 00000000 */ + + /* 81 0x51 'Q' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xf6, /* 11110110 */ + 0xde, /* 11011110 */ + 0x7c, /* 01111100 */ + 0x06, /* 00000110 */ + + /* 82 0x52 'R' */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xfc, /* 11111100 */ + 0xd8, /* 11011000 */ + 0xcc, /* 11001100 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 83 0x53 'S' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0x60, /* 01100000 */ + 0x38, /* 00111000 */ + 0x0c, /* 00001100 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 84 0x54 'T' */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 85 0x55 'U' */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 86 0x56 'V' */ + 0xc3, /* 11000011 */ + 0xc3, /* 11000011 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 87 0x57 'W' */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xd6, /* 11010110 */ + 0xfe, /* 11111110 */ + 0xee, /* 11101110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 88 0x58 'X' */ + 0xc3, /* 11000011 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x66, /* 01100110 */ + 0xc3, /* 11000011 */ + 0x00, /* 00000000 */ + + /* 89 0x59 'Y' */ + 0xc3, /* 11000011 */ + 0xc3, /* 11000011 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 90 0x5a 'Z' */ + 0xfe, /* 11111110 */ + 0x06, /* 00000110 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x60, /* 01100000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + + /* 91 0x5b '[' */ + 0x3c, /* 00111100 */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x3c, /* 00111100 */ + 0x00, /* 00000000 */ + + /* 92 0x5c '\' */ + 0xc0, /* 11000000 */ + 0x60, /* 01100000 */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0x06, /* 00000110 */ + 0x03, /* 00000011 */ + 0x00, /* 00000000 */ + + /* 93 0x5d ']' */ + 0x3c, /* 00111100 */ + 0x0c, /* 00001100 */ + 0x0c, /* 00001100 */ + 0x0c, /* 00001100 */ + 0x0c, /* 00001100 */ + 0x0c, /* 00001100 */ + 0x3c, /* 00111100 */ + 0x00, /* 00000000 */ + + /* 94 0x5e '^' */ + 0x10, /* 00010000 */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 95 0x5f '_' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + + /* 96 0x60 '`' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 97 0x61 'a' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7c, /* 01111100 */ + 0x06, /* 00000110 */ + 0x7e, /* 01111110 */ + 0xc6, /* 11000110 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + + /* 98 0x62 'b' */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xfc, /* 11111100 */ + 0x00, /* 00000000 */ + + /* 99 0x63 'c' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc0, /* 11000000 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 100 0x64 'd' */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + 0x7e, /* 01111110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + + /* 101 0x65 'e' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 102 0x66 'f' */ + 0x3c, /* 00111100 */ + 0x66, /* 01100110 */ + 0x60, /* 01100000 */ + 0xf0, /* 11110000 */ + 0x60, /* 01100000 */ + 0x60, /* 01100000 */ + 0x60, /* 01100000 */ + 0x00, /* 00000000 */ + + /* 103 0x67 'g' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7e, /* 01111110 */ + 0x06, /* 00000110 */ + 0x7c, /* 01111100 */ + + /* 104 0x68 'h' */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 105 0x69 'i' */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x38, /* 00111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 106 0x6a 'j' */ + 0x06, /* 00000110 */ + 0x00, /* 00000000 */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + + /* 107 0x6b 'k' */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xcc, /* 11001100 */ + 0xd8, /* 11011000 */ + 0xf0, /* 11110000 */ + 0xd8, /* 11011000 */ + 0xcc, /* 11001100 */ + 0x00, /* 00000000 */ + + /* 108 0x6c 'l' */ + 0x38, /* 00111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 109 0x6d 'm' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xec, /* 11101100 */ + 0xfe, /* 11111110 */ + 0xd6, /* 11010110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 110 0x6e 'n' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 111 0x6f 'o' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 112 0x70 'p' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xfc, /* 11111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xfc, /* 11111100 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + + /* 113 0x71 'q' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7e, /* 01111110 */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + + /* 114 0x72 'r' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xdc, /* 11011100 */ + 0xe6, /* 11100110 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0x00, /* 00000000 */ + + /* 115 0x73 's' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0xc0, /* 11000000 */ + 0x7c, /* 01111100 */ + 0x06, /* 00000110 */ + 0xfc, /* 11111100 */ + 0x00, /* 00000000 */ + + /* 116 0x74 't' */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x7c, /* 01111100 */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x36, /* 00110110 */ + 0x1c, /* 00011100 */ + 0x00, /* 00000000 */ + + /* 117 0x75 'u' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 118 0x76 'v' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x6c, /* 01101100 */ + 0x38, /* 00111000 */ + 0x00, /* 00000000 */ + + /* 119 0x77 'w' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xd6, /* 11010110 */ + 0xfe, /* 11111110 */ + 0x6c, /* 01101100 */ + 0x00, /* 00000000 */ + + /* 120 0x78 'x' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xc6, /* 11000110 */ + 0x6c, /* 01101100 */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 121 0x79 'y' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xc3, /* 11000011 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x60, /* 01100000 */ + + /* 122 0x7a 'z' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + 0x0c, /* 00001100 */ + 0x38, /* 00111000 */ + 0x60, /* 01100000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + + /* 123 0x7b '{' */ + 0x0e, /* 00001110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x70, /* 01110000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x0e, /* 00001110 */ + 0x00, /* 00000000 */ + + /* 124 0x7c '|' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 125 0x7d '}' */ + 0x70, /* 01110000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x0e, /* 00001110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x70, /* 01110000 */ + 0x00, /* 00000000 */ + + /* 126 0x7e '~' */ + 0x72, /* 01110010 */ + 0x9c, /* 10011100 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 127 0x7f '' */ + 0x00, /* 00000000 */ + 0x10, /* 00010000 */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + + /* 128 0x80 '' */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x0c, /* 00001100 */ + 0x78, /* 01111000 */ + + /* 129 0x81 '' */ + 0xcc, /* 11001100 */ + 0x00, /* 00000000 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 130 0x82 '' */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 131 0x83 '' */ + 0x7c, /* 01111100 */ + 0x82, /* 10000010 */ + 0x78, /* 01111000 */ + 0x0c, /* 00001100 */ + 0x7c, /* 01111100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 132 0x84 '' */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + 0x78, /* 01111000 */ + 0x0c, /* 00001100 */ + 0x7c, /* 01111100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 133 0x85 '' */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x78, /* 01111000 */ + 0x0c, /* 00001100 */ + 0x7c, /* 01111100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 134 0x86 '' */ + 0x30, /* 00110000 */ + 0x30, /* 00110000 */ + 0x78, /* 01111000 */ + 0x0c, /* 00001100 */ + 0x7c, /* 01111100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 135 0x87 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0x7e, /* 01111110 */ + 0x0c, /* 00001100 */ + 0x38, /* 00111000 */ + + /* 136 0x88 '' */ + 0x7c, /* 01111100 */ + 0x82, /* 10000010 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 137 0x89 '' */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 138 0x8a '' */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 139 0x8b '' */ + 0x66, /* 01100110 */ + 0x00, /* 00000000 */ + 0x38, /* 00111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x00, /* 00000000 */ + + /* 140 0x8c '' */ + 0x7c, /* 01111100 */ + 0x82, /* 10000010 */ + 0x38, /* 00111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x00, /* 00000000 */ + + /* 141 0x8d '' */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x38, /* 00111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x00, /* 00000000 */ + + /* 142 0x8e '' */ + 0xc6, /* 11000110 */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 143 0x8f '' */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 144 0x90 '' */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0xf8, /* 11111000 */ + 0xc0, /* 11000000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + + /* 145 0x91 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0xd8, /* 11011000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + + /* 146 0x92 '' */ + 0x3e, /* 00111110 */ + 0x6c, /* 01101100 */ + 0xcc, /* 11001100 */ + 0xfe, /* 11111110 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xce, /* 11001110 */ + 0x00, /* 00000000 */ + + /* 147 0x93 '' */ + 0x7c, /* 01111100 */ + 0x82, /* 10000010 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 148 0x94 '' */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 149 0x95 '' */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 150 0x96 '' */ + 0x78, /* 01111000 */ + 0x84, /* 10000100 */ + 0x00, /* 00000000 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 151 0x97 '' */ + 0x60, /* 01100000 */ + 0x30, /* 00110000 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 152 0x98 '' */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7e, /* 01111110 */ + 0x06, /* 00000110 */ + 0xfc, /* 11111100 */ + + /* 153 0x99 '' */ + 0xc6, /* 11000110 */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x6c, /* 01101100 */ + 0x38, /* 00111000 */ + 0x00, /* 00000000 */ + + /* 154 0x9a '' */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 155 0x9b '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 156 0x9c '' */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0x64, /* 01100100 */ + 0xf0, /* 11110000 */ + 0x60, /* 01100000 */ + 0x66, /* 01100110 */ + 0xfc, /* 11111100 */ + 0x00, /* 00000000 */ + + /* 157 0x9d '' */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 158 0x9e '' */ + 0xf8, /* 11111000 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xfa, /* 11111010 */ + 0xc6, /* 11000110 */ + 0xcf, /* 11001111 */ + 0xc6, /* 11000110 */ + 0xc7, /* 11000111 */ + + /* 159 0x9f '' */ + 0x0e, /* 00001110 */ + 0x1b, /* 00011011 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0xd8, /* 11011000 */ + 0x70, /* 01110000 */ + 0x00, /* 00000000 */ + + /* 160 0xa0 '' */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x78, /* 01111000 */ + 0x0c, /* 00001100 */ + 0x7c, /* 01111100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 161 0xa1 '' */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x38, /* 00111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x00, /* 00000000 */ + + /* 162 0xa2 '' */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + + /* 163 0xa3 '' */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 164 0xa4 '' */ + 0x76, /* 01110110 */ + 0xdc, /* 11011100 */ + 0x00, /* 00000000 */ + 0xdc, /* 11011100 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x00, /* 00000000 */ + + /* 165 0xa5 '' */ + 0x76, /* 01110110 */ + 0xdc, /* 11011100 */ + 0x00, /* 00000000 */ + 0xe6, /* 11100110 */ + 0xf6, /* 11110110 */ + 0xde, /* 11011110 */ + 0xce, /* 11001110 */ + 0x00, /* 00000000 */ + + /* 166 0xa6 '' */ + 0x3c, /* 00111100 */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0x3e, /* 00111110 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 167 0xa7 '' */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0x38, /* 00111000 */ + 0x00, /* 00000000 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 168 0xa8 '' */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x63, /* 01100011 */ + 0x3e, /* 00111110 */ + 0x00, /* 00000000 */ + + /* 169 0xa9 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 170 0xaa '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + 0x06, /* 00000110 */ + 0x06, /* 00000110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 171 0xab '' */ + 0x63, /* 01100011 */ + 0xe6, /* 11100110 */ + 0x6c, /* 01101100 */ + 0x7e, /* 01111110 */ + 0x33, /* 00110011 */ + 0x66, /* 01100110 */ + 0xcc, /* 11001100 */ + 0x0f, /* 00001111 */ + + /* 172 0xac '' */ + 0x63, /* 01100011 */ + 0xe6, /* 11100110 */ + 0x6c, /* 01101100 */ + 0x7a, /* 01111010 */ + 0x36, /* 00110110 */ + 0x6a, /* 01101010 */ + 0xdf, /* 11011111 */ + 0x06, /* 00000110 */ + + /* 173 0xad '' */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 174 0xae '' */ + 0x00, /* 00000000 */ + 0x33, /* 00110011 */ + 0x66, /* 01100110 */ + 0xcc, /* 11001100 */ + 0x66, /* 01100110 */ + 0x33, /* 00110011 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 175 0xaf '' */ + 0x00, /* 00000000 */ + 0xcc, /* 11001100 */ + 0x66, /* 01100110 */ + 0x33, /* 00110011 */ + 0x66, /* 01100110 */ + 0xcc, /* 11001100 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 176 0xb0 '' */ + 0x22, /* 00100010 */ + 0x88, /* 10001000 */ + 0x22, /* 00100010 */ + 0x88, /* 10001000 */ + 0x22, /* 00100010 */ + 0x88, /* 10001000 */ + 0x22, /* 00100010 */ + 0x88, /* 10001000 */ + + /* 177 0xb1 '' */ + 0x55, /* 01010101 */ + 0xaa, /* 10101010 */ + 0x55, /* 01010101 */ + 0xaa, /* 10101010 */ + 0x55, /* 01010101 */ + 0xaa, /* 10101010 */ + 0x55, /* 01010101 */ + 0xaa, /* 10101010 */ + + /* 178 0xb2 '' */ + 0x77, /* 01110111 */ + 0xdd, /* 11011101 */ + 0x77, /* 01110111 */ + 0xdd, /* 11011101 */ + 0x77, /* 01110111 */ + 0xdd, /* 11011101 */ + 0x77, /* 01110111 */ + 0xdd, /* 11011101 */ + + /* 179 0xb3 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 180 0xb4 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0xf8, /* 11111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 181 0xb5 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0xf8, /* 11111000 */ + 0x18, /* 00011000 */ + 0xf8, /* 11111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 182 0xb6 '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0xf6, /* 11110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 183 0xb7 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 184 0xb8 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xf8, /* 11111000 */ + 0x18, /* 00011000 */ + 0xf8, /* 11111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 185 0xb9 '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0xf6, /* 11110110 */ + 0x06, /* 00000110 */ + 0xf6, /* 11110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 186 0xba '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 187 0xbb '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + 0x06, /* 00000110 */ + 0xf6, /* 11110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 188 0xbc '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0xf6, /* 11110110 */ + 0x06, /* 00000110 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 189 0xbd '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 190 0xbe '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0xf8, /* 11111000 */ + 0x18, /* 00011000 */ + 0xf8, /* 11111000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 191 0xbf '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xf8, /* 11111000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 192 0xc0 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x1f, /* 00011111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 193 0xc1 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 194 0xc2 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 195 0xc3 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x1f, /* 00011111 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 196 0xc4 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 197 0xc5 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0xff, /* 11111111 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 198 0xc6 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x1f, /* 00011111 */ + 0x18, /* 00011000 */ + 0x1f, /* 00011111 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 199 0xc7 '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x37, /* 00110111 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 200 0xc8 '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x37, /* 00110111 */ + 0x30, /* 00110000 */ + 0x3f, /* 00111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 201 0xc9 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x3f, /* 00111111 */ + 0x30, /* 00110000 */ + 0x37, /* 00110111 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 202 0xca '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0xf7, /* 11110111 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 203 0xcb '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0xf7, /* 11110111 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 204 0xcc '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x37, /* 00110111 */ + 0x30, /* 00110000 */ + 0x37, /* 00110111 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 205 0xcd '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 206 0xce '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0xf7, /* 11110111 */ + 0x00, /* 00000000 */ + 0xf7, /* 11110111 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 207 0xcf '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 208 0xd0 '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 209 0xd1 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 210 0xd2 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 211 0xd3 '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x3f, /* 00111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 212 0xd4 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x1f, /* 00011111 */ + 0x18, /* 00011000 */ + 0x1f, /* 00011111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 213 0xd5 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x1f, /* 00011111 */ + 0x18, /* 00011000 */ + 0x1f, /* 00011111 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 214 0xd6 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x3f, /* 00111111 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 215 0xd7 '' */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0xff, /* 11111111 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + + /* 216 0xd8 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0xff, /* 11111111 */ + 0x18, /* 00011000 */ + 0xff, /* 11111111 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 217 0xd9 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0xf8, /* 11111000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 218 0xda '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x1f, /* 00011111 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 219 0xdb '' */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + + /* 220 0xdc '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + + /* 221 0xdd '' */ + 0xf0, /* 11110000 */ + 0xf0, /* 11110000 */ + 0xf0, /* 11110000 */ + 0xf0, /* 11110000 */ + 0xf0, /* 11110000 */ + 0xf0, /* 11110000 */ + 0xf0, /* 11110000 */ + 0xf0, /* 11110000 */ + + /* 222 0xde '' */ + 0x0f, /* 00001111 */ + 0x0f, /* 00001111 */ + 0x0f, /* 00001111 */ + 0x0f, /* 00001111 */ + 0x0f, /* 00001111 */ + 0x0f, /* 00001111 */ + 0x0f, /* 00001111 */ + 0x0f, /* 00001111 */ + + /* 223 0xdf '' */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0xff, /* 11111111 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 224 0xe0 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x76, /* 01110110 */ + 0xdc, /* 11011100 */ + 0xc8, /* 11001000 */ + 0xdc, /* 11011100 */ + 0x76, /* 01110110 */ + 0x00, /* 00000000 */ + + /* 225 0xe1 '' */ + 0x78, /* 01111000 */ + 0xcc, /* 11001100 */ + 0xcc, /* 11001100 */ + 0xd8, /* 11011000 */ + 0xcc, /* 11001100 */ + 0xc6, /* 11000110 */ + 0xcc, /* 11001100 */ + 0x00, /* 00000000 */ + + /* 226 0xe2 '' */ + 0xfe, /* 11111110 */ + 0xc6, /* 11000110 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0xc0, /* 11000000 */ + 0x00, /* 00000000 */ + + /* 227 0xe3 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0x00, /* 00000000 */ + + /* 228 0xe4 '' */ + 0xfe, /* 11111110 */ + 0xc6, /* 11000110 */ + 0x60, /* 01100000 */ + 0x30, /* 00110000 */ + 0x60, /* 01100000 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + + /* 229 0xe5 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0xd8, /* 11011000 */ + 0xd8, /* 11011000 */ + 0xd8, /* 11011000 */ + 0x70, /* 01110000 */ + 0x00, /* 00000000 */ + + /* 230 0xe6 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x7c, /* 01111100 */ + 0xc0, /* 11000000 */ + + /* 231 0xe7 '' */ + 0x00, /* 00000000 */ + 0x76, /* 01110110 */ + 0xdc, /* 11011100 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + + /* 232 0xe8 '' */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x3c, /* 00111100 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + + /* 233 0xe9 '' */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0xc6, /* 11000110 */ + 0xfe, /* 11111110 */ + 0xc6, /* 11000110 */ + 0x6c, /* 01101100 */ + 0x38, /* 00111000 */ + 0x00, /* 00000000 */ + + /* 234 0xea '' */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0xee, /* 11101110 */ + 0x00, /* 00000000 */ + + /* 235 0xeb '' */ + 0x0e, /* 00001110 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0x3e, /* 00111110 */ + 0x66, /* 01100110 */ + 0x66, /* 01100110 */ + 0x3c, /* 00111100 */ + 0x00, /* 00000000 */ + + /* 236 0xec '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0xdb, /* 11011011 */ + 0xdb, /* 11011011 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 237 0xed '' */ + 0x06, /* 00000110 */ + 0x0c, /* 00001100 */ + 0x7e, /* 01111110 */ + 0xdb, /* 11011011 */ + 0xdb, /* 11011011 */ + 0x7e, /* 01111110 */ + 0x60, /* 01100000 */ + 0xc0, /* 11000000 */ + + /* 238 0xee '' */ + 0x1e, /* 00011110 */ + 0x30, /* 00110000 */ + 0x60, /* 01100000 */ + 0x7e, /* 01111110 */ + 0x60, /* 01100000 */ + 0x30, /* 00110000 */ + 0x1e, /* 00011110 */ + 0x00, /* 00000000 */ + + /* 239 0xef '' */ + 0x00, /* 00000000 */ + 0x7c, /* 01111100 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0xc6, /* 11000110 */ + 0x00, /* 00000000 */ + + /* 240 0xf0 '' */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + 0xfe, /* 11111110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 241 0xf1 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x7e, /* 01111110 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + + /* 242 0xf2 '' */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + + /* 243 0xf3 '' */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x18, /* 00011000 */ + 0x0c, /* 00001100 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + + /* 244 0xf4 '' */ + 0x0e, /* 00001110 */ + 0x1b, /* 00011011 */ + 0x1b, /* 00011011 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + + /* 245 0xf5 '' */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0xd8, /* 11011000 */ + 0xd8, /* 11011000 */ + 0x70, /* 01110000 */ + + /* 246 0xf6 '' */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x7e, /* 01111110 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 247 0xf7 '' */ + 0x00, /* 00000000 */ + 0x76, /* 01110110 */ + 0xdc, /* 11011100 */ + 0x00, /* 00000000 */ + 0x76, /* 01110110 */ + 0xdc, /* 11011100 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 248 0xf8 '' */ + 0x38, /* 00111000 */ + 0x6c, /* 01101100 */ + 0x6c, /* 01101100 */ + 0x38, /* 00111000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 249 0xf9 '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 250 0xfa '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x18, /* 00011000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 251 0xfb '' */ + 0x0f, /* 00001111 */ + 0x0c, /* 00001100 */ + 0x0c, /* 00001100 */ + 0x0c, /* 00001100 */ + 0xec, /* 11101100 */ + 0x6c, /* 01101100 */ + 0x3c, /* 00111100 */ + 0x1c, /* 00011100 */ + + /* 252 0xfc '' */ + 0x6c, /* 01101100 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x36, /* 00110110 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 253 0xfd '' */ + 0x78, /* 01111000 */ + 0x0c, /* 00001100 */ + 0x18, /* 00011000 */ + 0x30, /* 00110000 */ + 0x7c, /* 01111100 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 254 0xfe '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x3c, /* 00111100 */ + 0x3c, /* 00111100 */ + 0x3c, /* 00111100 */ + 0x3c, /* 00111100 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + + /* 255 0xff '' */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + 0x00, /* 00000000 */ + +}; diff --git a/wiringPi/gertboard.c b/devLib/gertboard.c similarity index 68% rename from wiringPi/gertboard.c rename to devLib/gertboard.c index a8795d3..5aeaef7 100644 --- a/wiringPi/gertboard.c +++ b/devLib/gertboard.c @@ -38,16 +38,17 @@ #include #include -#include "wiringPiSPI.h" +#include +#include #include "gertboard.h" // The A-D convertor won't run at more than 1MHz @ 3.3v -#define SPI_ADC_SPEED 1000000 -#define SPI_DAC_SPEED 1000000 -#define SPI_A2D 0 -#define SPI_D2A 1 +#define SPI_ADC_SPEED 1000000 +#define SPI_DAC_SPEED 1000000 +#define SPI_A2D 0 +#define SPI_D2A 1 /* @@ -57,7 +58,7 @@ ********************************************************************************* */ -void gertboardAnalogWrite (int chan, int value) +void gertboardAnalogWrite (const int chan, const int value) { uint8_t spiData [2] ; uint8_t chanBits, dataBits ; @@ -84,7 +85,7 @@ void gertboardAnalogWrite (int chan, int value) ********************************************************************************* */ -int gertboardAnalogRead (int chan) +int gertboardAnalogRead (const int chan) { uint8_t spiData [2] ; @@ -120,3 +121,44 @@ int gertboardSPISetup (void) return 0 ; } + + +/* + * New wiringPi node extension methods. + ********************************************************************************* + */ + +static int myAnalogRead (struct wiringPiNodeStruct *node, const int chan) +{ + return gertboardAnalogRead (chan - node->pinBase) ; +} + +static void myAnalogWrite (struct wiringPiNodeStruct *node, const int chan, const int value) +{ + gertboardAnalogWrite (chan - node->pinBase, value) ; +} + + +/* + * gertboardAnalogSetup: + * Create a new wiringPi device node for the analog devices on the + * Gertboard. We create one node with 2 pins - each pin being read + * and write - although the operations actually go to different + * hardware devices. + ********************************************************************************* + */ + +int gertboardAnalogSetup (const int pinBase) +{ + struct wiringPiNodeStruct *node ; + int x ; + + if (( x = gertboardSPISetup ()) != 0) + return x; + + node = wiringPiNewNode (pinBase, 2) ; + node->analogRead = myAnalogRead ; + node->analogWrite = myAnalogWrite ; + + return 0 ; +} diff --git a/wiringPi/gertboard.h b/devLib/gertboard.h similarity index 86% rename from wiringPi/gertboard.h rename to devLib/gertboard.h index 98fd1e7..3fa1919 100644 --- a/wiringPi/gertboard.h +++ b/devLib/gertboard.h @@ -30,10 +30,16 @@ extern "C" { #endif -extern void gertboardAnalogWrite (int chan, int value) ; -extern int gertboardAnalogRead (int chan) ; +// Old routines + +extern void gertboardAnalogWrite (const int chan, const int value) ; +extern int gertboardAnalogRead (const int chan) ; extern int gertboardSPISetup (void) ; +// New + +extern int gertboardAnalogSetup (const int pinBase) ; + #ifdef __cplusplus } #endif diff --git a/wiringPi/lcd.c b/devLib/lcd.c similarity index 64% rename from wiringPi/lcd.c rename to devLib/lcd.c index f123db2..6c0e474 100644 --- a/wiringPi/lcd.c +++ b/devLib/lcd.c @@ -26,29 +26,40 @@ #include #include -#include #include -#include "wiringPi.h" +#include + #include "lcd.h" -// Commands +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + +// HD44780U Commands #define LCD_CLEAR 0x01 #define LCD_HOME 0x02 #define LCD_ENTRY 0x04 -#define LCD_ON_OFF 0x08 +#define LCD_CTRL 0x08 #define LCD_CDSHIFT 0x10 #define LCD_FUNC 0x20 #define LCD_CGRAM 0x40 #define LCD_DGRAM 0x80 -#define LCD_ENTRY_SH 0x01 -#define LCD_ENTRY_ID 0x02 +// Bits in the entry register -#define LCD_ON_OFF_B 0x01 -#define LCD_ON_OFF_C 0x02 -#define LCD_ON_OFF_D 0x04 +#define LCD_ENTRY_SH 0x01 +#define LCD_ENTRY_ID 0x02 + +// Bits in the control register + +#define LCD_BLINK_CTRL 0x01 +#define LCD_CURSOR_CTRL 0x02 +#define LCD_DISPLAY_CTRL 0x04 + +// Bits in the function register #define LCD_FUNC_F 0x04 #define LCD_FUNC_N 0x08 @@ -58,13 +69,20 @@ struct lcdDataStruct { - uint8_t bits, rows, cols ; - uint8_t rsPin, strbPin ; - uint8_t dataPins [8] ; + int bits, rows, cols ; + int rsPin, strbPin ; + int dataPins [8] ; + int cx, cy ; } ; struct lcdDataStruct *lcds [MAX_LCDS] ; +static int lcdControl ; + +// Row offsets + +static const int rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ; + /* * strobe: @@ -73,7 +91,7 @@ struct lcdDataStruct *lcds [MAX_LCDS] ; ********************************************************************************* */ -static void strobe (struct lcdDataStruct *lcd) +static void strobe (const struct lcdDataStruct *lcd) { // Note timing changes for new version of delayMicroseconds () @@ -89,13 +107,14 @@ static void strobe (struct lcdDataStruct *lcd) ********************************************************************************* */ -static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data) +static void sendDataCmd (const struct lcdDataStruct *lcd, unsigned char data) { - uint8_t i, d4 ; + register unsigned char myData = data ; + unsigned char i, d4 ; if (lcd->bits == 4) { - d4 = (data >> 4) & 0x0F; + d4 = (myData >> 4) & 0x0F; for (i = 0 ; i < 4 ; ++i) { digitalWrite (lcd->dataPins [i], (d4 & 1)) ; @@ -103,7 +122,7 @@ static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data) } strobe (lcd) ; - d4 = data & 0x0F ; + d4 = myData & 0x0F ; for (i = 0 ; i < 4 ; ++i) { digitalWrite (lcd->dataPins [i], (d4 & 1)) ; @@ -114,8 +133,8 @@ static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data) { for (i = 0 ; i < 8 ; ++i) { - digitalWrite (lcd->dataPins [i], (data & 1)) ; - data >>= 1 ; + digitalWrite (lcd->dataPins [i], (myData & 1)) ; + myData >>= 1 ; } } strobe (lcd) ; @@ -128,22 +147,24 @@ static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data) ********************************************************************************* */ -static void putCommand (struct lcdDataStruct *lcd, uint8_t command) +static void putCommand (const struct lcdDataStruct *lcd, unsigned char command) { digitalWrite (lcd->rsPin, 0) ; sendDataCmd (lcd, command) ; + delay (2) ; } -static void put4Command (struct lcdDataStruct *lcd, uint8_t command) +static void put4Command (const struct lcdDataStruct *lcd, unsigned char command) { - uint8_t i ; + register unsigned char myCommand = command ; + register unsigned char i ; digitalWrite (lcd->rsPin, 0) ; for (i = 0 ; i < 4 ; ++i) { - digitalWrite (lcd->dataPins [i], (command & 1)) ; - command >>= 1 ; + digitalWrite (lcd->dataPins [i], (myCommand & 1)) ; + myCommand >>= 1 ; } strobe (lcd) ; } @@ -151,7 +172,7 @@ static void put4Command (struct lcdDataStruct *lcd, uint8_t command) /* ********************************************************************************* - * User Code below here + * User Callable code below here ********************************************************************************* */ @@ -161,16 +182,66 @@ static void put4Command (struct lcdDataStruct *lcd, uint8_t command) ********************************************************************************* */ -void lcdHome (int fd) +void lcdHome (const int fd) { struct lcdDataStruct *lcd = lcds [fd] ; + putCommand (lcd, LCD_HOME) ; + lcd->cx = lcd->cy = 0 ; + delay (5) ; } -void lcdClear (int fd) +void lcdClear (const int fd) { struct lcdDataStruct *lcd = lcds [fd] ; + putCommand (lcd, LCD_CLEAR) ; + putCommand (lcd, LCD_HOME) ; + lcd->cx = lcd->cy = 0 ; + delay (5) ; +} + + +/* + * lcdDisplay: lcdCursor: lcdCursorBlink: + * Turn the display, cursor, cursor blinking on/off + ********************************************************************************* + */ + +void lcdDisplay (const int fd, int state) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + + if (state) + lcdControl |= LCD_DISPLAY_CTRL ; + else + lcdControl &= ~LCD_DISPLAY_CTRL ; + + putCommand (lcd, LCD_CTRL | lcdControl) ; +} + +void lcdCursor (const int fd, int state) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + + if (state) + lcdControl |= LCD_CURSOR_CTRL ; + else + lcdControl &= ~LCD_CURSOR_CTRL ; + + putCommand (lcd, LCD_CTRL | lcdControl) ; +} + +void lcdCursorBlink (const int fd, int state) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + + if (state) + lcdControl |= LCD_BLINK_CTRL ; + else + lcdControl &= ~LCD_BLINK_CTRL ; + + putCommand (lcd, LCD_CTRL | lcdControl) ; } @@ -180,40 +251,77 @@ void lcdClear (int fd) ********************************************************************************* */ -void lcdSendCommand (int fd, uint8_t command) +void lcdSendCommand (const int fd, unsigned char command) { struct lcdDataStruct *lcd = lcds [fd] ; putCommand (lcd, command) ; } + /* * lcdPosition: - * Update the position of the cursor on the display + * Update the position of the cursor on the display. + * Ignore invalid locations. ********************************************************************************* */ - -void lcdPosition (int fd, int x, int y) +void lcdPosition (const int fd, int x, int y) { - static uint8_t rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ; struct lcdDataStruct *lcd = lcds [fd] ; + if ((x > lcd->cols) || (x < 0)) + return ; + if ((y > lcd->rows) || (y < 0)) + return ; + putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ; + + lcd->cx = x ; + lcd->cy = y ; +} + + +/* + * lcdCharDef: + * Defines a new character in the CGRAM + ********************************************************************************* + */ + +void lcdCharDef (const int fd, int index, unsigned char data [8]) +{ + struct lcdDataStruct *lcd = lcds [fd] ; + int i ; + + putCommand (lcd, LCD_CGRAM | ((index & 7) << 3)) ; + + digitalWrite (lcd->rsPin, 1) ; + for (i = 0 ; i < 8 ; ++i) + sendDataCmd (lcd, data [i]) ; } /* * lcdPutchar: - * Send a data byte to be displayed on the display + * Send a data byte to be displayed on the display. We implement a very + * simple terminal here - with line wrapping, but no scrolling. Yet. ********************************************************************************* */ -void lcdPutchar (int fd, uint8_t data) +void lcdPutchar (const int fd, unsigned char data) { struct lcdDataStruct *lcd = lcds [fd] ; digitalWrite (lcd->rsPin, 1) ; - sendDataCmd (lcd, data) ; + sendDataCmd (lcd, data) ; + + if (++lcd->cx == lcd->cols) + { + lcd->cx = 0 ; + if (++lcd->cy == lcd->rows) + lcd->cy = 0 ; + + putCommand (lcd, lcd->cx + (LCD_DGRAM | rowOff [lcd->cy])) ; + } } @@ -223,7 +331,7 @@ void lcdPutchar (int fd, uint8_t data) ********************************************************************************* */ -void lcdPuts (int fd, char *string) +void lcdPuts (const int fd, const char *string) { while (*string) lcdPutchar (fd, *string++) ; @@ -236,7 +344,7 @@ void lcdPuts (int fd, char *string) ********************************************************************************* */ -void lcdPrintf (int fd, char *message, ...) +void lcdPrintf (const int fd, const char *message, ...) { va_list argp ; char buffer [1024] ; @@ -256,12 +364,14 @@ void lcdPrintf (int fd, char *message, ...) ********************************************************************************* */ -int lcdInit (int rows, int cols, int bits, int rs, int strb, - int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) +int lcdInit (const int rows, const int cols, const int bits, + const int rs, const int strb, + const int d0, const int d1, const int d2, const int d3, const int d4, + const int d5, const int d6, const int d7) { static int initialised = 0 ; - uint8_t func ; + unsigned char func ; int i ; int lcdFd = -1 ; struct lcdDataStruct *lcd ; @@ -298,7 +408,7 @@ int lcdInit (int rows, int cols, int bits, int rs, int strb, if (lcdFd == -1) return -1 ; - lcd = malloc (sizeof (struct lcdDataStruct)) ; + lcd = (struct lcdDataStruct *)malloc (sizeof (struct lcdDataStruct)) ; if (lcd == NULL) return -1 ; @@ -307,6 +417,8 @@ int lcdInit (int rows, int cols, int bits, int rs, int strb, lcd->bits = 8 ; // For now - we'll set it properly later. lcd->rows = rows ; lcd->cols = cols ; + lcd->cx = 0 ; + lcd->cy = 0 ; lcd->dataPins [0] = d0 ; lcd->dataPins [1] = d1 ; @@ -371,10 +483,13 @@ int lcdInit (int rows, int cols, int bits, int rs, int strb, // Rest of the initialisation sequence - putCommand (lcd, LCD_ON_OFF | LCD_ON_OFF_D) ; delay (2) ; - putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ; delay (2) ; - putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ; delay (2) ; - putCommand (lcd, LCD_CLEAR) ; delay (5) ; + lcdDisplay (lcdFd, TRUE) ; + lcdCursor (lcdFd, FALSE) ; + lcdCursorBlink (lcdFd, FALSE) ; + lcdClear (lcdFd) ; + + putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ; + putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ; return lcdFd ; } diff --git a/devLib/lcd.h b/devLib/lcd.h new file mode 100644 index 0000000..0a0e598 --- /dev/null +++ b/devLib/lcd.h @@ -0,0 +1,52 @@ +/* + * lcd.h: + * Text-based LCD driver. + * This is designed to drive the parallel interface LCD drivers + * based in the Hitachi HD44780U controller and compatables. + * + * 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 . + *********************************************************************** + */ + +#define MAX_LCDS 8 + +#ifdef __cplusplus +extern "C" { +#endif + +extern void lcdHome (const int fd) ; +extern void lcdClear (const int fd) ; +extern void lcdDisplay (const int fd, int state) ; +extern void lcdCursor (const int fd, int state) ; +extern void lcdCursorBlink (const int fd, int state) ; +extern void lcdSendCommand (const int fd, unsigned char command) ; +extern void lcdPosition (const int fd, int x, int y) ; +extern void lcdCharDef (const int fd, int index, unsigned char data [8]) ; +extern void lcdPutchar (const int fd, unsigned char data) ; +extern void lcdPuts (const int fd, const char *string) ; +extern void lcdPrintf (const int fd, const char *message, ...) ; + +extern int lcdInit (const int rows, const int cols, const int bits, + const int rs, const int strb, + const int d0, const int d1, const int d2, const int d3, const int d4, + const int d5, const int d6, const int d7) ; + +#ifdef __cplusplus +} +#endif diff --git a/devLib/lcd128x64.c b/devLib/lcd128x64.c new file mode 100644 index 0000000..accd5c3 --- /dev/null +++ b/devLib/lcd128x64.c @@ -0,0 +1,673 @@ +/* + * lcd128x64.c: + * Graphics-based LCD driver. + * This is designed to drive the parallel interface LCD drivers + * based on the generic 12864H chips + * + * There are many variations on these chips, however they all mostly + * seem to be similar. + * This implementation has the Pins from the Pi hard-wired into it, + * in particular wiringPi pins 0-7 so that we can use + * digitalWriteByete() to speed things up somewhat. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include + +#include + +#include "font.h" +#include "lcd128x64.h" + +// Size + +#define LCD_WIDTH 128 +#define LCD_HEIGHT 64 + +// Hardware Pins +// Note pins 0-7 are the 8-bit data port + +#define CS1 10 +#define CS2 11 +#define STROBE 12 +#define RS 13 + +// Software copy of the framebuffer +// it's 8-bit deep although the display itself is only 1-bit deep. + +static unsigned char frameBuffer [LCD_WIDTH * LCD_HEIGHT] ; + +static int maxX, maxY ; +static int lastX, lastY ; +static int xOrigin, yOrigin ; +static int lcdOrientation = 0 ; + +/* + * strobe: + * Toggle the strobe (Really the "E") pin to the device. + * According to the docs, data is latched on the falling edge. + ********************************************************************************* + */ + +static void strobe (void) +{ + digitalWrite (STROBE, 1) ; delayMicroseconds (1) ; + digitalWrite (STROBE, 0) ; delayMicroseconds (5) ; +} + + +/* + * sentData: + * Send an data or command byte to the display. + ********************************************************************************* + */ + +static void sendData (const int data, const int chip) +{ + digitalWrite (chip, 0) ; + digitalWriteByte (data) ; + strobe () ; + digitalWrite (chip, 1) ; +} + + +/* + * sendCommand: + * Send a command byte to the display + ********************************************************************************* + */ + +static void sendCommand (const int command, const int chip) +{ + digitalWrite (RS, 0) ; + sendData (command, chip) ; + digitalWrite (RS, 1) ; +} + + +/* + * setCol: SetLine: + * Set the column and line addresses + ********************************************************************************* + */ + +static void setCol (int col, const int chip) + { sendCommand (0x40 | (col & 0x3F), chip) ; } + +static void setLine (int line, const int chip) + { sendCommand (0xB8 | (line & 0x07), chip) ; } + + +/* + * lcd128x64update: + * Copy our software version to the real display + ********************************************************************************* + */ + +void lcd128x64update (void) +{ + int line, x, y, fbLoc ; + unsigned char byte ; + +// Left side + + for (line = 0 ; line < 8 ; ++line) + { + setCol (0, CS1) ; + setLine (line, CS1) ; + + for (x = 63 ; x >= 0 ; --x) + { + byte = 0 ; + for (y = 0 ; y < 8 ; ++y) + { + fbLoc = x + (((7 - line) * 8) + (7 - y)) * LCD_WIDTH ; + if (frameBuffer [fbLoc] != 0) + byte |= (1 << y) ; + } + sendData (byte, CS1) ; + } + } + +// Right side + + for (line = 0 ; line < 8 ; ++line) + { + setCol (0, CS2) ; + setLine (line, CS2) ; + + for (x = 127 ; x >= 64 ; --x) + { + byte = 0 ; + for (y = 0 ; y < 8 ; ++y) + { + fbLoc = x + (((7 - line) * 8) + (7 - y)) * LCD_WIDTH ; + if (frameBuffer [fbLoc] != 0) + byte |= (1 << y) ; + } + sendData (byte, CS2) ; + } + } +} + + +/* + * lcd128x64setOrigin: + * Set the display offset origin + ********************************************************************************* + */ + +void lcd128x64setOrigin (int x, int y) +{ + xOrigin = x ; + yOrigin = y ; +} + + +/* + * lcd128x64setOrientation: + * Set the display orientation: + * 0: Normal, the display is portrait mode, 0,0 is top left + * 1: Landscape + * 2: Portrait, flipped + * 3: Landscape, flipped + ********************************************************************************* + */ + +void lcd128x64setOrientation (int orientation) +{ + lcdOrientation = orientation & 3 ; + + lcd128x64setOrigin (0,0) ; + + switch (lcdOrientation) + { + case 0: + maxX = LCD_WIDTH ; + maxY = LCD_HEIGHT ; + break ; + + case 1: + maxX = LCD_HEIGHT ; + maxY = LCD_WIDTH ; + break ; + + case 2: + maxX = LCD_WIDTH ; + maxY = LCD_HEIGHT ; + break ; + + case 3: + maxX = LCD_HEIGHT ; + maxY = LCD_WIDTH ; + break ; + } +} + + +/* + * lcd128x64orientCoordinates: + * Adjust the coordinates given to the display orientation + ********************************************************************************* + */ + +void lcd128x64orientCoordinates (int *x, int *y) +{ + register int tmp ; + + *x += xOrigin ; + *y += yOrigin ; + *y = maxY - *y - 1 ; + + switch (lcdOrientation) + { + case 0: + break; + + case 1: + tmp = maxY - *y - 1 ; + *y = *x ; + *x = tmp ; + break; + + case 2: + *x = maxX - *x - 1 ; + *y = maxY - *y - 1 ; + break; + + case 3: + *x = maxX - *x - 1 ; + tmp = *y ; + *y = *x ; + *x = tmp ; + break ; + } +} + + +/* + * lcd128x64getScreenSize: + * Return the max X & Y screen sizes. Needs to be called again, if you + * change screen orientation. + ********************************************************************************* + */ + +void lcd128x64getScreenSize (int *x, int *y) +{ + *x = maxX ; + *y = maxY ; +} + + +/* + ********************************************************************************* + * Standard Graphical Functions + ********************************************************************************* + */ + + +/* + * lcd128x64point: + * Plot a pixel. + ********************************************************************************* + */ + +void lcd128x64point (int x, int y, int colour) +{ + lastX = x ; + lastY = y ; + + lcd128x64orientCoordinates (&x, &y) ; + + if ((x < 0) || (x >= LCD_WIDTH) || (y < 0) || (y >= LCD_HEIGHT)) + return ; + + frameBuffer [x + y * LCD_WIDTH] = colour ; +} + + +/* + * lcd128x64line: lcd128x64lineTo: + * Classic Bressenham Line code + ********************************************************************************* + */ + +void lcd128x64line (int x0, int y0, int x1, int y1, int colour) +{ + int dx, dy ; + int sx, sy ; + int err, e2 ; + + lastX = x1 ; + lastY = y1 ; + + dx = abs (x1 - x0) ; + dy = abs (y1 - y0) ; + + sx = (x0 < x1) ? 1 : -1 ; + sy = (y0 < y1) ? 1 : -1 ; + + err = dx - dy ; + + for (;;) + { + lcd128x64point (x0, y0, colour) ; + + if ((x0 == x1) && (y0 == y1)) + break ; + + e2 = 2 * err ; + + if (e2 > -dy) + { + err -= dy ; + x0 += sx ; + } + + if (e2 < dx) + { + err += dx ; + y0 += sy ; + } + } + +} + +void lcd128x64lineTo (int x, int y, int colour) +{ + lcd128x64line (lastX, lastY, x, y, colour) ; +} + + +/* + * lcd128x64rectangle: + * A rectangle is a spoilt days fishing + ********************************************************************************* + */ + +void lcd128x64rectangle (int x1, int y1, int x2, int y2, int colour, int filled) +{ + register int x ; + + if (filled) + { + /**/ if (x1 == x2) + lcd128x64line (x1, y1, x2, y2, colour) ; + else if (x1 < x2) + for (x = x1 ; x <= x2 ; ++x) + lcd128x64line (x, y1, x, y2, colour) ; + else + for (x = x2 ; x <= x1 ; ++x) + lcd128x64line (x, y1, x, y2, colour) ; + } + else + { + lcd128x64line (x1, y1, x2, y1, colour) ; + lcd128x64lineTo (x2, y2, colour) ; + lcd128x64lineTo (x1, y2, colour) ; + lcd128x64lineTo (x1, y1, colour) ; + } +} + + +/* + * lcd128x64circle: + * This is the midpoint circle algorithm. + ********************************************************************************* + */ + +void lcd128x64circle (int x, int y, int r, int colour, int filled) +{ + int ddF_x = 1 ; + int ddF_y = -2 * r ; + + int f = 1 - r ; + int x1 = 0 ; + int y1 = r ; + + if (filled) + { + lcd128x64line (x, y + r, x, y - r, colour) ; + lcd128x64line (x + r, y, x - r, y, colour) ; + } + else + { + lcd128x64point (x, y + r, colour) ; + lcd128x64point (x, y - r, colour) ; + lcd128x64point (x + r, y, colour) ; + lcd128x64point (x - r, y, colour) ; + } + + while (x1 < y1) + { + if (f >= 0) + { + y1-- ; + ddF_y += 2 ; + f += ddF_y ; + } + x1++ ; + ddF_x += 2 ; + f += ddF_x ; + if (filled) + { + lcd128x64line (x + x1, y + y1, x - x1, y + y1, colour) ; + lcd128x64line (x + x1, y - y1, x - x1, y - y1, colour) ; + lcd128x64line (x + y1, y + x1, x - y1, y + x1, colour) ; + lcd128x64line (x + y1, y - x1, x - y1, y - x1, colour) ; + } + else + { + lcd128x64point (x + x1, y + y1, colour) ; lcd128x64point (x - x1, y + y1, colour) ; + lcd128x64point (x + x1, y - y1, colour) ; lcd128x64point (x - x1, y - y1, colour) ; + lcd128x64point (x + y1, y + x1, colour) ; lcd128x64point (x - y1, y + x1, colour) ; + lcd128x64point (x + y1, y - x1, colour) ; lcd128x64point (x - y1, y - x1, colour) ; + } + } +} + + +/* + * lcd128x64ellipse: + * Fast ellipse drawing algorithm by + * John Kennedy + * Mathematics Department + * Santa Monica College + * 1900 Pico Blvd. + * Santa Monica, CA 90405 + * jrkennedy6@gmail.com + * -Confirned in email this algorithm is in the public domain -GH- + ********************************************************************************* + */ + +static void plot4ellipsePoints (int cx, int cy, int x, int y, int colour, int filled) +{ + if (filled) + { + lcd128x64line (cx + x, cy + y, cx - x, cy + y, colour) ; + lcd128x64line (cx - x, cy - y, cx + x, cy - y, colour) ; + } + else + { + lcd128x64point (cx + x, cy + y, colour) ; + lcd128x64point (cx - x, cy + y, colour) ; + lcd128x64point (cx - x, cy - y, colour) ; + lcd128x64point (cx + x, cy - y, colour) ; + } +} + +void lcd128x64ellipse (int cx, int cy, int xRadius, int yRadius, int colour, int filled) +{ + int x, y ; + int xChange, yChange, ellipseError ; + int twoAsquare, twoBsquare ; + int stoppingX, stoppingY ; + + twoAsquare = 2 * xRadius * xRadius ; + twoBsquare = 2 * yRadius * yRadius ; + + x = xRadius ; + y = 0 ; + + xChange = yRadius * yRadius * (1 - 2 * xRadius) ; + yChange = xRadius * xRadius ; + + ellipseError = 0 ; + stoppingX = twoBsquare * xRadius ; + stoppingY = 0 ; + + while (stoppingX >= stoppingY) // 1st set of points + { + plot4ellipsePoints (cx, cy, x, y, colour, filled) ; + ++y ; + stoppingY += twoAsquare ; + ellipseError += yChange ; + yChange += twoAsquare ; + + if ((2 * ellipseError + xChange) > 0 ) + { + --x ; + stoppingX -= twoBsquare ; + ellipseError += xChange ; + xChange += twoBsquare ; + } + } + + x = 0 ; + y = yRadius ; + + xChange = yRadius * yRadius ; + yChange = xRadius * xRadius * (1 - 2 * yRadius) ; + + ellipseError = 0 ; + stoppingX = 0 ; + stoppingY = twoAsquare * yRadius ; + + while (stoppingX <= stoppingY) //2nd set of points + { + plot4ellipsePoints (cx, cy, x, y, colour, filled) ; + ++x ; + stoppingX += twoBsquare ; + ellipseError += xChange ; + xChange += twoBsquare ; + + if ((2 * ellipseError + yChange) > 0 ) + { + --y ; + stoppingY -= twoAsquare ; + ellipseError += yChange ; + yChange += twoAsquare ; + } + } +} + + +/* + * lcd128x64putchar: + * Print a single character to the screen + ********************************************************************************* + */ + +void lcd128x64putchar (int x, int y, int c, int bgCol, int fgCol) +{ + int y1, y2 ; + + unsigned char line ; + unsigned char *fontPtr ; + +// Can't print if we're offscreen + +//if ((x < 0) || (x >= (maxX - fontWidth)) || (y < 0) || (y >= (maxY - fontHeight))) +// return ; + + fontPtr = font + c * fontHeight ; + + for (y1 = fontHeight - 1 ; y1 >= 0 ; --y1) + { + y2 = y + y1 ; + line = *fontPtr++ ; + lcd128x64point (x + 0, y2, (line & 0x80) == 0 ? bgCol : fgCol) ; + lcd128x64point (x + 1, y2, (line & 0x40) == 0 ? bgCol : fgCol) ; + lcd128x64point (x + 2, y2, (line & 0x20) == 0 ? bgCol : fgCol) ; + lcd128x64point (x + 3, y2, (line & 0x10) == 0 ? bgCol : fgCol) ; + lcd128x64point (x + 4, y2, (line & 0x08) == 0 ? bgCol : fgCol) ; + lcd128x64point (x + 5, y2, (line & 0x04) == 0 ? bgCol : fgCol) ; + lcd128x64point (x + 6, y2, (line & 0x02) == 0 ? bgCol : fgCol) ; + lcd128x64point (x + 7, y2, (line & 0x01) == 0 ? bgCol : fgCol) ; + } +} + + +/* + * lcd128x64puts: + * Send a string to the display. Obeys \n and \r formatting + ********************************************************************************* + */ + +void lcd128x64puts (int x, int y, const char *str, int bgCol, int fgCol) +{ + int c, mx, my ; + + mx = x ; my = y ; + + while (*str) + { + c = *str++ ; + + if (c == '\r') + { + mx = x ; + continue ; + } + + if (c == '\n') + { + mx = x ; + my -= fontHeight ; + continue ; + } + + lcd128x64putchar (mx, my, c, bgCol, fgCol) ; + + mx += fontWidth ; + if (mx >= (maxX - fontWidth)) + { + mx = 0 ; + my -= fontHeight ; + } + } +} + + +/* + * lcd128x64clear: + * Clear the display to the given colour. + ********************************************************************************* + */ + +void lcd128x64clear (int colour) +{ + register int i ; + register unsigned char *ptr = frameBuffer ; + + for (i = 0 ; i < (maxX * maxY) ; ++i) + *ptr++ = colour ; +} + + + + +/* + * lcd128x64setup: + * Initialise the display and GPIO. + ********************************************************************************* + */ + +int lcd128x64setup (void) +{ + int i ; + + for (i = 0 ; i < 8 ; ++i) + pinMode (i, OUTPUT) ; + + digitalWrite (CS1, 1) ; + digitalWrite (CS2, 1) ; + digitalWrite (STROBE, 0) ; + digitalWrite (RS, 1) ; + + pinMode (CS1, OUTPUT) ; + pinMode (CS2, OUTPUT) ; + pinMode (STROBE, OUTPUT) ; + pinMode (RS, OUTPUT) ; + + sendCommand (0x3F, CS1) ; // Display ON + sendCommand (0xC0, CS1) ; // Set display start line to 0 + + sendCommand (0x3F, CS2) ; // Display ON + sendCommand (0xC0, CS2) ; // Set display start line to 0 + + lcd128x64clear (0) ; + lcd128x64setOrientation (0) ; + lcd128x64update () ; + + return 0 ; +} diff --git a/devLib/lcd128x64.h b/devLib/lcd128x64.h new file mode 100644 index 0000000..b448bbc --- /dev/null +++ b/devLib/lcd128x64.h @@ -0,0 +1,39 @@ +/* + * lcd128x64.h: + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +extern void lcd128x64setOrigin (int x, int y) ; +extern void lcd128x64setOrientation (int orientation) ; +extern void lcd128x64orientCoordinates (int *x, int *y) ; +extern void lcd128x64getScreenSize (int *x, int *y) ; +extern void lcd128x64point (int x, int y, int colour) ; +extern void lcd128x64line (int x0, int y0, int x1, int y1, int colour) ; +extern void lcd128x64lineTo (int x, int y, int colour) ; +extern void lcd128x64rectangle (int x1, int y1, int x2, int y2, int colour, int filled) ; +extern void lcd128x64circle (int x, int y, int r, int colour, int filled) ; +extern void lcd128x64ellipse (int cx, int cy, int xRadius, int yRadius, int colour, int filled) ; +extern void lcd128x64putchar (int x, int y, int c, int bgCol, int fgCol) ; +extern void lcd128x64puts (int x, int y, const char *str, int bgCol, int fgCol) ; +extern void lcd128x64update (void) ; +extern void lcd128x64clear (int colour) ; + +extern int lcd128x64setup (void) ; diff --git a/devLib/maxdetect.c b/devLib/maxdetect.c new file mode 100755 index 0000000..23eabf8 --- /dev/null +++ b/devLib/maxdetect.c @@ -0,0 +1,165 @@ +/* + * maxdetect.c: + * Driver for the MaxDetect series sensors + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +//#include +//#include +//#include + +#include + +#include "maxdetect.h" + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + + +/* + * maxDetectLowHighWait: + * Wait for a transition from high to low on the bus + ********************************************************************************* + */ + +static void maxDetectLowHighWait (const int pin) +{ + unsigned int timeOut = millis () + 2000 ; + + while (digitalRead (pin) == HIGH) + if (millis () > timeOut) + return ; + + while (digitalRead (pin) == LOW) + if (millis () > timeOut) + return ; +} + + +/* + * maxDetectClockByte: + * Read in a single byte from the MaxDetect bus + ********************************************************************************* + */ + +static unsigned int maxDetectClockByte (const int pin) +{ + unsigned int byte = 0 ; + int bit ; + + for (bit = 0 ; bit < 8 ; ++bit) + { + maxDetectLowHighWait (pin) ; + +// bit starting now - we need to time it. + + delayMicroseconds (30) ; + byte <<= 1 ; + if (digitalRead (pin) == HIGH) // It's a 1 + byte |= 1 ; + } + + return byte ; +} + + +/* + * maxDetectRead: + * Read in and return the 4 data bytes from the MaxDetect sensor. + * Return TRUE/FALSE depending on the checksum validity + ********************************************************************************* + */ + +int maxDetectRead (const int pin, unsigned char buffer [4]) +{ + int i ; + unsigned int checksum ; + unsigned char localBuf [5] ; + +// Wake up the RHT03 by pulling the data line low, then high +// Low for 10mS, high for 40uS. + + pinMode (pin, OUTPUT) ; + digitalWrite (pin, 0) ; delay (10) ; + digitalWrite (pin, 1) ; delayMicroseconds (40) ; + pinMode (pin, INPUT) ; + +// Now wait for sensor to pull pin low + + maxDetectLowHighWait (pin) ; + +// and read in 5 bytes (40 bits) + + for (i = 0 ; i < 5 ; ++i) + localBuf [i] = maxDetectClockByte (pin) ; + + checksum = 0 ; + for (i = 0 ; i < 4 ; ++i) + { + buffer [i] = localBuf [i] ; + checksum += localBuf [i] ; + } + checksum &= 0xFF ; + + return checksum == localBuf [4] ; +} + + +/* + * readRHT03: + * Read the Temperature & Humidity from an RHT03 sensor + ********************************************************************************* + */ + +int readRHT03 (const int pin, int *temp, int *rh) +{ + static unsigned int nextTime = 0 ; + static int lastTemp = 0 ; + static int lastRh = 0 ; + static int lastResult = TRUE ; + + unsigned char buffer [4] ; + +// Don't read more than once a second + + if (millis () < nextTime) + { + *temp = lastTemp ; + *rh = lastRh ; + return lastResult ; + } + + lastResult = maxDetectRead (pin, buffer) ; + + if (lastResult) + { + *temp = lastTemp = (buffer [2] * 256 + buffer [3]) ; + *rh = lastRh = (buffer [0] * 256 + buffer [1]) ; + nextTime = millis () + 2000 ; + return TRUE ; + } + else + { + return FALSE ; + } +} diff --git a/devLib/maxdetect.h b/devLib/maxdetect.h new file mode 100755 index 0000000..a1fd742 --- /dev/null +++ b/devLib/maxdetect.h @@ -0,0 +1,40 @@ +/* + * maxdetect.h: + * Driver for the MaxDetect series sensors + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + + +#ifdef __cplusplus +extern "C" { +#endif + +// Main generic function + +int maxDetectRead (const int pin, unsigned char buffer [4]) ; + +// Individual sensors + +int readRHT03 (const int pin, int *temp, int *rh) ; + +#ifdef __cplusplus +} +#endif diff --git a/devLib/piFace.c b/devLib/piFace.c new file mode 100644 index 0000000..4475c7f --- /dev/null +++ b/devLib/piFace.c @@ -0,0 +1,112 @@ +/* + * piFace.: + * This file to interface with the PiFace peripheral device which + * has an MCP23S17 GPIO device connected via the SPI bus. + * + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + + +#include +#include + +#include +#include + +#include "piFace.h" + + +/* + * myDigitalWrite: + * Perform the digitalWrite function on the PiFace board + ********************************************************************************* + */ + +void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + digitalWrite (pin + 16, value) ; +} + + +/* + * myDigitalRead: + * Perform the digitalRead function on the PiFace board + * With a slight twist - if we read from base + 8, then we + * read from the output latch... + ********************************************************************************* + */ + +int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + if ((pin - node->pinBase) >= 8) + return digitalRead (pin + 8) ; + else + return digitalRead (pin + 16 + 8) ; +} + + +/* + * myPullUpDnControl: + * Perform the pullUpDnControl function on the PiFace board + ********************************************************************************* + */ + +void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int pud) +{ + pullUpDnControl (pin + 16 + 8, pud) ; +} + + +/* + * piFaceSetup + * We're going to create an instance of the mcp23s17 here, then + * provide our own read/write routines on-top of it... + * The supplied PiFace code (in Pithon) treats it as an 8-bit device + * where you write the output ports and read the input port using the + * same pin numbers, however I have had a request to be able to read + * the output port, so reading 8..15 will read the output latch. + ********************************************************************************* + */ + +int piFaceSetup (const int pinBase) +{ + int i ; + struct wiringPiNodeStruct *node ; + +// Create an mcp23s17 instance: + + mcp23s17Setup (pinBase + 16, 0, 0) ; + +// Set the direction bits + + for (i = 0 ; i < 8 ; ++i) + { + pinMode (pinBase + 16 + i, OUTPUT) ; // Port A is the outputs + pinMode (pinBase + 16 + 8 + i, INPUT) ; // Port B inputs. + } + + node = wiringPiNewNode (pinBase, 16) ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->pullUpDnControl = myPullUpDnControl ; + + return 0 ; +} diff --git a/devLib/piFace.h b/devLib/piFace.h new file mode 100644 index 0000000..4965314 --- /dev/null +++ b/devLib/piFace.h @@ -0,0 +1,32 @@ +/* + * piFace.h: + * Control the PiFace Interface board for the Raspberry Pi + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int piFaceSetup (const int pinBase) ; + +#ifdef __cplusplus +} +#endif diff --git a/devLib/piFaceOld.c b/devLib/piFaceOld.c new file mode 100644 index 0000000..1b1c0dd --- /dev/null +++ b/devLib/piFaceOld.c @@ -0,0 +1,178 @@ +/* + * piFace.: + * Arduino compatable (ish) Wiring library for the Raspberry Pi + * Copyright (c) 2012-2013 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 "../wiringPi/mcp23x0817.h" + +#include "piFace.h" + +#define PIFACE_SPEED 4000000 +#define PIFACE_DEVNO 0 + + + +/* + * writeByte: + * Write a byte to a register on the MCP23S17 on the SPI bus. + ********************************************************************************* + */ + +static void writeByte (uint8_t reg, uint8_t data) +{ + uint8_t spiData [4] ; + + spiData [0] = CMD_WRITE ; + spiData [1] = reg ; + spiData [2] = data ; + + wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ; +} + +/* + * readByte: + * Read a byte from a register on the MCP23S17 on the SPI bus. + ********************************************************************************* + */ + +static uint8_t readByte (uint8_t reg) +{ + uint8_t spiData [4] ; + + spiData [0] = CMD_READ ; + spiData [1] = reg ; + + wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ; + + return spiData [2] ; +} + + +/* + * myDigitalWrite: + * Perform the digitalWrite function on the PiFace board + ********************************************************************************* + */ + +void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + uint8_t mask, old ; + + pin -= node->pinBase ; + mask = 1 << pin ; + old = readByte (MCP23x17_GPIOA) ; + + if (value == 0) + old &= (~mask) ; + else + old |= mask ; + + writeByte (MCP23x17_GPIOA, old) ; +} + + +/* + * myDigitalRead: + * Perform the digitalRead function on the PiFace board + ********************************************************************************* + */ + +int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + uint8_t mask, reg ; + + mask = 1 << ((pin - node->pinBase) & 7) ; + + if (pin < 8) + reg = MCP23x17_GPIOB ; // Input regsiter + else + reg = MCP23x17_OLATA ; // Output latch regsiter + + if ((readByte (reg) & mask) != 0) + return HIGH ; + else + return LOW ; +} + + +/* + * myPullUpDnControl: + * Perform the pullUpDnControl function on the PiFace board + ********************************************************************************* + */ + +void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int pud) +{ + uint8_t mask, old ; + + mask = 1 << (pin - node->pinBase) ; + old = readByte (MCP23x17_GPPUB) ; + + if (pud == 0) + old &= (~mask) ; + else + old |= mask ; + + writeByte (MCP23x17_GPPUB, old) ; +} + + +/* + * piFaceSetup + * Setup the SPI interface and initialise the MCP23S17 chip + * We create one node with 16 pins - each if the first 8 pins being read + * and write - although the operations actually go to different + * hardware ports. The top 8 let you read the state of the output register. + ********************************************************************************* + */ + +int piFaceSetup (const int pinBase) +{ + int x ; + struct wiringPiNodeStruct *node ; + + if ((x = wiringPiSPISetup (PIFACE_DEVNO, PIFACE_SPEED)) < 0) + return x ; + +// Setup the MCP23S17 + + writeByte (MCP23x17_IOCON, IOCON_INIT) ; + writeByte (MCP23x17_IODIRA, 0x00) ; // Port A -> Outputs + writeByte (MCP23x17_IODIRB, 0xFF) ; // Port B -> Inputs + + node = wiringPiNewNode (pinBase, 16) ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->pullUpDnControl = myPullUpDnControl ; + + return 0 ; +} diff --git a/wiringPi/piNes.c b/devLib/piNes.c similarity index 100% rename from wiringPi/piNes.c rename to devLib/piNes.c diff --git a/wiringPi/piNes.h b/devLib/piNes.h similarity index 100% rename from wiringPi/piNes.h rename to devLib/piNes.h diff --git a/examples/Gertboard/7segments.c b/examples/Gertboard/7segments.c new file mode 100644 index 0000000..8797e49 --- /dev/null +++ b/examples/Gertboard/7segments.c @@ -0,0 +1,221 @@ +/* + * 7segments.c: + * Simple test program to see if we can drive a 7-segment LED + * display using the GPIO and little else on the Raspberry Pi + * + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + */ + +#undef PHOTO_HACK + +#include + +#include +#include +#include +#include + +/* + * Segment mapping + * + * --a-- + * | | + * f b + * | | + * --g-- + * | | + * e c + * | | + * --d-- p + */ + +// GPIO Pin Mapping + +static int digits [6] = { 7, 11, 10, 13, 12, 14 } ; +static int segments [7] = { 6, 5, 4, 3, 2, 1, 0 } ; + + +static const int segmentDigits [] = +{ +// a b c d e f g Segments +// 6 5 4 3 2 1 0, // wiringPi pin No. + + 1, 1, 1, 1, 1, 1, 0, // 0 + 0, 1, 1, 0, 0, 0, 0, // 1 + 1, 1, 0, 1, 1, 0, 1, // 2 + 1, 1, 1, 1, 0, 0, 1, // 3 + 0, 1, 1, 0, 0, 1, 1, // 4 + 1, 0, 1, 1, 0, 1, 1, // 5 + 1, 0, 1, 1, 1, 1, 1, // 6 + 1, 1, 1, 0, 0, 0, 0, // 7 + 1, 1, 1, 1, 1, 1, 1, // 8 + 1, 1, 1, 1, 0, 1, 1, // 9 + 1, 1, 1, 0, 1, 1, 1, // A + 0, 0, 1, 1, 1, 1, 1, // b + 1, 0, 0, 1, 1, 1, 0, // C + 0, 1, 1, 1, 1, 0, 1, // d + 1, 0, 0, 1, 1, 1, 1, // E + 1, 0, 0, 0, 1, 1, 1, // F + 0, 0, 0, 0, 0, 0, 0, // blank +} ; + + +// display: +// A global variable which is written to by the main program and +// read from by the thread that updates the display. Only the first +// 6 characters are used. + +char display [8] ; + + +/* + * displayDigits: + * This is our thread that's run concurrently with the main program. + * Essentially sit in a loop, parsing and displaying the data held in + * the "display" global. + ********************************************************************************* + */ + +PI_THREAD (displayDigits) +{ + int digit, segment ; + int index, d, segVal ; + + piHiPri (50) ; + + for (;;) + { + for (digit = 0 ; digit < 6 ; ++digit) + { + for (segment = 0 ; segment < 7 ; ++segment) + { + d = toupper (display [digit]) ; + /**/ if ((d >= '0') && (d <= '9')) // Digit + index = d - '0' ; + else if ((d >= 'A') && (d <= 'F')) // Hex + index = d - 'A' + 10 ; + else + index = 16 ; // Blank + + segVal = segmentDigits [index * 7 + segment] ; + + digitalWrite (segments [segment], segVal) ; + } + digitalWrite (digits [digit], 1) ; + delay (2) ; + digitalWrite (digits [digit], 0) ; + } + } +} + + +/* + * setup: + * Initialise the hardware and start the thread + ********************************************************************************* + */ + +void setup (void) +{ + int i, c ; + + wiringPiSetup () ; + +// 7 segments + + for (i = 0 ; i < 7 ; ++i) + { digitalWrite (segments [i], 0) ; pinMode (segments [i], OUTPUT) ; } + +// 6 digits + + for (i = 0 ; i < 6 ; ++i) + { digitalWrite (digits [i], 0) ; pinMode (digits [i], OUTPUT) ; } + + strcpy (display, " ") ; + piThreadCreate (displayDigits) ; + delay (10) ; // Just to make sure it's started + +// Quick countdown LED test sort of thing + + c = 999999 ; + for (i = 0 ; i < 10 ; ++i) + { + sprintf (display, "%06d", c) ; + delay (400) ; + c -= 111111 ; + } + + strcpy (display, " ") ; + delay (400) ; + +#ifdef PHOTO_HACK + sprintf (display, "%s", "123456") ; + for (;;) + delay (1000) ; +#endif + +} + + +/* + * teenager: + * No explanation needed. (Nor one given!) + ********************************************************************************* + */ + +void teenager (void) +{ + char *message = " feedbeef babe cafe b00b " ; + int i ; + + for (i = 0 ; i < strlen (message) - 4 ; ++i) + { + strncpy (display, &message [i], 6) ; + delay (200) ; + } + delay (1000) ; + for (i = 0 ; i < 3 ; ++i) + { + strcpy (display, " ") ; + delay (150) ; + strcpy (display, " b00b ") ; + delay (250) ; + } + delay (1000) ; + strcpy (display, " ") ; + delay (1000) ; +} + + +/* + ********************************************************************************* + * main: + * Let the fun begin + ********************************************************************************* + */ + +int main (void) +{ + struct tm *t ; + time_t tim ; + + setup () ; + teenager () ; + + tim = time (NULL) ; + for (;;) + { + while (time (NULL) == tim) + delay (5) ; + + tim = time (NULL) ; + t = localtime (&tim) ; + + sprintf (display, "%02d%02d%02d", t->tm_hour, t->tm_min, t->tm_sec) ; + + delay (500) ; + } + + return 0 ; +} diff --git a/examples/Gertboard/Makefile b/examples/Gertboard/Makefile new file mode 100644 index 0000000..7569261 --- /dev/null +++ b/examples/Gertboard/Makefile @@ -0,0 +1,74 @@ +# +# Makefile: +# Gertboard - Examples using wiringPi +# +# Copyright (c) 2013 Gordon Henderson +################################################################################# + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm + +# Should not alter anything below this line +############################################################################### + +SRC = gertboard.c \ + buttons.c 7segments.c \ + voltmeter.c temperature.c vumeter.c \ + record.c + +OBJ = $(SRC:.c=.o) + +BINS = $(SRC:.c=) + +all: $(BINS) + +gertboard: gertboard.o + @echo [link] + @$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS) + +buttons: buttons.o + @echo [link] + @$(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS) + +7segments: 7segments.o + @echo [link] + @$(CC) -o $@ 7segments.o $(LDFLAGS) $(LDLIBS) + +voltmeter: voltmeter.o + @echo [link] + @$(CC) -o $@ voltmeter.o $(LDFLAGS) $(LDLIBS) + +temperature: temperature.o + @echo [link] + @$(CC) -o $@ temperature.o $(LDFLAGS) $(LDLIBS) + +vumeter: vumeter.o + @echo [link] + @$(CC) -o $@ vumeter.o $(LDFLAGS) $(LDLIBS) + +record: record.o + @echo [link] + @$(CC) -o $@ record.o $(LDFLAGS) $(LDLIBS) + +.c.o: + @echo [CC] $< + @$(CC) -c $(CFLAGS) $< -o $@ + +clean: + @echo [Clean] + @rm -f $(OBJ) *~ core tags $(BINS) + +tags: $(SRC) + @echo [ctags] + @ctags $(SRC) + +depend: + makedepend -Y $(SRC) + +# DO NOT DELETE diff --git a/examples/Gertboard/buttons.c b/examples/Gertboard/buttons.c new file mode 100644 index 0000000..5f76764 --- /dev/null +++ b/examples/Gertboard/buttons.c @@ -0,0 +1,83 @@ +/* + * buttons.c: + * Read the Gertboard buttons. Each one will act as an on/off + * tiggle switch for 3 different LEDs + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include + +// Array to keep track of our LEDs + +int leds [] = { 0, 0, 0 } ; + +// scanButton: +// See if a button is pushed, if so, then flip that LED and +// wait for the button to be let-go + +void scanButton (int button) +{ + if (digitalRead (button) == HIGH) // Low is pushed + return ; + + leds [button] ^= 1 ; // Invert state + digitalWrite (4 + button, leds [button]) ; + + while (digitalRead (button) == LOW) // Wait for release + delay (10) ; +} + +int main (void) +{ + int i ; + + printf ("Raspberry Pi Gertboard Button Test\n") ; + + wiringPiSetup () ; + +// Setup the outputs: +// Pins 3, 4, 5, 6 and 7 output: +// We're not using 3 or 4, but make sure they're off anyway +// (Using same hardware config as blink12.c) + + for (i = 3 ; i < 8 ; ++i) + { + pinMode (i, OUTPUT) ; + digitalWrite (i, 0) ; + } + +// Setup the inputs + + for (i = 0 ; i < 3 ; ++i) + { + pinMode (i, INPUT) ; + pullUpDnControl (i, PUD_UP) ; + leds [i] = 0 ; + } + + for (;;) + { + for (i = 0 ; i < 3 ; ++i) + scanButton (i) ; + delay (1) ; + } +} diff --git a/examples/gertboard.c b/examples/Gertboard/gertboard.c similarity index 61% rename from examples/gertboard.c rename to examples/Gertboard/gertboard.c index f02e27d..aefcb12 100644 --- a/examples/gertboard.c +++ b/examples/Gertboard/gertboard.c @@ -6,8 +6,8 @@ * D/A port 0 jumpered to A/D port 0. * * We output a sine wave on D/A port 0 and sample A/D port 0. We then - * copy this value to D/A port 1 and use a 'scope on both D/A ports - * to check all's well. + * plot the input value on the terminal as a sort of vertical scrolling + * oscilloscipe. * * Copyright (c) 2012-2013 Gordon Henderson. *********************************************************************** @@ -30,11 +30,13 @@ */ #include -#include +#include +#include #include -#define B_SIZE 200 -#undef DO_TIMING +// Gertboard D to A is an 8-bit unit. + +#define B_SIZE 256 #include #include @@ -42,23 +44,28 @@ int main (void) { double angle ; - int i ; - uint32_t x1 ; + int i, inputValue ; int buffer [B_SIZE] ; + int cols ; + struct winsize w ; -#ifdef DO_TIMING - unsigned int now, then ; -#endif printf ("Raspberry Pi Gertboard SPI test program\n") ; + printf ("=======================================\n") ; - if (wiringPiSetupSys () < 0) - return -1 ; + ioctl (fileno (stdin), TIOCGWINSZ, &w); + cols = w.ws_col - 2 ; - if (gertboardSPISetup () < 0) - return 1 ; +// Always initialise wiringPi. Use wiringPiSys() if you don't need +// (or want) to run as root -// Generate a Sine Wave + wiringPiSetupSys () ; + +// Initialise the Gertboard analog hardware at pin 100 + + gertboardAnalogSetup (100) ; + +// Generate a Sine Wave and store in our buffer for (i = 0 ; i < B_SIZE ; ++i) { @@ -66,28 +73,23 @@ int main (void) buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ; } +// Loop, output the sine wave on analog out port 0, read it into A-D port 0 +// and display it on the screen for (;;) { -#ifdef DO_TIMING - then = millis () ; -#endif - for (i = 0 ; i < B_SIZE ; ++i) { - gertboardAnalogWrite (0, buffer [i]) ; + analogWrite (100, buffer [i]) ; -#ifndef DO_TIMING - x1 = gertboardAnalogRead (0) ; - gertboardAnalogWrite (1, x1 >> 2) ; // 10-bit A/D, 8-bit D/A -#endif + inputValue = analogRead (100) ; + +// We don't need to wory about the scale or sign - the analog hardware is +// a 10-bit value, so 0-1023. Just scale this to our terminal + + printf ("%*s\n", (inputValue * cols) / 1023, "*") ; + delay (2) ; } - -#ifdef DO_TIMING - now = millis () ; - printf ("%4d mS, %9.7f S/sample", now - then, ((double)(now - then) / 1000.0) / (double)B_SIZE) ; - printf (" -> %9.4f samples/sec \n", 1 / (((double)(now - then) / 1000.0) / (double)B_SIZE)) ; -#endif } return 0 ; diff --git a/examples/Gertboard/record.c b/examples/Gertboard/record.c new file mode 100644 index 0000000..71d8718 --- /dev/null +++ b/examples/Gertboard/record.c @@ -0,0 +1,60 @@ +/* + * record.c: + * Record some audio via the Gertboard + * + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + */ + +#include +#include + +#include +#include + +#define B_SIZE 40000 + +int main () +{ + int i ; + struct timeval tStart, tEnd, tTaken ; + unsigned char buffer [B_SIZE] ; + + printf ("\n") ; + printf ("Gertboard demo: Recorder\n") ; + printf ("========================\n") ; + +// Always initialise wiringPi. Use wiringPiSys() if you don't need +// (or want) to run as root + + wiringPiSetupSys () ; + +// Initialise the Gertboard analog hardware at pin 100 + + gertboardAnalogSetup (100) ; + + gettimeofday (&tStart, NULL) ; + + for (i = 0 ; i < B_SIZE ; ++i) + buffer [i] = analogRead (100) >> 2 ; + + gettimeofday (&tEnd, NULL) ; + + timersub (&tEnd, &tStart, &tTaken) ; + + printf ("Time taken for %d reads: %ld.%ld\n", B_SIZE, tTaken.tv_sec, tTaken.tv_usec) ; + + gettimeofday (&tStart, NULL) ; + + for (i = 0 ; i < B_SIZE ; ++i) + analogWrite (100, buffer [i]) ; + + gettimeofday (&tEnd, NULL) ; + + timersub (&tEnd, &tStart, &tTaken) ; + + printf ("Time taken for %d writes: %ld.%ld\n", B_SIZE, tTaken.tv_sec, tTaken.tv_usec) ; + + return 0 ; +} + diff --git a/examples/Gertboard/temperature.c b/examples/Gertboard/temperature.c new file mode 100644 index 0000000..5985a12 --- /dev/null +++ b/examples/Gertboard/temperature.c @@ -0,0 +1,78 @@ +/* + * temperature.c: + * Demonstrate use of the Gertboard A to D converter to make + * a simple thermometer using the LM35. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include + +#include +#include + +int main () +{ + int x1, x2 ; + double v1, v2 ; + + printf ("\n") ; + printf ("Gertboard demo: Simple Thermemeter\n") ; + printf ("==================================\n") ; + +// Always initialise wiringPi. Use wiringPiSys() if you don't need +// (or want) to run as root + + wiringPiSetupSys () ; + +// Initialise the Gertboard analog hardware at pin 100 + + gertboardAnalogSetup (100) ; + + printf ("\n") ; + printf ("| Channel 0 | Channel 1 | Temperature 1 | Temperature 2 |\n") ; + + for (;;) + { + +// Read the 2 channels: + + x1 = analogRead (100) ; + x2 = analogRead (101) ; + +// Convert to a voltage: + + v1 = (double)x1 / 1023.0 * 3.3 ; + v2 = (double)x2 / 1023.0 * 3.3 ; + +// Print + + printf ("| %6.3f | %6.3f |", v1, v2) ; + +// Print Temperature of both channels by converting the LM35 reading +// to a temperature. Fortunately these are easy: 0.01 volts per C. + + printf (" %4.1f | %4.1f |\r", v1 * 100.0, v2 * 100.0) ; + fflush (stdout) ; + } + + return 0 ; +} + diff --git a/examples/Gertboard/voltmeter.c b/examples/Gertboard/voltmeter.c new file mode 100644 index 0000000..c4d2113 --- /dev/null +++ b/examples/Gertboard/voltmeter.c @@ -0,0 +1,73 @@ +/* + * voltmeter.c: + * Demonstrate use of the Gertboard A to D converter to make + * a simple voltmeter. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include + +#include +#include + +int main () +{ + int x1, x2 ; + double v1, v2 ; + + printf ("\n") ; + printf ("Gertboard demo: Simple Voltmeters\n") ; + printf ("=================================\n") ; + +// Always initialise wiringPi. Use wiringPiSys() if you don't need +// (or want) to run as root + + wiringPiSetupSys () ; + +// Initialise the Gertboard analog hardware at pin 100 + + gertboardAnalogSetup (100) ; + + printf ("\n") ; + printf ("| Channel 0 | Channel 1 |\n") ; + + for (;;) + { + +// Read the 2 channels: + + x1 = analogRead (100) ; + x2 = analogRead (101) ; + +// Convert to a voltage: + + v1 = (double)x1 / 1023.0 * 3.3 ; + v2 = (double)x2 / 1023.0 * 3.3 ; + +// Print + + printf ("| %6.3f | %6.3f |\r", v1, v2) ; + fflush (stdout) ; + } + + return 0 ; +} + diff --git a/examples/Gertboard/vumeter.c b/examples/Gertboard/vumeter.c new file mode 100644 index 0000000..9643ace --- /dev/null +++ b/examples/Gertboard/vumeter.c @@ -0,0 +1,152 @@ +/* + * vumeter.c: + * Simple VU meter + * + * Heres the theory: + * We will sample at 4000 samples/sec and put the data into a + * low-pass filter with a depth of 1000 samples. This will give + * us 1/4 a second of lag on the signal, but I think it might + * produce a more pleasing output. + * + * The input of the microphone should be at mid-pont with no + * sound input, but we might have to sample that too, to get + * our reference zero... + * + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + */ + +#include +#include +#include + +#include +#include + +#ifndef TRUE +#define TRUE (1==1) +#define FALSE (!TRUE) +#endif + +#define B_SIZE 1000 +#define S_SIZE 128 + +static int buffer [B_SIZE] ; +static int bPtr = 0 ; + +/* + * ledPercent: + * Output the given value as a percentage on the LEDs + ********************************************************************************* + */ + +static void ledPercent (int percent) +{ + unsigned int output = 0 ; + + if (percent > 11) output |= 0x01 ; + if (percent > 22) output |= 0x02 ; + if (percent > 33) output |= 0x04 ; + if (percent > 44) output |= 0x08 ; + if (percent > 55) output |= 0x10 ; + if (percent > 66) output |= 0x20 ; + if (percent > 77) output |= 0x40 ; + if (percent > 88) output |= 0x80 ; + + digitalWriteByte (output) ; +} + +static unsigned int tPeriod, tNextSampleTime ; + +/* + * sample: + * Get a sample from the Gertboard. If not enough time has elapsed + * since the last sample, then wait... + ********************************************************************************* + */ + +static void sample (void) +{ + unsigned int tFuture ; + +// Calculate the future sample time + + tFuture = tPeriod + tNextSampleTime ; + +// Wait until the next sample time + + while (micros () < tNextSampleTime) + ; + + buffer [bPtr] = gertboardAnalogRead (0) ; + + tNextSampleTime = tFuture ; +} + + +int main () +{ + int quietLevel, min, max ; + int i, sum ; + unsigned int tStart, tEnd ; + + printf ("\n") ; + printf ("Gertboard demo: VU Meter\n") ; + printf ("========================\n") ; + + wiringPiSetup () ; + gertboardSPISetup () ; + + ledPercent (0) ; + for (i = 0 ; i < 8 ; ++i) + pinMode (i, OUTPUT) ; + + for (bPtr = 0 ; bPtr < B_SIZE ; ++bPtr) + buffer [bPtr] = 99 ; + + tPeriod = 1000000 / 1000 ; + + printf ("Shhhh.... ") ; fflush (stdout) ; + delay (1000) ; + printf ("Sampling quiet... ") ; fflush (stdout) ; + + tStart = micros () ; + + tNextSampleTime = micros () ; + for (bPtr = 0 ; bPtr < B_SIZE ; ++bPtr) + sample () ; + + tEnd = micros () ; + + quietLevel = 0 ; + max = 0 ; + min = 1024 ; + for (i = 0 ; i < B_SIZE ; ++i) + { + quietLevel += buffer [i] ; + if (buffer [i] > max) max = buffer [i] ; + if (buffer [i] < min) min = buffer [i] ; + } + quietLevel /= B_SIZE ; + + printf ("Done. Quiet level is: %d [%d:%d] [%d:%d]\n", quietLevel, min, max, quietLevel - min, max - quietLevel) ; + + printf ("Time taken for %d reads: %duS\n", B_SIZE, tEnd - tStart) ; + + for (bPtr = 0 ;;) + { + sample () ; + sum = 0 ; + for (i = 0 ; i < S_SIZE ; ++i) + sum += buffer [i] ; + sum /= S_SIZE ; + sum = abs (quietLevel - sum) ; + sum = (sum * 1000) / quietLevel ; + ledPercent (sum) ; + if (++bPtr > S_SIZE) + bPtr = 0 ; + } + + + return 0 ; +} diff --git a/examples/Makefile b/examples/Makefile index defd510..f1e1725 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -30,15 +30,19 @@ INCLUDE = -I/usr/local/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L/usr/local/lib -LDLIBS = -lwiringPi -lpthread -lm +LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm # Should not alter anything below this line ############################################################################### -SRC = blink.c test1.c test2.c speed.c lcd.c wfi.c isr.c isr-osc.c \ - piface.c gertboard.c nes.c \ - pwm.c tone.c servo.c \ - delayTest.c serialRead.c serialTest.c okLed.c +SRC = blink.c blink8.c blink12.c \ + pwm.c \ + speed.c wfi.c isr.c isr-osc.c \ + lcd.c clock.c \ + nes.c \ + softPwm.c softTone.c \ + delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \ + rht03.c OBJ = $(SRC:.c=.o) @@ -55,13 +59,13 @@ blink: blink.o @echo [link] @$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) -test1: test1.o +blink8: blink8.o @echo [link] - @$(CC) -o $@ test1.o $(LDFLAGS) $(LDLIBS) - -test2: test2.o + @$(CC) -o $@ blink8.o $(LDFLAGS) $(LDLIBS) + +blink12: blink12.o @echo [link] - @$(CC) -o $@ test2.o $(LDFLAGS) $(LDLIBS) + @$(CC) -o $@ blink12.o $(LDFLAGS) $(LDLIBS) speed: speed.o @echo [link] @@ -71,6 +75,10 @@ lcd: lcd.o @echo [link] @$(CC) -o $@ lcd.o $(LDFLAGS) $(LDLIBS) +clock: clock.o + @echo [link] + @$(CC) -o $@ clock.o $(LDFLAGS) $(LDLIBS) + wfi: wfi.o @echo [link] @$(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS) @@ -83,22 +91,26 @@ isr-osc: isr-osc.o @echo [link] @$(CC) -o $@ isr-osc.o $(LDFLAGS) $(LDLIBS) -piface: piface.o - @echo [link] - @$(CC) -o $@ piface.o $(LDFLAGS) $(LDLIBS) - -gertboard: gertboard.o - @echo [link] - @$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS) - nes: nes.o @echo [link] @$(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS) +rht03: rht03.o + @echo [link] + @$(CC) -o $@ rht03.o $(LDFLAGS) $(LDLIBS) + pwm: pwm.o @echo [link] @$(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS) +softPwm: softPwm.o + @echo [link] + @$(CC) -o $@ softPwm.o $(LDFLAGS) $(LDLIBS) + +softTone: softTone.o + @echo [link] + @$(CC) -o $@ softTone.o $(LDFLAGS) $(LDLIBS) + delayTest: delayTest.o @echo [link] @$(CC) -o $@ delayTest.o $(LDFLAGS) $(LDLIBS) @@ -119,9 +131,9 @@ tone: tone.o @echo [link] @$(CC) -o $@ tone.o $(LDFLAGS) $(LDLIBS) -servo: servo.o +ds1302: ds1302.o @echo [link] - @$(CC) -o $@ servo.o $(LDFLAGS) $(LDLIBS) + @$(CC) -o $@ ds1302.o $(LDFLAGS) $(LDLIBS) .c.o: @@ -129,7 +141,8 @@ servo: servo.o @$(CC) -c $(CFLAGS) $< -o $@ clean: - rm -f $(OBJ) *~ core tags $(BINS) + @echo "[Clean]" + @rm -f $(OBJ) *~ core tags $(BINS) tags: $(SRC) @echo [ctags] diff --git a/examples/PiFace/Makefile b/examples/PiFace/Makefile new file mode 100644 index 0000000..0bde334 --- /dev/null +++ b/examples/PiFace/Makefile @@ -0,0 +1,85 @@ +# +# Makefile: +# wiringPi - Wiring Compatable library for the Raspberry Pi +# https://projects.drogon.net/wiring-pi +# +# Copyright (c) 2012 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# + + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm + +# Should not alter anything below this line +############################################################################### + +SRC = blink.c buttons.c reaction.c ladder.c metro.c motor.c + +OBJ = $(SRC:.c=.o) + +BINS = $(SRC:.c=) + +all: $(BINS) + +blink: blink.o + @echo [link] + @$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) + +buttons: buttons.o + @echo [link] + @$(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS) + +reaction: reaction.o + @echo [link] + @$(CC) -o $@ reaction.o $(LDFLAGS) $(LDLIBS) + +ladder: ladder.o + @echo [link] + @$(CC) -o $@ ladder.o $(LDFLAGS) $(LDLIBS) + +metro: metro.o + @echo [link] + @$(CC) -o $@ metro.o $(LDFLAGS) $(LDLIBS) + +motor: motor.o + @echo [link] + @$(CC) -o $@ motor.o $(LDFLAGS) $(LDLIBS) + +.c.o: + @echo [CC] $< + @$(CC) -c $(CFLAGS) $< -o $@ + +clean: + @echo "[Clean]" + @rm -f $(OBJ) *~ core tags $(BINS) + +tags: $(SRC) + @echo [ctags] + @ctags $(SRC) + +depend: + makedepend -Y $(SRC) + +# DO NOT DELETE diff --git a/examples/PiFace/blink.c b/examples/PiFace/blink.c new file mode 100644 index 0000000..ffb8a2e --- /dev/null +++ b/examples/PiFace/blink.c @@ -0,0 +1,59 @@ +/* + * blink.c: + * Simple "blink" test for the PiFace interface board. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include + +#include +#include + +// Use 200 as the pin-base for the PiFace board, and pick a pin +// for the LED that's not connected to a relay + +#define PIFACE 200 +#define LED (PIFACE+2) + +int main (int argc, char *argv []) +{ + printf ("Raspberry Pi PiFace Blink\n") ; + printf ("=========================\n") ; + +// Always initialise wiringPi. Use wiringPiSys() if you don't need +// (or want) to run as root + + wiringPiSetupSys () ; + +// Setup the PiFace board + + piFaceSetup (PIFACE) ; + + for (;;) + { + digitalWrite (LED, HIGH) ; // On + delay (500) ; // mS + digitalWrite (LED, LOW) ; // Off + delay (500) ; + } + + return 0 ; +} diff --git a/examples/PiFace/buttons.c b/examples/PiFace/buttons.c new file mode 100644 index 0000000..147a4bd --- /dev/null +++ b/examples/PiFace/buttons.c @@ -0,0 +1,103 @@ +/* + * buttons.c: + * Simple test for the PiFace interface board. + * + * Read the buttons and output the same to the LEDs + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include + +#include +#include + +int outputs [4] = { 0,0,0,0 } ; + +// Use 200 as the pin-base for the PiFace board + +#define PIFACE_BASE 200 + + +/* + * scanButton: + * Read the guiven button - if it's pressed, then flip the state + * of the correspoinding output pin + ********************************************************************************* + */ + +void scanButton (int button) +{ + if (digitalRead (PIFACE_BASE + button) == LOW) + { + outputs [button] ^= 1 ; + digitalWrite (PIFACE_BASE + button, outputs [button]) ; + printf ("Button %d pushed - output now: %s\n", + button, (outputs [button] == 0) ? "Off" : "On") ; + } + + while (digitalRead (PIFACE_BASE + button) == LOW) + delay (1) ; +} + + +/* + * start here + ********************************************************************************* + */ + +int main (void) +{ + int pin, button ; + + printf ("Raspberry Pi wiringPi + PiFace test program\n") ; + printf ("===========================================\n") ; + printf ("\n") ; + printf ( +"This program reads the buttons and uses them to toggle the first 4\n" +"outputs. Push a button once to turn an output on, and push it again to\n" +"turn it off again.\n\n") ; + +// Always initialise wiringPi. Use wiringPiSys() if you don't need +// (or want) to run as root + + wiringPiSetupSys () ; + + piFaceSetup (PIFACE_BASE) ; + +// Enable internal pull-ups & start with all off + + for (pin = 0 ; pin < 8 ; ++pin) + { + pullUpDnControl (PIFACE_BASE + pin, PUD_UP) ; + digitalWrite (PIFACE_BASE + pin, 0) ; + } + +// Loop, scanning the buttons + + for (;;) + { + for (button = 0 ; button < 4 ; ++button) + scanButton (button) ; + delay (5) ; + } + + return 0 ; +} diff --git a/examples/PiFace/ladder.c b/examples/PiFace/ladder.c new file mode 100755 index 0000000..4f08a6f --- /dev/null +++ b/examples/PiFace/ladder.c @@ -0,0 +1,337 @@ +/* + * ladder.c: + * + * Gordon Henderson, June 2012 + *********************************************************************** + */ + +#include +#include +#include +#include + +#include +#include + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + +#undef DEBUG + +#define NUM_LEDS 8 + + +// Map the LEDs to the hardware pins +// using PiFace pin numbers here + +#define PIFACE 200 + +const int ledMap [NUM_LEDS] = +{ +// 0, 1, 2, 3, 4, 5, 6, 7, 8 + 200, 201, 202, 203, 204, 205, 206, 207 +} ; + + +// Some constants for our circuit simulation + +const double vBatt = 9.0 ; // Volts (ie. a PP3) +const double capacitor = 0.001 ; // 1000uF +const double rCharge = 2200.0 ; // ohms +const double rDischarge = 68000.0 ; // ohms +const double timeInc = 0.01 ; // Seconds + +double vCharge, vCap, vCapLast ; + + + +/* + * setup: + * Program the GPIO correctly and initialise the lamps + *********************************************************************** + */ + +void setup (void) +{ + int i ; + + wiringPiSetupSys () ; + + if (piFaceSetup (200) == -1) + exit (1) ; + +// Enable internal pull-ups + + for (i = 0 ; i < 8 ; ++i) + pullUpDnControl (PIFACE + i, PUD_UP) ; + +// Calculate the actual charging voltage - standard calculation of +// vCharge = r2 / (r1 + r2) * vBatt +// +// +// -----+--- vBatt +// | +// R1 +// | +// +---+---- vCharge +// | | +// R2 C +// | | +// -----+---+----- + + vCharge = rDischarge / (rCharge + rDischarge) * vBatt ; + +// Start with no charge + + vCap = vCapLast = 0.0 ; +} + + +/* + * introLeds + * Put a little pattern on the LEDs to start with + ********************************************************************************* + */ + +void introLeds (void) +{ + int i, j ; + + + printf ("Pi Ladder\n") ; + printf ("=========\n\n") ; + printf (" vBatt: %6.2f volts\n", vBatt) ; + printf (" rCharge: %6.0f ohms\n", rCharge) ; + printf (" rDischarge: %6.0f ohms\n", rDischarge) ; + printf (" vCharge: %6.2f volts\n", vCharge) ; + printf (" capacitor: %6.0f uF\n", capacitor * 1000.0) ; + +// Flash 3 times: + + for (j = 0 ; j < 3 ; ++j) + { + for (i = 0 ; i < NUM_LEDS ; ++i) + digitalWrite (ledMap [i], 1) ; + delay (500) ; + for (i = 0 ; i < NUM_LEDS ; ++i) + digitalWrite (ledMap [i], 0) ; + delay (100) ; + } + +// All On + + for (i = 0 ; i < NUM_LEDS ; ++i) + digitalWrite (ledMap [i], 1) ; + delay (500) ; + +// Countdown... + + for (i = NUM_LEDS - 1 ; i >= 0 ; --i) + { + digitalWrite (ledMap [i], 0) ; + delay (100) ; + } + delay (500) ; +} + + +/* + * winningLeds + * Put a little pattern on the LEDs to start with + ********************************************************************************* + */ + +void winningLeds (void) +{ + int i, j ; + +// Flash 3 times: + + for (j = 0 ; j < 3 ; ++j) + { + for (i = 0 ; i < NUM_LEDS ; ++i) + digitalWrite (ledMap [i], 1) ; + delay (500) ; + for (i = 0 ; i < NUM_LEDS ; ++i) + digitalWrite (ledMap [i], 0) ; + delay (100) ; + } + +// All On + + for (i = 0 ; i < NUM_LEDS ; ++i) + digitalWrite (ledMap [i], 1) ; + delay (500) ; + +// Countup... + + for (i = 0 ; i < NUM_LEDS ; ++i) + { + digitalWrite (ledMap [i], 0) ; + delay (100) ; + } + delay (500) ; +} + + +/* + * chargeCapacitor: dischargeCapacitor: + * Add or remove charge to the capacitor. + * Standard capacitor formulae. + ********************************************************************************* + */ + +void chargeCapacitor (void) +{ + vCap = (vCapLast - vCharge) * + exp (- timeInc / (rCharge * capacitor)) + vCharge ; + +#ifdef DEBUG + printf ("+vCap: %7.4f\n", vCap) ; +#endif + + vCapLast = vCap ; +} + +void dischargeCapacitor (void) +{ + vCap = vCapLast * + exp (- timeInc / (rDischarge * capacitor)) ; + +#ifdef DEBUG + printf ("-vCap: %7.4f\n", vCap) ; +#endif + + vCapLast = vCap ; +} + + +/* + * ledBargraph: + * Output the supplied number as a bargraph on the LEDs + ********************************************************************************* + */ + +void ledBargraph (double value, int topLedOn) +{ + int topLed = (int)floor (value / vCharge * (double)NUM_LEDS) + 1 ; + int i ; + + if (topLed > NUM_LEDS) + topLed = NUM_LEDS ; + + if (!topLedOn) + --topLed ; + + for (i = 0 ; i < topLed ; ++i) + digitalWrite (ledMap [i], 1) ; + + for (i = topLed ; i < NUM_LEDS ; ++i) + digitalWrite (ledMap [i], 0) ; +} + + +/* + * ledOnAction: + * Make sure the leading LED is on and check the button + ********************************************************************************* + */ + +void ledOnAction (void) +{ + if (digitalRead (PIFACE) == LOW) + { + chargeCapacitor () ; + ledBargraph (vCap, TRUE) ; + } +} + + +/* + * ledOffAction: + * Make sure the leading LED is off and check the button + ********************************************************************************* + */ + +void ledOffAction (void) +{ + dischargeCapacitor () ; + +// Are we still pushing the button? + + if (digitalRead (PIFACE) == LOW) + { + vCap = vCapLast = 0.0 ; + ledBargraph (vCap, FALSE) ; + +// Wait until we release the button + + while (digitalRead (PIFACE) == LOW) + delay (10) ; + } +} + + +/* + *********************************************************************** + * The main program + *********************************************************************** + */ + +int main (void) +{ + unsigned int then, ledOnTime, ledOffTime ; + unsigned int ourDelay = (int)(1000.0 * timeInc) ; + + setup () ; + introLeds () ; + +// Setup the LED times - TODO reduce the ON time as the game progresses + + ledOnTime = 1000 ; + ledOffTime = 1000 ; + +// This is our Gate/Squarewave loop + + for (;;) + { + +// LED ON: + + (void)ledBargraph (vCap, TRUE) ; + then = millis () + ledOnTime ; + while (millis () < then) + { + ledOnAction () ; + delay (ourDelay) ; + } + +// Have we won yet? +// We need vCap to be in the top NUM_LEDS of the vCharge + + if (vCap > ((double)(NUM_LEDS - 1) / (double)NUM_LEDS * vCharge)) // Woo hoo! + { + winningLeds () ; + while (digitalRead (PIFACE) == HIGH) + delay (10) ; + while (digitalRead (PIFACE) == LOW) + delay (10) ; + vCap = vCapLast = 0.0 ; + } + +// LED OFF: + + (void)ledBargraph (vCap, FALSE) ; + then = millis () + ledOffTime ; + while (millis () < then) + { + ledOffAction () ; + delay (ourDelay) ; + } + + } + + return 0 ; +} diff --git a/examples/PiFace/metro.c b/examples/PiFace/metro.c new file mode 100644 index 0000000..a4a8c1d --- /dev/null +++ b/examples/PiFace/metro.c @@ -0,0 +1,111 @@ +/* + * metronome.c: + * Simple test for the PiFace interface board. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#include +#include + +#define PIFACE 200 + +/* + * middleA: + * Play middle A (on the relays - yea!) + ********************************************************************************* + */ + +static void middleA (void) +{ + unsigned int next ; + + for (;;) + { + next = micros () + 1136 ; + digitalWrite (PIFACE + 0, 0) ; + digitalWrite (PIFACE + 1, 0) ; + while (micros () < next) + delayMicroseconds (1) ; + + next = micros () + 1137 ; + digitalWrite (PIFACE + 0, 1) ; + digitalWrite (PIFACE + 1, 1) ; + while (micros () < next) + delayMicroseconds (1) ; + + } +} + + +int main (int argc, char *argv []) +{ + int bpm, msPerBeat, state = 0 ; + unsigned int end ; + + printf ("Raspberry Pi PiFace Metronome\n") ; + printf ("=============================\n") ; + + piHiPri (50) ; + + wiringPiSetupSys () ; // Needed for timing functions + piFaceSetup (PIFACE) ; + + if (argc != 2) + { + printf ("Usage: %s \n", argv [0]) ; + exit (1) ; + } + + if (strcmp (argv [1], "a") == 0) + middleA () ; + + bpm = atoi (argv [1]) ; + + if ((bpm < 40) || (bpm > 208)) + { + printf ("%s range is 40 through 208 beats per minute\n", argv [0]) ; + exit (1) ; + } + + msPerBeat = 60000 / bpm ; + +// Main loop: +// Put some random LED pairs up for a few seconds, then blank ... + + for (;;) + { + end = millis () + msPerBeat ; + + digitalWrite (PIFACE + 0, state) ; + digitalWrite (PIFACE + 1, state) ; + + while (millis () < end) + delayMicroseconds (500) ; + + state ^= 1 ; + } + + return 0 ; +} diff --git a/examples/PiFace/motor.c b/examples/PiFace/motor.c new file mode 100644 index 0000000..14f5539 --- /dev/null +++ b/examples/PiFace/motor.c @@ -0,0 +1,120 @@ +/* + * motor.c: + * Use the PiFace board to demonstrate an H bridge + * circuit via the 2 relays. + * Then add on an external transsitor to help with PWM. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#include +#include +#include + +int outputs [2] = { 0,0 } ; + +#define PIFACE_BASE 200 +#define PWM_OUT_PIN 204 +#define PWM_UP 202 +#define PWM_DOWN 203 + +void scanButton (int button) +{ + if (digitalRead (PIFACE_BASE + button) == LOW) + { + outputs [button] ^= 1 ; + digitalWrite (PIFACE_BASE + button, outputs [button]) ; + printf ("Button %d pushed - output now: %s\n", + button, (outputs [button] == 0) ? "Off" : "On") ; + } + + while (digitalRead (PIFACE_BASE + button) == LOW) + delay (1) ; +} + + +int main (void) +{ + int pin, button ; + int pwmValue = 0 ; + + printf ("Raspberry Pi PiFace - Motor control\n") ; + printf ("==================================\n") ; + printf ("\n") ; + printf ( +"This program is designed to be used with a motor connected to the relays\n" +"in an H-Bridge type configuration with optional speeed control via PWM.\n" +"\n" +"Use the leftmost buttons to turn each relay on and off, and the rigthmost\n" +"buttons to increase ot decrease the PWM output on the control pin (pin\n" +"4)\n\n") ; + + wiringPiSetup () ; + piFaceSetup (PIFACE_BASE) ; + softPwmCreate (PWM_OUT_PIN, 100, 100) ; + +// Enable internal pull-ups & start with all off + + for (pin = 0 ; pin < 8 ; ++pin) + { + pullUpDnControl (PIFACE_BASE + pin, PUD_UP) ; + digitalWrite (PIFACE_BASE + pin, 0) ; + } + + for (;;) + { + for (button = 0 ; button < 2 ; ++button) + scanButton (button) ; + + if (digitalRead (PWM_UP) == LOW) + { + pwmValue += 10 ; + if (pwmValue > 100) + pwmValue = 100 ; + + softPwmWrite (PWM_OUT_PIN, pwmValue) ; + printf ("PWM -> %3d\n", pwmValue) ; + + while (digitalRead (PWM_UP) == LOW) + delay (5) ; + } + + if (digitalRead (PWM_DOWN) == LOW) + { + pwmValue -= 10 ; + if (pwmValue < 0) + pwmValue = 0 ; + + softPwmWrite (PWM_OUT_PIN, pwmValue) ; + printf ("PWM -> %3d\n", pwmValue) ; + + while (digitalRead (PWM_DOWN) == LOW) + delay (5) ; + } + + delay (5) ; + } + + return 0 ; +} diff --git a/examples/PiFace/reaction.c b/examples/PiFace/reaction.c new file mode 100644 index 0000000..5084508 --- /dev/null +++ b/examples/PiFace/reaction.c @@ -0,0 +1,194 @@ +/* + * reaction.c: + * Simple test for the PiFace interface board. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#include +#include + + +int outputs [4] = { 0,0,0,0 } ; + +#define PIFACE 200 + +/* + * light: + * Light up the given LED - actually lights up a pair + ********************************************************************************* + */ + +void light (int led, int value) +{ + led *= 2 ; + digitalWrite (PIFACE + led + 0, value) ; + digitalWrite (PIFACE + led + 1, value) ; +} + +/* + * lightAll: + * All On or Off + ********************************************************************************* + */ + +void lightAll (int onoff) +{ + light (0, onoff) ; + light (1, onoff) ; + light (2, onoff) ; + light (3, onoff) ; +} + + +/* + * waitForNoButtons: + * Wait for all buttons to be released + ********************************************************************************* + */ + +void waitForNoButtons (void) +{ + int i, button ; + + for (;;) + { + button = 0 ; + for (i = 0 ; i < 4 ; ++i) + button += digitalRead (PIFACE + i) ; + + if (button == 4) + break ; + } +} + + +void scanButton (int button) +{ + if (digitalRead (PIFACE + button) == LOW) + { + outputs [button] ^= 1 ; + digitalWrite (PIFACE + button, outputs [button]) ; + } + + while (digitalRead (PIFACE + button) == LOW) + delay (1) ; +} + + +int main (void) +{ + int i, j ; + int led, button ; + unsigned int start, stop ; + + printf ("Raspberry Pi PiFace Reaction Timer\n") ; + printf ("==================================\n") ; + + if (piFaceSetup (PIFACE) == -1) + exit (1) ; + +// Enable internal pull-ups + + for (i = 0 ; i < 8 ; ++i) + pullUpDnControl (PIFACE + i, PUD_UP) ; + + +// Main game loop: +// Put some random LED pairs up for a few seconds, then blank ... + + for (;;) + { + printf ("Press any button to start ... \n") ; fflush (stdout) ; + + for (;;) + { + led = rand () % 4 ; + light (led, 1) ; + delay (10) ; + light (led, 0) ; + + button = 0 ; + for (j = 0 ; j < 4 ; ++j) + button += digitalRead (PIFACE + j) ; + + if (button != 4) + break ; + } + + waitForNoButtons () ; + + printf ("Wait for it ... ") ; fflush (stdout) ; + + led = rand () % 4 ; + delay (rand () % 500 + 1000) ; + light (led, 1) ; + + start = millis () ; + for (button = -1 ; button == -1 ; ) + { + for (j = 0 ; j < 4 ; ++j) + if (digitalRead (PIFACE + j) == 0) // Pushed + { + button = j ; + break ; + } + } + stop = millis () ; + button = 3 - button ; // Correct for the buttons/LEDs reversed + + light (led, 0) ; + + waitForNoButtons () ; + + light (led, 1) ; + + if (button == led) + { + printf ("You got it in %3d mS\n", stop - start) ; + } + else + { + printf ("Missed: You pushed %d - LED was %d\n", button, led) ; + for (;;) + { + light (button, 1) ; + delay (100) ; + light (button, 0) ; + delay (100) ; + i = 0 ; + for (j = 0 ; j < 4 ; ++j) + i += digitalRead (PIFACE + j) ; + if (i != 4) + break ; + } + + waitForNoButtons () ; + } + light (led, 0) ; + delay (4000) ; + } + + return 0 ; +} diff --git a/examples/blink.c b/examples/blink.c index bb9f856..c27a20e 100644 --- a/examples/blink.c +++ b/examples/blink.c @@ -34,16 +34,14 @@ int main (void) { printf ("Raspberry Pi blink\n") ; - if (wiringPiSetup () == -1) - return 1 ; - + wiringPiSetup () ; pinMode (LED, OUTPUT) ; for (;;) { - digitalWrite (LED, 1) ; // On + digitalWrite (LED, HIGH) ; // On delay (500) ; // mS - digitalWrite (LED, 0) ; // Off + digitalWrite (LED, LOW) ; // Off delay (500) ; } return 0 ; diff --git a/examples/test1.c b/examples/blink12.c similarity index 70% rename from examples/test1.c rename to examples/blink12.c index 4c75711..c9b3d50 100644 --- a/examples/test1.c +++ b/examples/blink12.c @@ -1,8 +1,7 @@ /* - * test1.c: - * Simple test program to test the wiringPi functions - * This is a sequencer to make a patter appear on 8 LEDs - * connected to the GPIO pins. + * blink12.c: + * Simple sequence over the first 12 GPIO pins - LEDs + * Aimed at the Gertboard, but it's fairly generic. * * Copyright (c) 2012-2013 Gordon Henderson. *********************************************************************** @@ -24,17 +23,13 @@ *********************************************************************** */ -#include - #include -#include -#include - +#include // Simple sequencer data // Triplets of LED, On/Off and delay -uint8_t data [] = +int data [] = { 0, 1, 1, 1, 1, 1, @@ -44,15 +39,23 @@ uint8_t data [] = 3, 0, 0, 5, 1, 1, 4, 0, 0, 6, 1, 1, 5, 0, 0, 7, 1, 1, - 6, 0, 1, - 7, 0, 1, + 6, 0, 0, 11, 1, 1, + 7, 0, 0, 10, 1, 1, + 11, 0, 0, 13, 1, 1, + 10, 0, 0, 12, 1, 1, + 13, 0, 1, + 12, 0, 1, 0, 0, 1, // Extra delay // Back again - 7, 1, 1, - 6, 1, 1, + 12, 1, 1, + 13, 1, 1, + 12, 0, 0, 10, 1, 1, + 13, 0, 0, 11, 1, 1, + 10, 0, 0, 7, 1, 1, + 11, 0, 0, 6, 1, 1, 7, 0, 0, 5, 1, 1, 6, 0, 0, 4, 1, 1, 5, 0, 0, 3, 1, 1, @@ -64,7 +67,7 @@ uint8_t data [] = 0, 0, 1, // Extra delay - 9, 9, 9, // End marker + 0, 9, 0, // End marker } ; @@ -75,16 +78,17 @@ int main (void) int dataPtr ; int l, s, d ; - printf ("Raspberry Pi wiringPi test program\n") ; + printf ("Raspberry Pi - 12-LED Sequence\n") ; + printf ("==============================\n") ; + printf ("\n") ; + printf ("Connect LEDs up to the first 8 GPIO pins, then pins 11, 10, 13, 12 in\n") ; + printf (" that order, then sit back and watch the show!\n") ; - if (wiringPiSetup () == -1) - exit (1) ; + wiringPiSetup () ; - for (pin = 0 ; pin < 8 ; ++pin) + for (pin = 0 ; pin < 14 ; ++pin) pinMode (pin, OUTPUT) ; - pinMode (8, INPUT) ; // Pin 8 SDA0 - Has on-board 2k2 pull-up resistor - dataPtr = 0 ; for (;;) @@ -93,18 +97,14 @@ int main (void) s = data [dataPtr++] ; // State d = data [dataPtr++] ; // Duration (10ths) - if ((l + s + d) == 27) + if (s == 9) // 9 -> End Marker { dataPtr = 0 ; continue ; } digitalWrite (l, s) ; - - if (digitalRead (8) == 0) // Pressed as our switch shorts to ground - delay (d * 10) ; // Faster! - else - delay (d * 100) ; + delay (d * 100) ; } return 0 ; diff --git a/examples/test2.c b/examples/blink8.c similarity index 66% rename from examples/test2.c rename to examples/blink8.c index 580591e..602d3c0 100644 --- a/examples/test2.c +++ b/examples/blink8.c @@ -1,6 +1,7 @@ /* - * test2.c: - * This tests the hardware PWM channel. + * blink8.c: + * Simple sequence over the first 8 GPIO pins - LEDs + * Aimed at the Gertboard, but it's fairly generic. * * Copyright (c) 2012-2013 Gordon Henderson. *********************************************************************** @@ -22,37 +23,35 @@ *********************************************************************** */ -#include - #include -#include -#include +#include int main (void) { - int bright ; + int i, led ; - printf ("Raspberry Pi wiringPi PWM test program\n") ; + printf ("Raspberry Pi - 8-LED Sequencer\n") ; + printf ("==============================\n") ; + printf ("\n") ; + printf ("Connect LEDs to the first 8 GPIO pins and watch ...\n") ; - if (wiringPiSetup () == -1) - exit (1) ; + wiringPiSetup () ; - pinMode (1, PWM_OUTPUT) ; + for (i = 0 ; i < 8 ; ++i) + pinMode (i, OUTPUT) ; for (;;) { - for (bright = 0 ; bright < 1024 ; ++bright) + for (led = 0 ; led < 8 ; ++led) { - pwmWrite (1, bright) ; - delay (1) ; + digitalWrite (led, 1) ; + delay (100) ; } - for (bright = 1023 ; bright >= 0 ; --bright) + for (led = 0 ; led < 8 ; ++led) { - pwmWrite (1, bright) ; - delay (1) ; + digitalWrite (led, 0) ; + delay (100) ; } } - - return 0 ; } diff --git a/examples/clock.c b/examples/clock.c new file mode 100644 index 0000000..9a53210 --- /dev/null +++ b/examples/clock.c @@ -0,0 +1,201 @@ +/* + * clock.c: + * Demo of the 128x64 graphics based LCD driver. + * This is designed to drive the parallel interface LCD drivers + * based on the popular 12864H controller chip. + * + * This test program assumes the following: + * (Which is currently hard-wired into the driver) + * + * GPIO 0-7 is connected to display data pins 0-7. + * GPIO 10 is CS1 + * GPIO 11 is CS2 + * GPIO 12 is STROBE + * GPIO 10 is RS + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + +double clockRadius ; +double thickness, barLen ; +int maxX, maxY ; + +double rads (double degs) +{ + return degs * M_PI / 180.0 ; +} + +void drawClockHands (void) +{ + time_t t ; + struct tm *now ; + double angle, p, x0, y0, x1, y1 ; + int h24, h, m, s ; + char text [20] ; + + time (&t) ; + now = localtime (&t) ; + + h24 = now->tm_hour ; + m = now->tm_min ; + s = now->tm_sec ; + + h = h24 ; + if (h > 12) + h -= 12 ; + +// Hour hand + + angle = h * 30 + m * 0.5 ; + x0 = sin (rads (angle)) * (clockRadius * 0.75) ; + y0 = cos (rads (angle)) * (clockRadius * 0.75) ; + for (p = -3.0 ; p <= 3.0 ; p += 0.2) + { + x1 = sin (rads (angle + p)) * (clockRadius * 0.7) ; + y1 = cos (rads (angle + p)) * (clockRadius * 0.7) ; + lcd128x64line (0, 0, x1, y1, 1) ; + lcd128x64lineTo (x0, y0, 1) ; + } + +// Minute hand + + angle = m * 6 ; + x0 = sin (rads (angle)) * (clockRadius * 0.9) ; + y0 = cos (rads (angle)) * (clockRadius * 0.9) ; + for (p = -1.0 ; p <= 1.0 ; p += 0.2) + { + x1 = sin (rads (angle + p)) * (clockRadius * 0.85) ; + y1 = cos (rads (angle + p)) * (clockRadius * 0.85) ; + lcd128x64line (0, 0, x1, y1, 1) ; + lcd128x64lineTo (x0, y0, 1) ; + } + +// Second hand + + angle = s * 6 ; + x0 = sin (rads (angle)) * (clockRadius * 0.2) ; + y0 = cos (rads (angle)) * (clockRadius * 0.2) ; + x1 = sin (rads (angle)) * (clockRadius * 0.95) ; + y1 = cos (rads (angle)) * (clockRadius * 0.95) ; + lcd128x64line (0 - x0, 0 - y0, x1, y1, 1) ; + lcd128x64circle (0, 0, clockRadius * 0.1, 0, 1) ; + lcd128x64circle (0, 0, clockRadius * 0.05, 1, 1) ; + +// Text: + + sprintf (text, "%02d:%02d:%02d", h24, m, s) ; + lcd128x64puts (32, 24, text, 0, 1) ; + + sprintf (text, "%2d/%2d/%2d", now->tm_mday, now->tm_mon + 1, now->tm_year - 100) ; + lcd128x64puts (32, -23, text, 0, 1) ; +} + +void drawClockFace (void) +{ + int m ; + double d, px1, py1, px2, py2 ; + + lcd128x64clear (0) ; + lcd128x64circle (0,0, clockRadius, 1, TRUE) ; + lcd128x64circle (0,0, clockRadius - thickness, 0, TRUE) ; + +// The four big indicators for 12,15,30 and 45 + + lcd128x64rectangle (- 3, clockRadius - barLen, 3, clockRadius, 1, TRUE) ; // 12 + lcd128x64rectangle (clockRadius - barLen, 3, clockRadius, -3, 1, TRUE) ; // 3 + lcd128x64rectangle (- 3, -clockRadius + barLen, 3, -clockRadius, 1, TRUE) ; // 6 + lcd128x64rectangle (-clockRadius + barLen, 3, -clockRadius, -3, 1, TRUE) ; // 9 + + +// Smaller 5 and 1 minute ticks + + for (m = 0 ; m < 60 ; ++m) + { + px1 = sin (rads (m * 6)) * clockRadius ; + py1 = cos (rads (m * 6)) * clockRadius ; + if ((m % 5) == 0) + d = barLen ; + else + d = barLen / 2.0 ; + + px2 = sin (rads (m * 6)) * (clockRadius - d) ; + py2 = cos (rads (m * 6)) * (clockRadius - d) ; + lcd128x64line (px1, py1, px2, py2, 1) ; + } +} + +void setup (void) +{ + lcd128x64getScreenSize (&maxX, &maxY) ; + clockRadius = maxY / 2 - 1 ; + thickness = maxX / 48 ; + barLen = thickness * 4 ; + lcd128x64setOrigin (32, 32) ; +} + + + + +/* + *********************************************************************** + * The main program + *********************************************************************** + */ + +int main (int argc, char *argv []) +{ + time_t now ; + + wiringPiSetup () ; + + lcd128x64setup () ; + + setup () ; + for (;;) + { + drawClockFace () ; + drawClockHands () ; + lcd128x64update () ; + + now = time (NULL) ; + while (time (NULL) == now) + delay (10) ; + } + + + return 0 ; +} diff --git a/examples/ds1302.c b/examples/ds1302.c new file mode 100644 index 0000000..f1e9e20 --- /dev/null +++ b/examples/ds1302.c @@ -0,0 +1,238 @@ +/* + * ds1302.c: + * Real Time clock + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include +#include + +#include +#include + +// Register defines + +#define RTC_SECS 0 +#define RTC_MINS 1 +#define RTC_HOURS 2 +#define RTC_DATE 3 +#define RTC_MONTH 4 +#define RTC_DAY 5 +#define RTC_YEAR 6 +#define RTC_WP 7 +#define RTC_TC 8 +#define RTC_BM 31 + + +static unsigned int masks [] = { 0x7F, 0x7F, 0x3F, 0x3F, 0x1F, 0x07, 0xFF } ; + + +/* + * bcdToD: dToBCD: + * BCD decode/encode + ********************************************************************************* + */ + +static int bcdToD (unsigned int byte, unsigned int mask) +{ + unsigned int b1, b2 ; + byte &= mask ; + b1 = byte & 0x0F ; + b2 = ((byte >> 4) & 0x0F) * 10 ; + return b1 + b2 ; +} + +static unsigned int dToBcd (unsigned int byte) +{ + return ((byte / 10) << 4) + (byte % 10) ; +} + + +/* + * ramTest: + * Simple test of the 31 bytes of RAM inside the DS1302 chip + ********************************************************************************* + */ + +static int ramTestValues [] = + { 0x00, 0xFF, 0xAA, 0x55, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0xF0, 0x0F, -1 } ; + +static int ramTest (void) +{ + int addr ; + int got ; + int i = 0 ; + int errors = 0 ; + int testVal ; + + printf ("DS1302 RAM TEST\n") ; + + testVal = ramTestValues [i] ; + + while (testVal != -1) + { + for (addr = 0 ; addr < 31 ; ++addr) + ds1302ramWrite (addr, testVal) ; + + for (addr = 0 ; addr < 31 ; ++addr) + if ((got = ds1302ramRead (addr)) != testVal) + { + printf ("DS1302 RAM Failure: Address: %2d, Expected: 0x%02X, Got: 0x%02X\n", + addr, testVal, got) ; + ++errors ; + } + testVal = ramTestValues [++i] ; + } + + for (addr = 0 ; addr < 31 ; ++addr) + ds1302ramWrite (addr, addr) ; + + for (addr = 0 ; addr < 31 ; ++addr) + if ((got = ds1302ramRead (addr)) != addr) + { + printf ("DS1302 RAM Failure: Address: %2d, Expected: 0x%02X, Got: 0x%02X\n", + addr, addr, got) ; + ++errors ; + } + + if (errors == 0) + printf ("-- DS1302 RAM TEST: OK\n") ; + else + printf ("-- DS1302 RAM TEST FAILURE. %d errors.\n", errors) ; + + return 0 ; +} + +/* + * setLinuxClock: + * Set the Linux clock from the hardware + ********************************************************************************* + */ + +static int setLinuxClock (void) +{ + char dateTime [20] ; + char command [64] ; + int clock [8] ; + + + printf ("Setting the Linux Clock from the DS1302... ") ; fflush (stdout) ; + + ds1302clockRead (clock) ; + +// [MMDDhhmm[[CC]YY][.ss]] + + sprintf (dateTime, "%02d%02d%02d%02d%02d%02d.%02d", + bcdToD (clock [RTC_MONTH], masks [RTC_MONTH]), + bcdToD (clock [RTC_DATE], masks [RTC_DATE]), + bcdToD (clock [RTC_HOURS], masks [RTC_HOURS]), + bcdToD (clock [RTC_MINS], masks [RTC_MINS]), + 20, + bcdToD (clock [RTC_YEAR], masks [RTC_YEAR]), + bcdToD (clock [RTC_SECS], masks [RTC_SECS])) ; + + sprintf (command, "/bin/date %s", dateTime) ; + system (command) ; + + return 0 ; +} + + +/* + * setDSclock: + * Set the DS1302 block from Linux time + ********************************************************************************* + */ + +static int setDSclock (void) +{ + struct tm t ; + time_t now ; + int clock [8] ; + + printf ("Setting the clock in the DS1302 from Linux time... ") ; + + now = time (NULL) ; + gmtime_r (&now, &t) ; + + clock [ 0] = dToBcd (t.tm_sec) ; // seconds + clock [ 1] = dToBcd (t.tm_min) ; // mins + clock [ 2] = dToBcd (t.tm_hour) ; // hours + clock [ 3] = dToBcd (t.tm_mday) ; // date + clock [ 4] = dToBcd (t.tm_mon + 1) ; // months 0-11 --> 1-12 + clock [ 5] = dToBcd (t.tm_wday + 1) ; // weekdays (sun 0) + clock [ 6] = dToBcd (t.tm_year - 100) ; // years + clock [ 7] = 0 ; // W-Protect off + + ds1302clockWrite (clock) ; + + printf ("OK\n") ; + + return 0 ; +} + + + + +int main (int argc, char *argv []) +{ + int i ; + int clock [8] ; + + wiringPiSetup () ; + ds1302setup (0, 1, 2) ; + + if (argc == 2) + { + /**/ if (strcmp (argv [1], "-slc") == 0) + return setLinuxClock () ; + else if (strcmp (argv [1], "-sdsc") == 0) + return setDSclock () ; + else if (strcmp (argv [1], "-rtest") == 0) + return ramTest () ; + else + { + printf ("Usage: ds1302 [-slc | -sdsc | -rtest]\n") ; + return EXIT_FAILURE ; + } + } + + for (i = 0 ;; ++i) + { + printf ("%5d: ", i) ; + + ds1302clockRead (clock) ; + printf (" %2d:%02d:%02d", + bcdToD (clock [2], masks [2]), bcdToD (clock [1], masks [1]), bcdToD (clock [0], masks [0])) ; + + printf (" %2d/%02d/%04d", + bcdToD (clock [3], masks [3]), bcdToD (clock [4], masks [4]), bcdToD (clock [6], masks [6]) + 2000) ; + + printf ("\n") ; + + delay (200) ; + } + + return 0 ; +} diff --git a/examples/gertboard.png b/examples/gertboard.png deleted file mode 100644 index 03c5cdd63dbb163330d19400cb9d896211e86ae7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4834 zcmV<85*_V{P)Px#Fi=cXMfm*q0Q~p>ytw%M0JyvW_yG900Js4B006uIczk&Hpa1{>`1k+-00000 z0000003YmlivR!s32;bRa{vGf5&!@T5&_cPe*6Fc00(qQO+^RX2n!P-G8A#+egFUx zI7vi7RCwC$olTD9s1krZnpKbF1$wJ`5_jm9SN$X(qnC4zTIC&PJG-}?l=1|vx&z2+ zl`>0lmj^_E0RzTARqgDT&df?@772Vr1cbvFH}H#W&2zM!B+G2jWw!pux6B65neB8t zCpP$=*oe#VfdDXIT0#)HPVMzW4=|`V2Lj*z{P7Jy zJ@p&>a3Ch%lPUxB8-N43sH&eYw+q1S3^xM;@RkPQ08;(dRL#9CghUWkjDa|T1>urQ z17cXr7X$(wAc%S5yANWXdhgE~R32%_sxAeS5HFJ9ihLmos|3u}cB;=sxUAg=_X zU=c(ML;{J#$f4^Eh?WGPWTqenBL!j}Ckvu0Z$K`$n|`+-N-8gJdJn$8yrF~?2$v27 ztH-dNlOQmHFbm>TjM-{ef&BQE0XaDv0fV9dE+`qJTB_1-_|SXb;SIr@B&b%X><^@&Y4>wioqL?-2+X4Pn5Qy>KAa(z3E3 zCjvR8Bm(eW5>d7hgM3GT5k`HZ(piZR2qP2D*@)}4d>e@+vY(BlB=X%?E&_Qo-EW{; zUSCuM@q4ZD7kl?wK1k4OO+X+7alKXqazfpGO^+j1GW(As)b(l|I|t*4>aL8DzrhQB z)W)u>q$_X65M>qmg_;FUBEQDS{>%7UzYfT2D0I_TqavEV*9nG6I(E|=9`g5mD zrCV9_4K2g%Xb8j9FpjH3*=gsd3O$ z+g0A!`+5+CanUckZ@$!(HvkzB`(SZ3n^)aojRJWE`Y)tF48*V#2oQ+!f=3W769m!y zulkE4qUxi#2LcWRzcV0^0Wm!2&JxNimx^NROTAaK4ZiQM$gC=gZk7XJW>^Q>Ei#G?% z!8oD?VaAhwHOLuVUJc^LkvHoATM+8CfO;IQ@6&OF@rR3YP>{AddDD6hMqv}p4o%c9 z25J7T-2NIE`KZu~o` zR^x5$J*$?ke`BIh7DT8${R)Cp`u*?{fe54>gA#(OZ{Cj%(zcC&R`aw*e|!-Ag=j<@ zf>b^~x&2!?rMNE1HuE$f4GM-p)K_9O7*w<74vbw8h^af;##n8kpcJWkKRig=_#jx@ z5kyNwgE(tBZm;D)pa^n&V;3QaX`e=r)oVFukbJLon2$yZtr}q`s+RI3=}_^Gf)8ns(c8T%iReRHJ;@{hS!C?2 z)8*|Mt13yUUcK-Q(St^jpy`n)(wylfEa%(1XOd4&u4lqo~fV6gYZhxY44X%CN@&*#!IMeXkOdU3@VjSdTxAc z7$DheRT(?;vn_e%(m4{;klDoCePm^0_dyzAV?kn_8_yxr(6MSs$TYO5&N3^yJ=}N> zl^V^UQe$K_2;Vbg%4_S#q0*Wp{0tD81&LLQ=jtc&qALqo?g(;~K@CdA?j6U;qOtp3 z0WTT5gdHWqpi&Y1#w|BAhe~brK{n5zp^e2!qP{g`z8UTf7#8O+jdp z8JJ8dH!vj-o(BngEt&tFC=i!~%tPOmP?8|-;)_}kUT4CkhL%t(NY&W$&Jcz!^X1^o_Hi|1`i z6eYrItZwtjE{i=*L6>A@r&Ulw39z59C84%{d}YD|fD~0rW>xFwfUx5D=nv&Q{H^4< z!|-@0XStD`?Fqu6(ljMhHnfCVZRG(-u!`4q5dVunwOGNX#U3EK^1WG62Hf&19k|t` z{KaLORD`Ftp?PinszGEV5qjvwiU?%rz*~f=3^Ln!E(T#xsjU)XkUj$GLXg4gToR;k z2p_odJpQfBxbeJr2=CQ=0@8O82t*)O=Mo?cDjRy*tht8fP^qmG5HPFbgdqNkYhbmj zNd-n~L7=vN>p`?c`XMh7*9p{t@MN!51QBIHu?k}{HbSa3xPk>5=svH`C1Yx>z8WObVKuM5ZDof6$|iGOBU-aemV%BA8->f zw`4TW{+B1}#!|-4b_S`_7+Gr$^}ps3$dXfU@m#eTE%#K56_rEGSy&AS&1Li_I*gNQ zL@>FgF{}Pw=lY$MTD}#8rVE&}?a_2uh)z*AJesSvwFBL;&odxx2mq+xiwZbfie4CA5n218Z9pp+uBRamEoc<1)?}|sF z?qlp`jK7QyS!MPR0L$*SfQ+RMVf`keVo}0A*U+QFbYYhHSUx5Ey204xSUp<(Ix59w z$E|8%sv)kQm}F@Zc4L+kYvO*{T|azXHN@<~C-g>*ojAY&2eyYKRSG`vl;#IFE=ae{r0ivGd>G;3zFCIF@}Sp}=Ja$(Ouu76uO%UoCH^i2;wN0@ z#wS8m@ogXaL_@QL=%^AhA-XgOpNh^edzl-b0*QvyF4X8T=4ML0hO&#Jc`Q)DYuGl^ z({{r?ZakYRzH24vtCniM+cY75flVs{h3QiXQ}ge-ecX6H)fd-(iRqP-gs4G0*8J(M zJ&L$^RV}g60SFPmb4IS4suj2nFr}fW5lR1T@?s~eedFEd=%ly+&f5r9bL+j zPZ(WBlgqLXgv_Q{URS&EJfj9!Ms#seCZW!NhYFKXhCaSRD=vGr8*gr~mkH46e{ko< ztLE$elkQuT!J5`Z)sokEjLWr+F8i8^{VsLmCFJi)HFVLHgxT}nLv$5mHp|YJoomDh zWR)Awpm4i0C3pYY&iwtvRDJyGN{2jg7haAzDSNdWU#gxiN7`P?l$u?sMjZ=st6Cfi zGZs{7-j%-7vRAwDym-5_p>qu_OV!gda0$yF_Qgezbp1q4mQHpum2mCI-p)AqlQU*_((H}q60pUKk6j-{+~rLQG^pW#p<`Q5xVbHvcmdc3n)cAqI>s%h4N44-GK zr|Q?W8po7+E7nh6r(Hw*S+|WUdpii8dh5Ebfr99@{aT2o%SO*a4f zT$$yNEBi{$VgTZ=74JKaP2ct1muu|`UJF;q+LonTg~Kd*KMsiFd*gw03Z#3gtzLQ= zv@}U|!dyxZgqCk}Yhoja2tf3`;=gj3wO@+H%a2@e(t-pImA87ouUdNMY)0T;#9=5ApjG@m5bcs5cfmlu;PqOj z?sOy~nnZx^a8J|i3ggH7;UHWy9`H|_iv1o=wI1un|NnqI){Wo(@t#%d*I74yfg68l zi7avB?*pX5jbCVVAG>b+5;y)1BvR+b--BvZx$(#LwHCPX#~w#kxbcS^*&lBF!;OEq z@eeot;l@AQ_=g+6(v3glU0fe-{KJiZxbY7+{^7j~vk@f4wA5FDZuN!}uUTf>R@y8iQR;(L;$dP^Hy7326 zt@j(*5AOYbIwnZF7^FF*YKavfpDMRzOnLt2(>rg?a**m@jEBy*^SRiXO|xAj?*?+a ze58q-mw`M-+e!GLi!hFn0cm#F+7w8TAgtFp+AhsAJi`g#_4x$P&+7ezw@ejuO%`5p zX5z0JM5M~a|4u7L_lKvV@zP;eBd?Zi0mNowenzou{q^N`hKpA43uqm`AqYNBucue_ z*Mfitg7>%8ASvw~?|vJ|l4=09K`9+Udo3(xKw2M!Zal9+@Nu~rsAj~V7lE`s2<;(= z?sxy7ck?2dHTI;8y`!(Moere6(gR;0x{fypUj{^gxnnDVw1Pxx0M7Jx3Zfp*Y1P7f zZkPWmabDD`rFZkP7oj%LjB2JYPPHvS=g9CJfE9&($Mmh2AHC2+fAG9V_e+gaSeJX(4;4-7SfuorBTO}JX1Gz7hzPJs{d2i&4Z4g~AvY(s;n zeFdV$pe6B&f72kCjaDseuhwrnb4jp3iL|yC^CW`n1T^%ry<8-bSRR*41_Zw)S)A?P z7zAyk72u1}wz2_14Q_pH#x$*!h}YguB7T<<*+@%WJH%F!2Em_4AjU=r#P78l*G&T5 zQhc`8G9WF8QMH;#wIUEl+?L0Y7$oeq&R(yjY($%hKaS|kTXZwVQ^R=>v=0H+u(4v} z9dX?lLY~WXsLg}WnH+biaYOD8jw8xX8pn}EBYS#4Ji^HSKVc*y-#-4=i~s-t07*qo IM6N<$f;V*&MF0Q* diff --git a/examples/isr.c b/examples/isr.c index 2bef54a..abc6aec 100644 --- a/examples/isr.c +++ b/examples/isr.c @@ -38,16 +38,11 @@ #include -// What GPIO input are we using? -// This is a wiringPi pin number - -#define BUTTON_PIN 0 - // globalCounter: // Global variable to count interrupts // Should be declared volatile to make sure the compiler doesn't cache it. -static volatile int globalCounter = 0 ; +static volatile int globalCounter [8] ; /* @@ -55,10 +50,14 @@ static volatile int globalCounter = 0 ; ********************************************************************************* */ -void myInterrupt (void) -{ - ++globalCounter ; -} +void myInterrupt0 (void) { ++globalCounter [0] ; } +void myInterrupt1 (void) { ++globalCounter [1] ; } +void myInterrupt2 (void) { ++globalCounter [2] ; } +void myInterrupt3 (void) { ++globalCounter [3] ; } +void myInterrupt4 (void) { ++globalCounter [4] ; } +void myInterrupt5 (void) { ++globalCounter [5] ; } +void myInterrupt6 (void) { ++globalCounter [6] ; } +void myInterrupt7 (void) { ++globalCounter [7] ; } /* @@ -69,30 +68,42 @@ void myInterrupt (void) int main (void) { - int myCounter = 0 ; + int gotOne, pin ; + int myCounter [8] ; - if (wiringPiSetup () < 0) - { - fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ; - return 1 ; - } + for (pin = 0 ; pin < 8 ; ++pin) + globalCounter [pin] = myCounter [pin] = 0 ; - if (wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0) - { - fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ; - return 1 ; - } + wiringPiSetup () ; + wiringPiISR (0, INT_EDGE_FALLING, &myInterrupt0) ; + wiringPiISR (1, INT_EDGE_FALLING, &myInterrupt1) ; + wiringPiISR (2, INT_EDGE_FALLING, &myInterrupt2) ; + wiringPiISR (3, INT_EDGE_FALLING, &myInterrupt3) ; + wiringPiISR (4, INT_EDGE_FALLING, &myInterrupt4) ; + wiringPiISR (5, INT_EDGE_FALLING, &myInterrupt5) ; + wiringPiISR (6, INT_EDGE_FALLING, &myInterrupt6) ; + wiringPiISR (7, INT_EDGE_FALLING, &myInterrupt7) ; for (;;) { + gotOne = 0 ; printf ("Waiting ... ") ; fflush (stdout) ; - while (myCounter == globalCounter) - delay (100) ; - - printf (" Done. counter: %5d\n", globalCounter) ; - myCounter = globalCounter ; + for (;;) + { + for (pin = 0 ; pin < 8 ; ++pin) + { + if (globalCounter [pin] != myCounter [pin]) + { + printf (" Int on pin %d: Counter: %5d\n", pin, globalCounter [pin]) ; + myCounter [pin] = globalCounter [pin] ; + ++gotOne ; + } + } + if (gotOne != 0) + break ; + } } return 0 ; diff --git a/examples/lcd.c b/examples/lcd.c index 6024917..c013585 100644 --- a/examples/lcd.c +++ b/examples/lcd.c @@ -4,7 +4,19 @@ * This is designed to drive the parallel interface LCD drivers * based in the Hitachi HD44780U controller and compatables. * - * Copyright (c) 2012 Gordon Henderson. + * This test program assumes the following: + * + * 8-bit displays: + * GPIO 0-7 is connected to display data pins 0-7. + * GPIO 11 is the RS pin. + * GPIO 10 is the Strobe/E pin. + * + * For 4-bit interface: + * GPIO 4-7 is connected to display data pins 4-7. + * GPIO 11 is the RS pin. + * GPIO 10 is the Strobe/E pin. + * + * Copyright (c) 2012-2013 Gordon Henderson. *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -35,94 +47,208 @@ #include #include -int main (void) -{ - int i, j ; - int fd1, fd2 ; +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif - char message1 [256] ; - char message2 [256] ; - char buf1 [30] ; - char buf2 [30] ; +static unsigned char newChar [8] = +{ + 0b11111, + 0b10001, + 0b10001, + 0b10101, + 0b11111, + 0b10001, + 0b10001, + 0b11111, +} ; + + +int usage (const char *progName) +{ + fprintf (stderr, "Usage: %s bits cols rows\n", progName) ; + return EXIT_FAILURE ; +} + +static const char *message = + " " + "Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/" + " " ; + +void scrollMessage (int lcd, int line, int width) +{ + char buf [32] ; + static int position = 0 ; + + strncpy (buf, &message [position], width) ; + buf [width] = 0 ; + lcdPosition (lcd, 0, line) ; + lcdPuts (lcd, buf) ; + + if (++position == (strlen (message) - width)) + position = 0 ; +} + +static void pingPong (int lcd, int cols) +{ + static int position = 0 ; + static int dir = 0 ; + + if (dir == 0) // Setup + { + dir = 1 ; + lcdPosition (lcd, 0, 3) ; + lcdPutchar (lcd, '*') ; + return ; + } + + lcdPosition (lcd, position, 3) ; + lcdPutchar (lcd, ' ') ; + position += dir ; + + if (position == cols) + { + dir = -1 ; + --position ; + } + + if (position < 0) + { + dir = 1 ; + ++position ; + } + + lcdPosition (lcd, position, 3) ; + lcdPutchar (lcd, '#') ; +} + + + +static void waitForEnter (void) +{ + printf ("Press ENTER to continue: ") ; + (void)fgetc (stdin) ; +} + +int main (int argc, char *argv[]) +{ + int i ; + int lcd ; + int bits, rows, cols ; struct tm *t ; time_t tim ; - printf ("Raspberry Pi LCD test program\n") ; + char buf [32] ; - if (wiringPiSetup () == -1) - exit (1) ; + if (argc != 4) + return usage (argv [0]) ; - fd1 = lcdInit (4, 20, 4, 8, 9, 4,5,6,7,0,0,0,0) ; - fd2 = lcdInit (2, 16, 4, 8, 10, 4,5,6,7,0,0,0,0) ; + printf ("Raspberry Pi LCD test\n") ; + printf ("=====================\n") ; -//fd1 = lcdInit (4, 20, 8, 8, 9, 0,1,2,3,4,5,6,7) ; -//fd2 = lcdInit (2, 16, 8, 8, 10, 0,1,2,3,4,5,6,7) ; + bits = atoi (argv [1]) ; + cols = atoi (argv [2]) ; + rows = atoi (argv [3]) ; - if (fd1 == -1) + if (!((rows == 1) || (rows == 2) || (rows == 4))) { - printf ("lcdInit 1 failed\n") ; - return 1 ; + fprintf (stderr, "%s: rows must be 1, 2 or 4\n", argv [0]) ; + return EXIT_FAILURE ; } - if (fd2 == -1) + if (!((cols == 16) || (cols == 20))) { - printf ("lcdInit 2 failed\n") ; - return 1 ; + fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ; + return EXIT_FAILURE ; } - sleep (1) ; + wiringPiSetup () ; - lcdPosition (fd1, 0, 0) ; lcdPuts (fd1, " Gordon Henderson") ; - lcdPosition (fd1, 0, 1) ; lcdPuts (fd1, " --------------") ; -/* - lcdPosition (fd1, 0, 2) ; lcdPuts (fd1, " 00:00:00") ; - lcdPosition (fd1, 0, 3) ; lcdPuts (fd1, " DD:MM:YY") ; -*/ + if (bits == 4) + lcd = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ; + else + lcd = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ; - lcdPosition (fd2, 0, 0) ; lcdPuts (fd2, "Gordon Henderson") ; - lcdPosition (fd2, 0, 1) ; lcdPuts (fd2, "----------------") ; + if (lcd < 0) + { + fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ; + return -1 ; + } + + lcdPosition (lcd, 0, 0) ; lcdPuts (lcd, "Gordon Henderson") ; + + if (rows > 1) + { + lcdPosition (lcd, 0, 1) ; + for (i = 0 ; i < (cols - 1) ; ++i) + lcdPutchar (lcd, '*') ; + lcdPutchar (lcd, '2') ; + + if (rows == 4) + { + lcdPosition (lcd, 0, 2) ; + for (i = 0 ; i < ((cols - 1) / 2) ; ++i) + lcdPuts (lcd, "=-") ; + lcdPuts (lcd, "=3") ; + + lcdPosition (lcd, 0, 3) ; + for (i = 0 ; i < ((cols - 1) / 2) ; ++i) + lcdPuts (lcd, "-=") ; + lcdPuts (lcd, "-4") ; + } + } sleep (2) ; - sprintf (message1, "%s", " http://projects.drogon.net/ ") ; - sprintf (message2, "%s", " This is a long message to go into the smaller display just for a demonstration of what we can do. ") ; + lcdPosition (lcd, 0, 0) ; lcdPuts (lcd, " wiringpi.com ") ; + + + waitForEnter () ; + + lcdCharDef (lcd, 2, newChar) ; + + lcdClear (lcd) ; + lcdPosition (lcd, 0, 0) ; + lcdPuts (lcd, "User Char: ") ; + lcdPutchar (lcd, 2) ; + + lcdCursor (lcd, TRUE) ; + lcdCursorBlink (lcd, TRUE) ; + + waitForEnter () ; + + lcdCursor (lcd, FALSE) ; + lcdCursorBlink (lcd, FALSE) ; + for (;;) { - i = 0 ; - j = 0 ; - for (;;) - { - strncpy (buf1, &message1 [i], 20) ; - buf1 [20] = 0 ; - lcdPosition (fd1, 0, 1) ; - lcdPuts (fd1, buf1) ; - ++i ; - if (i == strlen (message1) - 20) - i = 0 ; + delay (250) ; - strncpy (buf2, &message2 [j], 16) ; - buf2 [16] = 0 ; - lcdPosition (fd2, 0, 1) ; - lcdPuts (fd2, buf2) ; - ++j ; - if (j == strlen (message2) - 16) - j = 0 ; + scrollMessage (lcd, 0, cols) ; + + if (rows == 1) + continue ; - tim = time (NULL) ; - t = localtime (&tim) ; + tim = time (NULL) ; + t = localtime (&tim) ; - sprintf (buf1, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ; - lcdPosition (fd1, 5, 2) ; - lcdPuts (fd1, buf1) ; + sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ; - sprintf (buf1, "%02d/%02d/%02d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ; - lcdPosition (fd1, 4, 3) ; - lcdPuts (fd1, buf1) ; + lcdPosition (lcd, (cols - 8) / 2, 1) ; + lcdPuts (lcd, buf) ; - delay (250) ; - } + if (rows == 2) + continue ; + + sprintf (buf, "%02d/%02d/%04d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ; + + lcdPosition (lcd, (cols - 10) / 2, 2) ; + lcdPuts (lcd, buf) ; + + pingPong (lcd, cols) ; } return 0 ; diff --git a/examples/okLed.c b/examples/okLed.c index 9b3a170..930f266 100644 --- a/examples/okLed.c +++ b/examples/okLed.c @@ -39,27 +39,26 @@ #include #include +// The OK/Act LED is connected to BCM_GPIO pin 16 + #define OK_LED 16 int main () { int fd, i ; + wiringPiSetupGpio () ; + +// Change the trigger on the OK/Act LED to "none" + if ((fd = open ("/sys/class/leds/led0/trigger", O_RDWR)) < 0) { fprintf (stderr, "Unable to change LED trigger: %s\n", strerror (errno)) ; return 1 ; } - write (fd, "none\n", 5) ; close (fd) ; - if (wiringPiSetupGpio () < 0) - { - fprintf (stderr, "Unable to setup GPIO: %s\n", strerror (errno)) ; - return 1 ; - } - softPwmCreate (OK_LED, 0, 100) ; for (;;) diff --git a/examples/pwm.c b/examples/pwm.c index c1fc331..816c832 100644 --- a/examples/pwm.c +++ b/examples/pwm.c @@ -1,7 +1,6 @@ /* * pwm.c: - * Test of the software PWM driver. Needs 12 LEDs connected - * to the Pi. + * This tests the hardware PWM channel. * * Copyright (c) 2012-2013 Gordon Henderson. *********************************************************************** @@ -23,71 +22,37 @@ *********************************************************************** */ -#include -#include -#include - #include -#include -#define RANGE 100 -#define NUM_LEDS 12 +#include +#include +#include -int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 } ; - -int values [NUM_LEDS] = { 0, 17, 32, 50, 67, 85, 100, 85, 67, 50, 32, 17 } ; - -int main () +int main (void) { - int i, j ; - char buf [80] ; + int bright ; + + printf ("Raspberry Pi wiringPi PWM test program\n") ; if (wiringPiSetup () == -1) - { - fprintf (stdout, "oops: %s\n", strerror (errno)) ; - return 1 ; - } + exit (1) ; - for (i = 0 ; i < NUM_LEDS ; ++i) - { - softPwmCreate (ledMap [i], 0, RANGE) ; - printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ; - } - - fgets (buf, 80, stdin) ; - -// Bring all up one by one: - - for (i = 0 ; i < NUM_LEDS ; ++i) - for (j = 0 ; j <= 100 ; ++j) - { - softPwmWrite (ledMap [i], j) ; - delay (10) ; - } - - fgets (buf, 80, stdin) ; - -// Down fast - - for (i = 100 ; i > 0 ; --i) - { - for (j = 0 ; j < NUM_LEDS ; ++j) - softPwmWrite (ledMap [j], i) ; - delay (10) ; - } - - fgets (buf, 80, stdin) ; + pinMode (1, PWM_OUTPUT) ; for (;;) { - for (i = 0 ; i < NUM_LEDS ; ++i) - softPwmWrite (ledMap [i], values [i]) ; + for (bright = 0 ; bright < 1024 ; ++bright) + { + pwmWrite (1, bright) ; + delay (1) ; + } - delay (50) ; - - i = values [0] ; - for (j = 0 ; j < NUM_LEDS - 1 ; ++j) - values [j] = values [j + 1] ; - values [NUM_LEDS - 1] = i ; + for (bright = 1023 ; bright >= 0 ; --bright) + { + pwmWrite (1, bright) ; + delay (1) ; + } } + + return 0 ; } diff --git a/examples/piface.c b/examples/rht03.c old mode 100644 new mode 100755 similarity index 60% rename from examples/piface.c rename to examples/rht03.c index 0f00960..e0cc116 --- a/examples/piface.c +++ b/examples/rht03.c @@ -1,8 +1,6 @@ /* - * piFace.c: - * Simple test for the PiFace interface board. - * - * Read the buttons and output the same to the LEDs + * rht03.c: + * Driver for the MaxDetect series sensors * * Copyright (c) 2012-2013 Gordon Henderson. *********************************************************************** @@ -24,47 +22,42 @@ *********************************************************************** */ -#include - #include -#include -#include -int outputs [4] = { 0,0,0,0 } ; +#include +#include -void scanButton (int button) -{ - if (digitalRead (button) == LOW) - { - outputs [button] ^= 1 ; - digitalWrite (button, outputs [button]) ; - } - - while (digitalRead (button) == LOW) - delay (1) ; -} +#define RHT03_PIN 0 +/* + *********************************************************************** + * The main program + *********************************************************************** + */ int main (void) { - int pin, button ; + int temp, rh ; + int newTemp, newRh ; - 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) ; + temp = rh = newTemp = newRh = 0 ; + wiringPiSetup () ; + piHiPri (55) ; for (;;) { - for (button = 0 ; button < 4 ; ++button) - scanButton (button) ; - delay (1) ; + delay (100) ; + + if (!readRHT03 (RHT03_PIN, &newTemp, &newRh)) + continue ; + + if ((temp != newTemp) || (rh != newRh)) + { + temp = newTemp ; + rh = newRh ; + printf ("Temp: %5.1f, RH: %5.1f%%\n", temp / 10.0, rh / 10.0) ; + } } return 0 ; diff --git a/examples/softPwm.c b/examples/softPwm.c new file mode 100644 index 0000000..11f7ad0 --- /dev/null +++ b/examples/softPwm.c @@ -0,0 +1,89 @@ +/* + * softPwm.c: + * Test of the software PWM driver. Needs 8 LEDs connected + * to the Pi - e.g. Ladder board. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#include +#include + +#define RANGE 100 +#define NUM_LEDS 8 + +int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7 } ; + +int values [NUM_LEDS] = { 0, 25, 50, 75, 100, 75, 50, 25 } ; + +int main () +{ + int i, j ; + char buf [80] ; + + wiringPiSetup () ; + + for (i = 0 ; i < NUM_LEDS ; ++i) + { + softPwmCreate (ledMap [i], 0, RANGE) ; + printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ; + } + + fgets (buf, 80, stdin) ; + +// Bring all up one by one: + + for (i = 0 ; i < NUM_LEDS ; ++i) + for (j = 0 ; j <= 100 ; ++j) + { + softPwmWrite (ledMap [i], j) ; + delay (10) ; + } + + fgets (buf, 80, stdin) ; + +// All Down + + for (i = 100 ; i > 0 ; --i) + { + for (j = 0 ; j < NUM_LEDS ; ++j) + softPwmWrite (ledMap [j], i) ; + delay (10) ; + } + + fgets (buf, 80, stdin) ; + + for (;;) + { + for (i = 0 ; i < NUM_LEDS ; ++i) + softPwmWrite (ledMap [i], values [i]) ; + + delay (50) ; + + i = values [0] ; + for (j = 0 ; j < NUM_LEDS - 1 ; ++j) + values [j] = values [j + 1] ; + values [NUM_LEDS - 1] = i ; + } +} diff --git a/examples/tone.c b/examples/softTone.c similarity index 92% rename from examples/tone.c rename to examples/softTone.c index 0e8a47d..2f46783 100644 --- a/examples/tone.c +++ b/examples/softTone.c @@ -1,5 +1,5 @@ /* - * tone.c: + * softTone.c: * Test of the softTone module in wiringPi * Plays a scale out on pin 3 - connect pizeo disc to pin 3 & 0v * @@ -38,11 +38,7 @@ int main () { int i ; - if (wiringPiSetup () == -1) - { - fprintf (stdout, "oops: %s\n", strerror (errno)) ; - return 1 ; - } + wiringPiSetup () ; softToneCreate (PIN) ; @@ -55,5 +51,4 @@ int main () delay (500) ; } } - } diff --git a/examples/speed.c b/examples/speed.c index 863317e..0a42b36 100644 --- a/examples/speed.c +++ b/examples/speed.c @@ -31,93 +31,65 @@ #define FAST_COUNT 10000000 #define SLOW_COUNT 1000000 +#define PASSES 5 + +void speedTest (int pin, int maxCount) +{ + int count, sum, perSec, i ; + unsigned int start, end ; + + sum = 0 ; + + for (i = 0 ; i < PASSES ; ++i) + { + start = millis () ; + for (count = 0 ; count < maxCount ; ++count) + digitalWrite (pin, 1) ; + end = millis () ; + printf (" %6d", end - start) ; + fflush (stdout) ; + sum += (end - start) ; + } + + digitalWrite (pin, 0) ; + printf (". Av: %6dmS", sum / PASSES) ; + perSec = (int)(double)maxCount / (double)((double)sum / (double)PASSES) * 1000.0 ; + printf (": %7d/sec\n", perSec) ; +} int main (void) { - int i ; - uint32_t start, end, count, sum, perSec ; - - printf ("Raspberry Pi wiringPi speed test program\n") ; + printf ("Raspberry Pi wiringPi GPIO speed test program\n") ; + printf ("=============================================\n") ; // Start the standard way - if (wiringPiSetup () == -1) - exit (1) ; - - printf ("Native wiringPi method: (%8d iterations)\n", FAST_COUNT) ; - + printf ("\nNative wiringPi method: (%8d iterations)\n", FAST_COUNT) ; + wiringPiSetup () ; pinMode (0, OUTPUT) ; + speedTest (0, FAST_COUNT) ; - sum = 0 ; - for (i = 0 ; i < 3 ; ++i) - { - printf (" Pass: %d: ", i) ; - fflush (stdout) ; - - start = millis () ; - for (count = 0 ; count < FAST_COUNT ; ++count) - digitalWrite (0, 1) ; - end = millis () ; - printf (" %8dmS\n", end - start) ; - sum += (end - start) ; - } - digitalWrite (0, 0) ; - printf (" Average: %8dmS", sum / 3) ; - perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ; - printf (": %6d/sec\n", perSec) ; - - - printf ("Native GPIO method: (%8d iterations)\n", FAST_COUNT) ; - - if (wiringPiSetupGpio () == -1) - exit (1) ; +// GPIO + printf ("\nNative GPIO method: (%8d iterations)\n", FAST_COUNT) ; + wiringPiSetupGpio () ; pinMode (17, OUTPUT) ; + speedTest (17, FAST_COUNT) ; - sum = 0 ; - for (i = 0 ; i < 3 ; ++i) - { - printf (" Pass: %d: ", i) ; - fflush (stdout) ; - - start = millis () ; - for (count = 0 ; count < 10000000 ; ++count) - digitalWrite (17, 1) ; - end = millis () ; - printf (" %8dmS\n", end - start) ; - sum += (end - start) ; - } - digitalWrite (17, 0) ; - printf (" Average: %8dmS", sum / 3) ; - perSec = (int)(double)FAST_COUNT / (double)((double)sum / 3.0) * 1000.0 ; - printf (": %6d/sec\n", perSec) ; +// Phys + printf ("\nPhysical pin GPIO method: (%8d iterations)\n", FAST_COUNT) ; + wiringPiSetupPhys () ; + pinMode (11, OUTPUT) ; + speedTest (11, FAST_COUNT) ; // Switch to SYS mode: - if (wiringPiSetupSys () == -1) - exit (1) ; - - printf ("/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ; - - sum = 0 ; - for (i = 0 ; i < 3 ; ++i) - { - printf (" Pass: %d: ", i) ; - fflush (stdout) ; - - start = millis () ; - for (count = 0 ; count < SLOW_COUNT ; ++count) - digitalWrite (17, 1) ; - end = millis () ; - printf (" %8dmS\n", end - start) ; - sum += (end - start) ; - } - digitalWrite (17, 0) ; - printf (" Average: %8dmS", sum / 3) ; - perSec = (int)(double)SLOW_COUNT / (double)((double)sum / 3.0) * 1000.0 ; - printf (": %6d/sec\n", perSec) ; + system ("/usr/local/bin/gpio export 17 out") ; + printf ("\n/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ; + wiringPiSetupSys () ; + speedTest (17, SLOW_COUNT) ; return 0 ; } diff --git a/gpio/Makefile b/gpio/Makefile index a043962..52b0150 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -30,20 +30,20 @@ INCLUDE = -I/usr/local/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L/usr/local/lib -LIBS = -lwiringPi -lpthread -lm +LIBS = -lwiringPi -lwiringPiDev -lpthread -lm # May not need to alter anything below this line ############################################################################### -SRC = gpio.c +SRC = gpio.c extensions.c OBJ = $(SRC:.c=.o) all: gpio -gpio: gpio.o +gpio: $(OBJ) @echo [Link] - @$(CC) -o $@ gpio.o $(LDFLAGS) $(LIBS) + @$(CC) -o $@ $(OBJ) $(LDFLAGS) $(LIBS) .c.o: @echo [Compile] $< @@ -51,7 +51,8 @@ gpio: gpio.o .PHONEY: clean clean: - rm -f $(OBJ) gpio *~ core tags *.bak + @echo "[Clean]" + @rm -f $(OBJ) gpio *~ core tags *.bak .PHONEY: tags tags: $(SRC) @@ -78,3 +79,6 @@ depend: makedepend -Y $(SRC) # DO NOT DELETE + +gpio.o: extensions.h +extensions.o: extensions.h diff --git a/gpio/extensions.c b/gpio/extensions.c new file mode 100644 index 0000000..c08d1c1 --- /dev/null +++ b/gpio/extensions.c @@ -0,0 +1,329 @@ +/* + * extensions.c: + * Part of the GPIO program to test, peek, poke and otherwise + * noodle with the GPIO hardware on the Raspberry Pi. + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include "extensions.h" + +extern int wiringPiDebug ; + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + +// Local structure to hold details + +struct extensionFunctionStruct +{ + const char *name ; + int (*function)(char *progName, int pinBase, char *params) ; +} ; + + +/* + * extractInt: + * Check & return an integer at the given location (prefixed by a :) + ********************************************************************************* + */ + +static char *extractInt (char *progName, char *p, int *num) +{ + if (*p != ':') + { + fprintf (stderr, "%s: colon expected\n", progName) ; + return NULL ; + } + + ++p ; + + if (!isdigit (*p)) + { + fprintf (stderr, "%s: digit expected\n", progName) ; + return NULL ; + } + + *num = strtol (p, NULL, 0) ; + while (isdigit (*p)) + ++p ; + + return p ; +} + + + +/* + * doExtensionMcp23008: + * MCP23008 - 8-bit I2C GPIO expansion chip + * mcp23002:base:i2cAddr + ********************************************************************************* + */ + +static int doExtensionMcp23008 (char *progName, int pinBase, char *params) +{ + int i2c ; + + if ((params = extractInt (progName, params, &i2c)) == NULL) + return FALSE ; + + if ((i2c < 0x03) || (i2c > 0x77)) + { + fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + return FALSE ; + } + + mcp23008Setup (pinBase, i2c) ; + + return TRUE ; +} + + +/* + * doExtensionMcp23017: + * MCP23008 - 16-bit I2C GPIO expansion chip + * mcp23002:base:i2cAddr + ********************************************************************************* + */ + +static int doExtensionMcp23017 (char *progName, int pinBase, char *params) +{ + int i2c ; + + if ((params = extractInt (progName, params, &i2c)) == NULL) + return FALSE ; + + if ((i2c < 0x03) || (i2c > 0x77)) + { + fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + return FALSE ; + } + + mcp23017Setup (pinBase, i2c) ; + + return TRUE ; +} + + +/* + * doExtensionMcp23s08: + * MCP23s08 - 8-bit SPI GPIO expansion chip + * mcp23s08:base:spi:port + ********************************************************************************* + */ + +static int doExtensionMcp23s08 (char *progName, int pinBase, char *params) +{ + int spi, port ; + + if ((params = extractInt (progName, params, &spi)) == NULL) + return FALSE ; + + if ((spi < 0) || (spi > 1)) + { + fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ; + return FALSE ; + } + + if ((params = extractInt (progName, params, &port)) == NULL) + return FALSE ; + + if ((port < 0) || (port > 7)) + { + fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ; + return FALSE ; + } + + mcp23s08Setup (pinBase, spi, port) ; + + return TRUE ; +} + + +/* + * doExtensionMcp23s17: + * MCP23s17 - 16-bit SPI GPIO expansion chip + * mcp23s17:base:spi:port + ********************************************************************************* + */ + +static int doExtensionMcp23s17 (char *progName, int pinBase, char *params) +{ + int spi, port ; + + if ((params = extractInt (progName, params, &spi)) == NULL) + return FALSE ; + + if ((spi < 0) || (spi > 1)) + { + fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ; + return FALSE ; + } + + if ((params = extractInt (progName, params, &port)) == NULL) + return FALSE ; + + if ((port < 0) || (port > 7)) + { + fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ; + return FALSE ; + } + + mcp23s17Setup (pinBase, spi, port) ; + + return TRUE ; +} + +/* + * doExtensionSr595: + * Shift Register 74x595 + * sr595:base:pins:data:clock:latch + ********************************************************************************* + */ + +static int doExtensionSr595 (char *progName, int pinBase, char *params) +{ + int pins, data, clock, latch ; + +// Extract pins + + if ((params = extractInt (progName, params, &pins)) == NULL) + return FALSE ; + + if ((pins < 8) || (pins > 32)) + { + fprintf (stderr, "%s: pin count (%d) out of range - 8-32 expected.\n", progName, pins) ; + return FALSE ; + } + + if ((params = extractInt (progName, params, &data)) == NULL) + return FALSE ; + + if ((params = extractInt (progName, params, &clock)) == NULL) + return FALSE ; + + if ((params = extractInt (progName, params, &latch)) == NULL) + return FALSE ; + + sr595Setup (pinBase, pins, data, clock, latch) ; + + return TRUE ; +} + + +/* + * Function list + ********************************************************************************* + */ + +struct extensionFunctionStruct extensionFunctions [] = +{ + { "mcp23008", &doExtensionMcp23008 }, + { "mcp23017", &doExtensionMcp23017 }, + { "mcp23s08", &doExtensionMcp23s08 }, + { "mcp23s17", &doExtensionMcp23s17 }, + { "sr595", &doExtensionSr595 }, + { NULL, NULL }, +} ; + + +/* + * doExtension: + * Load in a wiringPi extension + ********************************************************************************* + */ + +int doExtension (char *progName, char *extensionData) +{ + char *p ; + char *extension = extensionData ; + struct extensionFunctionStruct *extensionFn ; + int pinBase = 0 ; + +// Get the extension extension name by finding the first colon + + p = extension ; + while (*p != ':') + { + if (!*p) // ran out of characters + { + fprintf (stderr, "%s: extension name not terminated by a colon\n", progName) ; + return FALSE ; + } + ++p ; + } + + *p++ = 0 ; + + if (!isdigit (*p)) + { + fprintf (stderr, "%s: pinBase number expected after extension name\n", progName) ; + return FALSE ; + } + + while (isdigit (*p)) + { + if (pinBase > 1000000000) + { + fprintf (stderr, "%s: pinBase too large\n", progName) ; + return FALSE ; + } + + pinBase = pinBase * 10 + (*p - '0') ; + ++p ; + } + + if (pinBase < 64) + { + fprintf (stderr, "%s: pinBase (%d) too small. Minimum is 64.\n", progName, pinBase) ; + return FALSE ; + } + +// Search for extensions: + + for (extensionFn = extensionFunctions ; extensionFn->name != NULL ; ++extensionFn) + { + if (strcmp (extensionFn->name, extension) == 0) + return extensionFn->function (progName, pinBase, p) ; + } + + fprintf (stderr, "%s: extension %s not found\n", progName, extension) ; + return FALSE ; +} diff --git a/gpio/extensions.h b/gpio/extensions.h new file mode 100644 index 0000000..5d27123 --- /dev/null +++ b/gpio/extensions.h @@ -0,0 +1,26 @@ +/* + * extensions.h: + * Part of the GPIO program to test, peek, poke and otherwise + * noodle with the GPIO hardware on the Raspberry Pi. + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + + +extern int doExtension (char *progName, char *extensionData) ; diff --git a/gpio/gpio.1 b/gpio/gpio.1 index ec65519..ae4df4e 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -1,15 +1,19 @@ -.TH "GPIO" "21st October 2012" "Command-Line access to Raspberry Pi and PiFace GPIO" +.TH "GPIO" "March 2013" "Command-Line access to Raspberry Pi's GPIO" .SH NAME -gpio \- Command-line access to Raspberry Pi and PiFace GPIO +gpio \- Command-line access to Raspberry Pi's GPIO .SH SYNOPSIS .B gpio .B \-v .PP .B gpio -.B [ \-g ] -.B read/write/wb/pwm/clock/mode ... +.B [ \-g | \-1 ] +.B mode/read/write/aread/awrite/wb/pwm/clock ... +.PP +.B gpio +.B [ \-x extension:params ] +.B mode/read/write/aread/awrite/pwm ... .PP .B gpio .B [ \-p ] @@ -17,7 +21,7 @@ gpio \- Command-line access to Raspberry Pi and PiFace GPIO .B ... .PP .B gpio -.B readall +.B readall/reset .PP .B gpio .B unexportall/exports @@ -27,6 +31,10 @@ gpio \- Command-line access to Raspberry Pi and PiFace GPIO .B ... .PP .B gpio +.B wfi +.B ... +.PP +.B gpio .B drive group value .PP @@ -73,12 +81,28 @@ Output the current version including the board revision of the Raspberry Pi. .TP .B \-g Use the BCM_GPIO pins numbers rather than wiringPi pin numbers. -\fINOTE:\fR The BCM_GPIO pin numbers are always used with the +\fINote:\fR The BCM_GPIO pin numbers are always used with the export and edge commands. +.TP +.B \-1 +Use the physical pin numbers rather than wiringPi pin numbers. +\fINote:\fR that this applies to the P1 connector only. It is not possible to +use pins on the Revision 2 P5 connector this way, and as with \-g the +BCM_GPIO pin numbers are always used with the export and edge commands. + +.TP +.B \-x extension +This causes the named extension to be initialised. Extensions +comprise of a name (e.g. mcp23017) followed by a colon, then the +pin-base, then more optional parameters depending on the extension type. +See the web page on http://wiringpi.com/the-gpio-utility/ + .TP .B \-p -Use the PiFace interface board and its corresponding pin numbers. +Use the PiFace interface board and its corresponding pin numbers. The PiFace +will always appear at pin number 200 in the gpio command. You can assign any +pin numbers you like in your own programs though. .TP .B read @@ -102,6 +126,11 @@ Output a table of all GPIO pins values. The values represent the actual values r if the pin is in input mode, or the last value written if the pin is in output mode. +.TP +.B reset +Resets the GPIO - As much as it's possible to do. All pins are set to input +mode and all the internal pull-up/down resistors are disconnected (tristate mode). + .TP .B pwm Write a PWM value (0-1023) to the given pin. The pin needs to be put @@ -157,6 +186,12 @@ requiring root/sudo. .B unexport Un-Export a GPIO pin in the /sys/class/gpio directory. +.TP +.B wfi +This set the given pin to the supplied interrupt mode: rising, falling +or both then waits for the interrupt to happen. It's a non-busy wait, +so does not consume and CPU while it's waiting. + .TP .B drive group value @@ -207,26 +242,26 @@ The board jumpers need to be in-place to do this operation. .PP .TS -r r r l. -WiringPi GPIO-r1 GPIO-r2 Function +c c c c l. +WiringPi GPIO-r1 GPIO-r2 P1-Phys Function _ -0 17 17 -1 18 18 (PWM) -2 21 27 -3 22 22 -4 23 23 -5 24 24 -6 25 25 -7 4 4 -8 0 2 I2C: SDA0 -9 1 3 I2C: SCL0 -10 8 8 SPI: CE0 -11 7 7 SPI: CE1 -12 10 10 SPI: MOSI -13 9 9 SPI: MISO -14 11 11 SPI: SCLK -15 14 14 TxD -16 15 16 RxD + 0 17 17 11 + 1 18 18 12 (PWM) + 2 21 27 13 + 3 22 22 15 + 4 23 23 16 + 5 24 24 18 + 6 25 25 22 + 7 4 4 7 + 8 0 2 3 I2C: SDA0 + 9 1 3 5 I2C: SCL0 +10 8 8 24 SPI: CE0 +11 7 7 26 SPI: CE1 +12 10 10 19 SPI: MOSI +13 9 9 21 SPI: MISO +14 11 11 23 SPI: SCLK +15 14 14 8 TxD +16 15 16 10 RxD 17 - 28 18 - 29 19 - 30 @@ -272,7 +307,7 @@ pin numbers. .LP WiringPi's home page .IP -https://projects.drogon.net/raspberry-pi/wiringpi/ +http://wiringpi.com/ .SH AUTHOR @@ -284,7 +319,7 @@ Please report bugs to .SH COPYRIGHT -Copyright (c) 2012 Gordon Henderson +Copyright (c) 2012-2013 Gordon Henderson This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. diff --git a/gpio/gpio.c b/gpio/gpio.c index e71e432..8e17ae0 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -2,7 +2,7 @@ * gpio.c: * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry * Pi's GPIO. - * Copyright (c) 2012 Gordon Henderson + * Copyright (c) 2012-2013 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -26,14 +26,20 @@ #include #include #include +#include #include #include #include -#include #include +#include +#include #include + #include +#include + +#include "extensions.h" extern int wiringPiDebug ; @@ -42,22 +48,26 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "1.12" +#define VERSION "2.02" +#define I2CDETECT "/usr/sbin/i2cdetect" static int wpMode ; char *usage = "Usage: gpio -v\n" " gpio -h\n" - " gpio [-g] ...\n" + " gpio [-g|-1] [-x extension:params] ...\n" " gpio [-p] ...\n" - " gpio readall\n" - " gpio unexportall/exports ...\n" + " gpio ...\n" + " gpio readall/reset\n" + " gpio unexportall/exports\n" " gpio export/edge/unexport ...\n" + " gpio wfi \n" " gpio drive \n" " gpio pwm-bal/pwm-ms \n" " gpio pwmr \n" " gpio pwmc \n" " gpio load spi/i2c\n" + " gpio i2cd/i2cdetect\n" " gpio gbr \n" " gpio gbw " ; // No trailing newline needed here. @@ -195,6 +205,37 @@ static void doLoad (int argc, char *argv []) } +/* + * doI2Cdetect: + * Run the i2cdetect command with the right runes for this Pi revision + ********************************************************************************* + */ + +static void doI2Cdetect (int argc, char *argv []) +{ + int port = piBoardRev () == 1 ? 0 : 1 ; + char command [128] ; + struct stat statBuf ; + + if (stat (I2CDETECT, &statBuf) < 0) + { + fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ; + return ; + } + + if (!moduleLoaded ("i2c_dev")) + { + fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ; + return ; + } + + sprintf (command, "%s -y %d", I2CDETECT, port) ; + if (system (command) < 0) + fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ; + +} + + /* * doReadall: * Read all the GPIO pins @@ -215,27 +256,39 @@ static char *alts [] = "IN ", "OUT ", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3" } ; +static int wpiToPhys [64] = +{ + 11, 12, 13, 15, 16, 18, 22, 7, // 0...7 + 3, 5, // 8...9 + 24, 26, 19, 21, 23, // 10..14 + 8, 10, // 15..16 + 3, 4, 5, 6, // 17..20 + 0,0,0,0,0,0,0,0,0,0,0, // 20..31 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63 +} ; + static void doReadall (void) { int pin ; - printf ("+----------+------+--------+------+-------+\n") ; - printf ("| wiringPi | GPIO | Name | Mode | Value |\n") ; - printf ("+----------+------+--------+------+-------+\n") ; + printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ; + printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ; + printf ("+----------+------+------+--------+------+-------+\n") ; - for (pin = 0 ; pin < 64 ; ++pin) + for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective { if (wpiPinToGpio (pin) == -1) continue ; - printf ("| %6d | %3d | %s | %s | %s |\n", - pin, wpiPinToGpio (pin), + printf ("| %6d | %3d | %3d | %s | %s | %s |\n", + pin, wpiPinToGpio (pin), wpiToPhys [pin], pinNames [pin], alts [getAlt (pin)], digitalRead (pin) == HIGH ? "High" : "Low ") ; } - printf ("+----------+------+--------+------+-------+\n") ; + printf ("+----------+------+------+--------+------+-------+\n") ; } @@ -252,9 +305,7 @@ static void doExports (int argc, char *argv []) char fName [128] ; char buf [16] ; -// Rather crude, but who knows what others are up to... - - for (first = 0, i = 0 ; i < 64 ; ++i) + for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective { // Try to read the direction @@ -386,6 +437,52 @@ void doExport (int argc, char *argv []) } +/* + * doWfi: + * gpio wfi pin mode + * Wait for Interrupt on a given pin. + * Slight cheat here - it's easier to actually use ISR now (which calls + * gpio to set the pin modes!) then we simply sleep, and expect the thread + * to exit the program. Crude but effective. + ********************************************************************************* + */ + +static void wfi (void) + { exit (0) ; } + +void doWfi (int argc, char *argv []) +{ + int pin, mode ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + /**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ; + else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ; + else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ; + else + { + fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ; + exit (1) ; + } + + if (wiringPiISR (pin, mode, &wfi) < 0) + { + fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ; + exit (1) ; + } + + for (;;) + delay (9999) ; +} + + + /* * doEdge: * gpio edge pin mode @@ -499,7 +596,7 @@ void doUnexport (int argc, char *argv []) ********************************************************************************* */ -void doUnexportall (int argc, char *argv []) +void doUnexportall (char *progName) { FILE *fd ; int pin ; @@ -508,7 +605,7 @@ void doUnexportall (int argc, char *argv []) { if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL) { - fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ; + fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ; exit (1) ; } fprintf (fd, "%d\n", pin) ; @@ -517,6 +614,30 @@ void doUnexportall (int argc, char *argv []) } +/* + * doReset: + * Reset the GPIO pins - as much as we can do + ********************************************************************************* + */ + +static void doReset (char *progName) +{ + int pin ; + + doUnexportall (progName) ; + + for (pin = 0 ; pin < 64 ; ++pin) + { + if (wpiPinToGpio (pin) == -1) + continue ; + + digitalWrite (pin, LOW) ; + pinMode (pin, INPUT) ; + pullUpDnControl (pin, PUD_OFF) ; + } +} + + /* * doMode: * gpio mode pin mode ... @@ -536,9 +657,6 @@ void doMode (int argc, char *argv []) pin = atoi (argv [2]) ; - if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) - return ; - mode = argv [3] ; /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; @@ -604,7 +722,7 @@ static void doGbw (int argc, char *argv []) if (argc != 4) { - fprintf (stderr, "Usage: %s gbr \n", argv [0]) ; + fprintf (stderr, "Usage: %s gbw \n", argv [0]) ; exit (1) ; } @@ -613,23 +731,23 @@ static void doGbw (int argc, char *argv []) if ((channel < 0) || (channel > 1)) { - fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ; + fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ; exit (1) ; } if ((value < 0) || (value > 1023)) { - fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ; + fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ; exit (1) ; } - if (gertboardSPISetup () == -1) + if (gertboardAnalogSetup (64) < 0) { fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ; exit (1) ; } - gertboardAnalogWrite (channel, value) ; + analogWrite (64 + channel, value) ; } @@ -654,21 +772,20 @@ static void doGbr (int argc, char *argv []) if ((channel < 0) || (channel > 1)) { - fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ; + fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ; exit (1) ; } - if (gertboardSPISetup () == -1) + if (gertboardAnalogSetup (64) < 0) { fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ; exit (1) ; } - printf ("%d\n",gertboardAnalogRead (channel)) ; + printf ("%d\n", analogRead (64 + channel)) ; } - /* * doWrite: * gpio write pin value @@ -687,9 +804,6 @@ static void doWrite (int argc, char *argv []) pin = atoi (argv [2]) ; - if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) - return ; - /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0)) val = 1 ; else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0)) @@ -703,6 +817,31 @@ static void doWrite (int argc, char *argv []) digitalWrite (pin, HIGH) ; } + +/* + * doAwriterite: + * gpio awrite pin value + ********************************************************************************* + */ + +static void doAwrite (int argc, char *argv []) +{ + int pin, val ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + val = atoi (argv [3]) ; + + analogWrite (pin, val) ; +} + + /* * doWriteByte: * gpio write value @@ -742,19 +881,57 @@ void doRead (int argc, char *argv []) } pin = atoi (argv [2]) ; - - if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) - { - printf ("0\n") ; - return ; - } - val = digitalRead (pin) ; printf ("%s\n", val == 0 ? "0" : "1") ; } +/* + * doAread: + * Read an analog pin and return the value + ********************************************************************************* + */ + +void doAread (int argc, char *argv []) +{ + int pin, val ; + + if (argc != 3) + { + fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + val = analogRead (pin) ; + + printf ("%s\n", val == 0 ? "0" : "1") ; +} + + +/* + * doToggle: + * Toggle an IO pin + ********************************************************************************* + */ + +void doToggle (int argc, char *argv []) +{ + int pin ; + + if (argc != 3) + { + fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + + digitalWrite (pin, !digitalRead (pin)) ; +} + /* * doClock: * Output a clock on a pin @@ -773,9 +950,6 @@ void doClock (int argc, char *argv []) pin = atoi (argv [2]) ; - if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) - return ; - freq = atoi (argv [3]) ; gpioClockSet (pin, freq) ; @@ -800,9 +974,6 @@ void doPwm (int argc, char *argv []) pin = atoi (argv [2]) ; - if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS))) - return ; - val = atoi (argv [3]) ; pwmWrite (pin, val) ; @@ -885,16 +1056,34 @@ int main (int argc, char *argv []) return 1 ; } +// Help + if (strcasecmp (argv [1], "-h") == 0) { printf ("%s: %s\n", argv [0], usage) ; return 0 ; } - if (strcasecmp (argv [1], "-v") == 0) +// Sort of a special: + + if (strcmp (argv [1], "-R") == 0) + { + printf ("%d\n", piBoardRev ()) ; + return 0 ; + } + +// Version & Warranty + + if (strcmp (argv [1], "-V") == 0) + { + printf ("%d\n", piBoardRev ()) ; + return 0 ; + } + + if (strcmp (argv [1], "-v") == 0) { printf ("gpio version: %s\n", VERSION) ; - printf ("Copyright (c) 2012 Gordon Henderson\n") ; + printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ; printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ; printf ("For details type: %s -warranty\n", argv [0]) ; printf ("\n") ; @@ -905,7 +1094,7 @@ int main (int argc, char *argv []) if (strcasecmp (argv [1], "-warranty") == 0) { printf ("gpio version: %s\n", VERSION) ; - printf ("Copyright (c) 2012 Gordon Henderson\n") ; + printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ; printf ("\n") ; printf (" This program is free software; you can redistribute it and/or modify\n") ; printf (" it under the terms of the GNU Leser General Public License as published\n") ; @@ -934,8 +1123,8 @@ int main (int argc, char *argv []) /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; } else if (strcasecmp (argv [1], "export" ) == 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 ; } + else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argv [0]) ; return 0 ; } // Check for load command: @@ -948,13 +1137,9 @@ int main (int argc, char *argv []) // Check for -g argument - if (strcasecmp (argv [1], "-g") == 0) + /**/ if (strcasecmp (argv [1], "-g") == 0) { - if (wiringPiSetupGpio () == -1) - { - fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ; - exit (1) ; - } + wiringPiSetupGpio () ; for (i = 2 ; i < argc ; ++i) argv [i - 1] = argv [i] ; @@ -962,15 +1147,23 @@ int main (int argc, char *argv []) wpMode = WPI_MODE_GPIO ; } +// Check for -1 argument + + else if (strcasecmp (argv [1], "-1") == 0) + { + wiringPiSetupPhys () ; + + for (i = 2 ; i < argc ; ++i) + argv [i - 1] = argv [i] ; + --argc ; + wpMode = WPI_MODE_PHYS ; + } + // 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) ; - } + piFaceSetup (200) ; for (i = 2 ; i < argc ; ++i) argv [i - 1] = argv [i] ; @@ -982,38 +1175,65 @@ int main (int argc, char *argv []) else { - if (wiringPiSetup () == -1) - { - fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ; - exit (1) ; - } + wiringPiSetup () ; wpMode = WPI_MODE_PINS ; } -// Check for PWM or Pad Drive operations +// Check for -x argument to load in a new extension - if (wpMode != WPI_MODE_PIFACE) + if (strcasecmp (argv [1], "-x") == 0) { - if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; } - if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; } - if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; } - if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; } - if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; } + if (argc < 3) + { + fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ; + exit (EXIT_FAILURE) ; + } + + if (!doExtension (argv [0], argv [2])) // Prints its own error messages + exit (EXIT_FAILURE) ; + + for (i = 3 ; i < argc ; ++i) + argv [i - 2] = argv [i] ; + argc -= 2 ; } -// Check for wiring commands + if (argc <= 1) + { + fprintf (stderr, "%s: no command given\n", argv [0]) ; + exit (EXIT_FAILURE) ; + } - /**/ if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; - else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ; - else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ; - else if (strcasecmp (argv [1], "wb") == 0) doWriteByte (argc, argv) ; - else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ; - else if (strcasecmp (argv [1], "clock") == 0) doClock (argc, argv) ; - else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ; +// Core wiringPi functions + + /**/ if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ; + else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ; + else if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ; + else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ; + else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ; + else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ; + +// GPIO Nicies + + else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ; + +// Pi Specifics + + else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ; + else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ; + else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ; + else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ; + else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; + else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; + else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ; + else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ; + else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ; + else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ; + else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ; + else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ; else { fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ; - exit (1) ; + exit (EXIT_FAILURE) ; } return 0 ; } diff --git a/gpio/pintest b/gpio/pintest new file mode 100755 index 0000000..83ca12a --- /dev/null +++ b/gpio/pintest @@ -0,0 +1,193 @@ +#!/bin/bash +# +# pintest +# Test the Pi's GPIO port +# Copyright (c) 2013 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# + + +# logErr pin, expected got +################################################################################ + +logErr () +{ + if [ $errs = 0 ]; then + echo "" + fi + echo " --> Pin $1 failure. Expected $2, got $3" + let errs+=1 +} + + +# printErrorCount +################################################################################ + +printErrCount() +{ + if [ $errs = 0 ]; then + echo "No faults detected." + elif [ $errs = 1 ]; then + echo "One fault detected." + else + echo "$errs faults detected" + fi +} + + +# testPins start end +################################################################################ + +testPins() +{ + start=$1 + end=$2 + errs=0 + + printf "%30s %2d:%2d: " "$3" $1 $2 + +# Set range to inputs + + for i in `seq $start $end`; do + gpio mode $i in + done + +# Enable internal pull-ups and expect to read high + + for i in `seq $start $end`; do + gpio mode $i up + if [ `gpio read $i` = 0 ]; then + logErr $i 1 0 + fi + done + +# Enable internal pull-downs and expect to read low + + for i in `seq $start $end`; do + gpio mode $i down + if [ `gpio read $i` = 1 ]; then + echo "Pin $i failure - expected 0, got 1" + let errs+=1 + fi + done + +# Remove the internal pull up/downs + + for i in `seq $start $end`; do + gpio mode $i tri + done + + if [ $errs = 0 ]; then + echo " OK" + else + printErrCount + fi + + let totErrs+=errs +} + + +intro() +{ + revision=`gpio -V` + cat <|% z*#n}Y0vO}TZgh7j285UcdPEW(PlW<{Bpe;DgLk30;?>k3G&&WJ^MV8<*WI#zAX3VE zE{_@*QrC|^a!e(%T5@JylXu>bWHlnu$mZl?UKGQuy>&wk9@W0?VRh*IEt^#Hh&z3U zf+T&_+tNE;I*N`OijJ;5S)Fa^@2y%!i#SwO9PA8NUW+UIPFwD{b$0`4HT#^B(#G*o zGgk(i&E2B@#G=DqQek44`&k~bZ0Yk_Q=fYD>5b)e6{3Rh$+gw*+w7ZJj<3cjpZltvFgXAlYnodLHi&PdDTlBufBm9nlBm)& zSK7-g90+^25%iryXgDIABx-+y9*{QWdQblR7~oES^5V8(PZM(Q6Pto2QgDUb5-_+L zTpqGx?ZcLD{@&}9&nv6F4h3QZtGmCR4t#c+<#x-{@B9THB7H}FRt8r3KFr9Pj0{#( zq>gEuDVsWo_4i_DpI_Z>>+nJK)!to~2L~+Lcd-q3$PSOd9Fq$!QN3h!1xrzrjiu5X z*3Wc0oo>j>`KbxmsG}0BHADh>t^yWYI#zqasZVbc?yfcFDJ25-Qf1|-VxSVJ7 zYd2-SPv(T7L_a&~3@knJq%oqtiNz&3`;y4kLrTY*B=)9|DatW*ttJG^BsN5n+9Rc; zjz&ul^i6buj9LEI@hlBw$Mu9H9yeXVYZb=WJ!uOK&EJ{Q7d;+j^_NH9MmgS%bzccx z?)=CRFm=`B24UCZ+QfVI=4z&xy6VGER=l3wP6_wLjv9S^M8~GyNJ{V!?j7cn#=knA z+4D%|oP)Hp(83_saO10I8lOa9YH5`O-oU^xso6u>9#|jhV0+{A1(de`jr56=t0%{0 z2;xhtb=2wb4%m*2!G`LAWaN+1NG38 zMQoJ{{U0 zvSQnWzimwUFP{s0-YWm~-Lj`0A71NjR-`zq>Gr!-;?FI$x(QK6`7BDMa907Nt}^@Z zD<$yj9$t#9XO4vL+R+M+i%2Wf=gVU?P)K?$=EJ9_0IT0|h&k$5;z-p1@;9M`wAsY}54i-54~7L8(`v?P(P0K0wX(Q>kb^}+|A=Dv*x zo{zs%O~~_FdlP^ANImDZHyf~+haVat%|eH*<=)_zD^Cb#t)QKD%T`+3#9|#$e1=*N zqG(g--N$61T|Y0&dc9E#vj5lt7xvivMD8nJSZ~{L80wCENM6N@FuVqNqU$nz zpmcd9L$&WpkAX$VF1uyAbI^qv!m|)`P^eXD@}djJk^3CPC>X;>UG^Qh@3YqF>ELpC zE0Le4)r`gTGV_qTqU{TUFCcb!Y_hwC+D#3|63VupTX8m$H`FnIKuVzyUP+3FM8Rbv z;Lwcw_%hZMh^;W)7x2UPP5Xx=yQD{2;hw| zPr@+tu>zBj8UV4e{mL%B-ICXwc+oT=`ym&fc2RcxV~jz2=*}_p?!}sS>un$0m4y~r zipsStVrx(JDX&K#C~=?G6?bDzs6U(A8XzSdm?HwwdPA7lDwJ$ro$tbVtTmqd#x7uu?C$*oMx>NV+H6n;|i$TegEai8tnVAfjwvp^LE z{nvt@ZHE;(U$}*YTa|vf)!RUs5^$zXdost>CN>Q=tX1!Koj%0E(<%$im=jLsf8PCd z+VRHZuwxY(#@EdvlyviBhwMOIs0&aQYQ=2l9Rv{_06jDL_Ik0)_E^OuILBV(r&Ik? z*^diT>vLQLnbZ@DimYqvc^y0*r7!V3-4Wd|?G!%0_k5GI1;>5fl`yfy7I$W#zGcSH zt;?wa(eN&DO_y)L&4md;lG|qUjh!iCYlyXh3#XF8YA@QL+`3ZEK<}X27Ve@TXxq%h zn6po;U#2zMvo6hmZ#y|TM2`RH1^a{ik9<-ODmqvjbqqy(cz9e}-+XfXfso-RH?5>u z{`p$YHw_N2MV46P8moHRNMj+gV=qSsZ;~5WM#A<_I6mhJKwjzcR18gZ{8afuWa0d7 z<~ZxOdf)Bd6fG&RjPpQ?qVTT`m6TypcUBZ&<8SNGPU-3^(^boHp{pb_&iEg zu_f6CyWvGOD(@+u5ck(?a9@uMY*Y@-%}ZzU~?tBUS8yO zaC^v$QoSQb;VSr!*G8Y6Q=C66yk!hjEqE4@; zq$>PRdg^lZwSA(e4CMh)vfIX=Un%pN8jpzdD?^%=`)F`SH+V6J7sn!u6NVl<+W}{f z;O(vvNo-PK1_;eFjf5H}H$RcU^qX$}7>16X%i{XiudHi9F1G zbDuNjntM7$lw%FIsFBgv)*{*C)GoG8=HI{VJH}M~ox5~x;cjBDl39WF{vsf>hn zUZ%@<{+9?_)fX4*YiDxb8l30c;vJ32L`vP{7M~!9-qWkdaJkxM{5nfJ^?)+@qFDl| z@N;W__mJ4Tr7xEqvU11q=9etOiSKfQ$7@ZYMaTEd-;}^jbWRYc4y{Fxqg#{(GSkw1 z%ugLKposC}$7hbUS{p?6mQ=pdN%IC0rf5HddE3Yt$LIN(>Q)30Utmm+57P2R`g^=8g-fek3Qo0xvxBn`y;rmqT(HA{Is#|fd zFT@)0((hiMsGmJ&jSN;)&W}tvJ7&#W#ByW(fqgj@)tyf16V}yW3nxsnK5X)~A9|7S zR_fY0US5d|UU5O$;X4x}Tu)nNKjIcMoX3i+mA?dhlK!50BRS_nzgDZC+_}uyNSSrg zn6vja)@0j?_U5|r$Mgys(mMC)$-LGeIXxJC_K|=@Oe1{$dfUhLr@nYy0$Iu`r=7+u5Ji^68O)Vv;tyXFM#U?{49(%SfYh>toIr%+Adb?kw6x zieCtMNkf(&sqvk#z|o@xKOi5Uzzkd?)F0pzd;G$udhe%*^Jp1-<%#I4M!RTtPrMQd zW?s8@-c%R$go!3vJPg+z8KXXLnKjv~`dnXU_pIcz%IKn36&&6x#+Z+Tun%ySgymOA z&$LU0nqU2wiI>`quJL4>$ho;skl**eI(bXF*sGT7l)c!8Tiw^Z15bu52s9O2AZCqc z&6n<+KkDPR)Nm}#rm>9^dPX>88}kOyW@qZ7jO7X zKiQajR)h+)jHZhoJTDh$wnlmk-KZo#<67dl5N24*0qdWA&zTg2Dex^YHR{w&`@}jh z?Fg(~wQ`)mz8Pt2{U!-q@wvYnzwOm(5)6%#>(CbQtg(K)Y;)V|I=abmPXgAFWkh5u zV6PL~l)~QrrMJgtreFHA9hT2}e`MXTfm}Uw&1tWDFx!Z{*6Gd2&i%|E*E{${{@K(X zKG8n2D%&ago}8;5Izf=OjYfF2jhz?AgNKXNQYm9A^ZUbOPw-_e<*F~0j8@&8@L6c= zroZwW343A|J!bd$?OmI^dJcEzMd}scxiiN$Zn5ca+dJB>aG}*^{hg!}2yzlphwoO8 zSDbp2hO)*_R6v)DLg@b z<>Q>?^?_+VsnS`n#1kp{1-nFIZclw15+mOzWxcEWJ@9ISy<_``71up#Dc}5=>cb=2 zb>}c&T|H(6x=MP>u4 z6;X_TVCFS{5dzavEZjdrt18t#)5OA(`~JYJu3n)BcF}yP{qS_s?pyP~sPl(zTR>_A ze<4(`hX0M1tl_qTg}aGORq`vsYgftg34UcORa~qfLyf$g64l-1r*$uO#qE*MBt9Tn z3Hi!t)*GxECSDbK^2j1AV8U{+l-mzwk2QmT9Lzy|QsqCaPgk(;>fyCNbnA3ljHA(| zYi1Ue0{T;uFZc>=lijktQlLUnYy9{EmFDO>`%!{+Z<5$0KYI*>HIvo~UcL@oD49@3 z^a^Lu<>EBiY&GgiJwL@kc6}&S?d9%#D(NNPo8iU2CqSWGlc`kZ!`E=`10yiXx4E8g z;oMR}2lsrsNX@z!fb^-o+!kN8|7=}e#=d9eGwB`^*6{1m%GjWd+18^?BeUr8I)`?V z;FX5U*SM1e$hmxm8e0+URkT&|*?j!hAdZ)ZO6t{i@1_hkK(# z7OByqu>jP0g$p%a2kjAMiWHsd@^(3v8To>V&nuZe%8gO$H93ad62*C`eD0j!7w27l zXpwRFrFm5cVea(XJCob=hquJ|9Vt;j^L6gnncu!I*O7bHNG5jw=l4nz(E$NM-Z8fd z-@cJiW-83z_x!4gR-gAVT#u}%@xsKy3v=qrn0V2f<`Z1O#RQEvrRmN#98ll9XviB= zj0dHlCstA5iHW&$GObe-#@No@}u`5 zn|(wJX7@g&6|M~X_s>OLxiWgoViVpIqA%?XU(7m+8~4k~7#ht9a6M_G?J>Cx;kO*2 zm==wi`N$1O9XQBWOHN*cw41Vr1(%;GWiD-Gsws}FX;T^l2I8_iTIbrwOb6JH&qJnX zot(>yu6;gIDWEzfN#`rXYxt3z?#*6QHhZ0w_wYU@=YrlS_m_;LY?VR*rYDQT<-5WM zTPlT*Tc3;7cO|iMVbs>V{D&@~o5$no1!V_1x@Z+x#r>U=B|go9g84^tB!sY0b(iJs zE0tb#9p*b)Y1AL+=+~y%f9!HVS0JQl!CKayFseO2mm@rFLOOk~xs!*)`DLY|W+0@R zEodg0M^E{oS8(-Y>%Gn~n`{I*uenU+!$fa|3b1;ek`~h~Y05=`nZ8U>%2kzb?HsL0 z`EKC3AZvSHvRKgYV|z|%Da8x_UFyLLC6CnzYum-rvnxk2Q;yg7+}YmLq&4|?EmlW; ze3i;5@Hq4JD=9|*?N zD85t|JPqswno%h(7I?ZX0QL%@fF(YF4$|oc(Df}gNs*`@BzhE(9QlJDYz_k2;62EZ z24yrA%^@J@r+f^QA5BJ(8z>-jQ$iXPI81b_Q!gXJHj$ls(tDYbEQ z9Esxgi^WgrP4IM_D>ws1ZniJ~V4(P~4F1{%1UO=50!6B+{S9|I+b(%&fl5M2vL!!xu67~n~McskJqx49dD0RuHl2HUGd zvKth+sW`yG*O|^RBSTf7e804~Ir~$DO*8+Z19XT40v*)j6`Aq zE21mion{MFK%yDH5C-Yre;9MWVgDB6_be3gAI|^9^XJN&68zxv2L$p1@6W$rBp}fY zZ~m!sa0_HjGL88A{(n(E!?=G}<8NcawJ0RlKQ;EhFxgb?pB7};|1Z^VTEZ0Pg$Mp= zAT2PGZQ)2L9EHL{6%dM03>IzA=+3**pz`t%K$GUe2xbrp#v#tz08eyt|9Q(;fHB7) zg#d^o;0@b-_>yXTG>M@{D6z%<+i@} zBt3BfjVAE1!fjc5CMIl0*4f?pC1=$t`^_&&4BCIwb~v`}tj%6F-lVkd{nsDmEIx+Q zwNKSvCuu)0xoA3+I6iyjLt=jDh34rPe51+C`Z~3F>{LtRvK@=T7A8%e`L5dGVKz>E zwXsVNw)*&meod@BK7UmV|`grW=eXKf8U@@io;lb82+ zo`RiMmniuo>e|_6D{J4%<9FJpI=!(sL@#wn?JKm-#<=ft`Bc;3P$U)JUE`^wn?^i; zPM(D+EK*9o!B!qss{Fk5^H!$La4x5P1}rz99>33*&phV@x8@+6aYF27{U*$A{xrPh zB31`GM+=p7(qPGqpyz$LD63VbN!YW4NR8%sSDT1 z*0-P4#;;VQzU^Y4>~8Tsy;{4iY2V~~C;bMw>n&O70rT&^%kPO$uDbHT?=e4eZYEt&I$-X(zq2LrtS)H}y{fla z>~{5==AAx3aO32n{Z428h`PFac0~(ISOQ6Ho#jl;De1vPR(yIC`UWas(a)EQZYiIT zq(5C6>p^rUsil(*Y|_#DbGzE+n_IP&2{WwUq-Gqy*9?g4+!y$*+km|FNrnDp*?GmQ zaMQG-?K(Wst7q0&%UmbKDK2U0!z-h18HdVmY@cnjulSs^uBb@rbkjV{J!D-dJ1oAa zrt(f1RZuL2mgSK{T1k+XXtt^$If1MPB=xb=A-L1+A-W17dO@-PZ; zS|Qeq4#8+C=1*s(2oIdwG14!DWYT=-bN@O3Gy^WOjbaHi=PWI!I_?06VnwRn_LqF{ ziM`k7I2ejgn79=!WS}7CQ|g$++4gzfHQzci{M_grjW44Y^|{jf?+3p1C>+q8a7g=b z{>rIrz%X{+^RWgSAd;JZms&itRuBvm_w^rG6+J?9Gu`w4_=gh{cXJwNye&2EHV>)z zxl*jIz)S5435#&$>}}?8dCvW%mY&gs;zv>y%={W&>5Vp5N%zTNhjJuicCJa?ocnrH z6U|W=7DPBCF>$p$<<+&5C##iC>t$Mo|Y!}m> zT<2D;?l}dW_9r9>^XR}&lFR%X8+Z|g-LjKpd~HmrxZzTt3K%d&{C$3yvY>lDR;!PV0cLEiJO z22rl8=Hf}L!7$UTir4q*WusrAN~V=NJ9_$`4dk_2&D5hB9#j@et!zm|b1jfRUQyDQ zAPa`vD#*W=f40=p!lAD}`D0}biv9G4TUb@C1IuB;D4M_gMl#p=yrk4HW#kg{goD1b z?LE8eQ#Wgj6MKx$#78{Ua2)VtT4VFt4iquUjDIaPH*2XsA6LJstiO`5wTbZ`8CdCe z23AoI@W#1#;^|Olyc?0M3Y)EX0)rA=Rbf^NCI}O6ZM-|tAc%^$2r{*F3G#Hoy28}e zAS&uOu*Sv1190AO!sZKT8WenZsY+;@8|z6h5CW6~yu7`@VhINXOSS)}*0Vot4#DLQR$#h%*oJ`vXuD2-&(fT(T znI^X>lbj313m{qpNI3-XYoT8cdm@DlWh{(yrufoTVa_hDNCaL%3GVEQz`&76yek|5 zUT~Z<##PY;O~7DX@xNB`{zJllSqhv0*OCWS0FIQw8s62{1y5Cl>6;lt_1!5n`i~M1 zswAg~gaJQy{$CIXw8+2TJ7~tAn^ysGluf`Qmk_7}9zN7i2&6n50e;YyNQ^QXp^QYI zPyzmi1X>%U(gweA0Dc4E|EBr_=?^Lj)e6aw8JJQDqoW0T`g1}Xr2k~X8l)dj$l@C=D)prMhVoUSg)1Pav)2l_P5+Z!x2 zadgnt0ni~~YAWC)ok%C))iz^JlSKb9AQ2fcq)ft*-Be)#a92D5=S!l))VztHdxOUN zp?(rk?Z;q~4)}}LzcJ1x2#jgaPQdSsp$38em#+UFqrcK5n0!@X{){B}k2K6UZU4-| zzk*u&-^H{HSco%%1yndAT%>iB8DWNyN1%}yGy;q;1Of(rKr}EC{~wH>?k~L=J9g5Z zb~4caG=4z-mJBOP|FWOV?;~~q6;A+9ct}MEBfI^$pcn-Oc?Bo|`V*#r0y`C(Q&93R z7#1lHp1&B^UoZufJb2Xq8w{cN4;TW$I5+=~^V8XZTaZyFaXk48WwC3TPJLH-B9#x3Fi literal 0 HcmV?d00001 diff --git a/pins/pins.tex b/pins/pins.tex new file mode 100644 index 0000000..c3753e9 --- /dev/null +++ b/pins/pins.tex @@ -0,0 +1,116 @@ +\documentclass[12pt,a4paper]{article} +\parskip 1ex +\parindent 0em +\thispagestyle{empty} +\pagestyle{plain} +\pagenumbering{arabic} +\setlength{\topmargin}{0pt} +\setlength{\headheight}{0pt} +\setlength{\headsep}{0pt} +\setlength{\topskip}{0pt} +\setlength{\textheight}{240mm} +\setlength{\footskip}{5ex} +\setlength{\oddsidemargin}{0pt} +\setlength{\evensidemargin}{0pt} +\setlength{\textwidth}{160mm} +\usepackage[dvips]{graphics,color} +\usepackage{helvet} +\renewcommand{\familydefault}{\sfdefault} +\begin{document} +\begin{sffamily} +\definecolor{rtb-black}{rgb} {0.0, 0.0, 0.0} +\definecolor{rtb-navy}{rgb} {0.0, 0.0, 0.5} +\definecolor{rtb-green}{rgb} {0.0, 0.5, 0.0} +\definecolor{rtb-teal}{rgb} {0.0, 0.5, 0.5} +\definecolor{rtb-maroon}{rgb} {0.5, 0.0, 0.0} +\definecolor{rtb-purple}{rgb} {0.5, 0.0, 0.5} +\definecolor{rtb-olive}{rgb} {0.5, 0.5, 0.0} +\definecolor{rtb-silver}{rgb} {0.7, 0.7, 0.7} +\definecolor{rtb-grey}{rgb} {0.5, 0.5, 0.5} +\definecolor{rtb-blue}{rgb} {0.0, 0.0, 1.0} +\definecolor{rtb-lime}{rgb} {0.0, 1.0, 0.0} +\definecolor{rtb-aqua}{rgb} {0.0, 1.0, 1.0} +\definecolor{rtb-red}{rgb} {1.0, 0.0, 0.0} +\definecolor{rtb-fuchsia}{rgb}{1.0, 0.0, 1.0} +\definecolor{rtb-yellow}{rgb} {1.0, 1.0, 0.0} +\definecolor{rtb-white}{rgb} {1.0, 1.0, 1.0} + +\begin{center} +\bfseries{WiringPi: GPIO Pin Numbering Tables}\\ +\tt{http://wiringpi.com/} +\end{center} + +\begin{center} +\begin{tabular}{|c|c|c||p{8mm}|p{8mm}||c|c|c|c|} +\hline +\multicolumn{8}{|c|}{\bfseries{P1: The Main GPIO connector}}\\ +\hline +\hline +WiringPi Pin & BCM GPIO & Name & \multicolumn{2}{|c||}{Header} & Name & BCM GPIO & WiringPi Pin\\ +\hline +\hline + & & \textcolor{rtb-red}{3.3v} & \raggedleft{1} & 2 & \textcolor{rtb-maroon}{5v} & & \\ +\hline +8 & Rv1:0 - Rv2:2 & \textcolor{rtb-aqua}{SDA} & \raggedleft{3} & 4 & \textcolor{rtb-maroon}{5v} & & \\ +\hline +9 & Rv1:1 - Rv2:3 & \textcolor{rtb-aqua}{SCL} & \raggedleft{5} & 6 & \textcolor{rtb-black}{0v} & & \\ +\hline +7 & 4 & \textcolor{rtb-green}{GPIO7} & \raggedleft{7} & 8 & \textcolor{rtb-yellow}{TxD} & 14 & 15\\ +\hline + & & \textcolor{rtb-black}{0v} & \raggedleft{9} & 10 & \textcolor{rtb-yellow}{RxD} & 15 & 16\\ +\hline +0 & 17 & \textcolor{rtb-green}{GPIO0} & \raggedleft{11} & 12 & \textcolor{rtb-green}{GPIO1} & 18 & 1\\ +\hline +2 & Rv1:21 - Rv2:27 & \textcolor{rtb-green}{GPIO2} & \raggedleft{13} & 14 & \textcolor{rtb-black}{0v} & & \\ +\hline +3 & 22 & \textcolor{rtb-green}{GPIO3} & \raggedleft{15} & 16 & \textcolor{rtb-green}{GPIO4} & 23 & 4\\ +\hline + & & \textcolor{rtb-red}{3.3v} & \raggedleft{17} & 18 & \textcolor{rtb-green}{GPIO5} & 24 & 5\\ +\hline +12 & 10 & \textcolor{rtb-teal}{MOSI} & \raggedleft{19} & 20 & \textcolor{rtb-black}{0v} & & \\ +\hline +13 & 9 & \textcolor{rtb-teal}{MISO} & \raggedleft{21} & 22 & \textcolor{rtb-green}{GPIO6} & 25 & 6\\ +\hline +14 & 11 & \textcolor{rtb-teal}{SCLK} & \raggedleft{23} & 24 & \textcolor{rtb-teal}{CE0} & 8 & 10\\ +\hline + & & \textcolor{rtb-black}{0v} & \raggedleft{25} & 26 & \textcolor{rtb-teal}{CE1} & 7 & 11\\ +\hline +\hline +WiringPi Pin & BCM GPIO & Name & \multicolumn{2}{|c||}{Header} & Name & BCM GPIO & WiringPi Pin\\ +\hline +\end{tabular} +\end{center} + +Note the differences between Revision 1 and Revision 2 Raspberry +Pi's. The Revision 2 is readily identifiable by the presence of the 2 +mounting holes. + +The revision 2 Raspberry Pi has an additional GPIO connector, P5, which is next to the main P1 GPIO +connector: + +\begin{center} +\begin{tabular}{|c|c|c||p{8mm}|p{8mm}||c|c|c|c|} +\hline +\multicolumn{8}{|c|}{\bfseries{P5: Secondary GPIO connector (Rev. 2 Pi only)}}\\ +\hline +\hline +WiringPi Pin & BCM GPIO & Name & \multicolumn{2}{|c||}{Header} & Name & BCM GPIO & WiringPi Pin\\ +\hline +\hline + & & \textcolor{rtb-maroon}{5v} & \raggedleft{1} & 2 & \textcolor{rtb-red}{3.3v} & & \\ +\hline +17 & 28 & \textcolor{rtb-green}{GPIO8} & \raggedleft{3} & 4 & \textcolor{rtb-green}{GPIO9} & 29 & 18 \\ +\hline +19 & 30 & \textcolor{rtb-green}{GPIO10} & \raggedleft{5} & 6 & \textcolor{rtb-green}{GPIO11} & 31 & 20 \\ +\hline + & & \textcolor{rtb-black}{0v} & \raggedleft{7} & 8 & \textcolor{rtb-black}{0v} & & \\ +\hline +\hline +WiringPi Pin & BCM GPIO & Name & \multicolumn{2}{|c||}{Header} & Name & BCM GPIO & WiringPi Pin\\ +\hline +\end{tabular} +\end{center} + + +\end{sffamily} +\end{document} diff --git a/wiringPi/Makefile b/wiringPi/Makefile index c6a4555..8ece853 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -21,7 +21,7 @@ # along with wiringPi. If not, see . ################################################################################# -DYN_VERS_MAJ=1 +DYN_VERS_MAJ=2 DYN_VERS_MIN=0 VERSION=$(DYN_VERS_MAJ).$(DYN_VERS_MIN) @@ -35,26 +35,25 @@ DYNAMIC=libwiringPi.so.$(VERSION) DEBUG = -O2 CC = gcc INCLUDE = -I. -CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe -fPIC +CFLAGS = $(DEBUG) -Wformat=2 -Wall $(INCLUDE) -Winline -pipe -fPIC LIBS = # Should not alter anything below this line ############################################################################### -SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ - gertboard.c \ - piNes.c \ - lcd.c piHiPri.c piThread.c \ - wiringPiSPI.c \ - softPwm.c softServo.c softTone.c - -SRC_I2C = wiringPiI2C.c +SRC = wiringPi.c \ + wiringSerial.c wiringShift.c \ + piHiPri.c piThread.c \ + wiringPiSPI.c wiringPiI2C.c \ + softPwm.c softTone.c \ + mcp23s08.c mcp23008.c \ + mcp23s17.c mcp23017.c sr595.c pcf8574.c \ + mcp3002.c mcp4802.c mcp3422.c \ + drc.c OBJ = $(SRC:.c=.o) -OBJ_I2C = $(SRC_I2C:.c=.o) - all: $(DYNAMIC) static: $(STATIC) @@ -67,11 +66,7 @@ $(STATIC): $(OBJ) $(DYNAMIC): $(OBJ) @echo "[Link (Dynamic)]" - @$(CC) -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ) - -i2c: $(OBJ) $(OBJ_I2C) - @echo "[Link (Dynamic + I2C)]" - @$(CC) -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ) $(OBJ_I2C) + @$(CC) -shared -Wl,-soname,libwiringPi.so -o libwiringPi.so.$(VERSION) -lpthread $(OBJ) .c.o: @echo [Compile] $< @@ -79,37 +74,48 @@ i2c: $(OBJ) $(OBJ_I2C) .PHONEY: clean clean: - rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.* + @echo "[Clean]" + @rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.* .PHONEY: tags tags: $(SRC) @echo [ctags] @ctags $(SRC) -.PHONEY: install -install: $(DYNAMIC) - @echo "[Install]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + +.PHONEY: install-headers +install-headers: + @echo "[Install Headers]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringPi.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringSerial.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringShift.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 gertboard.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 piNes.h $(DESTDIR)$(PREFIX)/include @install -m 0644 softPwm.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 softServo.h $(DESTDIR)$(PREFIX)/include @install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include - @install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib - @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so - @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so.1 + @install -m 0644 mcp23008.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 mcp3002.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 mcp4802.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 mcp3422.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 sr595.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 pcf8574.h $(DESTDIR)$(PREFIX)/include + +.PHONEY: install +install: $(DYNAMIC) install-headers + @echo "[Install Dynamic Lib]" + @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + @install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) + @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so @ldconfig .PHONEY: install-static -install-static: $(STATIC) - @echo "[Install Static]" +install-static: $(STATIC) install-headers + @echo "[Install Static Lib]" + @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib @install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib .PHONEY: uninstall @@ -118,14 +124,19 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/wiringPi.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringSerial.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringShift.h - @rm -f $(DESTDIR)$(PREFIX)/include/gertboard.h - @rm -f $(DESTDIR)$(PREFIX)/include/piNes.h @rm -f $(DESTDIR)$(PREFIX)/include/softPwm.h - @rm -f $(DESTDIR)$(PREFIX)/include/softServo.h @rm -f $(DESTDIR)$(PREFIX)/include/softTone.h - @rm -f $(DESTDIR)$(PREFIX)/include/lcd.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h + @rm -f $(DESTDIR)$(PREFIX)/include/mcp23008.h + @rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h + @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h + @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h + @rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h + @rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h + @rm -f $(DESTDIR)$(PREFIX)/include/mcp3422.h + @rm -f $(DESTDIR)$(PREFIX)/include/sr595.h + @rm -f $(DESTDIR)$(PREFIX)/include/pcf8574.h @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.* @ldconfig @@ -137,16 +148,21 @@ depend: # DO NOT DELETE wiringPi.o: wiringPi.h -wiringPiFace.o: wiringPi.h wiringSerial.o: wiringSerial.h wiringShift.o: wiringPi.h wiringShift.h -gertboard.o: wiringPiSPI.h gertboard.h -piNes.o: wiringPi.h piNes.h -lcd.o: wiringPi.h lcd.h piHiPri.o: wiringPi.h piThread.o: wiringPi.h -wiringPiSPI.o: wiringPiSPI.h -softPwm.o: wiringPi.h softPwm.h -softServo.o: wiringPi.h softServo.h -softTone.o: wiringPi.h softTone.h +wiringPiSPI.o: wiringPi.h wiringPiSPI.h wiringPiI2C.o: wiringPi.h wiringPiI2C.h +softPwm.o: wiringPi.h softPwm.h +softTone.o: wiringPi.h softTone.h +mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h +mcp23008.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23008.h +mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h +mcp23017.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23017.h +sr595.o: wiringPi.h sr595.h +pcf8574.o: wiringPi.h wiringPiI2C.h pcf8574.h +mcp3002.o: wiringPi.h wiringPiSPI.h mcp3002.h +mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h +mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h +drc.o: wiringPi.h wiringSerial.h drc.h diff --git a/wiringPi/README b/wiringPi/README deleted file mode 100644 index c79754e..0000000 --- a/wiringPi/README +++ /dev/null @@ -1,9 +0,0 @@ - -WiringPi: An implementation of most of the Arduino Wiring - functions for the Raspberry Pi, - along with many more features and libraries to support - hardware, etc. on the Raspberry Pi - -Full details at: - https://projects.drogon.net/raspberry-pi/wiringpi/ - diff --git a/wiringPi/drc.c b/wiringPi/drc.c new file mode 100644 index 0000000..07baf17 --- /dev/null +++ b/wiringPi/drc.c @@ -0,0 +1,203 @@ +/* + * drc.c: + * Extend wiringPi with the DRC control protocll to Arduino + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include + +#include "wiringPi.h" +#include "wiringSerial.h" + +#include "drc.h" + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + + +/* + * myPinMode: + * Change the pin mode on the remote DRC device + ********************************************************************************* + */ + +static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) +{ + /**/ if (mode == OUTPUT) + serialPutchar (node->fd, 'o') ; // Input + else if (mode == PWM_OUTPUT) + serialPutchar (node->fd, 'p') ; // PWM + else + serialPutchar (node->fd, 'i') ; // Default to input + + serialPutchar (node->fd, pin - node->pinBase) ; +} + + +/* + * myPullUpDnControl: + * ATmegas only have pull-up's on of off. No pull-downs. + ********************************************************************************* + */ + +static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode) +{ + +// Force pin into input mode + + serialPutchar (node->fd, 'i' ) ; + serialPutchar (node->fd, pin) ; + + /**/ if (mode == PUD_UP) + { + serialPutchar (node->fd, '1') ; + serialPutchar (node->fd, pin - node->pinBase) ; + } + else if (mode == PUD_OFF) + { + serialPutchar (node->fd, '0') ; + serialPutchar (node->fd, pin - node->pinBase) ; + } +} + + +/* + * myDigitalWrite: + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + serialPutchar (node->fd, value == 0 ? '0' : '1') ; + serialPutchar (node->fd, pin - node->pinBase) ; +} + + +/* + * myPwmWrite: + ********************************************************************************* + */ + +static void myPwmWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + serialPutchar (node->fd, 'v') ; + serialPutchar (node->fd, pin - node->pinBase) ; + serialPutchar (node->fd, value & 0xFF) ; +} + + +/* + * myAnalogRead: + ********************************************************************************* + */ + +static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) +{ + int vHi, vLo ; + + serialPutchar (node->fd, 'a') ; + serialPutchar (node->fd, pin - node->pinBase) ; + vHi = serialGetchar (node->fd) ; + vLo = serialGetchar (node->fd) ; + + return (vHi << 8) | vLo ; +} + + +/* + * myDigitalRead: + ********************************************************************************* + */ + +static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + serialPutchar (node->fd, 'r') ; // Send read command + serialPutchar (node->fd, pin - node->pinBase) ; + return (serialGetchar (node->fd) == '0') ? 0 : 1 ; +} + + +/* + * drcSetup: + * Create a new instance of an DRC GPIO interface. + * Could be a variable nunber of pins here - we might not know in advance + * if it's an ATmega with 14 pins, or something with less or more! + ********************************************************************************* + */ + +int drcSetup (const int pinBase, const int numPins, const char *device) +{ + int fd ; + int ok, tries ; + time_t then ; + struct wiringPiNodeStruct *node ; + + if ((fd = serialOpen (device, 115200)) < 0) + return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ; + + delay (10) ; // May need longer if it's an Uno that reboots on the open... + +// Flush any pending input + + while (serialDataAvail (fd)) + (void)serialGetchar (fd) ; + + ok = FALSE ; + for (tries = 1 ; tries < 5 ; ++tries) + { + serialPutchar (fd, '@') ; + then = time (NULL) + 2 ; + while (time (NULL) < then) + if (serialDataAvail (fd)) + { + if (serialGetchar (fd) == '@') + { + ok = TRUE ; + break ; + } + } + if (ok) + break ; + } + + if (!ok) + { + serialClose (fd) ; + return wiringPiFailure (WPI_FATAL, "Unable to communidate with DRC device") ; + } + + node = wiringPiNewNode (pinBase, numPins) ; + + node->fd = fd ; + node->pinMode = myPinMode ; + node->pullUpDnControl = myPullUpDnControl ; + node->analogRead = myAnalogRead ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->pwmWrite = myPwmWrite ; + + return 0 ; +} diff --git a/wiringPi/drc.h b/wiringPi/drc.h new file mode 100644 index 0000000..c2c4ff3 --- /dev/null +++ b/wiringPi/drc.h @@ -0,0 +1,34 @@ +/* + * drc.h: + * Extend wiringPi with the DRC control protocll to Arduino + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +int drcSetup (const int pinBase, const int numPins, const char *device) ; + + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp23008.c b/wiringPi/mcp23008.c new file mode 100644 index 0000000..d21d237 --- /dev/null +++ b/wiringPi/mcp23008.c @@ -0,0 +1,149 @@ +/* + * mcp23008.c: + * Extend wiringPi with the MCP 23008 I2C GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "wiringPi.h" +#include "wiringPiI2C.h" +#include "mcp23x0817.h" + +#include "mcp23008.h" + + +/* + * myPinMode: + ********************************************************************************* + */ + +static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int mask, old, reg ; + + reg = MCP23x08_IODIR ; + mask = 1 << (pin - node->pinBase) ; + old = wiringPiI2CReadReg8 (node->fd, reg) ; + + if (mode == OUTPUT) + old &= (~mask) ; + else + old |= mask ; + + wiringPiI2CWriteReg8 (node->fd, reg, old) ; +} + + +/* + * myPullUpDnControl: + ********************************************************************************* + */ + +static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int mask, old, reg ; + + reg = MCP23x08_GPPU ; + mask = 1 << (pin - node->pinBase) ; + + old = wiringPiI2CReadReg8 (node->fd, reg) ; + + if (mode == PUD_UP) + old |= mask ; + else + old &= (~mask) ; + + wiringPiI2CWriteReg8 (node->fd, reg, old) ; +} + + +/* + * myDigitalWrite: + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + int bit, old ; + + bit = 1 << ((pin - node->pinBase) & 7) ; + + old = node->data2 ; + if (value == LOW) + old &= (~bit) ; + else + old |= bit ; + + wiringPiI2CWriteReg8 (node->fd, MCP23x08_GPIO, old) ; + node->data2 = old ; +} + + +/* + * myDigitalRead: + ********************************************************************************* + */ + +static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + int mask, value ; + + mask = 1 << ((pin - node->pinBase) & 7) ; + value = wiringPiI2CReadReg8 (node->fd, MCP23x08_GPIO) ; + + if ((value & mask) == 0) + return LOW ; + else + return HIGH ; +} + + +/* + * mcp23008Setup: + * Create a new instance of an MCP23008 I2C GPIO interface. We know it + * has 8 pins, so all we need to know here is the I2C address and the + * user-defined pin base. + ********************************************************************************* + */ + +int mcp23008Setup (const int pinBase, const int i2cAddress) +{ + int fd ; + struct wiringPiNodeStruct *node ; + + if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) + return fd ; + + wiringPiI2CWriteReg8 (fd, MCP23x08_IOCON, IOCON_INIT) ; + + node = wiringPiNewNode (pinBase, 8) ; + + node->fd = fd ; + node->pinMode = myPinMode ; + node->pullUpDnControl = myPullUpDnControl ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->data2 = wiringPiI2CReadReg8 (fd, MCP23x08_OLAT) ; + + return 0 ; +} diff --git a/wiringPi/mcp23008.h b/wiringPi/mcp23008.h new file mode 100644 index 0000000..e9299a8 --- /dev/null +++ b/wiringPi/mcp23008.h @@ -0,0 +1,33 @@ +/* + * 23008.h: + * Extend wiringPi with the MCP 23008 I2C GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp23008Setup (const int pinBase, const int i2cAddress) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp23017.c b/wiringPi/mcp23017.c new file mode 100644 index 0000000..5174195 --- /dev/null +++ b/wiringPi/mcp23017.c @@ -0,0 +1,195 @@ +/* + * mcp23017.c: + * Extend wiringPi with the MCP 23017 I2C GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "wiringPi.h" +#include "wiringPiI2C.h" +#include "mcp23x0817.h" + +#include "mcp23017.h" + + +/* + * myPinMode: + ********************************************************************************* + */ + +static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int mask, old, reg ; + + pin -= node->pinBase ; + + if (pin < 8) // Bank A + reg = MCP23x17_IODIRA ; + else + { + reg = MCP23x17_IODIRB ; + pin &= 0x07 ; + } + + mask = 1 << pin ; + old = wiringPiI2CReadReg8 (node->fd, reg) ; + + if (mode == OUTPUT) + old &= (~mask) ; + else + old |= mask ; + + wiringPiI2CWriteReg8 (node->fd, reg, old) ; +} + + +/* + * myPullUpDnControl: + ********************************************************************************* + */ + +static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int mask, old, reg ; + + pin -= node->pinBase ; + + if (pin < 8) // Bank A + reg = MCP23x17_GPPUA ; + else + { + reg = MCP23x17_GPPUB ; + pin &= 0x07 ; + } + + mask = 1 << pin ; + old = wiringPiI2CReadReg8 (node->fd, reg) ; + + if (mode == PUD_UP) + old |= mask ; + else + old &= (~mask) ; + + wiringPiI2CWriteReg8 (node->fd, reg, old) ; +} + + +/* + * myDigitalWrite: + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + int bit, old ; + + pin -= node->pinBase ; // Pin now 0-15 + + bit = 1 << (pin & 7) ; + + if (pin < 8) // Bank A + { + old = node->data2 ; + + if (value == LOW) + old &= (~bit) ; + else + old |= bit ; + + wiringPiI2CWriteReg8 (node->fd, MCP23x17_GPIOA, old) ; + node->data2 = old ; + } + else // Bank B + { + old = node->data3 ; + + if (value == LOW) + old &= (~bit) ; + else + old |= bit ; + + wiringPiI2CWriteReg8 (node->fd, MCP23x17_GPIOB, old) ; + node->data3 = old ; + } +} + + +/* + * myDigitalRead: + ********************************************************************************* + */ + +static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + int mask, value, gpio ; + + pin -= node->pinBase ; + + if (pin < 8) // Bank A + gpio = MCP23x17_GPIOA ; + else + { + gpio = MCP23x17_GPIOB ; + pin &= 0x07 ; + } + + mask = 1 << pin ; + value = wiringPiI2CReadReg8 (node->fd, gpio) ; + + if ((value & mask) == 0) + return LOW ; + else + return HIGH ; +} + + +/* + * mcp23017Setup: + * Create a new instance of an MCP23017 I2C GPIO interface. We know it + * has 16 pins, so all we need to know here is the I2C address and the + * user-defined pin base. + ********************************************************************************* + */ + +int mcp23017Setup (const int pinBase, const int i2cAddress) +{ + int fd ; + struct wiringPiNodeStruct *node ; + + if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) + return fd ; + + wiringPiI2CWriteReg8 (fd, MCP23x17_IOCON, IOCON_INIT) ; + + node = wiringPiNewNode (pinBase, 16) ; + + node->fd = fd ; + node->pinMode = myPinMode ; + node->pullUpDnControl = myPullUpDnControl ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->data2 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATA) ; + node->data3 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATB) ; + + return 0 ; +} diff --git a/wiringPi/mcp23017.h b/wiringPi/mcp23017.h new file mode 100644 index 0000000..79b4d7b --- /dev/null +++ b/wiringPi/mcp23017.h @@ -0,0 +1,33 @@ +/* + * 23017.h: + * Extend wiringPi with the MCP 23017 I2C GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp23017Setup (const int pinBase, const int i2cAddress) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp23s08.c b/wiringPi/mcp23s08.c new file mode 100644 index 0000000..d0acb5e --- /dev/null +++ b/wiringPi/mcp23s08.c @@ -0,0 +1,189 @@ +/* + * mcp23s08.c: + * Extend wiringPi with the MCP 23s08 SPI GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "wiringPi.h" +#include "wiringPiSPI.h" +#include "mcp23x0817.h" + +#include "mcp23s08.h" + +#define MCP_SPEED 4000000 + + + +/* + * writeByte: + * Write a byte to a register on the MCP23s08 on the SPI bus. + ********************************************************************************* + */ + +static void writeByte (uint8_t spiPort, uint8_t devId, uint8_t reg, uint8_t data) +{ + uint8_t spiData [4] ; + + spiData [0] = CMD_WRITE | ((devId & 7) << 1) ; + spiData [1] = reg ; + spiData [2] = data ; + + wiringPiSPIDataRW (spiPort, spiData, 3) ; +} + +/* + * readByte: + * Read a byte from a register on the MCP23s08 on the SPI bus. + ********************************************************************************* + */ + +static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg) +{ + uint8_t spiData [4] ; + + spiData [0] = CMD_READ | ((devId & 7) << 1) ; + spiData [1] = reg ; + + wiringPiSPIDataRW (spiPort, spiData, 3) ; + + return spiData [2] ; +} + + +/* + * myPinMode: + ********************************************************************************* + */ + +static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int mask, old, reg ; + + reg = MCP23x08_IODIR ; + mask = 1 << (pin - node->pinBase) ; + old = readByte (node->data0, node->data1, reg) ; + + if (mode == OUTPUT) + old &= (~mask) ; + else + old |= mask ; + + writeByte (node->data0, node->data1, reg, old) ; +} + + +/* + * myPullUpDnControl: + ********************************************************************************* + */ + +static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int mask, old, reg ; + + reg = MCP23x08_GPPU ; + mask = 1 << (pin - node->pinBase) ; + + old = readByte (node->data0, node->data1, reg) ; + + if (mode == PUD_UP) + old |= mask ; + else + old &= (~mask) ; + + writeByte (node->data0, node->data1, reg, old) ; +} + + +/* + * myDigitalWrite: + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + int bit, old ; + + bit = 1 << ((pin - node->pinBase) & 7) ; + + old = node->data2 ; + if (value == LOW) + old &= (~bit) ; + else + old |= bit ; + + writeByte (node->data0, node->data1, MCP23x08_GPIO, old) ; + node->data2 = old ; +} + + +/* + * myDigitalRead: + ********************************************************************************* + */ + +static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + int mask, value ; + + mask = 1 << ((pin - node->pinBase) & 7) ; + value = readByte (node->data0, node->data1, MCP23x08_GPIO) ; + + if ((value & mask) == 0) + return LOW ; + else + return HIGH ; +} + + +/* + * mcp23s08Setup: + * Create a new instance of an MCP23s08 SPI GPIO interface. We know it + * has 8 pins, so all we need to know here is the SPI address and the + * user-defined pin base. + ********************************************************************************* + */ + +int mcp23s08Setup (const int pinBase, const int spiPort, const int devId) +{ + int x ; + struct wiringPiNodeStruct *node ; + + if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0) + return x ; + + writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ; + + node = wiringPiNewNode (pinBase, 8) ; + + node->data0 = spiPort ; + node->data1 = devId ; + node->pinMode = myPinMode ; + node->pullUpDnControl = myPullUpDnControl ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->data2 = readByte (spiPort, devId, MCP23x08_OLAT) ; + + return 0 ; +} diff --git a/wiringPi/mcp23s08.h b/wiringPi/mcp23s08.h new file mode 100644 index 0000000..ebf93d1 --- /dev/null +++ b/wiringPi/mcp23s08.h @@ -0,0 +1,33 @@ +/* + * 23s08.h: + * Extend wiringPi with the MCP 23s08 SPI GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp23s08Setup (const int pinBase, const int spiPort, const int devId) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp23s17.c b/wiringPi/mcp23s17.c new file mode 100644 index 0000000..c2d1be3 --- /dev/null +++ b/wiringPi/mcp23s17.c @@ -0,0 +1,236 @@ +/* + * mcp23s17.c: + * Extend wiringPi with the MCP 23s17 SPI GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "wiringPi.h" +#include "wiringPiSPI.h" +#include "mcp23x0817.h" + +#include "mcp23s17.h" + +#define MCP_SPEED 4000000 + + + +/* + * writeByte: + * Write a byte to a register on the MCP23s17 on the SPI bus. + ********************************************************************************* + */ + +static void writeByte (uint8_t spiPort, uint8_t devId, uint8_t reg, uint8_t data) +{ + uint8_t spiData [4] ; + + spiData [0] = CMD_WRITE | ((devId & 7) << 1) ; + spiData [1] = reg ; + spiData [2] = data ; + + wiringPiSPIDataRW (spiPort, spiData, 3) ; +} + +/* + * readByte: + * Read a byte from a register on the MCP23s17 on the SPI bus. + ********************************************************************************* + */ + +static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg) +{ + uint8_t spiData [4] ; + + spiData [0] = CMD_READ | ((devId & 7) << 1) ; + spiData [1] = reg ; + + wiringPiSPIDataRW (spiPort, spiData, 3) ; + + return spiData [2] ; +} + + +/* + * myPinMode: + ********************************************************************************* + */ + +static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int mask, old, reg ; + + pin -= node->pinBase ; + + if (pin < 8) // Bank A + reg = MCP23x17_IODIRA ; + else + { + reg = MCP23x17_IODIRB ; + pin &= 0x07 ; + } + + mask = 1 << pin ; + old = readByte (node->data0, node->data1, reg) ; + + if (mode == OUTPUT) + old &= (~mask) ; + else + old |= mask ; + + writeByte (node->data0, node->data1, reg, old) ; +} + + +/* + * myPullUpDnControl: + ********************************************************************************* + */ + +static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int mask, old, reg ; + + pin -= node->pinBase ; + + if (pin < 8) // Bank A + reg = MCP23x17_GPPUA ; + else + { + reg = MCP23x17_GPPUB ; + pin &= 0x07 ; + } + + mask = 1 << pin ; + old = readByte (node->data0, node->data1, reg) ; + + if (mode == PUD_UP) + old |= mask ; + else + old &= (~mask) ; + + writeByte (node->data0, node->data1, reg, old) ; +} + + +/* + * myDigitalWrite: + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + int bit, old ; + + pin -= node->pinBase ; // Pin now 0-15 + + bit = 1 << (pin & 7) ; + + if (pin < 8) // Bank A + { + old = node->data2 ; + + if (value == LOW) + old &= (~bit) ; + else + old |= bit ; + + writeByte (node->data0, node->data1, MCP23x17_GPIOA, old) ; + node->data2 = old ; + } + else // Bank B + { + old = node->data3 ; + + if (value == LOW) + old &= (~bit) ; + else + old |= bit ; + + writeByte (node->data0, node->data1, MCP23x17_GPIOB, old) ; + node->data3 = old ; + } +} + + +/* + * myDigitalRead: + ********************************************************************************* + */ + +static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + int mask, value, gpio ; + + pin -= node->pinBase ; + + if (pin < 8) // Bank A + gpio = MCP23x17_GPIOA ; + else + { + gpio = MCP23x17_GPIOB ; + pin &= 0x07 ; + } + + mask = 1 << pin ; + value = readByte (node->data0, node->data1, gpio) ; + + if ((value & mask) == 0) + return LOW ; + else + return HIGH ; +} + + +/* + * mcp23s17Setup: + * Create a new instance of an MCP23s17 SPI GPIO interface. We know it + * has 16 pins, so all we need to know here is the SPI address and the + * user-defined pin base. + ********************************************************************************* + */ + +int mcp23s17Setup (const int pinBase, const int spiPort, const int devId) +{ + int x ; + struct wiringPiNodeStruct *node ; + + if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0) + return x ; + + writeByte (spiPort, devId, MCP23x17_IOCON, IOCON_INIT | IOCON_HAEN) ; + writeByte (spiPort, devId, MCP23x17_IOCONB, IOCON_INIT | IOCON_HAEN) ; + + node = wiringPiNewNode (pinBase, 16) ; + + node->data0 = spiPort ; + node->data1 = devId ; + node->pinMode = myPinMode ; + node->pullUpDnControl = myPullUpDnControl ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->data2 = readByte (spiPort, devId, MCP23x17_OLATA) ; + node->data3 = readByte (spiPort, devId, MCP23x17_OLATB) ; + + return 0 ; +} diff --git a/wiringPi/mcp23s17.h b/wiringPi/mcp23s17.h new file mode 100644 index 0000000..3b2a808 --- /dev/null +++ b/wiringPi/mcp23s17.h @@ -0,0 +1,33 @@ +/* + * 23s17.h: + * Extend wiringPi with the MCP 23s17 SPI GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp23s17Setup (int pinBase, int spiPort, int devId) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp23x08.h b/wiringPi/mcp23x08.h new file mode 100644 index 0000000..c4e6b27 --- /dev/null +++ b/wiringPi/mcp23x08.h @@ -0,0 +1,73 @@ +/* + * mcp23x17: + * Copyright (c) 2012-2013 Gordon Henderson + * + * Header file for code using the MCP23x17 GPIO expander chip. + * This comes in 2 flavours: MCP23017 which has an I2C interface, + * an the MXP23S17 which has an SPI interface. + *********************************************************************** + * 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 . + *********************************************************************** + */ + + +// MCP23x17 Registers + +#define IODIRA 0x00 +#define IPOLA 0x02 +#define GPINTENA 0x04 +#define DEFVALA 0x06 +#define INTCONA 0x08 +#define IOCON 0x0A +#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 IOCONB 0x0B +#define GPPUB 0x0D +#define INTFB 0x0F +#define INTCAPB 0x11 +#define GPIOB 0x13 +#define OLATB 0x15 + +// Bits in the IOCON register + +#define IOCON_UNUSED 0x01 +#define IOCON_INTPOL 0x02 +#define IOCON_ODR 0x04 +#define IOCON_HAEN 0x08 +#define IOCON_DISSLW 0x10 +#define IOCON_SEQOP 0x20 +#define IOCON_MIRROR 0x40 +#define IOCON_BANK_MODE 0x80 + +// Default initialisation mode + +#define IOCON_INIT (IOCON_SEQOP) + +// SPI Command codes + +#define CMD_WRITE 0x40 +#define CMD_READ 0x41 diff --git a/wiringPi/mcp23x0817.h b/wiringPi/mcp23x0817.h new file mode 100644 index 0000000..58bc038 --- /dev/null +++ b/wiringPi/mcp23x0817.h @@ -0,0 +1,87 @@ +/* + * mcp23xxx: + * Copyright (c) 2012-2013 Gordon Henderson + * + * Header file for code using the MCP23x08 and 17 GPIO expander + * chips. + * This comes in 2 flavours: MCP230xx (08/17) which has an I2C + * interface, and the MXP23Sxx (08/17) which has an SPI interface. + *********************************************************************** + * 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 . + *********************************************************************** + */ + +// MCP23x08 Registers + +#define MCP23x08_IODIR 0x00 +#define MCP23x08_IPOL 0x01 +#define MCP23x08_GPINTEN 0x02 +#define MCP23x08_DEFVAL 0x03 +#define MCP23x08_INTCON 0x04 +#define MCP23x08_IOCON 0x05 +#define MCP23x08_GPPU 0x06 +#define MCP23x08_INTF 0x07 +#define MCP23x08_INTCAP 0x08 +#define MCP23x08_GPIO 0x09 +#define MCP23x08_OLAT 0x0A + +// MCP23x17 Registers + +#define MCP23x17_IODIRA 0x00 +#define MCP23x17_IPOLA 0x02 +#define MCP23x17_GPINTENA 0x04 +#define MCP23x17_DEFVALA 0x06 +#define MCP23x17_INTCONA 0x08 +#define MCP23x17_IOCON 0x0A +#define MCP23x17_GPPUA 0x0C +#define MCP23x17_INTFA 0x0E +#define MCP23x17_INTCAPA 0x10 +#define MCP23x17_GPIOA 0x12 +#define MCP23x17_OLATA 0x14 + +#define MCP23x17_IODIRB 0x01 +#define MCP23x17_IPOLB 0x03 +#define MCP23x17_GPINTENB 0x05 +#define MCP23x17_DEFVALB 0x07 +#define MCP23x17_INTCONB 0x09 +#define MCP23x17_IOCONB 0x0B +#define MCP23x17_GPPUB 0x0D +#define MCP23x17_INTFB 0x0F +#define MCP23x17_INTCAPB 0x11 +#define MCP23x17_GPIOB 0x13 +#define MCP23x17_OLATB 0x15 + +// Bits in the IOCON register + +#define IOCON_UNUSED 0x01 +#define IOCON_INTPOL 0x02 +#define IOCON_ODR 0x04 +#define IOCON_HAEN 0x08 +#define IOCON_DISSLW 0x10 +#define IOCON_SEQOP 0x20 +#define IOCON_MIRROR 0x40 +#define IOCON_BANK_MODE 0x80 + +// Default initialisation mode + +#define IOCON_INIT (IOCON_SEQOP) + +// SPI Command codes + +#define CMD_WRITE 0x40 +#define CMD_READ 0x41 diff --git a/wiringPi/mcp3002.c b/wiringPi/mcp3002.c new file mode 100644 index 0000000..2e7d5cf --- /dev/null +++ b/wiringPi/mcp3002.c @@ -0,0 +1,76 @@ +/* + * mcp3002.c: + * Extend wiringPi with the MCP3002 SPI Analog to Digital convertor + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "mcp3002.h" + +/* + * myAnalogRead: + * Return the analog value of the given pin + ********************************************************************************* + */ + +static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) +{ + unsigned char spiData [2] ; + unsigned char chanBits ; + int chan = pin - node->pinBase ; + + if (chan == 0) + chanBits = 0b11010000 ; + else + chanBits = 0b11110000 ; + + spiData [0] = chanBits ; + spiData [1] = 0 ; + + wiringPiSPIDataRW (node->fd, spiData, 2) ; + + return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ; +} + + +/* + * mcp3002Setup: + * Create a new wiringPi device node for an mcp2003 on the Pi's + * SPI interface. + ********************************************************************************* + */ + +int mcp3002Setup (const int pinBase, int spiChannel) +{ + struct wiringPiNodeStruct *node ; + + if (wiringPiSPISetup (spiChannel, 1000000) < 0) + return -1 ; + + node = wiringPiNewNode (pinBase, 2) ; + + node->fd = spiChannel ; + node->analogRead = myAnalogRead ; + + return 0 ; +} diff --git a/wiringPi/mcp3002.h b/wiringPi/mcp3002.h new file mode 100644 index 0000000..0cd727f --- /dev/null +++ b/wiringPi/mcp3002.h @@ -0,0 +1,33 @@ +/* + * mcp3002.c: + * Extend wiringPi with the MCP3002 SPI Analog to Digital convertor + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp3002Setup (int pinBase, int spiChannel) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp3422.c b/wiringPi/mcp3422.c new file mode 100644 index 0000000..8e26d76 --- /dev/null +++ b/wiringPi/mcp3422.c @@ -0,0 +1,116 @@ +/* + * mcp3422.c: + * Extend wiringPi with the MCP3422 I2C ADC chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + + +#include +#include +#include +#include +#include + +#include +#include + +#include "mcp3422.h" + + +/* + * myAnalogRead: + * Read a channel from the device + ********************************************************************************* + */ + +int myAnalogRead (struct wiringPiNodeStruct *node, int chan) +{ + unsigned char config, b0, b1, b2, b3 ; + int value = 0 ; + +// One-shot mode, trigger plus the other configs. + + config = 0x80 | ((chan - node->pinBase) << 5) | (node->data0 << 2) | (node->data1) ; + + wiringPiI2CWrite (node->fd, config) ; + + switch (node->data0) // Sample rate + { + case MCP3422_SR_3_75: // 18 bits + delay (270) ; + b0 = wiringPiI2CRead (node->fd) ; + b1 = wiringPiI2CRead (node->fd) ; + b2 = wiringPiI2CRead (node->fd) ; + b3 = wiringPiI2CRead (node->fd) ; + value = ((b0 & 3) << 16) | (b1 << 8) | b2 ; + break ; + + case MCP3422_SR_15: // 16 bits + delay ( 70) ; + b0 = wiringPiI2CRead (node->fd) ; + b1 = wiringPiI2CRead (node->fd) ; + b2 = wiringPiI2CRead (node->fd) ; + value = (b0 << 8) | b1 ; + break ; + + case MCP3422_SR_60: // 14 bits + delay ( 17) ; + b0 = wiringPiI2CRead (node->fd) ; + b1 = wiringPiI2CRead (node->fd) ; + b2 = wiringPiI2CRead (node->fd) ; + value = ((b0 & 0x3F) << 8) | b1 ; + break ; + + case MCP3422_SR_240: // 12 bits + delay ( 5) ; + b0 = wiringPiI2CRead (node->fd) ; + b1 = wiringPiI2CRead (node->fd) ; + b2 = wiringPiI2CRead (node->fd) ; + value = ((b0 & 0x0F) << 8) | b1 ; + break ; + } + + return value ; +} + + +/* + * mcp3422Setup: + * Create a new wiringPi device node for the mcp3422 + ********************************************************************************* + */ + +int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int gain) +{ + int fd ; + struct wiringPiNodeStruct *node ; + + if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) + return fd ; + + node = wiringPiNewNode (pinBase, channels) ; + + node->data0 = sampleRate ; + node->data1 = gain ; + node->analogRead = myAnalogRead ; + + return 0 ; +} diff --git a/wiringPi/mcp3422.h b/wiringPi/mcp3422.h new file mode 100644 index 0000000..8b4e350 --- /dev/null +++ b/wiringPi/mcp3422.h @@ -0,0 +1,43 @@ +/* + * mcp3422.c: + * Extend wiringPi with the MCP3422 I2C ADC chip + *********************************************************************** + * 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 . + *********************************************************************** + */ + +#define MCP3422_SR_3_75 0 +#define MCP3422_SR_15 1 +#define MCP3422_SR_60 2 +#define MCP3422_SR_240 3 + +#define MCP3422_GAIN_1 0 +#define MCP3422_GAIN_2 1 +#define MCP3422_GAIN_4 2 +#define MCP3422_GAIN_8 3 + + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int gain) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp4802.c b/wiringPi/mcp4802.c new file mode 100644 index 0000000..5c5c17a --- /dev/null +++ b/wiringPi/mcp4802.c @@ -0,0 +1,76 @@ +/* + * mcp4802.c: + * Extend wiringPi with the MCP4802 SPI Digital to Analog convertor + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "mcp4802.h" + +/* + * myAnalogWrite: + * Write analog value on the given pin + ********************************************************************************* + */ + +static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + unsigned char spiData [2] ; + unsigned char chanBits, dataBits ; + int chan = pin - node->pinBase ; + + if (chan == 0) + chanBits = 0x30 ; + else + chanBits = 0xB0 ; + + chanBits |= ((value >> 4) & 0x0F) ; + dataBits = ((value << 4) & 0xF0) ; + + spiData [0] = chanBits ; + spiData [1] = dataBits ; + + wiringPiSPIDataRW (node->fd, spiData, 2) ; +} + +/* + * mcp4802Setup: + * Create a new wiringPi device node for an mcp4802 on the Pi's + * SPI interface. + ********************************************************************************* + */ + +int mcp4802Setup (const int pinBase, int spiChannel) +{ + struct wiringPiNodeStruct *node ; + + if (wiringPiSPISetup (spiChannel, 1000000) < 0) + return -1 ; + + node = wiringPiNewNode (pinBase, 2) ; + + node->fd = spiChannel ; + node->analogWrite = myAnalogWrite ; + + return 0 ; +} diff --git a/wiringPi/mcp4802.h b/wiringPi/mcp4802.h new file mode 100644 index 0000000..effa024 --- /dev/null +++ b/wiringPi/mcp4802.h @@ -0,0 +1,33 @@ +/* + * mcp4802.c: + * Extend wiringPi with the MCP4802 SPI Digital to Analog convertor + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp4802Setup (int pinBase, int spiChannel) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/pcf8574.c b/wiringPi/pcf8574.c new file mode 100644 index 0000000..ce9c533 --- /dev/null +++ b/wiringPi/pcf8574.c @@ -0,0 +1,126 @@ +/* + * pcf8574.c: + * Extend wiringPi with the PFC8574 I2C GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "wiringPi.h" +#include "wiringPiI2C.h" + +#include "pcf8574.h" + + +/* + * myPinMode: + * The PFC8574 is an odd chip - the pins are effectively bi-directional, + * however the pins should be drven high when used as an input pin... + * So, we're effectively copying digitalWrite... + ********************************************************************************* + */ + +static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int bit, old ; + + bit = 1 << ((pin - node->pinBase) & 7) ; + + old = node->data2 ; + if (mode == OUTPUT) + old &= (~bit) ; // Write bit to 0 + else + old |= bit ; // Write bit to 1 + + wiringPiI2CWrite (node->fd, old) ; + node->data2 = old ; +} + + + +/* + * myDigitalWrite: + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + int bit, old ; + + bit = 1 << ((pin - node->pinBase) & 7) ; + + old = node->data2 ; + if (value == LOW) + old &= (~bit) ; + else + old |= bit ; + + wiringPiI2CWrite (node->fd, old) ; + node->data2 = old ; +} + + +/* + * myDigitalRead: + ********************************************************************************* + */ + +static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + int mask, value ; + + mask = 1 << ((pin - node->pinBase) & 7) ; + value = wiringPiI2CRead (node->fd) ; + + if ((value & mask) == 0) + return LOW ; + else + return HIGH ; +} + + +/* + * pcf8574Setup: + * Create a new instance of a PFC8574 I2C GPIO interface. We know it + * has 8 pins, so all we need to know here is the I2C address and the + * user-defined pin base. + ********************************************************************************* + */ + +int pcf8574Setup (const int pinBase, const int i2cAddress) +{ + int fd ; + struct wiringPiNodeStruct *node ; + + if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) + return fd ; + + node = wiringPiNewNode (pinBase, 8) ; + + node->fd = fd ; + node->pinMode = myPinMode ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->data2 = wiringPiI2CRead (fd) ; + + return 0 ; +} diff --git a/wiringPi/pcf8574.h b/wiringPi/pcf8574.h new file mode 100644 index 0000000..3cad2dd --- /dev/null +++ b/wiringPi/pcf8574.h @@ -0,0 +1,33 @@ +/* + * pfc8574.h: + * Extend wiringPi with the PFC8574 I2C GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int pfc8574Setup (const int pinBase, const int i2cAddress) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/piHiPri.c b/wiringPi/piHiPri.c index e7e06b4..d2f3b4e 100644 --- a/wiringPi/piHiPri.c +++ b/wiringPi/piHiPri.c @@ -36,15 +36,16 @@ ********************************************************************************* */ -int piHiPri (int pri) +int piHiPri (const 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 = sched_get_priority_max (SCHED_RR) ; + else + sched.sched_priority = pri ; - sched.sched_priority = pri ; return sched_setscheduler (0, SCHED_RR, &sched) ; } diff --git a/wiringPi/softPwm.c b/wiringPi/softPwm.c index b568dfb..a4f0fc4 100644 --- a/wiringPi/softPwm.c +++ b/wiringPi/softPwm.c @@ -28,7 +28,7 @@ #include "wiringPi.h" #include "softPwm.h" -#define MAX_PINS 64 +#define MAX_PINS 1024 // The PWM Frequency is derived from the "pulse time" below. Essentially, // the frequency is a function of the range and this pulse time. @@ -93,7 +93,7 @@ static PI_THREAD (softPwmThread) void softPwmWrite (int pin, int value) { - pin &= 63 ; + pin &= (MAX_PINS - 1) ; /**/ if (value < 0) value = 0 ; diff --git a/wiringPi/softTone.c b/wiringPi/softTone.c index 8463627..b4a89f8 100644 --- a/wiringPi/softTone.c +++ b/wiringPi/softTone.c @@ -36,7 +36,7 @@ #define PULSE_TIME 100 -static int frewqs [MAX_PINS] ; +static int freqs [MAX_PINS] ; static int newPin = -1 ; @@ -49,7 +49,7 @@ static int newPin = -1 ; static PI_THREAD (softToneThread) { - int pin, frewq, halfPeriod ; + int pin, freq, halfPeriod ; pin = newPin ; newPin = -1 ; @@ -58,12 +58,12 @@ static PI_THREAD (softToneThread) for (;;) { - frewq = frewqs [pin] ; - if (frewq == 0) + freq = freqs [pin] ; + if (freq == 0) delay (1) ; else { - halfPeriod = 500000 / frewq ; + halfPeriod = 500000 / freq ; digitalWrite (pin, HIGH) ; delayMicroseconds (halfPeriod) ; @@ -83,16 +83,16 @@ static PI_THREAD (softToneThread) ********************************************************************************* */ -void softToneWrite (int pin, int frewq) +void softToneWrite (int pin, int freq) { pin &= 63 ; - /**/ if (frewq < 0) - frewq = 0 ; - else if (frewq > 5000) // Max 5KHz - frewq = 5000 ; + /**/ if (freq < 0) + freq = 0 ; + else if (freq > 5000) // Max 5KHz + freq = 5000 ; - frewqs [pin] = frewq ; + freqs [pin] = freq ; } @@ -109,7 +109,7 @@ int softToneCreate (int pin) pinMode (pin, OUTPUT) ; digitalWrite (pin, LOW) ; - frewqs [pin] = 0 ; + freqs [pin] = 0 ; newPin = pin ; res = piThreadCreate (softToneThread) ; diff --git a/wiringPi/softTone.h b/wiringPi/softTone.h index 80c64fe..d8b4e54 100644 --- a/wiringPi/softTone.h +++ b/wiringPi/softTone.h @@ -31,7 +31,7 @@ extern "C" { #endif extern int softToneCreate (int pin) ; -extern void softToneWrite (int pin, int frewq) ; +extern void softToneWrite (int pin, int freq) ; #ifdef __cplusplus } diff --git a/wiringPi/sr595.c b/wiringPi/sr595.c new file mode 100644 index 0000000..87210c2 --- /dev/null +++ b/wiringPi/sr595.c @@ -0,0 +1,109 @@ +/* + * sr595.c: + * Extend wiringPi with the 74x595 shift register as a GPIO + * expander chip. + * Note that the code can cope with a number of 595's + * daisy-chained together - up to 4 for now as we're storing + * the output "register" in a single unsigned int. + * + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "wiringPi.h" + +#include "sr595.h" + + +/* + * myDigitalWrite: + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + unsigned int mask ; + int dataPin, clockPin, latchPin ; + int bit, bits, output ; + + pin -= node->pinBase ; // Normalise pin number + bits = node->pinMax - node->pinBase + 1 ; // ie. number of clock pulses + dataPin = node->data0 ; + clockPin = node->data1 ; + latchPin = node->data2 ; + output = node->data3 ; + + mask = 1 << pin ; + + if (value == LOW) + output &= (~mask) ; + else + output |= mask ; + + node->data3 = output ; + +// A low -> high latch transition copies the latch to the output pins + + digitalWrite (latchPin, LOW) ; delayMicroseconds (1) ; + for (bit = bits - 1 ; bit >= 0 ; --bit) + { + digitalWrite (dataPin, output & (1 << bit)) ; + + digitalWrite (clockPin, HIGH) ; delayMicroseconds (1) ; + digitalWrite (clockPin, LOW) ; delayMicroseconds (1) ; + } + digitalWrite (latchPin, HIGH) ; delayMicroseconds (1) ; +} + + +/* + * sr595Setup: + * Create a new instance of a 74x595 shift register GPIO expander. + ********************************************************************************* + */ + +int sr595Setup (const int pinBase, const int numPins, + const int dataPin, const int clockPin, const int latchPin) +{ + struct wiringPiNodeStruct *node ; + + node = wiringPiNewNode (pinBase, numPins) ; + + node->data0 = dataPin ; + node->data1 = clockPin ; + node->data2 = latchPin ; + node->data3 = 0 ; // Output register + node->digitalWrite = myDigitalWrite ; + +// Initialise the underlying hardware + + digitalWrite (dataPin, LOW) ; + digitalWrite (clockPin, LOW) ; + digitalWrite (latchPin, HIGH) ; + + pinMode (dataPin, OUTPUT) ; + pinMode (clockPin, OUTPUT) ; + pinMode (latchPin, OUTPUT) ; + + return 0 ; +} diff --git a/wiringPi/sr595.h b/wiringPi/sr595.h new file mode 100644 index 0000000..4a26dc7 --- /dev/null +++ b/wiringPi/sr595.h @@ -0,0 +1,34 @@ +/* + * sr595.h: + * Extend wiringPi with the 74x595 shift registers. + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int sr595Setup (const int pinBase, const int numPins, + const int dataPin, const int clockPin, const int latchPin) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index a68ae33..b8e381a 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -53,6 +53,7 @@ #include +#include #include #include #include @@ -71,28 +72,24 @@ #include "wiringPi.h" -// Function stubs - -void (*pinMode) (int pin, int mode) ; -int (*getAlt) (int pin) ; -void (*pullUpDnControl) (int pin, int pud) ; -void (*digitalWrite) (int pin, int value) ; -void (*digitalWriteByte) (int value) ; -void (*pwmWrite) (int pin, int value) ; -void (*gpioClockSet) (int pin, int value) ; -void (*setPadDrive) (int group, int value) ; -int (*digitalRead) (int pin) ; -int (*waitForInterrupt) (int pin, int mS) ; -void (*pwmSetMode) (int mode) ; -void (*pwmSetRange) (unsigned int range) ; -void (*pwmSetClock) (int divisor) ; - - #ifndef TRUE #define TRUE (1==1) #define FALSE (1==2) #endif +// Environment Variables + +#define ENV_DEBUG "WIRINGPI_DEBUG" +#define ENV_CODES "WIRINGPI_CODES" + + +// Mask for the bottom 64 pins which belong to the Raspberry Pi +// The others are available for the other devices + +#define PI_GPIO_MASK (0xFFFFFFC0) + +static struct wiringPiNodeStruct *wiringPiNodes = NULL ; + // BCM Magic #define BCM_PASSWORD 0x5A000000 @@ -192,8 +189,11 @@ static volatile uint32_t *gpio ; static volatile uint32_t *pwm ; static volatile uint32_t *clk ; static volatile uint32_t *pads ; + +#ifdef USE_TIMER static volatile uint32_t *timer ; static volatile uint32_t *timerIrqRaw ; +#endif // Time for easy calculations @@ -202,10 +202,13 @@ static uint64_t epochMilli, epochMicro ; // Misc static int wiringPiMode = WPI_MODE_UNINITIALISED ; +static volatile int pinPass = -1 ; +static pthread_mutex_t pinMutex ; -// Debugging +// Debugging & Return codes -int wiringPiDebug = FALSE ; +int wiringPiDebug = FALSE ; +int wiringPiReturnCodes = FALSE ; // sysFds: // Map a file descriptor from the /sys/class/gpio/gpioX/value @@ -223,17 +226,17 @@ static void (*isrFunctions [64])(void) ; // pinToGpio: // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin -// Cope for 2 different board revieions here +// Cope for 2 different board revisions here. static int *pinToGpio ; static int pinToGpioR1 [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 + 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7 + 0, 1, // I2C - SDA0, SCL0 wpi 8 - 9 + 8, 7, // SPI - CE1, CE0 wpi 10 - 11 + 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14 + 14, 15, // UART - Tx, Rx wpi 15 - 16 // Padding: @@ -259,8 +262,65 @@ static int pinToGpioR2 [64] = } ; +// physToGpio: +// Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin +// Cope for 2 different board revisions here. + +static int *physToGpio ; + +static int physToGpioR1 [64] = +{ + -1, // 0 + -1, -1, // 1, 2 + 0, -1, + 1, -1, + 4, 14, + -1, 15, + 17, 18, + 21, -1, + 22, 23, + -1, 24, + 10, -1, + 9, 25, + 11, 8, + -1, 7, // 25, 26 + +// Padding: + + -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 +} ; + +static int physToGpioR2 [64] = +{ + -1, // 0 + -1, -1, // 1, 2 + 2, -1, + 3, -1, + 4, 14, + -1, 15, + 17, 18, + 27, -1, + 22, 23, + -1, 24, + 10, -1, + 9, 25, + 11, 8, + -1, 7, // 25, 26 + +// Padding: + + -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: -// Map a BCM_GPIO pin to it's control port. (GPFSEL 0-5) +// Map a BCM_GPIO pin to it's Function Selection +// control port. (GPFSEL 0-5) +// Groups of 10 - 3 bits per Function - 30 bits per port static uint8_t gpioToGPFSEL [] = { @@ -295,7 +355,6 @@ static uint8_t gpioToGPSET [] = 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, } ; - // gpioToGPCLR: // (Word) offset to the GPIO Clear registers for each GPIO pin @@ -376,6 +435,7 @@ static uint8_t gpioToPwmALT [] = 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63 } ; + // gpioToPwmPort // The port value to put a GPIO pin into PWM mode @@ -395,7 +455,7 @@ static uint8_t gpioToPwmPort [] = // gpioToGpClkALT: // ALT value to put a GPIO pin into GP Clock mode. // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21 -// for clocks 0 and 1 respectivey, however I'll include the full +// for clocks 0 and 1 respectively, however I'll include the full // list for completeness - maybe one day... #define GPIO_CLOCK_SOURCE 1 @@ -449,23 +509,34 @@ static uint8_t gpioToClkDiv [] = /* - * wpiPinToGpio: - * Translate a wiringPi Pin number to native GPIO pin number. - * (We don't use this here, prefering to just do the lookup directly, - * but it's been requested!) + * wiringPiFailure: + * Fail. Or not. ********************************************************************************* */ -int wpiPinToGpio (int wpiPin) +int wiringPiFailure (int fatal, const char *message, ...) { - return pinToGpio [wpiPin & 63] ; + va_list argp ; + char buffer [1024] ; + + if (!fatal && wiringPiReturnCodes) + return -1 ; + + va_start (argp, message) ; + vsnprintf (buffer, 1023, message, argp) ; + va_end (argp) ; + + fprintf (stderr, "%s", buffer) ; + exit (EXIT_FAILURE) ; + + return 0 ; } /* * piBoardRev: * Return a number representing the hardware revision of the board. - * Revision is currently 1 or 2. -1 is returned on error. + * Revision is currently 1 or 2. * * Much confusion here )-: * Seems there are some boards with 0000 in them (mistake in manufacture) @@ -488,7 +559,7 @@ int wpiPinToGpio (int wpiPin) ********************************************************************************* */ -static void piBoardRevOops (char *why) +static void piBoardRevOops (const char *why) { fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ; fprintf (stderr, " -> %s\n", why) ; @@ -556,6 +627,58 @@ int piBoardRev (void) } +/* + * wpiPinToGpio: + * Translate a wiringPi Pin number to native GPIO pin number. + * Provided for external support. + ********************************************************************************* + */ + +int wpiPinToGpio (int wpiPin) +{ + return pinToGpio [wpiPin & 63] ; +} + + +/* + * physPinToGpio: + * Translate a physical Pin number to native GPIO pin number. + * Provided for external support. + ********************************************************************************* + */ + +int physPinToGpio (int physPin) +{ + return physToGpio [physPin & 63] ; +} + + +/* + * setPadDrive: + * Set the PAD driver value + ********************************************************************************* + */ + +void setPadDrive (int group, int value) +{ + uint32_t wrVal ; + + if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) + { + if ((group < 0) || (group > 2)) + return ; + + wrVal = BCM_PASSWORD | 0x18 | (value & 7) ; + *(pads + group + 11) = wrVal ; + + if (wiringPiDebug) + { + printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ; + printf ("Read : %08X\n", *(pads + group + 11)) ; + } + } +} + /* * getAlt: @@ -564,12 +687,19 @@ int piBoardRev (void) ********************************************************************************* */ -int getAltGpio (int pin) +int getAlt (int pin) { int fSel, shift, alt ; pin &= 63 ; + /**/ if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + else if (wiringPiMode != WPI_MODE_GPIO) + return 0 ; + fSel = gpioToGPFSEL [pin] ; shift = gpioToShift [pin] ; @@ -578,70 +708,66 @@ int getAltGpio (int pin) return alt ; } -int getAltWPi (int pin) -{ - return getAltGpio (pinToGpio [pin & 63]) ; -} - -int getAltSys (int pin) -{ - return 0 ; -} - /* - * pwmControl: - * Allow the user to control some of the PWM functions + * pwmSetMode: + * Select the native "balanced" mode, or standard mark:space mode ********************************************************************************* */ -void pwmSetModeWPi (int mode) +void pwmSetMode (int mode) { - if (mode == PWM_MODE_MS) - *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ; - else - *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; + if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) + { + if (mode == PWM_MODE_MS) + *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ; + else + *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ; + } } -void pwmSetModeSys (int mode) -{ - return ; -} - - -void pwmSetRangeWPi (unsigned int range) -{ - *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ; - *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ; -} - -void pwmSetRangeSys (unsigned int range) -{ - return ; -} /* - * pwmSetClockWPi: + * pwmSetRange: + * Set the PWM range register. We set both range registers to the same + * value. If you want different in your own code, then write your own. + ********************************************************************************* + */ + +void pwmSetRange (unsigned int range) +{ + if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) + { + *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ; + *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ; + } +} + + +/* + * pwmSetClock: * Set/Change the PWM clock. Originally my code, but changed * (for the better!) by Chris Hall, * after further study of the manual and testing with a 'scope ********************************************************************************* */ -void pwmSetClockWPi (int divisor) +void pwmSetClock (int divisor) { uint32_t pwm_control ; divisor &= 4095 ; - if (wiringPiDebug) - printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ; + if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) + { + if (wiringPiDebug) + printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ; - pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL + pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY // stays high. - *(pwm + PWM_CONTROL) = 0 ; // Stop PWM + *(pwm + PWM_CONTROL) = 0 ; // Stop PWM // Stop PWM clock before changing divisor. The delay after this does need to // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY @@ -649,24 +775,125 @@ void pwmSetClockWPi (int divisor) // adjusted the clock sometimes switches to very slow, once slow further DIV // adjustments do nothing and it's difficult to get out of this mode. - *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock - delayMicroseconds (110) ; // prevents clock going sloooow + *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock + delayMicroseconds (110) ; // prevents clock going sloooow - while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY - delayMicroseconds (1) ; + while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY + delayMicroseconds (1) ; - *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ; + *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ; - *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock - *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL + *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock + *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL - if (wiringPiDebug) - printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ; + if (wiringPiDebug) + printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ; + } } -void pwmSetClockSys (int divisor) + +/* + * gpioClockSet: + * Set the freuency on a GPIO clock pin + ********************************************************************************* + */ + +void gpioClockSet (int pin, int freq) { - return ; + int divi, divr, divf ; + + pin &= 63 ; + + /**/ if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + else if (wiringPiMode != WPI_MODE_GPIO) + return ; + + divi = 19200000 / freq ; + divr = 19200000 % freq ; + divf = (int)((double)divr * 4096.0 / 19200000.0) ; + + if (divi > 4095) + divi = 4095 ; + + *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock + while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait + ; + + *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers + *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock +} + + +/* + * wiringPiFindNode: + * Locate our device node + ********************************************************************************* + */ + +static struct wiringPiNodeStruct *wiringPiFindNode (int pin) +{ + struct wiringPiNodeStruct *node = wiringPiNodes ; + + while (node != NULL) + if ((pin >= node->pinBase) && (pin <= node->pinMax)) + return node ; + else + node = node->next ; + + return NULL ; +} + + +/* + * wiringPiNewNode: + * Create a new GPIO node into the wiringPi handling system + ********************************************************************************* + */ + +static void pinModeDummy (struct wiringPiNodeStruct *node, int pin, int mode) { return ; } +static void pullUpDnControlDummy (struct wiringPiNodeStruct *node, int pin, int pud) { return ; } +static int digitalReadDummy (struct wiringPiNodeStruct *node, int pin) { return LOW ; } +static void digitalWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; } +static void pwmWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; } +static int analogReadDummy (struct wiringPiNodeStruct *node, int pin) { return 0 ; } +static void analogWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; } + +struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) +{ + int pin ; + struct wiringPiNodeStruct *node ; + +// Minimum pin base is 64 + + if (pinBase < 64) + (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ; + +// Check all pins in-case there is overlap: + + for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin) + if (wiringPiFindNode (pin) != NULL) + (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ; + + node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros + if (node == NULL) + (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ; + + node->pinBase = pinBase ; + node->pinMax = pinBase + numPins - 1 ; + node->pinMode = pinModeDummy ; + node->pullUpDnControl = pullUpDnControlDummy ; + node->digitalRead = digitalReadDummy ; + node->digitalWrite = digitalWriteDummy ; + node->pwmWrite = pwmWriteDummy ; + node->analogRead = analogReadDummy ; + node->analogWrite = analogWriteDummy ; + node->next = wiringPiNodes ; + wiringPiNodes = node ; + + return node ; } @@ -686,230 +913,74 @@ void pinEnableED01Pi (int pin) #endif - /* - * digitalWrite: - * Set an output bit + ********************************************************************************* + * Core Functions ********************************************************************************* */ -void digitalWriteWPi (int pin, int value) -{ - pin = pinToGpio [pin & 63] ; - if (value == LOW) - *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ; +/* + * pinMode: + * Sets the mode of a pin to be input, output or PWM output + ********************************************************************************* + */ + +void pinMode (int pin, int mode) +{ + int fSel, shift, alt ; + struct wiringPiNodeStruct *node = wiringPiNodes ; + + if ((pin & PI_GPIO_MASK) == 0) // On-board pin + { + /**/ if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + else if (wiringPiMode != WPI_MODE_GPIO) + return ; + + 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) ; + delayMicroseconds (110) ; // See comments in pwmSetClockWPi + + pwmSetMode (PWM_MODE_BAL) ; // Pi default mode + pwmSetRange (1024) ; // Default range of 1024 + pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM + } + else if (mode == GPIO_CLOCK) + { + if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin + return ; + +// Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz + + *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; + delayMicroseconds (110) ; + gpioClockSet (pin, 100000) ; + } + } else - *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ; -} - -void digitalWriteGpio (int pin, int value) -{ - pin &= 63 ; - - if (value == LOW) - *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ; - else - *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ; -} - -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) ; - } -} - - -/* - * digitalWriteByte: - * Write an 8-bit byte to the first 8 GPIO pins - try to do it as - * fast as possible. - * However it still needs 2 operations to set the bits, so any external - * hardware must not rely on seeing a change as there will be a change - * to set the outputs bits to zero, then another change to set the 1's - ********************************************************************************* - */ - -void digitalWriteByteGpio (int value) -{ - uint32_t pinSet = 0 ; - uint32_t pinClr = 0 ; - int mask = 1 ; - int pin ; - - for (pin = 0 ; pin < 8 ; ++pin) - { - if ((value & mask) == 0) - pinClr |= (1 << pinToGpio [pin]) ; - else - pinSet |= (1 << pinToGpio [pin]) ; - - mask <<= 1 ; - } - - *(gpio + gpioToGPCLR [0]) = pinClr ; - *(gpio + gpioToGPSET [0]) = pinSet ; -} - -void digitalWriteByteSys (int value) -{ - int mask = 1 ; - int pin ; - - for (pin = 0 ; pin < 8 ; ++pin) - { - digitalWriteSys (pinToGpio [pin], value & mask) ; - mask <<= 1 ; - } -} - - -/* - * pwmWrite: - * Set an output PWM value - ********************************************************************************* - */ - -void pwmWriteGpio (int pin, int value) -{ - int port ; - - pin = pin & 63 ; - port = gpioToPwmPort [pin] ; - - *(pwm + port) = value ; -} - -void pwmWriteWPi (int pin, int value) -{ - pwmWriteGpio (pinToGpio [pin & 63], value) ; -} - -void pwmWriteSys (int pin, int value) -{ - return ; -} - - -/* - * gpioClockSet: - * Set the freuency on a GPIO clock pin - ********************************************************************************* - */ - -void gpioClockSetGpio (int pin, int freq) -{ - int divi, divr, divf ; - - pin &= 63 ; - - divi = 19200000 / freq ; - divr = 19200000 % freq ; - divf = (int)((double)divr * 4096.0 / 19200000.0) ; - - if (divi > 4095) - divi = 4095 ; - - *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock - while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait - ; - - *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers - *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock -} - -void gpioClockSetWPi (int pin, int freq) -{ - gpioClockSetGpio (pinToGpio [pin & 63], freq) ; -} - -void gpioClockSetSys (int pin, int freq) -{ - return ; -} - - -/* - * setPadDrive: - * Set the PAD driver value - ********************************************************************************* - */ - -void setPadDriveWPi (int group, int value) -{ - uint32_t wrVal ; - - if ((group < 0) || (group > 2)) + if ((node = wiringPiFindNode (pin)) != NULL) + node->pinMode (node, pin, mode) ; return ; - - wrVal = BCM_PASSWORD | 0x18 | (value & 7) ; - *(pads + group + 11) = wrVal ; - - if (wiringPiDebug) - { - printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ; - printf ("Read : %08X\n", *(pads + group + 11)) ; } } -void setPadDriveGpio (int group, int value) -{ - setPadDriveWPi (group, value) ; -} - -void setPadDriveSys (int group, int value) -{ - return ; -} - - -/* - * digitalRead: - * Read the value of a given Pin, returning HIGH or LOW - ********************************************************************************* - */ - -int digitalReadWPi (int pin) -{ - pin = pinToGpio [pin & 63] ; - - if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0) - return HIGH ; - else - return LOW ; -} - -int digitalReadGpio (int pin) -{ - pin &= 63 ; - - if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 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: @@ -920,90 +991,236 @@ int digitalReadSys (int pin) ********************************************************************************* */ -void pullUpDnControlGpio (int pin, int pud) +void pullUpDnControl (int pin, int pud) { - pin &= 63 ; - pud &= 3 ; + struct wiringPiNodeStruct *node = wiringPiNodes ; - *(gpio + GPPUD) = pud ; delayMicroseconds (5) ; - *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ; - - *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ; - *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ; -} + if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin + { + /**/ if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + else if (wiringPiMode != WPI_MODE_GPIO) + return ; -void pullUpDnControlWPi (int pin, int pud) -{ - pullUpDnControlGpio (pinToGpio [pin & 63], pud) ; -} - -void pullUpDnControlSys (int pin, int pud) -{ - return ; + *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ; + *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ; + + *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ; + *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ; + } + else // Extension module + { + if ((node = wiringPiFindNode (pin)) != NULL) + node->pullUpDnControl (node, pin, pud) ; + return ; + } } /* - * pinMode: - * Sets the mode of a pin to be input, output or PWM output + * digitalRead: + * Read the value of a given Pin, returning HIGH or LOW ********************************************************************************* */ -void pinModeGpio (int pin, int mode) +int digitalRead (int pin) { -// register int barrier ; + char c ; + struct wiringPiNodeStruct *node = wiringPiNodes ; - 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 ((pin & PI_GPIO_MASK) == 0) // On-Board Pin { - if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin - return ; + /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode + { + if (sysFds [pin] == -1) + return LOW ; -// Set pin to PWM mode + lseek (sysFds [pin], 0L, SEEK_SET) ; + read (sysFds [pin], &c, 1) ; + return (c == '0') ? LOW : HIGH ; + } + else if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + else if (wiringPiMode != WPI_MODE_GPIO) + return LOW ; - *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; - delayMicroseconds (110) ; // See comments in pwmSetClockWPi - - pwmSetModeWPi (PWM_MODE_BAL) ; // Pi default mode - pwmSetRangeWPi (1024) ; // Default range of 1024 - pwmSetClockWPi (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM + if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0) + return HIGH ; + else + return LOW ; } - else if (mode == GPIO_CLOCK) + else { - if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin - return ; - -// Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz - - *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; - delayMicroseconds (110) ; - gpioClockSetGpio (pin, 100000) ; + if ((node = wiringPiFindNode (pin)) == NULL) + return LOW ; + return node->digitalRead (node, pin) ; } } -void pinModeWPi (int pin, int mode) + +/* + * digitalWrite: + * Set an output bit + ********************************************************************************* + */ + +void digitalWrite (int pin, int value) { - pinModeGpio (pinToGpio [pin & 63], mode) ; + struct wiringPiNodeStruct *node = wiringPiNodes ; + + if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin + { + /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode + { + if (sysFds [pin] != -1) + { + if (value == LOW) + write (sysFds [pin], "0\n", 2) ; + else + write (sysFds [pin], "1\n", 2) ; + } + return ; + } + else if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + else if (wiringPiMode != WPI_MODE_GPIO) + return ; + + if (value == LOW) + *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ; + else + *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ; + } + else + { + if ((node = wiringPiFindNode (pin)) != NULL) + node->digitalWrite (node, pin, value) ; + } } -void pinModeSys (int pin, int mode) + +/* + * pwmWrite: + * Set an output PWM value + ********************************************************************************* + */ + +void pwmWrite (int pin, int value) { - return ; + struct wiringPiNodeStruct *node = wiringPiNodes ; + + if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin + { + /**/ if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + else if (wiringPiMode != WPI_MODE_GPIO) + return ; + + *(pwm + gpioToPwmPort [pin]) = value ; + } + else + { + if ((node = wiringPiFindNode (pin)) != NULL) + node->pwmWrite (node, pin, value) ; + } +} + + +/* + * analogRead: + * Read the analog value of a given Pin. + * There is no on-board Pi analog hardware, + * so this needs to go to a new node. + ********************************************************************************* + */ + +int analogRead (int pin) +{ + struct wiringPiNodeStruct *node = wiringPiNodes ; + + if ((node = wiringPiFindNode (pin)) == NULL) + return 0 ; + else + return node->analogRead (node, pin) ; +} + + +/* + * analogWrite: + * Write the analog value to the given Pin. + * There is no on-board Pi analog hardware, + * so this needs to go to a new node. + ********************************************************************************* + */ + +void analogWrite (int pin, int value) +{ + struct wiringPiNodeStruct *node = wiringPiNodes ; + + if ((node = wiringPiFindNode (pin)) == NULL) + return ; + + node->analogWrite (node, pin, value) ; +} + + + +/* + * digitalWriteByte: + * Pi Specific + * Write an 8-bit byte to the first 8 GPIO pins - try to do it as + * fast as possible. + * However it still needs 2 operations to set the bits, so any external + * hardware must not rely on seeing a change as there will be a change + * to set the outputs bits to zero, then another change to set the 1's + ********************************************************************************* + */ + +void digitalWriteByte (int value) +{ + uint32_t pinSet = 0 ; + uint32_t pinClr = 0 ; + int mask = 1 ; + int pin ; + + /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) + { + for (pin = 0 ; pin < 8 ; ++pin) + { + digitalWrite (pin, value & mask) ; + mask <<= 1 ; + } + return ; + } + else + { + for (pin = 0 ; pin < 8 ; ++pin) + { + if ((value & mask) == 0) + pinClr |= (1 << pinToGpio [pin]) ; + else + pinSet |= (1 << pinToGpio [pin]) ; + + mask <<= 1 ; + } + + *(gpio + gpioToGPCLR [0]) = pinClr ; + *(gpio + gpioToGPSET [0]) = pinSet ; + } } /* * waitForInterrupt: + * Pi Specific. * 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 @@ -1011,13 +1228,18 @@ void pinModeSys (int pin, int mode) ********************************************************************************* */ -int waitForInterruptSys (int pin, int mS) +int waitForInterrupt (int pin, int mS) { int fd, x ; uint8_t c ; struct pollfd polls ; - if ((fd = sysFds [pin & 63]) == -1) + /**/ if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + + if ((fd = sysFds [pin]) == -1) return -2 ; // Setup poll structure @@ -1037,16 +1259,6 @@ int waitForInterruptSys (int pin, int mS) return x ; } -int waitForInterruptWPi (int pin, int mS) -{ - return waitForInterruptSys (pinToGpio [pin & 63], mS) ; -} - -int waitForInterruptGpio (int pin, int mS) -{ - return waitForInterruptSys (pin, mS) ; -} - /* * interruptHandler: @@ -1058,12 +1270,15 @@ int waitForInterruptGpio (int pin, int mS) static void *interruptHandler (void *arg) { - int myPin = *(int *)arg ; + int myPin ; (void)piHiPri (55) ; // Only effective if we run as root + myPin = pinPass ; + pinPass = -1 ; + for (;;) - if (waitForInterruptSys (myPin, -1) > 0) + if (waitForInterrupt (myPin, -1) > 0) isrFunctions [myPin] () ; return NULL ; @@ -1072,6 +1287,7 @@ static void *interruptHandler (void *arg) /* * wiringPiISR: + * Pi Specific. * Take the details and create an interrupt handler that will do a call- * back to the user supplied function. ********************************************************************************* @@ -1080,22 +1296,24 @@ static void *interruptHandler (void *arg) int wiringPiISR (int pin, int mode, void (*function)(void)) { pthread_t threadId ; + const char *modeS ; char fName [64] ; - char *modeS ; char pinS [8] ; pid_t pid ; int count, i ; - uint8_t c ; + char c ; + int bcmGpioPin ; pin &= 63 ; - if (wiringPiMode == WPI_MODE_UNINITIALISED) - { - fprintf (stderr, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ; - exit (EXIT_FAILURE) ; - } + /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED) + return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ; else if (wiringPiMode == WPI_MODE_PINS) - pin = pinToGpio [pin] ; + bcmGpioPin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + bcmGpioPin = physToGpio [pin] ; + else + bcmGpioPin = pin ; // Now export the pin and set the right edge // We're going to use the gpio program to do this, so it assumes @@ -1112,7 +1330,7 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) else modeS = "both" ; - sprintf (pinS, "%d", pin) ; + sprintf (pinS, "%d", bcmGpioPin) ; if ((pid = fork ()) < 0) // Fail return pid ; @@ -1127,23 +1345,29 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) } // Now pre-open the /sys/class node - it may already be open if -// we are in Sys mode, but this will do no harm. +// we are in Sys mode or if we call here twice, if-so, we'll close it. - sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; - if ((sysFds [pin] = open (fName, O_RDWR)) < 0) + if (sysFds [bcmGpioPin] != -1) + close (sysFds [bcmGpioPin]) ; + + sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ; + if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0) return -1 ; // Clear any initial pending interrupt - ioctl (sysFds [pin], FIONREAD, &count) ; + ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ; for (i = 0 ; i < count ; ++i) - read (sysFds [pin], &c, 1) ; + read (sysFds [bcmGpioPin], &c, 1) ; isrFunctions [pin] = function ; - pthread_create (&threadId, NULL, interruptHandler, &pin) ; - - delay (1) ; + pthread_mutex_lock (&pinMutex) ; + pinPass = pin ; + pthread_create (&threadId, NULL, interruptHandler, NULL) ; + while (pinPass != -1) + delay (1) ; + pthread_mutex_unlock (&pinMutex) ; return 0 ; } @@ -1152,7 +1376,7 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) /* * initialiseEpoch: * Initialise our start-of-time variable to be the current unix - * time in milliseconds. + * time in milliseconds and microseconds. ********************************************************************************* */ @@ -1165,9 +1389,10 @@ static void initialiseEpoch (void) epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ; } + /* * delay: - * Wait for some number of milli seconds + * Wait for some number of milliseconds ********************************************************************************* */ @@ -1280,124 +1505,66 @@ int wiringPiSetup (void) int fd ; int boardRev ; - if (geteuid () != 0) - { - fprintf (stderr, "wiringPi:\n Must be root to call wiringPiSetup().\n (Did you forget sudo?)\n") ; - exit (EXIT_FAILURE) ; - } - - if (getenv ("WIRINGPI_DEBUG") != NULL) - { - printf ("wiringPi: Debug mode enabled\n") ; + if (getenv (ENV_DEBUG) != NULL) wiringPiDebug = TRUE ; - } + + if (getenv (ENV_CODES) != NULL) + wiringPiReturnCodes = TRUE ; + + if (geteuid () != 0) + (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ; if (wiringPiDebug) printf ("wiringPi: wiringPiSetup called\n") ; - pinMode = pinModeWPi ; - getAlt = getAltWPi ; - pullUpDnControl = pullUpDnControlWPi ; - digitalWrite = digitalWriteWPi ; - digitalWriteByte = digitalWriteByteGpio ; // Same code - gpioClockSet = gpioClockSetWPi ; - pwmWrite = pwmWriteWPi ; - setPadDrive = setPadDriveWPi ; - digitalRead = digitalReadWPi ; - waitForInterrupt = waitForInterruptWPi ; - pwmSetMode = pwmSetModeWPi ; - pwmSetRange = pwmSetRangeWPi ; - pwmSetClock = pwmSetClockWPi ; - boardRev = piBoardRev () ; if (boardRev == 1) - pinToGpio = pinToGpioR1 ; + { + pinToGpio = pinToGpioR1 ; + physToGpio = physToGpioR1 ; + } else - pinToGpio = pinToGpioR2 ; + { + pinToGpio = pinToGpioR2 ; + physToGpio = physToGpioR2 ; + } // Open the master /dev/memory device if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) - { - if (wiringPiDebug) - { - int serr = errno ; - fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; - errno = serr ; - } - return -1 ; - } + return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; // GPIO: gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ; if ((int32_t)gpio == -1) - { - if (wiringPiDebug) - { - int serr = errno ; - fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ; - errno = serr ; - } - return -1 ; - } + return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ; // PWM pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ; if ((int32_t)pwm == -1) - { - if (wiringPiDebug) - { - int serr = errno ; - fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ; - errno = serr ; - } - return -1 ; - } + return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ; // Clock control (needed for PWM) clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ; if ((int32_t)clk == -1) - { - if (wiringPiDebug) - { - int serr = errno ; - fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ; - errno = serr ; - } - return -1 ; - } + return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ; // The drive pads pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ; if ((int32_t)pads == -1) - { - if (wiringPiDebug) - { - int serr = errno ; - fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ; - errno = serr ; - } - return -1 ; - } + return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ; +#ifdef USE_TIMER // The system timer timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ; if ((int32_t)timer == -1) - { - if (wiringPiDebug) - { - int serr = errno ; - fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ; - errno = serr ; - } - return -1 ; - } + return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ; // Set the timer to free-running, 1MHz. // 0xF9 is 249, the timer divide is base clock / (divide+1) @@ -1406,6 +1573,7 @@ int wiringPiSetup (void) *(timer + TIMER_CONTROL) = 0x0000280 ; *(timer + TIMER_PRE_DIV) = 0x00000F9 ; timerIrqRaw = timer + TIMER_IRQ_RAW ; +#endif initialiseEpoch () ; @@ -1426,40 +1594,39 @@ int wiringPiSetup (void) int wiringPiSetupGpio (void) { - int x ; - - if (geteuid () != 0) - { - fprintf (stderr, "Must be root to call wiringPiSetupGpio(). (Did you forget sudo?)\n") ; - exit (EXIT_FAILURE) ; - } - - if ((x = wiringPiSetup ()) < 0) - return x ; + (void)wiringPiSetup () ; if (wiringPiDebug) printf ("wiringPi: wiringPiSetupGpio called\n") ; - pinMode = pinModeGpio ; - getAlt = getAltGpio ; - pullUpDnControl = pullUpDnControlGpio ; - digitalWrite = digitalWriteGpio ; - digitalWriteByte = digitalWriteByteGpio ; - gpioClockSet = gpioClockSetGpio ; - pwmWrite = pwmWriteGpio ; - setPadDrive = setPadDriveGpio ; - digitalRead = digitalReadGpio ; - waitForInterrupt = waitForInterruptGpio ; - pwmSetMode = pwmSetModeWPi ; - pwmSetRange = pwmSetRangeWPi ; - pwmSetClock = pwmSetClockWPi ; - wiringPiMode = WPI_MODE_GPIO ; return 0 ; } +/* + * wiringPiSetupPhys: + * Must be called once at the start of your program execution. + * + * Phys setup: Initialises the system into Physical Pin mode and uses the + * memory mapped hardware directly. + ********************************************************************************* + */ + +int wiringPiSetupPhys (void) +{ + (void)wiringPiSetup () ; + + if (wiringPiDebug) + printf ("wiringPi: wiringPiSetupPhys called\n") ; + + wiringPiMode = WPI_MODE_PHYS ; + + return 0 ; +} + + /* * wiringPiSetupSys: * Must be called once at the start of your program execution. @@ -1475,32 +1642,27 @@ int wiringPiSetupSys (void) int pin ; char fName [128] ; - if (getenv ("WIRINGPI_DEBUG") != NULL) + if (getenv (ENV_DEBUG) != NULL) wiringPiDebug = TRUE ; + if (getenv (ENV_CODES) != NULL) + wiringPiReturnCodes = TRUE ; + if (wiringPiDebug) printf ("wiringPi: wiringPiSetupSys called\n") ; - pinMode = pinModeSys ; - getAlt = getAltSys ; - pullUpDnControl = pullUpDnControlSys ; - digitalWrite = digitalWriteSys ; - digitalWriteByte = digitalWriteByteSys ; - gpioClockSet = gpioClockSetSys ; - pwmWrite = pwmWriteSys ; - setPadDrive = setPadDriveSys ; - digitalRead = digitalReadSys ; - waitForInterrupt = waitForInterruptSys ; - pwmSetMode = pwmSetModeSys ; - pwmSetRange = pwmSetRangeSys ; - pwmSetClock = pwmSetClockSys ; - boardRev = piBoardRev () ; if (boardRev == 1) - pinToGpio = pinToGpioR1 ; + { + pinToGpio = pinToGpioR1 ; + physToGpio = physToGpioR1 ; + } else - pinToGpio = pinToGpioR2 ; + { + pinToGpio = pinToGpioR2 ; + physToGpio = physToGpioR2 ; + } // Open and scan the directory, looking for exported GPIOs, and pre-open // the 'value' interface to speed things up for later diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 18c6da5..600c851 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -29,7 +29,8 @@ #define WPI_MODE_PINS 0 #define WPI_MODE_GPIO 1 #define WPI_MODE_GPIO_SYS 2 -#define WPI_MODE_PIFACE 3 +#define WPI_MODE_PHYS 3 +#define WPI_MODE_PIFACE 4 #define WPI_MODE_UNINITIALISED -1 // Pin modes @@ -64,6 +65,42 @@ #define PI_THREAD(X) void *X (void *dummy) +// Failure modes + +#define WPI_FATAL (1==1) +#define WPI_ALMOST (1==2) + + +// wiringPiNodeStruct: +// This describes additional device nodes in the extended wiringPi +// 2.0 scheme of things. +// It's a simple linked list for now, but will hopefully migrate to +// a binary tree for efficiency reasons - but then again, the chances +// of more than 1 or 2 devices being added are fairly slim, so who +// knows.... + +struct wiringPiNodeStruct +{ + int pinBase ; + int pinMax ; + + int fd ; // Node specific + unsigned int data0 ; // ditto + unsigned int data1 ; // ditto + unsigned int data2 ; // ditto + unsigned int data3 ; // ditto + + void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ; + void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ; + int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ; + void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ; + void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ; + int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ; + void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ; + + struct wiringPiNodeStruct *next ; +} ; + // Function prototypes // c++ wrappers thanks to a comment by Nick Lott @@ -73,46 +110,61 @@ extern "C" { #endif -// Basic wiringPi functions +// Internal + +extern int wiringPiFailure (int fatal, const char *message, ...) ; + +// Core wiringPi functions + +extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ; extern int wiringPiSetup (void) ; extern int wiringPiSetupSys (void) ; extern int wiringPiSetupGpio (void) ; +extern int wiringPiSetupPhys (void) ; + +extern void pinMode (int pin, int mode) ; +extern void pullUpDnControl (int pin, int pud) ; +extern int digitalRead (int pin) ; +extern void digitalWrite (int pin, int value) ; +extern void pwmWrite (int pin, int value) ; +extern int analogRead (int pin) ; +extern void analogWrite (int pin, int value) ; + +// PiFace specifics +// (Deprecated) + extern int wiringPiSetupPiFace (void) ; +extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only + +// On-Board Raspberry Pi hardware specific stuff extern int piBoardRev (void) ; extern int wpiPinToGpio (int wpiPin) ; - -extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only - -extern void (*pinMode) (int pin, int mode) ; -extern int (*getAlt) (int pin) ; -extern void (*pullUpDnControl) (int pin, int pud) ; -extern void (*digitalWrite) (int pin, int value) ; -extern void (*digitalWriteByte) (int value) ; -extern void (*gpioClockSet) (int pin, int freq) ; -extern void (*pwmWrite) (int pin, int value) ; -extern void (*setPadDrive) (int group, int value) ; -extern int (*digitalRead) (int pin) ; -extern void (*pwmSetMode) (int mode) ; -extern void (*pwmSetRange) (unsigned int range) ; -extern void (*pwmSetClock) (int divisor) ; +extern int physPinToGpio (int physPin) ; +extern void setPadDrive (int group, int value) ; +extern int getAlt (int pin) ; +extern void digitalWriteByte (int value) ; +extern void pwmSetMode (int mode) ; +extern void pwmSetRange (unsigned int range) ; +extern void pwmSetClock (int divisor) ; +extern void gpioClockSet (int pin, int freq) ; // Interrupts +// (Also Pi hardware specific) -extern int (*waitForInterrupt) (int pin, int mS) ; +extern int waitForInterrupt (int pin, int mS) ; extern int wiringPiISR (int pin, int mode, void (*function)(void)) ; // Threads -extern int piThreadCreate (void *(*fn)(void *)) ; -extern void piLock (int key) ; -extern void piUnlock (int key) ; +extern int piThreadCreate (void *(*fn)(void *)) ; +extern void piLock (int key) ; +extern void piUnlock (int key) ; // Schedulling priority -extern int piHiPri (int pri) ; - +extern int piHiPri (const int pri) ; // Extras from arduino land diff --git a/wiringPi/wiringPiFace.c b/wiringPi/wiringPiFace.c deleted file mode 100644 index ac3c6fa..0000000 --- a/wiringPi/wiringPiFace.c +++ /dev/null @@ -1,362 +0,0 @@ -/* - * 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 digitalWriteBytePiFace (int value) -{ - writeByte (GPIOA, value) ; -} - - -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 ; - digitalWriteByte = digitalWriteBytePiFace ; - 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 ; - digitalWriteByte = digitalWriteBytePiFace ; - pwmWrite = pwmWritePiFace ; - digitalRead = digitalReadPiFace ; - waitForInterrupt = waitForInterruptPiFace ; - - return 0 ; -} diff --git a/wiringPi/wiringPiI2C.c b/wiringPi/wiringPiI2C.c index 93fe1d3..d370b60 100644 --- a/wiringPi/wiringPiI2C.c +++ b/wiringPi/wiringPiI2C.c @@ -22,15 +22,93 @@ *********************************************************************** */ +/* + * Notes: + * The Linux I2C code is actually the same (almost) as the SMBus code. + * SMBus is System Management Bus - and in essentially I2C with some + * additional functionality added, and stricter controls on the electrical + * specifications, etc. however I2C does work well with it and the + * protocols work over both. + * + * I'm directly including the SMBus functions here as some Linux distros + * lack the correct header files, and also some header files are GPLv2 + * rather than the LGPL that wiringPi is released under - presumably because + * originally no-one expected I2C/SMBus to be used outside the kernel - + * however enter the Raspberry Pi with people now taking directly to I2C + * devices without going via the kernel... + * + * This may ultimately reduce the flexibility of this code, but it won't be + * hard to maintain it and keep it current, should things change. + * + * Information here gained from: kernel/Documentation/i2c/dev-interface + * as well as other online resources. + ********************************************************************************* + */ + #include #include +#include +#include +#include #include #include -#include #include "wiringPi.h" #include "wiringPiI2C.h" +// I2C definitions + +#define I2C_SLAVE 0x0703 +#define I2C_SMBUS 0x0720 /* SMBus-level access */ + +#define I2C_SMBUS_READ 1 +#define I2C_SMBUS_WRITE 0 + +// SMBus transaction types + +#define I2C_SMBUS_QUICK 0 +#define I2C_SMBUS_BYTE 1 +#define I2C_SMBUS_BYTE_DATA 2 +#define I2C_SMBUS_WORD_DATA 3 +#define I2C_SMBUS_PROC_CALL 4 +#define I2C_SMBUS_BLOCK_DATA 5 +#define I2C_SMBUS_I2C_BLOCK_BROKEN 6 +#define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */ +#define I2C_SMBUS_I2C_BLOCK_DATA 8 + +// SMBus messages + +#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */ +#define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */ + +// Structures used in the ioctl() calls + +union i2c_smbus_data +{ + uint8_t byte ; + uint16_t word ; + uint8_t block [I2C_SMBUS_BLOCK_MAX + 2] ; // block [0] is used for length + one more for PEC +} ; + +struct i2c_smbus_ioctl_data +{ + char read_write ; + uint8_t command ; + int size ; + union i2c_smbus_data *data ; +} ; + +static inline int i2c_smbus_access (int fd, char rw, uint8_t command, int size, union i2c_smbus_data *data) +{ + struct i2c_smbus_ioctl_data args ; + + args.read_write = rw ; + args.command = command ; + args.size = size ; + args.data = data ; + return ioctl (fd, I2C_SMBUS, &args) ; +} + /* * wiringPiI2CRead: @@ -40,7 +118,12 @@ int wiringPiI2CRead (int fd) { - return i2c_smbus_read_byte (fd) ; + union i2c_smbus_data data ; + + if (i2c_smbus_access (fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data)) + return -1 ; + else + return data.byte & 0xFF ; } @@ -52,12 +135,22 @@ int wiringPiI2CRead (int fd) int wiringPiI2CReadReg8 (int fd, int reg) { - return i2c_smbus_read_byte_data (fd, reg) ; + union i2c_smbus_data data; + + if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data)) + return -1 ; + else + return data.byte & 0xFF ; } int wiringPiI2CReadReg16 (int fd, int reg) { - return i2c_smbus_read_word_data (fd, reg) ; + union i2c_smbus_data data; + + if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data)) + return -1 ; + else + return data.byte & 0xFF ; } @@ -69,7 +162,7 @@ int wiringPiI2CReadReg16 (int fd, int reg) int wiringPiI2CWrite (int fd, int data) { - return i2c_smbus_write_byte (fd, data) ; + return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ; } @@ -79,14 +172,41 @@ int wiringPiI2CWrite (int fd, int data) ********************************************************************************* */ -int wiringPiI2CWriteReg8 (int fd, int reg, int data) +int wiringPiI2CWriteReg8 (int fd, int reg, int value) { - return i2c_smbus_write_byte_data (fd, reg, data) ; + union i2c_smbus_data data ; + + data.byte = value ; + return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data) ; } -int wiringPiI2CWriteReg16 (int fd, int reg, int data) +int wiringPiI2CWriteReg16 (int fd, int reg, int value) { - return i2c_smbus_write_word_data (fd, reg, data) ; + union i2c_smbus_data data ; + + data.word = value ; + return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ; +} + + +/* + * wiringPiI2CSetupInterface: + * Undocumented access to set the interface explicitly - might be used + * for the Pi's 2nd I2C interface... + ********************************************************************************* + */ + +int wiringPiI2CSetupInterface (const char *device, int devId) +{ + int fd ; + + if ((fd = open (device, O_RDWR)) < 0) + return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ; + + if (ioctl (fd, I2C_SLAVE, devId) < 0) + return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ; + + return fd ; } @@ -96,27 +216,17 @@ int wiringPiI2CWriteReg16 (int fd, int reg, int data) ********************************************************************************* */ -int wiringPiI2CSetup (int devId) +int wiringPiI2CSetup (const int devId) { - int rev, fd ; - char *device ; + int rev ; + const char *device ; - if ((rev = piBoardRev ()) < 0) - { - fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ; - exit (1) ; - } + rev = piBoardRev () ; if (rev == 1) device = "/dev/i2c-0" ; else device = "/dev/i2c-1" ; - if ((fd = open (device, O_RDWR)) < 0) - return -1 ; - - if (ioctl (fd, I2C_SLAVE, devId) < 0) - return -1 ; - - return fd ; + return wiringPiI2CSetupInterface (device, devId) ; } diff --git a/wiringPi/wiringPiI2C.h b/wiringPi/wiringPiI2C.h index 6710ff4..6db8c68 100644 --- a/wiringPi/wiringPiI2C.h +++ b/wiringPi/wiringPiI2C.h @@ -26,15 +26,16 @@ extern "C" { #endif -extern int wiringPiI2CRead (int fd) ; -extern int wiringPiI2CReadReg8 (int fd, int reg) ; -extern int wiringPiI2CReadReg16 (int fd, int reg) ; +extern int wiringPiI2CRead (int fd) ; +extern int wiringPiI2CReadReg8 (int fd, int reg) ; +extern int wiringPiI2CReadReg16 (int fd, int reg) ; -extern int wiringPiI2CWrite (int fd, int data) ; -extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ; -extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ; +extern int wiringPiI2CWrite (int fd, int data) ; +extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ; +extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ; -int wiringPiI2CSetup (int devId) ; +extern int wiringPiI2CSetupInterface (const char *device, int devId) ; +extern int wiringPiI2CSetup (const int devId) ; #ifdef __cplusplus } diff --git a/wiringPi/wiringPiSPI.c b/wiringPi/wiringPiSPI.c index 4441498..ded07ba 100644 --- a/wiringPi/wiringPiSPI.c +++ b/wiringPi/wiringPiSPI.c @@ -25,20 +25,24 @@ #include #include +#include +#include #include #include +#include "wiringPi.h" + #include "wiringPiSPI.h" // The SPI bus parameters // Variables as they need to be passed as pointers later on -static char *spiDev0 = "/dev/spidev0.0" ; -static char *spiDev1 = "/dev/spidev0.1" ; -static uint8_t spiMode = 0 ; -static uint8_t spiBPW = 8 ; -static uint16_t spiDelay = 0; +const static char *spiDev0 = "/dev/spidev0.0" ; +const static char *spiDev1 = "/dev/spidev0.1" ; +const static uint8_t spiMode = 0 ; +const static uint8_t spiBPW = 8 ; +const static uint16_t spiDelay = 0 ; static uint32_t spiSpeeds [2] ; static int spiFds [2] ; @@ -95,7 +99,7 @@ int wiringPiSPISetup (int channel, int speed) channel &= 1 ; if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0) - return -1 ; + return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ; spiSpeeds [channel] = speed ; spiFds [channel] = fd ; @@ -104,14 +108,14 @@ int wiringPiSPISetup (int channel, int speed) // Why are we reading it afterwriting it? I've no idea, but for now I'm blindly // copying example code I've seen online... - if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) return -1 ; - if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0) return -1 ; - - if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) return -1 ; - if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) return -1 ; + if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) + return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ; + + if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) + return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ; if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) return -1 ; - if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) return -1 ; + return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ; return fd ; } diff --git a/wiringPi/wiringSerial.c b/wiringPi/wiringSerial.c index 28ec598..ca976a9 100644 --- a/wiringPi/wiringSerial.c +++ b/wiringPi/wiringSerial.c @@ -41,7 +41,7 @@ ********************************************************************************* */ -int serialOpen (char *device, int baud) +int serialOpen (const char *device, const int baud) { struct termios options ; speed_t myBaud ; @@ -60,6 +60,7 @@ int serialOpen (char *device, int baud) case 1200: myBaud = B1200 ; break ; case 1800: myBaud = B1800 ; break ; case 2400: myBaud = B2400 ; break ; + case 4800: myBaud = B4800 ; break ; case 9600: myBaud = B9600 ; break ; case 19200: myBaud = B19200 ; break ; case 38400: myBaud = B38400 ; break ; @@ -116,7 +117,7 @@ int serialOpen (char *device, int baud) ********************************************************************************* */ -void serialFlush (int fd) +void serialFlush (const int fd) { tcflush (fd, TCIOFLUSH) ; } @@ -128,7 +129,7 @@ void serialFlush (int fd) ********************************************************************************* */ -void serialClose (int fd) +void serialClose (const int fd) { close (fd) ; } @@ -140,7 +141,7 @@ void serialClose (int fd) ********************************************************************************* */ -void serialPutchar (int fd, unsigned char c) +void serialPutchar (const int fd, const unsigned char c) { write (fd, &c, 1) ; } @@ -152,7 +153,7 @@ void serialPutchar (int fd, unsigned char c) ********************************************************************************* */ -void serialPuts (int fd, char *s) +void serialPuts (const int fd, const char *s) { write (fd, s, strlen (s)) ; } @@ -163,7 +164,7 @@ void serialPuts (int fd, char *s) ********************************************************************************* */ -void serialPrintf (int fd, char *message, ...) +void serialPrintf (const int fd, const char *message, ...) { va_list argp ; char buffer [1024] ; @@ -182,7 +183,7 @@ void serialPrintf (int fd, char *message, ...) ********************************************************************************* */ -int serialDataAvail (int fd) +int serialDataAvail (const int fd) { int result ; @@ -201,7 +202,7 @@ int serialDataAvail (int fd) ********************************************************************************* */ -int serialGetchar (int fd) +int serialGetchar (const int fd) { uint8_t x ; diff --git a/wiringPi/wiringSerial.h b/wiringPi/wiringSerial.h index 1a4198c..430dc73 100644 --- a/wiringPi/wiringSerial.h +++ b/wiringPi/wiringSerial.h @@ -24,14 +24,14 @@ extern "C" { #endif -extern int serialOpen (char *device, int baud) ; -extern void serialClose (int fd) ; -extern void serialFlush (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) ; +extern int serialOpen (const char *device, const int baud) ; +extern void serialClose (const int fd) ; +extern void serialFlush (const int fd) ; +extern void serialPutchar (const int fd, const unsigned char c) ; +extern void serialPuts (const int fd, const char *s) ; +extern void serialPrintf (const int fd, const char *message, ...) ; +extern int serialDataAvail (const int fd) ; +extern int serialGetchar (const int fd) ; #ifdef __cplusplus } diff --git a/wiringPi/wiringShift.c b/wiringPi/wiringShift.c index b9b7a44..3df94e8 100644 --- a/wiringPi/wiringShift.c +++ b/wiringPi/wiringShift.c @@ -56,7 +56,6 @@ uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order) return value; } - /* * shiftOut: * Shift data out to a clocked source diff --git a/wiringPi/wiringShift.h b/wiringPi/wiringShift.h index a3f4581..419ade4 100644 --- a/wiringPi/wiringShift.h +++ b/wiringPi/wiringShift.h @@ -33,8 +33,8 @@ extern "C" { #endif -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 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) ; #ifdef __cplusplus } From 25895a86704dfa00eb439fd82844a57dd8c0baa7 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 13 May 2013 20:10:41 +0100 Subject: [PATCH 39/97] Bother. Small issue crept into the SPI code. --- gpio/gpio.c | 2 +- wiringPi/wiringPiSPI.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 8e17ae0..e7099c8 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -48,7 +48,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "2.02" +#define VERSION "2.03" #define I2CDETECT "/usr/sbin/i2cdetect" static int wpMode ; diff --git a/wiringPi/wiringPiSPI.c b/wiringPi/wiringPiSPI.c index ded07ba..215a027 100644 --- a/wiringPi/wiringPiSPI.c +++ b/wiringPi/wiringPiSPI.c @@ -114,7 +114,7 @@ int wiringPiSPISetup (int channel, int speed) if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ; - if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) return -1 ; + if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ; return fd ; From bfaf266adaf4d2627c578c9a69199ef32acb7c9c Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 20 May 2013 21:13:44 +0100 Subject: [PATCH 40/97] Aded lcd-adafruit to test/drive the Adafruit RGB LCD plate Added the Quick 2 Wire codes, etc. Minor typo/bug fixes. Added more modules into gpio -x --- People | 3 + build | 19 ++- examples/Makefile | 6 +- examples/lcd-adafruit.c | 347 ++++++++++++++++++++++++++++++++++++++++ examples/lcd.c | 125 +++++++++------ examples/q2w/Makefile | 81 ++++++++++ examples/q2w/binary.c | 79 +++++++++ examples/q2w/blink-io.c | 61 +++++++ examples/q2w/blink.c | 50 ++++++ examples/q2w/blink.sh | 37 +++++ examples/q2w/bright.c | 59 +++++++ examples/q2w/button.c | 63 ++++++++ examples/q2w/volts.c | 62 +++++++ examples/rht03.c | 0 gpio/extensions.c | 91 ++++++++++- gpio/gpio.c | 8 +- wiringPi/Makefile | 16 +- wiringPi/mcp23016.c | 164 +++++++++++++++++++ wiringPi/mcp23016.h | 33 ++++ wiringPi/mcp23016reg.h | 48 ++++++ wiringPi/pcf8574.c | 16 +- wiringPi/pcf8574.h | 6 +- wiringPi/pcf8591.c | 90 +++++++++++ wiringPi/pcf8591.h | 33 ++++ wiringPi/wiringPi.h | 5 + wiringPi/wiringPiI2C.c | 2 +- 26 files changed, 1424 insertions(+), 80 deletions(-) create mode 100644 examples/lcd-adafruit.c create mode 100644 examples/q2w/Makefile create mode 100644 examples/q2w/binary.c create mode 100644 examples/q2w/blink-io.c create mode 100644 examples/q2w/blink.c create mode 100755 examples/q2w/blink.sh create mode 100644 examples/q2w/bright.c create mode 100644 examples/q2w/button.c create mode 100644 examples/q2w/volts.c mode change 100755 => 100644 examples/rht03.c create mode 100644 wiringPi/mcp23016.c create mode 100644 wiringPi/mcp23016.h create mode 100644 wiringPi/mcp23016reg.h create mode 100644 wiringPi/pcf8591.c create mode 100644 wiringPi/pcf8591.h diff --git a/People b/People index 8be8b6d..92d1ea4 100644 --- a/People +++ b/People @@ -25,3 +25,6 @@ CHARLES Thibaut: Xian Stannard Fixing some typos in the man page! + +Andre Crone + Suggested the __WIRING_PI.H__ round wiringPi.h diff --git a/build b/build index 048ebb3..776c756 100755 --- a/build +++ b/build @@ -15,17 +15,19 @@ check-make-ok() if [ x$1 = "xclean" ]; then cd wiringPi - echo -n "wiringPi: " ; make clean + echo -n "wiringPi: " ; make clean cd ../devLib - echo -n "DevLib: " ; make clean + echo -n "DevLib: " ; make clean cd ../gpio - echo -n "gpio: " ; make clean + echo -n "gpio: " ; make clean cd ../examples - echo -n "Examples: " ; make clean + echo -n "Examples: " ; make clean cd Gertboard - echo -n "Gertboard: " ; make clean + echo -n "Gertboard: " ; make clean cd ../PiFace - echo -n "PiFace: " ; make clean + echo -n "PiFace: " ; make clean + cd ../q2w + echo -n "Quick2Wire: " ; make clean exit fi @@ -78,3 +80,8 @@ fi echo echo All Done. +echo "" +echo "NOTE: This is wiringPi v2, and if you need to use the lcd, Piface," +echo " Gertboard, MaxDetext, etc. routines then you must change your" +echo " compile scripts to add -lwiringPiDev" +echo "" diff --git a/examples/Makefile b/examples/Makefile index f1e1725..8cc256f 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -38,7 +38,7 @@ LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm SRC = blink.c blink8.c blink12.c \ pwm.c \ speed.c wfi.c isr.c isr-osc.c \ - lcd.c clock.c \ + lcd.c lcd-adafruit.c clock.c \ nes.c \ softPwm.c softTone.c \ delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \ @@ -75,6 +75,10 @@ lcd: lcd.o @echo [link] @$(CC) -o $@ lcd.o $(LDFLAGS) $(LDLIBS) +lcd-adafruit: lcd-adafruit.o + @echo [link] + @$(CC) -o $@ lcd-adafruit.o $(LDFLAGS) $(LDLIBS) + clock: clock.o @echo [link] @$(CC) -o $@ clock.o $(LDFLAGS) $(LDLIBS) diff --git a/examples/lcd-adafruit.c b/examples/lcd-adafruit.c new file mode 100644 index 0000000..47c9b9b --- /dev/null +++ b/examples/lcd-adafruit.c @@ -0,0 +1,347 @@ +/* + * lcd-adafruit.c: + * Text-based LCD driver test code + * This is designed to drive the Adafruit RGB LCD Plate + * with the additional 5 buttons for the Raspberry Pi + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + + +// Defines for the Adafruit Pi LCD interface board + +#define AF_BASE 100 +#define AF_RED (AF_BASE + 6) +#define AF_GREEN (AF_BASE + 7) +#define AF_BLUE (AF_BASE + 8) + +#define AF_E (AF_BASE + 13) +#define AF_RW (AF_BASE + 14) +#define AF_RS (AF_BASE + 15) + +#define AF_DB4 (AF_BASE + 12) +#define AF_DB5 (AF_BASE + 11) +#define AF_DB6 (AF_BASE + 10) +#define AF_DB7 (AF_BASE + 9) + +#define AF_SELECT (AF_BASE + 0) +#define AF_RIGHT (AF_BASE + 1) +#define AF_DOWN (AF_BASE + 2) +#define AF_UP (AF_BASE + 3) +#define AF_LEFT (AF_BASE + 4) + + +// User-Defined character test + +static unsigned char newChar [8] = +{ + 0b00100, + 0b00100, + 0b00000, + 0b00100, + 0b01110, + 0b11011, + 0b11011, + 0b10001, +} ; + +// Global lcd handle: + +static int lcdHandle ; + +/* + * usage: + ********************************************************************************* + */ + +int usage (const char *progName) +{ + fprintf (stderr, "Usage: %s colour\n", progName) ; + return EXIT_FAILURE ; +} + + +/* + * scrollMessage: + ********************************************************************************* + */ + +static const char *message = + " " + "Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/" + " " ; + +void scrollMessage (int line, int width) +{ + char buf [32] ; + static int position = 0 ; + static int timer = 0 ; + + if (millis () < timer) + return ; + + timer = millis () + 200 ; + + strncpy (buf, &message [position], width) ; + buf [width] = 0 ; + lcdPosition (lcdHandle, 0, line) ; + lcdPuts (lcdHandle, buf) ; + + if (++position == (strlen (message) - width)) + position = 0 ; +} + + +/* + * setBacklightColour: + * The colour outputs are inverted. + ********************************************************************************* + */ + +static void setBacklightColour (int colour) +{ + colour &= 7 ; + + digitalWrite (AF_RED, !(colour & 1)) ; + digitalWrite (AF_GREEN, !(colour & 2)) ; + digitalWrite (AF_BLUE, !(colour & 4)) ; +} + + +/* + * adafruitLCDSetup: + * Setup the Adafruit board by making sure the additional pins are + * set to the correct modes, etc. + ********************************************************************************* + */ + +static void adafruitLCDSetup (int colour) +{ + int i ; + +// Backlight LEDs + + pinMode (AF_RED, OUTPUT) ; + pinMode (AF_GREEN, OUTPUT) ; + pinMode (AF_BLUE, OUTPUT) ; + setBacklightColour (colour) ; + +// Input buttons + + for (i = 0 ; i <= 4 ; ++i) + { + pinMode (AF_BASE + i, INPUT) ; + pullUpDnControl (AF_BASE + i, PUD_UP) ; // Enable pull-ups, switches close to 0v + } + +// Control signals + + pinMode (AF_RW, OUTPUT) ; digitalWrite (AF_RW, LOW) ; // Not used with wiringPi - always in write mode + +// The other control pins are initialised with lcdInit () + + lcdHandle = lcdInit (2, 16, 4, AF_RS, AF_E, AF_DB4,AF_DB5,AF_DB6,AF_DB7, 0,0,0,0) ; + + if (lcdHandle < 0) + { + fprintf (stderr, "lcdInit failed\n") ; + exit (EXIT_FAILURE) ; + } +} + + +/* + * waitForEnter: + * On the Adafruit display, wait for the select button + ********************************************************************************* + */ + +static void waitForEnter (void) +{ + printf ("Press SELECT to continue: ") ; fflush (stdout) ; + + while (digitalRead (AF_SELECT) == HIGH) // Wait for push + delay (1) ; + + while (digitalRead (AF_SELECT) == LOW) // Wait for release + delay (1) ; + + printf ("OK\n") ; +} + + +/* + * speedTest: + * Test the update speed of the display + ********************************************************************************* + */ + +static void speedTest (void) +{ + unsigned int start, end, taken ; + int times ; + + lcdClear (lcdHandle) ; + start = millis () ; + for (times = 0 ; times < 10 ; ++times) + { + lcdPuts (lcdHandle, "0123456789ABCDEF") ; + lcdPuts (lcdHandle, "0123456789ABCDEF") ; + } + end = millis () ; + taken = (end - start) / 10; + + lcdClear (lcdHandle) ; + lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "Speed: %dmS", taken) ; + lcdPosition (lcdHandle, 0, 1) ; lcdPrintf (lcdHandle, "For full update") ; + + waitForEnter () ; + + lcdClear (lcdHandle) ; + lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "Time: %dmS", taken / 32) ; + lcdPosition (lcdHandle, 0, 1) ; lcdPrintf (lcdHandle, "Per character") ; + + waitForEnter () ; + + lcdClear (lcdHandle) ; + lcdPosition (lcdHandle, 0, 0) ; lcdPrintf (lcdHandle, "%d cps...", 32000 / taken) ; + + waitForEnter () ; +} + + +/* + * The works + ********************************************************************************* + */ + +int main (int argc, char *argv[]) +{ + int colour ; + int cols = 16 ; + int waitForRelease = FALSE ; + + struct tm *t ; + time_t tim ; + + char buf [32] ; + + if (argc != 2) + return usage (argv [0]) ; + + printf ("Raspberry Pi Adafruit LCD test\n") ; + printf ("==============================\n") ; + + colour = atoi (argv [1]) ; + + wiringPiSetupSys () ; + mcp23017Setup (AF_BASE, 0x20) ; + + adafruitLCDSetup (colour) ; + + lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ; + lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ; + + waitForEnter () ; + + lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, "Adafruit RGB LCD") ; + + waitForEnter () ; + + lcdCharDef (lcdHandle, 2, newChar) ; + + lcdClear (lcdHandle) ; + lcdPosition (lcdHandle, 0, 0) ; + lcdPuts (lcdHandle, "User Char: ") ; + lcdPutchar (lcdHandle, 2) ; + + lcdCursor (lcdHandle, TRUE) ; + lcdCursorBlink (lcdHandle, TRUE) ; + + waitForEnter () ; + + lcdCursor (lcdHandle, FALSE) ; + lcdCursorBlink (lcdHandle, FALSE) ; + + speedTest () ; + + lcdClear (lcdHandle) ; + + for (;;) + { + scrollMessage (0, cols) ; + + tim = time (NULL) ; + t = localtime (&tim) ; + + sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ; + + lcdPosition (lcdHandle, (cols - 8) / 2, 1) ; + lcdPuts (lcdHandle, buf) ; + +// Check buttons to cycle colour + +// If Up or Down are still pushed, then skip + + if (waitForRelease) + { + if ((digitalRead (AF_UP) == LOW) || (digitalRead (AF_DOWN) == LOW)) + continue ; + else + waitForRelease = FALSE ; + } + + if (digitalRead (AF_UP) == LOW) // Pushed + { + colour = colour + 1 ; + if (colour == 8) + colour = 0 ; + setBacklightColour (colour) ; + waitForRelease = TRUE ; + } + + if (digitalRead (AF_DOWN) == LOW) // Pushed + { + colour = colour - 1 ; + if (colour == -1) + colour = 7 ; + setBacklightColour (colour) ; + waitForRelease = TRUE ; + } + + } + + return 0 ; +} diff --git a/examples/lcd.c b/examples/lcd.c index c013585..510f562 100644 --- a/examples/lcd.c +++ b/examples/lcd.c @@ -65,31 +65,59 @@ static unsigned char newChar [8] = } ; +// Global lcd handle: + +static int lcdHandle ; + +/* + * usage: + ********************************************************************************* + */ + int usage (const char *progName) { fprintf (stderr, "Usage: %s bits cols rows\n", progName) ; return EXIT_FAILURE ; } + +/* + * scrollMessage: + ********************************************************************************* + */ + static const char *message = " " "Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/" " " ; -void scrollMessage (int lcd, int line, int width) +void scrollMessage (int line, int width) { char buf [32] ; static int position = 0 ; + static int timer = 0 ; + + if (millis () < timer) + return ; + + timer = millis () + 200 ; strncpy (buf, &message [position], width) ; buf [width] = 0 ; - lcdPosition (lcd, 0, line) ; - lcdPuts (lcd, buf) ; + lcdPosition (lcdHandle, 0, line) ; + lcdPuts (lcdHandle, buf) ; if (++position == (strlen (message) - width)) position = 0 ; } + +/* + * pingPong: + * Bounce a character - only on 4-line displays + ********************************************************************************* + */ + static void pingPong (int lcd, int cols) { static int position = 0 ; @@ -98,13 +126,13 @@ static void pingPong (int lcd, int cols) if (dir == 0) // Setup { dir = 1 ; - lcdPosition (lcd, 0, 3) ; - lcdPutchar (lcd, '*') ; + lcdPosition (lcdHandle, 0, 3) ; + lcdPutchar (lcdHandle, '*') ; return ; } - lcdPosition (lcd, position, 3) ; - lcdPutchar (lcd, ' ') ; + lcdPosition (lcdHandle, position, 3) ; + lcdPutchar (lcdHandle, ' ') ; position += dir ; if (position == cols) @@ -119,11 +147,15 @@ static void pingPong (int lcd, int cols) ++position ; } - lcdPosition (lcd, position, 3) ; - lcdPutchar (lcd, '#') ; + lcdPosition (lcdHandle, position, 3) ; + lcdPutchar (lcdHandle, '#') ; } +/* + * waitForEnter: + ********************************************************************************* + */ static void waitForEnter (void) { @@ -131,6 +163,12 @@ static void waitForEnter (void) (void)fgetc (stdin) ; } + +/* + * The works + ********************************************************************************* + */ + int main (int argc, char *argv[]) { int i ; @@ -167,67 +205,60 @@ int main (int argc, char *argv[]) wiringPiSetup () ; if (bits == 4) - lcd = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ; + lcdHandle = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ; else - lcd = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ; + lcdHandle = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ; - if (lcd < 0) + if (lcdHandle < 0) { fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ; return -1 ; } - lcdPosition (lcd, 0, 0) ; lcdPuts (lcd, "Gordon Henderson") ; + lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ; + lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ; + + waitForEnter () ; if (rows > 1) { - lcdPosition (lcd, 0, 1) ; - for (i = 0 ; i < (cols - 1) ; ++i) - lcdPutchar (lcd, '*') ; - lcdPutchar (lcd, '2') ; + lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ; if (rows == 4) { - lcdPosition (lcd, 0, 2) ; + lcdPosition (lcdHandle, 0, 2) ; for (i = 0 ; i < ((cols - 1) / 2) ; ++i) - lcdPuts (lcd, "=-") ; - lcdPuts (lcd, "=3") ; + lcdPuts (lcdHandle, "=-") ; + lcdPuts (lcdHandle, "=3") ; - lcdPosition (lcd, 0, 3) ; + lcdPosition (lcdHandle, 0, 3) ; for (i = 0 ; i < ((cols - 1) / 2) ; ++i) - lcdPuts (lcd, "-=") ; - lcdPuts (lcd, "-4") ; + lcdPuts (lcdHandle, "-=") ; + lcdPuts (lcdHandle, "-4") ; } } - sleep (2) ; + waitForEnter () ; - lcdPosition (lcd, 0, 0) ; lcdPuts (lcd, " wiringpi.com ") ; + lcdCharDef (lcdHandle, 2, newChar) ; + lcdClear (lcdHandle) ; + lcdPosition (lcdHandle, 0, 0) ; + lcdPuts (lcdHandle, "User Char: ") ; + lcdPutchar (lcdHandle, 2) ; + + lcdCursor (lcdHandle, TRUE) ; + lcdCursorBlink (lcdHandle, TRUE) ; waitForEnter () ; - lcdCharDef (lcd, 2, newChar) ; - - lcdClear (lcd) ; - lcdPosition (lcd, 0, 0) ; - lcdPuts (lcd, "User Char: ") ; - lcdPutchar (lcd, 2) ; - - lcdCursor (lcd, TRUE) ; - lcdCursorBlink (lcd, TRUE) ; - - waitForEnter () ; - - lcdCursor (lcd, FALSE) ; - lcdCursorBlink (lcd, FALSE) ; - + lcdCursor (lcdHandle, FALSE) ; + lcdCursorBlink (lcdHandle, FALSE) ; + lcdClear (lcdHandle) ; for (;;) { - delay (250) ; - - scrollMessage (lcd, 0, cols) ; + scrollMessage (0, cols) ; if (rows == 1) continue ; @@ -237,16 +268,16 @@ int main (int argc, char *argv[]) sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ; - lcdPosition (lcd, (cols - 8) / 2, 1) ; - lcdPuts (lcd, buf) ; + lcdPosition (lcdHandle, (cols - 8) / 2, 1) ; + lcdPuts (lcdHandle, buf) ; if (rows == 2) continue ; sprintf (buf, "%02d/%02d/%04d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ; - lcdPosition (lcd, (cols - 10) / 2, 2) ; - lcdPuts (lcd, buf) ; + lcdPosition (lcdHandle, (cols - 10) / 2, 2) ; + lcdPuts (lcdHandle, buf) ; pingPong (lcd, cols) ; } diff --git a/examples/q2w/Makefile b/examples/q2w/Makefile new file mode 100644 index 0000000..150c825 --- /dev/null +++ b/examples/q2w/Makefile @@ -0,0 +1,81 @@ +# +# Makefile: +# wiringPi - Wiring Compatable library for the Raspberry Pi +# https://projects.drogon.net/wiring-pi +# +# Copyright (c) 2012-2013 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# + + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm + +############################################################################### + +SRC = blink.c button.c blink-io.c volts.c bright.c + +OBJ = $(SRC:.c=.o) + +BINS = $(SRC:.c=) + +all: $(BINS) + +blink: blink.o + @echo [link] + @$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) + +blink-io: blink-io.o + @echo [link] + @$(CC) -o $@ blink-io.o $(LDFLAGS) $(LDLIBS) + +button: button.o + @echo [link] + @$(CC) -o $@ button.o $(LDFLAGS) $(LDLIBS) + +volts: volts.o + @echo [link] + @$(CC) -o $@ volts.o $(LDFLAGS) $(LDLIBS) + +bright: bright.o + @echo [link] + @$(CC) -o $@ bright.o $(LDFLAGS) $(LDLIBS) + + +.c.o: + @echo [CC] $< + @$(CC) -c $(CFLAGS) $< -o $@ + +clean: + @echo "[Clean]" + @rm -f $(OBJ) *~ core tags $(BINS) + +tags: $(SRC) + @echo [ctags] + @ctags $(SRC) + +depend: + makedepend -Y $(SRC) + +# DO NOT DELETE diff --git a/examples/q2w/binary.c b/examples/q2w/binary.c new file mode 100644 index 0000000..3c987c6 --- /dev/null +++ b/examples/q2w/binary.c @@ -0,0 +1,79 @@ +/* + * binary.c: + * Using the Quick 2 wire 16-bit GPIO expansion board to output + * a binary counter. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#define Q2W_BASE 100 + +int main (void) +{ + int i, bit ; + +// Enable the on-goard GPIO + + wiringPiSetup () ; + +// Add in the mcp23017 on the q2w board + + mcp23017Setup (Q2W_BASE, 0x20) ; + + printf ("Raspberry Pi - quite2Wire MCP23017 Test\n") ; + +// On-board button Input: + + pinMode (0, INPUT) ; + +// First 10 pins on q2w board as outputs: + + for (i = 0 ; i < 10 ; ++i) + pinMode (Q2W_BASE + i, OUTPUT) ; + +// Last pin as an input with the internal pull-up enabled + + pinMode (Q2W_BASE + 15, INPUT) ; + pullUpDnControl (Q2W_BASE + 15, PUD_UP) ; + +// Loop, outputting a binary number, +// Go faster with the button, or stop if the +// on-board button is pushed + + for (;;) + { + for (i = 0 ; i < 1024 ; ++i) + { + for (bit = 0 ; bit < 10 ; ++bit) + digitalWrite (Q2W_BASE + bit, i & (1 << bit)) ; + + while (digitalRead (0) == HIGH) // While pushed + delay (1) ; + + if (digitalRead (Q2W_BASE + 15) == HIGH) // Not Pushed + delay (100) ; + } + } + return 0 ; +} diff --git a/examples/q2w/blink-io.c b/examples/q2w/blink-io.c new file mode 100644 index 0000000..4dd4276 --- /dev/null +++ b/examples/q2w/blink-io.c @@ -0,0 +1,61 @@ +/* + * blink-io.c: + * Simple "blink" test for the Quick2Wire 16-pin IO board. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#define LED 1 +#define Q2W_BASE 100 + +int main (void) +{ + +// Enable the on-goard GPIO + + wiringPiSetup () ; + +// Add in the mcp23017 on the q2w board + + mcp23017Setup (Q2W_BASE, 0x20) ; + + printf ("Raspberry Pi - Quick2Wire MCP23017 Blink Test\n") ; + +// Blink the on-board LED as well as one on the mcp23017 + + pinMode (LED, OUTPUT) ; + pinMode (Q2W_BASE + 0, OUTPUT) ; + + for (;;) + { + digitalWrite (LED, HIGH) ; + digitalWrite (Q2W_BASE + 0, HIGH) ; + delay (500) ; + digitalWrite (LED, LOW) ; + digitalWrite (Q2W_BASE + 0, LOW) ; + delay (500) ; + } + + return 0 ; +} diff --git a/examples/q2w/blink.c b/examples/q2w/blink.c new file mode 100644 index 0000000..62b694a --- /dev/null +++ b/examples/q2w/blink.c @@ -0,0 +1,50 @@ +/* + * blink.c: + * Simple "blink" test for the Quick2Wire interface board. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include + +#define LED 1 + +int main (void) +{ + +// Enable the on-goard GPIO + + wiringPiSetup () ; + + printf ("Raspberry Pi - Quick2Wire Mainboard LED Blink Test\n") ; + + pinMode (LED, OUTPUT) ; + + for (;;) + { + digitalWrite (LED, HIGH) ; + delay (500) ; + digitalWrite (LED, LOW) ; + delay (500) ; + } + + return 0 ; +} diff --git a/examples/q2w/blink.sh b/examples/q2w/blink.sh new file mode 100755 index 0000000..2dee6c7 --- /dev/null +++ b/examples/q2w/blink.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# +# blink.sh: +# Standard "blink" program in wiringPi. Blinks an LED connected +# to the LED on the Quick2Wire board +# +# Copyright (c) 2012-2013 Gordon Henderson. +####################################################################### +# This file is part of wiringPi: +# https://projects.drogon.net/raspberry-pi/wiringpi/ +# +# wiringPi is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# wiringPi is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with wiringPi. If not, see . +####################################################################### + +# LED Pin - wiringPi pin 1 is BCM_GPIO 18. + +LED=1 + +gpio mode $LED out + +while true; do + gpio write $LED 1 + sleep 0.5 + gpio write $LED 0 + sleep 0.5 +done diff --git a/examples/q2w/bright.c b/examples/q2w/bright.c new file mode 100644 index 0000000..2318834 --- /dev/null +++ b/examples/q2w/bright.c @@ -0,0 +1,59 @@ +/* + * bright.c: + * Vary the Q2W LED brightness with the analog card + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#define LED 1 +#define Q2W_ABASE 120 + +int main (void) +{ + int value ; + +// Enable the on-goard GPIO + + wiringPiSetup () ; + +// Add in the pcf8591 on the q2w board + + pcf8591Setup (Q2W_ABASE, 0x48) ; + + printf ("Raspberry Pi - Quick2Wire Analog Test\n") ; + +// Setup the LED + + pinMode (LED, PWM_OUTPUT) ; + pwmWrite (LED, 0) ; + + for (;;) + { + value = analogRead (Q2W_ABASE + 0) ; + pwmWrite (LED, value * 4) ; + delay (10) ; + } + + return 0 ; +} diff --git a/examples/q2w/button.c b/examples/q2w/button.c new file mode 100644 index 0000000..1781f02 --- /dev/null +++ b/examples/q2w/button.c @@ -0,0 +1,63 @@ +/* + * button.c: + * Simple button test for the Quick2Wire interface board. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include + +#define BUTTON 0 +#define LED1 1 +#define LED2 7 + +int main (void) +{ + +// Enable the on-goard GPIO + + wiringPiSetup () ; + + printf ("Raspberry Pi - Quick2Wire Mainboard Button & LED Test\n") ; + + pinMode (BUTTON, INPUT) ; + pinMode (LED1, OUTPUT) ; + pinMode (LED2, OUTPUT) ; + + digitalWrite (LED1, HIGH) ; // On-board LED on + digitalWrite (LED2, LOW) ; // 2nd LED off + + for (;;) + { + if (digitalRead (BUTTON) == HIGH) // Swap LED states + { + digitalWrite (LED1, LOW) ; + digitalWrite (LED2, HIGH) ; + while (digitalRead (BUTTON) == HIGH) + delay (1) ; + digitalWrite (LED1, HIGH) ; + digitalWrite (LED2, LOW) ; + } + delay (1) ; + } + + return 0 ; +} diff --git a/examples/q2w/volts.c b/examples/q2w/volts.c new file mode 100644 index 0000000..e091093 --- /dev/null +++ b/examples/q2w/volts.c @@ -0,0 +1,62 @@ +/* + * volts.c: + * Read in all 4 analogs on the Q2W analog board. + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#define LED 1 +#define Q2W_ABASE 120 + +int main (void) +{ + int value, pin ; + +// Enable the on-goard GPIO + + wiringPiSetup () ; + + pinMode (LED, OUTPUT) ; // On-board LED + +// Add in the pcf8591 on the q2w board + + pcf8591Setup (Q2W_ABASE, 0x48) ; + + printf ("Raspberry Pi - Quick2Wire Voltmeter\n") ; + + for (;;) + { + for (pin = 0 ; pin < 4 ; ++pin) + { + value = analogRead (Q2W_ABASE + pin) ; + printf (" %5.2f", (double)value * 3.3 / 255.0) ; + } + printf ("\r") ; fflush (stdout) ; + + delay (100) ; + digitalWrite (LED, !digitalRead (LED)) ; // Flicker the LED + } + + return 0 ; +} diff --git a/examples/rht03.c b/examples/rht03.c old mode 100755 new mode 100644 diff --git a/gpio/extensions.c b/gpio/extensions.c index c08d1c1..dae1eac 100644 --- a/gpio/extensions.c +++ b/gpio/extensions.c @@ -36,10 +36,13 @@ #include #include +#include #include #include #include #include +#include +#include #include "extensions.h" @@ -116,10 +119,36 @@ static int doExtensionMcp23008 (char *progName, int pinBase, char *params) } +/* + * doExtensionMcp23016: + * MCP230016- 16-bit I2C GPIO expansion chip + * mcp23016:base:i2cAddr + ********************************************************************************* + */ + +static int doExtensionMcp23016 (char *progName, int pinBase, char *params) +{ + int i2c ; + + if ((params = extractInt (progName, params, &i2c)) == NULL) + return FALSE ; + + if ((i2c < 0x03) || (i2c > 0x77)) + { + fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + return FALSE ; + } + + mcp23016Setup (pinBase, i2c) ; + + return TRUE ; +} + + /* * doExtensionMcp23017: - * MCP23008 - 16-bit I2C GPIO expansion chip - * mcp23002:base:i2cAddr + * MCP230017- 16-bit I2C GPIO expansion chip + * mcp23017:base:i2cAddr ********************************************************************************* */ @@ -211,6 +240,7 @@ static int doExtensionMcp23s17 (char *progName, int pinBase, char *params) return TRUE ; } + /* * doExtensionSr595: * Shift Register 74x595 @@ -248,6 +278,58 @@ static int doExtensionSr595 (char *progName, int pinBase, char *params) } +/* + * doExtensionPcf8574: + * Digital IO (Crude!) + * pcf8574:base:i2cAddr + ********************************************************************************* + */ + +static int doExtensionPcf8574 (char *progName, int pinBase, char *params) +{ + int i2c ; + + if ((params = extractInt (progName, params, &i2c)) == NULL) + return FALSE ; + + if ((i2c < 0x03) || (i2c > 0x77)) + { + fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + return FALSE ; + } + + pcf8574Setup (pinBase, i2c) ; + + return TRUE ; +} + + +/* + * doExtensionPcf8591: + * Analog IO + * pcf8591:base:i2cAddr + ********************************************************************************* + */ + +static int doExtensionPcf8591 (char *progName, int pinBase, char *params) +{ + int i2c ; + + if ((params = extractInt (progName, params, &i2c)) == NULL) + return FALSE ; + + if ((i2c < 0x03) || (i2c > 0x77)) + { + fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + return FALSE ; + } + + pcf8591Setup (pinBase, i2c) ; + + return TRUE ; +} + + /* * Function list ********************************************************************************* @@ -256,10 +338,13 @@ static int doExtensionSr595 (char *progName, int pinBase, char *params) struct extensionFunctionStruct extensionFunctions [] = { { "mcp23008", &doExtensionMcp23008 }, + { "mcp23016", &doExtensionMcp23016 }, { "mcp23017", &doExtensionMcp23017 }, { "mcp23s08", &doExtensionMcp23s08 }, { "mcp23s17", &doExtensionMcp23s17 }, - { "sr595", &doExtensionSr595 }, + { "sr595", &doExtensionSr595 }, + { "pcf8574", &doExtensionPcf8574 }, + { "pcf8591", &doExtensionPcf8591 }, { NULL, NULL }, } ; diff --git a/gpio/gpio.c b/gpio/gpio.c index e7099c8..4724ab3 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -895,19 +895,13 @@ void doRead (int argc, char *argv []) void doAread (int argc, char *argv []) { - int pin, val ; - if (argc != 3) { fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ; exit (1) ; } - pin = atoi (argv [2]) ; - - val = analogRead (pin) ; - - printf ("%s\n", val == 0 ? "0" : "1") ; + printf ("%d\n", analogRead (atoi (argv [2]))) ; } diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 8ece853..f0120bb 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -47,8 +47,10 @@ SRC = wiringPi.c \ piHiPri.c piThread.c \ wiringPiSPI.c wiringPiI2C.c \ softPwm.c softTone.c \ - mcp23s08.c mcp23008.c \ - mcp23s17.c mcp23017.c sr595.c pcf8574.c \ + mcp23008.c mcp23016.c mcp23017.c \ + mcp23s08.c mcp23s17.c \ + sr595.c \ + pcf8574.c pcf8591.c \ mcp3002.c mcp4802.c mcp3422.c \ drc.c @@ -95,6 +97,7 @@ install-headers: @install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23008.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 mcp23016.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include @@ -103,6 +106,7 @@ install-headers: @install -m 0644 mcp3422.h $(DESTDIR)$(PREFIX)/include @install -m 0644 sr595.h $(DESTDIR)$(PREFIX)/include @install -m 0644 pcf8574.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 pcf8591.h $(DESTDIR)$(PREFIX)/include .PHONEY: install install: $(DYNAMIC) install-headers @@ -129,6 +133,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23008.h + @rm -f $(DESTDIR)$(PREFIX)/include/mcp23016.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h @@ -137,6 +142,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/mcp3422.h @rm -f $(DESTDIR)$(PREFIX)/include/sr595.h @rm -f $(DESTDIR)$(PREFIX)/include/pcf8574.h + @rm -f $(DESTDIR)$(PREFIX)/include/pcf8591.h @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.* @ldconfig @@ -156,12 +162,14 @@ wiringPiSPI.o: wiringPi.h wiringPiSPI.h wiringPiI2C.o: wiringPi.h wiringPiI2C.h softPwm.o: wiringPi.h softPwm.h softTone.o: wiringPi.h softTone.h -mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h mcp23008.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23008.h -mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h +mcp23016.o: wiringPi.h wiringPiI2C.h mcp23016.h mcp23016reg.h mcp23017.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23017.h +mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h +mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h sr595.o: wiringPi.h sr595.h pcf8574.o: wiringPi.h wiringPiI2C.h pcf8574.h +pcf8591.o: wiringPi.h wiringPiI2C.h pcf8591.h mcp3002.o: wiringPi.h wiringPiSPI.h mcp3002.h mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h diff --git a/wiringPi/mcp23016.c b/wiringPi/mcp23016.c new file mode 100644 index 0000000..e5cc632 --- /dev/null +++ b/wiringPi/mcp23016.c @@ -0,0 +1,164 @@ +/* + * mcp23016.c: + * Extend wiringPi with the MCP 23016 I2C GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "wiringPi.h" +#include "wiringPiI2C.h" +#include "mcp23016.h" + +#include "mcp23016reg.h" + + +/* + * myPinMode: + ********************************************************************************* + */ + +static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) +{ + int mask, old, reg ; + + pin -= node->pinBase ; + + if (pin < 8) // Bank A + reg = MCP23016_IODIR0 ; + else + { + reg = MCP23016_IODIR1 ; + pin &= 0x07 ; + } + + mask = 1 << pin ; + old = wiringPiI2CReadReg8 (node->fd, reg) ; + + if (mode == OUTPUT) + old &= (~mask) ; + else + old |= mask ; + + wiringPiI2CWriteReg8 (node->fd, reg, old) ; +} + + +/* + * myDigitalWrite: + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + int bit, old ; + + pin -= node->pinBase ; // Pin now 0-15 + + bit = 1 << (pin & 7) ; + + if (pin < 8) // Bank A + { + old = node->data2 ; + + if (value == LOW) + old &= (~bit) ; + else + old |= bit ; + + wiringPiI2CWriteReg8 (node->fd, MCP23016_GP0, old) ; + node->data2 = old ; + } + else // Bank B + { + old = node->data3 ; + + if (value == LOW) + old &= (~bit) ; + else + old |= bit ; + + wiringPiI2CWriteReg8 (node->fd, MCP23016_GP1, old) ; + node->data3 = old ; + } +} + + +/* + * myDigitalRead: + ********************************************************************************* + */ + +static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + int mask, value, gpio ; + + pin -= node->pinBase ; + + if (pin < 8) // Bank A + gpio = MCP23016_GP0 ; + else + { + gpio = MCP23016_GP1 ; + pin &= 0x07 ; + } + + mask = 1 << pin ; + value = wiringPiI2CReadReg8 (node->fd, gpio) ; + + if ((value & mask) == 0) + return LOW ; + else + return HIGH ; +} + + +/* + * mcp23016Setup: + * Create a new instance of an MCP23016 I2C GPIO interface. We know it + * has 16 pins, so all we need to know here is the I2C address and the + * user-defined pin base. + ********************************************************************************* + */ + +int mcp23016Setup (const int pinBase, const int i2cAddress) +{ + int fd ; + struct wiringPiNodeStruct *node ; + + if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) + return fd ; + + wiringPiI2CWriteReg8 (fd, MCP23016_IOCON0, IOCON_INIT) ; + wiringPiI2CWriteReg8 (fd, MCP23016_IOCON1, IOCON_INIT) ; + + node = wiringPiNewNode (pinBase, 16) ; + + node->fd = fd ; + node->pinMode = myPinMode ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->data2 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT0) ; + node->data3 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT1) ; + + return 0 ; +} diff --git a/wiringPi/mcp23016.h b/wiringPi/mcp23016.h new file mode 100644 index 0000000..f9b5cc5 --- /dev/null +++ b/wiringPi/mcp23016.h @@ -0,0 +1,33 @@ +/* + * mcp23016.h: + * Extend wiringPi with the MCP 23016 I2C GPIO expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp23016Setup (const int pinBase, const int i2cAddress) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp23016reg.h b/wiringPi/mcp23016reg.h new file mode 100644 index 0000000..9aea92d --- /dev/null +++ b/wiringPi/mcp23016reg.h @@ -0,0 +1,48 @@ +/* + * mcp23016: + * Copyright (c) 2012-2013 Gordon Henderson + * + * Header file for code using the MCP23016 GPIO expander + * chip. + *********************************************************************** + * 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 . + *********************************************************************** + */ + +// MCP23016 Registers + +#define MCP23016_GP0 0x00 +#define MCP23016_GP1 0x01 +#define MCP23016_OLAT0 0x02 +#define MCP23016_OLAT1 0x03 +#define MCP23016_IPOL0 0x04 +#define MCP23016_IPOL1 0x05 +#define MCP23016_IODIR0 0x06 +#define MCP23016_IODIR1 0x07 +#define MCP23016_INTCAP0 0x08 +#define MCP23016_INTCAP1 0x09 +#define MCP23016_IOCON0 0x0A +#define MCP23016_IOCON1 0x0B + +// Bits in the IOCON register + +#define IOCON_IARES 0x01 + +// Default initialisation mode + +#define IOCON_INIT 0 diff --git a/wiringPi/pcf8574.c b/wiringPi/pcf8574.c index ce9c533..c172d1c 100644 --- a/wiringPi/pcf8574.c +++ b/wiringPi/pcf8574.c @@ -1,6 +1,6 @@ /* * pcf8574.c: - * Extend wiringPi with the PFC8574 I2C GPIO expander chip + * Extend wiringPi with the PCF8574 I2C GPIO expander chip * Copyright (c) 2013 Gordon Henderson *********************************************************************** * This file is part of wiringPi: @@ -33,7 +33,7 @@ /* * myPinMode: - * The PFC8574 is an odd chip - the pins are effectively bi-directional, + * The PCF8574 is an odd chip - the pins are effectively bi-directional, * however the pins should be drven high when used as an input pin... * So, we're effectively copying digitalWrite... ********************************************************************************* @@ -100,7 +100,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) /* * pcf8574Setup: - * Create a new instance of a PFC8574 I2C GPIO interface. We know it + * Create a new instance of a PCF8574 I2C GPIO interface. We know it * has 8 pins, so all we need to know here is the I2C address and the * user-defined pin base. ********************************************************************************* @@ -116,11 +116,11 @@ int pcf8574Setup (const int pinBase, const int i2cAddress) node = wiringPiNewNode (pinBase, 8) ; - node->fd = fd ; - node->pinMode = myPinMode ; - node->digitalRead = myDigitalRead ; - node->digitalWrite = myDigitalWrite ; - node->data2 = wiringPiI2CRead (fd) ; + node->fd = fd ; + node->pinMode = myPinMode ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->data2 = wiringPiI2CRead (fd) ; return 0 ; } diff --git a/wiringPi/pcf8574.h b/wiringPi/pcf8574.h index 3cad2dd..8e2d818 100644 --- a/wiringPi/pcf8574.h +++ b/wiringPi/pcf8574.h @@ -1,6 +1,6 @@ /* - * pfc8574.h: - * Extend wiringPi with the PFC8574 I2C GPIO expander chip + * pcf8574.h: + * Extend wiringPi with the PCF8574 I2C GPIO expander chip * Copyright (c) 2013 Gordon Henderson *********************************************************************** * This file is part of wiringPi: @@ -26,7 +26,7 @@ extern "C" { #endif -extern int pfc8574Setup (const int pinBase, const int i2cAddress) ; +extern int pcf8574Setup (const int pinBase, const int i2cAddress) ; #ifdef __cplusplus } diff --git a/wiringPi/pcf8591.c b/wiringPi/pcf8591.c new file mode 100644 index 0000000..0c86056 --- /dev/null +++ b/wiringPi/pcf8591.c @@ -0,0 +1,90 @@ +/* + * pcf8591.c: + * Extend wiringPi with the PCF8591 I2C GPIO Analog expander chip + * The chip has 1 8-bit DAC and 4 x 8-bit ADCs + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include + +#include "wiringPi.h" +#include "wiringPiI2C.h" + +#include "pcf8591.h" + + +/* + * myAnalogWrite: + ********************************************************************************* + */ + +static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + unsigned char b [2] ; + b [0] = 0x40 ; + b [1] = value & 0xFF ; + write (node->fd, b, 2) ; +} + + +/* + * myAnalogRead: + ********************************************************************************* + */ + +static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) +{ + int x ; + + wiringPiI2CWrite (node->fd, 0x40 | ((pin - node->pinBase) & 3)) ; + + x = wiringPiI2CRead (node->fd) ; // Throw away the first read + x = wiringPiI2CRead (node->fd) ; + + return x ; +} + + +/* + * pcf8591Setup: + * Create a new instance of a PCF8591 I2C GPIO interface. We know it + * has 4 pins, (4 analog inputs and 1 analog output which we'll shadow + * input 0) so all we need to know here is the I2C address and the + * user-defined pin base. + ********************************************************************************* + */ + +int pcf8591Setup (const int pinBase, const int i2cAddress) +{ + int fd ; + struct wiringPiNodeStruct *node ; + + if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) + return fd ; + + node = wiringPiNewNode (pinBase, 4) ; + + node->fd = fd ; + node->analogRead = myAnalogRead ; + node->analogWrite = myAnalogWrite ; + + return 0 ; +} diff --git a/wiringPi/pcf8591.h b/wiringPi/pcf8591.h new file mode 100644 index 0000000..6b44ccf --- /dev/null +++ b/wiringPi/pcf8591.h @@ -0,0 +1,33 @@ +/* + * pcf8591.h: + * Extend wiringPi with the PCF8591 I2C GPIO Analog expander chip + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int pcf8591Setup (const int pinBase, const int i2cAddress) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 600c851..600b318 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -21,6 +21,9 @@ *********************************************************************** */ +#ifndef __WIRING_PI_H__ +#define __WIRING_PI_H__ + // Handy defines // Deprecated @@ -176,3 +179,5 @@ extern unsigned int micros (void) ; #ifdef __cplusplus } #endif + +#endif diff --git a/wiringPi/wiringPiI2C.c b/wiringPi/wiringPiI2C.c index d370b60..9e9ccf1 100644 --- a/wiringPi/wiringPiI2C.c +++ b/wiringPi/wiringPiI2C.c @@ -150,7 +150,7 @@ int wiringPiI2CReadReg16 (int fd, int reg) if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data)) return -1 ; else - return data.byte & 0xFF ; + return data.byte & 0xFFFF ; } From a7c21d3efc48c4040f336fc8ac01d9566a421fd5 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 20 May 2013 21:14:55 +0100 Subject: [PATCH 41/97] Bumped version number to 2.04 --- gpio/gpio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 4724ab3..5044997 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -48,7 +48,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "2.03" +#define VERSION "2.04" #define I2CDETECT "/usr/sbin/i2cdetect" static int wpMode ; From 41fb1b9e2a292e7ace582ea834a6241542b68a9b Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 20 May 2013 22:23:14 +0100 Subject: [PATCH 42/97] Gah. another small fix to the I2C code. --- gpio/gpio.c | 2 +- wiringPi/wiringPiI2C.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 5044997..871548e 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -48,7 +48,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "2.04" +#define VERSION "2.05" #define I2CDETECT "/usr/sbin/i2cdetect" static int wpMode ; diff --git a/wiringPi/wiringPiI2C.c b/wiringPi/wiringPiI2C.c index 9e9ccf1..c787bce 100644 --- a/wiringPi/wiringPiI2C.c +++ b/wiringPi/wiringPiI2C.c @@ -150,7 +150,7 @@ int wiringPiI2CReadReg16 (int fd, int reg) if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data)) return -1 ; else - return data.byte & 0xFFFF ; + return data.word & 0xFFFF ; } From 711017ec363de7bfc012e84afcbf2270ef147dc2 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 21 May 2013 22:38:16 +0100 Subject: [PATCH 43/97] More typos, added mcp3004/mcp3008 --- People | 3 ++ gpio/extensions.c | 130 +++++++++++++++++++++++++++++++++++++++++++++ gpio/gpio.c | 2 +- wiringPi/Makefile | 4 +- wiringPi/mcp3004.c | 73 +++++++++++++++++++++++++ wiringPi/mcp3004.h | 33 ++++++++++++ wiringPi/mcp3422.c | 5 +- wiringPi/mcp3422.h | 2 +- 8 files changed, 247 insertions(+), 5 deletions(-) create mode 100644 wiringPi/mcp3004.c create mode 100644 wiringPi/mcp3004.h diff --git a/People b/People index 92d1ea4..b339494 100644 --- a/People +++ b/People @@ -28,3 +28,6 @@ Xian Stannard Andre Crone Suggested the __WIRING_PI.H__ round wiringPi.h + +Rik Teerling + Pointing out some silly mistooks in the I2C code... diff --git a/gpio/extensions.c b/gpio/extensions.c index dae1eac..637dc8c 100644 --- a/gpio/extensions.c +++ b/gpio/extensions.c @@ -43,6 +43,10 @@ #include #include #include +#include +#include +#include +#include #include "extensions.h" @@ -330,6 +334,128 @@ static int doExtensionPcf8591 (char *progName, int pinBase, char *params) } +/* + * doExtensionMcp3002: + * Analog IO + * mcp3002:base:spiChan + ********************************************************************************* + */ + +static int doExtensionMcp3002 (char *progName, int pinBase, char *params) +{ + int spi ; + + if ((params = extractInt (progName, params, &spi)) == NULL) + return FALSE ; + + if ((spi < 0) || (spi > 1)) + { + fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + return FALSE ; + } + + mcp3002Setup (pinBase, spi) ; + + return TRUE ; +} + + +/* + * doExtensionMcp3004: + * Analog IO + * mcp3004:base:spiChan + ********************************************************************************* + */ + +static int doExtensionMcp3004 (char *progName, int pinBase, char *params) +{ + int spi ; + + if ((params = extractInt (progName, params, &spi)) == NULL) + return FALSE ; + + if ((spi < 0) || (spi > 1)) + { + fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + return FALSE ; + } + + mcp3004Setup (pinBase, spi) ; + + return TRUE ; +} + + +/* + * doExtensionMcp4802: + * Analog IO + * mcp4802:base:spiChan + ********************************************************************************* + */ + +static int doExtensionMcp4802 (char *progName, int pinBase, char *params) +{ + int spi ; + + if ((params = extractInt (progName, params, &spi)) == NULL) + return FALSE ; + + if ((spi < 0) || (spi > 1)) + { + fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + return FALSE ; + } + + mcp4802Setup (pinBase, spi) ; + + return TRUE ; +} + + +/* + * doExtensionMcp3422: + * Analog IO + * mcp3422:base:i2cAddr + ********************************************************************************* + */ + +static int doExtensionMcp3422 (char *progName, int pinBase, char *params) +{ + int i2c, sampleRate, gain ; + + if ((params = extractInt (progName, params, &i2c)) == NULL) + return FALSE ; + + if ((i2c < 0x03) || (i2c > 0x77)) + { + fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + return FALSE ; + } + + if ((params = extractInt (progName, params, &sampleRate)) == NULL) + return FALSE ; + + if ((sampleRate < 0) || (sampleRate > 3)) + { + fprintf (stderr, "%s: sample rate (%d) out of range\n", progName, sampleRate) ; + return FALSE ; + } + + if ((params = extractInt (progName, params, &gain)) == NULL) + return FALSE ; + + if ((gain < 0) || (gain > 3)) + { + fprintf (stderr, "%s: gain (%d) out of range\n", progName, gain) ; + return FALSE ; + } + + mcp3422Setup (pinBase, i2c, sampleRate, gain) ; + + return TRUE ; +} + + /* * Function list ********************************************************************************* @@ -345,6 +471,10 @@ struct extensionFunctionStruct extensionFunctions [] = { "sr595", &doExtensionSr595 }, { "pcf8574", &doExtensionPcf8574 }, { "pcf8591", &doExtensionPcf8591 }, + { "mcp3002", &doExtensionMcp3002 }, + { "mcp3004", &doExtensionMcp3004 }, + { "mcp4802", &doExtensionMcp4802 }, + { "mcp3422", &doExtensionMcp3422 }, { NULL, NULL }, } ; diff --git a/gpio/gpio.c b/gpio/gpio.c index 871548e..1933a69 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -48,7 +48,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "2.05" +#define VERSION "2.06" #define I2CDETECT "/usr/sbin/i2cdetect" static int wpMode ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index f0120bb..16bc476 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -51,7 +51,7 @@ SRC = wiringPi.c \ mcp23s08.c mcp23s17.c \ sr595.c \ pcf8574.c pcf8591.c \ - mcp3002.c mcp4802.c mcp3422.c \ + mcp3002.c mcp3004.c mcp4802.c mcp3422.c \ drc.c OBJ = $(SRC:.c=.o) @@ -102,6 +102,7 @@ install-headers: @install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp3002.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 mcp3004.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp4802.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp3422.h $(DESTDIR)$(PREFIX)/include @install -m 0644 sr595.h $(DESTDIR)$(PREFIX)/include @@ -138,6 +139,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h + @rm -f $(DESTDIR)$(PREFIX)/include/mcp3004.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp3422.h @rm -f $(DESTDIR)$(PREFIX)/include/sr595.h diff --git a/wiringPi/mcp3004.c b/wiringPi/mcp3004.c new file mode 100644 index 0000000..1c9357f --- /dev/null +++ b/wiringPi/mcp3004.c @@ -0,0 +1,73 @@ +/* + * mcp3004.c: + * Extend wiringPi with the MCP3004 SPI Analog to Digital convertor + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "mcp3004.h" + +/* + * myAnalogRead: + * Return the analog value of the given pin + ********************************************************************************* + */ + +static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) +{ + unsigned char spiData [2] ; + unsigned char chanBits ; + int chan = pin - node->pinBase ; + + chanBits = 0b11000000 | (chan << 3) ; + + spiData [0] = chanBits ; + spiData [1] = 0 ; + + wiringPiSPIDataRW (node->fd, spiData, 2) ; + + return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ; +} + + +/* + * mcp3004Setup: + * Create a new wiringPi device node for an mcp3004 on the Pi's + * SPI interface. + ********************************************************************************* + */ + +int mcp3004Setup (const int pinBase, int spiChannel) +{ + struct wiringPiNodeStruct *node ; + + if (wiringPiSPISetup (spiChannel, 1000000) < 0) + return -1 ; + + node = wiringPiNewNode (pinBase, 8) ; + + node->fd = spiChannel ; + node->analogRead = myAnalogRead ; + + return 0 ; +} diff --git a/wiringPi/mcp3004.h b/wiringPi/mcp3004.h new file mode 100644 index 0000000..a07c0bf --- /dev/null +++ b/wiringPi/mcp3004.h @@ -0,0 +1,33 @@ +/* + * mcp3004.c: + * Extend wiringPi with the MCP3004 SPI Analog to Digital convertor + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp3004Setup (int pinBase, int spiChannel) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp3422.c b/wiringPi/mcp3422.c index 8e26d76..4d07abc 100644 --- a/wiringPi/mcp3422.c +++ b/wiringPi/mcp3422.c @@ -1,6 +1,7 @@ /* * mcp3422.c: * Extend wiringPi with the MCP3422 I2C ADC chip + * Also works for the MCP3423 and MCP3224 (4 channel) chips * Copyright (c) 2013 Gordon Henderson *********************************************************************** * This file is part of wiringPi: @@ -98,7 +99,7 @@ int myAnalogRead (struct wiringPiNodeStruct *node, int chan) ********************************************************************************* */ -int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int gain) +int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain) { int fd ; struct wiringPiNodeStruct *node ; @@ -106,7 +107,7 @@ int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) return fd ; - node = wiringPiNewNode (pinBase, channels) ; + node = wiringPiNewNode (pinBase, 4) ; node->data0 = sampleRate ; node->data1 = gain ; diff --git a/wiringPi/mcp3422.h b/wiringPi/mcp3422.h index 8b4e350..bb4692d 100644 --- a/wiringPi/mcp3422.h +++ b/wiringPi/mcp3422.h @@ -36,7 +36,7 @@ extern "C" { #endif -extern int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int gain) ; +extern int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain) ; #ifdef __cplusplus } From 348bc739d1ba5d8e6bdd5995d91516f6d4f76df9 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 25 May 2013 13:02:02 +0100 Subject: [PATCH 44/97] Sorted a variable initialisation issue that was giving incorrect results with wiringPiISR. Added the ability to readall/reset from extension modules. Changed the build script to use /bin/sh rather than /bin/bash as it seems some installations don't have /bin/bash (or if they do, then the people who are telling me that ./build gives them command not found are wrong) --- build | 17 ++++---- gpio/gpio.1 | 12 +++++- gpio/gpio.c | 102 ++++++++++++++++++++++++++++++++++---------- wiringPi/wiringPi.c | 35 +++++++++------ wiringPi/wiringPi.h | 5 ++- 5 files changed, 122 insertions(+), 49 deletions(-) diff --git a/build b/build index 776c756..f578d4a 100755 --- a/build +++ b/build @@ -1,7 +1,6 @@ -#!/bin/bash +#!/bin/sh -check-make-ok() -{ +check_make_ok() { if [ $? != 0 ]; then echo "" echo "Make Failed..." @@ -51,26 +50,26 @@ fi cd wiringPi sudo make uninstall make - check-make-ok + check_make_ok sudo make install - check-make-ok + check_make_ok echo echo "WiringPi Devices Library" cd ../devLib sudo make uninstall make - check-make-ok + check_make_ok sudo make install - check-make-ok + check_make_ok echo echo "GPIO Utility" cd ../gpio make - check-make-ok + check_make_ok sudo make install - check-make-ok + check_make_ok # echo # echo "Examples" diff --git a/gpio/gpio.1 b/gpio/gpio.1 index ae4df4e..e1cba32 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -126,11 +126,19 @@ Output a table of all GPIO pins values. The values represent the actual values r if the pin is in input mode, or the last value written if the pin is in output mode. +The readall command is usable with an extension module (via the -x parameter), +but it's unable to determine pin modes or states, so will perform both a +digital and analog read on each pin in-turn. + .TP .B reset Resets the GPIO - As much as it's possible to do. All pins are set to input mode and all the internal pull-up/down resistors are disconnected (tristate mode). +The reset command is usable with an extension module (via the -x parameter), +but it's limited to turning the pin into input mode (if applicable) and +removing any pull up/down resistor. + .TP .B pwm Write a PWM value (0-1023) to the given pin. The pin needs to be put @@ -212,8 +220,8 @@ Change the PWM range register. The default is 1024. .B load i2c [baudrate] This loads the i2c or drivers into the kernel and changes the permissions on the associated /dev/ entries so that the current user has access to -them. Optionally it will set the I2C baudrate to that supplied (or as -close as the Pi can manage) The default speed is 100Kb/sec. +them. Optionally it will set the I2C baudrate to that supplied in Kb/sec +(or as close as the Pi can manage) The default speed is 100Kb/sec. .TP .B load spi [buffer size in KB] diff --git a/gpio/gpio.c b/gpio/gpio.c index 1933a69..7a6ef8e 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -48,7 +48,7 @@ extern int wiringPiDebug ; # define FALSE (1==2) #endif -#define VERSION "2.06" +#define VERSION "2.07" #define I2CDETECT "/usr/sbin/i2cdetect" static int wpMode ; @@ -239,6 +239,10 @@ static void doI2Cdetect (int argc, char *argv []) /* * doReadall: * Read all the GPIO pins + * We also want to use this to read the state of pins on an externally + * connected device, so we need to do some fiddling with the internal + * wiringPi node structures - since the gpio command can only use + * one external device at a time, we'll use that to our advantage... ********************************************************************************* */ @@ -268,27 +272,56 @@ static int wpiToPhys [64] = 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63 } ; + +/* + * doReadallExternal: + * A relatively crude way to read the pins on an external device. + * We don't know the input/output mode of pins, but we can tell + * if it's an analog pin or a digital one... + ********************************************************************************* + */ + +static void doReadallExternal (void) +{ + int pin ; + + printf ("+------+---------+--------+\n") ; + printf ("| Pin | Digital | Analog |\n") ; + printf ("+------+---------+--------+\n") ; + + for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin) + printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ; + + printf ("+------+---------+--------+\n") ; +} + + static void doReadall (void) { int pin ; - printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ; - printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ; - printf ("+----------+------+------+--------+------+-------+\n") ; - - for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective + if (wiringPiNodes != NULL) // External readall + doReadallExternal () ; + else { - if (wpiPinToGpio (pin) == -1) - continue ; + printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ; + printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ; + printf ("+----------+------+------+--------+------+-------+\n") ; - printf ("| %6d | %3d | %3d | %s | %s | %s |\n", - pin, wpiPinToGpio (pin), wpiToPhys [pin], - pinNames [pin], - alts [getAlt (pin)], - digitalRead (pin) == HIGH ? "High" : "Low ") ; + for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective + { + if (wpiPinToGpio (pin) == -1) + continue ; + + printf ("| %6d | %3d | %3d | %s | %s | %s |\n", + pin, wpiPinToGpio (pin), wpiToPhys [pin], + pinNames [pin], + alts [getAlt (pin)], + digitalRead (pin) == HIGH ? "High" : "Low ") ; + } + + printf ("+----------+------+------+--------+------+-------+\n") ; } - - printf ("+----------+------+------+--------+------+-------+\n") ; } @@ -614,6 +647,24 @@ void doUnexportall (char *progName) } +/* + * doResetExternal: + * Load readallExternal, we try to do this with an external device. + ********************************************************************************* + */ + +static void doResetExternal (void) +{ + int pin ; + + for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin) + { + pinMode (pin, INPUT) ; + pullUpDnControl (pin, PUD_OFF) ; + } +} + + /* * doReset: * Reset the GPIO pins - as much as we can do @@ -624,16 +675,21 @@ static void doReset (char *progName) { int pin ; - doUnexportall (progName) ; - - for (pin = 0 ; pin < 64 ; ++pin) + if (wiringPiNodes != NULL) // External reset + doResetExternal () ; + else { - if (wpiPinToGpio (pin) == -1) - continue ; + doUnexportall (progName) ; - digitalWrite (pin, LOW) ; - pinMode (pin, INPUT) ; - pullUpDnControl (pin, PUD_OFF) ; + for (pin = 0 ; pin < 64 ; ++pin) + { + if (wpiPinToGpio (pin) == -1) + continue ; + + digitalWrite (pin, LOW) ; + pinMode (pin, INPUT) ; + pullUpDnControl (pin, PUD_OFF) ; + } } } diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index b8e381a..e97a6d1 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -88,7 +88,7 @@ #define PI_GPIO_MASK (0xFFFFFFC0) -static struct wiringPiNodeStruct *wiringPiNodes = NULL ; +struct wiringPiNodeStruct *wiringPiNodes = NULL ; // BCM Magic @@ -213,7 +213,13 @@ int wiringPiReturnCodes = FALSE ; // sysFds: // Map a file descriptor from the /sys/class/gpio/gpioX/value -static int sysFds [64] ; +static int sysFds [64] = +{ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, +} ; // ISR Data @@ -833,7 +839,7 @@ void gpioClockSet (int pin, int freq) ********************************************************************************* */ -static struct wiringPiNodeStruct *wiringPiFindNode (int pin) +struct wiringPiNodeStruct *wiringPiFindNode (int pin) { struct wiringPiNodeStruct *node = wiringPiNodes ; @@ -1304,7 +1310,8 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) char c ; int bcmGpioPin ; - pin &= 63 ; + if ((pin < 0) || (pin > 63)) + return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ; /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED) return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ; @@ -1333,26 +1340,26 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) sprintf (pinS, "%d", bcmGpioPin) ; if ((pid = fork ()) < 0) // Fail - return pid ; + return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ; if (pid == 0) // Child, exec { execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ; - return -1 ; // Failure ... + return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ; } else // Parent, wait wait (NULL) ; } -// Now pre-open the /sys/class node - it may already be open if -// we are in Sys mode or if we call here twice, if-so, we'll close it. +// Now pre-open the /sys/class node - but it may already be open if +// we are in Sys mode... - if (sysFds [bcmGpioPin] != -1) - close (sysFds [bcmGpioPin]) ; - - sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ; - if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0) - return -1 ; + if (sysFds [bcmGpioPin] == -1) + { + sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ; + if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0) + return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ; + } // Clear any initial pending interrupt diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 600b318..84042e9 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -104,6 +104,8 @@ struct wiringPiNodeStruct struct wiringPiNodeStruct *next ; } ; +extern struct wiringPiNodeStruct *wiringPiNodes ; + // Function prototypes // c++ wrappers thanks to a comment by Nick Lott @@ -119,7 +121,8 @@ extern int wiringPiFailure (int fatal, const char *message, ...) ; // Core wiringPi functions -extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ; +extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ; +extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ; extern int wiringPiSetup (void) ; extern int wiringPiSetupSys (void) ; From 27698766b211f6bb5d1cb36a07d81339e5ba3338 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 27 Jun 2013 21:51:55 +0100 Subject: [PATCH 45/97] Fixed delayMicroseconds for more than 1 second. Added new code for max31855 --- gpio/Makefile | 2 +- gpio/extensions.c | 28 ++++++++++ gpio/gpio.c | 124 ++++++++------------------------------------ wiringPi/Makefile | 8 ++- wiringPi/wiringPi.c | 8 +-- 5 files changed, 62 insertions(+), 108 deletions(-) diff --git a/gpio/Makefile b/gpio/Makefile index 52b0150..883bfc2 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -35,7 +35,7 @@ LIBS = -lwiringPi -lwiringPiDev -lpthread -lm # May not need to alter anything below this line ############################################################################### -SRC = gpio.c extensions.c +SRC = gpio.c extensions.c readall.c OBJ = $(SRC:.c=.o) diff --git a/gpio/extensions.c b/gpio/extensions.c index 637dc8c..713fb93 100644 --- a/gpio/extensions.c +++ b/gpio/extensions.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -334,6 +335,32 @@ static int doExtensionPcf8591 (char *progName, int pinBase, char *params) } +/* + * doExtensionMax31855: + * Analog IO + * max31855:base:spiChan + ********************************************************************************* + */ + +static int doExtensionMax31855 (char *progName, int pinBase, char *params) +{ + int spi ; + + if ((params = extractInt (progName, params, &spi)) == NULL) + return FALSE ; + + if ((spi < 0) || (spi > 1)) + { + fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + return FALSE ; + } + + max31855Setup (pinBase, spi) ; + + return TRUE ; +} + + /* * doExtensionMcp3002: * Analog IO @@ -475,6 +502,7 @@ struct extensionFunctionStruct extensionFunctions [] = { "mcp3004", &doExtensionMcp3004 }, { "mcp4802", &doExtensionMcp4802 }, { "mcp3422", &doExtensionMcp3422 }, + { "max31855", &doExtensionMax31855 }, { NULL, NULL }, } ; diff --git a/gpio/gpio.c b/gpio/gpio.c index 7a6ef8e..9b16921 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -43,15 +43,18 @@ extern int wiringPiDebug ; +extern void doReadall (void) ; +extern void doReadallOld (void) ; + #ifndef TRUE # define TRUE (1==1) # define FALSE (1==2) #endif -#define VERSION "2.07" +#define VERSION "2.08" #define I2CDETECT "/usr/sbin/i2cdetect" -static int wpMode ; +int wpMode ; char *usage = "Usage: gpio -v\n" " gpio -h\n" @@ -236,95 +239,6 @@ static void doI2Cdetect (int argc, char *argv []) } -/* - * doReadall: - * Read all the GPIO pins - * We also want to use this to read the state of pins on an externally - * connected device, so we need to do some fiddling with the internal - * wiringPi node structures - since the gpio command can only use - * one external device at a time, we'll use that to our advantage... - ********************************************************************************* - */ - -static char *pinNames [] = -{ - "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7", - "SDA ", "SCL ", - "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ", - "TxD ", "RxD ", - "GPIO 8", "GPIO 9", "GPIO10", "GPIO11", -} ; - -static char *alts [] = -{ - "IN ", "OUT ", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3" -} ; - -static int wpiToPhys [64] = -{ - 11, 12, 13, 15, 16, 18, 22, 7, // 0...7 - 3, 5, // 8...9 - 24, 26, 19, 21, 23, // 10..14 - 8, 10, // 15..16 - 3, 4, 5, 6, // 17..20 - 0,0,0,0,0,0,0,0,0,0,0, // 20..31 - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47 - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63 -} ; - - -/* - * doReadallExternal: - * A relatively crude way to read the pins on an external device. - * We don't know the input/output mode of pins, but we can tell - * if it's an analog pin or a digital one... - ********************************************************************************* - */ - -static void doReadallExternal (void) -{ - int pin ; - - printf ("+------+---------+--------+\n") ; - printf ("| Pin | Digital | Analog |\n") ; - printf ("+------+---------+--------+\n") ; - - for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin) - printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ; - - printf ("+------+---------+--------+\n") ; -} - - -static void doReadall (void) -{ - int pin ; - - if (wiringPiNodes != NULL) // External readall - doReadallExternal () ; - else - { - printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ; - printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ; - printf ("+----------+------+------+--------+------+-------+\n") ; - - for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective - { - if (wpiPinToGpio (pin) == -1) - continue ; - - printf ("| %6d | %3d | %3d | %s | %s | %s |\n", - pin, wpiPinToGpio (pin), wpiToPhys [pin], - pinNames [pin], - alts [getAlt (pin)], - digitalRead (pin) == HIGH ? "High" : "Low ") ; - } - - printf ("+----------+------+------+--------+------+-------+\n") ; - } -} - - /* * doExports: * List all GPIO exports @@ -716,12 +630,15 @@ void doMode (int argc, char *argv []) mode = argv [3] ; /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; + else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ; else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ; + else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ; else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ; else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ; else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ; else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; + else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ; else { fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ; @@ -1268,18 +1185,19 @@ int main (int argc, char *argv []) // Pi Specifics - else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ; - else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ; - else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ; - else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ; - else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; - else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; - else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ; - else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ; - else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ; - else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ; - else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ; - else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ; + else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ; + else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ; + else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ; + else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ; + else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; + else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; + else if (strcasecmp (argv [1], "oreadall" ) == 0) doReadallOld () ; + else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ; + else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ; + else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ; + else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ; + else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ; + else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ; else { fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 16bc476..6f62070 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -35,7 +35,8 @@ DYNAMIC=libwiringPi.so.$(VERSION) DEBUG = -O2 CC = gcc INCLUDE = -I. -CFLAGS = $(DEBUG) -Wformat=2 -Wall $(INCLUDE) -Winline -pipe -fPIC +DEFS = -D_GNU_SOURCE +CFLAGS = $(DEBUG) $(DEFS) -Wformat=2 -Wall -Winline $(INCLUDE) -pipe -fPIC LIBS = @@ -52,6 +53,7 @@ SRC = wiringPi.c \ sr595.c \ pcf8574.c pcf8591.c \ mcp3002.c mcp3004.c mcp4802.c mcp3422.c \ + max31855.c \ drc.c OBJ = $(SRC:.c=.o) @@ -101,6 +103,7 @@ install-headers: @install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 max31855.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp3002.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp3004.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp4802.h $(DESTDIR)$(PREFIX)/include @@ -138,6 +141,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h + @rm -f $(DESTDIR)$(PREFIX)/include/max31855.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp3004.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h @@ -173,6 +177,8 @@ sr595.o: wiringPi.h sr595.h pcf8574.o: wiringPi.h wiringPiI2C.h pcf8574.h pcf8591.o: wiringPi.h wiringPiI2C.h pcf8591.h mcp3002.o: wiringPi.h wiringPiSPI.h mcp3002.h +mcp3004.o: wiringPi.h wiringPiSPI.h mcp3004.h mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h +max31855.o: wiringPi.h wiringPiSPI.h max31855.h drc.o: wiringPi.h wiringSerial.h drc.h diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index e97a6d1..ba61d9f 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1448,6 +1448,8 @@ void delayMicrosecondsHard (unsigned int howLong) void delayMicroseconds (unsigned int howLong) { struct timespec sleeper ; + unsigned int uSecs = howLong % 1000000 ; + unsigned int wSecs = howLong / 1000000 ; /**/ if (howLong == 0) return ; @@ -1455,8 +1457,8 @@ void delayMicroseconds (unsigned int howLong) delayMicrosecondsHard (howLong) ; else { - sleeper.tv_sec = 0 ; - sleeper.tv_nsec = (long)(howLong * 1000) ; + sleeper.tv_sec = wSecs ; + sleeper.tv_nsec = (long)(uSecs * 1000L) ; nanosleep (&sleeper, NULL) ; } } @@ -1539,7 +1541,7 @@ int wiringPiSetup (void) // Open the master /dev/memory device - if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0) + if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; // GPIO: From 4a379ec085c6a9ef400839e99c9af885830899e0 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 27 Jun 2013 21:52:42 +0100 Subject: [PATCH 46/97] Properly added the max31855 files now --- wiringPi/max31855.c | 81 +++++++++++++++++++++++++++++++++++++++++++++ wiringPi/max31855.h | 33 ++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 wiringPi/max31855.c create mode 100644 wiringPi/max31855.h diff --git a/wiringPi/max31855.c b/wiringPi/max31855.c new file mode 100644 index 0000000..2185839 --- /dev/null +++ b/wiringPi/max31855.c @@ -0,0 +1,81 @@ +/* + * max31855.c: + * Extend wiringPi with the max31855 SPI Analog to Digital convertor + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "max31855.h" + +/* + * myAnalogRead: + * Return the analog value of the given pin + * Note: The chip really only has one read "channel", but we're faking it + * here so we can read the error registers. Channel 0 will be the data + * channel, and 1 is the error register code. + * Note: Temperature returned is temp in C * 4, so divide result by 4 + ********************************************************************************* + */ + +static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) +{ + unsigned int spiData ; + int temp ; + int chan = pin - node->pinBase ; + + wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ; + + if (chan == 0) // Read temp in C + { + spiData >>= 18 ; + temp = spiData & 0x3FFF ; // Bottom 13 bits + if ((spiData & 0x2000) != 0) // Negative + temp = -temp ; + return temp ; + } + else // Return error bits + return spiData & 0x7 ; +} + + +/* + * max31855Setup: + * Create a new wiringPi device node for an max31855 on the Pi's + * SPI interface. + ********************************************************************************* + */ + +int max31855Setup (const int pinBase, int spiChannel) +{ + struct wiringPiNodeStruct *node ; + + if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi + return -1 ; + + node = wiringPiNewNode (pinBase, 2) ; + + node->fd = spiChannel ; + node->analogRead = myAnalogRead ; + + return 0 ; +} diff --git a/wiringPi/max31855.h b/wiringPi/max31855.h new file mode 100644 index 0000000..385c4bd --- /dev/null +++ b/wiringPi/max31855.h @@ -0,0 +1,33 @@ +/* + * max31855.c: + * Extend wiringPi with the MAX31855 SPI Thermocouple driver + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int max31855Setup (int pinBase, int spiChannel) ; + +#ifdef __cplusplus +} +#endif From 02a3bd8d8f2ae5c873e63875a8faef5b627f9db6 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 27 Jun 2013 21:57:09 +0100 Subject: [PATCH 47/97] Readall command in gpio changed. --- gpio/readall.c | 295 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 295 insertions(+) create mode 100644 gpio/readall.c diff --git a/gpio/readall.c b/gpio/readall.c new file mode 100644 index 0000000..0237110 --- /dev/null +++ b/gpio/readall.c @@ -0,0 +1,295 @@ +/* + * readall.c: + * The readall functions - getting a bit big, so split them out. + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern int wpMode ; + +/* + * doReadallExternal: + * A relatively crude way to read the pins on an external device. + * We don't know the input/output mode of pins, but we can tell + * if it's an analog pin or a digital one... + ********************************************************************************* + */ + +static void doReadallExternal (void) +{ + int pin ; + + printf ("+------+---------+--------+\n") ; + printf ("| Pin | Digital | Analog |\n") ; + printf ("+------+---------+--------+\n") ; + + for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin) + printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ; + + printf ("+------+---------+--------+\n") ; +} + + +/* + * doReadall: + * Read all the GPIO pins + * We also want to use this to read the state of pins on an externally + * connected device, so we need to do some fiddling with the internal + * wiringPi node structures - since the gpio command can only use + * one external device at a time, we'll use that to our advantage... + ********************************************************************************* + */ + +static char *pinNames [] = +{ + "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7", + "SDA ", "SCL ", + "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ", + "TxD ", "RxD ", + "GPIO 8", "GPIO 9", "GPIO10", "GPIO11", +} ; + +static char *alts [] = +{ + "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3" +} ; + +static int wpiToPhys [64] = +{ + 11, 12, 13, 15, 16, 18, 22, 7, // 0...7 + 3, 5, // 8...9 + 24, 26, 19, 21, 23, // 10..14 + 8, 10, // 15..16 + 3, 4, 5, 6, // 17..20 + 0,0,0,0,0,0,0,0,0,0,0, // 20..31 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63 +} ; + +// The other mappings needed are in wiringPi.c + +static int physToWpi [64] = +{ + -1, // 0 + -1, -1, // 1, 2 + 8, -1, + 9, -1, + 7, 15, + -1, 16, + 0, 1, + 2, -1, + 3, 4, + -1, 5, + 12, -1, + 13, 6, + 14, 10, + -1, 11, // 25, 26 + +// Padding: + + -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 +} ; + +static char *physNames [64] = +{ + NULL, + + "3.3v", "5v", + "SDA", "5V", + "SCL", "0v", + "GPIO7", "TxD", + "0v", "RxD", + "GPIO0", "GPIO1", + "GPIO2", "0v", + "GPIO3", "GPIO4", + "3.3v", "GPIO5", + "MOSI", "0v", + "MISO", "GPIO6", + "SCLK", "CE1", + "0v", "CE1", + + NULL,NULL,NULL,NULL,NULL, + NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, + NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, +} ; + +static void readallPhys (int physPin) +{ + int pin ; + + /**/ if (wpMode == WPI_MODE_GPIO) + { + if (physPinToGpio (physPin) == -1) + printf (" | ") ; + else + printf (" | %3d", physPinToGpio (physPin)) ; + } + else if (wpMode != WPI_MODE_PHYS) + { + if (physToWpi [physPin] == -1) + printf (" | ") ; + else + printf (" | %3d", physToWpi [physPin]) ; + } + + printf (" | %5s", physNames [physPin]) ; + + if (physToWpi [physPin] == -1) + printf (" | | ") ; + else + { + /**/ if (wpMode == WPI_MODE_GPIO) + pin = physPinToGpio (physPin) ; + else if (wpMode == WPI_MODE_PHYS) + pin = physPin ; + else + pin = physToWpi [physPin] ; + + printf (" | %4s", alts [getAlt (pin)]) ; + printf (" | %s", (digitalRead (pin) == 0) ? "Hi" : "Lo") ; + } + +// Pin numbers: + + printf (" | %2d", physPin) ; + ++physPin ; + printf (" || %-2d", physPin) ; + +// Same, reversed + + if (physToWpi [physPin] == -1) + printf (" | | ") ; + else + { + /**/ if (wpMode == WPI_MODE_GPIO) + pin = physPinToGpio (physPin) ; + else if (wpMode == WPI_MODE_PHYS) + pin = physPin ; + else + pin = physToWpi [physPin] ; + + printf (" | %s", (digitalRead (pin) == 0) ? "Hi" : "Lo") ; + printf (" | %-4s", alts [getAlt (pin)]) ; + } + + printf (" | %-5s", physNames [physPin]) ; + + /**/ if (wpMode == WPI_MODE_GPIO) + { + if (physPinToGpio (physPin) == -1) + printf (" | ") ; + else + printf (" | %-3d", physPinToGpio (physPin)) ; + } + else if (wpMode != WPI_MODE_PHYS) + { + if (physToWpi [physPin] == -1) + printf (" | ") ; + else + printf (" | %-3d", physToWpi [physPin]) ; + } + + printf (" |\n") ; +} + + +void doReadall (void) +{ + int pin ; + + if (wiringPiNodes != NULL) // External readall + { + doReadallExternal () ; + return ; + } + + /**/ if (wpMode == WPI_MODE_GPIO) + { + printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ; + printf (" | BCM | Name | Mode | Val| Physical |Val | Mode | Name | BCM |\n") ; + printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ; + for (pin = 1 ; pin <= 26 ; pin += 2) + readallPhys (pin) ; + printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ; + } + else if (wpMode == WPI_MODE_PHYS) + { + printf (" +-------+------+----+-Rev%d-----+----+------+-------+\n", piBoardRev ()) ; + printf (" | Name | Mode | Val| Physical |Val | Mode | Name |\n") ; + printf (" +-------+------+----+----++----+----+------+-------+\n") ; + for (pin = 1 ; pin <= 26 ; pin += 2) + readallPhys (pin) ; + printf (" +-------+------+----+----++----+----+------+-------+\n") ; + } + else // wiringPi + { + printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ; + printf (" | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi |\n") ; + printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ; + for (pin = 1 ; pin <= 26 ; pin += 2) + readallPhys (pin) ; + printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ; + } +} + + +void doReadallOld (void) +{ + int pin ; + + if (wiringPiNodes != NULL) // External readall + { + doReadallExternal () ; + return ; + } + + printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ; + printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ; + printf ("+----------+------+------+--------+------+-------+\n") ; + + for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective + { + if (wpiPinToGpio (pin) == -1) + continue ; + + printf ("| %6d | %3d | %3d | %s | %-4s | %-4s |\n", + pin, wpiPinToGpio (pin), wpiToPhys [pin], + pinNames [pin], + alts [getAlt (pin)], + digitalRead (pin) == HIGH ? "High" : "Low ") ; + } + + printf ("+----------+------+------+--------+------+-------+\n") ; +} From 45bfe43c1e8fe7e51a410403fe628c53d7a5e387 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 30 Jun 2013 11:55:26 +0100 Subject: [PATCH 48/97] Typo in mcp3002.c --- wiringPi/mcp3002.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wiringPi/mcp3002.c b/wiringPi/mcp3002.c index 2e7d5cf..5d44940 100644 --- a/wiringPi/mcp3002.c +++ b/wiringPi/mcp3002.c @@ -55,7 +55,7 @@ static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) /* * mcp3002Setup: - * Create a new wiringPi device node for an mcp2003 on the Pi's + * Create a new wiringPi device node for an mcp3002 on the Pi's * SPI interface. ********************************************************************************* */ From 4d43c8cdca06a03e7e6c051ef0b7a5180da17467 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 14 Jul 2013 12:28:12 +0100 Subject: [PATCH 49/97] gpio Makefile changed to add PREFIX & DESTDIR mcp3004.c bugfixed. It works now! --- gpio/Makefile | 20 +++++++++++--------- wiringPi/mcp3004.c | 15 +++++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/gpio/Makefile b/gpio/Makefile index 883bfc2..b4519fa 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -22,14 +22,16 @@ # along with wiringPi. If not, see . ################################################################################# +DESTDIR=/usr +PREFIX=/local #DEBUG = -g -O0 DEBUG = -O2 CC = gcc -INCLUDE = -I/usr/local/include +INCLUDE = -I$(DESTDIR)$(PREFIX)/usr/local/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe -LDFLAGS = -L/usr/local/lib +LDFLAGS = -L$(DESTDIR)$(PREFIX)/lib LIBS = -lwiringPi -lwiringPiDev -lpthread -lm # May not need to alter anything below this line @@ -62,17 +64,17 @@ tags: $(SRC) .PHONEY: install install: @echo "[Install]" - @cp gpio /usr/local/bin - @chown root.root /usr/local/bin/gpio - @chmod 4755 /usr/local/bin/gpio - @mkdir -p /usr/local/man/man1 - @cp gpio.1 /usr/local/man/man1 + @cp gpio $(DESTDIR)$(PREFIX)/bin + @chown root.root $(DESTDIR)$(PREFIX)/bin/gpio + @chmod 4755 $(DESTDIR)$(PREFIX)/bin/gpio + @mkdir -p $(DESTDIR)$(PREFIX)/man/man1 + @cp gpio.1 $(DESTDIR)$(PREFIX)/man/man1 .PHONEY: uninstall uninstall: @echo "[UnInstall]" - @rm -f /usr/local/bin/gpio - @rm -f /usr/local/man/man1/gpio.1 + @rm -f $(DESTDIR)$(PREFIX)/bin/gpio + @rm -f $(DESTDIR)$(PREFIX)/man/man1/gpio.1 .PHONEY: depend depend: diff --git a/wiringPi/mcp3004.c b/wiringPi/mcp3004.c index 1c9357f..82c73dd 100644 --- a/wiringPi/mcp3004.c +++ b/wiringPi/mcp3004.c @@ -2,6 +2,8 @@ * mcp3004.c: * Extend wiringPi with the MCP3004 SPI Analog to Digital convertor * Copyright (c) 2012-2013 Gordon Henderson + * + * Thanks also to "ShorTie" on IRC for some remote debugging help! *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -35,18 +37,19 @@ static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) { - unsigned char spiData [2] ; + unsigned char spiData [3] ; unsigned char chanBits ; int chan = pin - node->pinBase ; - chanBits = 0b11000000 | (chan << 3) ; + chanBits = 0b10000000 | (chan << 4) ; - spiData [0] = chanBits ; - spiData [1] = 0 ; + spiData [0] = 1 ; // Start bit + spiData [1] = chanBits ; + spiData [2] = 0 ; - wiringPiSPIDataRW (node->fd, spiData, 2) ; + wiringPiSPIDataRW (node->fd, spiData, 3) ; - return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ; + return ((spiData [1] << 8) | spiData [2]) & 0x3FF ; } From 27afc017b9d8f08f2f67b990c078d3127f7c4ef1 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 16 Jul 2013 10:37:26 +0100 Subject: [PATCH 50/97] Added in a max5322 SPI D to A chip --- gpio/extensions.c | 28 ++++++++++++++++ wiringPi/Makefile | 5 ++- wiringPi/max5322.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++ wiringPi/max5322.h | 33 ++++++++++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 wiringPi/max5322.c create mode 100644 wiringPi/max5322.h diff --git a/gpio/extensions.c b/gpio/extensions.c index 713fb93..8be0ec0 100644 --- a/gpio/extensions.c +++ b/gpio/extensions.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -413,6 +414,32 @@ static int doExtensionMcp3004 (char *progName, int pinBase, char *params) } +/* + * doExtensionMax5322: + * Analog O + * max5322:base:spiChan + ********************************************************************************* + */ + +static int doExtensionMax5322 (char *progName, int pinBase, char *params) +{ + int spi ; + + if ((params = extractInt (progName, params, &spi)) == NULL) + return FALSE ; + + if ((spi < 0) || (spi > 1)) + { + fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + return FALSE ; + } + + max5322Setup (pinBase, spi) ; + + return TRUE ; +} + + /* * doExtensionMcp4802: * Analog IO @@ -503,6 +530,7 @@ struct extensionFunctionStruct extensionFunctions [] = { "mcp4802", &doExtensionMcp4802 }, { "mcp3422", &doExtensionMcp3422 }, { "max31855", &doExtensionMax31855 }, + { "max5322", &doExtensionMax5322 }, { NULL, NULL }, } ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 6f62070..05dedec 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -53,7 +53,7 @@ SRC = wiringPi.c \ sr595.c \ pcf8574.c pcf8591.c \ mcp3002.c mcp3004.c mcp4802.c mcp3422.c \ - max31855.c \ + max31855.c max5322.c \ drc.c OBJ = $(SRC:.c=.o) @@ -104,6 +104,7 @@ install-headers: @install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include @install -m 0644 max31855.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 max5322.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp3002.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp3004.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp4802.h $(DESTDIR)$(PREFIX)/include @@ -142,6 +143,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h @rm -f $(DESTDIR)$(PREFIX)/include/max31855.h + @rm -f $(DESTDIR)$(PREFIX)/include/max5322.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp3004.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h @@ -181,4 +183,5 @@ mcp3004.o: wiringPi.h wiringPiSPI.h mcp3004.h mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h max31855.o: wiringPi.h wiringPiSPI.h max31855.h +max5322.o: wiringPi.h wiringPiSPI.h max5322.h drc.o: wiringPi.h wiringSerial.h drc.h diff --git a/wiringPi/max5322.c b/wiringPi/max5322.c new file mode 100644 index 0000000..c0b6264 --- /dev/null +++ b/wiringPi/max5322.c @@ -0,0 +1,84 @@ +/* + * max5322.c: + * Extend wiringPi with the MAX5322 SPI Digital to Analog convertor + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "max5322.h" + +/* + * myAnalogWrite: + * Write analog value on the given pin + ********************************************************************************* + */ + +static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + unsigned char spiData [2] ; + unsigned char chanBits, dataBits ; + int chan = pin - node->pinBase ; + + if (chan == 0) + chanBits = 0b01000000 ; + else + chanBits = 0b01010000 ; + + chanBits |= ((value >> 4) & 0x0F) ; + dataBits = ((value << 4) & 0xF0) ; + + spiData [0] = chanBits ; + spiData [1] = dataBits ; + + wiringPiSPIDataRW (node->fd, spiData, 2) ; +} + +/* + * max5322Setup: + * Create a new wiringPi device node for an max5322 on the Pi's + * SPI interface. + ********************************************************************************* + */ + +int max5322Setup (const int pinBase, int spiChannel) +{ + struct wiringPiNodeStruct *node ; + unsigned char spiData [2] ; + + if (wiringPiSPISetup (spiChannel, 8000000) < 0) // 10MHz Max + return -1 ; + + node = wiringPiNewNode (pinBase, 2) ; + + node->fd = spiChannel ; + node->analogWrite = myAnalogWrite ; + +// Enable both DACs + + spiData [0] = 0b11100000 ; + spiData [1] = 0 ; + + wiringPiSPIDataRW (node->fd, spiData, 2) ; + + return 0 ; +} diff --git a/wiringPi/max5322.h b/wiringPi/max5322.h new file mode 100644 index 0000000..a217cf8 --- /dev/null +++ b/wiringPi/max5322.h @@ -0,0 +1,33 @@ +/* + * max5322.h: + * Extend wiringPi with the MAX5322 SPI Digital to Analog convertor + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int max5322Setup (int pinBase, int spiChannel) ; + +#ifdef __cplusplus +} +#endif From 8c8578a26b17a95e388e00d48cd1a8a042ffc025 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 23 Jul 2013 09:19:34 +0100 Subject: [PATCH 51/97] Reverted gpio readall to older version - new version is gpio nreadall fixed the max5322 code. --- gpio/gpio.c | 6 +++--- gpio/readall.c | 6 +++--- wiringPi/max5322.c | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 9b16921..dced95c 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -51,7 +51,7 @@ extern void doReadallOld (void) ; # define FALSE (1==2) #endif -#define VERSION "2.08" +#define VERSION "2.09" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; @@ -1190,8 +1190,8 @@ int main (int argc, char *argv []) else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ; else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ; else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; - else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; - else if (strcasecmp (argv [1], "oreadall" ) == 0) doReadallOld () ; + else if (strcasecmp (argv [1], "readall" ) == 0) doReadallOld () ; + else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ; else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ; else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ; else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ; diff --git a/gpio/readall.c b/gpio/readall.c index 0237110..8fc9f88 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -137,7 +137,7 @@ static char *physNames [64] = "3.3v", "GPIO5", "MOSI", "0v", "MISO", "GPIO6", - "SCLK", "CE1", + "SCLK", "CE0", "0v", "CE1", NULL,NULL,NULL,NULL,NULL, @@ -178,7 +178,7 @@ static void readallPhys (int physPin) pin = physToWpi [physPin] ; printf (" | %4s", alts [getAlt (pin)]) ; - printf (" | %s", (digitalRead (pin) == 0) ? "Hi" : "Lo") ; + printf (" | %s", (digitalRead (pin) == LOW) ? "Lo" : "Hi") ; } // Pin numbers: @@ -200,7 +200,7 @@ static void readallPhys (int physPin) else pin = physToWpi [physPin] ; - printf (" | %s", (digitalRead (pin) == 0) ? "Hi" : "Lo") ; + printf (" | %s", (digitalRead (pin) == LOW) ? "Lo" : "Hi") ; printf (" | %-4s", alts [getAlt (pin)]) ; } diff --git a/wiringPi/max5322.c b/wiringPi/max5322.c index c0b6264..b7cd6a9 100644 --- a/wiringPi/max5322.c +++ b/wiringPi/max5322.c @@ -44,8 +44,8 @@ static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value) else chanBits = 0b01010000 ; - chanBits |= ((value >> 4) & 0x0F) ; - dataBits = ((value << 4) & 0xF0) ; + chanBits |= ((value >> 12) & 0x0F) ; + dataBits = ((value ) & 0xFF) ; spiData [0] = chanBits ; spiData [1] = dataBits ; From d24cf0630bb2653e95b93b89a12412c9bcf9a27b Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 23 Jul 2013 17:52:27 +0100 Subject: [PATCH 52/97] Added in the SN3218 LED controller IC - as used in the PiGlow so write a little test program with it too. Updated gpio to use the sn3218 too. --- examples/Makefile | 6 +++- examples/piglow.c | 51 ++++++++++++++++++++++++++++++++ gpio/extensions.c | 16 ++++++++++ wiringPi/Makefile | 4 +++ wiringPi/sn3218.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++ wiringPi/sn3218.h | 33 +++++++++++++++++++++ 6 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 examples/piglow.c create mode 100644 wiringPi/sn3218.c create mode 100644 wiringPi/sn3218.h diff --git a/examples/Makefile b/examples/Makefile index 8cc256f..8848b33 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -42,7 +42,7 @@ SRC = blink.c blink8.c blink12.c \ nes.c \ softPwm.c softTone.c \ delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \ - rht03.c + rht03.c piglow.c OBJ = $(SRC:.c=.o) @@ -139,6 +139,10 @@ ds1302: ds1302.o @echo [link] @$(CC) -o $@ ds1302.o $(LDFLAGS) $(LDLIBS) +piglow: piglow.o + @echo [link] + @$(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS) + .c.o: @echo [CC] $< diff --git a/examples/piglow.c b/examples/piglow.c new file mode 100644 index 0000000..d3fe4b9 --- /dev/null +++ b/examples/piglow.c @@ -0,0 +1,51 @@ +/* + * piglow.c: + * Very simple demonstration of the PiGlow board. + * This uses the SN3218 directly - soon there will be a new PiGlow + * devLib device which will handle the PiGlow board on a more easy + * to use manner... + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include + +#define LED_BASE 533 + +int main (void) +{ + int i, j ; + + wiringPiSetupSys () ; + + sn3218Setup (LED_BASE) ; + + for (;;) + { + for (i = 0 ; i < 256 ; ++i) + for (j = 0 ; j < 18 ; ++j) + analogWrite (LED_BASE + j, i) ; + + for (i = 255 ; i >= 0 ; --i) + for (j = 0 ; j < 18 ; ++j) + analogWrite (LED_BASE + j, i) ; + } +} diff --git a/gpio/extensions.c b/gpio/extensions.c index 8be0ec0..c7e5308 100644 --- a/gpio/extensions.c +++ b/gpio/extensions.c @@ -49,6 +49,7 @@ #include #include #include +#include #include "extensions.h" @@ -466,6 +467,20 @@ static int doExtensionMcp4802 (char *progName, int pinBase, char *params) } +/* + * doExtensionSn3218: + * Analog Output (LED Driver) + * sn3218:base + ********************************************************************************* + */ + +static int doExtensionSn3218 (char *progName, int pinBase, char *params) +{ + sn3218Setup (pinBase) ; + return TRUE ; +} + + /* * doExtensionMcp3422: * Analog IO @@ -531,6 +546,7 @@ struct extensionFunctionStruct extensionFunctions [] = { "mcp3422", &doExtensionMcp3422 }, { "max31855", &doExtensionMax31855 }, { "max5322", &doExtensionMax5322 }, + { "sn3218", &doExtensionSn3218 }, { NULL, NULL }, } ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 05dedec..ed4f6be 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -54,6 +54,7 @@ SRC = wiringPi.c \ pcf8574.c pcf8591.c \ mcp3002.c mcp3004.c mcp4802.c mcp3422.c \ max31855.c max5322.c \ + sn3218.c \ drc.c OBJ = $(SRC:.c=.o) @@ -112,6 +113,7 @@ install-headers: @install -m 0644 sr595.h $(DESTDIR)$(PREFIX)/include @install -m 0644 pcf8574.h $(DESTDIR)$(PREFIX)/include @install -m 0644 pcf8591.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 sn3218.h $(DESTDIR)$(PREFIX)/include .PHONEY: install install: $(DYNAMIC) install-headers @@ -151,6 +153,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/sr595.h @rm -f $(DESTDIR)$(PREFIX)/include/pcf8574.h @rm -f $(DESTDIR)$(PREFIX)/include/pcf8591.h + @rm -f $(DESTDIR)$(PREFIX)/include/sn3218.h @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.* @ldconfig @@ -184,4 +187,5 @@ mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h max31855.o: wiringPi.h wiringPiSPI.h max31855.h max5322.o: wiringPi.h wiringPiSPI.h max5322.h +sn3218.o: wiringPi.h wiringPiI2C.h sn3218.h drc.o: wiringPi.h wiringSerial.h drc.h diff --git a/wiringPi/sn3218.c b/wiringPi/sn3218.c new file mode 100644 index 0000000..7ceb156 --- /dev/null +++ b/wiringPi/sn3218.c @@ -0,0 +1,75 @@ +/* + * sn3218.c: + * Extend wiringPi with the SN3218 I2C LEd Driver + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "sn3218.h" + +/* + * myAnalogWrite: + * Write analog value on the given pin + ********************************************************************************* + */ + +static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + int fd = node->fd ; + int chan = 0x01 + (pin - node->pinBase) ; + + wiringPiI2CWriteReg8 (fd, chan, value & 0xFF) ; // Value + wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update +} + +/* + * sn3218Setup: + * Create a new wiringPi device node for an sn3218 on the Pi's + * SPI interface. + ********************************************************************************* + */ + +int sn3218Setup (const int pinBase) +{ + int fd ; + struct wiringPiNodeStruct *node ; + + if ((fd = wiringPiI2CSetup (0x54)) < 0) + return fd ; + +// Setup the chip - initialise all 18 LEDs to off + +//wiringPiI2CWriteReg8 (fd, 0x17, 0) ; // Reset + wiringPiI2CWriteReg8 (fd, 0x00, 1) ; // Not Shutdown + wiringPiI2CWriteReg8 (fd, 0x13, 0x3F) ; // Enable LEDs 0- 5 + wiringPiI2CWriteReg8 (fd, 0x14, 0x3F) ; // Enable LEDs 6-11 + wiringPiI2CWriteReg8 (fd, 0x15, 0x3F) ; // Enable LEDs 12-17 + wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update + + node = wiringPiNewNode (pinBase, 18) ; + + node->fd = fd ; + node->analogWrite = myAnalogWrite ; + + return 0 ; +} diff --git a/wiringPi/sn3218.h b/wiringPi/sn3218.h new file mode 100644 index 0000000..580d5f9 --- /dev/null +++ b/wiringPi/sn3218.h @@ -0,0 +1,33 @@ +/* + * sn3218.c: + * Extend wiringPi with the SN3218 I2C LED driver board. + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int sn3218Setup (int pinBase) ; + +#ifdef __cplusplus +} +#endif From cbf6d642b5a593b971fcc8181cb692c539353d04 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 23 Jul 2013 20:25:46 +0100 Subject: [PATCH 53/97] Added in PiGlow devLib and a couple of examples for the PiGlow bumped version. --- devLib/Makefile | 6 +- devLib/piGlow.c | 117 +++++++++++++ devLib/piGlow.h | 36 ++++ examples/PiGlow/Makefile | 69 ++++++++ examples/{piglow.c => PiGlow/piGlow0.c} | 0 examples/PiGlow/piGlow1.c | 223 ++++++++++++++++++++++++ gpio/gpio.c | 2 +- 7 files changed, 451 insertions(+), 2 deletions(-) create mode 100644 devLib/piGlow.c create mode 100644 devLib/piGlow.h create mode 100644 examples/PiGlow/Makefile rename examples/{piglow.c => PiGlow/piGlow0.c} (100%) create mode 100644 examples/PiGlow/piGlow1.c diff --git a/devLib/Makefile b/devLib/Makefile index a106d93..a3c0d42 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -42,7 +42,8 @@ LIBS = SRC = ds1302.c maxdetect.c piNes.c \ gertboard.c piFace.c \ - lcd128x64.c lcd.c + lcd128x64.c lcd.c \ + piGlow.c OBJ = $(SRC:.c=.o) @@ -86,6 +87,7 @@ install-headers: @install -m 0644 piFace.h $(DESTDIR)$(PREFIX)/include @install -m 0644 lcd128x64.h $(DESTDIR)$(PREFIX)/include @install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 piGlow.h $(DESTDIR)$(PREFIX)/include .PHONEY: install install: $(DYNAMIC) install-headers @@ -111,6 +113,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/piFace.h @rm -f $(DESTDIR)$(PREFIX)/include/lcd128x64.h @rm -f $(DESTDIR)$(PREFIX)/include/lcd.h + @rm -f $(DESTDIR)$(PREFIX)/include/piGlow.h @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.* @ldconfig @@ -128,3 +131,4 @@ gertboard.o: gertboard.h piFace.o: piFace.h lcd128x64.o: font.h lcd128x64.h lcd.o: lcd.h +piGlow.o: piGlow.h diff --git a/devLib/piGlow.c b/devLib/piGlow.c new file mode 100644 index 0000000..2036b7d --- /dev/null +++ b/devLib/piGlow.c @@ -0,0 +1,117 @@ +/* + * piGlow.c: + * Easy access to the Pimoroni PiGlow board. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include + +#include "piGlow.h" + +static int myBase ; + +static int leg0 [6] = { 6, 7, 8, 5, 4, 9 } ; +static int leg1 [6] = { 17, 16, 15, 13, 11, 10 } ; +static int leg2 [6] = { 0, 1, 2, 3, 14, 12 } ; + + +/* + * piGlow1: + * Light up an individual LED + ********************************************************************************* + */ + +void piGlow1 (const int leg, const int ring, const int intensity) +{ + int *legLeds ; + + if ((leg < 0) || (leg > 2)) return ; + if ((ring < 0) || (ring > 5)) return ; + + /**/ if (leg == 0) + legLeds = leg0 ; + else if (leg == 1) + legLeds = leg1 ; + else + legLeds = leg2 ; + + analogWrite (myBase + legLeds [ring], intensity) ; +} + +/* + * piGlowLeg: + * Light up all 6 LEDs on a leg + ********************************************************************************* + */ + +void piGlowLeg (const int leg, const int intensity) +{ + int i ; + int *legLeds ; + + if ((leg < 0) || (leg > 2)) + return ; + + /**/ if (leg == 0) + legLeds = leg0 ; + else if (leg == 1) + legLeds = leg1 ; + else + legLeds = leg2 ; + + for (i = 0 ; i < 6 ; ++i) + analogWrite (myBase + legLeds [i], intensity) ; +} + + +/* + * piGlowRing: + * Light up 3 LEDs in a ring. Ring 0 is the outermost, 5 the innermost + ********************************************************************************* + */ + +void piGlowRing (const int ring, const int intensity) +{ + if ((ring < 0) || (ring > 5)) + return ; + + analogWrite (myBase + leg0 [ring], intensity) ; + analogWrite (myBase + leg1 [ring], intensity) ; + analogWrite (myBase + leg2 [ring], intensity) ; +} + +/* + * piGlowSetup: + * Initialise the board & remember the pins we're using + ********************************************************************************* + */ + +void piGlowSetup (const int pinBase) +{ + sn3218Setup (myBase = pinBase) ; + +// Turn all off to start with + + piGlowLeg (0, 0) ; + piGlowLeg (1, 0) ; + piGlowLeg (2, 0) ; +} diff --git a/devLib/piGlow.h b/devLib/piGlow.h new file mode 100644 index 0000000..5f107d6 --- /dev/null +++ b/devLib/piGlow.h @@ -0,0 +1,36 @@ +/* + * piglow.h: + * Easy access to the Pimoroni PiGlow board. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern void piGlow1 (const int leg, const int ring, const int intensity) ; +extern void piGlowLeg (const int leg, const int intensity) ; +extern void piGlowRing (const int ring, const int intensity) ; +extern void piGlowSetup (const int pinBase) ; + +#ifdef __cplusplus +} +#endif diff --git a/examples/PiGlow/Makefile b/examples/PiGlow/Makefile new file mode 100644 index 0000000..a09895b --- /dev/null +++ b/examples/PiGlow/Makefile @@ -0,0 +1,69 @@ +# +# Makefile: +# wiringPi - Wiring Compatable library for the Raspberry Pi +# https://projects.drogon.net/wiring-pi +# +# Copyright (c) 2012-2013 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# + + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm + +# Should not alter anything below this line +############################################################################### + +SRC = piGlow0.c piGlow1.c + +OBJ = $(SRC:.c=.o) + +BINS = $(SRC:.c=) + +all: $(BINS) + +piGlow0: piGlow0.o + @echo [link] + @$(CC) -o $@ piGlow0.o $(LDFLAGS) $(LDLIBS) + +piGlow1: piGlow1.o + @echo [link] + @$(CC) -o $@ piGlow1.o $(LDFLAGS) $(LDLIBS) + +.c.o: + @echo [CC] $< + @$(CC) -c $(CFLAGS) $< -o $@ + +clean: + @echo "[Clean]" + @rm -f $(OBJ) *~ core tags $(BINS) + +tags: $(SRC) + @echo [ctags] + @ctags $(SRC) + +depend: + makedepend -Y $(SRC) + +# DO NOT DELETE diff --git a/examples/piglow.c b/examples/PiGlow/piGlow0.c similarity index 100% rename from examples/piglow.c rename to examples/PiGlow/piGlow0.c diff --git a/examples/PiGlow/piGlow1.c b/examples/PiGlow/piGlow1.c new file mode 100644 index 0000000..068564d --- /dev/null +++ b/examples/PiGlow/piGlow1.c @@ -0,0 +1,223 @@ +/* + * piGlow1.c: + * Very simple demonstration of the PiGlow board. + * This uses the piGlow devLib. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#include +#include + +#define PIGLOW_BASE 533 + + +/* + * keypressed: clearKeypressed: + * Simple but effective ways to tell if the enter key has been pressed + ********************************************************************************* + */ + +static int keypressed (void) +{ + struct pollfd polls ; + + polls.fd = fileno (stdin) ; + polls.events = POLLIN ; + + return poll (&polls, 1, 0) != 0 ; +} + +static void clearKeypressed (void) +{ + while (keypressed ()) + (void)getchar () ; +} + + +/* + * pulseLed: + * Pulses the LED at position leg, ring from off to a max. value, + * then off again + ********************************************************************************* + */ + +static void pulseLed (int leg, int ring) +{ + int i ; + + for (i = 0 ; i < 140 ; ++i) + { + piGlow1 (leg, ring, i) ; + delay (1) ; + } + delay (10) ; + for (i = 140 ; i >= 0 ; --i) + { + piGlow1 (leg, ring, i) ; + delay (1) ; + } +} + +/* + * pulseLeg: + * Same as above, but a whole leg at a time + ********************************************************************************* + */ + +static void pulseLeg (int leg) +{ + int i ; + + for (i = 0 ; i < 140 ; ++i) + { + piGlowLeg (leg, i) ; delay (1) ; + } + delay (10) ; + for (i = 140 ; i >= 0 ; --i) + { + piGlowLeg (leg, i) ; delay (1) ; + } +} + + +/* + * pulse Ring: + * Same as above, but a whole ring at a time + ********************************************************************************* + */ + +static void pulseRing (int ring) +{ + int i ; + + for (i = 0 ; i < 140 ; ++i) + { + piGlowRing (ring, i) ; delay (1) ; + } + delay (10) ; + for (i = 140 ; i >= 0 ; --i) + { + piGlowRing (ring, i) ; delay (1) ; + } +} + + +/* + * main: + * Our little demo prgoram + ********************************************************************************* + */ + +int main (void) +{ + int i ; + int ring, leg ; + +// Always initialise wiringPi: +// Use the Sys method if you don't need to run as root + + wiringPiSetupSys () ; + +// Initialise the piGlow devLib with our chosen pin base + + piGlowSetup (PIGLOW_BASE) ; + +// LEDs, one at a time + + printf ("LEDs, one at a time\n") ; + for (; !keypressed () ;) + for (leg = 0 ; leg < 3 ; ++leg) + { + for (ring = 0 ; ring < 6 ; ++ring) + { + pulseLed (leg, ring) ; + if (keypressed ()) + break ; + } + if (keypressed ()) + break ; + } + clearKeypressed () ; + +// Rings, one at a time + + printf ("Rings, one at a time\n") ; + for (; !keypressed () ;) + for (ring = 0 ; ring < 6 ; ++ring) + { + pulseRing (ring) ; + if (keypressed ()) + break ; + } + clearKeypressed () ; + +// Legs, one at a time + + printf ("Legs, one at a time\n") ; + for (; !keypressed () ;) + for (leg = 0 ; leg < 3 ; ++leg) + { + pulseLeg (leg) ; + if (keypressed ()) + break ; + } + clearKeypressed () ; + + delay (1000) ; + +// Sequence - alternating rings, legs and random + + printf ("Sequence now\n") ; + + for (; !keypressed () ;) + { + for (i = 0 ; i < 20 ; ++i) + for (leg = 2 ; leg >= 0 ; --leg) + { + piGlowLeg (leg, 100) ; + delay (100) ; + piGlowLeg (leg, 0) ; + } + + for (i = 0 ; i < 20 ; ++i) + for (ring = 5 ; ring >= 0 ; --ring) + { + piGlowRing (ring, 100) ; + delay (50) ; + piGlowRing (ring, 0) ; + } + + for (i = 0 ; i < 1000 ; ++i) + { + leg = random () % 3 ; + ring = random () % 6 ; + piGlow1 (leg, ring, random () % 256) ; + delay (5) ; + piGlow1 (leg, ring, 0) ; + } + } + + return 0 ; +} diff --git a/gpio/gpio.c b/gpio/gpio.c index dced95c..ebbb377 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -51,7 +51,7 @@ extern void doReadallOld (void) ; # define FALSE (1==2) #endif -#define VERSION "2.09" +#define VERSION "2.10" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; From e25cbc0a627b6d109f7ac1ff6d7591af1b0b552f Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Wed, 24 Jul 2013 15:14:09 +0100 Subject: [PATCH 54/97] Added in the PiGlow devLib extension driver. Written some examples for the PiGlow board bumped wiringPi version --- build | 2 + devLib/piGlow.c | 29 ++++--- devLib/piGlow.h | 11 ++- examples/PiGlow/Makefile | 12 ++- examples/PiGlow/piGlow1.c | 61 ++++++++++--- examples/PiGlow/piglow.c | 176 ++++++++++++++++++++++++++++++++++++++ gpio/gpio.c | 2 +- 7 files changed, 263 insertions(+), 30 deletions(-) create mode 100644 examples/PiGlow/piglow.c diff --git a/build b/build index f578d4a..01fa706 100755 --- a/build +++ b/build @@ -27,6 +27,8 @@ if [ x$1 = "xclean" ]; then echo -n "PiFace: " ; make clean cd ../q2w echo -n "Quick2Wire: " ; make clean + cd ../PiGlow + echo -n "PiGlow: " ; make clean exit fi diff --git a/devLib/piGlow.c b/devLib/piGlow.c index 2036b7d..44e3db8 100644 --- a/devLib/piGlow.c +++ b/devLib/piGlow.c @@ -27,7 +27,7 @@ #include "piGlow.h" -static int myBase ; +#define PIGLOW_BASE 577 static int leg0 [6] = { 6, 7, 8, 5, 4, 9 } ; static int leg1 [6] = { 17, 16, 15, 13, 11, 10 } ; @@ -54,7 +54,7 @@ void piGlow1 (const int leg, const int ring, const int intensity) else legLeds = leg2 ; - analogWrite (myBase + legLeds [ring], intensity) ; + analogWrite (PIGLOW_BASE + legLeds [ring], intensity) ; } /* @@ -65,7 +65,7 @@ void piGlow1 (const int leg, const int ring, const int intensity) void piGlowLeg (const int leg, const int intensity) { - int i ; + int i ; int *legLeds ; if ((leg < 0) || (leg > 2)) @@ -79,7 +79,7 @@ void piGlowLeg (const int leg, const int intensity) legLeds = leg2 ; for (i = 0 ; i < 6 ; ++i) - analogWrite (myBase + legLeds [i], intensity) ; + analogWrite (PIGLOW_BASE + legLeds [i], intensity) ; } @@ -94,9 +94,9 @@ void piGlowRing (const int ring, const int intensity) if ((ring < 0) || (ring > 5)) return ; - analogWrite (myBase + leg0 [ring], intensity) ; - analogWrite (myBase + leg1 [ring], intensity) ; - analogWrite (myBase + leg2 [ring], intensity) ; + analogWrite (PIGLOW_BASE + leg0 [ring], intensity) ; + analogWrite (PIGLOW_BASE + leg1 [ring], intensity) ; + analogWrite (PIGLOW_BASE + leg2 [ring], intensity) ; } /* @@ -105,13 +105,14 @@ void piGlowRing (const int ring, const int intensity) ********************************************************************************* */ -void piGlowSetup (const int pinBase) +void piGlowSetup (int clear) { - sn3218Setup (myBase = pinBase) ; + sn3218Setup (PIGLOW_BASE) ; -// Turn all off to start with - - piGlowLeg (0, 0) ; - piGlowLeg (1, 0) ; - piGlowLeg (2, 0) ; + if (clear) + { + piGlowLeg (0, 0) ; + piGlowLeg (1, 0) ; + piGlowLeg (2, 0) ; + } } diff --git a/devLib/piGlow.h b/devLib/piGlow.h index 5f107d6..d501c2c 100644 --- a/devLib/piGlow.h +++ b/devLib/piGlow.h @@ -22,6 +22,15 @@ *********************************************************************** */ + +#define PIGLOW_RED 0 +#define PIGLOW_YELLOW 1 +#define PIGLOW_ORANGE 2 +#define PIGLOW_GREEN 3 +#define PIGLOW_BLUE 4 +#define PIGLOW_WHITE 5 + + #ifdef __cplusplus extern "C" { #endif @@ -29,7 +38,7 @@ extern "C" { extern void piGlow1 (const int leg, const int ring, const int intensity) ; extern void piGlowLeg (const int leg, const int intensity) ; extern void piGlowRing (const int ring, const int intensity) ; -extern void piGlowSetup (const int pinBase) ; +extern void piGlowSetup (int clear) ; #ifdef __cplusplus } diff --git a/examples/PiGlow/Makefile b/examples/PiGlow/Makefile index a09895b..8d31141 100644 --- a/examples/PiGlow/Makefile +++ b/examples/PiGlow/Makefile @@ -35,7 +35,7 @@ LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm # Should not alter anything below this line ############################################################################### -SRC = piGlow0.c piGlow1.c +SRC = piGlow0.c piGlow1.c piglow.c OBJ = $(SRC:.c=.o) @@ -51,6 +51,10 @@ piGlow1: piGlow1.o @echo [link] @$(CC) -o $@ piGlow1.o $(LDFLAGS) $(LDLIBS) +piglow: piglow.o + @echo [link] + @$(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS) + .c.o: @echo [CC] $< @$(CC) -c $(CFLAGS) $< -o $@ @@ -63,6 +67,12 @@ tags: $(SRC) @echo [ctags] @ctags $(SRC) +install: piglow + @echo Installing piglow into /usr/local/bin + @cp -a piglow /usr/local/bin/piglow + @chmod 755 /usr/local/bin/piglow + @echo Done. Remember to load the I2C drivers! + depend: makedepend -Y $(SRC) diff --git a/examples/PiGlow/piGlow1.c b/examples/PiGlow/piGlow1.c index 068564d..a00b31e 100644 --- a/examples/PiGlow/piGlow1.c +++ b/examples/PiGlow/piGlow1.c @@ -32,6 +32,11 @@ #define PIGLOW_BASE 533 +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (!TRUE) +#endif + /* * keypressed: clearKeypressed: @@ -123,6 +128,37 @@ static void pulseRing (int ring) } } +#define LEG_STEPS 3 + +static int legSequence [] = +{ + 4, 12, 99, + 99, 4, 12, + 12, 99, 4, +} ; + + +#define RING_STEPS 16 + +static int ringSequence [] = +{ + 0, 0, 0, 0, 0, 64, + 0, 0, 0, 0, 64, 64, + 0, 0, 0, 64, 64, 0, + 0, 0, 64, 64, 0, 0, + 0, 64, 64, 0, 0, 0, + 64, 64, 0, 0, 0, 0, + 64, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 64, 0, 0, 0, 0, 0, + 64, 64, 0, 0, 0, 0, + 0, 64, 64, 0, 0, 0, + 0, 0, 64, 64, 0, 0, + 0, 0, 0, 64, 64, 0, + 0, 0, 0, 0, 64, 64, + 0, 0, 0, 0, 0, 64, + 0, 0, 0, 0, 0, 0, +} ; /* * main: @@ -133,7 +169,7 @@ static void pulseRing (int ring) int main (void) { int i ; - int ring, leg ; + int step, ring, leg ; // Always initialise wiringPi: // Use the Sys method if you don't need to run as root @@ -142,7 +178,7 @@ int main (void) // Initialise the piGlow devLib with our chosen pin base - piGlowSetup (PIGLOW_BASE) ; + piGlowSetup (1) ; // LEDs, one at a time @@ -190,28 +226,27 @@ int main (void) // Sequence - alternating rings, legs and random printf ("Sequence now\n") ; - for (; !keypressed () ;) { for (i = 0 ; i < 20 ; ++i) - for (leg = 2 ; leg >= 0 ; --leg) + for (step = 0 ; step < LEG_STEPS ; ++step) { - piGlowLeg (leg, 100) ; - delay (100) ; - piGlowLeg (leg, 0) ; + for (leg = 0 ; leg < 3 ; ++leg) + piGlowLeg (leg, legSequence [step * 3 + leg]) ; + delay (80) ; } - for (i = 0 ; i < 20 ; ++i) - for (ring = 5 ; ring >= 0 ; --ring) + for (i = 0 ; i < 10 ; ++i) + for (step = 0 ; step < RING_STEPS ; ++step) { - piGlowRing (ring, 100) ; - delay (50) ; - piGlowRing (ring, 0) ; + for (ring = 0 ; ring < 6 ; ++ring) + piGlowRing (ring, ringSequence [step * 6 + ring]) ; + delay (80) ; } for (i = 0 ; i < 1000 ; ++i) { - leg = random () % 3 ; + leg = random () % 3 ; ring = random () % 6 ; piGlow1 (leg, ring, random () % 256) ; delay (5) ; diff --git a/examples/PiGlow/piglow.c b/examples/PiGlow/piglow.c new file mode 100644 index 0000000..e6a2db3 --- /dev/null +++ b/examples/PiGlow/piglow.c @@ -0,0 +1,176 @@ +/* + * piglow.c: + * Very simple demonstration of the PiGlow board. + * This uses the piGlow devLib. + * + * Copyright (c) 2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (!TRUE) +#endif + +#include +#include + +static void failUsage (void) +{ + fprintf (stderr, "Usage examples:\n") ; + fprintf (stderr, " piglow off # All off\n") ; + fprintf (stderr, " piglow red 50 # Light the 3 red LEDs to 50%%\n") ; + fprintf (stderr, " colours are: red, yellow, orange, green, blue and white\n") ; + fprintf (stderr, " piglow all 75 # Light all to 75%%\n") ; + fprintf (stderr, " piglow leg 0 25 # Light leg 0 to 25%%\n") ; + fprintf (stderr, " piglow ring 3 100 # Light ring 3 to 100%%\n") ; + fprintf (stderr, " piglow led 2 5 100 # Light the single LED on Leg 2, ring 5 to 100%%\n") ; + + exit (EXIT_FAILURE) ; +} + +static int getPercent (char *typed) +{ + int percent ; + + percent = atoi (typed) ; + if ((percent < 0) || (percent > 100)) + { + fprintf (stderr, "piglow: percent value out of range\n") ; + exit (EXIT_FAILURE) ; + } + return (percent * 255) / 100 ; +} + + +/* + * main: + * Our little demo prgoram + ********************************************************************************* + */ + +int main (int argc, char *argv []) +{ + int percent ; + int ring, leg ; + +// Always initialise wiringPi: +// Use the Sys method if you don't need to run as root + + wiringPiSetupSys () ; + +// Initialise the piGlow devLib + + piGlowSetup (FALSE) ; + + if (argc == 1) + failUsage () ; + + if ((argc == 2) && (strcasecmp (argv [1], "off") == 0)) + { + for (leg = 0 ; leg < 3 ; ++leg) + piGlowLeg (leg, 0) ; + return 0 ; + } + + if (argc == 3) + { + percent = getPercent (argv [2]) ; + + /**/ if (strcasecmp (argv [1], "red") == 0) + piGlowRing (PIGLOW_RED, percent) ; + else if (strcasecmp (argv [1], "yellow") == 0) + piGlowRing (PIGLOW_YELLOW, percent) ; + else if (strcasecmp (argv [1], "orange") == 0) + piGlowRing (PIGLOW_ORANGE, percent) ; + else if (strcasecmp (argv [1], "green") == 0) + piGlowRing (PIGLOW_GREEN, percent) ; + else if (strcasecmp (argv [1], "blue") == 0) + piGlowRing (PIGLOW_BLUE, percent) ; + else if (strcasecmp (argv [1], "white") == 0) + piGlowRing (PIGLOW_WHITE, percent) ; + else if (strcasecmp (argv [1], "all") == 0) + for (ring = 0 ; ring < 6 ; ++ring) + piGlowRing (ring, percent) ; + else + { + fprintf (stderr, "piglow: invalid colour\n") ; + exit (EXIT_FAILURE) ; + } + return 0 ; + } + + if (argc == 4) + { + /**/ if (strcasecmp (argv [1], "leg") == 0) + { + leg = atoi (argv [2]) ; + if ((leg < 0) || (leg > 2)) + { + fprintf (stderr, "piglow: leg value out of range\n") ; + exit (EXIT_FAILURE) ; + } + percent = getPercent (argv [3]) ; + piGlowLeg (leg, percent) ; + } + else if (strcasecmp (argv [1], "ring") == 0) + { + ring = atoi (argv [2]) ; + if ((ring < 0) || (ring > 5)) + { + fprintf (stderr, "piglow: ring value out of range\n") ; + exit (EXIT_FAILURE) ; + } + percent = getPercent (argv [3]) ; + piGlowRing (ring, percent) ; + } + return 0 ; + } + + if (argc == 5) + { + if (strcasecmp (argv [1], "led") != 0) + failUsage () ; + + leg = atoi (argv [2]) ; + if ((leg < 0) || (leg > 2)) + { + fprintf (stderr, "piglow: leg value out of range\n") ; + exit (EXIT_FAILURE) ; + } + ring = atoi (argv [3]) ; + if ((ring < 0) || (ring > 5)) + { + fprintf (stderr, "piglow: ring value out of range\n") ; + exit (EXIT_FAILURE) ; + } + percent = getPercent (argv [4]) ; + piGlow1 (leg, ring, percent) ; + return 0 ; + } + + failUsage () ; + return 0 ; +} + + diff --git a/gpio/gpio.c b/gpio/gpio.c index ebbb377..1823222 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -51,7 +51,7 @@ extern void doReadallOld (void) ; # define FALSE (1==2) #endif -#define VERSION "2.10" +#define VERSION "2.11" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; From 5e16e15a59437fc93d75f31d9c37abfe9febd479 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 28 Jul 2013 10:54:32 +0100 Subject: [PATCH 55/97] tidied and tested DRC Serial (renamed it it drcSerial too) Tweaked the mcp3422 code altered the build script to let me build static --- build | 24 ++++-- gpio/Makefile | 7 +- gpio/extensions.c | 84 +++++++++++++++++++ wiringPi/Makefile | 4 +- wiringPi/drc.c | 203 --------------------------------------------- wiringPi/drc.h | 34 -------- wiringPi/mcp3422.c | 29 +++---- 7 files changed, 120 insertions(+), 265 deletions(-) delete mode 100644 wiringPi/drc.c delete mode 100644 wiringPi/drc.h diff --git a/build b/build index 01fa706..e17cf18 100755 --- a/build +++ b/build @@ -51,18 +51,30 @@ fi echo "WiringPi Library" cd wiringPi sudo make uninstall - make - check_make_ok - sudo make install + if [ x$1 = "xstatic" ]; then + make static + check_make_ok + sudo make install-static + else + make + check_make_ok + sudo make install + fi check_make_ok echo echo "WiringPi Devices Library" cd ../devLib sudo make uninstall - make - check_make_ok - sudo make install + if [ x$1 = "xstatic" ]; then + make static + check_make_ok + sudo make install-static + else + make + check_make_ok + sudo make install + fi check_make_ok echo diff --git a/gpio/Makefile b/gpio/Makefile index b4519fa..8fa12d5 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -1,9 +1,10 @@ # # Makefile: -# wiringPi - Wiring Compatable library for the Raspberry Pi +# The gpio command: +# A swiss-army knige of GPIO shenanigans. # https://projects.drogon.net/wiring-pi # -# Copyright (c) 2012 Gordon Henderson +# Copyright (c) 2012-2013 Gordon Henderson ################################################################################# # This file is part of wiringPi: # Wiring Compatable library for the Raspberry Pi @@ -28,7 +29,7 @@ PREFIX=/local #DEBUG = -g -O0 DEBUG = -O2 CC = gcc -INCLUDE = -I$(DESTDIR)$(PREFIX)/usr/local/include +INCLUDE = -I$(DESTDIR)$(PREFIX)/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L$(DESTDIR)$(PREFIX)/lib diff --git a/gpio/extensions.c b/gpio/extensions.c index c7e5308..96d6255 100644 --- a/gpio/extensions.c +++ b/gpio/extensions.c @@ -50,6 +50,7 @@ #include #include #include +#include #include "extensions.h" @@ -99,6 +100,43 @@ static char *extractInt (char *progName, char *p, int *num) } +/* + * extractStr: + * Check & return a string at the given location (prefixed by a :) + ********************************************************************************* + */ + +static char *extractStr (char *progName, char *p, char **str) +{ + char *q, *r ; + + if (*p != ':') + { + fprintf (stderr, "%s: colon expected\n", progName) ; + return NULL ; + } + + ++p ; + + if (!isprint (*p)) + { + fprintf (stderr, "%s: character expected\n", progName) ; + return NULL ; + } + + q = p ; + while ((*q != 0) && (*q != ':')) + ++q ; + + *str = r = calloc (q - p + 2, 1) ; // Zeros it + + while (p != q) + *r++ = *p++ ; + + return p ; +} + + /* * doExtensionMcp23008: @@ -524,6 +562,51 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params) return TRUE ; } +/* + * doExtensionDrcS: + * Interface to a DRC Serial system + * drcs:base:pins:serialPort:baud + ********************************************************************************* + */ + +static int doExtensionDrcS (char *progName, int pinBase, char *params) +{ + char *port ; + int pins, baud ; + + if ((params = extractInt (progName, params, &pins)) == NULL) + return FALSE ; + + if ((pins < 1) || (pins > 100)) + { + fprintf (stderr, "%s: pins (%d) out of range (2-100)\n", progName, pins) ; + return FALSE ; + } + + if ((params = extractStr (progName, params, &port)) == NULL) + return FALSE ; + + if (strlen (port) == 0) + { + fprintf (stderr, "%s: serial port device name required\n", progName) ; + return FALSE ; + } + + if ((params = extractInt (progName, params, &baud)) == NULL) + return FALSE ; + + if ((baud < 1) || (baud > 4000000)) + { + fprintf (stderr, "%s: baud rate (%d) out of range\n", progName, baud) ; + return FALSE ; + } + + drcSetupSerial (pinBase, pins, port, baud) ; + + return TRUE ; +} + + /* * Function list @@ -547,6 +630,7 @@ struct extensionFunctionStruct extensionFunctions [] = { "max31855", &doExtensionMax31855 }, { "max5322", &doExtensionMax5322 }, { "sn3218", &doExtensionSn3218 }, + { "drcs", &doExtensionDrcS }, { NULL, NULL }, } ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index ed4f6be..619a3dc 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -55,7 +55,7 @@ SRC = wiringPi.c \ mcp3002.c mcp3004.c mcp4802.c mcp3422.c \ max31855.c max5322.c \ sn3218.c \ - drc.c + drcSerial.c OBJ = $(SRC:.c=.o) @@ -99,6 +99,7 @@ install-headers: @install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 drcSerial.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23008.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23016.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include @@ -139,6 +140,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/softTone.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h + @rm -f $(DESTDIR)$(PREFIX)/include/drcSerial.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23008.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23016.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h diff --git a/wiringPi/drc.c b/wiringPi/drc.c deleted file mode 100644 index 07baf17..0000000 --- a/wiringPi/drc.c +++ /dev/null @@ -1,203 +0,0 @@ -/* - * drc.c: - * Extend wiringPi with the DRC control protocll to Arduino - * Copyright (c) 2013 Gordon Henderson - *********************************************************************** - * This file is part of wiringPi: - * https://projects.drogon.net/raspberry-pi/wiringpi/ - * - * wiringPi is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * wiringPi is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with wiringPi. - * If not, see . - *********************************************************************** - */ - -#include -#include -#include -#include - -#include "wiringPi.h" -#include "wiringSerial.h" - -#include "drc.h" - -#ifndef TRUE -# define TRUE (1==1) -# define FALSE (1==2) -#endif - - -/* - * myPinMode: - * Change the pin mode on the remote DRC device - ********************************************************************************* - */ - -static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) -{ - /**/ if (mode == OUTPUT) - serialPutchar (node->fd, 'o') ; // Input - else if (mode == PWM_OUTPUT) - serialPutchar (node->fd, 'p') ; // PWM - else - serialPutchar (node->fd, 'i') ; // Default to input - - serialPutchar (node->fd, pin - node->pinBase) ; -} - - -/* - * myPullUpDnControl: - * ATmegas only have pull-up's on of off. No pull-downs. - ********************************************************************************* - */ - -static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode) -{ - -// Force pin into input mode - - serialPutchar (node->fd, 'i' ) ; - serialPutchar (node->fd, pin) ; - - /**/ if (mode == PUD_UP) - { - serialPutchar (node->fd, '1') ; - serialPutchar (node->fd, pin - node->pinBase) ; - } - else if (mode == PUD_OFF) - { - serialPutchar (node->fd, '0') ; - serialPutchar (node->fd, pin - node->pinBase) ; - } -} - - -/* - * myDigitalWrite: - ********************************************************************************* - */ - -static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) -{ - serialPutchar (node->fd, value == 0 ? '0' : '1') ; - serialPutchar (node->fd, pin - node->pinBase) ; -} - - -/* - * myPwmWrite: - ********************************************************************************* - */ - -static void myPwmWrite (struct wiringPiNodeStruct *node, int pin, int value) -{ - serialPutchar (node->fd, 'v') ; - serialPutchar (node->fd, pin - node->pinBase) ; - serialPutchar (node->fd, value & 0xFF) ; -} - - -/* - * myAnalogRead: - ********************************************************************************* - */ - -static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) -{ - int vHi, vLo ; - - serialPutchar (node->fd, 'a') ; - serialPutchar (node->fd, pin - node->pinBase) ; - vHi = serialGetchar (node->fd) ; - vLo = serialGetchar (node->fd) ; - - return (vHi << 8) | vLo ; -} - - -/* - * myDigitalRead: - ********************************************************************************* - */ - -static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) -{ - serialPutchar (node->fd, 'r') ; // Send read command - serialPutchar (node->fd, pin - node->pinBase) ; - return (serialGetchar (node->fd) == '0') ? 0 : 1 ; -} - - -/* - * drcSetup: - * Create a new instance of an DRC GPIO interface. - * Could be a variable nunber of pins here - we might not know in advance - * if it's an ATmega with 14 pins, or something with less or more! - ********************************************************************************* - */ - -int drcSetup (const int pinBase, const int numPins, const char *device) -{ - int fd ; - int ok, tries ; - time_t then ; - struct wiringPiNodeStruct *node ; - - if ((fd = serialOpen (device, 115200)) < 0) - return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ; - - delay (10) ; // May need longer if it's an Uno that reboots on the open... - -// Flush any pending input - - while (serialDataAvail (fd)) - (void)serialGetchar (fd) ; - - ok = FALSE ; - for (tries = 1 ; tries < 5 ; ++tries) - { - serialPutchar (fd, '@') ; - then = time (NULL) + 2 ; - while (time (NULL) < then) - if (serialDataAvail (fd)) - { - if (serialGetchar (fd) == '@') - { - ok = TRUE ; - break ; - } - } - if (ok) - break ; - } - - if (!ok) - { - serialClose (fd) ; - return wiringPiFailure (WPI_FATAL, "Unable to communidate with DRC device") ; - } - - node = wiringPiNewNode (pinBase, numPins) ; - - node->fd = fd ; - node->pinMode = myPinMode ; - node->pullUpDnControl = myPullUpDnControl ; - node->analogRead = myAnalogRead ; - node->digitalRead = myDigitalRead ; - node->digitalWrite = myDigitalWrite ; - node->pwmWrite = myPwmWrite ; - - return 0 ; -} diff --git a/wiringPi/drc.h b/wiringPi/drc.h deleted file mode 100644 index c2c4ff3..0000000 --- a/wiringPi/drc.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * drc.h: - * Extend wiringPi with the DRC control protocll to Arduino - * Copyright (c) 2013 Gordon Henderson - *********************************************************************** - * This file is part of wiringPi: - * https://projects.drogon.net/raspberry-pi/wiringpi/ - * - * wiringPi is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * wiringPi is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with wiringPi. - * If not, see . - *********************************************************************** - */ - -#ifdef __cplusplus -extern "C" { -#endif - -int drcSetup (const int pinBase, const int numPins, const char *device) ; - - -#ifdef __cplusplus -} -#endif diff --git a/wiringPi/mcp3422.c b/wiringPi/mcp3422.c index 4d07abc..831aece 100644 --- a/wiringPi/mcp3422.c +++ b/wiringPi/mcp3422.c @@ -25,6 +25,7 @@ #include +#include #include #include #include @@ -44,7 +45,8 @@ int myAnalogRead (struct wiringPiNodeStruct *node, int chan) { - unsigned char config, b0, b1, b2, b3 ; + unsigned char config ; + unsigned char buffer [4] ; int value = 0 ; // One-shot mode, trigger plus the other configs. @@ -57,35 +59,26 @@ int myAnalogRead (struct wiringPiNodeStruct *node, int chan) { case MCP3422_SR_3_75: // 18 bits delay (270) ; - b0 = wiringPiI2CRead (node->fd) ; - b1 = wiringPiI2CRead (node->fd) ; - b2 = wiringPiI2CRead (node->fd) ; - b3 = wiringPiI2CRead (node->fd) ; - value = ((b0 & 3) << 16) | (b1 << 8) | b2 ; + read (node->fd, buffer, 4) ; + value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [0] ; break ; case MCP3422_SR_15: // 16 bits delay ( 70) ; - b0 = wiringPiI2CRead (node->fd) ; - b1 = wiringPiI2CRead (node->fd) ; - b2 = wiringPiI2CRead (node->fd) ; - value = (b0 << 8) | b1 ; + read (node->fd, buffer, 3) ; + value = (buffer [0] << 8) | buffer [1] ; break ; case MCP3422_SR_60: // 14 bits delay ( 17) ; - b0 = wiringPiI2CRead (node->fd) ; - b1 = wiringPiI2CRead (node->fd) ; - b2 = wiringPiI2CRead (node->fd) ; - value = ((b0 & 0x3F) << 8) | b1 ; + read (node->fd, buffer, 3) ; + value = ((buffer [0] & 0x3F) << 8) | buffer [1] ; break ; case MCP3422_SR_240: // 12 bits delay ( 5) ; - b0 = wiringPiI2CRead (node->fd) ; - b1 = wiringPiI2CRead (node->fd) ; - b2 = wiringPiI2CRead (node->fd) ; - value = ((b0 & 0x0F) << 8) | b1 ; + read (node->fd, buffer, 3) ; + value = ((buffer [0] & 0x0F) << 8) | buffer [0] ; break ; } From f52dd8393ce1c85edb8e4d74d76920417b96c493 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 28 Jul 2013 11:05:28 +0100 Subject: [PATCH 56/97] Minor changes to the files and removed a bit of debug. --- examples/Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/Makefile b/examples/Makefile index 8848b33..571e341 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -36,6 +36,7 @@ LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm ############################################################################### SRC = blink.c blink8.c blink12.c \ + blink12drcs.c \ pwm.c \ speed.c wfi.c isr.c isr-osc.c \ lcd.c lcd-adafruit.c clock.c \ @@ -63,6 +64,10 @@ blink8: blink8.o @echo [link] @$(CC) -o $@ blink8.o $(LDFLAGS) $(LDLIBS) +blink12drcs: blink12drcs.o + @echo [link] + @$(CC) -o $@ blink12drcs.o $(LDFLAGS) $(LDLIBS) + blink12: blink12.o @echo [link] @$(CC) -o $@ blink12.o $(LDFLAGS) $(LDLIBS) From 477f8562471d63933ec9f2d3916bd75887209c31 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 28 Jul 2013 11:12:09 +0100 Subject: [PATCH 57/97] It helps if you add the files into GIT... --- examples/blink12drcs.c | 125 +++++++++++++++++++++++++ wiringPi/drcSerial.c | 201 +++++++++++++++++++++++++++++++++++++++++ wiringPi/drcSerial.h | 33 +++++++ 3 files changed, 359 insertions(+) create mode 100644 examples/blink12drcs.c create mode 100644 wiringPi/drcSerial.c create mode 100644 wiringPi/drcSerial.h diff --git a/examples/blink12drcs.c b/examples/blink12drcs.c new file mode 100644 index 0000000..6ee11fd --- /dev/null +++ b/examples/blink12drcs.c @@ -0,0 +1,125 @@ +/* + * blink12drcs.c: + * Simple sequence over the first 12 GPIO pins - LEDs + * Aimed at the Gertboard, but it's fairly generic. + * This version uses DRC totalk to the ATmega on the Gertboard + * + * Copyright (c) 2012-2013 Gordon Henderson. + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + +#include +#include +#include + +#define GERT_BASE 100 + +static int pinMap [] = +{ + 0, 1, 2, 3, // Pi Native + GERT_BASE + 2, GERT_BASE + 3, GERT_BASE + 4, GERT_BASE + 5, + GERT_BASE + 6, GERT_BASE + 7, GERT_BASE + 8, GERT_BASE + 9, +} ; + +// Simple sequencer data +// Triplets of LED, On/Off and delay + + +int data [] = +{ + 0, 1, 1, + 1, 1, 1, + 0, 0, 0, 2, 1, 1, + 1, 0, 0, 3, 1, 1, + 2, 0, 0, 4, 1, 1, + 3, 0, 0, 5, 1, 1, + 4, 0, 0, 6, 1, 1, + 5, 0, 0, 7, 1, 1, + 6, 0, 0, 8, 1, 1, + 7, 0, 0, 9, 1, 1, + 8, 0, 0, 10, 1, 1, + 9, 0, 0, 11, 1, 1, + 10, 0, 1, + 11, 0, 1, + + 0, 0, 1, // Extra delay + +// Back again + + 11, 1, 1, + 10, 1, 1, + 11, 0, 0, 9, 1, 1, + 10, 0, 0, 8, 1, 1, + 9, 0, 0, 7, 1, 1, + 8, 0, 0, 6, 1, 1, + 7, 0, 0, 5, 1, 1, + 6, 0, 0, 4, 1, 1, + 5, 0, 0, 3, 1, 1, + 4, 0, 0, 2, 1, 1, + 3, 0, 0, 1, 1, 1, + 2, 0, 0, 0, 1, 1, + 1, 0, 1, + 0, 0, 1, + + 0, 0, 1, // Extra delay + + 0, 9, 0, // End marker + +} ; + + +int main (void) +{ + int pin ; + int dataPtr ; + int l, s, d ; + + printf ("Raspberry Pi - 12-LED Sequence\n") ; + printf ("==============================\n") ; + printf ("\n") ; + printf ("Connect LEDs up to the first 4 Pi pins and 8 pins on the ATmega\n") ; + printf (" from PD2 through PB1 in that order,\n") ; + printf (" then sit back and watch the show!\n") ; + + wiringPiSetup () ; + drcSetupSerial (GERT_BASE, 20, "/dev/ttyAMA0", 115200) ; + + for (pin = 0 ; pin < 12 ; ++pin) + pinMode (pinMap [pin], OUTPUT) ; + + dataPtr = 0 ; + + for (;;) + { + l = data [dataPtr++] ; // LED + s = data [dataPtr++] ; // State + d = data [dataPtr++] ; // Duration (10ths) + + if (s == 9) // 9 -> End Marker + { + dataPtr = 0 ; + continue ; + } + + digitalWrite (pinMap [l], s) ; + delay (d * analogRead (GERT_BASE) / 4) ; + } + + return 0 ; +} diff --git a/wiringPi/drcSerial.c b/wiringPi/drcSerial.c new file mode 100644 index 0000000..6491a98 --- /dev/null +++ b/wiringPi/drcSerial.c @@ -0,0 +1,201 @@ +/* + * drcSerial.c: + * Extend wiringPi with the DRC Serial protocol (e.g. to Arduino) + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include +#include +#include + +#include "wiringPi.h" +#include "wiringSerial.h" + +#include "drcSerial.h" + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + + +/* + * myPinMode: + * Change the pin mode on the remote DRC device + ********************************************************************************* + */ + +static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) +{ + /**/ if (mode == OUTPUT) + serialPutchar (node->fd, 'o') ; // Input + else if (mode == PWM_OUTPUT) + serialPutchar (node->fd, 'p') ; // PWM + else + serialPutchar (node->fd, 'i') ; // Default to input + + serialPutchar (node->fd, pin - node->pinBase) ; +} + + +/* + * myPullUpDnControl: + * ATmegas only have pull-up's on of off. No pull-downs. + ********************************************************************************* + */ + +static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode) +{ + +// Force pin into input mode + + serialPutchar (node->fd, 'i' ) ; + serialPutchar (node->fd, pin - node->pinBase) ; + + /**/ if (mode == PUD_UP) + { + serialPutchar (node->fd, '1') ; + serialPutchar (node->fd, pin - node->pinBase) ; + } + else if (mode == PUD_OFF) + { + serialPutchar (node->fd, '0') ; + serialPutchar (node->fd, pin - node->pinBase) ; + } +} + + +/* + * myDigitalWrite: + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + serialPutchar (node->fd, value == 0 ? '0' : '1') ; + serialPutchar (node->fd, pin - node->pinBase) ; +} + + +/* + * myPwmWrite: + ********************************************************************************* + */ + +static void myPwmWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + serialPutchar (node->fd, 'v') ; + serialPutchar (node->fd, pin - node->pinBase) ; + serialPutchar (node->fd, value & 0xFF) ; +} + + +/* + * myAnalogRead: + ********************************************************************************* + */ + +static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) +{ + int vHi, vLo ; + + serialPutchar (node->fd, 'a') ; + serialPutchar (node->fd, pin - node->pinBase) ; + vHi = serialGetchar (node->fd) ; + vLo = serialGetchar (node->fd) ; + + return (vHi << 8) | vLo ; +} + + +/* + * myDigitalRead: + ********************************************************************************* + */ + +static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) +{ + serialPutchar (node->fd, 'r') ; // Send read command + serialPutchar (node->fd, pin - node->pinBase) ; + return (serialGetchar (node->fd) == '0') ? 0 : 1 ; +} + + +/* + * drcSetup: + * Create a new instance of an DRC GPIO interface. + * Could be a variable nunber of pins here - we might not know in advance + * if it's an ATmega with 14 pins, or something with less or more! + ********************************************************************************* + */ + +int drcSetupSerial (const int pinBase, const int numPins, const char *device, const int baud) +{ + int fd ; + int ok, tries ; + time_t then ; + struct wiringPiNodeStruct *node ; + + if ((fd = serialOpen (device, baud)) < 0) + return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ; + + delay (10) ; // May need longer if it's an Uno that reboots on the open... + +// Flush any pending input + + while (serialDataAvail (fd)) + (void)serialGetchar (fd) ; + + ok = FALSE ; + for (tries = 1 ; (tries < 5) && (!ok) ; ++tries) + { + serialPutchar (fd, '@') ; // Ping + then = time (NULL) + 2 ; + while (time (NULL) < then) + if (serialDataAvail (fd)) + { + if (serialGetchar (fd) == '@') + { + ok = TRUE ; + break ; + } + } + } + + if (!ok) + { + serialClose (fd) ; + return wiringPiFailure (WPI_FATAL, "Unable to communicate with DRC serial device") ; + } + + node = wiringPiNewNode (pinBase, numPins) ; + + node->fd = fd ; + node->pinMode = myPinMode ; + node->pullUpDnControl = myPullUpDnControl ; + node->analogRead = myAnalogRead ; + node->digitalRead = myDigitalRead ; + node->digitalWrite = myDigitalWrite ; + node->pwmWrite = myPwmWrite ; + + return 0 ; +} diff --git a/wiringPi/drcSerial.h b/wiringPi/drcSerial.h new file mode 100644 index 0000000..29e988e --- /dev/null +++ b/wiringPi/drcSerial.h @@ -0,0 +1,33 @@ +/* + * drcSerial.h: + * Extend wiringPi with the DRC Serial protocol (e.g. to Arduino) + * Copyright (c) 2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int drcSetupSerial (const int pinBase, const int numPins, const char *device, const int baud) ; + +#ifdef __cplusplus +} +#endif From 43422be6ac6f0dde5f2d8de38de634b21baa5e26 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 28 Jul 2013 21:38:08 +0100 Subject: [PATCH 58/97] Bumped version --- gpio/gpio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 1823222..fab0036 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -51,7 +51,7 @@ extern void doReadallOld (void) ; # define FALSE (1==2) #endif -#define VERSION "2.11" +#define VERSION "2.12" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; From f18c8f7204d6354220fd6754578b3daa43734e1b Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 3 Aug 2013 23:53:35 +0100 Subject: [PATCH 59/97] Added some tweaks to gpio to set alt modes on pins (sssh!) --- gpio/gpio.c | 30 +++++++++++++++++++++++++++++- wiringPi/wiringPi.c | 27 ++++++++++++++++++++++++++- wiringPi/wiringPi.h | 1 + 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index fab0036..b65af0e 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -51,7 +51,7 @@ extern void doReadallOld (void) ; # define FALSE (1==2) #endif -#define VERSION "2.12" +#define VERSION "2.13" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; @@ -75,6 +75,25 @@ char *usage = "Usage: gpio -v\n" " gpio gbw " ; // No trailing newline needed here. +/* + * decodePin: + * Decode a pin "number" which can actually be a pin name to represent + * one of the Pi's on-board pins. + ********************************************************************************* + */ + +static int decodePin (const char *str) +{ + +// The first case - see if it's a number: + + if (isdigit (str [0])) + return atoi (str) ; + + return 0 ; +} + + /* * changeOwner: * Change the ownership of the file to the real userId of the calling @@ -639,6 +658,15 @@ void doMode (int argc, char *argv []) else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ; + +// Undocumented + + else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ; + else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ; + else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ; + else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ; + else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ; + else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ; else { fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index ba61d9f..b54ad29 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -118,7 +118,6 @@ struct wiringPiNodeStruct *wiringPiNodes = NULL ; #define FSEL_INPT 0b000 #define FSEL_OUTP 0b001 #define FSEL_ALT0 0b100 -#define FSEL_ALT0 0b100 #define FSEL_ALT1 0b101 #define FSEL_ALT2 0b110 #define FSEL_ALT3 0b111 @@ -925,6 +924,32 @@ void pinEnableED01Pi (int pin) ********************************************************************************* */ +/* + * pinModeAlt: + * This is an un-documented special to let you set any pin to any mode + ********************************************************************************* + */ + +void pinModeAlt (int pin, int mode) +{ + int fSel, shift ; + + if ((pin & PI_GPIO_MASK) == 0) // On-board pin + { + /**/ if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + else if (wiringPiMode != WPI_MODE_GPIO) + return ; + + fSel = gpioToGPFSEL [pin] ; + shift = gpioToShift [pin] ; + + *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ; + } +} + /* * pinMode: diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 84042e9..ce4680a 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -129,6 +129,7 @@ extern int wiringPiSetupSys (void) ; extern int wiringPiSetupGpio (void) ; extern int wiringPiSetupPhys (void) ; +extern void pinModeAlt (int pin, int mode) ; extern void pinMode (int pin, int mode) ; extern void pullUpDnControl (int pin, int pud) ; extern int digitalRead (int pin) ; From 05e2f67e7f43d83195fff108bcd952e7afe917ce Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 20 May 2014 11:43:07 +0100 Subject: [PATCH 60/97] changed to pin mode to support softPwm. bugfix in blink.sh - wring pin improving the maxdetect routing - a little. gpio pins --- examples/blink.sh | 2 +- examples/blink6drcs.c | 115 ++++++++++++++++++++++++++++++++++++++++++ examples/rht03.c | 5 ++ gpio/Makefile | 12 ++--- gpio/gpio.1 | 27 +++++++++- gpio/gpio.c | 9 +++- gpio/pins.c | 70 +++++++++++++++++++++++++ wiringPi/softPwm.c | 49 +++++++++++++++--- wiringPi/softPwm.h | 1 + wiringPi/wiringPi.c | 11 +++- wiringPi/wiringPi.h | 1 + 11 files changed, 281 insertions(+), 21 deletions(-) create mode 100644 examples/blink6drcs.c create mode 100644 gpio/pins.c diff --git a/examples/blink.sh b/examples/blink.sh index 2aa378a..7755383 100644 --- a/examples/blink.sh +++ b/examples/blink.sh @@ -25,7 +25,7 @@ # LED Pin - wiringPi pin 0 is BCM_GPIO 17. -LED=0 +PIN=0 gpio mode $PIN out diff --git a/examples/blink6drcs.c b/examples/blink6drcs.c new file mode 100644 index 0000000..32f4921 --- /dev/null +++ b/examples/blink6drcs.c @@ -0,0 +1,115 @@ +/* + * blink6drcs.c: + * Simple sequence over 6 pins on a remote DRC board. + * Aimed at the Gertduino, but it's fairly generic. + * This version uses DRC to talk to the ATmega on the Gertduino + * + * Copyright (c) 2012-2014 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 + +#define GERT_BASE 100 + +static int pinMap [] = +{ + GERT_BASE + 6, GERT_BASE + 5, GERT_BASE + 3, GERT_BASE + 10, GERT_BASE + 9, GERT_BASE + 13, +} ; + +// Simple sequencer data +// Triplets of LED, On/Off and delay + + +int data [] = +{ + 0, 1, 1, + 1, 1, 1, + 0, 0, 0, 2, 1, 1, + 1, 0, 0, 3, 1, 1, + 2, 0, 0, 4, 1, 1, + 3, 0, 0, 5, 1, 1, + 4, 0, 1, + 5, 0, 1, + + 0, 0, 1, // Extra delay + +// Back again + + 5, 1, 1, + 4, 1, 1, + 5, 0, 0, 3, 1, 1, + 4, 0, 0, 2, 1, 1, + 3, 0, 0, 1, 1, 1, + 2, 0, 0, 0, 1, 1, + 1, 0, 1, + 0, 0, 1, + + 0, 0, 1, // Extra delay + + 0, 9, 0, // End marker + +} ; + + +int main (void) +{ + int pin ; + int dataPtr ; + int l, s, d ; + + printf ("Raspberry Pi - 6-LED Sequence\n") ; + printf ("=============================\n") ; + printf ("\n") ; + printf (" Use the 2 buttons to temporarily speed up the sequence\n") ; + + wiringPiSetupSys () ; // Not using the Pi's GPIO here + drcSetupSerial (GERT_BASE, 20, "/dev/ttyAMA0", 115200) ; + + for (pin = 0 ; pin < 6 ; ++pin) + pinMode (pinMap [pin], OUTPUT) ; + + pinMode (GERT_BASE + 16, INPUT) ; // Buttons + pinMode (GERT_BASE + 17, INPUT) ; + + pullUpDnControl (GERT_BASE + 16, PUD_UP) ; + pullUpDnControl (GERT_BASE + 17, PUD_UP) ; + + dataPtr = 0 ; + + for (;;) + { + l = data [dataPtr++] ; // LED + s = data [dataPtr++] ; // State + d = data [dataPtr++] ; // Duration (10ths) + + if (s == 9) // 9 -> End Marker + { + dataPtr = 0 ; + continue ; + } + + digitalWrite (pinMap [l], s) ; + delay (d * digitalRead (GERT_BASE + 16) * 15 + digitalRead (GERT_BASE + 17) * 20) ; + } + + return 0 ; +} diff --git a/examples/rht03.c b/examples/rht03.c index e0cc116..566e954 100644 --- a/examples/rht03.c +++ b/examples/rht03.c @@ -56,6 +56,11 @@ int main (void) { temp = newTemp ; rh = newRh ; + if ((temp & 0x8000) != 0) // Negative + { + temp &= 0x7FFF ; + temp = -temp ; + } printf ("Temp: %5.1f, RH: %5.1f%%\n", temp / 10.0, rh / 10.0) ; } } diff --git a/gpio/Makefile b/gpio/Makefile index 8fa12d5..37b4887 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -38,7 +38,7 @@ LIBS = -lwiringPi -lwiringPiDev -lpthread -lm # May not need to alter anything below this line ############################################################################### -SRC = gpio.c extensions.c readall.c +SRC = gpio.c extensions.c readall.c pins.c OBJ = $(SRC:.c=.o) @@ -52,17 +52,17 @@ gpio: $(OBJ) @echo [Compile] $< @$(CC) -c $(CFLAGS) $< -o $@ -.PHONEY: clean +.PHONY: clean clean: @echo "[Clean]" @rm -f $(OBJ) gpio *~ core tags *.bak -.PHONEY: tags +.PHONY: tags tags: $(SRC) @echo [ctags] @ctags $(SRC) -.PHONEY: install +.PHONY: install install: @echo "[Install]" @cp gpio $(DESTDIR)$(PREFIX)/bin @@ -71,13 +71,13 @@ install: @mkdir -p $(DESTDIR)$(PREFIX)/man/man1 @cp gpio.1 $(DESTDIR)$(PREFIX)/man/man1 -.PHONEY: uninstall +.PHONY: uninstall uninstall: @echo "[UnInstall]" @rm -f $(DESTDIR)$(PREFIX)/bin/gpio @rm -f $(DESTDIR)$(PREFIX)/man/man1/gpio.1 -.PHONEY: depend +.PHONY: depend depend: makedepend -Y $(SRC) diff --git a/gpio/gpio.1 b/gpio/gpio.1 index e1cba32..7300466 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -114,6 +114,28 @@ respective logic levels. Write the given value (0 or 1) to the pin. You need to set the pin to output mode first. +.TP +.B aread +Read the analog value of the given pin. This needs to be uses in +conjunction with a -x flag to add in an extension that handles analog +inputs. respective logic levels. + +e.g. gpio -x mcp3002:200:0 aread 200 + +will read the first analog input on an mcp3002 SPI ADC chip. + +.TP +.B awrite +Write the analog value to the given pin. This needs to be used in +conjunction with a -x flag to add in an extension that handles analog +inputs. respective logic levels. + +e.g. gpio -x mcp4802:200:0 awrite 200 128 + +will write the value 128 to the first DAC port on an mcp4802 chip on +the Pi's SPI bus 0. + + .TP .B wb Write the given byte to the 8 main GPIO pins. You can prefix it with 0x @@ -132,8 +154,9 @@ digital and analog read on each pin in-turn. .TP .B reset -Resets the GPIO - As much as it's possible to do. All pins are set to input -mode and all the internal pull-up/down resistors are disconnected (tristate mode). +Resets the GPIO - As much as it's possible to do. All pins are set to +input mode and all the internal pull-up/down resistors are disconnected +(tristate mode). The reset command is usable with an extension module (via the -x parameter), but it's limited to turning the pin into input mode (if applicable) and diff --git a/gpio/gpio.c b/gpio/gpio.c index b65af0e..d941db5 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -43,15 +43,19 @@ extern int wiringPiDebug ; +// External functions I can't be bothered creating a separate .h file for: + extern void doReadall (void) ; extern void doReadallOld (void) ; +extern void doPins (void) ; + #ifndef TRUE # define TRUE (1==1) # define FALSE (1==2) #endif -#define VERSION "2.13" +#define VERSION "2.14" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; @@ -1078,7 +1082,7 @@ int main (int argc, char *argv []) if (strcmp (argv [1], "-v") == 0) { printf ("gpio version: %s\n", VERSION) ; - printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ; + printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ; printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ; printf ("For details type: %s -warranty\n", argv [0]) ; printf ("\n") ; @@ -1220,6 +1224,7 @@ int main (int argc, char *argv []) else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; else if (strcasecmp (argv [1], "readall" ) == 0) doReadallOld () ; else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ; + else if (strcasecmp (argv [1], "pins" ) == 0) doPins () ; else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ; else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ; else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ; diff --git a/gpio/pins.c b/gpio/pins.c new file mode 100644 index 0000000..6df3cb2 --- /dev/null +++ b/gpio/pins.c @@ -0,0 +1,70 @@ +/* + * pins.c: + * Just display a handy Pi pinnout diagram. + * Copyright (c) 2012-2013 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see . + *********************************************************************** + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern int wpMode ; + +void doPins (void) +{ + printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ; + + printf ( +" +-----+--------+------------+--------+-----+\n" +" | Pin | Name || P1 Pin || Name | Pin |\n" +" +-----+--------+------------+--------+-----+\n" +" | | 3.3v || 1 oo 2 || 5v | |\n" +" | 8 | SDA || 3 oo 4 || 5v | |\n" +" | 9 | SCL || 5 oo 6 || Gnd | |\n" +" | 7 | GPIO 7 || 7 oo 8 || TxD | 15 |\n" +" | | GND || 9 oo 10 || RxD | 16 |\n" +" | 0 | GPIO 0 || 11 oo 12 || GPIO 1 | 1 |\n" +" | 2 | GPIO 2 || 13 oo 14 || Gnd | |\n" +" | 3 | GPIO 3 || 15 oo 16 || GPIO 4 | 4 |\n" +" | | 3.3v || 17 oo 18 || GPIO 5 | 5 |\n" +" | 12 | MOSI || 19 oo 20 || Gnd | |\n" +" | 13 | MISO || 21 oo 22 || GPIO 6 | 6 |\n" +" | 14 | SCLK || 23 oo 24 || CE 0 | 10 |\n" +" | | Gnd || 25 oo 26 || CE 1 | 11 |\n" +" +-----+--------+------------+--------+-----+\n") ; + +/*** + +--- + | 5v| 5v| Gnd | TxD | RxD | G1 | Gnd | G4 | G5 | G + | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 |\n" + | 1 | 3 | 5 | 7 | 9 | 11 | 13 | 15 | 17 | 19 | 21 | 23 | 25 |\n" +***/ + +} diff --git a/wiringPi/softPwm.c b/wiringPi/softPwm.c index a4f0fc4..0bde180 100644 --- a/wiringPi/softPwm.c +++ b/wiringPi/softPwm.c @@ -1,7 +1,7 @@ /* * softPwm.c: * Provide 2 channels of software driven PWM. - * Copyright (c) 2012 Gordon Henderson + * Copyright (c) 2012-2014 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -28,17 +28,22 @@ #include "wiringPi.h" #include "softPwm.h" +// MAX_PINS: +// This is more than the number of Pi pins because we can actually softPwm +// pins that are on GPIO expanders. It's not that efficient and more than 1 or +// 2 pins on e.g. (SPI) mcp23s17 won't really be that effective, however... + #define MAX_PINS 1024 // The PWM Frequency is derived from the "pulse time" below. Essentially, // the frequency is a function of the range and this pulse time. -// The total period will be range * pulse time in uS, so a pulse time -// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 uS +// The total period will be range * pulse time in µS, so a pulse time +// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 µS // which is a frequency of 100Hz. // // It's possible to get a higher frequency by lowering the pulse time, // however CPU uage will skyrocket as wiringPi uses a hard-loop to time -// periods under 100uS - this is because the Linux timer calls are just +// periods under 100µS - this is because the Linux timer calls are just // accurate at all, and have an overhead. // // Another way to increase the frequency is to reduce the range - however @@ -46,8 +51,9 @@ #define PULSE_TIME 100 -static int marks [MAX_PINS] ; -static int range [MAX_PINS] ; +static int marks [MAX_PINS] ; +static int range [MAX_PINS] ; +static pthread_t threads [MAX_PINS] ; int newPin = -1 ; @@ -106,13 +112,20 @@ void softPwmWrite (int pin, int value) /* * softPwmCreate: - * Create a new PWM thread. + * Create a new softPWM thread. ********************************************************************************* */ int softPwmCreate (int pin, int initialValue, int pwmRange) { int res ; + pthread_t myThread ; + + if (range [pin] != 0) // Already running on this pin + return -1 ; + + if (range <= 0) + return -1 ; pinMode (pin, OUTPUT) ; digitalWrite (pin, LOW) ; @@ -121,10 +134,30 @@ int softPwmCreate (int pin, int initialValue, int pwmRange) range [pin] = pwmRange ; newPin = pin ; - res = piThreadCreate (softPwmThread) ; + res = pthread_create (&myThread, NULL, softPwmThread, NULL) ; while (newPin != -1) delay (1) ; + threads [pin] = myThread ; + return res ; } + + +/* + * softPwmStop: + * Stop an existing softPWM thread + ********************************************************************************* + */ + +void softPwmStop (int pin) +{ + if (range [pin] != 0) + { + pthread_cancel (threads [pin]) ; + pthread_join (threads [pin], NULL) ; + range [pin] = 0 ; + digitalWrite (pin, LOW) ; +} +} diff --git a/wiringPi/softPwm.h b/wiringPi/softPwm.h index 28ad299..0351da5 100644 --- a/wiringPi/softPwm.h +++ b/wiringPi/softPwm.h @@ -28,6 +28,7 @@ extern "C" { extern int softPwmCreate (int pin, int value, int range) ; extern void softPwmWrite (int pin, int value) ; +extern void softPwmStop (int pin) ; #ifdef __cplusplus } diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index b54ad29..4660a67 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -70,6 +70,8 @@ #include #include +#include "softPwm.h" + #include "wiringPi.h" #ifndef TRUE @@ -961,6 +963,7 @@ void pinMode (int pin, int mode) { int fSel, shift, alt ; struct wiringPiNodeStruct *node = wiringPiNodes ; + int origPin = pin ; if ((pin & PI_GPIO_MASK) == 0) // On-board pin { @@ -971,6 +974,8 @@ void pinMode (int pin, int mode) else if (wiringPiMode != WPI_MODE_GPIO) return ; + softPwmStop (origPin) ; + fSel = gpioToGPFSEL [pin] ; shift = gpioToShift [pin] ; @@ -978,9 +983,11 @@ void pinMode (int pin, int mode) *(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 == SOFT_PWM_OUTPUT) + softPwmCreate (origPin, 0, 100) ; else if (mode == PWM_OUTPUT) { - if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin + if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin return ; // Set pin to PWM mode @@ -990,7 +997,7 @@ void pinMode (int pin, int mode) pwmSetMode (PWM_MODE_BAL) ; // Pi default mode pwmSetRange (1024) ; // Default range of 1024 - pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM + pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM } else if (mode == GPIO_CLOCK) { diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index ce4680a..4eea3e5 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -42,6 +42,7 @@ #define OUTPUT 1 #define PWM_OUTPUT 2 #define GPIO_CLOCK 3 +#define SOFT_PWM_OUTPUT 4 #define LOW 0 #define HIGH 1 From 6fba403b2f749ae0ea64019037fc692f2de1483d Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 24 Jun 2014 19:23:31 +0100 Subject: [PATCH 61/97] Updates for the Raspnerry Pi Compute Module - changes to the gpio program to produce a nicer 'readall' output too. It also understands new pins 53,54,55 and 56 in wiringPiSetupPhys() mode to represent the pins on the P5 connector on the Rev 2 Pi's Changes to pinMode() to allow new modes SOFT_PWM and SOFT_TONE. --- gpio/gpio.c | 9 ++- gpio/pins.c | 11 ++++ gpio/readall.c | 59 +++++++++++++++-- wiringPi/softPwm.c | 8 ++- wiringPi/softTone.c | 33 +++++++++- wiringPi/softTone.h | 1 + wiringPi/wiringPi.c | 156 ++++++++++++++++++++++++++++++++++++++------ wiringPi/wiringPi.h | 15 +++++ 8 files changed, 260 insertions(+), 32 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index d941db5..c6021d4 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -79,6 +79,7 @@ char *usage = "Usage: gpio -v\n" " gpio gbw " ; // No trailing newline needed here. +#ifdef NOT_FOR_NOW /* * decodePin: * Decode a pin "number" which can actually be a pin name to represent @@ -96,6 +97,7 @@ static int decodePin (const char *str) return 0 ; } +#endif /* @@ -1042,6 +1044,8 @@ static void doPwmClock (int argc, char *argv []) int main (int argc, char *argv []) { int i ; + int model, rev, mem ; + char *maker ; if (getenv ("WIRINGPI_DEBUG") != NULL) { @@ -1086,7 +1090,10 @@ int main (int argc, char *argv []) printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ; printf ("For details type: %s -warranty\n", argv [0]) ; printf ("\n") ; - printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ; + piBoardId (&model, &rev, &mem, &maker) ; + printf ("Raspberry Pi Details:\n") ; + printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s\n", + piModelNames [model], piRevisionNames [rev], mem, maker) ; return 0 ; } diff --git a/gpio/pins.c b/gpio/pins.c index 6df3cb2..457922d 100644 --- a/gpio/pins.c +++ b/gpio/pins.c @@ -39,6 +39,17 @@ extern int wpMode ; void doPins (void) { + int model, rev, mem ; + char *maker ; + + piBoardId (&model, &rev, &mem, &maker) ; + if (model == PI_MODEL_CM) + { + printf ("This Raspberry Pi is a Compute Module.\n") ; + printf (" (who knows what's been done to the pins!)\n") ; + return ; + } + printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ; printf ( diff --git a/gpio/readall.c b/gpio/readall.c index 8fc9f88..b363d3b 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -37,6 +37,11 @@ extern int wpMode ; +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (1==2) +#endif + /* * doReadallExternal: * A relatively crude way to read the pins on an external device. @@ -90,7 +95,7 @@ static int wpiToPhys [64] = 3, 5, // 8...9 24, 26, 19, 21, 23, // 10..14 8, 10, // 15..16 - 3, 4, 5, 6, // 17..20 + 53, 54, 55, 56, // 17..20 0,0,0,0,0,0,0,0,0,0,0, // 20..31 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63 @@ -115,11 +120,11 @@ static int physToWpi [64] = 14, 10, -1, 11, // 25, 26 -// Padding: - -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 + -1, -1, -1, -1, -1, // ... 52 + 28, 29, 30, 31, // ... 53, 54, 55, 56 - P5 + -1, -1, -1, -1, -1, -1, -1, // ... 63 } ; static char *physNames [64] = @@ -140,9 +145,11 @@ static char *physNames [64] = "SCLK", "CE0", "0v", "CE1", - NULL,NULL,NULL,NULL,NULL, - NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, - NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, + NULL,NULL,NULL,NULL,NULL, // ... 31 + NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, // ... 47 + NULL,NULL,NULL,NULL,NULL, // ... 52 + "GPIO8", "GPIO9", "GPIO10", "GPIO11", // ... 53, 54, 55, 56 - P5 + NULL,NULL,NULL,NULL,NULL,NULL, // ... 63 } ; static void readallPhys (int physPin) @@ -225,6 +232,38 @@ static void readallPhys (int physPin) } +int cmReadall (void) +{ + int model, rev, mem ; + int pin ; + char *maker ; + + piBoardId (&model, &rev, &mem, &maker) ; + if (model != PI_MODEL_CM) + return FALSE ; + + printf ("+-----+------+-------+ +-----+------+-------+\n") ; + printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ; + printf ("+-----+------+-------+ +-----+------+-------+\n") ; + + for (pin = 0 ; pin < 28 ; ++pin) + { + printf ("| %3d ", pin) ; + printf ("| %-4s ", alts [getAlt (pin)]) ; + printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ; + printf ("| ") ; + printf ("| %3d ", pin + 28) ; + printf ("| %-4s ", alts [getAlt (pin + 28)]) ; + printf ("| %s ", digitalRead (pin + 28) == HIGH ? "High" : "Low ") ; + printf ("|\n") ; + } + + printf ("+-----+------+-------+ +-----+------+-------+\n") ; + + return TRUE ; +} + + void doReadall (void) { int pin ; @@ -235,6 +274,9 @@ void doReadall (void) return ; } + if (cmReadall ()) + return ; + /**/ if (wpMode == WPI_MODE_GPIO) { printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ; @@ -275,6 +317,9 @@ void doReadallOld (void) return ; } + if (cmReadall ()) + return ; + printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ; printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ; printf ("+----------+------+------+--------+------+-------+\n") ; diff --git a/wiringPi/softPwm.c b/wiringPi/softPwm.c index 0bde180..3c79ba7 100644 --- a/wiringPi/softPwm.c +++ b/wiringPi/softPwm.c @@ -67,11 +67,15 @@ int newPin = -1 ; static PI_THREAD (softPwmThread) { int pin, mark, space ; + struct sched_param param ; + + param.sched_priority = sched_get_priority_max (SCHED_RR) ; + pthread_setschedparam (pthread_self (), SCHED_RR, ¶m) ; pin = newPin ; newPin = -1 ; - piHiPri (50) ; + piHiPri (90) ; for (;;) { @@ -159,5 +163,5 @@ void softPwmStop (int pin) pthread_join (threads [pin], NULL) ; range [pin] = 0 ; digitalWrite (pin, LOW) ; -} + } } diff --git a/wiringPi/softTone.c b/wiringPi/softTone.c index b4a89f8..e2fb737 100644 --- a/wiringPi/softTone.c +++ b/wiringPi/softTone.c @@ -36,7 +36,8 @@ #define PULSE_TIME 100 -static int freqs [MAX_PINS] ; +static int freqs [MAX_PINS] ; +static pthread_t threads [MAX_PINS] ; static int newPin = -1 ; @@ -50,6 +51,10 @@ static int newPin = -1 ; static PI_THREAD (softToneThread) { int pin, freq, halfPeriod ; + struct sched_param param ; + + param.sched_priority = sched_get_priority_max (SCHED_RR) ; + pthread_setschedparam (pthread_self (), SCHED_RR, ¶m) ; pin = newPin ; newPin = -1 ; @@ -105,17 +110,41 @@ void softToneWrite (int pin, int freq) int softToneCreate (int pin) { int res ; + pthread_t myThread ; pinMode (pin, OUTPUT) ; digitalWrite (pin, LOW) ; + if (threads [pin] != 0) + return -1 ; + freqs [pin] = 0 ; newPin = pin ; - res = piThreadCreate (softToneThread) ; + res = pthread_create (&myThread, NULL, softToneThread, NULL) ; while (newPin != -1) delay (1) ; + threads [pin] = myThread ; + return res ; } + + +/* + * softToneStop: + * Stop an existing softTone thread + ********************************************************************************* + */ + +void softToneStop (int pin) +{ + if (threads [pin] != 0) + { + pthread_cancel (threads [pin]) ; + pthread_join (threads [pin], NULL) ; + threads [pin] = 0 ; + digitalWrite (pin, LOW) ; + } +} diff --git a/wiringPi/softTone.h b/wiringPi/softTone.h index d8b4e54..a93c5af 100644 --- a/wiringPi/softTone.h +++ b/wiringPi/softTone.h @@ -31,6 +31,7 @@ extern "C" { #endif extern int softToneCreate (int pin) ; +extern void softToneStop (int pin) ; extern void softToneWrite (int pin, int freq) ; #ifdef __cplusplus diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 4660a67..63a8946 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -71,6 +71,7 @@ #include #include "softPwm.h" +#include "softTone.h" #include "wiringPi.h" @@ -272,6 +273,7 @@ static int pinToGpioR2 [64] = // physToGpio: // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin // Cope for 2 different board revisions here. +// Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56 static int *physToGpio ; @@ -292,8 +294,6 @@ static int physToGpioR1 [64] = 11, 8, -1, 7, // 25, 26 -// Padding: - -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 @@ -316,11 +316,13 @@ static int physToGpioR2 [64] = 11, 8, -1, 7, // 25, 26 -// Padding: +// the P5 connector on the Rev 2 boards: -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 + -1, -1, -1, -1, -1, // ... 52 + 28, 29, 30, 31, // ... 53, 54, 55, 56 - P5 + -1, -1, -1, -1, -1, -1, -1, // ... 63 } ; @@ -547,22 +549,30 @@ int wiringPiFailure (int fatal, const char *message, ...) * * Much confusion here )-: * Seems there are some boards with 0000 in them (mistake in manufacture) - * and some board with 0005 in them (another mistake in manufacture?) * So the distinction between boards that I can see is: * 0000 - Error - * 0001 - Not used - * 0002 - Rev 1 - * 0003 - Rev 1 - * 0004 - Rev 2 (Early reports? - * 0005 - Rev 2 (but error?) - * 0006 - Rev 2 - * 0008 - Rev 2 - Model A - * 000e - Rev 2 + 512MB - * 000f - Rev 2 + 512MB + * 0001 - Not used (Compute - default to Rev 2) + * 0002 - Model B, Rev 1, 256MB + * 0003 - Model B, Rev 1.1, 256MB, Fuses/D14 removed. + * 0004 - Model B, Rev 2, 256MB, Sony + * 0005 - Model B, Rev 2, 256MB, Qisda + * 0006 - Model B, Rev 2, 256MB, Egoman + * 0007 - Model A, Rev 2, 256MB, Egoman + * 0008 - Model A, Rev 2, 256MB, Sony + * 0009 - Model A, Rev 2, 256MB, Qisda + * 000d - Model B, Rev 2, 512MB, Egoman + * 000e - Model B, Rev 2, 512MB, Sony + * 000f - Model B, Rev 2, 512MB, Qisda + * 0011 - Pi compute Module * * A small thorn is the olde style overvolting - that will add in * 1000000 * + * The Pi compute module has an revision of 0011 - since we only check the + * last digit, then it's 1, therefore it'll default to not 2 or 3 for a + * Rev 1, so will appear as a Rev 2. This is fine for the most part, but + * we'll properly detect the Compute Module later and adjust accordingly. + * ********************************************************************************* */ @@ -634,6 +644,89 @@ int piBoardRev (void) } +/* + * piBoardId: + * Do more digging into the board revision string as above, but return + * as much details as we can. + ********************************************************************************* + */ + +const char *piModelNames [] = +{ + "Model A", + "Model B", + "Compute Module", +} ; + +const char *piRevisionNames[] = +{ + "1", + "1.1", + "2", +} ; + +void piBoardId (int *model, int *rev, int *mem, char **maker) +{ + FILE *cpuFd ; + char line [120] ; + char *c ; + + piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result. + + if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) + piBoardRevOops ("Unable to open /proc/cpuinfo") ; + + while (fgets (line, 120, cpuFd) != NULL) + if (strncmp (line, "Revision", 8) == 0) + break ; + + fclose (cpuFd) ; + + if (strncmp (line, "Revision", 8) != 0) + piBoardRevOops ("No \"Revision\" line") ; + +// Chomp trailing CR/NL + + for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c) + *c = 0 ; + + if (wiringPiDebug) + printf ("piboardId: Revision string: %s\n", line) ; + +// Scan to first digit + + for (c = line ; *c ; ++c) + if (isdigit (*c)) + break ; + +// Make sure its long enough + + if (strlen (c) < 4) + piBoardRevOops ("Bogus \"Revision\" line") ; + +// Extract last 4 characters: + + c = c + strlen (c) - 4 ; + +// Fill out the replys as appropriate + + /**/ if (strcmp (c, "0002") == 0) { *model = 1 ; *rev = 0 ; *mem = 256 ; *maker = "China" ; } + else if (strcmp (c, "0003") == 0) { *model = 1 ; *rev = 1 ; *mem = 256 ; *maker = "China" ; } + else if (strcmp (c, "0004") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Sony" ; } + else if (strcmp (c, "0005") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Qisda" ; } + else if (strcmp (c, "0006") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Egoman" ; } + else if (strcmp (c, "0007") == 0) { *model = 0 ; *rev = 2 ; *mem = 256 ; *maker = "Egoman" ; } + else if (strcmp (c, "0008") == 0) { *model = 0 ; *rev = 2 ; *mem = 256 ; *maker = "Sony" ; } + else if (strcmp (c, "0009") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Qisda" ; } + else if (strcmp (c, "000d") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; } + else if (strcmp (c, "000e") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Sony" ; } + else if (strcmp (c, "000f") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; } + else if (strcmp (c, "0011") == 0) { *model = 2 ; *rev = 1 ; *mem = 512 ; *maker = "Sony" ; } + else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = "Unkn" ; } +} + + + /* * wpiPinToGpio: * Translate a wiringPi Pin number to native GPIO pin number. @@ -974,7 +1067,8 @@ void pinMode (int pin, int mode) else if (wiringPiMode != WPI_MODE_GPIO) return ; - softPwmStop (origPin) ; + softPwmStop (origPin) ; + softToneStop (origPin) ; fSel = gpioToGPFSEL [pin] ; shift = gpioToShift [pin] ; @@ -985,6 +1079,8 @@ void pinMode (int pin, int mode) *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ; else if (mode == SOFT_PWM_OUTPUT) softPwmCreate (origPin, 0, 100) ; + else if (mode == SOFT_TONE_OUTPUT) + softToneCreate (origPin) ; else if (mode == PWM_OUTPUT) { if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin @@ -1376,8 +1472,18 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) if (pid == 0) // Child, exec { - execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ; - return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ; + if (access ("/usr/local/bin/gpio", X_OK)) + { + execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ; + return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ; + } + else if (access ("/usr/bin/gpio", X_OK)) + { + execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ; + return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ; + } + else + return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ; } else // Parent, wait wait (NULL) ; @@ -1538,13 +1644,17 @@ unsigned int micros (void) * * Default setup: Initialises the system into wiringPi Pin mode and uses the * memory mapped hardware directly. + * + * Changed now to revert to "gpio" mode if we're running on a Compute Module. ********************************************************************************* */ int wiringPiSetup (void) { - int fd ; - int boardRev ; + int fd ; + int boardRev ; + int model, rev, mem ; + char *maker ; if (getenv (ENV_DEBUG) != NULL) wiringPiDebug = TRUE ; @@ -1618,7 +1728,13 @@ int wiringPiSetup (void) initialiseEpoch () ; - wiringPiMode = WPI_MODE_PINS ; +// If we're running on a compute module, then wiringPi pin numbers don't really many anything... + + piBoardId (&model, &rev, &mem, &maker) ; + if (model == PI_MODEL_CM) + wiringPiMode = WPI_MODE_GPIO ; + else + wiringPiMode = WPI_MODE_PINS ; return 0 ; } diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 4eea3e5..5117cc9 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -43,6 +43,7 @@ #define PWM_OUTPUT 2 #define GPIO_CLOCK 3 #define SOFT_PWM_OUTPUT 4 +#define SOFT_TONE_OUTPUT 5 #define LOW 0 #define HIGH 1 @@ -65,6 +66,13 @@ #define INT_EDGE_RISING 2 #define INT_EDGE_BOTH 3 +// Pi model types + +#define PI_MODEL_A 0 +#define PI_MODEL_B 1 +#define PI_MODEL_CM 2 + + // Threads #define PI_THREAD(X) void *X (void *dummy) @@ -116,6 +124,12 @@ extern struct wiringPiNodeStruct *wiringPiNodes ; extern "C" { #endif +// Data + +extern const char *piModelNames [] ; +extern const char *piRevisionNames[] ; +extern const char *piComputeRevisionNames[] ; + // Internal extern int wiringPiFailure (int fatal, const char *message, ...) ; @@ -148,6 +162,7 @@ extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio // On-Board Raspberry Pi hardware specific stuff extern int piBoardRev (void) ; +extern void piBoardId (int *model, int *rev, int *mem, char **maker) ; extern int wpiPinToGpio (int wpiPin) ; extern int physPinToGpio (int physPin) ; extern void setPadDrive (int group, int value) ; From 7cbb8865c8dc6675618a17492a1175c278d38685 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 24 Jun 2014 19:25:42 +0100 Subject: [PATCH 62/97] Bumped version to 2.15 --- gpio/gpio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index c6021d4..b2f9953 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -55,7 +55,7 @@ extern void doPins (void) ; # define FALSE (1==2) #endif -#define VERSION "2.14" +#define VERSION "2.15" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; From 0150b024789f6b7356a738334d3b325a50297452 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Fri, 27 Jun 2014 10:55:39 +0100 Subject: [PATCH 63/97] Fixed a small bug in the ISR code where it was looking for the gpio program. Should now work correctly on Arch, or other systems that install gpio as /usr/bin/gpio. --- gpio/gpio.c | 2 +- wiringPi/wiringPi.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index b2f9953..b9d1799 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -55,7 +55,7 @@ extern void doPins (void) ; # define FALSE (1==2) #endif -#define VERSION "2.15" +#define VERSION "2.16" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 63a8946..f0e8282 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1472,12 +1472,12 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) if (pid == 0) // Child, exec { - if (access ("/usr/local/bin/gpio", X_OK)) + /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0) { execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ; return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ; } - else if (access ("/usr/bin/gpio", X_OK)) + else if (access ("/usr/bin/gpio", X_OK) == 0) { execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ; return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ; From dca8a19fb816b1314008d84c092b11148a85715c Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 14 Jul 2014 08:39:38 +0100 Subject: [PATCH 64/97] Updated mostly to the gpio readall command to support the Raspberry Pi B+ --- gpio/gpio.c | 52 +++++++++++++++++------ gpio/readall.c | 101 +++++++++++++++++++++++++++++++++----------- wiringPi/wiringPi.c | 67 ++++++++++++++++++++++++----- wiringPi/wiringPi.h | 6 ++- 4 files changed, 176 insertions(+), 50 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index b9d1799..be22e0e 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -55,7 +55,7 @@ extern void doPins (void) ; # define FALSE (1==2) #endif -#define VERSION "2.16" +#define VERSION "2.18" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; @@ -654,16 +654,17 @@ void doMode (int argc, char *argv []) mode = argv [3] ; - /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; - else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ; - else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ; - else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ; - else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ; - else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ; - else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ; - else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; - else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; - else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ; + /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ; + else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ; + else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ; + else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ; + else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ; + else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ; + else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ; + else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ; + else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; + else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; + else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ; // Undocumented @@ -742,7 +743,7 @@ static void doGbw (int argc, char *argv []) exit (1) ; } - if ((value < 0) || (value > 1023)) + if ((value < 0) || (value > 255)) { fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ; exit (1) ; @@ -933,6 +934,30 @@ void doToggle (int argc, char *argv []) digitalWrite (pin, !digitalRead (pin)) ; } + +/* + * doPwmTone: + * Output a tone in a PWM pin + ********************************************************************************* + */ + +void doPwmTone (int argc, char *argv []) +{ + int pin, freq ; + + if (argc != 4) + { + fprintf (stderr, "Usage: %s pwmTone \n", argv [0]) ; + exit (1) ; + } + + pin = atoi (argv [2]) ; + freq = atoi (argv [3]) ; + + pwmToneWrite (pin, freq) ; +} + + /* * doClock: * Output a clock on a pin @@ -1100,7 +1125,7 @@ int main (int argc, char *argv []) if (strcasecmp (argv [1], "-warranty") == 0) { printf ("gpio version: %s\n", VERSION) ; - printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ; + printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ; printf ("\n") ; printf (" This program is free software; you can redistribute it and/or modify\n") ; printf (" it under the terms of the GNU Leser General Public License as published\n") ; @@ -1228,6 +1253,7 @@ int main (int argc, char *argv []) else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ; else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ; else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ; + else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ; else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; else if (strcasecmp (argv [1], "readall" ) == 0) doReadallOld () ; else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ; diff --git a/gpio/readall.c b/gpio/readall.c index b363d3b..a92e58c 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -119,11 +119,16 @@ static int physToWpi [64] = 13, 6, 14, 10, -1, 11, // 25, 26 - - -1, -1, -1, -1, -1, // ... 31 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47 + 30, 31, // Actually I2C, but not used + 21, -1, + 22, 26, + 23, -1, + 24, 27, + 25, 28, + -1, 29, + -1, -1, -1, -1, -1, -1, -1, // ... 47 -1, -1, -1, -1, -1, // ... 52 - 28, 29, 30, 31, // ... 53, 54, 55, 56 - P5 + 17, 18, 19, 20, // ... 53, 54, 55, 56 - P5 -1, -1, -1, -1, -1, -1, -1, // ... 63 } ; @@ -131,25 +136,34 @@ static char *physNames [64] = { NULL, - "3.3v", "5v", - "SDA", "5V", - "SCL", "0v", - "GPIO7", "TxD", - "0v", "RxD", - "GPIO0", "GPIO1", - "GPIO2", "0v", - "GPIO3", "GPIO4", - "3.3v", "GPIO5", - "MOSI", "0v", - "MISO", "GPIO6", - "SCLK", "CE0", - "0v", "CE1", - - NULL,NULL,NULL,NULL,NULL, // ... 31 - NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, // ... 47 - NULL,NULL,NULL,NULL,NULL, // ... 52 - "GPIO8", "GPIO9", "GPIO10", "GPIO11", // ... 53, 54, 55, 56 - P5 - NULL,NULL,NULL,NULL,NULL,NULL, // ... 63 + " 3.3v", "5v ", + " SDA.1", "5V ", + " SCL.1", "0v ", + "GPIO. 7", "TxD ", + " 0v", "RxD ", + "GPIO. 0", "GPIO. 1", + "GPIO. 2", "0v ", + "GPIO. 3", "GPIO. 4", + " 3.3v", "GPIO. 5", + " MOSI", "0v ", + " MISO", "GPIO. 6", + " SCLK", "CE0 ", + " 0v", "CE1 ", + " SDA.0", "SCL0 ", + "GPIO.21", "0v ", + "GPIO.22", "GPIO.26", + "GPIO.23", "0v ", + "GPIO.24", "GPIO.27", + "GPIO.25", "GPIO.28", + " 0v", "GPIO.29", + NULL, NULL, + NULL, NULL, + NULL, NULL, + NULL, NULL, + NULL, NULL, + "GPIO.17", "GPIO.18", + "GPIO.19", "GPIO.20", + NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, } ; static void readallPhys (int physPin) @@ -171,7 +185,7 @@ static void readallPhys (int physPin) printf (" | %3d", physToWpi [physPin]) ; } - printf (" | %5s", physNames [physPin]) ; + printf (" | %s", physNames [physPin]) ; if (physToWpi [physPin] == -1) printf (" | | ") ; @@ -264,6 +278,39 @@ int cmReadall (void) } +/* + * bPlusReadall: + * Read all the pins on the model B+ + ********************************************************************************* + */ + +int bPlusReadall (void) +{ + int model, rev, mem ; + int pin ; + char *maker ; + char *name ; + + piBoardId (&model, &rev, &mem, &maker) ; + if (model != PI_MODEL_BPLUS) + return FALSE ; + + /**/ if (wpMode == WPI_MODE_GPIO) + name = "BCM" ; + else + name = "wPi" ; + + printf (" +-----+---------+------+----+--B Plus--+----+------+---------+-----+\n") ; + printf (" | %s | Name | Mode | Val| Physical |Val | Mode | Name | %s |\n", name, name) ; + printf (" +-----+---------+------+----+----++----+----+------+---------+-----+\n") ; + for (pin = 1 ; pin <= 40 ; pin += 2) + readallPhys (pin) ; + printf (" +-----+---------+------+----+----++----+----+------+---------+-----+\n") ; + + return TRUE ; +} + + void doReadall (void) { int pin ; @@ -277,6 +324,9 @@ void doReadall (void) if (cmReadall ()) return ; + if (bPlusReadall ()) + return ; + /**/ if (wpMode == WPI_MODE_GPIO) { printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ; @@ -320,6 +370,9 @@ void doReadallOld (void) if (cmReadall ()) return ; + if (bPlusReadall ()) + return ; + printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ; printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ; printf ("+----------+------+------+--------+------+-------+\n") ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index f0e8282..1e1cfe4 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -234,14 +234,16 @@ static void (*isrFunctions [64])(void) ; // pinToGpio: // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin -// Cope for 2 different board revisions here. +// Cope for 3 different board revisions here. static int *pinToGpio ; +// Revision 1, 1.1: + static int pinToGpioR1 [64] = { 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7 - 0, 1, // I2C - SDA0, SCL0 wpi 8 - 9 + 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9 8, 7, // SPI - CE1, CE0 wpi 10 - 11 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14 14, 15, // UART - Tx, Rx wpi 15 - 16 @@ -253,6 +255,8 @@ static int pinToGpioR1 [64] = -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63 } ; +// Revision 2: + static int pinToGpioR2 [64] = { 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7 @@ -260,11 +264,13 @@ static int pinToGpioR2 [64] = 8, 7, // SPI - CE1, CE0 wpi 10 - 11 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14 14, 15, // UART - Tx, Rx wpi 15 - 16 - 28, 29, 30, 31, // New GPIOs 8 though 11 wpi 17 - 20 + 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20 + 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25 + 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29 + 0, 1, // B+ wpi 30, 31 // Padding: - -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 } ; @@ -316,16 +322,24 @@ static int physToGpioR2 [64] = 11, 8, -1, 7, // 25, 26 +// B+ + + 0, 1, + 5, -1, + 6, 12, + 13, -1, + 19, 16, + 26, 20, + -1, 21, + // the P5 connector on the Rev 2 boards: - -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, // ... 47 -1, -1, -1, -1, -1, // ... 52 28, 29, 30, 31, // ... 53, 54, 55, 56 - P5 -1, -1, -1, -1, -1, -1, -1, // ... 63 } ; - // gpioToGPFSEL: // Map a BCM_GPIO pin to it's Function Selection // control port. (GPFSEL 0-5) @@ -395,7 +409,7 @@ static uint8_t gpioToEDS [] = } ; // gpioToREN -// (Word) offset to the Rising edgde ENable register +// (Word) offset to the Rising edge ENable register static uint8_t gpioToREN [] = { @@ -563,6 +577,7 @@ int wiringPiFailure (int fatal, const char *message, ...) * 000d - Model B, Rev 2, 512MB, Egoman * 000e - Model B, Rev 2, 512MB, Sony * 000f - Model B, Rev 2, 512MB, Qisda + * 0010 - Model B+ 512MB, Sony * 0011 - Pi compute Module * * A small thorn is the olde style overvolting - that will add in @@ -655,6 +670,7 @@ const char *piModelNames [] = { "Model A", "Model B", + "Model B+", "Compute Module", } ; @@ -663,6 +679,7 @@ const char *piRevisionNames[] = "1", "1.1", "2", + "1.2", } ; void piBoardId (int *model, int *rev, int *mem, char **maker) @@ -721,7 +738,8 @@ void piBoardId (int *model, int *rev, int *mem, char **maker) else if (strcmp (c, "000d") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; } else if (strcmp (c, "000e") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Sony" ; } else if (strcmp (c, "000f") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; } - else if (strcmp (c, "0011") == 0) { *model = 2 ; *rev = 1 ; *mem = 512 ; *maker = "Sony" ; } + else if (strcmp (c, "0010") == 0) { *model = 2 ; *rev = 3 ; *mem = 512 ; *maker = "Sony" ; } + else if (strcmp (c, "0011") == 0) { *model = 3 ; *rev = 1 ; *mem = 512 ; *maker = "Sony" ; } else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = "Unkn" ; } } @@ -1081,6 +1099,11 @@ void pinMode (int pin, int mode) softPwmCreate (origPin, 0, 100) ; else if (mode == SOFT_TONE_OUTPUT) softToneCreate (origPin) ; + else if (mode == PWM_TONE_OUTPUT) + { + pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode + pwmSetMode (PWM_MODE_MS) ; + } else if (mode == PWM_OUTPUT) { if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin @@ -1306,6 +1329,28 @@ void analogWrite (int pin, int value) } +/* + * pwmToneWrite: + * Pi Specific. + * Output the given frequency on the Pi's PWM pin + ********************************************************************************* + */ + +void pwmToneWrite (int pin, int freq) +{ + int range ; + + if (freq == 0) + pwmWrite (pin, 0) ; // Off + else + { + range = 600000 / freq ; + pwmSetRange (range) ; + pwmWrite (pin, freq / 2) ; + } +} + + /* * digitalWriteByte: @@ -1670,12 +1715,12 @@ int wiringPiSetup (void) boardRev = piBoardRev () ; - if (boardRev == 1) + /**/ if (boardRev == 1) // A, B, Rev 1, 1.1 { pinToGpio = pinToGpioR1 ; physToGpio = physToGpioR1 ; } - else + else // A, B, Rev 2, B+, CM { pinToGpio = pinToGpioR2 ; physToGpio = physToGpioR2 ; diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 5117cc9..78ff87d 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -44,6 +44,7 @@ #define GPIO_CLOCK 3 #define SOFT_PWM_OUTPUT 4 #define SOFT_TONE_OUTPUT 5 +#define PWM_TONE_OUTPUT 6 #define LOW 0 #define HIGH 1 @@ -70,7 +71,8 @@ #define PI_MODEL_A 0 #define PI_MODEL_B 1 -#define PI_MODEL_CM 2 +#define PI_MODEL_BPLUS 2 +#define PI_MODEL_CM 3 // Threads @@ -128,7 +130,6 @@ extern "C" { extern const char *piModelNames [] ; extern const char *piRevisionNames[] ; -extern const char *piComputeRevisionNames[] ; // Internal @@ -167,6 +168,7 @@ extern int wpiPinToGpio (int wpiPin) ; extern int physPinToGpio (int physPin) ; extern void setPadDrive (int group, int value) ; extern int getAlt (int pin) ; +extern void pwmToneWrite (int pin, int freq) ; extern void digitalWriteByte (int value) ; extern void pwmSetMode (int mode) ; extern void pwmSetRange (unsigned int range) ; From df45388f6431f7baba31ac1e8e242d89828637a0 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 17 Jul 2014 22:23:57 +0100 Subject: [PATCH 65/97] Fixed a bug in the gpio readall command on model B's (caused by a side-effect of changing it for the B+) Aded a gpio command: usbp hi/low - to change the usb power --- build | 9 +- gpio/gpio.1 | 15 ++- gpio/gpio.c | 83 +++++++++++++-- gpio/pins.c | 56 +---------- gpio/readall.c | 240 ++++++++++++++++---------------------------- wiringPi/Makefile | 4 +- wiringPi/wiringPi.c | 159 ++++++++++++++++++----------- wiringPi/wiringPi.h | 35 +++++-- 8 files changed, 308 insertions(+), 293 deletions(-) diff --git a/build b/build index e17cf18..bac483a 100755 --- a/build +++ b/build @@ -94,7 +94,10 @@ fi echo echo All Done. echo "" -echo "NOTE: This is wiringPi v2, and if you need to use the lcd, Piface," -echo " Gertboard, MaxDetext, etc. routines then you must change your" -echo " compile scripts to add -lwiringPiDev" +echo "NOTE: To compile programs with wiringPi, you need to add:" +echo " -lwiringPi" +echo " to your compile line(s) To use the Gertboard, MaxDetect, etc." +echo " code (the devLib), you need to also add:" +echo " -lwiringPiDev" +echo " to your compile line(s)." echo "" diff --git a/gpio/gpio.1 b/gpio/gpio.1 index 7300466..e7131c1 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -13,11 +13,11 @@ gpio \- Command-line access to Raspberry Pi's GPIO .PP .B gpio .B [ \-x extension:params ] -.B mode/read/write/aread/awrite/pwm ... +.B mode/read/write/aread/awrite/pwm/pwmTone ... .PP .B gpio .B [ \-p ] -.B read/write/wb +.B read/write/toggle/wb .B ... .PP .B gpio @@ -39,6 +39,10 @@ gpio \- Command-line access to Raspberry Pi's GPIO group value .PP .B gpio +.B usbp +high | low +.PP +.B gpio .B pwm-bal/pwm-ms .PP .B gpio @@ -231,6 +235,13 @@ Change the pad driver value for the given pad group to the supplied drive value. Group is 0, 1 or 2 and value is 0-7. Do not use unless you are absolutely sure you know what you're doing. +.TP +.B usbp +high | low + +Change the USB current limiter to high (1.2 amps) or low (the default, 600mA) +This is only applicable to the model B+ + .TP .B pwm-bal/pwm-ms Change the PWM mode to balanced (the default) or mark:space ratio (traditional) diff --git a/gpio/gpio.c b/gpio/gpio.c index be22e0e..c440874 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -46,8 +46,6 @@ extern int wiringPiDebug ; // External functions I can't be bothered creating a separate .h file for: extern void doReadall (void) ; -extern void doReadallOld (void) ; - extern void doPins (void) ; #ifndef TRUE @@ -55,8 +53,9 @@ extern void doPins (void) ; # define FALSE (1==2) #endif -#define VERSION "2.18" -#define I2CDETECT "/usr/sbin/i2cdetect" +#define VERSION "2.20" +#define PI_USB_POWER_CONTROL 38 +#define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; @@ -75,6 +74,7 @@ char *usage = "Usage: gpio -v\n" " gpio pwmc \n" " gpio load spi/i2c\n" " gpio i2cd/i2cdetect\n" + " gpio usbp high/low\n" " gpio gbr \n" " gpio gbw " ; // No trailing newline needed here. @@ -717,6 +717,58 @@ static void doPadDrive (int argc, char *argv []) } +/* + * doUsbP: + * Control USB Power - High (1.2A) or Low (600mA) + * gpio usbp high/low + ********************************************************************************* + */ + +static void doUsbP (int argc, char *argv []) +{ + int model, rev, mem, maker, overVolted ; + + if (argc != 3) + { + fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ; + exit (1) ; + } + +// Make sure we're on a B+ + + piBoardId (&model, &rev, &mem, &maker, &overVolted) ; + + if (model != PI_MODEL_BP) + { + fprintf (stderr, "USB power contol is applicable to B+ boards only.\n") ; + exit (1) ; + } + +// Need to force BCM_GPIO mode: + + wiringPiSetupGpio () ; + + if ((strcasecmp (argv [2], "high") == 0) || (strcasecmp (argv [2], "hi") == 0)) + { + digitalWrite (PI_USB_POWER_CONTROL, 1) ; + pinMode (PI_USB_POWER_CONTROL, OUTPUT) ; + printf ("Switched to HIGH current USB (1.2A)\n") ; + return ; + } + + if ((strcasecmp (argv [2], "low") == 0) || (strcasecmp (argv [2], "lo") == 0)) + { + digitalWrite (PI_USB_POWER_CONTROL, 0) ; + pinMode (PI_USB_POWER_CONTROL, OUTPUT) ; + printf ("Switched to LOW current USB (600mA)\n") ; + return ; + } + + fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ; + exit (1) ; +} + + /* * doGbw: * gpio gbw channel value @@ -1069,8 +1121,7 @@ static void doPwmClock (int argc, char *argv []) int main (int argc, char *argv []) { int i ; - int model, rev, mem ; - char *maker ; + int model, rev, mem, maker, overVolted ; if (getenv ("WIRINGPI_DEBUG") != NULL) { @@ -1115,10 +1166,19 @@ int main (int argc, char *argv []) printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ; printf ("For details type: %s -warranty\n", argv [0]) ; printf ("\n") ; - piBoardId (&model, &rev, &mem, &maker) ; - printf ("Raspberry Pi Details:\n") ; - printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s\n", - piModelNames [model], piRevisionNames [rev], mem, maker) ; + piBoardId (&model, &rev, &mem, &maker, &overVolted) ; + if (model == PI_MODEL_UNKNOWN) + { + printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ; + printf (" projects@drogon.net\n") ; + printf ("with a copy of your /proc/cpuinfo if possible\n") ; + } + else + { + printf ("Raspberry Pi Details:\n") ; + printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n", + piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ; + } return 0 ; } @@ -1255,7 +1315,8 @@ int main (int argc, char *argv []) else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ; else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ; else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; - else if (strcasecmp (argv [1], "readall" ) == 0) doReadallOld () ; + else if (strcasecmp (argv [1], "usbp" ) == 0) doUsbP (argc, argv) ; + else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ; else if (strcasecmp (argv [1], "pins" ) == 0) doPins () ; else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ; diff --git a/gpio/pins.c b/gpio/pins.c index 457922d..19fcc1a 100644 --- a/gpio/pins.c +++ b/gpio/pins.c @@ -23,59 +23,11 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -extern int wpMode ; void doPins (void) { - int model, rev, mem ; - char *maker ; - - piBoardId (&model, &rev, &mem, &maker) ; - if (model == PI_MODEL_CM) - { - printf ("This Raspberry Pi is a Compute Module.\n") ; - printf (" (who knows what's been done to the pins!)\n") ; - return ; - } - - printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ; - - printf ( -" +-----+--------+------------+--------+-----+\n" -" | Pin | Name || P1 Pin || Name | Pin |\n" -" +-----+--------+------------+--------+-----+\n" -" | | 3.3v || 1 oo 2 || 5v | |\n" -" | 8 | SDA || 3 oo 4 || 5v | |\n" -" | 9 | SCL || 5 oo 6 || Gnd | |\n" -" | 7 | GPIO 7 || 7 oo 8 || TxD | 15 |\n" -" | | GND || 9 oo 10 || RxD | 16 |\n" -" | 0 | GPIO 0 || 11 oo 12 || GPIO 1 | 1 |\n" -" | 2 | GPIO 2 || 13 oo 14 || Gnd | |\n" -" | 3 | GPIO 3 || 15 oo 16 || GPIO 4 | 4 |\n" -" | | 3.3v || 17 oo 18 || GPIO 5 | 5 |\n" -" | 12 | MOSI || 19 oo 20 || Gnd | |\n" -" | 13 | MISO || 21 oo 22 || GPIO 6 | 6 |\n" -" | 14 | SCLK || 23 oo 24 || CE 0 | 10 |\n" -" | | Gnd || 25 oo 26 || CE 1 | 11 |\n" -" +-----+--------+------------+--------+-----+\n") ; - -/*** - +--- - | 5v| 5v| Gnd | TxD | RxD | G1 | Gnd | G4 | G5 | G - | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 |\n" - | 1 | 3 | 5 | 7 | 9 | 11 | 13 | 15 | 17 | 19 | 21 | 23 | 25 |\n" -***/ - + printf ("The pins command has been deprecated - sorry. Please use the\n") ; + printf (" gpio readall\n") ; + printf ("command to get a list of the pinnouts for your Pi.\n") ; } + diff --git a/gpio/readall.c b/gpio/readall.c index a92e58c..76b5cfc 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -75,34 +75,11 @@ static void doReadallExternal (void) ********************************************************************************* */ -static char *pinNames [] = -{ - "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7", - "SDA ", "SCL ", - "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ", - "TxD ", "RxD ", - "GPIO 8", "GPIO 9", "GPIO10", "GPIO11", -} ; - static char *alts [] = { "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3" } ; -static int wpiToPhys [64] = -{ - 11, 12, 13, 15, 16, 18, 22, 7, // 0...7 - 3, 5, // 8...9 - 24, 26, 19, 21, 23, // 10..14 - 8, 10, // 15..16 - 53, 54, 55, 56, // 17..20 - 0,0,0,0,0,0,0,0,0,0,0, // 20..31 - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47 - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63 -} ; - -// The other mappings needed are in wiringPi.c - static int physToWpi [64] = { -1, // 0 @@ -126,10 +103,14 @@ static int physToWpi [64] = 24, 27, 25, 28, -1, 29, - -1, -1, -1, -1, -1, -1, -1, // ... 47 - -1, -1, -1, -1, -1, // ... 52 - 17, 18, 19, 20, // ... 53, 54, 55, 56 - P5 - -1, -1, -1, -1, -1, -1, -1, // ... 63 + -1, -1, + -1, -1, + -1, -1, + -1, -1, + -1, -1, + 17, 18, + 19, 20, + -1, -1, -1, -1, -1, -1, -1, -1, -1 } ; static char *physNames [64] = @@ -149,7 +130,7 @@ static char *physNames [64] = " MISO", "GPIO. 6", " SCLK", "CE0 ", " 0v", "CE1 ", - " SDA.0", "SCL0 ", + " SDA.0", "SCL.0 ", "GPIO.21", "0v ", "GPIO.22", "GPIO.26", "GPIO.23", "0v ", @@ -166,29 +147,27 @@ static char *physNames [64] = NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, } ; + +/* + * readallPhys: + * Given a physical pin output the data on it and the next pin: + *| BCM | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi | BCM | + ********************************************************************************* + */ + static void readallPhys (int physPin) { int pin ; - /**/ if (wpMode == WPI_MODE_GPIO) - { - if (physPinToGpio (physPin) == -1) - printf (" | ") ; - else - printf (" | %3d", physPinToGpio (physPin)) ; - } - else if (wpMode != WPI_MODE_PHYS) - { - if (physToWpi [physPin] == -1) - printf (" | ") ; - else - printf (" | %3d", physToWpi [physPin]) ; - } + if (physPinToGpio (physPin) == -1) + printf (" | | ") ; + else + printf (" | %3d | %3d", physPinToGpio (physPin), physToWpi [physPin]) ; printf (" | %s", physNames [physPin]) ; if (physToWpi [physPin] == -1) - printf (" | | ") ; + printf (" | | ") ; else { /**/ if (wpMode == WPI_MODE_GPIO) @@ -199,7 +178,7 @@ static void readallPhys (int physPin) pin = physToWpi [physPin] ; printf (" | %4s", alts [getAlt (pin)]) ; - printf (" | %s", (digitalRead (pin) == LOW) ? "Lo" : "Hi") ; + printf (" | %d", digitalRead (pin)) ; } // Pin numbers: @@ -211,7 +190,7 @@ static void readallPhys (int physPin) // Same, reversed if (physToWpi [physPin] == -1) - printf (" | | ") ; + printf (" | | ") ; else { /**/ if (wpMode == WPI_MODE_GPIO) @@ -221,40 +200,24 @@ static void readallPhys (int physPin) else pin = physToWpi [physPin] ; - printf (" | %s", (digitalRead (pin) == LOW) ? "Lo" : "Hi") ; + printf (" | %d", digitalRead (pin)) ; printf (" | %-4s", alts [getAlt (pin)]) ; } printf (" | %-5s", physNames [physPin]) ; - /**/ if (wpMode == WPI_MODE_GPIO) - { - if (physPinToGpio (physPin) == -1) - printf (" | ") ; - else - printf (" | %-3d", physPinToGpio (physPin)) ; - } - else if (wpMode != WPI_MODE_PHYS) - { - if (physToWpi [physPin] == -1) - printf (" | ") ; - else - printf (" | %-3d", physToWpi [physPin]) ; - } + if (physToWpi [physPin] == -1) + printf (" | | ") ; + else + printf (" | %-3d | %-3d", physToWpi [physPin], physPinToGpio (physPin)) ; printf (" |\n") ; } -int cmReadall (void) +void cmReadall (void) { - int model, rev, mem ; int pin ; - char *maker ; - - piBoardId (&model, &rev, &mem, &maker) ; - if (model != PI_MODEL_CM) - return FALSE ; printf ("+-----+------+-------+ +-----+------+-------+\n") ; printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ; @@ -273,8 +236,44 @@ int cmReadall (void) } printf ("+-----+------+-------+ +-----+------+-------+\n") ; +} - return TRUE ; + +/* + * abReadall: + * Read all the pins on the model A or B. + ********************************************************************************* + */ + +void abReadall (int model, int rev) +{ + int pin ; + char *type ; + + if (model == PI_MODEL_A) + type = " A" ; + else + if (rev == PI_VERSION_2) + type = "B2" ; + else + type = "B1" ; + + printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ; + printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ; + printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ; + for (pin = 1 ; pin <= 26 ; pin += 2) + readallPhys (pin) ; + + if (rev == PI_VERSION_2) // B version 2 + { + printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ; + for (pin = 51 ; pin <= 54 ; pin += 2) + readallPhys (pin) ; + } + + printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ; + printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ; + printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ; } @@ -284,36 +283,24 @@ int cmReadall (void) ********************************************************************************* */ -int bPlusReadall (void) +void bPlusReadall (void) { - int model, rev, mem ; int pin ; - char *maker ; - char *name ; - piBoardId (&model, &rev, &mem, &maker) ; - if (model != PI_MODEL_BPLUS) - return FALSE ; - - /**/ if (wpMode == WPI_MODE_GPIO) - name = "BCM" ; - else - name = "wPi" ; - - printf (" +-----+---------+------+----+--B Plus--+----+------+---------+-----+\n") ; - printf (" | %s | Name | Mode | Val| Physical |Val | Mode | Name | %s |\n", name, name) ; - printf (" +-----+---------+------+----+----++----+----+------+---------+-----+\n") ; + printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ; + printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ; + printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ; for (pin = 1 ; pin <= 40 ; pin += 2) readallPhys (pin) ; - printf (" +-----+---------+------+----+----++----+----+------+---------+-----+\n") ; - - return TRUE ; + printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ; + printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ; + printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ; } void doReadall (void) { - int pin ; + int model, rev, mem, maker, overVolted ; if (wiringPiNodes != NULL) // External readall { @@ -321,73 +308,14 @@ void doReadall (void) return ; } - if (cmReadall ()) - return ; + piBoardId (&model, &rev, &mem, &maker, &overVolted) ; - if (bPlusReadall ()) - return ; - - /**/ if (wpMode == WPI_MODE_GPIO) - { - printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ; - printf (" | BCM | Name | Mode | Val| Physical |Val | Mode | Name | BCM |\n") ; - printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ; - for (pin = 1 ; pin <= 26 ; pin += 2) - readallPhys (pin) ; - printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ; - } - else if (wpMode == WPI_MODE_PHYS) - { - printf (" +-------+------+----+-Rev%d-----+----+------+-------+\n", piBoardRev ()) ; - printf (" | Name | Mode | Val| Physical |Val | Mode | Name |\n") ; - printf (" +-------+------+----+----++----+----+------+-------+\n") ; - for (pin = 1 ; pin <= 26 ; pin += 2) - readallPhys (pin) ; - printf (" +-------+------+----+----++----+----+------+-------+\n") ; - } - else // wiringPi - { - printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ; - printf (" | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi |\n") ; - printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ; - for (pin = 1 ; pin <= 26 ; pin += 2) - readallPhys (pin) ; - printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ; - } -} - - -void doReadallOld (void) -{ - int pin ; - - if (wiringPiNodes != NULL) // External readall - { - doReadallExternal () ; - return ; - } - - if (cmReadall ()) - return ; - - if (bPlusReadall ()) - return ; - - printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ; - printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ; - printf ("+----------+------+------+--------+------+-------+\n") ; - - for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective - { - if (wpiPinToGpio (pin) == -1) - continue ; - - printf ("| %6d | %3d | %3d | %s | %-4s | %-4s |\n", - pin, wpiPinToGpio (pin), wpiToPhys [pin], - pinNames [pin], - alts [getAlt (pin)], - digitalRead (pin) == HIGH ? "High" : "Low ") ; - } - - printf ("+----------+------+------+--------+------+-------+\n") ; + /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B)) + abReadall (model, rev) ; + else if (model == PI_MODEL_BP) + bPlusReadall () ; + else if (model == PI_MODEL_CM) + cmReadall () ; + else + printf ("Oops - unable to determine board type... model: %d\n", model) ; } diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 619a3dc..78d3612 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -166,7 +166,7 @@ depend: # DO NOT DELETE -wiringPi.o: wiringPi.h +wiringPi.o: softPwm.h softTone.h wiringPi.h wiringSerial.o: wiringSerial.h wiringShift.o: wiringPi.h wiringShift.h piHiPri.o: wiringPi.h @@ -190,4 +190,4 @@ mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h max31855.o: wiringPi.h wiringPiSPI.h max31855.h max5322.o: wiringPi.h wiringPiSPI.h max5322.h sn3218.o: wiringPi.h wiringPiI2C.h sn3218.h -drc.o: wiringPi.h wiringSerial.h drc.h +drcSerial.o: wiringPi.h wiringSerial.h drcSerial.h diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 1e1cfe4..e7ae386 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -197,6 +197,39 @@ static volatile uint32_t *timer ; static volatile uint32_t *timerIrqRaw ; #endif + +// Data for use with the boardId functions. +// The order of entries here to correspond with the PI_MODEL_X +// and PI_VERSION_X defines in wiringPi.h +// Only intended for the gpio command - use at your own risk! + +const char *piModelNames [5] = +{ + "Unknown", + "Model A", + "Model B", + "Model B+", + "Compute Module", +} ; + +const char *piRevisionNames [5] = +{ + "Unknown", + "1", + "1.1", + "1.2", + "2", +} ; + +const char *piMakerNames [4] = +{ + "Unknown", + "Egoman", + "Sony", + "Qusda", +} ; + + // Time for easy calculations static uint64_t epochMilli, epochMicro ; @@ -334,10 +367,17 @@ static int physToGpioR2 [64] = // the P5 connector on the Rev 2 boards: - -1, -1, -1, -1, -1, -1, -1, // ... 47 - -1, -1, -1, -1, -1, // ... 52 - 28, 29, 30, 31, // ... 53, 54, 55, 56 - P5 - -1, -1, -1, -1, -1, -1, -1, // ... 63 + -1, -1, + -1, -1, + -1, -1, + -1, -1, + -1, -1, + 28, 29, + 30, 31, + -1, -1, + -1, -1, + -1, -1, + -1, -1, } ; // gpioToGPFSEL: @@ -559,26 +599,27 @@ int wiringPiFailure (int fatal, const char *message, ...) /* * piBoardRev: * Return a number representing the hardware revision of the board. - * Revision is currently 1 or 2. * - * Much confusion here )-: + * Revision 1 really means the early Model B's. + * Revision 2 is everything else - it covers the B, B+ and CM. + * * Seems there are some boards with 0000 in them (mistake in manufacture) * So the distinction between boards that I can see is: * 0000 - Error - * 0001 - Not used (Compute - default to Rev 2) - * 0002 - Model B, Rev 1, 256MB - * 0003 - Model B, Rev 1.1, 256MB, Fuses/D14 removed. - * 0004 - Model B, Rev 2, 256MB, Sony - * 0005 - Model B, Rev 2, 256MB, Qisda - * 0006 - Model B, Rev 2, 256MB, Egoman - * 0007 - Model A, Rev 2, 256MB, Egoman - * 0008 - Model A, Rev 2, 256MB, Sony - * 0009 - Model A, Rev 2, 256MB, Qisda - * 000d - Model B, Rev 2, 512MB, Egoman - * 000e - Model B, Rev 2, 512MB, Sony - * 000f - Model B, Rev 2, 512MB, Qisda - * 0010 - Model B+ 512MB, Sony - * 0011 - Pi compute Module + * 0001 - Not used + * 0002 - Model B, Rev 1, 256MB, Egoman + * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed. + * 0004 - Model B, Rev 2, 256MB, Sony + * 0005 - Model B, Rev 2, 256MB, Qisda + * 0006 - Model B, Rev 2, 256MB, Egoman + * 0007 - Model A, Rev 2, 256MB, Egoman + * 0008 - Model A, Rev 2, 256MB, Sony + * 0009 - Model A, Rev 2, 256MB, Qisda + * 000d - Model B, Rev 2, 512MB, Egoman + * 000e - Model B, Rev 2, 512MB, Sony + * 000f - Model B, Rev 2, 512MB, Qisda + * 0010 - Model B+, Rev 1.2, 512MB, Sony + * 0011 - Pi CM, Rev 1.2, 512MB, Sony * * A small thorn is the olde style overvolting - that will add in * 1000000 @@ -604,7 +645,7 @@ int piBoardRev (void) { FILE *cpuFd ; char line [120] ; - char *c, lastChar ; + char *c ; static int boardRev = -1 ; if (boardRev != -1) // No point checking twice @@ -622,12 +663,16 @@ int piBoardRev (void) if (strncmp (line, "Revision", 8) != 0) piBoardRevOops ("No \"Revision\" line") ; +// Chomp trailing CR/NL + for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c) *c = 0 ; if (wiringPiDebug) printf ("piboardRev: Revision string: %s\n", line) ; +// Scan to first digit + for (c = line ; *c ; ++c) if (isdigit (*c)) break ; @@ -635,6 +680,11 @@ int piBoardRev (void) if (!isdigit (*c)) piBoardRevOops ("No numeric revision string") ; +// Make sure its long enough + + if (strlen (c) < 4) + piBoardRevOops ("Bogus \"Revision\" line (too small)") ; + // If you have overvolted the Pi, then it appears that the revision // has 100000 added to it! @@ -642,12 +692,14 @@ int piBoardRev (void) if (strlen (c) != 4) printf ("piboardRev: This Pi has/is overvolted!\n") ; - lastChar = line [strlen (line) - 1] ; +// Isolate last 4 characters: + + c = c + strlen (c) - 4 ; if (wiringPiDebug) - printf ("piboardRev: lastChar is: '%c' (%d, 0x%02X)\n", lastChar, lastChar, lastChar) ; + printf ("piboardRev: last4Chars are: \"%s\"\n", c) ; - /**/ if ((lastChar == '2') || (lastChar == '3')) + if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0)) boardRev = 1 ; else boardRev = 2 ; @@ -663,32 +715,18 @@ int piBoardRev (void) * piBoardId: * Do more digging into the board revision string as above, but return * as much details as we can. + * This is undocumented and really only intended for the GPIO command. + * Use at your own risk! ********************************************************************************* */ -const char *piModelNames [] = -{ - "Model A", - "Model B", - "Model B+", - "Compute Module", -} ; - -const char *piRevisionNames[] = -{ - "1", - "1.1", - "2", - "1.2", -} ; - -void piBoardId (int *model, int *rev, int *mem, char **maker) +void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) { FILE *cpuFd ; char line [120] ; char *c ; - piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result. + (void)piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) piBoardRevOops ("Unable to open /proc/cpuinfo") ; @@ -720,6 +758,10 @@ void piBoardId (int *model, int *rev, int *mem, char **maker) if (strlen (c) < 4) piBoardRevOops ("Bogus \"Revision\" line") ; + +// If longer than 4, we'll assume it's been overvolted + + *overVolted = strlen (c) > 4 ; // Extract last 4 characters: @@ -727,20 +769,20 @@ void piBoardId (int *model, int *rev, int *mem, char **maker) // Fill out the replys as appropriate - /**/ if (strcmp (c, "0002") == 0) { *model = 1 ; *rev = 0 ; *mem = 256 ; *maker = "China" ; } - else if (strcmp (c, "0003") == 0) { *model = 1 ; *rev = 1 ; *mem = 256 ; *maker = "China" ; } - else if (strcmp (c, "0004") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Sony" ; } - else if (strcmp (c, "0005") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Qisda" ; } - else if (strcmp (c, "0006") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Egoman" ; } - else if (strcmp (c, "0007") == 0) { *model = 0 ; *rev = 2 ; *mem = 256 ; *maker = "Egoman" ; } - else if (strcmp (c, "0008") == 0) { *model = 0 ; *rev = 2 ; *mem = 256 ; *maker = "Sony" ; } - else if (strcmp (c, "0009") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Qisda" ; } - else if (strcmp (c, "000d") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; } - else if (strcmp (c, "000e") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Sony" ; } - else if (strcmp (c, "000f") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; } - else if (strcmp (c, "0010") == 0) { *model = 2 ; *rev = 3 ; *mem = 512 ; *maker = "Sony" ; } - else if (strcmp (c, "0011") == 0) { *model = 3 ; *rev = 1 ; *mem = 512 ; *maker = "Sony" ; } - else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = "Unkn" ; } + /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; } + else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; ; } + else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; } + else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } + else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; } } @@ -1698,8 +1740,7 @@ int wiringPiSetup (void) { int fd ; int boardRev ; - int model, rev, mem ; - char *maker ; + int model, rev, mem, maker, overVolted ; if (getenv (ENV_DEBUG) != NULL) wiringPiDebug = TRUE ; @@ -1775,7 +1816,7 @@ int wiringPiSetup (void) // If we're running on a compute module, then wiringPi pin numbers don't really many anything... - piBoardId (&model, &rev, &mem, &maker) ; + piBoardId (&model, &rev, &mem, &maker, &overVolted) ; if (model == PI_MODEL_CM) wiringPiMode = WPI_MODE_GPIO ; else diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 78ff87d..2bc63ba 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -67,13 +67,32 @@ #define INT_EDGE_RISING 2 #define INT_EDGE_BOTH 3 -// Pi model types +// Pi model types and version numbers +// Intended for the GPIO program Use at your own risk. -#define PI_MODEL_A 0 -#define PI_MODEL_B 1 -#define PI_MODEL_BPLUS 2 -#define PI_MODEL_CM 3 +#define PI_MODEL_UNKNOWN 0 +#define PI_MODEL_A 1 +#define PI_MODEL_B 2 +#define PI_MODEL_BP 3 +#define PI_MODEL_CM 4 +#define PI_VERSION_UNKNOWN 0 +#define PI_VERSION_1 1 +#define PI_VERSION_1_1 2 +#define PI_VERSION_1_2 3 +#define PI_VERSION_2 4 + +#define PI_MAKER_UNKNOWN 0 +#define PI_MAKER_EGOMAN 1 +#define PI_MAKER_SONY 2 +#define PI_MAKER_QISDA 3 + +extern const char *piModelNames [5] ; +extern const char *piRevisionNames [5] ; +extern const char *piMakerNames [4] ; + + +// Intended for the GPIO program Use at your own risk. // Threads @@ -128,8 +147,8 @@ extern "C" { // Data -extern const char *piModelNames [] ; -extern const char *piRevisionNames[] ; +//extern const char *piModelNames [] ; +//extern const char *piRevisionNames[] ; // Internal @@ -163,7 +182,7 @@ extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio // On-Board Raspberry Pi hardware specific stuff extern int piBoardRev (void) ; -extern void piBoardId (int *model, int *rev, int *mem, char **maker) ; +extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ; extern int wpiPinToGpio (int wpiPin) ; extern int physPinToGpio (int physPin) ; extern void setPadDrive (int group, int value) ; From 0a9fdeb29d31609ef834f050bdb8a7a65cd35e2b Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 10 Nov 2014 10:55:23 +0000 Subject: [PATCH 66/97] Minor changes to the PiGlow code - got the orange & yellow the right way round at last.. Updated for the 0x0012 board revision on the new A+ --- devLib/Makefile | 14 +++--- devLib/piGlow.h | 4 +- examples/Makefile | 1 + examples/lowPower.c | 68 +++++++++++++++++++++++++ examples/spiSpeed.c | 118 ++++++++++++++++++++++++++++++++++++++++++++ gpio/gpio.c | 47 +++++++++++++----- gpio/readall.c | 22 ++++++--- pins/Makefile | 2 +- wiringPi/Makefile | 14 +++--- wiringPi/wiringPi.c | 11 ++++- wiringPi/wiringPi.h | 3 +- 11 files changed, 264 insertions(+), 40 deletions(-) create mode 100644 examples/lowPower.c create mode 100644 examples/spiSpeed.c diff --git a/devLib/Makefile b/devLib/Makefile index a3c0d42..4ba1774 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -65,18 +65,18 @@ $(DYNAMIC): $(OBJ) @echo [Compile] $< @$(CC) -c $(CFLAGS) $< -o $@ -.PHONEY: clean +.PHONY: clean clean: @echo "[Clean]" @rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPiDev.* -.PHONEY: tags +.PHONY: tags tags: $(SRC) @echo [ctags] @ctags $(SRC) -.PHONEY: install-headers +.PHONY: install-headers install-headers: @echo "[Install Headers]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/include @@ -89,7 +89,7 @@ install-headers: @install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include @install -m 0644 piGlow.h $(DESTDIR)$(PREFIX)/include -.PHONEY: install +.PHONY: install install: $(DYNAMIC) install-headers @echo "[Install Dynamic Lib]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib @@ -97,13 +97,13 @@ install: $(DYNAMIC) install-headers @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) $(DESTDIR)/lib/libwiringPiDev.so @ldconfig -.PHONEY: install-static +.PHONY: install-static install-static: $(STATIC) install-headers @echo "[Install Static Lib]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib @install -m 0755 libwiringPiDev.a $(DESTDIR)$(PREFIX)/lib -.PHONEY: uninstall +.PHONY: uninstall uninstall: @echo "[UnInstall]" @rm -f $(DESTDIR)$(PREFIX)/include/ds1302.h @@ -118,7 +118,7 @@ uninstall: @ldconfig -.PHONEY: depend +.PHONY: depend depend: makedepend -Y $(SRC) diff --git a/devLib/piGlow.h b/devLib/piGlow.h index d501c2c..a4d89d0 100644 --- a/devLib/piGlow.h +++ b/devLib/piGlow.h @@ -24,8 +24,8 @@ #define PIGLOW_RED 0 -#define PIGLOW_YELLOW 1 -#define PIGLOW_ORANGE 2 +#define PIGLOW_ORANGE 1 +#define PIGLOW_YELLOW 2 #define PIGLOW_GREEN 3 #define PIGLOW_BLUE 4 #define PIGLOW_WHITE 5 diff --git a/examples/Makefile b/examples/Makefile index 571e341..c9967dc 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -43,6 +43,7 @@ SRC = blink.c blink8.c blink12.c \ nes.c \ softPwm.c softTone.c \ delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \ + lowPower.c \ rht03.c piglow.c OBJ = $(SRC:.c=.o) diff --git a/examples/lowPower.c b/examples/lowPower.c new file mode 100644 index 0000000..e901e7f --- /dev/null +++ b/examples/lowPower.c @@ -0,0 +1,68 @@ +/* + * lowPower.c: + * Check the Pi's LOW-Power signal. + * + * This is a demonstration program that could be turned into some sort + * of logger via e.g. syslog - however it's also probably something + * that might be better handled by a future kernel - who knows. + * + * Copyright (c) 2014 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 + + +#define LOW_POWER 35 + +/* + * lowPower: + * This is an ISR that waits for the low-power signal going low and + * prints the result. + ********************************************************************************* + */ + +void lowPower (void) +{ + time_t t ; + + time (&t) ; + printf ("%s: LOW POWER DETECTED\n", ctime (&t)) ; +} + + +/* + ********************************************************************************* + * main + ********************************************************************************* + */ + +int main (void) +{ + wiringPiSetupGpio () ; // GPIO mode as it's an internal pin + + wiringPiISR (LOW_POWER, INT_EDGE_FALLING, &lowPower) ; + + for (;;) + delay (1000) ; + + return 0 ; +} diff --git a/examples/spiSpeed.c b/examples/spiSpeed.c new file mode 100644 index 0000000..0208f0a --- /dev/null +++ b/examples/spiSpeed.c @@ -0,0 +1,118 @@ +/* + * spiSpeed.c: + * Code to measure the SPI speed/latency. + * Copyright (c) 2014 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 +#include +#include +#include +//#include +//#include +//#include + +#include +#include + +#define TRUE (1==1) +#define FALSE (!TRUE) + +#define SPI_CHAN 0 +#define NUM_TIMES 100 +#define MAX_SIZE (1024*1024) + +static int myFd ; + + +void spiSetup (int speed) +{ + if ((myFd = wiringPiSPISetup (SPI_CHAN, speed)) < 0) + { + fprintf (stderr, "Can't open the SPI bus: %s\n", strerror (errno)) ; + exit (EXIT_FAILURE) ; + } +} + + +int main (void) +{ + int speed, times, size ; + unsigned int start, end ; + int spiFail ; + unsigned char *myData ; + double timePerTransaction, perfectTimePerTransaction, dataSpeed ; + + if ((myData = malloc (MAX_SIZE)) == NULL) + { + fprintf (stderr, "Unable to allocate buffer: %s\n", strerror (errno)) ; + exit (EXIT_FAILURE) ; + } + + wiringPiSetup () ; + + for (speed = 1 ; speed <= 32 ; speed *= 2) + { + printf ("+-------+--------+----------+----------+-----------+------------+\n") ; + printf ("| MHz | Size | mS/Trans | TpS | Mb/Sec | Latency mS |\n") ; + printf ("+-------+--------+----------+----------+-----------+------------+\n") ; + + spiFail = FALSE ; + spiSetup (speed * 1000000) ; + for (size = 1 ; size <= MAX_SIZE ; size *= 2) + { + printf ("| %5d | %6d ", speed, size) ; + + start = millis () ; + for (times = 0 ; times < NUM_TIMES ; ++times) + if (wiringPiSPIDataRW (SPI_CHAN, myData, size) == -1) + { + printf ("SPI failure: %s\n", strerror (errno)) ; + spiFail = TRUE ; + break ; + } + end = millis () ; + + if (spiFail) + break ; + + timePerTransaction = ((double)(end - start) / (double)NUM_TIMES) / 1000.0 ; + dataSpeed = (double)(size * 8) / (1024.0 * 1024.0) / timePerTransaction ; + perfectTimePerTransaction = ((double)(size * 8)) / ((double)(speed * 1000000)) ; + + printf ("| %8.3f ", timePerTransaction * 1000.0) ; + printf ("| %8.1f ", 1.0 / timePerTransaction) ; + printf ("| %9.5f ", dataSpeed) ; + printf ("| %8.5f ", (timePerTransaction - perfectTimePerTransaction) * 1000.0) ; + printf ("|\n") ; + + } + + close (myFd) ; + printf ("+-------+--------+----------+----------+-----------+------------+\n") ; + printf ("\n") ; + } + + return 0 ; +} diff --git a/gpio/gpio.c b/gpio/gpio.c index c440874..43e6934 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -2,7 +2,7 @@ * gpio.c: * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry * Pi's GPIO. - * Copyright (c) 2012-2013 Gordon Henderson + * Copyright (c) 2012-2014 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -53,7 +53,7 @@ extern void doPins (void) ; # define FALSE (1==2) #endif -#define VERSION "2.20" +#define VERSION "2.21" #define PI_USB_POWER_CONTROL 38 #define I2CDETECT "/usr/sbin/i2cdetect" @@ -612,23 +612,44 @@ static void doResetExternal (void) static void doReset (char *progName) { - int pin ; + int model, rev, mem, maker, overVolted ; + int pin, endPin ; - if (wiringPiNodes != NULL) // External reset + printf ("GPIO Reset is dangerous!\n") ; + printf (" - Do Not rely on this to do anything sensible!\n") ; + + if (wiringPiNodes != NULL) // External + { doResetExternal () ; + return ; + } + + piBoardId (&model, &rev, &mem, &maker, &overVolted) ; + + /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B)) + endPin = 16 ; + else if (model == PI_MODEL_BP) + endPin = 39 ; + else if (model == PI_MODEL_CM) + { + printf (" - Don't know how to reset a comput module:\n") ; + printf (" Write a shell-script to reset the pins to the state you need.\n") ; + return ; + } else { - doUnexportall (progName) ; + printf ("Oops - unable to determine board type... model: %d\n", model) ; + return ; + } - for (pin = 0 ; pin < 64 ; ++pin) - { - if (wpiPinToGpio (pin) == -1) - continue ; + for (pin = 0 ; pin <= endPin ; ++pin) + { + if (wpiPinToGpio (pin) == -1) + continue ; - digitalWrite (pin, LOW) ; - pinMode (pin, INPUT) ; - pullUpDnControl (pin, PUD_OFF) ; - } + digitalWrite (pin, LOW) ; + pinMode (pin, INPUT) ; + pullUpDnControl (pin, PUD_OFF) ; } } diff --git a/gpio/readall.c b/gpio/readall.c index 76b5cfc..aba8de8 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -278,23 +278,31 @@ void abReadall (int model, int rev) /* - * bPlusReadall: - * Read all the pins on the model B+ + * piPlusReadall: + * Read all the pins on the model A+ or the B+ ********************************************************************************* */ -void bPlusReadall (void) +void piPlusReadall (int model) { int pin ; - printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ; + if (model == PI_MODEL_AP) + printf (" +-----+-----+---------+------+---+--A Plus--+---+------+---------+-----+-----+\n") ; + else + printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ; + printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ; printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ; for (pin = 1 ; pin <= 40 ; pin += 2) readallPhys (pin) ; printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ; printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ; - printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ; + + if (model == PI_MODEL_AP) + printf (" +-----+-----+---------+------+---+--A Plus--+---+------+---------+-----+-----+\n") ; + else + printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ; } @@ -312,8 +320,8 @@ void doReadall (void) /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B)) abReadall (model, rev) ; - else if (model == PI_MODEL_BP) - bPlusReadall () ; + else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP)) + piPlusReadall (model) ; else if (model == PI_MODEL_CM) cmReadall () ; else diff --git a/pins/Makefile b/pins/Makefile index 5e200c2..fd34ee9 100644 --- a/pins/Makefile +++ b/pins/Makefile @@ -13,6 +13,6 @@ pdf: pins.dvi @dvipdf pins.dvi -.PHONEY: clean +.PHONY: clean clean: @rm -f *.dvi *.aux *.log *.ps *.toc *.bak *~ diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 78d3612..4e3342e 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -77,18 +77,18 @@ $(DYNAMIC): $(OBJ) @echo [Compile] $< @$(CC) -c $(CFLAGS) $< -o $@ -.PHONEY: clean +.PHONY: clean clean: @echo "[Clean]" @rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.* -.PHONEY: tags +.PHONY: tags tags: $(SRC) @echo [ctags] @ctags $(SRC) -.PHONEY: install-headers +.PHONY: install-headers install-headers: @echo "[Install Headers]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/include @@ -116,7 +116,7 @@ install-headers: @install -m 0644 pcf8591.h $(DESTDIR)$(PREFIX)/include @install -m 0644 sn3218.h $(DESTDIR)$(PREFIX)/include -.PHONEY: install +.PHONY: install install: $(DYNAMIC) install-headers @echo "[Install Dynamic Lib]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib @@ -124,13 +124,13 @@ install: $(DYNAMIC) install-headers @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so @ldconfig -.PHONEY: install-static +.PHONY: install-static install-static: $(STATIC) install-headers @echo "[Install Static Lib]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib @install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib -.PHONEY: uninstall +.PHONY: uninstall uninstall: @echo "[UnInstall]" @rm -f $(DESTDIR)$(PREFIX)/include/wiringPi.h @@ -160,7 +160,7 @@ uninstall: @ldconfig -.PHONEY: depend +.PHONY: depend depend: makedepend -Y $(SRC) $(SRC_I2C) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index e7ae386..ba45f4d 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -203,13 +203,14 @@ static volatile uint32_t *timerIrqRaw ; // and PI_VERSION_X defines in wiringPi.h // Only intended for the gpio command - use at your own risk! -const char *piModelNames [5] = +const char *piModelNames [6] = { "Unknown", "Model A", "Model B", "Model B+", "Compute Module", + "Model A+", } ; const char *piRevisionNames [5] = @@ -620,6 +621,7 @@ int wiringPiFailure (int fatal, const char *message, ...) * 000f - Model B, Rev 2, 512MB, Qisda * 0010 - Model B+, Rev 1.2, 512MB, Sony * 0011 - Pi CM, Rev 1.2, 512MB, Sony + * 0012 - Model A+ Rev 1.2, 256MB, Sony * * A small thorn is the olde style overvolting - that will add in * 1000000 @@ -687,10 +689,12 @@ int piBoardRev (void) // If you have overvolted the Pi, then it appears that the revision // has 100000 added to it! +// The actual condition for it being set is: +// (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0 if (wiringPiDebug) if (strlen (c) != 4) - printf ("piboardRev: This Pi has/is overvolted!\n") ; + printf ("piboardRev: This Pi has/is (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0\n") ; // Isolate last 4 characters: @@ -782,6 +786,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; } else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; } } @@ -1474,8 +1479,10 @@ int waitForInterrupt (int pin, int mS) // Do a dummy read to clear the interrupt // A one character read appars to be enough. +// Followed by a seek to reset it. (void)read (fd, &c, 1) ; + lseek (fd, 0, SEEK_SET) ; return x ; } diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 2bc63ba..42265fc 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -75,6 +75,7 @@ #define PI_MODEL_B 2 #define PI_MODEL_BP 3 #define PI_MODEL_CM 4 +#define PI_MODEL_AP 5 #define PI_VERSION_UNKNOWN 0 #define PI_VERSION_1 1 @@ -87,7 +88,7 @@ #define PI_MAKER_SONY 2 #define PI_MAKER_QISDA 3 -extern const char *piModelNames [5] ; +extern const char *piModelNames [6] ; extern const char *piRevisionNames [5] ; extern const char *piMakerNames [4] ; From d42e831089c9d15d229fc475d84dee9456527b42 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Wed, 7 Jan 2015 14:26:17 +0000 Subject: [PATCH 67/97] Updated versions for new maker - mbest --- gpio/gpio.c | 2 +- wiringPi/wiringPi.c | 4 +++- wiringPi/wiringPi.h | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index 43e6934..7881c65 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -53,7 +53,7 @@ extern void doPins (void) ; # define FALSE (1==2) #endif -#define VERSION "2.21" +#define VERSION "2.22" #define PI_USB_POWER_CONTROL 38 #define I2CDETECT "/usr/sbin/i2cdetect" diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index ba45f4d..1c82aee 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -222,12 +222,13 @@ const char *piRevisionNames [5] = "2", } ; -const char *piMakerNames [4] = +const char *piMakerNames [5] = { "Unknown", "Egoman", "Sony", "Qusda", + "MBest", } ; @@ -787,6 +788,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST ; } else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; } } diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 42265fc..2b3e295 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -87,10 +87,11 @@ #define PI_MAKER_EGOMAN 1 #define PI_MAKER_SONY 2 #define PI_MAKER_QISDA 3 +#define PI_MAKER_MBEST 4 extern const char *piModelNames [6] ; extern const char *piRevisionNames [5] ; -extern const char *piMakerNames [4] ; +extern const char *piMakerNames [5] ; // Intended for the GPIO program Use at your own risk. From 51fff3cab9d45a4775eb0fb1f5de23eb2e2b2328 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 29 Jan 2015 10:17:25 +0000 Subject: [PATCH 68/97] Updated a technicality in softPwm, and added a suggested memset to zero for the SPI code - it's mentioned in spidev.h but not used in the actual kernel documentation example spi_test.c program )-: --- wiringPi/softPwm.c | 9 ++++----- wiringPi/wiringPiSPI.c | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/wiringPi/softPwm.c b/wiringPi/softPwm.c index 3c79ba7..98b408f 100644 --- a/wiringPi/softPwm.c +++ b/wiringPi/softPwm.c @@ -51,11 +51,10 @@ #define PULSE_TIME 100 -static int marks [MAX_PINS] ; -static int range [MAX_PINS] ; -static pthread_t threads [MAX_PINS] ; - -int newPin = -1 ; +static volatile int marks [MAX_PINS] ; +static volatile int range [MAX_PINS] ; +static volatile pthread_t threads [MAX_PINS] ; +static volatile int newPin = -1 ; /* diff --git a/wiringPi/wiringPiSPI.c b/wiringPi/wiringPiSPI.c index 215a027..002ee01 100644 --- a/wiringPi/wiringPiSPI.c +++ b/wiringPi/wiringPiSPI.c @@ -75,6 +75,11 @@ int wiringPiSPIDataRW (int channel, unsigned char *data, int len) channel &= 1 ; +// Mentioned in spidev.h but not used in the original kernel documentation +// test program )-: + + memset (&spi, 0, sizeof (spi)) ; + spi.tx_buf = (unsigned long)data ; spi.rx_buf = (unsigned long)data ; spi.len = len ; From eb1fc2c92003420eab641c5f53a3047f09d9fbe3 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Fri, 30 Jan 2015 18:14:49 +0000 Subject: [PATCH 69/97] Updates to the build process Moved the extensions into wiringPi from gpio and made it more general purpose more so that RTB and anything else can dymanically add devices into wiringPi. Changes to GPIO to updates for the SPI and I2C module loads Added gpio unload for SPI and I2C. Added a new way to setup SPI - by passing the mode in. Support for the new Pi2 thing too --- build | 49 ++++++ gpio/Makefile | 7 +- gpio/gpio.1 | 79 ++++------ gpio/gpio.c | 144 +++++++++--------- gpio/pins.c | 2 +- gpio/pintest | 16 +- gpio/readall.c | 25 +-- wiringPi/Makefile | 9 +- wiringPi/wiringPi.c | 76 ++++++--- wiringPi/wiringPi.h | 6 +- wiringPi/wiringPiSPI.c | 28 ++-- wiringPi/wiringPiSPI.h | 9 +- gpio/extensions.c => wiringPi/wpiExtensions.c | 134 +++++++++------- gpio/extensions.h => wiringPi/wpiExtensions.h | 4 +- 14 files changed, 344 insertions(+), 244 deletions(-) rename gpio/extensions.c => wiringPi/wpiExtensions.c (78%) rename gpio/extensions.h => wiringPi/wpiExtensions.h (89%) diff --git a/build b/build index bac483a..377bd16 100755 --- a/build +++ b/build @@ -1,5 +1,36 @@ #!/bin/sh +# build +# Simple wiringPi build and install script +# +# Copyright (c) 2012-2015 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# +# +# wiringPi is designed to run on a Raspberry Pi only. +# However if you're clever enough to actually look at this script to +# see why it's not building for you, then good luck. +# +# To everyone else: Stop using cheap alternatives. Support the +# Raspberry Pi Foundation as they're the only ones putting money +# back into education! +################################################################################# + check_make_ok() { if [ $? != 0 ]; then echo "" @@ -47,6 +78,24 @@ fi echo "=====================" echo + hardware=`fgrep Hardware /proc/cpuinfo | head -1 | awk '{ print $3 }'` + + if [ x$hardware != "xBCM2708" ]; then + echo "" + echo " +------------------------------------------------------------+" + echo " | wiringPi is designed to run on the Raspberry Pi only. |" + echo " | This processor does not appear to be a Raspberry Pi. |" + echo " +------------------------------------------------------------+" + echo " | In the unlikely event that you think it is a Raspberry Pi, |" + echo " | then please accept my apologies and email the contents of |" + echo " | /proc/cpuinfo to projects@drogon.net. |" + echo " | - Thanks, Gordon |" + echo " +------------------------------------------------------------+" + echo "" + exit 1 + fi + + echo echo "WiringPi Library" cd wiringPi diff --git a/gpio/Makefile b/gpio/Makefile index 37b4887..9c8585f 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -4,7 +4,7 @@ # A swiss-army knige of GPIO shenanigans. # https://projects.drogon.net/wiring-pi # -# Copyright (c) 2012-2013 Gordon Henderson +# Copyright (c) 2012-2015 Gordon Henderson ################################################################################# # This file is part of wiringPi: # Wiring Compatable library for the Raspberry Pi @@ -38,7 +38,7 @@ LIBS = -lwiringPi -lwiringPiDev -lpthread -lm # May not need to alter anything below this line ############################################################################### -SRC = gpio.c extensions.c readall.c pins.c +SRC = gpio.c readall.c pins.c OBJ = $(SRC:.c=.o) @@ -82,6 +82,3 @@ depend: makedepend -Y $(SRC) # DO NOT DELETE - -gpio.o: extensions.h -extensions.o: extensions.h diff --git a/gpio/gpio.1 b/gpio/gpio.1 index e7131c1..2d3b860 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -1,4 +1,4 @@ -.TH "GPIO" "March 2013" "Command-Line access to Raspberry Pi's GPIO" +.TH "GPIO" "January 2015" "Command-Line access to Raspberry Pi's GPIO" .SH NAME gpio \- Command-line access to Raspberry Pi's GPIO @@ -21,7 +21,7 @@ gpio \- Command-line access to Raspberry Pi's GPIO .B ... .PP .B gpio -.B readall/reset +.B readall .PP .B gpio .B unexportall/exports @@ -156,16 +156,6 @@ The readall command is usable with an extension module (via the -x parameter), but it's unable to determine pin modes or states, so will perform both a digital and analog read on each pin in-turn. -.TP -.B reset -Resets the GPIO - As much as it's possible to do. All pins are set to -input mode and all the internal pull-up/down resistors are disconnected -(tristate mode). - -The reset command is usable with an extension module (via the -x parameter), -but it's limited to turning the pin into input mode (if applicable) and -removing any pull up/down resistor. - .TP .B pwm Write a PWM value (0-1023) to the given pin. The pin needs to be put @@ -182,6 +172,8 @@ 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. +The ALT modes can also be set using \fIalt0\fR, \fIalt1\fR, ... \fIalt5\fR. + .TP .B unexportall Un-Export all the GPIO pins in the /sys/class/gpio directory. @@ -257,12 +249,30 @@ on the associated /dev/ entries so that the current user has access to them. Optionally it will set the I2C baudrate to that supplied in Kb/sec (or as close as the Pi can manage) The default speed is 100Kb/sec. +Note that on a Pi with a recent 3.18 kernel with the device-tree structure +enable, the load may fail until you add: + +.I dtparam=i2c=on + +into \fB/boot/config.txt\fR to allow user use of the I2C bus. + .TP -.B load spi [buffer size in KB] +.B load spi This loads the spi drivers into the kernel and changes the permissions on the associated /dev/ entries so that the current user has access to -them. Optionally it will set the SPI buffer size to that supplied. The -default is 4KB. +them. It used to have the ability to change the buffer size from the +default of 4096 bytes to an arbitary value, however for some time the +Pi Foundation have compiled the SPI device driver into the kernel and +this has fixed the buffer size. The way to change it now is to edit +the /boot/cmdline.txt file and add on spdev.bufsiz=8192 to set it to +e.g. 8192 bytes then reboot. + +Note that on a Pi with a recent 3.18 kernel with the device-tree structure +enable, the load may fail until you add: + +.I dtparam=spi=on + +into \fB/boot/config.txt\fR to allow user use of the I2C bus. .TP .B gbr @@ -280,41 +290,12 @@ SPI digital to analogue converter. The board jumpers need to be in-place to do this operation. -.SH "WiringPi vs. BCM_GPIO Pin numbering" +.SH "WiringPi vs. BCM_GPIO Pin numbering vs. Physical pin numbering" .PP -.TS -c c c c l. -WiringPi GPIO-r1 GPIO-r2 P1-Phys Function -_ - 0 17 17 11 - 1 18 18 12 (PWM) - 2 21 27 13 - 3 22 22 15 - 4 23 23 16 - 5 24 24 18 - 6 25 25 22 - 7 4 4 7 - 8 0 2 3 I2C: SDA0 - 9 1 3 5 I2C: SCL0 -10 8 8 24 SPI: CE0 -11 7 7 26 SPI: CE1 -12 10 10 19 SPI: MOSI -13 9 9 21 SPI: MISO -14 11 11 23 SPI: SCLK -15 14 14 8 TxD -16 15 16 10 RxD -17 - 28 -18 - 29 -19 - 30 -20 - 31 -.TE - -Note that "r1" and "r2" above refers to the board revision. Normally -wiringPi detects the correct board revision with use for it's own -numbering scheme, but if you are using a Revision 2 board with some -of the pins which change numbers between revisions you will need -to alter your software. +The quickest way to get a list of the pin differences is to run the command +.TP +gpio readall .SH FILES @@ -361,7 +342,7 @@ Please report bugs to .SH COPYRIGHT -Copyright (c) 2012-2013 Gordon Henderson +Copyright (c) 2012-2015 Gordon Henderson This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. diff --git a/gpio/gpio.c b/gpio/gpio.c index 7881c65..3cee59f 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -2,7 +2,7 @@ * gpio.c: * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry * Pi's GPIO. - * Copyright (c) 2012-2014 Gordon Henderson + * Copyright (c) 2012-2015 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -35,11 +35,11 @@ #include #include +#include #include #include -#include "extensions.h" extern int wiringPiDebug ; @@ -53,7 +53,7 @@ extern void doPins (void) ; # define FALSE (1==2) #endif -#define VERSION "2.22" +#define VERSION "2.23" #define PI_USB_POWER_CONTROL 38 #define I2CDETECT "/usr/sbin/i2cdetect" @@ -73,6 +73,7 @@ char *usage = "Usage: gpio -v\n" " gpio pwmr \n" " gpio pwmc \n" " gpio load spi/i2c\n" + " gpio unload spi/i2c\n" " gpio i2cd/i2cdetect\n" " gpio usbp high/low\n" " gpio gbr \n" @@ -115,12 +116,9 @@ static void changeOwner (char *cmd, char *file) if (chown (file, uid, gid) != 0) { if (errno == ENOENT) // Warn that it's not there - fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ; + fprintf (stderr, "%s: Warning (not an error): File not present: %s\n", cmd, file) ; else - { - fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ; - exit (1) ; - } + fprintf (stderr, "%s: Warning (not an error): Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ; } } @@ -167,7 +165,7 @@ static int moduleLoaded (char *modName) static void _doLoadUsage (char *argv []) { - fprintf (stderr, "Usage: %s load [SPI bufferSize in KB | I2C baudrate in Kb/sec]\n", argv [0]) ; + fprintf (stderr, "Usage: %s load [I2C baudrate in Kb/sec]\n", argv [0]) ; exit (1) ; } @@ -190,7 +188,10 @@ static void doLoad (int argc, char *argv []) file1 = "/dev/spidev0.0" ; file2 = "/dev/spidev0.1" ; if (argc == 4) - sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ; + { + fprintf (stderr, "%s: Unable to set the buffer size now. Load aborted. Please see the man page.\n", argv [0]) ; + exit (1) ; + } else if (argc > 4) _doLoadUsage (argv) ; } @@ -210,13 +211,13 @@ static void doLoad (int argc, char *argv []) if (!moduleLoaded (module1)) { - sprintf (cmd, "modprobe %s%s", module1, args1) ; + sprintf (cmd, "/sbin/modprobe %s%s", module1, args1) ; system (cmd) ; } if (!moduleLoaded (module2)) { - sprintf (cmd, "modprobe %s%s", module2, args2) ; + sprintf (cmd, "/sbin/modprobe %s%s", module2, args2) ; system (cmd) ; } @@ -233,6 +234,53 @@ static void doLoad (int argc, char *argv []) } +/* + * doUnLoad: + * Un-Load either the spi or i2c modules and change device ownerships, etc. + ********************************************************************************* + */ + +static void _doUnLoadUsage (char *argv []) +{ + fprintf (stderr, "Usage: %s unload \n", argv [0]) ; + exit (1) ; +} + +static void doUnLoad (int argc, char *argv []) +{ + char *module1, *module2 ; + char cmd [80] ; + + if (argc != 3) + _doUnLoadUsage (argv) ; + + /**/ if (strcasecmp (argv [2], "spi") == 0) + { + module1 = "spidev" ; + module2 = "spi_bcm2708" ; + } + else if (strcasecmp (argv [2], "i2c") == 0) + { + module1 = "i2c_dev" ; + module2 = "i2c_bcm2708" ; + } + else + _doUnLoadUsage (argv) ; + + if (moduleLoaded (module1)) + { + sprintf (cmd, "/sbin/rmmod %s", module1) ; + system (cmd) ; + } + + if (moduleLoaded (module2)) + { + sprintf (cmd, "/sbin/rmmod %s", module2) ; + system (cmd) ; + } +} + + /* * doI2Cdetect: * Run the i2cdetect command with the right runes for this Pi revision @@ -586,24 +634,6 @@ void doUnexportall (char *progName) } -/* - * doResetExternal: - * Load readallExternal, we try to do this with an external device. - ********************************************************************************* - */ - -static void doResetExternal (void) -{ - int pin ; - - for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin) - { - pinMode (pin, INPUT) ; - pullUpDnControl (pin, PUD_OFF) ; - } -} - - /* * doReset: * Reset the GPIO pins - as much as we can do @@ -612,45 +642,9 @@ static void doResetExternal (void) static void doReset (char *progName) { - int model, rev, mem, maker, overVolted ; - int pin, endPin ; - - printf ("GPIO Reset is dangerous!\n") ; - printf (" - Do Not rely on this to do anything sensible!\n") ; - - if (wiringPiNodes != NULL) // External - { - doResetExternal () ; - return ; - } - - piBoardId (&model, &rev, &mem, &maker, &overVolted) ; - - /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B)) - endPin = 16 ; - else if (model == PI_MODEL_BP) - endPin = 39 ; - else if (model == PI_MODEL_CM) - { - printf (" - Don't know how to reset a comput module:\n") ; - printf (" Write a shell-script to reset the pins to the state you need.\n") ; - return ; - } - else - { - printf ("Oops - unable to determine board type... model: %d\n", model) ; - return ; - } - - for (pin = 0 ; pin <= endPin ; ++pin) - { - if (wpiPinToGpio (pin) == -1) - continue ; - - digitalWrite (pin, LOW) ; - pinMode (pin, INPUT) ; - pullUpDnControl (pin, PUD_OFF) ; - } + printf ("GPIO Reset is dangerous and has been removed from the gpio command.\n") ; + printf (" - Please write a shell-script to reset the GPIO pins into the state\n") ; + printf (" that you need them in for your applications.\n") ; } @@ -686,9 +680,6 @@ void doMode (int argc, char *argv []) else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ; - -// Undocumented - else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ; else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ; else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ; @@ -1183,7 +1174,7 @@ int main (int argc, char *argv []) if (strcmp (argv [1], "-v") == 0) { printf ("gpio version: %s\n", VERSION) ; - printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ; + printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ; printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ; printf ("For details type: %s -warranty\n", argv [0]) ; printf ("\n") ; @@ -1206,7 +1197,7 @@ int main (int argc, char *argv []) if (strcasecmp (argv [1], "-warranty") == 0) { printf ("gpio version: %s\n", VERSION) ; - printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ; + printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ; printf ("\n") ; printf (" This program is free software; you can redistribute it and/or modify\n") ; printf (" it under the terms of the GNU Leser General Public License as published\n") ; @@ -1240,7 +1231,8 @@ int main (int argc, char *argv []) // Check for load command: - if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; } + if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; } + if (strcasecmp (argv [1], "unload" ) == 0) { doUnLoad (argc, argv) ; return 0 ; } // Gertboard commands @@ -1301,7 +1293,7 @@ int main (int argc, char *argv []) exit (EXIT_FAILURE) ; } - if (!doExtension (argv [0], argv [2])) // Prints its own error messages + if (!loadWPiExtension (argv [0], argv [2], TRUE)) // Prints its own error messages exit (EXIT_FAILURE) ; for (i = 3 ; i < argc ; ++i) diff --git a/gpio/pins.c b/gpio/pins.c index 19fcc1a..d889a45 100644 --- a/gpio/pins.c +++ b/gpio/pins.c @@ -1,7 +1,7 @@ /* * pins.c: * Just display a handy Pi pinnout diagram. - * Copyright (c) 2012-2013 Gordon Henderson + * Copyright (c) 2012-2015 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ diff --git a/gpio/pintest b/gpio/pintest index 83ca12a..4da3d94 100755 --- a/gpio/pintest +++ b/gpio/pintest @@ -2,7 +2,7 @@ # # pintest # Test the Pi's GPIO port -# Copyright (c) 2013 Gordon Henderson +# Copyright (c) 2013-2015 Gordon Henderson ################################################################################# # This file is part of wiringPi: # Wiring Compatable library for the Raspberry Pi @@ -104,13 +104,11 @@ testPins() intro() { - revision=`gpio -V` cat < #include #include +#include #include #include #include @@ -35,27 +38,31 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "mcp23008.h" +#include "mcp23016.h" +#include "mcp23017.h" +#include "mcp23s08.h" +#include "mcp23s17.h" +#include "sr595.h" +#include "pcf8591.h" +#include "pcf8574.h" +#include "max31855.h" +#include "max5322.h" +#include "mcp3002.h" +#include "mcp3004.h" +#include "mcp4802.h" +#include "mcp3422.h" +#include "sn3218.h" +#include "drcSerial.h" -#include "extensions.h" +#include "wpiExtensions.h" extern int wiringPiDebug ; +static int verbose ; +static char errorMessage [1024] ; + + #ifndef TRUE # define TRUE (1==1) # define FALSE (1==2) @@ -70,6 +77,24 @@ struct extensionFunctionStruct } ; +/* + * verbError: + * Convenient error handling + ********************************************************************************* + */ + +static void verbError (const char *message, ...) +{ + va_list argp ; + va_start (argp, message) ; + vsnprintf (errorMessage, 1023, message, argp) ; + va_end (argp) ; + + if (verbose) + fprintf (stderr, "%s\n", errorMessage) ; +} + + /* * extractInt: * Check & return an integer at the given location (prefixed by a :) @@ -80,7 +105,7 @@ static char *extractInt (char *progName, char *p, int *num) { if (*p != ':') { - fprintf (stderr, "%s: colon expected\n", progName) ; + verbError ("%s: colon expected", progName) ; return NULL ; } @@ -88,7 +113,7 @@ static char *extractInt (char *progName, char *p, int *num) if (!isdigit (*p)) { - fprintf (stderr, "%s: digit expected\n", progName) ; + verbError ("%s: digit expected", progName) ; return NULL ; } @@ -112,7 +137,7 @@ static char *extractStr (char *progName, char *p, char **str) if (*p != ':') { - fprintf (stderr, "%s: colon expected\n", progName) ; + verbError ("%s: colon expected", progName) ; return NULL ; } @@ -120,7 +145,7 @@ static char *extractStr (char *progName, char *p, char **str) if (!isprint (*p)) { - fprintf (stderr, "%s: character expected\n", progName) ; + verbError ("%s: character expected", progName) ; return NULL ; } @@ -152,9 +177,9 @@ static int doExtensionMcp23008 (char *progName, int pinBase, char *params) if ((params = extractInt (progName, params, &i2c)) == NULL) return FALSE ; - if ((i2c < 0x03) || (i2c > 0x77)) + if ((i2c < 0x01) || (i2c > 0x77)) { - fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ; return FALSE ; } @@ -180,7 +205,7 @@ static int doExtensionMcp23016 (char *progName, int pinBase, char *params) if ((i2c < 0x03) || (i2c > 0x77)) { - fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ; return FALSE ; } @@ -206,7 +231,7 @@ static int doExtensionMcp23017 (char *progName, int pinBase, char *params) if ((i2c < 0x03) || (i2c > 0x77)) { - fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ; return FALSE ; } @@ -232,7 +257,7 @@ static int doExtensionMcp23s08 (char *progName, int pinBase, char *params) if ((spi < 0) || (spi > 1)) { - fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ; + verbError ("%s: SPI address (%d) out of range", progName, spi) ; return FALSE ; } @@ -241,7 +266,7 @@ static int doExtensionMcp23s08 (char *progName, int pinBase, char *params) if ((port < 0) || (port > 7)) { - fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ; + verbError ("%s: port address (%d) out of range", progName, port) ; return FALSE ; } @@ -267,7 +292,7 @@ static int doExtensionMcp23s17 (char *progName, int pinBase, char *params) if ((spi < 0) || (spi > 1)) { - fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ; + verbError ("%s: SPI address (%d) out of range", progName, spi) ; return FALSE ; } @@ -276,7 +301,7 @@ static int doExtensionMcp23s17 (char *progName, int pinBase, char *params) if ((port < 0) || (port > 7)) { - fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ; + verbError ("%s: port address (%d) out of range", progName, port) ; return FALSE ; } @@ -304,7 +329,7 @@ static int doExtensionSr595 (char *progName, int pinBase, char *params) if ((pins < 8) || (pins > 32)) { - fprintf (stderr, "%s: pin count (%d) out of range - 8-32 expected.\n", progName, pins) ; + verbError ("%s: pin count (%d) out of range - 8-32 expected.", progName, pins) ; return FALSE ; } @@ -339,7 +364,7 @@ static int doExtensionPcf8574 (char *progName, int pinBase, char *params) if ((i2c < 0x03) || (i2c > 0x77)) { - fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ; return FALSE ; } @@ -365,7 +390,7 @@ static int doExtensionPcf8591 (char *progName, int pinBase, char *params) if ((i2c < 0x03) || (i2c > 0x77)) { - fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ; return FALSE ; } @@ -391,7 +416,7 @@ static int doExtensionMax31855 (char *progName, int pinBase, char *params) if ((spi < 0) || (spi > 1)) { - fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + verbError ("%s: SPI channel (%d) out of range", progName, spi) ; return FALSE ; } @@ -417,7 +442,7 @@ static int doExtensionMcp3002 (char *progName, int pinBase, char *params) if ((spi < 0) || (spi > 1)) { - fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + verbError ("%s: SPI channel (%d) out of range", progName, spi) ; return FALSE ; } @@ -443,7 +468,7 @@ static int doExtensionMcp3004 (char *progName, int pinBase, char *params) if ((spi < 0) || (spi > 1)) { - fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + verbError ("%s: SPI channel (%d) out of range", progName, spi) ; return FALSE ; } @@ -469,7 +494,7 @@ static int doExtensionMax5322 (char *progName, int pinBase, char *params) if ((spi < 0) || (spi > 1)) { - fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + verbError ("%s: SPI channel (%d) out of range", progName, spi) ; return FALSE ; } @@ -495,7 +520,7 @@ static int doExtensionMcp4802 (char *progName, int pinBase, char *params) if ((spi < 0) || (spi > 1)) { - fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ; + verbError ("%s: SPI channel (%d) out of range", progName, spi) ; return FALSE ; } @@ -535,7 +560,7 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params) if ((i2c < 0x03) || (i2c > 0x77)) { - fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ; + verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ; return FALSE ; } @@ -544,7 +569,7 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params) if ((sampleRate < 0) || (sampleRate > 3)) { - fprintf (stderr, "%s: sample rate (%d) out of range\n", progName, sampleRate) ; + verbError ("%s: sample rate (%d) out of range", progName, sampleRate) ; return FALSE ; } @@ -553,7 +578,7 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params) if ((gain < 0) || (gain > 3)) { - fprintf (stderr, "%s: gain (%d) out of range\n", progName, gain) ; + verbError ("%s: gain (%d) out of range", progName, gain) ; return FALSE ; } @@ -562,6 +587,7 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params) return TRUE ; } + /* * doExtensionDrcS: * Interface to a DRC Serial system @@ -579,7 +605,7 @@ static int doExtensionDrcS (char *progName, int pinBase, char *params) if ((pins < 1) || (pins > 100)) { - fprintf (stderr, "%s: pins (%d) out of range (2-100)\n", progName, pins) ; + verbError ("%s: pins (%d) out of range (2-100)", progName, pins) ; return FALSE ; } @@ -588,7 +614,7 @@ static int doExtensionDrcS (char *progName, int pinBase, char *params) if (strlen (port) == 0) { - fprintf (stderr, "%s: serial port device name required\n", progName) ; + verbError ("%s: serial port device name required", progName) ; return FALSE ; } @@ -597,7 +623,7 @@ static int doExtensionDrcS (char *progName, int pinBase, char *params) if ((baud < 1) || (baud > 4000000)) { - fprintf (stderr, "%s: baud rate (%d) out of range\n", progName, baud) ; + verbError ("%s: baud rate (%d) out of range", progName, baud) ; return FALSE ; } @@ -613,7 +639,7 @@ static int doExtensionDrcS (char *progName, int pinBase, char *params) ********************************************************************************* */ -struct extensionFunctionStruct extensionFunctions [] = +static struct extensionFunctionStruct extensionFunctions [] = { { "mcp23008", &doExtensionMcp23008 }, { "mcp23016", &doExtensionMcp23016 }, @@ -636,18 +662,20 @@ struct extensionFunctionStruct extensionFunctions [] = /* - * doExtension: + * loadWPiExtension: * Load in a wiringPi extension ********************************************************************************* */ -int doExtension (char *progName, char *extensionData) +int loadWPiExtension (char *progName, char *extensionData, int printErrors) { char *p ; char *extension = extensionData ; struct extensionFunctionStruct *extensionFn ; int pinBase = 0 ; + verbose = printErrors ; + // Get the extension extension name by finding the first colon p = extension ; @@ -655,7 +683,7 @@ int doExtension (char *progName, char *extensionData) { if (!*p) // ran out of characters { - fprintf (stderr, "%s: extension name not terminated by a colon\n", progName) ; + verbError ("%s: extension name not terminated by a colon", progName) ; return FALSE ; } ++p ; @@ -665,7 +693,7 @@ int doExtension (char *progName, char *extensionData) if (!isdigit (*p)) { - fprintf (stderr, "%s: pinBase number expected after extension name\n", progName) ; + verbError ("%s: pinBase number expected after extension name", progName) ; return FALSE ; } @@ -673,7 +701,7 @@ int doExtension (char *progName, char *extensionData) { if (pinBase > 1000000000) { - fprintf (stderr, "%s: pinBase too large\n", progName) ; + verbError ("%s: pinBase too large", progName) ; return FALSE ; } @@ -683,7 +711,7 @@ int doExtension (char *progName, char *extensionData) if (pinBase < 64) { - fprintf (stderr, "%s: pinBase (%d) too small. Minimum is 64.\n", progName, pinBase) ; + verbError ("%s: pinBase (%d) too small. Minimum is 64.", progName, pinBase) ; return FALSE ; } @@ -695,6 +723,6 @@ int doExtension (char *progName, char *extensionData) return extensionFn->function (progName, pinBase, p) ; } - fprintf (stderr, "%s: extension %s not found\n", progName, extension) ; + verbError ("%s: extension %s not found", progName, extension) ; return FALSE ; } diff --git a/gpio/extensions.h b/wiringPi/wpiExtensions.h similarity index 89% rename from gpio/extensions.h rename to wiringPi/wpiExtensions.h index 5d27123..fcaec96 100644 --- a/gpio/extensions.h +++ b/wiringPi/wpiExtensions.h @@ -2,7 +2,7 @@ * extensions.h: * Part of the GPIO program to test, peek, poke and otherwise * noodle with the GPIO hardware on the Raspberry Pi. - * Copyright (c) 2012-2013 Gordon Henderson + * Copyright (c) 2012-2015 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -23,4 +23,4 @@ */ -extern int doExtension (char *progName, char *extensionData) ; +extern int loadWPiExtension (char *progName, char *extensionData, int verbose) ; From 7cb817498e294e4d9ccc981524661be963b7a97f Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 31 Jan 2015 16:43:08 +0000 Subject: [PATCH 70/97] OK, so an easier way to manage versions. Debian package creation - crude, probably violated the Debian policys but it seems to work just fine... some other changes. --- VERSION | 1 + build | 32 +++++++-- debian/wiringPi/DEBIAN/control | 10 +++ debian/wiringPi/DEBIAN/postinst | 5 ++ debian/wiringPi/DEBIAN/postrm | 2 + devLib/Makefile | 61 +++++++++-------- gpio/Makefile | 11 +++- gpio/gpio.c | 2 +- gpio/newVersion | 26 ++++++++ gpio/version.h | 1 + wiringPi/Makefile | 112 +++++++++++++------------------- wiringPi/wiringPi.c | 8 +-- wiringPi/wpiExtensions.c | 15 +++-- 13 files changed, 169 insertions(+), 117 deletions(-) create mode 100644 VERSION create mode 100644 debian/wiringPi/DEBIAN/control create mode 100755 debian/wiringPi/DEBIAN/postinst create mode 100755 debian/wiringPi/DEBIAN/postrm create mode 100755 gpio/newVersion create mode 100644 gpio/version.h diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..fd6915c --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +2.24 diff --git a/build b/build index 377bd16..fb5a8b7 100755 --- a/build +++ b/build @@ -73,6 +73,28 @@ if [ x$1 = "xuninstall" ]; then exit fi +# Only if you know what you're doing! + +if [ x$1 = "xdebian" ]; then + here=`pwd` + cd debian/wiringPi + rm -rf usr + cd $here/wiringPi + make install-deb + cd $here/devLib + make install-deb + cd $here/gpio + make install-deb + cd $here/debian + fakeroot dpkg-deb --build wiringPi + mv wiringPi.deb wiringpi-`cat $here/VERSION`-1.deb + exit +fi + +if [ x$1 != "x" ]; then + echo "Usage: $0 [clean | uninstall]" + exit 1 +fi echo "wiringPi Build script" echo "=====================" @@ -101,11 +123,11 @@ fi cd wiringPi sudo make uninstall if [ x$1 = "xstatic" ]; then - make static + make -j5 static check_make_ok sudo make install-static else - make + make -j5 check_make_ok sudo make install fi @@ -116,11 +138,11 @@ fi cd ../devLib sudo make uninstall if [ x$1 = "xstatic" ]; then - make static + make -j5 static check_make_ok sudo make install-static else - make + make -j5 check_make_ok sudo make install fi @@ -129,7 +151,7 @@ fi echo echo "GPIO Utility" cd ../gpio - make + make -j5 check_make_ok sudo make install check_make_ok diff --git a/debian/wiringPi/DEBIAN/control b/debian/wiringPi/DEBIAN/control new file mode 100644 index 0000000..73ee094 --- /dev/null +++ b/debian/wiringPi/DEBIAN/control @@ -0,0 +1,10 @@ +Package: wiringpi +Version: 2.24 +Section: libraries +Priority: optional +Architecture: armhf +Depends: libc6 +Maintainer: Gordon Henderson +Description: The wiringPi libraries, headers and gpio command + Libraries to allow GPIO access on a Raspberry Pi from C and C++ + programs as well as from the command-line diff --git a/debian/wiringPi/DEBIAN/postinst b/debian/wiringPi/DEBIAN/postinst new file mode 100755 index 0000000..4997e98 --- /dev/null +++ b/debian/wiringPi/DEBIAN/postinst @@ -0,0 +1,5 @@ +#!/bin/sh +set -e +/bin/chown root.root /usr/bin/gpio +/bin/chmod 4755 /usr/bin/gpio +/sbin/ldconfig diff --git a/debian/wiringPi/DEBIAN/postrm b/debian/wiringPi/DEBIAN/postrm new file mode 100755 index 0000000..4be8c58 --- /dev/null +++ b/debian/wiringPi/DEBIAN/postrm @@ -0,0 +1,2 @@ +#!/bin/sh +/sbin/ldconfig diff --git a/devLib/Makefile b/devLib/Makefile index 4ba1774..d62b532 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -1,7 +1,8 @@ +# # Makefile: # wiringPi device - Wiring Compatable library for the Raspberry Pi # -# Copyright (c) 2012-2013 Gordon Henderson +# Copyright (c) 2012-2015 Gordon Henderson ################################################################################# # This file is part of wiringPi: # https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -20,10 +21,7 @@ # along with wiringPi. If not, see . ################################################################################# -DYN_VERS_MAJ=2 -DYN_VERS_MIN=0 - -VERSION=$(DYN_VERS_MAJ).$(DYN_VERS_MIN) +VERSION=$(shell cat ../VERSION) DESTDIR=/usr PREFIX=/local @@ -34,7 +32,8 @@ DYNAMIC=libwiringPiDev.so.$(VERSION) DEBUG = -O2 CC = gcc INCLUDE = -I. -CFLAGS = $(DEBUG) -Wformat=2 -Wall $(INCLUDE) -Winline -pipe -fPIC +DEFS = -D_GNU_SOURCE +CFLAGS = $(DEBUG) $(DEFS) -Wformat=2 -Wall -Winline $(INCLUDE) -pipe -fPIC LIBS = @@ -47,6 +46,8 @@ SRC = ds1302.c maxdetect.c piNes.c \ OBJ = $(SRC:.c=.o) +HEADERS = ds1302.h gertboard.h lcd128x64.h lcd.h maxdetect.h piFace.h piGlow.h piNes.h + all: $(DYNAMIC) static: $(STATIC) @@ -76,21 +77,11 @@ tags: $(SRC) @ctags $(SRC) -.PHONY: install-headers -install-headers: - @echo "[Install Headers]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/include - @install -m 0644 ds1302.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 maxdetect.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 piNes.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 gertboard.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 piFace.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 lcd128x64.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 piGlow.h $(DESTDIR)$(PREFIX)/include - .PHONY: install -install: $(DYNAMIC) install-headers +install: $(DYNAMIC) + @echo "[Install Headers]" + @install -m 0755 -d $(DESTDIR)$(PREFIX)/include + @install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include @echo "[Install Dynamic Lib]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib @install -m 0755 libwiringPiDev.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) @@ -98,23 +89,29 @@ install: $(DYNAMIC) install-headers @ldconfig .PHONY: install-static -install-static: $(STATIC) install-headers +install-static: $(STATIC) + @echo "[Install Headers]" + @install -m 0755 -d $(DESTDIR)$(PREFIX)/include + @install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include @echo "[Install Static Lib]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib - @install -m 0755 libwiringPiDev.a $(DESTDIR)$(PREFIX)/lib + @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + @install -m 0755 libwiringPiDev.a $(DESTDIR)$(PREFIX)/lib + +.PHONY: install-deb +install-deb: $(DYNAMIC) + @echo "[Install Headers: deb]" + @install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/include + @install -m 0644 $(HEADERS) ~/wiringPi/debian/wiringPi/usr/include + @echo "[Install Dynamic Lib: deb]" + install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/lib + install -m 0755 libwiringPiDev.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so.$(VERSION) + ln -sf ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so .PHONY: uninstall uninstall: @echo "[UnInstall]" - @rm -f $(DESTDIR)$(PREFIX)/include/ds1302.h - @rm -f $(DESTDIR)$(PREFIX)/include/maxdetect.h - @rm -f $(DESTDIR)$(PREFIX)/include/piNes.h - @rm -f $(DESTDIR)$(PREFIX)/include/gertboard.h - @rm -f $(DESTDIR)$(PREFIX)/include/piFace.h - @rm -f $(DESTDIR)$(PREFIX)/include/lcd128x64.h - @rm -f $(DESTDIR)$(PREFIX)/include/lcd.h - @rm -f $(DESTDIR)$(PREFIX)/include/piGlow.h - @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.* + @cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS) + @cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPiDev.* @ldconfig diff --git a/gpio/Makefile b/gpio/Makefile index 9c8585f..449986e 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -44,6 +44,9 @@ OBJ = $(SRC:.c=.o) all: gpio +version.h: ../VERSION + ./newVersion + gpio: $(OBJ) @echo [Link] @$(CC) -o $@ $(OBJ) $(LDFLAGS) $(LIBS) @@ -63,7 +66,7 @@ tags: $(SRC) @ctags $(SRC) .PHONY: install -install: +install: gpio @echo "[Install]" @cp gpio $(DESTDIR)$(PREFIX)/bin @chown root.root $(DESTDIR)$(PREFIX)/bin/gpio @@ -71,6 +74,12 @@ install: @mkdir -p $(DESTDIR)$(PREFIX)/man/man1 @cp gpio.1 $(DESTDIR)$(PREFIX)/man/man1 +.PHONY: install-deb +install-deb: gpio + @echo "[Install: deb]" + @install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/bin + @install -m 0755 gpio ~/wiringPi/debian/wiringPi/usr/bin + .PHONY: uninstall uninstall: @echo "[UnInstall]" diff --git a/gpio/gpio.c b/gpio/gpio.c index 3cee59f..461274f 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -40,6 +40,7 @@ #include #include +#include "version.h" extern int wiringPiDebug ; @@ -53,7 +54,6 @@ extern void doPins (void) ; # define FALSE (1==2) #endif -#define VERSION "2.23" #define PI_USB_POWER_CONTROL 38 #define I2CDETECT "/usr/sbin/i2cdetect" diff --git a/gpio/newVersion b/gpio/newVersion new file mode 100755 index 0000000..b8728a5 --- /dev/null +++ b/gpio/newVersion @@ -0,0 +1,26 @@ +#!/bin/sh +# +# newVersion: +# Utility to create the version.h include file for the gpio command. +# +# Copyright (c) 2012-2015 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# + +rm -f version.h +echo "#define VERSION \"`cat ../VERSION`\"" > version.h diff --git a/gpio/version.h b/gpio/version.h new file mode 100644 index 0000000..5416bcb --- /dev/null +++ b/gpio/version.h @@ -0,0 +1 @@ +#define VERSION "2.24" diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 1a06c35..015a894 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -1,8 +1,8 @@ -# ; +# # Makefile: # wiringPi - Wiring Compatable library for the Raspberry Pi # -# Copyright (c) 2012 Gordon Henderson +# Copyright (c) 2012-2015 Gordon Henderson ################################################################################# # This file is part of wiringPi: # https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -21,10 +21,7 @@ # along with wiringPi. If not, see . ################################################################################# -DYN_VERS_MAJ=2 -DYN_VERS_MIN=0 - -VERSION=$(DYN_VERS_MAJ).$(DYN_VERS_MIN) +VERSION=$(shell cat ../VERSION) DESTDIR=/usr PREFIX=/local @@ -40,7 +37,6 @@ CFLAGS = $(DEBUG) $(DEFS) -Wformat=2 -Wall -Winline $(INCLUDE) -pipe -fPIC LIBS = -# Should not alter anything below this line ############################################################################### SRC = wiringPi.c \ @@ -58,6 +54,21 @@ SRC = wiringPi.c \ drcSerial.c \ wpiExtensions.c +HEADERS = wiringPi.h \ + wiringSerial.h wiringShift.h \ + wiringPiSPI.h wiringPiI2C.h \ + softPwm.h softTone.h \ + mcp23008.h mcp23016.h mcp23017.h \ + mcp23s08.h mcp23s17.h \ + sr595.h \ + pcf8574.h pcf8591.h \ + mcp3002.h mcp3004.h mcp4802.h mcp3422.h \ + max31855.h max5322.h \ + sn3218.h \ + drcSerial.h \ + wpiExtensions.h + + OBJ = $(SRC:.c=.o) all: $(DYNAMIC) @@ -78,6 +89,7 @@ $(DYNAMIC): $(OBJ) @echo [Compile] $< @$(CC) -c $(CFLAGS) $< -o $@ + .PHONY: clean clean: @echo "[Clean]" @@ -89,37 +101,11 @@ tags: $(SRC) @ctags $(SRC) -.PHONY: install-headers -install-headers: - @echo "[Install Headers]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/include - @install -m 0644 wiringPi.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 wiringSerial.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 wiringShift.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 softPwm.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 drcSerial.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 mcp23008.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 mcp23016.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 max31855.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 max5322.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 mcp3002.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 mcp3004.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 mcp4802.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 mcp3422.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 sr595.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 pcf8574.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 pcf8591.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 sn3218.h $(DESTDIR)$(PREFIX)/include - @install -m 0644 wpiExtensions.h $(DESTDIR)$(PREFIX)/include - .PHONY: install -install: $(DYNAMIC) install-headers +install: $(DYNAMIC) + @echo "[Install Headers]" + @install -m 0755 -d $(DESTDIR)$(PREFIX)/include + @install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include @echo "[Install Dynamic Lib]" @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib @install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) @@ -127,39 +113,29 @@ install: $(DYNAMIC) install-headers @ldconfig .PHONY: install-static -install-static: $(STATIC) install-headers +install-static: $(STATIC) + @echo "[Install Headers]" + @install -m 0755 -d $(DESTDIR)$(PREFIX)/include + @install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include @echo "[Install Static Lib]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib - @install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib + @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + @install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib + +.PHONY: install-deb +install-deb: $(DYNAMIC) + @echo "[Install Headers: deb]" + @install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/include + @install -m 0644 $(HEADERS) ~/wiringPi/debian/wiringPi/usr/include + @echo "[Install Dynamic Lib: deb]" + install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/lib + install -m 0755 libwiringPi.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so.$(VERSION) + ln -sf ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so .PHONY: uninstall uninstall: @echo "[UnInstall]" - @rm -f $(DESTDIR)$(PREFIX)/include/wiringPi.h - @rm -f $(DESTDIR)$(PREFIX)/include/wiringSerial.h - @rm -f $(DESTDIR)$(PREFIX)/include/wiringShift.h - @rm -f $(DESTDIR)$(PREFIX)/include/softPwm.h - @rm -f $(DESTDIR)$(PREFIX)/include/softTone.h - @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h - @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h - @rm -f $(DESTDIR)$(PREFIX)/include/drcSerial.h - @rm -f $(DESTDIR)$(PREFIX)/include/mcp23008.h - @rm -f $(DESTDIR)$(PREFIX)/include/mcp23016.h - @rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h - @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h - @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h - @rm -f $(DESTDIR)$(PREFIX)/include/max31855.h - @rm -f $(DESTDIR)$(PREFIX)/include/max5322.h - @rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h - @rm -f $(DESTDIR)$(PREFIX)/include/mcp3004.h - @rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h - @rm -f $(DESTDIR)$(PREFIX)/include/mcp3422.h - @rm -f $(DESTDIR)$(PREFIX)/include/sr595.h - @rm -f $(DESTDIR)$(PREFIX)/include/pcf8574.h - @rm -f $(DESTDIR)$(PREFIX)/include/pcf8591.h - @rm -f $(DESTDIR)$(PREFIX)/include/sn3218.h - @rm -f $(DESTDIR)$(PREFIX)/includewpiExtensions.h - @rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.* + @cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS) + @cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPi.* @ldconfig @@ -195,6 +171,6 @@ max5322.o: wiringPi.h wiringPiSPI.h max5322.h sn3218.o: wiringPi.h wiringPiI2C.h sn3218.h drcSerial.o: wiringPi.h wiringSerial.h drcSerial.h wpiExtensions.o: wiringPi.h mcp23008.h mcp23016.h mcp23017.h mcp23s08.h -wpiExtensions.o: mcp23s17.h sr595.h pcf8591.h pcf8574.h max31855.h max5322.h -wpiExtensions.o: mcp3002.h mcp3004.h mcp4802.h mcp3422.h sn3218.h drcSerial.h -wpiExtensions.o: wpiExtensions.h +wpiExtensions.o: mcp23s17.h sr595.h pcf8574.h pcf8591.h mcp3002.h mcp3004.h +wpiExtensions.o: mcp4802.h mcp3422.h max31855.h max5322.h sn3218.h +wpiExtensions.o: drcSerial.h wpiExtensions.h diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 990c641..e36af8a 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1,7 +1,7 @@ /* * wiringPi: - * Arduino compatable (ish) Wiring library for the Raspberry Pi - * Copyright (c) 2012 Gordon Henderson + * Arduino look-a-like Wiring library for the Raspberry Pi + * Copyright (c) 2012-2015 Gordon Henderson * Additional code for pwmSetClock by Chris Hall * * Thanks to code samples from Gert Jan van Loo and the @@ -131,7 +131,7 @@ struct wiringPiNodeStruct *wiringPiNodes = NULL ; // Taken from Gert/Doms code. Some of this is not in the manual // that I can find )-: -static volatile unsigned int BCM2708_PERI_BASE = 0x20000000 ; +static volatile unsigned int BCM2708_PERI_BASE = 0x20000000 ; // Variable for Pi2 #define GPIO_PADS (BCM2708_PERI_BASE + 0x00100000) #define CLOCK_BASE (BCM2708_PERI_BASE + 0x00101000) #define GPIO_BASE (BCM2708_PERI_BASE + 0x00200000) @@ -607,7 +607,7 @@ int wiringPiFailure (int fatal, const char *message, ...) * * Revision 1 really means the early Model B's. * Revision 2 is everything else - it covers the B, B+ and CM. - * ... and the quad core Pi 2 - which is a B+ ++ ... + * ... and the Pi 2 - which is a B+ ++ ... * * Seems there are some boards with 0000 in them (mistake in manufacture) * So the distinction between boards that I can see is: diff --git a/wiringPi/wpiExtensions.c b/wiringPi/wpiExtensions.c index da41f29..4cae9c4 100644 --- a/wiringPi/wpiExtensions.c +++ b/wiringPi/wpiExtensions.c @@ -44,14 +44,14 @@ #include "mcp23s08.h" #include "mcp23s17.h" #include "sr595.h" -#include "pcf8591.h" #include "pcf8574.h" -#include "max31855.h" -#include "max5322.h" +#include "pcf8591.h" #include "mcp3002.h" #include "mcp3004.h" #include "mcp4802.h" #include "mcp3422.h" +#include "max31855.h" +#include "max5322.h" #include "sn3218.h" #include "drcSerial.h" @@ -664,6 +664,8 @@ static struct extensionFunctionStruct extensionFunctions [] = /* * loadWPiExtension: * Load in a wiringPi extension + * The extensionData always starts with the name, a colon then the pinBase + * number. Other parameters after that are decoded by the module in question. ********************************************************************************* */ @@ -676,7 +678,7 @@ int loadWPiExtension (char *progName, char *extensionData, int printErrors) verbose = printErrors ; -// Get the extension extension name by finding the first colon +// Get the extension name by finding the first colon p = extension ; while (*p != ':') @@ -688,9 +690,10 @@ int loadWPiExtension (char *progName, char *extensionData, int printErrors) } ++p ; } - *p++ = 0 ; +// Simple ATOI code + if (!isdigit (*p)) { verbError ("%s: pinBase number expected after extension name", progName) ; @@ -699,7 +702,7 @@ int loadWPiExtension (char *progName, char *extensionData, int printErrors) while (isdigit (*p)) { - if (pinBase > 1000000000) + if (pinBase > 1000000000) // Lets be realistic here... { verbError ("%s: pinBase too large", progName) ; return FALSE ; From 72b2af231be337d45a557ce6ca3ed1eeb6675ffd Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 3 Feb 2015 14:53:32 +0000 Subject: [PATCH 71/97] OK. So the Pi v2 I have had older firmware and it wasn't quite the same as everyone elses. This is an interim version which will work on both Pi v1 and v2 boards until I have a bit more time to implement the correct stuff for v2. --- VERSION | 2 +- build | 28 +++++++------- gpio/gpio.1 | 8 ++-- gpio/gpio.c | 12 ++++-- gpio/version.h | 2 +- wiringPi/wiringPi.c | 91 ++++++++++++++++++++++++++++++--------------- 6 files changed, 91 insertions(+), 52 deletions(-) diff --git a/VERSION b/VERSION index fd6915c..8bd2249 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.24 +2.25 diff --git a/build b/build index fb5a8b7..d38ad1b 100755 --- a/build +++ b/build @@ -102,20 +102,20 @@ fi hardware=`fgrep Hardware /proc/cpuinfo | head -1 | awk '{ print $3 }'` - if [ x$hardware != "xBCM2708" ]; then - echo "" - echo " +------------------------------------------------------------+" - echo " | wiringPi is designed to run on the Raspberry Pi only. |" - echo " | This processor does not appear to be a Raspberry Pi. |" - echo " +------------------------------------------------------------+" - echo " | In the unlikely event that you think it is a Raspberry Pi, |" - echo " | then please accept my apologies and email the contents of |" - echo " | /proc/cpuinfo to projects@drogon.net. |" - echo " | - Thanks, Gordon |" - echo " +------------------------------------------------------------+" - echo "" - exit 1 - fi +# if [ x$hardware != "xBCM2708" ]; then +# echo "" +# echo " +------------------------------------------------------------+" +# echo " | wiringPi is designed to run on the Raspberry Pi only. |" +# echo " | This processor does not appear to be a Raspberry Pi. |" +# echo " +------------------------------------------------------------+" +# echo " | In the unlikely event that you think it is a Raspberry Pi, |" +# echo " | then please accept my apologies and email the contents of |" +# echo " | /proc/cpuinfo to projects@drogon.net. |" +# echo " | - Thanks, Gordon |" +# echo " +------------------------------------------------------------+" +# echo "" +# exit 1 +# fi echo diff --git a/gpio/gpio.1 b/gpio/gpio.1 index 2d3b860..77b41bf 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -185,9 +185,11 @@ Print a list (if any) of all the exported GPIO pins and their current values. .TP .B export 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. +mode command above however only \fIin\fR, \fIout\fR, \fIhigh\fR and +\fRlow\fR are supported at this time. Note that the pin number is the +\fBBCM_GPIO\fR number and not the wiringPi number. The \fIhigh\fR and +\fIlow\fR commands pre-set the output value at the same time as the +export to output mode. 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 diff --git a/gpio/gpio.c b/gpio/gpio.c index 461274f..6dc6113 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -434,19 +434,23 @@ void doExport (int argc, char *argv []) exit (1) ; } - /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0)) + /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0)) fprintf (fd, "in\n") ; - else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0)) + else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0)) fprintf (fd, "out\n") ; + else if ((strcasecmp (mode, "high") == 0) || (strcasecmp (mode, "up") == 0)) + fprintf (fd, "high\n") ; + else if ((strcasecmp (mode, "low") == 0) || (strcasecmp (mode, "down") == 0)) + fprintf (fd, "low\n") ; else { - fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ; + fprintf (stderr, "%s: Invalid mode: %s. Should be in, out, high or low\n", argv [1], mode) ; exit (1) ; } fclose (fd) ; -// Change ownership so the current user can actually use it! +// Change ownership so the current user can actually use it sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; changeOwner (argv [0], fName) ; diff --git a/gpio/version.h b/gpio/version.h index 5416bcb..fd96a9b 100644 --- a/gpio/version.h +++ b/gpio/version.h @@ -1 +1 @@ -#define VERSION "2.24" +#define VERSION "2.25" diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index e36af8a..be636a0 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -667,19 +667,25 @@ int piBoardRev (void) // Start by looking for the Architecture, then we can look for a B2 revision.... while (fgets (line, 120, cpuFd) != NULL) - if (strncmp (line, "model name", 10) == 0) + if (strncmp (line, "Hardware", 8) == 0) break ; - if (strncmp (line, "model name", 10) != 0) - piBoardRevOops ("No \"model name\" line") ; + if (strncmp (line, "Hardware", 8) != 0) + piBoardRevOops ("No \"Hardware\" line") ; if (wiringPiDebug) - printf ("piboardRev: Model name: %s\n", line) ; + printf ("piboardRev: Hardware: %s\n", line) ; -// See if it's v7 +// See if it's BCM2708 or BCM2709 - if (strstr (line, "ARMv7") != NULL) + if (strstr (line, "BCM2709") != NULL) piModel2 = TRUE ; + else if (strstr (line, "BCM2708") == NULL) + { + fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ; + fprintf (stderr, " - expecting BCM2708 or BCM2709. Please report this to projects@drogon.net\n") ; + exit (EXIT_FAILURE) ; + } // Now do the rest of it as before @@ -750,6 +756,15 @@ int piBoardRev (void) * as much details as we can. * This is undocumented and really only intended for the GPIO command. * Use at your own risk! + * + * for Pi v2: + * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4] + * NEW 23: will be 1 for the new scheme, 0 for the old scheme + * MEMSIZE 20: 0=256M 1=512M 2=1G + * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST + * PROCESSOR 12: 0=2835 1=2836 + * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM + * REV 00: 0=REV0 1=REV1 2=REV2 ********************************************************************************* */ @@ -759,6 +774,9 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) char line [120] ; char *c ; +// Will deal with the properly later on - for now, lets just get it going... +// unsigned int modelNum ; + (void)piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) @@ -781,34 +799,49 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) if (wiringPiDebug) printf ("piboardId: Revision string: %s\n", line) ; -// Scan to first digit - - for (c = line ; *c ; ++c) - if (isdigit (*c)) - break ; - -// Make sure its long enough - - if (strlen (c) < 4) - piBoardRevOops ("Bogus \"Revision\" line") ; - -// If longer than 4, we'll assume it's been overvolted - - *overVolted = strlen (c) > 4 ; - -// Extract last 4 characters: - - c = c + strlen (c) - 4 ; - -// Fill out the replys as appropriate - if (piModel2) { - /**/ if (strcmp (c, "0010") == 0) { *model = PI_MODEL_2 ; *rev = PI_VERSION_1_1 ; *mem = 1024 ; *maker = PI_MAKER_SONY ; } - else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; } + +// Scan to the colon + + for (c = line ; *c ; ++c) + if (*c == ':') + break ; + + if (*c != ':') + piBoardRevOops ("Bogus \"Revision\" line") ; + +// modelNum = (unsigned int)strtol (++c, NULL, 16) ; // Hex number with no leading 0x + + *model = PI_MODEL_2 ; + *rev = PI_VERSION_1_1 ; + *mem = 1024 ; + *maker = PI_MAKER_SONY ; } else { + +// Scan to first digit + + for (c = line ; *c ; ++c) + if (isdigit (*c)) + break ; + +// Make sure its long enough + + if (strlen (c) < 4) + piBoardRevOops ("Bogus \"Revision\" line") ; + +// If longer than 4, we'll assume it's been overvolted + + *overVolted = strlen (c) > 4 ; + +// Extract last 4 characters: + + c = c + strlen (c) - 4 ; + +// Fill out the replys as appropriate + /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } From 5edd177112c99416f68ba3e8c6c4db6ed942e796 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 8 Mar 2015 16:59:33 +0000 Subject: [PATCH 72/97] Updated the board types to cope with an 0014 version in the new compute module --- gpio/version.h | 2 +- wiringPi/wiringPi.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/gpio/version.h b/gpio/version.h index fd96a9b..be8e84c 100644 --- a/gpio/version.h +++ b/gpio/version.h @@ -1 +1 @@ -#define VERSION "2.25" +#define VERSION "2.26" diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index be636a0..2382112 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -627,6 +627,7 @@ int wiringPiFailure (int fatal, const char *message, ...) * 0010 - Model B+, Rev 1.2, 512MB, Sony * 0011 - Pi CM, Rev 1.2, 512MB, Sony * 0012 - Model A+ Rev 1.2, 256MB, Sony + * 0014 - Pi CM, Rev 1.1, 512MB, Sony (Actual Revision might be different) * * For the Pi 2: * 0010 - Model 2, Rev 1.1, Quad Core, 1GB, Sony @@ -638,6 +639,7 @@ int wiringPiFailure (int fatal, const char *message, ...) * last digit, then it's 1, therefore it'll default to not 2 or 3 for a * Rev 1, so will appear as a Rev 2. This is fine for the most part, but * we'll properly detect the Compute Module later and adjust accordingly. + * And the next rev of the CN is 0014 ... * ********************************************************************************* */ @@ -741,7 +743,7 @@ int piBoardRev (void) if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0)) boardRev = 1 ; else - boardRev = 2 ; + boardRev = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2 and CM's. if (wiringPiDebug) printf ("piBoardRev: Returning revision: %d\n", boardRev) ; @@ -809,7 +811,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) break ; if (*c != ':') - piBoardRevOops ("Bogus \"Revision\" line") ; + piBoardRevOops ("Bogus \"Revision\" line (no colon)") ; // modelNum = (unsigned int)strtol (++c, NULL, 16) ; // Hex number with no leading 0x @@ -830,7 +832,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) // Make sure its long enough if (strlen (c) < 4) - piBoardRevOops ("Bogus \"Revision\" line") ; + piBoardRevOops ("Bogus \"Revision\" line (not long enough)") ; // If longer than 4, we'll assume it's been overvolted @@ -857,6 +859,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST ; } + else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; } } } From 95a292824ff9188f013fc230f432acf1a8b1092f Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 9 Sep 2015 23:30:16 +0000 Subject: [PATCH 73/97] Build system: Add missing set -e So that shell scripts do not blunder on after errors. Signed-off-by: Ian Jackson --- build | 2 +- examples/blink.sh | 2 +- examples/q2w/blink.sh | 2 +- gpio/newVersion | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/build b/build index d38ad1b..7c05087 100755 --- a/build +++ b/build @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh -e # build # Simple wiringPi build and install script diff --git a/examples/blink.sh b/examples/blink.sh index 7755383..3975bb7 100644 --- a/examples/blink.sh +++ b/examples/blink.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh -e # # blink.sh: # Standard "blink" program in wiringPi. Blinks an LED connected diff --git a/examples/q2w/blink.sh b/examples/q2w/blink.sh index 2dee6c7..bb6107a 100755 --- a/examples/q2w/blink.sh +++ b/examples/q2w/blink.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh -e # # blink.sh: # Standard "blink" program in wiringPi. Blinks an LED connected diff --git a/gpio/newVersion b/gpio/newVersion index b8728a5..b919743 100755 --- a/gpio/newVersion +++ b/gpio/newVersion @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh -e # # newVersion: # Utility to create the version.h include file for the gpio command. From 2ab836ecea3ba1f9cfef762cff0669be4d9bcf1c Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 9 Sep 2015 23:31:15 +0000 Subject: [PATCH 74/97] Build system: Add a .gitignore Signed-off-by: Ian Jackson --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c1855e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.o +*.so +lib*.so.* +*~ +debian/wiringPi +debian/wiringpi-*.deb +gpio/gpio From cf6f38107e9d0f656ac8f1d3f99121781a7ed17c Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 9 Sep 2015 23:34:18 +0000 Subject: [PATCH 75/97] Build system: Add a formulaic setting of Q from V for debugging We want to be able to disable the many @'s in the Makefiles so that we can see what they are doing, when things go wrong. It is conventional to arrange for setting V=1 to have this effect. Here we supply a formulaic stanza for either setting Q?=@ or doing nothing. (There is sadly no standard place which is included in all the Makefiles so this is probably best). In this patch we do not introduce any users of Q yet. This is because the next patch, which introduces all the users of Q, can be generated entirely automatically. (This is also convenient in case something needs to be rebased across it.) Signed-off-by: Ian Jackson --- devLib/Makefile | 4 ++++ examples/Gertboard/Makefile | 4 ++++ examples/Makefile | 3 +++ examples/PiFace/Makefile | 3 +++ examples/PiGlow/Makefile | 3 +++ examples/q2w/Makefile | 3 +++ gpio/Makefile | 4 ++++ pins/Makefile | 4 ++++ wiringPi/Makefile | 4 ++++ 9 files changed, 32 insertions(+) diff --git a/devLib/Makefile b/devLib/Makefile index d62b532..21ea512 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -25,6 +25,10 @@ VERSION=$(shell cat ../VERSION) DESTDIR=/usr PREFIX=/local +ifneq ($V,1) +Q ?= @ +endif + STATIC=libwiringPiDev.a DYNAMIC=libwiringPiDev.so.$(VERSION) diff --git a/examples/Gertboard/Makefile b/examples/Gertboard/Makefile index 7569261..7be81ab 100644 --- a/examples/Gertboard/Makefile +++ b/examples/Gertboard/Makefile @@ -5,6 +5,10 @@ # Copyright (c) 2013 Gordon Henderson ################################################################################# +ifneq ($V,1) +Q ?= @ +endif + #DEBUG = -g -O0 DEBUG = -O3 CC = gcc diff --git a/examples/Makefile b/examples/Makefile index c9967dc..7671b08 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -22,6 +22,9 @@ # along with wiringPi. If not, see . ################################################################################# +ifneq ($V,1) +Q ?= @ +endif #DEBUG = -g -O0 DEBUG = -O3 diff --git a/examples/PiFace/Makefile b/examples/PiFace/Makefile index 0bde334..7bab7ce 100644 --- a/examples/PiFace/Makefile +++ b/examples/PiFace/Makefile @@ -22,6 +22,9 @@ # along with wiringPi. If not, see . ################################################################################# +ifneq ($V,1) +Q ?= @ +endif #DEBUG = -g -O0 DEBUG = -O3 diff --git a/examples/PiGlow/Makefile b/examples/PiGlow/Makefile index 8d31141..6d8d5bf 100644 --- a/examples/PiGlow/Makefile +++ b/examples/PiGlow/Makefile @@ -22,6 +22,9 @@ # along with wiringPi. If not, see . ################################################################################# +ifneq ($V,1) +Q ?= @ +endif #DEBUG = -g -O0 DEBUG = -O3 diff --git a/examples/q2w/Makefile b/examples/q2w/Makefile index 150c825..a7bc337 100644 --- a/examples/q2w/Makefile +++ b/examples/q2w/Makefile @@ -22,6 +22,9 @@ # along with wiringPi. If not, see . ################################################################################# +ifneq ($V,1) +Q ?= @ +endif #DEBUG = -g -O0 DEBUG = -O3 diff --git a/gpio/Makefile b/gpio/Makefile index 449986e..df05dbe 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -26,6 +26,10 @@ DESTDIR=/usr PREFIX=/local +ifneq ($V,1) +Q ?= @ +endif + #DEBUG = -g -O0 DEBUG = -O2 CC = gcc diff --git a/pins/Makefile b/pins/Makefile index fd34ee9..649961b 100644 --- a/pins/Makefile +++ b/pins/Makefile @@ -1,6 +1,10 @@ SRC = pins.tex +ifneq ($V,1) +Q ?= @ +endif + all: ${SRC} @echo Generating DVI diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 015a894..a04f907 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -25,6 +25,10 @@ VERSION=$(shell cat ../VERSION) DESTDIR=/usr PREFIX=/local +ifneq ($V,1) +Q ?= @ +endif + STATIC=libwiringPi.a DYNAMIC=libwiringPi.so.$(VERSION) From da03f9ed2f5eaeafc154cdadfa9a1c8c9816798d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 9 Sep 2015 23:38:36 +0000 Subject: [PATCH 76/97] Build system: Enable V=1 to disable @'s This patch is autogenerated, with: git-ls-files | grep Makefile | xargs perl -i~ -pe 's/^\t\@/\t\$Q /' Signed-off-by: Ian Jackson --- devLib/Makefile | 66 +++++++++++----------- examples/Gertboard/Makefile | 40 ++++++------- examples/Makefile | 110 ++++++++++++++++++------------------ examples/PiFace/Makefile | 36 ++++++------ examples/PiGlow/Makefile | 32 +++++------ examples/q2w/Makefile | 32 +++++------ gpio/Makefile | 40 ++++++------- pins/Makefile | 10 ++-- wiringPi/Makefile | 66 +++++++++++----------- 9 files changed, 216 insertions(+), 216 deletions(-) diff --git a/devLib/Makefile b/devLib/Makefile index 21ea512..227568c 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -57,66 +57,66 @@ all: $(DYNAMIC) static: $(STATIC) $(STATIC): $(OBJ) - @echo "[Link (Static)]" - @ar rcs $(STATIC) $(OBJ) - @ranlib $(STATIC) + $Q echo "[Link (Static)]" + $Q ar rcs $(STATIC) $(OBJ) + $Q ranlib $(STATIC) # @size $(STATIC) $(DYNAMIC): $(OBJ) - @echo "[Link (Dynamic)]" - @$(CC) -shared -Wl,-soname,libwiringPiDev.so -o libwiringPiDev.so.$(VERSION) -lpthread $(OBJ) + $Q echo "[Link (Dynamic)]" + $Q $(CC) -shared -Wl,-soname,libwiringPiDev.so -o libwiringPiDev.so.$(VERSION) -lpthread $(OBJ) .c.o: - @echo [Compile] $< - @$(CC) -c $(CFLAGS) $< -o $@ + $Q echo [Compile] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ .PHONY: clean clean: - @echo "[Clean]" - @rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPiDev.* + $Q echo "[Clean]" + $Q rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPiDev.* .PHONY: tags tags: $(SRC) - @echo [ctags] - @ctags $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) .PHONY: install install: $(DYNAMIC) - @echo "[Install Headers]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/include - @install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include - @echo "[Install Dynamic Lib]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib - @install -m 0755 libwiringPiDev.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) - @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) $(DESTDIR)/lib/libwiringPiDev.so - @ldconfig + $Q echo "[Install Headers]" + $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/include + $Q install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include + $Q echo "[Install Dynamic Lib]" + $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + $Q install -m 0755 libwiringPiDev.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) + $Q ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) $(DESTDIR)/lib/libwiringPiDev.so + $Q ldconfig .PHONY: install-static install-static: $(STATIC) - @echo "[Install Headers]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/include - @install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include - @echo "[Install Static Lib]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib - @install -m 0755 libwiringPiDev.a $(DESTDIR)$(PREFIX)/lib + $Q echo "[Install Headers]" + $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/include + $Q install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include + $Q echo "[Install Static Lib]" + $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + $Q install -m 0755 libwiringPiDev.a $(DESTDIR)$(PREFIX)/lib .PHONY: install-deb install-deb: $(DYNAMIC) - @echo "[Install Headers: deb]" - @install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/include - @install -m 0644 $(HEADERS) ~/wiringPi/debian/wiringPi/usr/include - @echo "[Install Dynamic Lib: deb]" + $Q echo "[Install Headers: deb]" + $Q install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/include + $Q install -m 0644 $(HEADERS) ~/wiringPi/debian/wiringPi/usr/include + $Q echo "[Install Dynamic Lib: deb]" install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/lib install -m 0755 libwiringPiDev.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so.$(VERSION) ln -sf ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so .PHONY: uninstall uninstall: - @echo "[UnInstall]" - @cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS) - @cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPiDev.* - @ldconfig + $Q echo "[UnInstall]" + $Q cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS) + $Q cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPiDev.* + $Q ldconfig .PHONY: depend diff --git a/examples/Gertboard/Makefile b/examples/Gertboard/Makefile index 7be81ab..1939ad6 100644 --- a/examples/Gertboard/Makefile +++ b/examples/Gertboard/Makefile @@ -33,44 +33,44 @@ BINS = $(SRC:.c=) all: $(BINS) gertboard: gertboard.o - @echo [link] - @$(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ gertboard.o $(LDFLAGS) $(LDLIBS) buttons: buttons.o - @echo [link] - @$(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS) 7segments: 7segments.o - @echo [link] - @$(CC) -o $@ 7segments.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ 7segments.o $(LDFLAGS) $(LDLIBS) voltmeter: voltmeter.o - @echo [link] - @$(CC) -o $@ voltmeter.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ voltmeter.o $(LDFLAGS) $(LDLIBS) temperature: temperature.o - @echo [link] - @$(CC) -o $@ temperature.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ temperature.o $(LDFLAGS) $(LDLIBS) vumeter: vumeter.o - @echo [link] - @$(CC) -o $@ vumeter.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ vumeter.o $(LDFLAGS) $(LDLIBS) record: record.o - @echo [link] - @$(CC) -o $@ record.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ record.o $(LDFLAGS) $(LDLIBS) .c.o: - @echo [CC] $< - @$(CC) -c $(CFLAGS) $< -o $@ + $Q echo [CC] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ clean: - @echo [Clean] - @rm -f $(OBJ) *~ core tags $(BINS) + $Q echo [Clean] + $Q rm -f $(OBJ) *~ core tags $(BINS) tags: $(SRC) - @echo [ctags] - @ctags $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) depend: makedepend -Y $(SRC) diff --git a/examples/Makefile b/examples/Makefile index 7671b08..60659f5 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -54,116 +54,116 @@ OBJ = $(SRC:.c=.o) BINS = $(SRC:.c=) all: - @cat README.TXT - @echo " $(BINS)" | fmt - @echo "" + $Q cat README.TXT + $Q echo " $(BINS)" | fmt + $Q echo "" really-all: $(BINS) blink: blink.o - @echo [link] - @$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) blink8: blink8.o - @echo [link] - @$(CC) -o $@ blink8.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ blink8.o $(LDFLAGS) $(LDLIBS) blink12drcs: blink12drcs.o - @echo [link] - @$(CC) -o $@ blink12drcs.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ blink12drcs.o $(LDFLAGS) $(LDLIBS) blink12: blink12.o - @echo [link] - @$(CC) -o $@ blink12.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ blink12.o $(LDFLAGS) $(LDLIBS) speed: speed.o - @echo [link] - @$(CC) -o $@ speed.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ speed.o $(LDFLAGS) $(LDLIBS) lcd: lcd.o - @echo [link] - @$(CC) -o $@ lcd.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ lcd.o $(LDFLAGS) $(LDLIBS) lcd-adafruit: lcd-adafruit.o - @echo [link] - @$(CC) -o $@ lcd-adafruit.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ lcd-adafruit.o $(LDFLAGS) $(LDLIBS) clock: clock.o - @echo [link] - @$(CC) -o $@ clock.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ clock.o $(LDFLAGS) $(LDLIBS) wfi: wfi.o - @echo [link] - @$(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS) isr: isr.o - @echo [link] - @$(CC) -o $@ isr.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ isr.o $(LDFLAGS) $(LDLIBS) isr-osc: isr-osc.o - @echo [link] - @$(CC) -o $@ isr-osc.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ isr-osc.o $(LDFLAGS) $(LDLIBS) nes: nes.o - @echo [link] - @$(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS) rht03: rht03.o - @echo [link] - @$(CC) -o $@ rht03.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ rht03.o $(LDFLAGS) $(LDLIBS) pwm: pwm.o - @echo [link] - @$(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS) softPwm: softPwm.o - @echo [link] - @$(CC) -o $@ softPwm.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ softPwm.o $(LDFLAGS) $(LDLIBS) softTone: softTone.o - @echo [link] - @$(CC) -o $@ softTone.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ softTone.o $(LDFLAGS) $(LDLIBS) delayTest: delayTest.o - @echo [link] - @$(CC) -o $@ delayTest.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ delayTest.o $(LDFLAGS) $(LDLIBS) serialRead: serialRead.o - @echo [link] - @$(CC) -o $@ serialRead.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ serialRead.o $(LDFLAGS) $(LDLIBS) serialTest: serialTest.o - @echo [link] - @$(CC) -o $@ serialTest.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ serialTest.o $(LDFLAGS) $(LDLIBS) okLed: okLed.o - @echo [link] - @$(CC) -o $@ okLed.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ okLed.o $(LDFLAGS) $(LDLIBS) tone: tone.o - @echo [link] - @$(CC) -o $@ tone.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ tone.o $(LDFLAGS) $(LDLIBS) ds1302: ds1302.o - @echo [link] - @$(CC) -o $@ ds1302.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ ds1302.o $(LDFLAGS) $(LDLIBS) piglow: piglow.o - @echo [link] - @$(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS) .c.o: - @echo [CC] $< - @$(CC) -c $(CFLAGS) $< -o $@ + $Q echo [CC] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ clean: - @echo "[Clean]" - @rm -f $(OBJ) *~ core tags $(BINS) + $Q echo "[Clean]" + $Q rm -f $(OBJ) *~ core tags $(BINS) tags: $(SRC) - @echo [ctags] - @ctags $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) depend: makedepend -Y $(SRC) diff --git a/examples/PiFace/Makefile b/examples/PiFace/Makefile index 7bab7ce..4685adc 100644 --- a/examples/PiFace/Makefile +++ b/examples/PiFace/Makefile @@ -47,40 +47,40 @@ BINS = $(SRC:.c=) all: $(BINS) blink: blink.o - @echo [link] - @$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) buttons: buttons.o - @echo [link] - @$(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ buttons.o $(LDFLAGS) $(LDLIBS) reaction: reaction.o - @echo [link] - @$(CC) -o $@ reaction.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ reaction.o $(LDFLAGS) $(LDLIBS) ladder: ladder.o - @echo [link] - @$(CC) -o $@ ladder.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ ladder.o $(LDFLAGS) $(LDLIBS) metro: metro.o - @echo [link] - @$(CC) -o $@ metro.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ metro.o $(LDFLAGS) $(LDLIBS) motor: motor.o - @echo [link] - @$(CC) -o $@ motor.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ motor.o $(LDFLAGS) $(LDLIBS) .c.o: - @echo [CC] $< - @$(CC) -c $(CFLAGS) $< -o $@ + $Q echo [CC] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ clean: - @echo "[Clean]" - @rm -f $(OBJ) *~ core tags $(BINS) + $Q echo "[Clean]" + $Q rm -f $(OBJ) *~ core tags $(BINS) tags: $(SRC) - @echo [ctags] - @ctags $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) depend: makedepend -Y $(SRC) diff --git a/examples/PiGlow/Makefile b/examples/PiGlow/Makefile index 6d8d5bf..b7a8595 100644 --- a/examples/PiGlow/Makefile +++ b/examples/PiGlow/Makefile @@ -47,34 +47,34 @@ BINS = $(SRC:.c=) all: $(BINS) piGlow0: piGlow0.o - @echo [link] - @$(CC) -o $@ piGlow0.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ piGlow0.o $(LDFLAGS) $(LDLIBS) piGlow1: piGlow1.o - @echo [link] - @$(CC) -o $@ piGlow1.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ piGlow1.o $(LDFLAGS) $(LDLIBS) piglow: piglow.o - @echo [link] - @$(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS) .c.o: - @echo [CC] $< - @$(CC) -c $(CFLAGS) $< -o $@ + $Q echo [CC] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ clean: - @echo "[Clean]" - @rm -f $(OBJ) *~ core tags $(BINS) + $Q echo "[Clean]" + $Q rm -f $(OBJ) *~ core tags $(BINS) tags: $(SRC) - @echo [ctags] - @ctags $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) install: piglow - @echo Installing piglow into /usr/local/bin - @cp -a piglow /usr/local/bin/piglow - @chmod 755 /usr/local/bin/piglow - @echo Done. Remember to load the I2C drivers! + $Q echo Installing piglow into /usr/local/bin + $Q cp -a piglow /usr/local/bin/piglow + $Q chmod 755 /usr/local/bin/piglow + $Q echo Done. Remember to load the I2C drivers! depend: makedepend -Y $(SRC) diff --git a/examples/q2w/Makefile b/examples/q2w/Makefile index a7bc337..6f50fa0 100644 --- a/examples/q2w/Makefile +++ b/examples/q2w/Makefile @@ -46,37 +46,37 @@ BINS = $(SRC:.c=) all: $(BINS) blink: blink.o - @echo [link] - @$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS) blink-io: blink-io.o - @echo [link] - @$(CC) -o $@ blink-io.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ blink-io.o $(LDFLAGS) $(LDLIBS) button: button.o - @echo [link] - @$(CC) -o $@ button.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ button.o $(LDFLAGS) $(LDLIBS) volts: volts.o - @echo [link] - @$(CC) -o $@ volts.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ volts.o $(LDFLAGS) $(LDLIBS) bright: bright.o - @echo [link] - @$(CC) -o $@ bright.o $(LDFLAGS) $(LDLIBS) + $Q echo [link] + $Q $(CC) -o $@ bright.o $(LDFLAGS) $(LDLIBS) .c.o: - @echo [CC] $< - @$(CC) -c $(CFLAGS) $< -o $@ + $Q echo [CC] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ clean: - @echo "[Clean]" - @rm -f $(OBJ) *~ core tags $(BINS) + $Q echo "[Clean]" + $Q rm -f $(OBJ) *~ core tags $(BINS) tags: $(SRC) - @echo [ctags] - @ctags $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) depend: makedepend -Y $(SRC) diff --git a/gpio/Makefile b/gpio/Makefile index df05dbe..09a9cc9 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -52,43 +52,43 @@ version.h: ../VERSION ./newVersion gpio: $(OBJ) - @echo [Link] - @$(CC) -o $@ $(OBJ) $(LDFLAGS) $(LIBS) + $Q echo [Link] + $Q $(CC) -o $@ $(OBJ) $(LDFLAGS) $(LIBS) .c.o: - @echo [Compile] $< - @$(CC) -c $(CFLAGS) $< -o $@ + $Q echo [Compile] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ .PHONY: clean clean: - @echo "[Clean]" - @rm -f $(OBJ) gpio *~ core tags *.bak + $Q echo "[Clean]" + $Q rm -f $(OBJ) gpio *~ core tags *.bak .PHONY: tags tags: $(SRC) - @echo [ctags] - @ctags $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) .PHONY: install install: gpio - @echo "[Install]" - @cp gpio $(DESTDIR)$(PREFIX)/bin - @chown root.root $(DESTDIR)$(PREFIX)/bin/gpio - @chmod 4755 $(DESTDIR)$(PREFIX)/bin/gpio - @mkdir -p $(DESTDIR)$(PREFIX)/man/man1 - @cp gpio.1 $(DESTDIR)$(PREFIX)/man/man1 + $Q echo "[Install]" + $Q cp gpio $(DESTDIR)$(PREFIX)/bin + $Q chown root.root $(DESTDIR)$(PREFIX)/bin/gpio + $Q chmod 4755 $(DESTDIR)$(PREFIX)/bin/gpio + $Q mkdir -p $(DESTDIR)$(PREFIX)/man/man1 + $Q cp gpio.1 $(DESTDIR)$(PREFIX)/man/man1 .PHONY: install-deb install-deb: gpio - @echo "[Install: deb]" - @install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/bin - @install -m 0755 gpio ~/wiringPi/debian/wiringPi/usr/bin + $Q echo "[Install: deb]" + $Q install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/bin + $Q install -m 0755 gpio ~/wiringPi/debian/wiringPi/usr/bin .PHONY: uninstall uninstall: - @echo "[UnInstall]" - @rm -f $(DESTDIR)$(PREFIX)/bin/gpio - @rm -f $(DESTDIR)$(PREFIX)/man/man1/gpio.1 + $Q echo "[UnInstall]" + $Q rm -f $(DESTDIR)$(PREFIX)/bin/gpio + $Q rm -f $(DESTDIR)$(PREFIX)/man/man1/gpio.1 .PHONY: depend depend: diff --git a/pins/Makefile b/pins/Makefile index 649961b..9535fb5 100644 --- a/pins/Makefile +++ b/pins/Makefile @@ -7,16 +7,16 @@ endif all: ${SRC} - @echo Generating DVI - @latex pins.tex + $Q echo Generating DVI + $Q latex pins.tex pins.dvi: pins.tex - @latex pins.tex + $Q latex pins.tex pdf: pins.dvi - @dvipdf pins.dvi + $Q dvipdf pins.dvi .PHONY: clean clean: - @rm -f *.dvi *.aux *.log *.ps *.toc *.bak *~ + $Q rm -f *.dvi *.aux *.log *.ps *.toc *.bak *~ diff --git a/wiringPi/Makefile b/wiringPi/Makefile index a04f907..bc8c848 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -80,67 +80,67 @@ all: $(DYNAMIC) static: $(STATIC) $(STATIC): $(OBJ) - @echo "[Link (Static)]" - @ar rcs $(STATIC) $(OBJ) - @ranlib $(STATIC) + $Q echo "[Link (Static)]" + $Q ar rcs $(STATIC) $(OBJ) + $Q ranlib $(STATIC) # @size $(STATIC) $(DYNAMIC): $(OBJ) - @echo "[Link (Dynamic)]" - @$(CC) -shared -Wl,-soname,libwiringPi.so -o libwiringPi.so.$(VERSION) -lpthread $(OBJ) + $Q echo "[Link (Dynamic)]" + $Q $(CC) -shared -Wl,-soname,libwiringPi.so -o libwiringPi.so.$(VERSION) -lpthread $(OBJ) .c.o: - @echo [Compile] $< - @$(CC) -c $(CFLAGS) $< -o $@ + $Q echo [Compile] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ .PHONY: clean clean: - @echo "[Clean]" - @rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.* + $Q echo "[Clean]" + $Q rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.* .PHONY: tags tags: $(SRC) - @echo [ctags] - @ctags $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) .PHONY: install install: $(DYNAMIC) - @echo "[Install Headers]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/include - @install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include - @echo "[Install Dynamic Lib]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib - @install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) - @ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so - @ldconfig + $Q echo "[Install Headers]" + $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/include + $Q install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include + $Q echo "[Install Dynamic Lib]" + $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + $Q install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) + $Q ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so + $Q ldconfig .PHONY: install-static install-static: $(STATIC) - @echo "[Install Headers]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/include - @install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include - @echo "[Install Static Lib]" - @install -m 0755 -d $(DESTDIR)$(PREFIX)/lib - @install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib + $Q echo "[Install Headers]" + $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/include + $Q install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include + $Q echo "[Install Static Lib]" + $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib + $Q install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib .PHONY: install-deb install-deb: $(DYNAMIC) - @echo "[Install Headers: deb]" - @install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/include - @install -m 0644 $(HEADERS) ~/wiringPi/debian/wiringPi/usr/include - @echo "[Install Dynamic Lib: deb]" + $Q echo "[Install Headers: deb]" + $Q install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/include + $Q install -m 0644 $(HEADERS) ~/wiringPi/debian/wiringPi/usr/include + $Q echo "[Install Dynamic Lib: deb]" install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/lib install -m 0755 libwiringPi.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so.$(VERSION) ln -sf ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so .PHONY: uninstall uninstall: - @echo "[UnInstall]" - @cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS) - @cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPi.* - @ldconfig + $Q echo "[UnInstall]" + $Q cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS) + $Q cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPi.* + $Q ldconfig .PHONY: depend From 5f7dc3263bc2a286f1bc940c94c7e1b44c294076 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 9 Sep 2015 23:39:59 +0000 Subject: [PATCH 77/97] Build system: Remove a trailing tab from gpio/Makefile emacs complains about this line, because it thinks lines with just a tab on in Makefiles are suspicious. Remove the needless tab. No functional change. Signed-off-by: Ian Jackson --- gpio/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpio/Makefile b/gpio/Makefile index 09a9cc9..82416a7 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -54,7 +54,7 @@ version.h: ../VERSION gpio: $(OBJ) $Q echo [Link] $Q $(CC) -o $@ $(OBJ) $(LDFLAGS) $(LIBS) - + .c.o: $Q echo [Compile] $< $Q $(CC) -c $(CFLAGS) $< -o $@ From a6ee925df67de60c1e0d9fe9a09318c775c43fd6 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 10 Sep 2015 00:09:36 +0000 Subject: [PATCH 78/97] Build system: Fix ./build debian We need to -I and -L the build tree directories, not the directories in /usr/local or wheveever. Otherwise (a) the `./debian build' fails if wiringPi is not installed (b) if it _is_ installed, the build picks up the installed versions rather than the versions being built, which is wrong. Signed-off-by: Ian Jackson --- build | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build b/build index 7c05087..00a366b 100755 --- a/build +++ b/build @@ -82,9 +82,9 @@ if [ x$1 = "xdebian" ]; then cd $here/wiringPi make install-deb cd $here/devLib - make install-deb + make install-deb INCLUDE='-I. -I../wiringPi' cd $here/gpio - make install-deb + make install-deb INCLUDE='-I../wiringPi -I../devLib' LDFLAGS=-L../debian/wiringPi/usr/lib cd $here/debian fakeroot dpkg-deb --build wiringPi mv wiringPi.deb wiringpi-`cat $here/VERSION`-1.deb From f8a025189f8c9b4bd21c9d2077a08d0179a4236c Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 10 Sep 2015 00:11:33 +0000 Subject: [PATCH 79/97] Build system: Fix erroneous symlink in devLib/Makefile The symlink libwiringPiDev.so was mistakenly created pointing to libwiringPi.so.$(VERSION) (note lack of Dev), ie to the wrong library. Signed-off-by: Ian Jackson --- devLib/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devLib/Makefile b/devLib/Makefile index 227568c..5cac38e 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -109,7 +109,7 @@ install-deb: $(DYNAMIC) $Q echo "[Install Dynamic Lib: deb]" install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/lib install -m 0755 libwiringPiDev.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so.$(VERSION) - ln -sf ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so + ln -sf ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so .PHONY: uninstall uninstall: From a19bfb579804db76c11ac41a47a097f53ec25ea8 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 12 Sep 2015 16:38:12 +0000 Subject: [PATCH 80/97] examples/blink.sh: Add missing +x bit with chmod Signed-off-by: Ian Jackson --- examples/blink.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 examples/blink.sh diff --git a/examples/blink.sh b/examples/blink.sh old mode 100644 new mode 100755 From 532083f4741214ed9b23ccc4fa5b0b24db2d32a5 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 12 Sep 2015 19:16:50 +0000 Subject: [PATCH 81/97] Manpage gpio(1): Regularise header Signed-off-by: Ian Jackson --- gpio/gpio.1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpio/gpio.1 b/gpio/gpio.1 index 77b41bf..8949442 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -1,4 +1,4 @@ -.TH "GPIO" "January 2015" "Command-Line access to Raspberry Pi's GPIO" +.TH gpio 1 "January 2015" wiringPi "Command-Line access to Raspberry Pi's GPIO" .SH NAME gpio \- Command-line access to Raspberry Pi's GPIO From ac0f074ff7b79eb5201fdbbb5a44200ada2a6f99 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 12 Sep 2015 17:25:39 +0000 Subject: [PATCH 82/97] Build system: Move template-based debian build into debian-template We are going to want to introduce a real Debian source package build system. But that needs to contain a lot of things in debian/ which ought not to appear in the .deb made by the existing ad-hoc `./build debian' arrangements, which we want to keep so as not to unduly disturb existing users. So rename the whole debian/ directory to debian-template/ and change all references in the build system. Signed-off-by: Ian Jackson --- .gitignore | 4 ++-- build | 6 +++--- {debian => debian-template}/wiringPi/DEBIAN/control | 0 {debian => debian-template}/wiringPi/DEBIAN/postinst | 0 {debian => debian-template}/wiringPi/DEBIAN/postrm | 0 devLib/Makefile | 10 +++++----- gpio/Makefile | 4 ++-- wiringPi/Makefile | 10 +++++----- 8 files changed, 17 insertions(+), 17 deletions(-) rename {debian => debian-template}/wiringPi/DEBIAN/control (100%) rename {debian => debian-template}/wiringPi/DEBIAN/postinst (100%) rename {debian => debian-template}/wiringPi/DEBIAN/postrm (100%) diff --git a/.gitignore b/.gitignore index c1855e9..ed2ec80 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,6 @@ *.so lib*.so.* *~ -debian/wiringPi -debian/wiringpi-*.deb +debian-template/wiringPi +debian-template/wiringpi-*.deb gpio/gpio diff --git a/build b/build index 00a366b..524c14a 100755 --- a/build +++ b/build @@ -77,15 +77,15 @@ fi if [ x$1 = "xdebian" ]; then here=`pwd` - cd debian/wiringPi + cd debian-template/wiringPi rm -rf usr cd $here/wiringPi make install-deb cd $here/devLib make install-deb INCLUDE='-I. -I../wiringPi' cd $here/gpio - make install-deb INCLUDE='-I../wiringPi -I../devLib' LDFLAGS=-L../debian/wiringPi/usr/lib - cd $here/debian + make install-deb INCLUDE='-I../wiringPi -I../devLib' LDFLAGS=-L../debian-template/wiringPi/usr/lib + cd $here/debian-template fakeroot dpkg-deb --build wiringPi mv wiringPi.deb wiringpi-`cat $here/VERSION`-1.deb exit diff --git a/debian/wiringPi/DEBIAN/control b/debian-template/wiringPi/DEBIAN/control similarity index 100% rename from debian/wiringPi/DEBIAN/control rename to debian-template/wiringPi/DEBIAN/control diff --git a/debian/wiringPi/DEBIAN/postinst b/debian-template/wiringPi/DEBIAN/postinst similarity index 100% rename from debian/wiringPi/DEBIAN/postinst rename to debian-template/wiringPi/DEBIAN/postinst diff --git a/debian/wiringPi/DEBIAN/postrm b/debian-template/wiringPi/DEBIAN/postrm similarity index 100% rename from debian/wiringPi/DEBIAN/postrm rename to debian-template/wiringPi/DEBIAN/postrm diff --git a/devLib/Makefile b/devLib/Makefile index 5cac38e..48cf3ae 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -104,12 +104,12 @@ install-static: $(STATIC) .PHONY: install-deb install-deb: $(DYNAMIC) $Q echo "[Install Headers: deb]" - $Q install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/include - $Q install -m 0644 $(HEADERS) ~/wiringPi/debian/wiringPi/usr/include + $Q install -m 0755 -d ~/wiringPi/debian-template/wiringPi/usr/include + $Q install -m 0644 $(HEADERS) ~/wiringPi/debian-template/wiringPi/usr/include $Q echo "[Install Dynamic Lib: deb]" - install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/lib - install -m 0755 libwiringPiDev.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so.$(VERSION) - ln -sf ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPiDev.so + install -m 0755 -d ~/wiringPi/debian-template/wiringPi/usr/lib + install -m 0755 libwiringPiDev.so.$(VERSION) ~/wiringPi/debian-template/wiringPi/usr/lib/libwiringPiDev.so.$(VERSION) + ln -sf ~/wiringPi/debian-template/wiringPi/usr/lib/libwiringPiDev.so.$(VERSION) ~/wiringPi/debian-template/wiringPi/usr/lib/libwiringPiDev.so .PHONY: uninstall uninstall: diff --git a/gpio/Makefile b/gpio/Makefile index 82416a7..82a817c 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -81,8 +81,8 @@ install: gpio .PHONY: install-deb install-deb: gpio $Q echo "[Install: deb]" - $Q install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/bin - $Q install -m 0755 gpio ~/wiringPi/debian/wiringPi/usr/bin + $Q install -m 0755 -d ~/wiringPi/debian-template/wiringPi/usr/bin + $Q install -m 0755 gpio ~/wiringPi/debian-template/wiringPi/usr/bin .PHONY: uninstall uninstall: diff --git a/wiringPi/Makefile b/wiringPi/Makefile index bc8c848..3a1b99f 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -128,12 +128,12 @@ install-static: $(STATIC) .PHONY: install-deb install-deb: $(DYNAMIC) $Q echo "[Install Headers: deb]" - $Q install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/include - $Q install -m 0644 $(HEADERS) ~/wiringPi/debian/wiringPi/usr/include + $Q install -m 0755 -d ~/wiringPi/debian-template/wiringPi/usr/include + $Q install -m 0644 $(HEADERS) ~/wiringPi/debian-template/wiringPi/usr/include $Q echo "[Install Dynamic Lib: deb]" - install -m 0755 -d ~/wiringPi/debian/wiringPi/usr/lib - install -m 0755 libwiringPi.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so.$(VERSION) - ln -sf ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so.$(VERSION) ~/wiringPi/debian/wiringPi/usr/lib/libwiringPi.so + install -m 0755 -d ~/wiringPi/debian-template/wiringPi/usr/lib + install -m 0755 libwiringPi.so.$(VERSION) ~/wiringPi/debian-template/wiringPi/usr/lib/libwiringPi.so.$(VERSION) + ln -sf ~/wiringPi/debian-template/wiringPi/usr/lib/libwiringPi.so.$(VERSION) ~/wiringPi/debian-template/wiringPi/usr/lib/libwiringPi.so .PHONY: uninstall uninstall: From 73aa8956c543777c048c185132abe585fcb9ded8 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 12 Sep 2015 16:41:38 +0000 Subject: [PATCH 83/97] build: Introduce a variable "$sudo" and honour WIRINGPI_SUDO We are going to want to not run sudo sometimes. So replace literal invocations of sudo with a variable, which is set to `sudo' by default, but can be overridden from the environment. No functional change if the environment variable is not set. Signed-off-by: Ian Jackson --- build | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/build b/build index 524c14a..c2a589c 100755 --- a/build +++ b/build @@ -43,6 +43,8 @@ check_make_ok() { fi } +sudo=${WIRINGPI_SUDO-sudo} + if [ x$1 = "xclean" ]; then cd wiringPi echo -n "wiringPi: " ; make clean @@ -65,11 +67,11 @@ fi if [ x$1 = "xuninstall" ]; then cd wiringPi - echo -n "wiringPi: " ; sudo make uninstall + echo -n "wiringPi: " ; $sudo make uninstall cd ../devLib - echo -n "DevLib: " ; sudo make uninstall + echo -n "DevLib: " ; $sudo make uninstall cd ../gpio - echo -n "gpio: " ; sudo make uninstall + echo -n "gpio: " ; $sudo make uninstall exit fi @@ -121,30 +123,30 @@ fi echo echo "WiringPi Library" cd wiringPi - sudo make uninstall + $sudo make uninstall if [ x$1 = "xstatic" ]; then make -j5 static check_make_ok - sudo make install-static + $sudo make install-static else make -j5 check_make_ok - sudo make install + $sudo make install fi check_make_ok echo echo "WiringPi Devices Library" cd ../devLib - sudo make uninstall + $sudo make uninstall if [ x$1 = "xstatic" ]; then make -j5 static check_make_ok - sudo make install-static + $sudo make install-static else make -j5 check_make_ok - sudo make install + $sudo make install fi check_make_ok @@ -153,7 +155,7 @@ fi cd ../gpio make -j5 check_make_ok - sudo make install + $sudo make install check_make_ok # echo From 508d6b31bd9564fa2f0a6676089d4495d2917a2d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 12 Sep 2015 17:15:44 +0000 Subject: [PATCH 84/97] Makefiles: Do not override PREFIX or DESTDIR from the environment These are not set in normal shell sessions. Honouring existing settings allows the caller of ./build to specify alternative values. Signed-off-by: Ian Jackson --- devLib/Makefile | 4 ++-- gpio/Makefile | 4 ++-- wiringPi/Makefile | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/devLib/Makefile b/devLib/Makefile index 48cf3ae..9ec163d 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -22,8 +22,8 @@ ################################################################################# VERSION=$(shell cat ../VERSION) -DESTDIR=/usr -PREFIX=/local +DESTDIR?=/usr +PREFIX?=/local ifneq ($V,1) Q ?= @ diff --git a/gpio/Makefile b/gpio/Makefile index 82a817c..095ec48 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -23,8 +23,8 @@ # along with wiringPi. If not, see . ################################################################################# -DESTDIR=/usr -PREFIX=/local +DESTDIR?=/usr +PREFIX?=/local ifneq ($V,1) Q ?= @ diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 3a1b99f..c0623ce 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -22,8 +22,8 @@ ################################################################################# VERSION=$(shell cat ../VERSION) -DESTDIR=/usr -PREFIX=/local +DESTDIR?=/usr +PREFIX?=/local ifneq ($V,1) Q ?= @ From f5dd04b5aea2a26bf393013f33a99b1ccaae864f Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 12 Sep 2015 17:16:46 +0000 Subject: [PATCH 85/97] Makefiles: Honour LDCONFIG from the environment This is not set in normal shell sessions. Honouring an existing setting of LDCONFIG allows the caller of ./build to specify an alternative program to run. Signed-off-by: Ian Jackson --- devLib/Makefile | 6 ++++-- wiringPi/Makefile | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/devLib/Makefile b/devLib/Makefile index 9ec163d..38ab9d9 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -25,6 +25,8 @@ VERSION=$(shell cat ../VERSION) DESTDIR?=/usr PREFIX?=/local +LDCONFIG?=ldconfig + ifneq ($V,1) Q ?= @ endif @@ -90,7 +92,7 @@ install: $(DYNAMIC) $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib $Q install -m 0755 libwiringPiDev.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) $Q ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION) $(DESTDIR)/lib/libwiringPiDev.so - $Q ldconfig + $Q $(LDCONFIG) .PHONY: install-static install-static: $(STATIC) @@ -116,7 +118,7 @@ uninstall: $Q echo "[UnInstall]" $Q cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS) $Q cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPiDev.* - $Q ldconfig + $Q $(LDCONFIG) .PHONY: depend diff --git a/wiringPi/Makefile b/wiringPi/Makefile index c0623ce..5518c9e 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -25,6 +25,8 @@ VERSION=$(shell cat ../VERSION) DESTDIR?=/usr PREFIX?=/local +LDCONFIG?=ldconfig + ifneq ($V,1) Q ?= @ endif @@ -114,7 +116,7 @@ install: $(DYNAMIC) $Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib $Q install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $Q ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so - $Q ldconfig + $Q $(LDCONFIG) .PHONY: install-static install-static: $(STATIC) @@ -140,7 +142,7 @@ uninstall: $Q echo "[UnInstall]" $Q cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS) $Q cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPi.* - $Q ldconfig + $Q $(LDCONFIG) .PHONY: depend From 3ceb07a9a65b4bcbe54a80001522327cfdb9d91d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 12 Sep 2015 17:17:36 +0000 Subject: [PATCH 86/97] Makefiles: Honour WIRINGPI_SUDO=0 This makes it possible for the caller of ./build to suppress the chown/chmod of the gpio utility. Signed-off-by: Ian Jackson --- gpio/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gpio/Makefile b/gpio/Makefile index 095ec48..fa78421 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -73,8 +73,10 @@ tags: $(SRC) install: gpio $Q echo "[Install]" $Q cp gpio $(DESTDIR)$(PREFIX)/bin +ifneq ($(WIRINGPI_SUID),0) $Q chown root.root $(DESTDIR)$(PREFIX)/bin/gpio $Q chmod 4755 $(DESTDIR)$(PREFIX)/bin/gpio +endif $Q mkdir -p $(DESTDIR)$(PREFIX)/man/man1 $Q cp gpio.1 $(DESTDIR)$(PREFIX)/man/man1 From ae421d4a9ccc09c440d5a30cca6f1a206ffec89d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 12 Sep 2015 18:45:09 +0000 Subject: [PATCH 87/97] Makefiles: Honour WIRINGPI_SONAME_SUFFIX This makes it possible for the caller of ./build to add a version number to the sonames of the shared libraries. (This is required for Debian packages.) Signed-off-by: Ian Jackson --- devLib/Makefile | 2 +- wiringPi/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/devLib/Makefile b/devLib/Makefile index 38ab9d9..0fb0033 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -66,7 +66,7 @@ $(STATIC): $(OBJ) $(DYNAMIC): $(OBJ) $Q echo "[Link (Dynamic)]" - $Q $(CC) -shared -Wl,-soname,libwiringPiDev.so -o libwiringPiDev.so.$(VERSION) -lpthread $(OBJ) + $Q $(CC) -shared -Wl,-soname,libwiringPiDev.so$(WIRINGPI_SONAME_SUFFIX) -o libwiringPiDev.so.$(VERSION) -lpthread $(OBJ) .c.o: $Q echo [Compile] $< diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 5518c9e..6bbcc5d 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -89,7 +89,7 @@ $(STATIC): $(OBJ) $(DYNAMIC): $(OBJ) $Q echo "[Link (Dynamic)]" - $Q $(CC) -shared -Wl,-soname,libwiringPi.so -o libwiringPi.so.$(VERSION) -lpthread $(OBJ) + $Q $(CC) -shared -Wl,-soname,libwiringPi.so$(WIRINGPI_SONAME_SUFFIX) -o libwiringPi.so.$(VERSION) -lpthread $(OBJ) .c.o: $Q echo [Compile] $< From a776e6b0e834189b69bd5d33a9bcc5e07ad6cc03 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 12 Sep 2015 17:13:42 +0000 Subject: [PATCH 88/97] Introduce a proper Debian source package build system It's not perfect - there is no symbol versioning, and I have had to make some (perhaps false) assumptions about library ABI stability, but it does produce something vaguely resembling what would be expected from a library package found in Debian, and it does so using the standard Debian source format. Signed-off-by: Ian Jackson --- debian/.gitignore | 9 +++++++ debian/changelog | 6 +++++ debian/compat | 1 + debian/control | 29 ++++++++++++++++++++ debian/copyright | 15 +++++++++++ debian/libwiringpi-dev.dirs | 2 ++ debian/libwiringpi-dev.install | 3 +++ debian/libwiringpi2.install | 1 + debian/libwiringpi2.shlibs | 2 ++ debian/rules | 49 ++++++++++++++++++++++++++++++++++ debian/wiringpi.dirs | 1 + debian/wiringpi.install | 4 +++ 12 files changed, 122 insertions(+) create mode 100644 debian/.gitignore create mode 100644 debian/changelog create mode 100644 debian/compat create mode 100644 debian/control create mode 100644 debian/copyright create mode 100644 debian/libwiringpi-dev.dirs create mode 100644 debian/libwiringpi-dev.install create mode 100644 debian/libwiringpi2.install create mode 100644 debian/libwiringpi2.shlibs create mode 100755 debian/rules create mode 100644 debian/wiringpi.dirs create mode 100644 debian/wiringpi.install diff --git a/debian/.gitignore b/debian/.gitignore new file mode 100644 index 0000000..6296064 --- /dev/null +++ b/debian/.gitignore @@ -0,0 +1,9 @@ +*.debhelper.log +*.substvars +tmp +wiringpi +libwiringpi2 +libwiringpi-dev +files +*.postinst.debhelper +*.postrm.debhelper diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..7f04ad5 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,6 @@ +wiringpi (2.26~iwj) rpi-unstable; urgency=low + + * Initial version with real Debian source package build. + + -- Ian Jackson Sat, 12 Sep 2015 18:31:35 +0100 + diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..45a4fb7 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +8 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..1d6d228 --- /dev/null +++ b/debian/control @@ -0,0 +1,29 @@ +Source: wiringpi +Section: electronics +Priority: optional +Maintainer: Ian Jackson +Standards-Version: 3.8.0 +Homepage: http://wiringpi.com/ +Build-Depends: debhelper (>= 8) + +Package: libwiringpi2 +Section: libs +Architecture: armhf +Depends: ${shlibs:Depends}, ${misc:Depends} +Description: GPIO librariees for Raspberry Pi (runtime). + Runtime for the popular wiringPi library. + +Package: wiringpi +Architecture: armhf +Depends: ${shlibs:Depends}, ${misc:Depends} +Description: gpio utility for Raspberry Pi + The wiringPi gpio command line utility, for GPIO access on a + Raspberry Pi from the command line. + +Package: libwiringpi-dev +Architecture: armhf +Depends: libwiringpi2 (= ${binary:Version}), libc6-dev, ${misc:Depends} +Suggests: wiringpi +Description: GPIO development library for Raspberry Pi + Development libraries to allow GPIO access on a Raspberry Pi from C + and C++ programs. diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..de82701 --- /dev/null +++ b/debian/copyright @@ -0,0 +1,15 @@ +wiringPi is Copyright (C) 2012-2015 Gordon Henderson. + +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. + +On all Debian and Raspbian systems, a copy of the GNU Lesser General +Public License version 3 can be found in +`/usr/share/common-licenses/LGPL-3'. diff --git a/debian/libwiringpi-dev.dirs b/debian/libwiringpi-dev.dirs new file mode 100644 index 0000000..4418816 --- /dev/null +++ b/debian/libwiringpi-dev.dirs @@ -0,0 +1,2 @@ +usr/lib +usr/include diff --git a/debian/libwiringpi-dev.install b/debian/libwiringpi-dev.install new file mode 100644 index 0000000..d7ea901 --- /dev/null +++ b/debian/libwiringpi-dev.install @@ -0,0 +1,3 @@ +debian/tmp/usr/include +debian/tmp/usr/lib/*.so +examples usr/share/doc/libwiringpi-dev diff --git a/debian/libwiringpi2.install b/debian/libwiringpi2.install new file mode 100644 index 0000000..c45ebcf --- /dev/null +++ b/debian/libwiringpi2.install @@ -0,0 +1 @@ +debian/tmp/usr/lib/lib*.so.* diff --git a/debian/libwiringpi2.shlibs b/debian/libwiringpi2.shlibs new file mode 100644 index 0000000..0be8db1 --- /dev/null +++ b/debian/libwiringpi2.shlibs @@ -0,0 +1,2 @@ +libwiringPi 2 libwiringpi2 +libwiringPiDev 2 libwiringpi2 diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..423a473 --- /dev/null +++ b/debian/rules @@ -0,0 +1,49 @@ +#!/usr/bin/make -f + +.PHONY: build + +VERSION:=$(shell cat VERSION) +export VERSION + +soname:=$(shell echo $${VERSION%%.*}) +WIRINGPI_SONAME_SUFFIX:=.$(soname) +export soname +export WIRINGPI_SONAME_SUFFIX + +build: + dh $@ + +override_dh_auto_configure: + +override_dh_prep: + dh_prep -Xdebian/tmp + +dirs: + dh_installdirs -A + mkdir debian/tmp + set -e; for p in `dh_listpackages`; do \ + (cd debian/$$p; find -type d) | \ + (cd debian/tmp; xargs mkdir -p) \ + done + +override_dh_clean: + dh_clean + WIRINGPI_SUDO= bash -xe ./build clean + +override_dh_auto_build: dirs + V=1 LDCONFIG=: WIRINGPI_SUDO= WIRINGPI_SUID=0 \ + DESTDIR=`pwd`/debian/tmp/usr \ + PREFIX= WIRINGPI_SUDO= \ + bash -xe ./build + +override_dh_auto_install: + dh_install + set -ex; for l in libwiringPi libwiringPiDev; do \ + ln -sf $$l.so.$${VERSION} \ + debian/libwiringpi$$soname/usr/lib/$$l.so.$$soname; \ + ln -sf $$l.so.$${VERSION} \ + debian/libwiringpi-dev/usr/lib/$$l.so; \ + done + +%: + dh $@ diff --git a/debian/wiringpi.dirs b/debian/wiringpi.dirs new file mode 100644 index 0000000..e772481 --- /dev/null +++ b/debian/wiringpi.dirs @@ -0,0 +1 @@ +usr/bin diff --git a/debian/wiringpi.install b/debian/wiringpi.install new file mode 100644 index 0000000..41ae22d --- /dev/null +++ b/debian/wiringpi.install @@ -0,0 +1,4 @@ +debian/tmp/usr/bin +debian/tmp/usr/man usr/share +README.TXT usr/share/doc/wiringpi +People usr/share/doc/wiringpi From 170dce5f1939a3629bbc2329c5557cbaa0bc278f Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 20 Sep 2015 19:30:38 +0100 Subject: [PATCH 89/97] Updated the Debian build system thanks to Ian Jackson for the help. --- VERSION | 2 +- debian-template/wiringPi/DEBIAN/control | 2 +- gpio/Makefile | 4 +- gpio/gpio.c | 11 ++++ gpio/version.h | 2 +- gpio/newVersion => newVersion | 21 ++++++- wiringPi/max31855.c | 59 ++++++++++++------- wiringPi/wiringPi.c | 78 ++++++++++++++++++------- 8 files changed, 132 insertions(+), 47 deletions(-) rename gpio/newVersion => newVersion (64%) diff --git a/VERSION b/VERSION index 8bd2249..7dba3a2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.25 +2.27 diff --git a/debian-template/wiringPi/DEBIAN/control b/debian-template/wiringPi/DEBIAN/control index 73ee094..2a65ff5 100644 --- a/debian-template/wiringPi/DEBIAN/control +++ b/debian-template/wiringPi/DEBIAN/control @@ -1,5 +1,5 @@ Package: wiringpi -Version: 2.24 +Version: 2.27 Section: libraries Priority: optional Architecture: armhf diff --git a/gpio/Makefile b/gpio/Makefile index fa78421..e998d84 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -49,7 +49,7 @@ OBJ = $(SRC:.c=.o) all: gpio version.h: ../VERSION - ./newVersion + $Q echo Need to run newVersion above. gpio: $(OBJ) $Q echo [Link] @@ -97,3 +97,5 @@ depend: makedepend -Y $(SRC) # DO NOT DELETE + +gpio.o: version.h diff --git a/gpio/gpio.c b/gpio/gpio.c index 6dc6113..a25f454 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -1194,6 +1194,17 @@ int main (int argc, char *argv []) printf ("Raspberry Pi Details:\n") ; printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n", piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ; + +// Quick check for /dev/gpiomem + + if ((i = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) + printf (" This Raspberry Pi supports user-level GPIO access via /dev/gpiomem.\n") ; + else + { + printf (" You need to run your programs as root for GPIO access\n") ; + printf (" (Old /dev/mem method - consider upgrading)\n") ; + } + } return 0 ; } diff --git a/gpio/version.h b/gpio/version.h index be8e84c..ac80bc5 100644 --- a/gpio/version.h +++ b/gpio/version.h @@ -1 +1 @@ -#define VERSION "2.26" +#define VERSION "2.27" diff --git a/gpio/newVersion b/newVersion similarity index 64% rename from gpio/newVersion rename to newVersion index b919743..bab6dc5 100755 --- a/gpio/newVersion +++ b/newVersion @@ -2,6 +2,7 @@ # # newVersion: # Utility to create the version.h include file for the gpio command. +# and the Debian package # # Copyright (c) 2012-2015 Gordon Henderson ################################################################################# @@ -22,5 +23,21 @@ # along with wiringPi. If not, see . ################################################################################# -rm -f version.h -echo "#define VERSION \"`cat ../VERSION`\"" > version.h +echo Updating to version: `cat VERSION` + +rm -f gpio/version.h +echo "#define VERSION \"`cat VERSION`\"" > gpio/version.h + +rm -f debian-template/wiringPi/DEBIAN/control +cat > debian-template/wiringPi/DEBIAN/control < +Description: The wiringPi libraries, headers and gpio command + Libraries to allow GPIO access on a Raspberry Pi from C and C++ + programs as well as from the command-line +EOF diff --git a/wiringPi/max31855.c b/wiringPi/max31855.c index 2185839..702d7c0 100644 --- a/wiringPi/max31855.c +++ b/wiringPi/max31855.c @@ -22,39 +22,58 @@ *********************************************************************** */ +#include +#include +#include + #include #include #include "max31855.h" -/* - * myAnalogRead: - * Return the analog value of the given pin - * Note: The chip really only has one read "channel", but we're faking it - * here so we can read the error registers. Channel 0 will be the data - * channel, and 1 is the error register code. - * Note: Temperature returned is temp in C * 4, so divide result by 4 - ********************************************************************************* - */ - static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) { - unsigned int spiData ; + uint32_t spiData ; int temp ; int chan = pin - node->pinBase ; wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ; - if (chan == 0) // Read temp in C + spiData = __bswap_32(spiData) ; + + switch (chan) { - spiData >>= 18 ; - temp = spiData & 0x3FFF ; // Bottom 13 bits - if ((spiData & 0x2000) != 0) // Negative - temp = -temp ; - return temp ; + case 0: // Existing read - return raw value * 4 + spiData >>= 18 ; + temp = spiData & 0x1FFF ; // Bottom 13 bits + if ((spiData & 0x2000) != 0) // Negative + temp = -temp ; + + return temp ; + + case 1: // Return error bits + return spiData & 0x7 ; + + case 2: // Return temp in C * 10 + spiData >>= 18 ; + temp = spiData & 0x1FFF ; // Bottom 13 bits + if ((spiData & 0x2000) != 0) // Negative + temp = -temp ; + + return (int)rint ((double)temp * 2.5) ; + + case 3: // Return temp in F * 10 + spiData >>= 18 ; + temp = spiData & 0x1FFF ; // Bottom 13 bits + if ((spiData & 0x2000) != 0) // Negative + temp = -temp ; + + return (int)rint ((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 10.0) ; + + default: // Who knows... + return 0 ; + } - else // Return error bits - return spiData & 0x7 ; } @@ -72,7 +91,7 @@ int max31855Setup (const int pinBase, int spiChannel) if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi return -1 ; - node = wiringPiNewNode (pinBase, 2) ; + node = wiringPiNewNode (pinBase, 4) ; node->fd = spiChannel ; node->analogRead = myAnalogRead ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 2382112..7c899ea 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -130,13 +130,16 @@ struct wiringPiNodeStruct *wiringPiNodes = NULL ; // Access from ARM Running Linux // Taken from Gert/Doms code. Some of this is not in the manual // that I can find )-: +// +// Updates in September 2015 - all now static variables (and apologies for the caps) +// due to the Pi v2 and the new /dev/gpiomem interface -static volatile unsigned int BCM2708_PERI_BASE = 0x20000000 ; // Variable for Pi2 -#define GPIO_PADS (BCM2708_PERI_BASE + 0x00100000) -#define CLOCK_BASE (BCM2708_PERI_BASE + 0x00101000) -#define GPIO_BASE (BCM2708_PERI_BASE + 0x00200000) -#define GPIO_TIMER (BCM2708_PERI_BASE + 0x0000B000) -#define GPIO_PWM (BCM2708_PERI_BASE + 0x0020C000) +static volatile unsigned int RASPBERRY_PI_PERI_BASE ; +static volatile unsigned int GPIO_PADS ; +static volatile unsigned int GPIO_CLOCK_BASE ; +static volatile unsigned int GPIO_BASE ; +static volatile unsigned int GPIO_TIMER ; +static volatile unsigned int GPIO_PWM ; #define PAGE_SIZE (4*1024) #define BLOCK_SIZE (4*1024) @@ -628,6 +631,7 @@ int wiringPiFailure (int fatal, const char *message, ...) * 0011 - Pi CM, Rev 1.2, 512MB, Sony * 0012 - Model A+ Rev 1.2, 256MB, Sony * 0014 - Pi CM, Rev 1.1, 512MB, Sony (Actual Revision might be different) + * 0015 - Model A+ Rev 1.1, 256MB, Sony * * For the Pi 2: * 0010 - Model 2, Rev 1.1, Quad Core, 1GB, Sony @@ -685,7 +689,11 @@ int piBoardRev (void) else if (strstr (line, "BCM2708") == NULL) { fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ; - fprintf (stderr, " - expecting BCM2708 or BCM2709. Please report this to projects@drogon.net\n") ; + fprintf (stderr, " - expecting BCM2708 or BCM2709.\n") ; + fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ; + fprintf (stderr, "to projects@drogon.net. If this is not a Raspberry Pi then you\n") ; + fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ; + fprintf (stderr, "Raspberry Pi ONLY.\n") ; exit (EXIT_FAILURE) ; } @@ -860,6 +868,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST ; } else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; } } } @@ -1829,9 +1838,6 @@ int wiringPiSetup (void) if (getenv (ENV_CODES) != NULL) wiringPiReturnCodes = TRUE ; - if (geteuid () != 0) - (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ; - if (wiringPiDebug) printf ("wiringPi: wiringPiSetup called\n") ; @@ -1844,43 +1850,73 @@ int wiringPiSetup (void) } else // A, B, Rev 2, B+, CM, Pi2 { - if (piModel2) - BCM2708_PERI_BASE = 0x3F000000 ; pinToGpio = pinToGpioR2 ; physToGpio = physToGpioR2 ; } -// Open the master /dev/memory device + if (piModel2) + RASPBERRY_PI_PERI_BASE = 0x3F000000 ; + else + RASPBERRY_PI_PERI_BASE = 0x20000000 ; - if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) - return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; +// Open the master /dev/ memory control device -// GPIO: +// See if /dev/gpiomem exists and we can open it... + + if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) + RASPBERRY_PI_PERI_BASE = 0 ; + +// ... otherwise fall back to the original /dev/mem which requires root level access + + else + { + +// This check is here because people are too stupid to check for themselves or read +// error messages. + + if (geteuid () != 0) + (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ; + + if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) + return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ; + } + +// Set the offsets into the memory interface. + + GPIO_PADS = RASPBERRY_PI_PERI_BASE + 0x00100000 ; + GPIO_CLOCK_BASE = RASPBERRY_PI_PERI_BASE + 0x00101000 ; + GPIO_BASE = RASPBERRY_PI_PERI_BASE + 0x00200000 ; + GPIO_TIMER = RASPBERRY_PI_PERI_BASE + 0x0000B000 ; + GPIO_PWM = RASPBERRY_PI_PERI_BASE + 0x0020C000 ; + +// Map the individual hardware components + +// GPIO: gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ; if ((int32_t)gpio == -1) return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ; -// PWM +// PWM pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ; if ((int32_t)pwm == -1) return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ; -// Clock control (needed for PWM) +// Clock control (needed for PWM) - clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ; + clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ; if ((int32_t)clk == -1) return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ; -// The drive pads +// The drive pads pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ; if ((int32_t)pads == -1) return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ; #ifdef USE_TIMER -// The system timer +// The system timer timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ; if ((int32_t)timer == -1) From f6c40cb2a69a43de97903c9fdd6f24a4f32f8eae Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 24 Sep 2015 22:35:31 +0100 Subject: [PATCH 90/97] Fixed the requirement for -lm Updated the gpio program to be device tree aware. --- VERSION | 2 +- debian-template/wiringPi/DEBIAN/control | 2 +- examples/Makefile | 10 ++--- examples/PiGlow/Makefile | 2 +- examples/max31855.c | 49 +++++++++++++++++++++++++ gpio/gpio.1 | 20 ++++------ gpio/gpio.c | 38 +++++++++++++++---- gpio/version.h | 2 +- wiringPi/max31855.c | 7 ++-- 9 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 examples/max31855.c diff --git a/VERSION b/VERSION index 7dba3a2..410158a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.27 +2.28 diff --git a/debian-template/wiringPi/DEBIAN/control b/debian-template/wiringPi/DEBIAN/control index 2a65ff5..9def8c5 100644 --- a/debian-template/wiringPi/DEBIAN/control +++ b/debian-template/wiringPi/DEBIAN/control @@ -1,5 +1,5 @@ Package: wiringpi -Version: 2.27 +Version: 2.28 Section: libraries Priority: optional Architecture: armhf diff --git a/examples/Makefile b/examples/Makefile index 60659f5..04bcaec 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -3,7 +3,7 @@ # wiringPi - Wiring Compatable library for the Raspberry Pi # https://projects.drogon.net/wiring-pi # -# Copyright (c) 2012 Gordon Henderson +# Copyright (c) 2012-2015 Gordon Henderson ################################################################################# # This file is part of wiringPi: # Wiring Compatable library for the Raspberry Pi @@ -47,7 +47,8 @@ SRC = blink.c blink8.c blink12.c \ softPwm.c softTone.c \ delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \ lowPower.c \ - rht03.c piglow.c + max31855.c \ + rht03.c OBJ = $(SRC:.c=.o) @@ -148,10 +149,9 @@ ds1302: ds1302.o $Q echo [link] $Q $(CC) -o $@ ds1302.o $(LDFLAGS) $(LDLIBS) -piglow: piglow.o +max31855: max31855.o $Q echo [link] - $Q $(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS) - + $Q $(CC) -o $@ max31855.c.o $(LDFLAGS) $(LDLIBS) .c.o: $Q echo [CC] $< diff --git a/examples/PiGlow/Makefile b/examples/PiGlow/Makefile index b7a8595..acd4818 100644 --- a/examples/PiGlow/Makefile +++ b/examples/PiGlow/Makefile @@ -3,7 +3,7 @@ # wiringPi - Wiring Compatable library for the Raspberry Pi # https://projects.drogon.net/wiring-pi # -# Copyright (c) 2012-2013 Gordon Henderson +# Copyright (c) 2012-2015 Gordon Henderson ################################################################################# # This file is part of wiringPi: # Wiring Compatable library for the Raspberry Pi diff --git a/examples/max31855.c b/examples/max31855.c new file mode 100644 index 0000000..2060a55 --- /dev/null +++ b/examples/max31855.c @@ -0,0 +1,49 @@ +/* + * max31855.c: + * SPI Thermocouple interface chip + * + * Copyright (c) 2015 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 +#include +#include + +#include +#include + +int main (int argc, char *argv []) +{ + int i ; + + wiringPiSetup () ; + max31855Setup (200, 0) ; + max31855Setup (400, 1) ; + + for (;;) + { + printf ("%6d, %6d, %6d, %6d ::::", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ; + printf ("%6d, %6d, %6d, %6d\n", analogRead (400), analogRead (401), analogRead (402), analogRead (403)) ; + delay (500) ; + } + +} diff --git a/gpio/gpio.1 b/gpio/gpio.1 index 8949442..673ebbe 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -1,4 +1,4 @@ -.TH gpio 1 "January 2015" wiringPi "Command-Line access to Raspberry Pi's GPIO" +.TH GPIO 1 "September 2015" wiringPi "Command-Line access to Raspberry Pi's GPIO" .SH NAME gpio \- Command-line access to Raspberry Pi's GPIO @@ -251,12 +251,9 @@ on the associated /dev/ entries so that the current user has access to them. Optionally it will set the I2C baudrate to that supplied in Kb/sec (or as close as the Pi can manage) The default speed is 100Kb/sec. -Note that on a Pi with a recent 3.18 kernel with the device-tree structure -enable, the load may fail until you add: - -.I dtparam=i2c=on - -into \fB/boot/config.txt\fR to allow user use of the I2C bus. +Note: On recent kernels with the device tree enabled you should use the +raspi-config program to load/unload the I2C device at boot time. +(or disable the device tree to continue to use this method) .TP .B load spi @@ -269,12 +266,9 @@ this has fixed the buffer size. The way to change it now is to edit the /boot/cmdline.txt file and add on spdev.bufsiz=8192 to set it to e.g. 8192 bytes then reboot. -Note that on a Pi with a recent 3.18 kernel with the device-tree structure -enable, the load may fail until you add: - -.I dtparam=spi=on - -into \fB/boot/config.txt\fR to allow user use of the I2C bus. +Note: On recent kernels with the device tree enabled you should use the +raspi-config program to load/unload the SPI device at boot time. +(or disable the device tree to continue to use this method) .TP .B gbr diff --git a/gpio/gpio.c b/gpio/gpio.c index a25f454..5c87a7e 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -116,7 +116,7 @@ static void changeOwner (char *cmd, char *file) if (chown (file, uid, gid) != 0) { if (errno == ENOENT) // Warn that it's not there - fprintf (stderr, "%s: Warning (not an error): File not present: %s\n", cmd, file) ; + fprintf (stderr, "%s: Warning (not an error, do not report): File not present: %s\n", cmd, file) ; else fprintf (stderr, "%s: Warning (not an error): Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ; } @@ -163,6 +163,22 @@ static int moduleLoaded (char *modName) ********************************************************************************* */ +static void checkDevTree (char *argv []) +{ + struct stat statBuf ; + + if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ... + { + fprintf (stderr, +"%s: Unable to load/unload modules as this Pi has the device tree enabled.\n" +" You need to run the raspi-config program (as root) and select the\n" +" modules (SPI or I2C) that you wish to load/unload there and reboot.\n" +" There is more information here:\n" +" https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=97314\n", argv [0]) ; + exit (1) ; + } +} + static void _doLoadUsage (char *argv []) { fprintf (stderr, "Usage: %s load [I2C baudrate in Kb/sec]\n", argv [0]) ; @@ -176,6 +192,8 @@ static void doLoad (int argc, char *argv []) char *file1, *file2 ; char args1 [32], args2 [32] ; + checkDevTree (argv) ; + if (argc < 3) _doLoadUsage (argv) ; @@ -251,6 +269,8 @@ static void doUnLoad (int argc, char *argv []) char *module1, *module2 ; char cmd [80] ; + checkDevTree (argv) ; + if (argc != 3) _doUnLoadUsage (argv) ; @@ -1138,6 +1158,8 @@ int main (int argc, char *argv []) { int i ; int model, rev, mem, maker, overVolted ; + struct stat statBuf ; + if (getenv ("WIRINGPI_DEBUG") != NULL) { @@ -1195,15 +1217,15 @@ int main (int argc, char *argv []) printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n", piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ; -// Quick check for /dev/gpiomem +// Check for device tree - if ((i = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) - printf (" This Raspberry Pi supports user-level GPIO access via /dev/gpiomem.\n") ; + if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ... + printf (" Device tree is enabled.\n") ; + + if (stat ("/dev/gpiomem", &statBuf) == 0) // User level GPIO is GO + printf (" This Raspberry Pi supports user-level GPIO access.\n") ; else - { - printf (" You need to run your programs as root for GPIO access\n") ; - printf (" (Old /dev/mem method - consider upgrading)\n") ; - } + printf (" * Root or sudo required for GPIO access.\n") ; } return 0 ; diff --git a/gpio/version.h b/gpio/version.h index ac80bc5..704c10d 100644 --- a/gpio/version.h +++ b/gpio/version.h @@ -1 +1 @@ -#define VERSION "2.27" +#define VERSION "2.28" diff --git a/wiringPi/max31855.c b/wiringPi/max31855.c index 702d7c0..ea184d8 100644 --- a/wiringPi/max31855.c +++ b/wiringPi/max31855.c @@ -1,7 +1,7 @@ /* * max31855.c: * Extend wiringPi with the max31855 SPI Analog to Digital convertor - * Copyright (c) 2012-2013 Gordon Henderson + * Copyright (c) 2012-2015 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -24,7 +24,6 @@ #include #include -#include #include #include @@ -60,7 +59,7 @@ static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) if ((spiData & 0x2000) != 0) // Negative temp = -temp ; - return (int)rint ((double)temp * 2.5) ; + return (int)((((double)temp * 25) + 0.5) / 10.0) ; case 3: // Return temp in F * 10 spiData >>= 18 ; @@ -68,7 +67,7 @@ static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) if ((spiData & 0x2000) != 0) // Negative temp = -temp ; - return (int)rint ((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 10.0) ; + return (int)((((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 100.0) + 0.5) / 10.0) ; default: // Who knows... return 0 ; From d79506694d7ba1c3da865d095238289d6175057d Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Wed, 30 Sep 2015 16:11:42 +0100 Subject: [PATCH 91/97] Fixing a few issues - makefile goofs. Also adding in the stuff for /dev/gpiomem with warnings and information in the gpio manual page (who reads that though!) --- VERSION | 2 +- debian-template/wiringPi/DEBIAN/control | 2 +- examples/Makefile | 4 +-- examples/max31855.c | 17 ++++++++-- gpio/Makefile | 2 +- gpio/gpio.1 | 14 +++++++- gpio/gpio.c | 3 ++ gpio/version.h | 2 +- wiringPi/wiringPi.c | 45 ++++++++++++++++++++++++- 9 files changed, 80 insertions(+), 11 deletions(-) diff --git a/VERSION b/VERSION index 410158a..072e651 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.28 +2.29 diff --git a/debian-template/wiringPi/DEBIAN/control b/debian-template/wiringPi/DEBIAN/control index 9def8c5..3b15a24 100644 --- a/debian-template/wiringPi/DEBIAN/control +++ b/debian-template/wiringPi/DEBIAN/control @@ -1,5 +1,5 @@ Package: wiringpi -Version: 2.28 +Version: 2.29 Section: libraries Priority: optional Architecture: armhf diff --git a/examples/Makefile b/examples/Makefile index 04bcaec..e6b9b71 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -45,7 +45,7 @@ SRC = blink.c blink8.c blink12.c \ lcd.c lcd-adafruit.c clock.c \ nes.c \ softPwm.c softTone.c \ - delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \ + delayTest.c serialRead.c serialTestc okLed.c ds1302.c \ lowPower.c \ max31855.c \ rht03.c @@ -151,7 +151,7 @@ ds1302: ds1302.o max31855: max31855.o $Q echo [link] - $Q $(CC) -o $@ max31855.c.o $(LDFLAGS) $(LDLIBS) + $Q $(CC) -o $@ max31855.o $(LDFLAGS) $(LDLIBS) .c.o: $Q echo [CC] $< diff --git a/examples/max31855.c b/examples/max31855.c index 2060a55..36b3cf6 100644 --- a/examples/max31855.c +++ b/examples/max31855.c @@ -33,7 +33,7 @@ int main (int argc, char *argv []) { - int i ; + int i = 0 ; wiringPiSetup () ; max31855Setup (200, 0) ; @@ -41,9 +41,20 @@ int main (int argc, char *argv []) for (;;) { - printf ("%6d, %6d, %6d, %6d ::::", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ; - printf ("%6d, %6d, %6d, %6d\n", analogRead (400), analogRead (401), analogRead (402), analogRead (403)) ; + if (i == 0) + { + printf ("+------+------+------+------++------+------+------+------+\n") ; + printf ("| Raw | Err | C | F || Raw | Err | C | F |\n") ; + printf ("+------+------+------+------++------+------+------+------+\n") ; + } + + printf ("| %4d | %4d | %4d | %4d |", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ; + printf ("| %4d | %4d | %4d | %4d |\n", analogRead (400), analogRead (401), analogRead (402), analogRead (403)) ; delay (500) ; + + if (++i == 10) + i = 0 ; + } } diff --git a/gpio/Makefile b/gpio/Makefile index e998d84..7dcd090 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -37,7 +37,7 @@ INCLUDE = -I$(DESTDIR)$(PREFIX)/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L$(DESTDIR)$(PREFIX)/lib -LIBS = -lwiringPi -lwiringPiDev -lpthread -lm +LIBS = -lwiringPi -lwiringPiDev -lpthread # May not need to alter anything below this line ############################################################################### diff --git a/gpio/gpio.1 b/gpio/gpio.1 index 673ebbe..e5fe181 100644 --- a/gpio/gpio.1 +++ b/gpio/gpio.1 @@ -260,7 +260,7 @@ raspi-config program to load/unload the I2C device at boot time. This loads the spi drivers into the kernel and changes the permissions on the associated /dev/ entries so that the current user has access to them. It used to have the ability to change the buffer size from the -default of 4096 bytes to an arbitary value, however for some time the +default of 4096 bytes to an arbitrary value, however for some time the Pi Foundation have compiled the SPI device driver into the kernel and this has fixed the buffer size. The way to change it now is to edit the /boot/cmdline.txt file and add on spdev.bufsiz=8192 to set it to @@ -321,6 +321,18 @@ 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. +As of kernels 4.1.7, a user-level GPIO access mechanism is available, +however wiringPi will not use this by default - because at this time +there appears to be issues when trying to program the PWM or clock output +hardware. If you can live without PWM or GPIO clocks and you want to use +the GPIO from a non-root program, then you need to make sure that the +module \fIbcm2835_gpiomem\fR is loaded at boot time. This should happen +automatically when you enable the device tree in raspi-config. You may +also need some additional information in /etc/udev/rules.d/ to change the +mode and ownership of the /dev/gpiomem file. Finally, you need to set +the environment variable \fIWIRINGPI_GPIOMEM\fR. This will go-away +in future releases once the /dev/gpiomem interface is fully operational. + .SH "SEE ALSO" .LP diff --git a/gpio/gpio.c b/gpio/gpio.c index 5c87a7e..4eef19d 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -1223,7 +1223,10 @@ int main (int argc, char *argv []) printf (" Device tree is enabled.\n") ; if (stat ("/dev/gpiomem", &statBuf) == 0) // User level GPIO is GO + { printf (" This Raspberry Pi supports user-level GPIO access.\n") ; + printf (" -> See the man-page for more details\n") ; + } else printf (" * Root or sudo required for GPIO access.\n") ; diff --git a/gpio/version.h b/gpio/version.h index 704c10d..ac09492 100644 --- a/gpio/version.h +++ b/gpio/version.h @@ -1 +1 @@ -#define VERSION "2.28" +#define VERSION "2.29" diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 7c899ea..32e5100 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -84,6 +84,7 @@ #define ENV_DEBUG "WIRINGPI_DEBUG" #define ENV_CODES "WIRINGPI_CODES" +#define ENV_GPIOMEM "WIRINGPI_GPIOMEM" // Mask for the bottom 64 pins which belong to the Raspberry Pi @@ -253,6 +254,10 @@ static pthread_mutex_t pinMutex ; int wiringPiDebug = FALSE ; int wiringPiReturnCodes = FALSE ; +// Use /dev/gpiomem ? + +int wiringPiTryGpioMem = FALSE ; + // sysFds: // Map a file descriptor from the /sys/class/gpio/gpioX/value @@ -913,6 +918,9 @@ void setPadDrive (int group, int value) if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if ((group < 0) || (group > 2)) return ; @@ -986,6 +994,9 @@ void pwmSetRange (unsigned int range) { if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ; *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ; } @@ -1007,6 +1018,9 @@ void pwmSetClock (int divisor) if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO)) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if (wiringPiDebug) printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ; @@ -1059,6 +1073,9 @@ void gpioClockSet (int pin, int freq) else if (wiringPiMode != WPI_MODE_GPIO) return ; + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + divi = 19200000 / freq ; divr = 19200000 % freq ; divf = (int)((double)divr * 4096.0 / 19200000.0) ; @@ -1231,11 +1248,17 @@ void pinMode (int pin, int mode) softToneCreate (origPin) ; else if (mode == PWM_TONE_OUTPUT) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode pwmSetMode (PWM_MODE_MS) ; } else if (mode == PWM_OUTPUT) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin return ; @@ -1250,6 +1273,9 @@ void pinMode (int pin, int mode) } else if (mode == GPIO_CLOCK) { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin return ; @@ -1404,6 +1430,9 @@ void pwmWrite (int pin, int value) if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin { + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + /**/ if (wiringPiMode == WPI_MODE_PINS) pin = pinToGpio [pin] ; else if (wiringPiMode == WPI_MODE_PHYS) @@ -1470,6 +1499,9 @@ void pwmToneWrite (int pin, int freq) { int range ; + if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now + return ; + if (freq == 0) pwmWrite (pin, 0) ; // Off else @@ -1838,8 +1870,15 @@ int wiringPiSetup (void) if (getenv (ENV_CODES) != NULL) wiringPiReturnCodes = TRUE ; + if (getenv (ENV_GPIOMEM) != NULL) + wiringPiTryGpioMem = TRUE ; + if (wiringPiDebug) + { printf ("wiringPi: wiringPiSetup called\n") ; + if (wiringPiTryGpioMem) + printf ("wiringPi: Using /dev/gpiomem\n") ; + } boardRev = piBoardRev () ; @@ -1863,8 +1902,12 @@ int wiringPiSetup (void) // See if /dev/gpiomem exists and we can open it... - if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) + if (wiringPiTryGpioMem) + { + if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) + return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/gpiomem: %s\n", strerror (errno)) ; RASPBERRY_PI_PERI_BASE = 0 ; + } // ... otherwise fall back to the original /dev/mem which requires root level access From bb6f08487cc8025c8e7d0260cfb493f2a07452a5 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 29 Nov 2015 21:08:04 +0000 Subject: [PATCH 92/97] Updated wiringPi for the new Pi Zero. Also affects the Pi v2 which also uses the new Revision encoding scheme (It was slightly bodged before) This ought to be relatively future proof, but who knows. Fixed a minor thing in the examples Makefile (not that anyone bothers to use it, judging by the emails I get )-: --- VERSION | 2 +- debian-template/wiringPi/DEBIAN/control | 2 +- examples/Makefile | 2 +- gpio/gpio.c | 99 ++++---- gpio/version.h | 2 +- wiringPi/wiringPi.c | 321 ++++++++++++++++-------- wiringPi/wiringPi.h | 38 ++- 7 files changed, 291 insertions(+), 175 deletions(-) diff --git a/VERSION b/VERSION index 072e651..81b133a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.29 +2.30 diff --git a/debian-template/wiringPi/DEBIAN/control b/debian-template/wiringPi/DEBIAN/control index 3b15a24..7b739c6 100644 --- a/debian-template/wiringPi/DEBIAN/control +++ b/debian-template/wiringPi/DEBIAN/control @@ -1,5 +1,5 @@ Package: wiringpi -Version: 2.29 +Version: 2.30 Section: libraries Priority: optional Architecture: armhf diff --git a/examples/Makefile b/examples/Makefile index e6b9b71..4278b6d 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -45,7 +45,7 @@ SRC = blink.c blink8.c blink12.c \ lcd.c lcd-adafruit.c clock.c \ nes.c \ softPwm.c softTone.c \ - delayTest.c serialRead.c serialTestc okLed.c ds1302.c \ + delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \ lowPower.c \ max31855.c \ rht03.c diff --git a/gpio/gpio.c b/gpio/gpio.c index 4eef19d..6c95b21 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -1148,6 +1148,56 @@ static void doPwmClock (int argc, char *argv []) } +/* + * doVersion: + * Handle the ever more complicated version command + ********************************************************************************* + */ + +static void doVersion (char *argv []) +{ + int model, rev, mem, maker, warranty ; + struct stat statBuf ; + + printf ("gpio version: %s\n", VERSION) ; + printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ; + printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ; + printf ("For details type: %s -warranty\n", argv [0]) ; + printf ("\n") ; + piBoardId (&model, &rev, &mem, &maker, &warranty) ; + +/************* + if (model == PI_MODEL_UNKNOWN) + { + printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ; + printf (" projects@drogon.net\n") ; + printf ("with a copy of your /proc/cpuinfo if possible\n") ; + } + else +***************/ + + { + printf ("Raspberry Pi Details:\n") ; + printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n", + piModelNames [model], piRevisionNames [rev], piMemorySize [mem], piMakerNames [maker], warranty ? "[Out of Warranty]" : "") ; + +// Check for device tree + + if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ... + printf (" Device tree is enabled.\n") ; + + if (stat ("/dev/gpiomem", &statBuf) == 0) // User level GPIO is GO + { + printf (" This Raspberry Pi supports user-level GPIO access.\n") ; + printf (" -> See the man-page for more details\n") ; + } + else + printf (" * Root or sudo required for GPIO access.\n") ; + + } +} + + /* * main: * Start here @@ -1157,9 +1207,6 @@ static void doPwmClock (int argc, char *argv []) int main (int argc, char *argv []) { int i ; - int model, rev, mem, maker, overVolted ; - struct stat statBuf ; - if (getenv ("WIRINGPI_DEBUG") != NULL) { @@ -1181,56 +1228,20 @@ int main (int argc, char *argv []) return 0 ; } -// Sort of a special: - - if (strcmp (argv [1], "-R") == 0) - { - printf ("%d\n", piBoardRev ()) ; - return 0 ; - } - // Version & Warranty +// Wish I could remember why I have both -R and -V ... - if (strcmp (argv [1], "-V") == 0) + if ((strcmp (argv [1], "-R") == 0) || (strcmp (argv [1], "-V") == 0)) { printf ("%d\n", piBoardRev ()) ; return 0 ; } +// Version and information + if (strcmp (argv [1], "-v") == 0) { - printf ("gpio version: %s\n", VERSION) ; - printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ; - printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ; - printf ("For details type: %s -warranty\n", argv [0]) ; - printf ("\n") ; - piBoardId (&model, &rev, &mem, &maker, &overVolted) ; - if (model == PI_MODEL_UNKNOWN) - { - printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ; - printf (" projects@drogon.net\n") ; - printf ("with a copy of your /proc/cpuinfo if possible\n") ; - } - else - { - printf ("Raspberry Pi Details:\n") ; - printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n", - piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ; - -// Check for device tree - - if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ... - printf (" Device tree is enabled.\n") ; - - if (stat ("/dev/gpiomem", &statBuf) == 0) // User level GPIO is GO - { - printf (" This Raspberry Pi supports user-level GPIO access.\n") ; - printf (" -> See the man-page for more details\n") ; - } - else - printf (" * Root or sudo required for GPIO access.\n") ; - - } + doVersion (argv) ; return 0 ; } diff --git a/gpio/version.h b/gpio/version.h index ac09492..a46348a 100644 --- a/gpio/version.h +++ b/gpio/version.h @@ -1 +1 @@ -#define VERSION "2.29" +#define VERSION "2.30" diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 32e5100..503151f 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -209,35 +209,77 @@ static volatile uint32_t *timerIrqRaw ; static int piModel2 = FALSE ; -const char *piModelNames [7] = +const char *piModelNames [16] = { - "Unknown", - "Model A", - "Model B", - "Model B+", - "Compute Module", - "Model A+", - "Model 2", // Quad Core + "Model A", // 0 + "Model B", // 1 + "Model A+", // 2 + "Model B+", // 3 + "Pi 2", // 4 + "Alpha", // 5 + "CM", // 6 + "Unknown07", // 07 + "Unknown08", // 08 + "Pi Zero", // 09 + "Unknown10", // 10 + "Unknown11", // 11 + "Unknown12", // 12 + "Unknown13", // 13 + "Unknown14", // 14 + "Unknown15", // 15 } ; -const char *piRevisionNames [5] = +const char *piRevisionNames [16] = { - "Unknown", - "1", - "1.1", - "1.2", - "2", + "00", + "01", + "02", + "03", + "04", + "05", + "06", + "07", + "08", + "09", + "10", + "11", + "12", + "13", + "14", + "15", } ; -const char *piMakerNames [5] = +const char *piMakerNames [16] = { - "Unknown", - "Egoman", - "Sony", - "Qusda", - "MBest", + "Sony", // 0 + "Egoman", // 1 + "Embest", // 2 + "Unknown", // 3 + "Embest", // 4 + "Unknown05", // 5 + "Unknown06", // 6 + "Unknown07", // 7 + "Unknown08", // 8 + "Unknown09", // 9 + "Unknown10", // 10 + "Unknown11", // 11 + "Unknown12", // 12 + "Unknown13", // 13 + "Unknown14", // 14 + "Unknown15", // 15 } ; +const int piMemorySize [8] = +{ + 256, // 0 + 512, // 1 + 1024, // 2 + 0, // 3 + 0, // 4 + 0, // 5 + 0, // 6 + 0, // 7 +} ; // Time for easy calculations @@ -612,43 +654,20 @@ int wiringPiFailure (int fatal, const char *message, ...) /* * piBoardRev: * Return a number representing the hardware revision of the board. + * This is not strictly the board revision but is used to check the + * layout of the GPIO connector - and there are 2 types that we are + * really interested in here. The very earliest Pi's and the + * ones that came after that which switched some pins .... * - * Revision 1 really means the early Model B's. + * Revision 1 really means the early Model A and B's. * Revision 2 is everything else - it covers the B, B+ and CM. * ... and the Pi 2 - which is a B+ ++ ... + * ... and the Pi 0 - which is an A+ ... * - * Seems there are some boards with 0000 in them (mistake in manufacture) - * So the distinction between boards that I can see is: - * 0000 - Error - * 0001 - Not used - * 0002 - Model B, Rev 1, 256MB, Egoman - * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed. - * 0004 - Model B, Rev 2, 256MB, Sony - * 0005 - Model B, Rev 2, 256MB, Qisda - * 0006 - Model B, Rev 2, 256MB, Egoman - * 0007 - Model A, Rev 2, 256MB, Egoman - * 0008 - Model A, Rev 2, 256MB, Sony - * 0009 - Model A, Rev 2, 256MB, Qisda - * 000d - Model B, Rev 2, 512MB, Egoman - * 000e - Model B, Rev 2, 512MB, Sony - * 000f - Model B, Rev 2, 512MB, Qisda - * 0010 - Model B+, Rev 1.2, 512MB, Sony - * 0011 - Pi CM, Rev 1.2, 512MB, Sony - * 0012 - Model A+ Rev 1.2, 256MB, Sony - * 0014 - Pi CM, Rev 1.1, 512MB, Sony (Actual Revision might be different) - * 0015 - Model A+ Rev 1.1, 256MB, Sony - * - * For the Pi 2: - * 0010 - Model 2, Rev 1.1, Quad Core, 1GB, Sony - * - * A small thorn is the olde style overvolting - that will add in - * 1000000 - * - * The Pi compute module has an revision of 0011 - since we only check the - * last digit, then it's 1, therefore it'll default to not 2 or 3 for a - * Rev 1, so will appear as a Rev 2. This is fine for the most part, but - * we'll properly detect the Compute Module later and adjust accordingly. - * And the next rev of the CN is 0014 ... + * The main difference between the revision 1 and 2 system that I use here + * is the mapping of the GPIO pins. From revision 2, the Pi Foundation changed + * 3 GPIO pins on the (original) 26-way header - BCM_GPIO 22 was dropped and + * replaced with 27, and 0 + 1 - I2C bus 0 was changed to 2 + 3; I2C bus 1. * ********************************************************************************* */ @@ -675,22 +694,28 @@ int piBoardRev (void) if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) piBoardRevOops ("Unable to open /proc/cpuinfo") ; -// Start by looking for the Architecture, then we can look for a B2 revision.... +// Start by looking for the Architecture to make sure we're really running +// on a Pi. I'm getting fed-up with people whinging at me because +// they can't get it to work on weirdFruitPi boards... while (fgets (line, 120, cpuFd) != NULL) if (strncmp (line, "Hardware", 8) == 0) break ; if (strncmp (line, "Hardware", 8) != 0) - piBoardRevOops ("No \"Hardware\" line") ; + piBoardRevOops ("No hardware line") ; if (wiringPiDebug) printf ("piboardRev: Hardware: %s\n", line) ; // See if it's BCM2708 or BCM2709 - if (strstr (line, "BCM2709") != NULL) + if (strstr (line, "BCM2709") != NULL) // Pi v2 - no point doing anything more at this point + { piModel2 = TRUE ; + fclose (cpuFd) ; + return boardRev = 2 ; + } else if (strstr (line, "BCM2708") == NULL) { fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ; @@ -702,10 +727,12 @@ int piBoardRev (void) exit (EXIT_FAILURE) ; } -// Now do the rest of it as before +// Now do the rest of it as before - we just need to see if it's an older +// Rev 1 as anything else is rev 2. + +// Isolate the Revision line rewind (cpuFd) ; - while (fgets (line, 120, cpuFd) != NULL) if (strncmp (line, "Revision", 8) == 0) break ; @@ -723,28 +750,43 @@ int piBoardRev (void) if (wiringPiDebug) printf ("piboardRev: Revision string: %s\n", line) ; -// Scan to first digit +// Scan to the first character of the revision number for (c = line ; *c ; ++c) - if (isdigit (*c)) + if (*c == ':') break ; - if (!isdigit (*c)) - piBoardRevOops ("No numeric revision string") ; + if (*c != ':') + piBoardRevOops ("Bogus \"Revision\" line (no colon)") ; + +// Chomp spaces + + ++c ; + while (isspace (*c)) + ++c ; + + if (!isxdigit (*c)) + piBoardRevOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ; // Make sure its long enough if (strlen (c) < 4) - piBoardRevOops ("Bogus \"Revision\" line (too small)") ; - + piBoardRevOops ("Bogus revision line (too small)") ; + // If you have overvolted the Pi, then it appears that the revision // has 100000 added to it! // The actual condition for it being set is: // (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0 + +// This test is not correct for the new encoding scheme, so we'll remove it here as +// we don't really need it at this point. + +/******************** if (wiringPiDebug) if (strlen (c) != 4) printf ("piboardRev: This Pi has/is (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0\n") ; +*******************/ // Isolate last 4 characters: @@ -767,12 +809,49 @@ int piBoardRev (void) /* * piBoardId: - * Do more digging into the board revision string as above, but return - * as much details as we can. + * Return the real details of the board we have. + * * This is undocumented and really only intended for the GPIO command. * Use at your own risk! * - * for Pi v2: + * Seems there are some boards with 0000 in them (mistake in manufacture) + * So the distinction between boards that I can see is: + * + * 0000 - Error + * 0001 - Not used + * + * Original Pi boards: + * 0002 - Model B, Rev 1, 256MB, Egoman + * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed. + * + * Newer Pi's with remapped GPIO: + * 0004 - Model B, Rev 2, 256MB, Sony + * 0005 - Model B, Rev 2, 256MB, Qisda + * 0006 - Model B, Rev 2, 256MB, Egoman + * 0007 - Model A, Rev 2, 256MB, Egoman + * 0008 - Model A, Rev 2, 256MB, Sony + * 0009 - Model A, Rev 2, 256MB, Qisda + * 000d - Model B, Rev 2, 512MB, Egoman (Red Pi, Blue Pi?) + * 000e - Model B, Rev 2, 512MB, Sony + * 000f - Model B, Rev 2, 512MB, Qisda + * 0010 - Model B+, Rev 1.2, 512MB, Sony + * 0011 - Pi CM, Rev 1.2, 512MB, Sony + * 0012 - Model A+ Rev 1.2, 256MB, Sony + * 0014 - Pi CM, Rev 1.1, 512MB, Sony (Actual Revision might be different) + * 0015 - Model A+ Rev 1.1, 256MB, Sony + * + * A small thorn is the olde style overvolting - that will add in + * 1000000 + * + * The Pi compute module has an revision of 0011 or 0014 - since we only + * check the last digit, then it's 1, therefore it'll default to not 2 or + * 3 for a Rev 1, so will appear as a Rev 2. This is fine for the most part, but + * we'll properly detect the Compute Module later and adjust accordingly. + * + * And then things changed with the introduction of the v2... + * + * For Pi v2 and subsequent models - e.g. the Zero: + * * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4] * NEW 23: will be 1 for the new scheme, 0 for the old scheme * MEMSIZE 20: 0=256M 1=512M 2=1G @@ -783,11 +862,13 @@ int piBoardRev (void) ********************************************************************************* */ -void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) +void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty) { FILE *cpuFd ; char line [120] ; char *c ; + unsigned int revision ; + int bRev, bType, bProc, bMfg, bMem, bWarranty ; // Will deal with the properly later on - for now, lets just get it going... // unsigned int modelNum ; @@ -814,33 +895,59 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) if (wiringPiDebug) printf ("piboardId: Revision string: %s\n", line) ; - if (piModel2) +// Need to work out if it's using the new or old encoding scheme: + +// Scan to the first character of the revision number + + for (c = line ; *c ; ++c) + if (*c == ':') + break ; + + if (*c != ':') + piBoardRevOops ("Bogus \"Revision\" line (no colon)") ; + +// Chomp spaces + + ++c ; + while (isspace (*c)) + ++c ; + + if (!isxdigit (*c)) + piBoardRevOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ; + + revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x + +// Check for new way: + + if ((revision & (1 << 23)) != 0) // New way { + if (wiringPiDebug) + printf ("piBoardId: New Way: revision is: 0x%08X\n", revision) ; -// Scan to the colon - - for (c = line ; *c ; ++c) - if (*c == ':') - break ; - - if (*c != ':') - piBoardRevOops ("Bogus \"Revision\" line (no colon)") ; - -// modelNum = (unsigned int)strtol (++c, NULL, 16) ; // Hex number with no leading 0x + bRev = (revision & (0x0F << 0)) >> 0 ; + bType = (revision & (0xFF << 4)) >> 4 ; + bProc = (revision & (0x0F << 12)) >> 12 ; // Not used for now. + bMfg = (revision & (0x0F << 16)) >> 16 ; + bMem = (revision & (0x07 << 20)) >> 20 ; + bWarranty = (revision & (0x03 << 24)) != 0 ; - *model = PI_MODEL_2 ; - *rev = PI_VERSION_1_1 ; - *mem = 1024 ; - *maker = PI_MAKER_SONY ; + *model = bType ; + *rev = bRev ; + *mem = bMem ; + *maker = bMfg ; + *warranty = bWarranty ; + + if (wiringPiDebug) + printf ("piboardId: rev: %d, type: %d, proc: %d, mfg: %d, mem: %d, warranty: %d\n", + bRev, bType, bProc, bMfg, bMem, bWarranty) ; } - else + else // Old way { + if (wiringPiDebug) + printf ("piBoardId: Old Way: revision is: %s\n", c) ; -// Scan to first digit - - for (c = line ; *c ; ++c) - if (isdigit (*c)) - break ; + if (!isdigit (*c)) + piBoardRevOops ("Bogus \"Revision\" line (no digit at start of revision)") ; // Make sure its long enough @@ -849,7 +956,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) // If longer than 4, we'll assume it's been overvolted - *overVolted = strlen (c) > 4 ; + *warranty = strlen (c) > 4 ; // Extract last 4 characters: @@ -857,23 +964,23 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) // Fill out the replys as appropriate - /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } - else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } - else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } - else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; } - else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } - else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; } - else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; ; } - else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; } - else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; } - else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } - else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; } - else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } - else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } - else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } - else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST ; } - else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; } - else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_SONY ; } + /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 0 ; *maker = PI_MAKER_UNKNOWN ; } + else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; ; } + else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 0 ; *maker = PI_MAKER_UNKNOWN ; } + else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; } + else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; } + else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; } else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; } } } diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index dd110fa..e09d06a 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -26,8 +26,7 @@ // Handy defines -// Deprecated -#define NUM_PINS 17 +// wiringPi modes #define WPI_MODE_PINS 0 #define WPI_MODE_GPIO 1 @@ -70,29 +69,28 @@ // Pi model types and version numbers // Intended for the GPIO program Use at your own risk. -#define PI_MODEL_UNKNOWN 0 -#define PI_MODEL_A 1 -#define PI_MODEL_B 2 +#define PI_MODEL_A 0 +#define PI_MODEL_B 1 +#define PI_MODEL_AP 2 #define PI_MODEL_BP 3 -#define PI_MODEL_CM 4 -#define PI_MODEL_AP 5 -#define PI_MODEL_2 6 +#define PI_MODEL_2 4 +#define PI_ALPHA 5 +#define PI_MODEL_CM 6 -#define PI_VERSION_UNKNOWN 0 -#define PI_VERSION_1 1 -#define PI_VERSION_1_1 2 -#define PI_VERSION_1_2 3 -#define PI_VERSION_2 4 +#define PI_VERSION_1 0 +#define PI_VERSION_1_1 1 +#define PI_VERSION_1_2 2 +#define PI_VERSION_2 3 -#define PI_MAKER_UNKNOWN 0 +#define PI_MAKER_SONY 0 #define PI_MAKER_EGOMAN 1 -#define PI_MAKER_SONY 2 -#define PI_MAKER_QISDA 3 -#define PI_MAKER_MBEST 4 +#define PI_MAKER_MBEST 2 +#define PI_MAKER_UNKNOWN 3 -extern const char *piModelNames [7] ; -extern const char *piRevisionNames [5] ; -extern const char *piMakerNames [5] ; +extern const char *piModelNames [16] ; +extern const char *piRevisionNames [16] ; +extern const char *piMakerNames [16] ; +extern const int piMemorySize [ 8] ; // Intended for the GPIO program Use at your own risk. From 31a8a2cdb89af2c090e8925ad1716b46425c21d3 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Wed, 2 Dec 2015 15:13:01 +0000 Subject: [PATCH 93/97] gpio readall didn't work on the Zero. This fixed that little goof --- gpio/readall.c | 4 +++- wiringPi/wiringPi.h | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/gpio/readall.c b/gpio/readall.c index cf9b5f0..2d90cd1 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -289,6 +289,8 @@ static void plus2header (int model) printf (" +-----+-----+---------+------+---+--A Plus--+---+------+---------+-----+-----+\n") ; else if (model == PI_MODEL_BP) printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ; + else if (model == PI_MODEL_ZERO) + printf (" +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+\n") ; else printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ; } @@ -325,7 +327,7 @@ void doReadall (void) /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B)) abReadall (model, rev) ; - else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2)) + else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2) || (model == PI_MODEL_ZERO)) piPlusReadall (model) ; else if (model == PI_MODEL_CM) cmReadall () ; diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index e09d06a..7b9605f 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -76,6 +76,9 @@ #define PI_MODEL_2 4 #define PI_ALPHA 5 #define PI_MODEL_CM 6 +#define PI_MODEL_07 7 +#define PI_MODEL_08 8 +#define PI_MODEL_ZERO 9 #define PI_VERSION_1 0 #define PI_VERSION_1_1 1 From 78b5c323b74de782df58ee558c249e4e4fadd25f Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Wed, 2 Dec 2015 15:13:50 +0000 Subject: [PATCH 94/97] Bumped Version to 2.31 --- VERSION | 2 +- debian-template/wiringPi/DEBIAN/control | 2 +- gpio/version.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index 81b133a..3125d73 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.30 +2.31 diff --git a/debian-template/wiringPi/DEBIAN/control b/debian-template/wiringPi/DEBIAN/control index 7b739c6..5d698c8 100644 --- a/debian-template/wiringPi/DEBIAN/control +++ b/debian-template/wiringPi/DEBIAN/control @@ -1,5 +1,5 @@ Package: wiringpi -Version: 2.30 +Version: 2.31 Section: libraries Priority: optional Architecture: armhf diff --git a/gpio/version.h b/gpio/version.h index a46348a..290ab36 100644 --- a/gpio/version.h +++ b/gpio/version.h @@ -1 +1 @@ -#define VERSION "2.30" +#define VERSION "2.31" From 143b383b8da7d6aed23dd8644f2b9a07a695a4e1 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 28 Jan 2016 19:20:31 +0000 Subject: [PATCH 95/97] Added support for the Pimoroni scrollPhat Added support for the ADS1115 16-bit ADC Updated the gpio readall command to correctly with with the Compute Module and fixed a resulting bug in wiringPi... --- devLib/Makefile | 5 +- devLib/scrollPhat.c | 430 +++++++++++++++++++++++++++ devLib/scrollPhat.h | 39 +++ devLib/scrollPhatFont.h | 544 +++++++++++++++++++++++++++++++++++ examples/scrollPhat/Makefile | 79 +++++ examples/scrollPhat/scphat.c | 230 +++++++++++++++ examples/scrollPhat/test.c | 115 ++++++++ gpio/readall.c | 9 +- wiringPi/Makefile | 4 +- wiringPi/ads1115.c | 293 +++++++++++++++++++ wiringPi/ads1115.h | 55 ++++ wiringPi/wiringPi.c | 24 +- wiringPi/wpiExtensions.c | 28 ++ 13 files changed, 1847 insertions(+), 8 deletions(-) create mode 100644 devLib/scrollPhat.c create mode 100644 devLib/scrollPhat.h create mode 100644 devLib/scrollPhatFont.h create mode 100644 examples/scrollPhat/Makefile create mode 100644 examples/scrollPhat/scphat.c create mode 100644 examples/scrollPhat/test.c create mode 100644 wiringPi/ads1115.c create mode 100644 wiringPi/ads1115.h diff --git a/devLib/Makefile b/devLib/Makefile index 0fb0033..1b1ebe0 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -48,11 +48,13 @@ LIBS = SRC = ds1302.c maxdetect.c piNes.c \ gertboard.c piFace.c \ lcd128x64.c lcd.c \ + scrollPhat.c \ piGlow.c OBJ = $(SRC:.c=.o) -HEADERS = ds1302.h gertboard.h lcd128x64.h lcd.h maxdetect.h piFace.h piGlow.h piNes.h +HEADERS = ds1302.h gertboard.h lcd128x64.h lcd.h maxdetect.h piFace.h piGlow.h piNes.h\ + scrollPhat.h all: $(DYNAMIC) @@ -134,4 +136,5 @@ gertboard.o: gertboard.h piFace.o: piFace.h lcd128x64.o: font.h lcd128x64.h lcd.o: lcd.h +scrollPhat.o: scrollPhatFont.h scrollPhat.h piGlow.o: piGlow.h diff --git a/devLib/scrollPhat.c b/devLib/scrollPhat.c new file mode 100644 index 0000000..c1a6f11 --- /dev/null +++ b/devLib/scrollPhat.c @@ -0,0 +1,430 @@ +/* + * scrollPhat.c: + * Simple driver for the Pimoroni Scroll Phat device + * + * Copyright (c) 2015 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 +#include +#include + +#include + +#include "scrollPhatFont.h" +#include "scrollPhat.h" + +// Size + +#define SP_WIDTH 11 +#define SP_HEIGHT 5 + +// I2C + +#define PHAT_I2C_ADDR 0x60 + +// Software copy of the framebuffer +// it's 8-bit deep although the display itself is only 1-bit deep. + +static unsigned char frameBuffer [SP_WIDTH * SP_HEIGHT] ; + +static int lastX, lastY ; +static int printDelayFactor ; +static int scrollPhatFd ; + +static int putcharX ; + +#undef DEBUG + + +/* + * delay: + * Wait for some number of milliseconds. + * This taken from wiringPi as there is no-need to include the whole of + * wiringPi just for the delay function. + ********************************************************************************* + */ + +static void delay (unsigned int howLong) +{ + struct timespec sleeper, dummy ; + + sleeper.tv_sec = (time_t)(howLong / 1000) ; + sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ; + + nanosleep (&sleeper, &dummy) ; +} + + + +/* + * scrollPhatUpdate: + * Copy our software version to the real display + ********************************************************************************* + */ + +void scrollPhatUpdate (void) +{ + register int x, y ; + register unsigned char data, pixel ; + unsigned char pixels [SP_WIDTH] ; + +#ifdef DEBUG + printf ("+-----------+\n") ; + for (y = 0 ; y < SP_HEIGHT ; ++y) + { + putchar ('|') ; + for (x = 0 ; x < SP_WIDTH ; ++x) + { + pixel = frameBuffer [x + y * SP_WIDTH] ; + putchar (pixel == 0 ? ' ' : '*') ; + } + printf ("|\n") ; + } + printf ("+-----------+\n") ; +#endif + + for (x = 0 ; x < SP_WIDTH ; ++x) + { + data = 0 ; + for (y = 0 ; y < SP_HEIGHT ; ++y) + { + pixel = frameBuffer [x + y * SP_WIDTH] ; + data = (data << 1) | ((pixel == 0) ? 0 : 1) ; + } + pixels [x] = data ; + } + + for (x = 0 ; x < SP_WIDTH ; ++x) + wiringPiI2CWriteReg8 (scrollPhatFd, 1 + x, pixels [x]) ; + + wiringPiI2CWriteReg8 (scrollPhatFd, 0x0C, 0) ; +} + + +/* + ********************************************************************************* + * Standard Graphical Functions + ********************************************************************************* + */ + + +/* + * scrollPhatPoint: + * Plot a pixel. Crude clipping - speed is not the essence here. + ********************************************************************************* + */ + +void scrollPhatPoint (int x, int y, int colour) +{ + lastX = x ; + lastY = y ; + + if ((x < 0) || (x >= SP_WIDTH) || (y < 0) || (y >= SP_HEIGHT)) + return ; + + frameBuffer [x + y * SP_WIDTH] = colour ; +} + + +/* + * scrollPhatLine: scrollPhatLineTo: + * Classic Bressenham Line code - rely on the point function to do the + * clipping for us here. + ********************************************************************************* + */ + +void scrollPhatLine (int x0, int y0, int x1, int y1, int colour) +{ + int dx, dy ; + int sx, sy ; + int err, e2 ; + + lastX = x1 ; + lastY = y1 ; + + dx = abs (x1 - x0) ; + dy = abs (y1 - y0) ; + + sx = (x0 < x1) ? 1 : -1 ; + sy = (y0 < y1) ? 1 : -1 ; + + err = dx - dy ; + + for (;;) + { + scrollPhatPoint (x0, y0, colour) ; + + if ((x0 == x1) && (y0 == y1)) + break ; + + e2 = 2 * err ; + + if (e2 > -dy) + { + err -= dy ; + x0 += sx ; + } + + if (e2 < dx) + { + err += dx ; + y0 += sy ; + } + } + +} + +void scrollPhatLineTo (int x, int y, int colour) +{ + scrollPhatLine (lastX, lastY, x, y, colour) ; +} + + +/* + * scrollPhatRectangle: + * A rectangle is a spoilt days fishing + ********************************************************************************* + */ + +void scrollPhatRectangle (int x1, int y1, int x2, int y2, int colour, int filled) +{ + register int x ; + + if (filled) + { + /**/ if (x1 == x2) + scrollPhatLine (x1, y1, x2, y2, colour) ; + else if (x1 < x2) + for (x = x1 ; x <= x2 ; ++x) + scrollPhatLine (x, y1, x, y2, colour) ; + else + for (x = x2 ; x <= x1 ; ++x) + scrollPhatLine (x, y1, x, y2, colour) ; + } + else + { + scrollPhatLine (x1, y1, x2, y1, colour) ; + scrollPhatLineTo (x2, y2, colour) ; + scrollPhatLineTo (x1, y2, colour) ; + scrollPhatLineTo (x1, y1, colour) ; + } +} + + +/* + * scrollPhatPutchar: + * Print a single character to the screen then advance the pointer by an + * appropriate ammount (variable width font). + * We rely on the clipping done by the pixel plot function to keep us + * out of trouble. + * Return the width + space + ********************************************************************************* + */ + +int scrollPhatPutchar (int c) +{ + register int x, y ; + + unsigned char line ; + unsigned char *fontPtr ; + unsigned char *p2 ; + int lineWidth, width, mask ; + +// The font is printable characters, uppercase only... +// and somewhat varaible width... + + c &= 0x7F ; + if (c > 0x60) + c -= 64 ; + else + c -= 32 ; + + fontPtr = scrollPhatFont + c * fontHeight ; + +// Work out width of this character +// There probably is a more efficient way to do this, but... + + p2 = fontPtr ; + width = 0 ; + for (y = 0 ; y < fontHeight ; ++y) + { + mask = 0x80 ; + for (lineWidth = 8 ; lineWidth > 0 ; --lineWidth) + { + if ((*p2 & mask) != 0) + break ; + mask >>= 1 ; + } + if (lineWidth > width) + width = lineWidth ; + + ++p2 ; + } + + if (width == 0) // Likely to be a blank or space character + width = 3 ; + + for (y = fontHeight - 1 ; y >= 0 ; --y) + { + x = 0 ; + line = *fontPtr++ ; + for (mask = 1 << (width - 1) ; mask != 0 ; mask >>= 1) + { + scrollPhatPoint (putcharX + x, y, (line & mask)) ; + ++x ; + } + } + +// make a line of space + + for (y = fontHeight - 1 ; y >= 0 ; --y) + scrollPhatPoint (putcharX + width, y, 0) ; + + putcharX = putcharX + width + 1 ; + + return width + 1 ; +} + + +/* + * scrollPhatPuts: + * Send a string to the display - and scroll it across. + * This is somewhat of a hack in that we print the entire string to the + * display and let the point clipping take care of what's off-screen... + ********************************************************************************* + */ + +void scrollPhatPuts (const char *str) +{ + int i ; + int movingX = 0 ; + const char *s ; + int pixelLen ; + +// Print it once, then we know the width in pixels... + + putcharX = 0 ; + s = str ; + while (*s) + scrollPhatPutchar (*s++) ; + + pixelLen = putcharX ; + +// Now scroll it by printing it and moving left one pixel + + movingX = 0 ; + for (i = 0 ; i < pixelLen ; ++i) + { + putcharX = movingX ; + s = str ; + while (*s) + scrollPhatPutchar (*s++) ; + --movingX ; + scrollPhatUpdate () ; + delay (printDelayFactor) ; + } +} + + +/* + * scrollPhatPrintf: + * Does what it says + ********************************************************************************* + */ + +void scrollPhatPrintf (const char *message, ...) +{ + va_list argp ; + char buffer [1024] ; + + va_start (argp, message) ; + vsnprintf (buffer, 1023, message, argp) ; + va_end (argp) ; + + scrollPhatPuts (buffer) ; +} + + +/* + * scrollPhatPrintSpeed: + * Change the print speed - mS per shift by 1 pixel + ********************************************************************************* + */ + +void scrollPhatPrintSpeed (const int pps) +{ + if (pps < 0) + printDelayFactor = 0 ; + else + printDelayFactor = pps ; +} + + +/* + * scrollPhatClear: + * Clear the display + ********************************************************************************* + */ + +void scrollPhatClear (void) +{ + register int i ; + register unsigned char *ptr = frameBuffer ; + + for (i = 0 ; i < (SP_WIDTH * SP_HEIGHT) ; ++i) + *ptr++ = 0 ; + + scrollPhatUpdate () ; +} + + +/* + * scrollPhatIntensity: + * Set the display brightness - percentage + ********************************************************************************* + */ + +void scrollPhatIntensity (const int percent) +{ + wiringPiI2CWriteReg8 (scrollPhatFd, 0x19, (127 * percent) / 100) ; +} + + +/* + * scrollPhatSetup: + * Initialise the Scroll Phat display + ********************************************************************************* + */ + +int scrollPhatSetup (void) +{ + if ((scrollPhatFd = wiringPiI2CSetup (PHAT_I2C_ADDR)) < 0) + return scrollPhatFd ; + + wiringPiI2CWriteReg8 (scrollPhatFd, 0x00, 0x03) ; // Enable display, set to 5x11 mode + scrollPhatIntensity (10) ; + scrollPhatClear () ; + scrollPhatPrintSpeed (100) ; + + return 0 ; +} diff --git a/devLib/scrollPhat.h b/devLib/scrollPhat.h new file mode 100644 index 0000000..0e762b1 --- /dev/null +++ b/devLib/scrollPhat.h @@ -0,0 +1,39 @@ +/* + * scrollPhat.h: + * Simple driver for the Pimoroni Scroll Phat device + * + * Copyright (c) 2015 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 . + *********************************************************************** + */ + +extern void scrollPhatPoint (int x, int y, int colour) ; +extern void scrollPhatLine (int x0, int y0, int x1, int y1, int colour) ; +extern void scrollPhatLineTo (int x, int y, int colour) ; +extern void scrollPhatRectangle (int x1, int y1, int x2, int y2, int colour, int filled) ; +extern void scrollPhatUpdate (void) ; +extern void scrollPhatClear (void) ; + +extern int scrollPhatPutchar (int c) ; +//extern void scrollPhatPutchar (int c) ; +extern void scrollPhatPuts (const char *str) ; +extern void scrollPhatPrintf (const char *message, ...) ; +extern void scrollPhatPrintSpeed (const int cps10) ; + +extern void scrollPhatIntensity (const int percent) ; +extern int scrollPhatSetup (void) ; diff --git a/devLib/scrollPhatFont.h b/devLib/scrollPhatFont.h new file mode 100644 index 0000000..35481f8 --- /dev/null +++ b/devLib/scrollPhatFont.h @@ -0,0 +1,544 @@ +/* + * scrollPhatFont.h: + * Simple font for the Pimoroni Scroll Phat. + * Note: this is a very much reduced font - 5 pixels high and + * mostly 4 pixels wide - sometimes 5. Also only + * printable characters from space to _ uppercase only. + * + * Copyright (c) 2015-2016 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 . + *********************************************************************** + */ + +static const int fontHeight = 5 ; + +static unsigned char scrollPhatFont [] = +{ + +// 0x20, Space. Handeled as a special case in the code. + + 0x0, // .... + 0x0, // .... + 0x0, // .... + 0x0, // .... + 0x0, // .... + +// 0x21, ! + + 0x1, // * + 0x1, // * + 0x1, // * + 0x0, // . + 0x1, // * + +// 0x22, " + + 0x5, // *..* + 0x5, // *..* + 0x0, // .... + 0x0, // .... + 0x0, // .... + +// 0x23, # + + 0x9, // *..* + 0xF, // **** + 0x9, // *..* + 0xF, // **** + 0x9, // *..* + +// 0x24, $ + + 0x1, // ...* + 0x7, // .*** + 0x2, // ..*. + 0xE, // ***. + 0x8, // *... + +// 0x25, % + + 0x9, // *..* + 0x1, // ...* + 0x6, // .**. + 0x8, // *... + 0x9, // *..* + +// 0x26, & + + 0x6, // .**. + 0x8, // *... + 0x4, // .*.. + 0xA, // *.*. + 0x5, // .*.* + +// 0x27, ' + + 0x1, // .* + 0x2, // *. + 0x0, // .. + 0x0, // .. + 0x0, // .. + +// 0x28, ( + + 0x3, // ..** + 0x4, // .*.. + 0x8, // *... + 0x4, // .*.. + 0x3, // ..** + +// 0x29, ) + + 0xC, // **.. + 0x2, // ..*. + 0x1, // ...* + 0x2, // ..*. + 0xC, // **.. + +// 0x2A, * + + 0x9, // *..* + 0x6, // .**. + 0xF, // **** + 0x6, // .**. + 0x9, // *..* + +// 0x2B, + + + 0x6, // .**. + 0x6, // .**. + 0xF, // **** + 0x6, // .**. + 0x6, // .**. + +// 0x2C, , + + 0x0, // .. + 0x0, // .. + 0x0, // .. + 0x1, // .* + 0x2, // *. + +// 0x2D, - + + 0x0, // .... + 0x0, // .... + 0xF, // **** + 0x0, // .... + 0x0, // .... + +// 0x2E, . + + 0x0, // . + 0x0, // . + 0x0, // . + 0x0, // . + 0x1, // * + +// 0x2F, / + + 0x1, // ...* + 0x3, // ..** + 0x4, // ..*. + 0xC, // **.. + 0x8, // *... + +// 0x30, 0 + + 0x6, // .**. + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0x6, // .**. + +// 0x31, 1 + + 0x2, // ..*. + 0x6, // .**. + 0x2, // ..*. + 0x2, // ..*. + 0x7, // .*** + +// 0x32, 2 + + 0x6, // .**. + 0x1, // ...* + 0x6, // .**. + 0x8, // *... + 0xF, // **** + +// 0x33, 3 + + 0xE, // ***. + 0x1, // ...* + 0xE, // ***. + 0x1, // ...* + 0xE, // ***. + +// 0x34, 4 + + 0x6, // .**. + 0xA, // *.*. + 0xF, // **** + 0x2, // ..*. + 0x2, // ..*. + +// 0x35, 5 + + 0xF, // **** + 0x8, // *... + 0xF, // **** + 0x1, // ...* + 0xE, // ***. + +// 0x36, 6 + + 0x2, // ..*. + 0x4, // .*.. + 0xA, // *.*. + 0x9, // *..* + 0x6, // .**. + +// 0x37, 7 + + 0xF, // **** + 0x1, // ...* + 0x2, // ..*. + 0x4, // .*.. + 0x8, // *... + +// 0x38, 8 + + 0x6, // .**. + 0x9, // *..* + 0x6, // .**. + 0x9, // *..* + 0x6, // .**. + +// 0x39, 9 + + 0x6, // .**. + 0x9, // *..* + 0x7, // .*.* + 0x1, // ..*. + 0x2, // .*.. + +// 0x3A, : + + 0x0, // . + 0x1, // * + 0x0, // . + 0x1, // * + 0x0, // . + +// 0x3B, ; + + 0x0, // .. + 0x1, // .* + 0x0, // .. + 0x1, // .* + 0x2, // *. + +// 0x3C, < + + 0x2, // ..*. + 0x4, // .*.. + 0x8, // *... + 0x4, // .*.. + 0x2, // ..*. + +// 0x3D, = + + 0x0, // .... + 0xF, // **** + 0x0, // .... + 0xF, // **** + 0x0, // .... + +// 0x3E, > + + 0x0, // .*.. + 0x0, // ..*. + 0x0, // ...* + 0x0, // ..*. + 0x0, // .*.. + +// 0x3F, ? + + 0x6, // .**. + 0x1, // ...* + 0x2, // ..*. + 0x0, // .... + 0x2, // ..*. + +// 0x40, @ + + 0x6, // .**. + 0xD, // **.* + 0x8, // *... + 0x4, // .*.. + 0x3, // ..** + +// 0x41, A + + 0x6, // .**. + 0x9, // *..* + 0xF, // **** + 0x9, // *..* + 0x9, // *..* + +// 0x42, B + + 0xE, // ***. + 0x9, // *..* + 0xE, // ***. + 0x9, // *..* + 0xE, // ***. + +// 0x43, C + + 0x6, // .**. + 0x9, // *..* + 0x8, // *... + 0x9, // *..* + 0x6, // .**. + +// 0x44, D + + 0xE, // ***. + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0xE, // ***. + +// 0x45, E + + 0xF, // **** + 0x8, // *... + 0xE, // ***. + 0x8, // *... + 0xF, // **** + +// 0x46, F + + 0xF, // **** + 0x8, // *... + 0xE, // ***. + 0x8, // *... + 0x8, // *... + +// 0x47, G + + 0x6, // .**. + 0x9, // *..* + 0x8, // *... + 0xB, // *.** + 0x6, // .**. + +// 0x48, H + + 0x9, // *..* + 0x9, // *..* + 0xF, // **** + 0x9, // *..* + 0x9, // *..* + +// 0x49, I + + 0x7, // *** + 0x2, // .*. + 0x2, // .*. + 0x2, // .*. + 0x7, // *** + +// 0x4A, J + + 0x7, // .*** + 0x2, // ..*. + 0x2, // ..*. + 0xA, // *.*. + 0x4, // .*.. + +// 0x4B, K + + 0x9, // *..* + 0xA, // *.*. + 0xC, // **.. + 0xA, // *.*. + 0x9, // *..* + +// 0x4C, L + + 0x4, // *.. + 0x4, // *.. + 0x4, // *.. + 0x4, // *.. + 0x7, // *** + +// 0x4D, M + + 0x11, // *...* + 0x1B, // **.** + 0x15, // *.*.* + 0x11, // *...* + 0x11, // *...* + +// 0x4E, N + + 0x9, // *..* + 0xD, // **.* + 0xB, // *.** + 0x9, // *..* + 0x9, // *..* + +// 0x4F, O + + 0x6, // .**. + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0x6, // .**. + +// 0x50, P + + 0xE, // ***. + 0x9, // *..* + 0xE, // ***. + 0x8, // *... + 0x8, // *... + +// 0x51, Q + + 0x6, // .**. + 0x9, // *..* + 0x9, // *..* + 0xA, // *.*. + 0x5, // .*.* + +// 0x52, R + + 0xE, // ***. + 0x9, // *..* + 0xF, // ***. + 0xA, // *.*. + 0x9, // *..* + +// 0x53, S + + 0x6, // .**. + 0x8, // *... + 0x6, // .**. + 0x1, // ...* + 0x6, // .**. + +// 0x54, T + + 0x7, // .*** + 0x2, // ..*. + 0x2, // ..*. + 0x2, // ..*. + 0x2, // ..*. + +// 0x55, U + + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0x9, // *..* + 0x6, // .**. + +// 0x56, V + + 0x11, // *...* + 0x11, // *...* + 0x11, // *...* + 0x0A, // .*.*. + 0x04, // ..*.. + +// 0x57, W + + 0x11, // *...* + 0x11, // *...* + 0x11, // *...* + 0x15, // *.*.* + 0x1B, // **.** + +// 0x58, X + + 0x9, // *..* + 0x9, // *..* + 0x6, // .**. + 0x9, // *..* + 0x9, // *..* + +// 0x59, Y + + 0x11, // *...* + 0x0A, // .*.*. + 0x04, // ..*.. + 0x04, // ..*.. + 0x04, // ..*.. + +// 0x5A, Z + + 0xF, // **** + 0x1, // ...* + 0x6, // .**. + 0x8, // *... + 0xF, // **** + +// 0x5B, [ + + 0xE, // ***. + 0x8, // *... + 0x8, // *... + 0x8, // *... + 0xE, // ***. + +// 0x5C, Backslash + + 0x8, // *... + 0xC, // **.. + 0x6, // .**. + 0x3, // ..** + 0x1, // ...* + +// 0x5D, ] + + 0x7, // .*** + 0x1, // ...* + 0x1, // ...* + 0x1, // ...* + 0x7, // .*** + +// 0x5E, ^ + + 0x6, // .**. + 0x9, // *..* + 0x0, // .... + 0x0, // .... + 0x0, // .... + +// 0x5F, _ + + 0x0, // .... + 0x0, // .... + 0x0, // .... + 0x0, // .... + 0xF, // **** +} ; diff --git a/examples/scrollPhat/Makefile b/examples/scrollPhat/Makefile new file mode 100644 index 0000000..c0b7241 --- /dev/null +++ b/examples/scrollPhat/Makefile @@ -0,0 +1,79 @@ +# +# Makefile: +# wiringPi - Wiring Compatable library for the Raspberry Pi +# https://projects.drogon.net/wiring-pi +# +# Copyright (c) 2012-2015 Gordon Henderson +################################################################################# +# This file is part of wiringPi: +# 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 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 . +################################################################################# + +ifneq ($V,1) +Q ?= @ +endif + +#DEBUG = -g -O0 +DEBUG = -O3 +CC = gcc +INCLUDE = -I/usr/local/include +CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe + +LDFLAGS = -L/usr/local/lib +LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm + +# Should not alter anything below this line +############################################################################### + +SRC = scphat.c test.c + +OBJ = $(SRC:.c=.o) + +BINS = $(SRC:.c=) + +all: $(BINS) + +test: test.o + $Q echo [link] + $Q $(CC) -o $@ test.o $(LDFLAGS) $(LDLIBS) + +scphat: scphat.o + $Q echo [link] + $Q $(CC) -o $@ scphat.o $(LDFLAGS) $(LDLIBS) + + +.c.o: + $Q echo [CC] $< + $Q $(CC) -c $(CFLAGS) $< -o $@ + +clean: + $Q echo "[Clean]" + $Q rm -f $(OBJ) *~ core tags $(BINS) + +tags: $(SRC) + $Q echo [ctags] + $Q ctags $(SRC) + +install: scphat + $Q echo Installing scphat into /usr/local/bin + $Q cp -a scphat /usr/local/bin/scphat + $Q chmod 755 /usr/local/bin/scphat + $Q echo Done. Remember to load the I2C drivers if needed. + +depend: + makedepend -Y $(SRC) + +# DO NOT DELETE diff --git a/examples/scrollPhat/scphat.c b/examples/scrollPhat/scphat.c new file mode 100644 index 0000000..8f90bad --- /dev/null +++ b/examples/scrollPhat/scphat.c @@ -0,0 +1,230 @@ +/* + * scphat.c: + * Little program to allow use of the Pimoroni Sctoll Phat + * from the command-line. + * + * Copyright (c) 2015-2016 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 +#include + +#include +#include + +static char *progName ; + + +/* + * checkArgs: + * Count the arguments for each little function + ********************************************************************************* + */ + +static void checkArgs (char *command, int num, int arg, int argc) +{ + if ((arg + num) < argc) + return ; + + fprintf (stderr, "%s: Not enough data for %s command.\n", progName, command) ; + exit (EXIT_FAILURE) ; +} + + +/* + * doClear: + * Clear the display + ********************************************************************************* + */ + +static int doClear (void) +{ + scrollPhatClear () ; + return 1 ; +} + + +/* + * doBright + ********************************************************************************* + */ + +static int doBright (int arg, int argc, char *argv []) +{ + checkArgs ("bright", 1, arg, argc) ; + scrollPhatIntensity (atoi (argv [arg+1])) ; + return 2 ; +} + + + +/* + * doPlot + ********************************************************************************* + */ + +static int doPlot (int arg, int argc, char *argv []) +{ + checkArgs ("plot", 2, arg, argc) ; + scrollPhatPoint (atoi (argv [arg+1]), atoi (argv [arg+2]), 1) ; + scrollPhatUpdate () ; + return 3 ; +} + + +/* + * doLine + ********************************************************************************* + */ + +static int doLine (int arg, int argc, char *argv []) +{ + checkArgs ("line", 4, arg, argc) ; + scrollPhatLine (atoi (argv [arg+1]), atoi (argv [arg+2]), + atoi (argv [arg+3]), atoi (argv [arg+4]), 1) ; + scrollPhatUpdate () ; + return 5 ; +} + + +/* + * doLineTo + ********************************************************************************* + */ + +static int doLineTo (int arg, int argc, char *argv []) +{ + checkArgs ("lineto", 2, arg, argc) ; + scrollPhatLineTo (atoi (argv [arg+1]), atoi (argv [arg+2]), 1) ; + scrollPhatUpdate () ; + return 3 ; +} + + +/* + * doWait + ********************************************************************************* + */ + +static int doWait (int arg, int argc, char *argv []) +{ + checkArgs ("wait", 1, arg, argc) ; + delay (atoi (argv [arg+1]) * 100) ; + scrollPhatUpdate () ; + return 2 ; +} + + +/* + * doSpeed + ********************************************************************************* + */ + +static int doSpeed (int arg, int argc, char *argv []) +{ + checkArgs ("speed", 1, arg, argc) ; + scrollPhatPrintSpeed (atoi (argv [arg+1])) ; + return 2 ; +} + + +/* + * doScroll + ********************************************************************************* + */ + +static int doScroll (int arg, int argc, char *argv []) +{ + checkArgs ("scroll", 1, arg, argc) ; + scrollPhatPuts (argv [arg+1]) ; + return 2 ; +} + + +static void failUsage (void) +{ + fprintf (stderr, "Usage: %s command [paremters] ...\n", progName) ; + fprintf (stderr, " commands:\n") ; + fprintf (stderr, " clear/cls - Clear the display\n") ; + fprintf (stderr, " bright N - Set display brightness; 1-100\n") ; + fprintf (stderr, " plot X Y - Set a single pixel at location X Y; 0-10, 0-4\n") ; + fprintf (stderr, " line X1 Y1 X2 Y2 - Draw a line from the 2 points\n") ; + fprintf (stderr, " lineto X2 Y2 - Draw a line from the last point to the new one\n") ; + fprintf (stderr, " wait/delay N - Wait for N 10ths seconds\n") ; + fprintf (stderr, " speed N - Set scrolling speed (cps)\n") ; + fprintf (stderr, " scroll S - Scroll the given string\n") ; + fprintf (stderr, "\n") ; + fprintf (stderr, " Example: %s plot 0 0 wait 50 scroll \" Hello \"\n", progName) ; + exit (EXIT_FAILURE) ; +} + + +/* + * the works + ********************************************************************************* + */ + +int main (int argc, char *argv []) +{ + int arg = 1 ; + char *command ; + + progName = argv [0] ; + + wiringPiSetupSys () ; + + if (scrollPhatSetup () != 0) + { + fprintf (stderr, "%s: Unable to initialise the scrollPhat: %s\n", progName, strerror (errno)) ; + exit (EXIT_FAILURE) ; + } + + progName = argv [0] ; + + if (argc < 2) + { + fprintf (stderr, "%s: Nothing to do...\n", argv [0]) ; + failUsage () ; + } + + while (arg != argc) + { + command = argv [arg] ; + /**/ if (strcasecmp (command, "clear") == 0) arg += doClear () ; + else if (strcasecmp (command, "cls") == 0) arg += doClear () ; + else if (strcasecmp (command, "bright") == 0) arg += doBright (arg, argc, argv) ; + else if (strcasecmp (command, "plot") == 0) arg += doPlot (arg, argc, argv) ; + else if (strcasecmp (command, "line") == 0) arg += doLine (arg, argc, argv) ; + else if (strcasecmp (command, "lineto") == 0) arg += doLineTo (arg, argc, argv) ; + else if (strcasecmp (command, "wait") == 0) arg += doWait (arg, argc, argv) ; + else if (strcasecmp (command, "delay") == 0) arg += doWait (arg, argc, argv) ; + else if (strcasecmp (command, "speed") == 0) arg += doSpeed (arg, argc, argv) ; + else if (strcasecmp (command, "scroll") == 0) arg += doScroll (arg, argc, argv) ; + else + { + fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [arg]) ; + failUsage () ; + } + } + + return 0 ; +} diff --git a/examples/scrollPhat/test.c b/examples/scrollPhat/test.c new file mode 100644 index 0000000..31e4311 --- /dev/null +++ b/examples/scrollPhat/test.c @@ -0,0 +1,115 @@ +/* + * test.c: + * Little test program forthe Pimoroni Scroll Phat. + * + * Copyright (c) 2015-2016 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 +#include + +#include + + +/* + * prompt: + * Simple prompt & wait + ********************************************************************************* + */ + +static void prompt (const char *p) +{ + printf (" %s. Press ENTER: ", p) ; + (void)getchar () ; +} + + +/* + * the works + ********************************************************************************* + */ + +int main (void) +{ + int x, y ; + + printf ("\n") ; + printf ("Scroll Phat Test program\n") ; + printf ("========================\n") ; + + if (scrollPhatSetup () != 0) + { + printf ("Unable to initialise the scrollPhat: %s\n", strerror (errno)) ; + exit (1) ; + } + + printf ("-> Scroll Phat initialised OK\n") ; + printf ("... Basic display tests.\n\n") ; + + prompt ("Display ought to be blank") ; + +// Light all pixels using one point at a time + + for (y = 0 ; y < 5 ; ++y) + for (x = 0 ; x < 12 ; ++x) + scrollPhatPoint (x, y, 1) ; + scrollPhatUpdate () ; + + prompt ("Display ought to be all lit-up") ; + +// Big rectangle + + scrollPhatClear () ; + scrollPhatRectangle (0,0, 10, 4, 1, 0) ; + scrollPhatUpdate () ; + + prompt ("There should now be a rectangle round the outside") ; + + scrollPhatLine (0,0, 10,4, 1) ; + scrollPhatLine (0,4, 10,0, 1) ; + scrollPhatUpdate () ; + + prompt ("Diagonal lines") ; + + scrollPhatIntensity (1) ; + + prompt ("Minimum brightness") ; + + scrollPhatIntensity (100) ; + + prompt ("Maximum brightness") ; + + scrollPhatIntensity (10) ; + + prompt ("Default brightness") ; + + scrollPhatClear () ; + + printf (" Message Test...Press Ctrl-C to exit: ") ; + fflush (stdout) ; + + scrollPhatPrintSpeed (20) ; + for (;;) + scrollPhatPuts (" Welcome to the scroll phat from Pimoroni ") ; + + return 0 ; +} diff --git a/gpio/readall.c b/gpio/readall.c index 2d90cd1..278e6ee 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -223,19 +223,20 @@ void cmReadall (void) printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ; printf ("+-----+------+-------+ +-----+------+-------+\n") ; - for (pin = 0 ; pin < 28 ; ++pin) + for (pin = 0 ; pin < 27 ; ++pin) { printf ("| %3d ", pin) ; printf ("| %-4s ", alts [getAlt (pin)]) ; printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ; printf ("| ") ; - printf ("| %3d ", pin + 28) ; - printf ("| %-4s ", alts [getAlt (pin + 28)]) ; - printf ("| %s ", digitalRead (pin + 28) == HIGH ? "High" : "Low ") ; + printf ("| %3d ", pin + 27) ; + printf ("| %-4s ", alts [getAlt (pin + 27)]) ; + printf ("| %s ", digitalRead (pin + 27) == HIGH ? "High" : "Low ") ; printf ("|\n") ; } printf ("+-----+------+-------+ +-----+------+-------+\n") ; + } diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 6bbcc5d..acb6e58 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -55,7 +55,7 @@ SRC = wiringPi.c \ sr595.c \ pcf8574.c pcf8591.c \ mcp3002.c mcp3004.c mcp4802.c mcp3422.c \ - max31855.c max5322.c \ + max31855.c max5322.c ads1115.c \ sn3218.c \ drcSerial.c \ wpiExtensions.c @@ -69,7 +69,7 @@ HEADERS = wiringPi.h \ sr595.h \ pcf8574.h pcf8591.h \ mcp3002.h mcp3004.h mcp4802.h mcp3422.h \ - max31855.h max5322.h \ + max31855.h max5322.h ads1115.h \ sn3218.h \ drcSerial.h \ wpiExtensions.h diff --git a/wiringPi/ads1115.c b/wiringPi/ads1115.c new file mode 100644 index 0000000..62e2a7f --- /dev/null +++ b/wiringPi/ads1115.c @@ -0,0 +1,293 @@ +/* + * ads1115.c: + * Extend wiringPi with the ADS1115 I2C 16-bit ADC + * Copyright (c) 2016 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 . + *********************************************************************** + */ + +/* + ********************************************************************************* + * We're going to work in a hybrid mode to fit in with the wiringPi way of + * doing things, so there will be 4 analog pin which read the 4 single-ended + * channels as usual, also some fake digitalOutputs - these are the control + * registers that allow the user to put it into single/diff mode, set the + * gain and data rates. + ********************************************************************************* + */ + +#include +#include +#include + +#include +#include + +#include "ads1115.h" + +// Bits in the config register (it's a 16-bit register) + +#define CONFIG_OS_MASK (0x8000) // Operational Status Register +#define CONFIG_OS_SINGLE (0x8000) // Write - Starts a single-conversion + // Read 1 = Conversion complete + +// The multiplexor + +#define CONFIG_MUX_MASK (0x7000) + +// Differential modes + +#define CONFIG_MUX_DIFF_0_1 (0x0000) // Pos = AIN0, Neg = AIN1 (default) +#define CONFIG_MUX_DIFF_0_3 (0x1000) // Pos = AIN0, Neg = AIN3 +#define CONFIG_MUX_DIFF_1_3 (0x2000) // Pos = AIN1, Neg = AIN3 +#define CONFIG_MUX_DIFF_2_3 (0x3000) // Pos = AIN2, Neg = AIN3 (2nd differential channel) + +// Single-ended modes + +#define CONFIG_MUX_SINGLE_0 (0x4000) // AIN0 +#define CONFIG_MUX_SINGLE_1 (0x5000) // AIN1 +#define CONFIG_MUX_SINGLE_2 (0x6000) // AIN2 +#define CONFIG_MUX_SINGLE_3 (0x7000) // AIN3 + +// Programmable Gain Amplifier + +#define CONFIG_PGA_MASK (0x0E00) +#define CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3 +#define CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1 +#define CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default) +#define CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4 +#define CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8 +#define CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16 + +#define CONFIG_MODE (0x0100) // 0 is continuous, 1 is single-shot (default) + +// Data Rate + +#define CONFIG_DR_MASK (0x00E0) +#define CONFIG_DR_8SPS (0x0000) // 8 samples per second +#define CONFIG_DR_16SPS (0x0020) // 16 samples per second +#define CONFIG_DR_32SPS (0x0040) // 32 samples per second +#define CONFIG_DR_64SPS (0x0060) // 64 samples per second +#define CONFIG_DR_128SPS (0x0080) // 128 samples per second (default) +#define CONFIG_DR_475SPS (0x00A0) // 475 samples per second +#define CONFIG_DR_860SPS (0x00C0) // 860 samples per second + +// Comparator mode + +#define CONFIG_CMODE_MASK (0x0010) +#define CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default) +#define CONFIG_CMODE_WINDOW (0x0010) // Window comparator + +// Comparator polarity - the polarity of the output alert/rdy pin + +#define CONFIG_CPOL_MASK (0x0008) +#define CONFIG_CPOL_ACTVLOW (0x0000) // Active low (default) +#define CONFIG_CPOL_ACTVHI (0x0008) // Active high + +// Latching comparator - does the alert/rdy pin latch + +#define CONFIG_CLAT_MASK (0x0004) +#define CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default) +#define CONFIG_CLAT_LATCH (0x0004) // Latching comparator + +// Comparitor queue + +#define CONFIG_CQUE_MASK (0x0003) +#define CONFIG_CQUE_1CONV (0x0000) // Assert after one conversions +#define CONFIG_CQUE_2CONV (0x0001) // Assert after two conversions +#define CONFIG_CQUE_4CONV (0x0002) // Assert after four conversions +#define CONFIG_CQUE_NONE (0x0003) // Disable the comparator (default) + +#define CONFIG_DEFAULT (0x8583) // From the datasheet + + +static const uint16_t dataRates [8] = +{ + CONFIG_DR_8SPS, CONFIG_DR_16SPS, CONFIG_DR_32SPS, CONFIG_DR_64SPS, CONFIG_DR_128SPS, CONFIG_DR_475SPS, CONFIG_DR_860SPS +} ; + +static const uint16_t gains [6] = +{ + CONFIG_PGA_6_144V, CONFIG_PGA_4_096V, CONFIG_PGA_2_048V, CONFIG_PGA_1_024V, CONFIG_PGA_0_512V, CONFIG_PGA_0_256V +} ; + + +/* + * analogRead: + * Pin is the channel to sample on the device. + * Channels 0-3 are single ended inputs, + * channels 4-7 are the various differential combinations. + ********************************************************************************* + */ + +static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) +{ + int chan = pin - node->pinBase ; + int16_t result ; + uint16_t config = CONFIG_DEFAULT ; + + chan &= 7 ; + +// Setup the configuration register + +// Set PGA/voltage range + + config &= ~CONFIG_PGA_MASK ; + config |= node->data0 ; + +// Set sample speed + + config &= ~CONFIG_DR_MASK ; + config |= node->data1 ; + +// Set single-ended channel or differential mode + + config &= ~CONFIG_MUX_MASK ; + + switch (chan) + { + case 0: config |= CONFIG_MUX_SINGLE_0 ; break ; + case 1: config |= CONFIG_MUX_SINGLE_1 ; break ; + case 2: config |= CONFIG_MUX_SINGLE_2 ; break ; + case 3: config |= CONFIG_MUX_SINGLE_3 ; break ; + + case 4: config |= CONFIG_MUX_DIFF_0_1 ; break ; + case 5: config |= CONFIG_MUX_DIFF_2_3 ; break ; + case 6: config |= CONFIG_MUX_DIFF_0_3 ; break ; + case 7: config |= CONFIG_MUX_DIFF_1_3 ; break ; + } + +// Start a single conversion + + config |= CONFIG_OS_SINGLE ; + config = __bswap_16 (config) ; + wiringPiI2CWriteReg16 (node->fd, 1, config) ; + +// Wait for the conversion to complete + + for (;;) + { + result = wiringPiI2CReadReg16 (node->fd, 1) ; + result = __bswap_16 (result) ; + if ((result & CONFIG_OS_MASK) != 0) + break ; + delayMicroseconds (100) ; + } + + result = wiringPiI2CReadReg16 (node->fd, 0) ; + result = __bswap_16 (result) ; + +// Sometimes with a 0v input on a single-ended channel the internal 0v reference +// can be higher than the input, so you get a negative result... + + if ( (chan < 4) && (result < 0) ) + return 0 ; + else + return (int)result ; +} + + +/* + * digitalWrite: + * It may seem odd to have a digital write here, but it's the best way + * to pass paramters into the chip in the wiringPi way of things. + * We have 2 digital registers: + * 0 is the gain control + * 1 is the data rate control + ********************************************************************************* + */ + +static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int data) +{ + int chan = pin - node->pinBase ; + chan &= 3 ; + + if (chan == 0) // Gain Control + { + if ( (data < 0) || (data > 6) ) // Use default if out of range + data = 2 ; + node->data0 = gains [data] ; + } + else // Data rate control + { + if ( (data < 0) || (data > 7) ) // Use default if out of range + data = 4 ; + node->data0 = dataRates [data] ; + } + +} + + +/* + * analogWrite: + * We're using this to write to the 2 comparitor threshold registers. + * We could use a digitalWrite here but as it's an analog comparison + * then it feels better to do it this way. + ********************************************************************************* + */ + +static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int data) +{ + int chan = pin - node->pinBase ; + int reg ; + int16_t ndata ; + + chan &= 3 ; + + reg = chan + 2 ; + + /**/ if (data < -32767) + ndata = -32767 ; + else if (data > 32767) + ndata = 32767 ; + else + ndata = (int16_t)data ; + + ndata = __bswap_16 (ndata) ; + wiringPiI2CWriteReg16 (node->fd, reg, data) ; +} + + + +/* + * ads1115Setup: + * Create a new wiringPi device node for an ads1115 on the Pi's + * I2C interface. + ********************************************************************************* + */ + +int ads1115Setup (const int pinBase, int i2cAddr) +{ + struct wiringPiNodeStruct *node ; + int fd ; + + if ((fd = wiringPiI2CSetup (i2cAddr)) < 0) + return -1 ; + + node = wiringPiNewNode (pinBase, 8) ; + + node->fd = fd ; + node->data0 = CONFIG_PGA_4_096V ; // Gain in data0 + node->data1 = CONFIG_DR_128SPS ; // Samples/sec in data1 + node->analogRead = myAnalogRead ; + node->analogWrite = myAnalogWrite ; + node->digitalWrite = myDigitalWrite ; + + return 0 ; +} diff --git a/wiringPi/ads1115.h b/wiringPi/ads1115.h new file mode 100644 index 0000000..5c91735 --- /dev/null +++ b/wiringPi/ads1115.h @@ -0,0 +1,55 @@ +/* + * ads1115.c: + * Extend wiringPi with the ADS1115 I2C 16-bit ADC + * Copyright (c) 2016 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 . + *********************************************************************** + */ + +// Constants for some of the internal functions + +// Gain + +#define ADS1115_GAIN_6 0 +#define ADS1115_GAIN_4 1 +#define ADS1115_GAIN_2 2 +#define ADS1115_GAIN_1 3 +#define ADS1115_GAIN_HALF 4 +#define ADS1115_GAIN_QUARTER 5 + +// Data rate + +#define ADS1115_DR_8 0 +#define ADS1115_DR_16 1 +#define ADS1115_DR_32 2 +#define ADS1115_DR_64 3 +#define ADS1115_DR_128 4 +#define ADS1115_DR_250 5 +#define ADS1115_DR_475 6 +#define ADS1115_DR_860 7 + +#ifdef __cplusplus +extern "C" { +#endif + +extern int ads1115Setup (int pinBase, int i2cAddress) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 503151f..03c97c5 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -461,6 +461,7 @@ static uint8_t gpioToShift [] = 0,3,6,9,12,15,18,21,24,27, 0,3,6,9,12,15,18,21,24,27, 0,3,6,9,12,15,18,21,24,27, + 0,3,6,9,12,15,18,21,24,27, } ; @@ -1970,6 +1971,16 @@ int wiringPiSetup (void) int fd ; int boardRev ; int model, rev, mem, maker, overVolted ; + static int alreadyCalled = FALSE ; + +// This is here to trap the unwary - those who's program appears to work then fails some +// time later with a weird error message because you run out of file-handles. + + if (alreadyCalled) + (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup*: You must only call this once per program run. This is a fatal error. Please fix your code.\n") ; + + alreadyCalled = TRUE ; + if (getenv (ENV_DEBUG) != NULL) wiringPiDebug = TRUE ; @@ -1994,12 +2005,14 @@ int wiringPiSetup (void) pinToGpio = pinToGpioR1 ; physToGpio = physToGpioR1 ; } - else // A, B, Rev 2, B+, CM, Pi2 + else // A, B, Rev 2, B+, CM, Pi2, Zero { pinToGpio = pinToGpioR2 ; physToGpio = physToGpioR2 ; } +// Note that a Zero is a model 1 + if (piModel2) RASPBERRY_PI_PERI_BASE = 0x3F000000 ; else @@ -2153,6 +2166,15 @@ int wiringPiSetupSys (void) int boardRev ; int pin ; char fName [128] ; + static int alreadyCalled = FALSE ; + +// This is here to trap the unwary - those who's program appears to work then fails some +// time later with a weird error message because you run out of file-handles. + + if (alreadyCalled) + (void)wiringPiFailure (WPI_FATAL, "wiringPiSetupSys: You must only call this once per program run. This is a fatal error. Please fix your code.\n") ; + + alreadyCalled = TRUE ; if (getenv (ENV_DEBUG) != NULL) wiringPiDebug = TRUE ; diff --git a/wiringPi/wpiExtensions.c b/wiringPi/wpiExtensions.c index 4cae9c4..b25428c 100644 --- a/wiringPi/wpiExtensions.c +++ b/wiringPi/wpiExtensions.c @@ -52,6 +52,7 @@ #include "mcp3422.h" #include "max31855.h" #include "max5322.h" +#include "ads1115.h" #include "sn3218.h" #include "drcSerial.h" @@ -374,6 +375,32 @@ static int doExtensionPcf8574 (char *progName, int pinBase, char *params) } +/* + * doExtensionAds1115: + * Analog Input + * ads1115:base:i2cAddr + ********************************************************************************* + */ + +static int doExtensionAds1115 (char *progName, int pinBase, char *params) +{ + int i2c ; + + if ((params = extractInt (progName, params, &i2c)) == NULL) + return FALSE ; + + if ((i2c < 0x03) || (i2c > 0x77)) + { + verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ; + return FALSE ; + } + + ads1115Setup (pinBase, i2c) ; + + return TRUE ; +} + + /* * doExtensionPcf8591: * Analog IO @@ -654,6 +681,7 @@ static struct extensionFunctionStruct extensionFunctions [] = { "mcp4802", &doExtensionMcp4802 }, { "mcp3422", &doExtensionMcp3422 }, { "max31855", &doExtensionMax31855 }, + { "ads1115", &doExtensionAds1115 }, { "max5322", &doExtensionMax5322 }, { "sn3218", &doExtensionSn3218 }, { "drcs", &doExtensionDrcS }, From 2dbecfca0a3d4a7dbaa86738a6f93867457f235c Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Thu, 28 Jan 2016 21:36:30 +0000 Subject: [PATCH 96/97] Minor change to the scroll phat test program, and bumped the version. --- VERSION | 2 +- debian-template/wiringPi/DEBIAN/control | 2 +- examples/scrollPhat/test.c | 2 +- gpio/version.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/VERSION b/VERSION index 3125d73..0f34dc7 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.31 +2.32 diff --git a/debian-template/wiringPi/DEBIAN/control b/debian-template/wiringPi/DEBIAN/control index 5d698c8..230da07 100644 --- a/debian-template/wiringPi/DEBIAN/control +++ b/debian-template/wiringPi/DEBIAN/control @@ -1,5 +1,5 @@ Package: wiringpi -Version: 2.31 +Version: 2.32 Section: libraries Priority: optional Architecture: armhf diff --git a/examples/scrollPhat/test.c b/examples/scrollPhat/test.c index 31e4311..e4d8021 100644 --- a/examples/scrollPhat/test.c +++ b/examples/scrollPhat/test.c @@ -107,7 +107,7 @@ int main (void) printf (" Message Test...Press Ctrl-C to exit: ") ; fflush (stdout) ; - scrollPhatPrintSpeed (20) ; + scrollPhatPrintSpeed (75) ; for (;;) scrollPhatPuts (" Welcome to the scroll phat from Pimoroni ") ; diff --git a/gpio/version.h b/gpio/version.h index 290ab36..5b92dc9 100644 --- a/gpio/version.h +++ b/gpio/version.h @@ -1 +1 @@ -#define VERSION "2.31" +#define VERSION "2.32" From b0a60c3302973ca1878d149d61f2f612c8f27fac Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Mon, 29 Feb 2016 06:57:38 +0000 Subject: [PATCH 97/97] Many changes - tidying up the extensions interfaces. Updating the GPIO command - new command - allreadall ScrollPhat code max31855 code (tested with adafruit breakout board) more tests updated rht03 code Raspberry Pi v3 support. --- build | 2 + devLib/maxdetect.c | 131 ++++++++++++++++++++++++++++++--------- devLib/scrollPhatFont.h | 4 +- examples/rht03.c | 47 +++++++++----- gpio/gpio.c | 72 +++++++++++---------- gpio/readall.c | 39 ++++++++++-- wiringPi/ads1115.c | 4 +- wiringPi/drcSerial.c | 13 ++-- wiringPi/max31855.c | 4 +- wiringPi/max5322.c | 4 +- wiringPi/mcp23008.c | 4 +- wiringPi/mcp23016.c | 4 +- wiringPi/mcp23017.c | 4 +- wiringPi/mcp23s08.c | 7 +-- wiringPi/mcp23s17.c | 7 +-- wiringPi/mcp3002.c | 4 +- wiringPi/mcp3004.c | 4 +- wiringPi/mcp3422.c | 51 +++++++++------ wiringPi/mcp3422.h | 12 ++-- wiringPi/mcp4802.c | 4 +- wiringPi/pcf8574.c | 4 +- wiringPi/pcf8591.c | 4 +- wiringPi/sn3218.c | 4 +- wiringPi/sr595.c | 2 +- wiringPi/wiringPi.c | 96 +++++++++++++++++++++++++--- wiringPi/wiringPi.h | 41 +++++++----- wiringPi/wpiExtensions.c | 19 +++--- 27 files changed, 406 insertions(+), 185 deletions(-) diff --git a/build b/build index c2a589c..368e8f4 100755 --- a/build +++ b/build @@ -62,6 +62,8 @@ if [ x$1 = "xclean" ]; then echo -n "Quick2Wire: " ; make clean cd ../PiGlow echo -n "PiGlow: " ; make clean + cd ../scrollPhat + echo -n "scrollPhat: " ; make clean exit fi diff --git a/devLib/maxdetect.c b/devLib/maxdetect.c index 23eabf8..74ff70e 100755 --- a/devLib/maxdetect.c +++ b/devLib/maxdetect.c @@ -22,7 +22,8 @@ *********************************************************************** */ -//#include +#include +#include //#include //#include @@ -38,21 +39,43 @@ /* * maxDetectLowHighWait: - * Wait for a transition from high to low on the bus + * Wait for a transition from low to high on the bus ********************************************************************************* */ -static void maxDetectLowHighWait (const int pin) +static int maxDetectLowHighWait (const int pin) { - unsigned int timeOut = millis () + 2000 ; + struct timeval now, timeOut, timeUp ; + +// If already high then wait for pin to go low + + gettimeofday (&now, NULL) ; + timerclear (&timeOut) ; + timeOut.tv_usec = 1000 ; + timeradd (&now, &timeOut, &timeUp) ; while (digitalRead (pin) == HIGH) - if (millis () > timeOut) - return ; + { + gettimeofday (&now, NULL) ; + if (timercmp (&now, &timeUp, >)) + return FALSE ; + } + +// Wait for it to go HIGH + + gettimeofday (&now, NULL) ; + timerclear (&timeOut) ; + timeOut.tv_usec = 1000 ; + timeradd (&now, &timeOut, &timeUp) ; while (digitalRead (pin) == LOW) - if (millis () > timeOut) - return ; + { + gettimeofday (&now, NULL) ; + if (timercmp (&now, &timeUp, >)) + return FALSE ; + } + + return TRUE ; } @@ -69,7 +92,8 @@ static unsigned int maxDetectClockByte (const int pin) for (bit = 0 ; bit < 8 ; ++bit) { - maxDetectLowHighWait (pin) ; + if (!maxDetectLowHighWait (pin)) + return 0 ; // bit starting now - we need to time it. @@ -95,6 +119,11 @@ int maxDetectRead (const int pin, unsigned char buffer [4]) int i ; unsigned int checksum ; unsigned char localBuf [5] ; + struct timeval now, then, took ; + +// See how long we took + + gettimeofday (&then, NULL) ; // Wake up the RHT03 by pulling the data line low, then high // Low for 10mS, high for 40uS. @@ -106,7 +135,8 @@ int maxDetectRead (const int pin, unsigned char buffer [4]) // Now wait for sensor to pull pin low - maxDetectLowHighWait (pin) ; + if (!maxDetectLowHighWait (pin)) + return FALSE ; // and read in 5 bytes (40 bits) @@ -121,6 +151,22 @@ int maxDetectRead (const int pin, unsigned char buffer [4]) } checksum &= 0xFF ; +// See how long we took + + gettimeofday (&now, NULL) ; + timersub (&now, &then, &took) ; + +// Total time to do this should be: +// 10mS + 40µS - reset +// + 80µS + 80µS - sensor doing its low -> high thing +// + 40 * (50µS + 27µS (0) or 70µS (1) ) +// = 15010µS +// so if we take more than that, we've had a scheduling interruption and the +// reading is probably bogus. + + if ((took.tv_sec != 0) || (took.tv_usec > 16000)) + return FALSE ; + return checksum == localBuf [4] ; } @@ -128,38 +174,65 @@ int maxDetectRead (const int pin, unsigned char buffer [4]) /* * readRHT03: * Read the Temperature & Humidity from an RHT03 sensor + * Values returned are *10, so 123 is 12.3. ********************************************************************************* */ int readRHT03 (const int pin, int *temp, int *rh) { - static unsigned int nextTime = 0 ; - static int lastTemp = 0 ; - static int lastRh = 0 ; - static int lastResult = TRUE ; + static struct timeval then ; // will initialise to zero + static int lastTemp = 0 ; + static int lastRh = 0 ; + int result ; + struct timeval now, timeOut ; unsigned char buffer [4] ; -// Don't read more than once a second +// The data sheets say to not read more than once every 2 seconds, so you +// get the last good reading - if (millis () < nextTime) + gettimeofday (&now, NULL) ; + if (timercmp (&now, &then, <)) { - *temp = lastTemp ; *rh = lastRh ; - return lastResult ; - } - - lastResult = maxDetectRead (pin, buffer) ; - - if (lastResult) - { - *temp = lastTemp = (buffer [2] * 256 + buffer [3]) ; - *rh = lastRh = (buffer [0] * 256 + buffer [1]) ; - nextTime = millis () + 2000 ; + *temp = lastTemp ; return TRUE ; } - else - { + +// Set timeout for next read + + gettimeofday (&now, NULL) ; + timerclear (&timeOut) ; + timeOut.tv_sec = 2 ; + timeradd (&now, &timeOut, &then) ; + +// Read ... + + result = maxDetectRead (pin, buffer) ; + + if (!result) // Try again, but just once + result = maxDetectRead (pin, buffer) ; + + if (!result) return FALSE ; + + *rh = (buffer [0] * 256 + buffer [1]) ; + *temp = (buffer [2] * 256 + buffer [3]) ; + + if ((*temp & 0x8000) != 0) // Negative + { + *temp &= 0x7FFF ; + *temp = -*temp ; } + +// Discard obviously bogus readings - the checksum can't detect a 2-bit error +// (which does seem to happen - no realtime here) + + if ((*rh > 999) || (*temp > 800) || (*temp < -400)) + return FALSE ; + + lastRh = *rh ; + lastTemp = *temp ; + + return TRUE ; } diff --git a/devLib/scrollPhatFont.h b/devLib/scrollPhatFont.h index 35481f8..92f623a 100644 --- a/devLib/scrollPhatFont.h +++ b/devLib/scrollPhatFont.h @@ -64,11 +64,11 @@ static unsigned char scrollPhatFont [] = // 0x24, $ - 0x1, // ...* + 0x1, // ..*. 0x7, // .*** 0x2, // ..*. 0xE, // ***. - 0x8, // *... + 0x8, // ..*. // 0x25, % diff --git a/examples/rht03.c b/examples/rht03.c index 566e954..854f837 100644 --- a/examples/rht03.c +++ b/examples/rht03.c @@ -27,7 +27,7 @@ #include #include -#define RHT03_PIN 0 +#define RHT03_PIN 7 /* *********************************************************************** @@ -37,32 +37,49 @@ int main (void) { - int temp, rh ; - int newTemp, newRh ; + int result, temp, rh ; + int minT, maxT, minRH, maxRH ; - temp = rh = newTemp = newRh = 0 ; + int numGood, numBad ; wiringPiSetup () ; piHiPri (55) ; + minT = 1000 ; + maxT = -1000 ; + + minRH = 1000 ; + maxRH = -1000 ; + + numGood = numBad = 0 ; + for (;;) { delay (100) ; - if (!readRHT03 (RHT03_PIN, &newTemp, &newRh)) - continue ; + result = readRHT03 (RHT03_PIN, &temp, &rh) ; - if ((temp != newTemp) || (rh != newRh)) + if (!result) { - temp = newTemp ; - rh = newRh ; - if ((temp & 0x8000) != 0) // Negative - { - temp &= 0x7FFF ; - temp = -temp ; - } - printf ("Temp: %5.1f, RH: %5.1f%%\n", temp / 10.0, rh / 10.0) ; + printf (".") ; + fflush (stdout) ; + ++numBad ; + continue ; } + + ++numGood ; + + if (temp < minT) minT = temp ; + if (temp > maxT) maxT = temp ; + if (rh < minRH) minRH = rh ; + if (rh > maxRH) maxRH = rh ; + + printf ("\r%6d, %6d: ", numGood, numBad) ; + printf ("Temp: %5.1f, RH: %5.1f%%", temp / 10.0, rh / 10.0) ; + printf (" Max/Min Temp: %5.1f:%5.1f", maxT / 10.0, minT / 10.0) ; + printf (" Max/Min RH: %5.1f:%5.1f", maxRH / 10.0, minRH / 10.0) ; + + printf ("\n") ; } return 0 ; diff --git a/gpio/gpio.c b/gpio/gpio.c index 6c95b21..6162090 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -47,6 +47,7 @@ extern int wiringPiDebug ; // External functions I can't be bothered creating a separate .h file for: extern void doReadall (void) ; +extern void doAllReadall (void) ; extern void doPins (void) ; #ifndef TRUE @@ -115,10 +116,12 @@ static void changeOwner (char *cmd, char *file) if (chown (file, uid, gid) != 0) { - if (errno == ENOENT) // Warn that it's not there - fprintf (stderr, "%s: Warning (not an error, do not report): File not present: %s\n", cmd, file) ; - else - fprintf (stderr, "%s: Warning (not an error): Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ; + +// Removed (ignoring) the check for not existing as I'm fed-up with morons telling me that +// the warning message is an error. + + if (errno != ENOENT) + fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ; } } @@ -138,7 +141,7 @@ static int moduleLoaded (char *modName) if (fd == NULL) { - fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ; + fprintf (stderr, "gpio: Unable to check /proc/modules: %s\n", strerror (errno)) ; exit (1) ; } @@ -774,13 +777,13 @@ static void doUsbP (int argc, char *argv []) piBoardId (&model, &rev, &mem, &maker, &overVolted) ; - if (model != PI_MODEL_BP) + if (!((model == PI_MODEL_BP) || (model == PI_MODEL_2))) { - fprintf (stderr, "USB power contol is applicable to B+ boards only.\n") ; + fprintf (stderr, "USB power contol is applicable to B+ and v2 boards only.\n") ; exit (1) ; } -// Need to force BCM_GPIO mode: +// Make sure we start in BCM_GPIO mode wiringPiSetupGpio () ; @@ -1150,7 +1153,8 @@ static void doPwmClock (int argc, char *argv []) /* * doVersion: - * Handle the ever more complicated version command + * Handle the ever more complicated version command and print out + * some usefull information. ********************************************************************************* */ @@ -1166,35 +1170,23 @@ static void doVersion (char *argv []) printf ("\n") ; piBoardId (&model, &rev, &mem, &maker, &warranty) ; -/************* - if (model == PI_MODEL_UNKNOWN) - { - printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ; - printf (" projects@drogon.net\n") ; - printf ("with a copy of your /proc/cpuinfo if possible\n") ; - } - else -***************/ - - { - printf ("Raspberry Pi Details:\n") ; - printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n", - piModelNames [model], piRevisionNames [rev], piMemorySize [mem], piMakerNames [maker], warranty ? "[Out of Warranty]" : "") ; + printf ("Raspberry Pi Details:\n") ; + printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n", + piModelNames [model], piRevisionNames [rev], piMemorySize [mem], piMakerNames [maker], warranty ? "[Out of Warranty]" : "") ; // Check for device tree - if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ... - printf (" Device tree is enabled.\n") ; + if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ... + printf (" * Device tree is enabled.\n") ; - if (stat ("/dev/gpiomem", &statBuf) == 0) // User level GPIO is GO - { - printf (" This Raspberry Pi supports user-level GPIO access.\n") ; - printf (" -> See the man-page for more details\n") ; - } - else - printf (" * Root or sudo required for GPIO access.\n") ; - + if (stat ("/dev/gpiomem", &statBuf) == 0) // User level GPIO is GO + { + printf (" * This Raspberry Pi supports user-level GPIO access.\n") ; + printf (" -> See the man-page for more details\n") ; + printf (" -> ie. export WIRINGPI_GPIOMEM=1\n") ; } + else + printf (" * Root or sudo required for GPIO access.\n") ; } @@ -1285,11 +1277,24 @@ int main (int argc, char *argv []) if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; } if (strcasecmp (argv [1], "unload" ) == 0) { doUnLoad (argc, argv) ; return 0 ; } +// Check for usb power command + + if (strcasecmp (argv [1], "usbp" ) == 0) { doUsbP (argc, argv) ; return 0 ; } + // Gertboard commands if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; } if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; } +// Check for allreadall command, force Gpio mode + + if (strcasecmp (argv [1], "allreadall") == 0) + { + wiringPiSetupGpio () ; + doAllReadall () ; + return 0 ; + } + // Check for -g argument /**/ if (strcasecmp (argv [1], "-g") == 0) @@ -1379,7 +1384,6 @@ int main (int argc, char *argv []) else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ; else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ; else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; - else if (strcasecmp (argv [1], "usbp" ) == 0) doUsbP (argc, argv) ; else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ; else if (strcasecmp (argv [1], "pins" ) == 0) doPins () ; diff --git a/gpio/readall.c b/gpio/readall.c index 278e6ee..cb7e18f 100644 --- a/gpio/readall.c +++ b/gpio/readall.c @@ -215,7 +215,14 @@ static void readallPhys (int physPin) } -void cmReadall (void) +/* + * allReadall: + * Read all the pins regardless of the model. Primarily of use for + * the compute module, but handy for other fiddling... + ********************************************************************************* + */ + +static void allReadall (void) { int pin ; @@ -292,12 +299,16 @@ static void plus2header (int model) printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ; else if (model == PI_MODEL_ZERO) printf (" +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+\n") ; - else + else if (model == PI_MODEL_2) printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ; + else if (model == PI_MODEL_3) + printf (" +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+\n") ; + else + printf (" +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+\n") ; } -void piPlusReadall (int model) +static void piPlusReadall (int model) { int pin ; @@ -314,6 +325,13 @@ void piPlusReadall (int model) } +/* + * doReadall: + * Generic read all pins called from main program. Works out the Pi type + * and calls the appropriate function. + ********************************************************************************* + */ + void doReadall (void) { int model, rev, mem, maker, overVolted ; @@ -328,10 +346,21 @@ void doReadall (void) /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B)) abReadall (model, rev) ; - else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2) || (model == PI_MODEL_ZERO)) + else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2) || (model == PI_MODEL_3) || (model == PI_MODEL_ZERO)) piPlusReadall (model) ; else if (model == PI_MODEL_CM) - cmReadall () ; + allReadall () ; else printf ("Oops - unable to determine board type... model: %d\n", model) ; } + +/* + * doAllReadall: + * Force reading of all pins regardless of Pi model + ********************************************************************************* + */ + +void doAllReadall (void) +{ + allReadall () ; +} diff --git a/wiringPi/ads1115.c b/wiringPi/ads1115.c index 62e2a7f..62b92f8 100644 --- a/wiringPi/ads1115.c +++ b/wiringPi/ads1115.c @@ -278,7 +278,7 @@ int ads1115Setup (const int pinBase, int i2cAddr) int fd ; if ((fd = wiringPiI2CSetup (i2cAddr)) < 0) - return -1 ; + return FALSE ; node = wiringPiNewNode (pinBase, 8) ; @@ -289,5 +289,5 @@ int ads1115Setup (const int pinBase, int i2cAddr) node->analogWrite = myAnalogWrite ; node->digitalWrite = myDigitalWrite ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/drcSerial.c b/wiringPi/drcSerial.c index 6491a98..db7cc09 100644 --- a/wiringPi/drcSerial.c +++ b/wiringPi/drcSerial.c @@ -1,7 +1,7 @@ /* * drcSerial.c: * Extend wiringPi with the DRC Serial protocol (e.g. to Arduino) - * Copyright (c) 2013 Gordon Henderson + * Copyright (c) 2013-2016 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -32,11 +32,6 @@ #include "drcSerial.h" -#ifndef TRUE -# define TRUE (1==1) -# define FALSE (1==2) -#endif - /* * myPinMode: @@ -156,7 +151,7 @@ int drcSetupSerial (const int pinBase, const int numPins, const char *device, co struct wiringPiNodeStruct *node ; if ((fd = serialOpen (device, baud)) < 0) - return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ; + return FALSE ; delay (10) ; // May need longer if it's an Uno that reboots on the open... @@ -184,7 +179,7 @@ int drcSetupSerial (const int pinBase, const int numPins, const char *device, co if (!ok) { serialClose (fd) ; - return wiringPiFailure (WPI_FATAL, "Unable to communicate with DRC serial device") ; + return FALSE ; } node = wiringPiNewNode (pinBase, numPins) ; @@ -197,5 +192,5 @@ int drcSetupSerial (const int pinBase, const int numPins, const char *device, co node->digitalWrite = myDigitalWrite ; node->pwmWrite = myPwmWrite ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/max31855.c b/wiringPi/max31855.c index ea184d8..d86cabd 100644 --- a/wiringPi/max31855.c +++ b/wiringPi/max31855.c @@ -88,12 +88,12 @@ int max31855Setup (const int pinBase, int spiChannel) struct wiringPiNodeStruct *node ; if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi - return -1 ; + return FALSE ; node = wiringPiNewNode (pinBase, 4) ; node->fd = spiChannel ; node->analogRead = myAnalogRead ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/max5322.c b/wiringPi/max5322.c index b7cd6a9..e56b085 100644 --- a/wiringPi/max5322.c +++ b/wiringPi/max5322.c @@ -66,7 +66,7 @@ int max5322Setup (const int pinBase, int spiChannel) unsigned char spiData [2] ; if (wiringPiSPISetup (spiChannel, 8000000) < 0) // 10MHz Max - return -1 ; + return FALSE ; node = wiringPiNewNode (pinBase, 2) ; @@ -80,5 +80,5 @@ int max5322Setup (const int pinBase, int spiChannel) wiringPiSPIDataRW (node->fd, spiData, 2) ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/mcp23008.c b/wiringPi/mcp23008.c index d21d237..71757a8 100644 --- a/wiringPi/mcp23008.c +++ b/wiringPi/mcp23008.c @@ -132,7 +132,7 @@ int mcp23008Setup (const int pinBase, const int i2cAddress) struct wiringPiNodeStruct *node ; if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) - return fd ; + return FALSE ; wiringPiI2CWriteReg8 (fd, MCP23x08_IOCON, IOCON_INIT) ; @@ -145,5 +145,5 @@ int mcp23008Setup (const int pinBase, const int i2cAddress) node->digitalWrite = myDigitalWrite ; node->data2 = wiringPiI2CReadReg8 (fd, MCP23x08_OLAT) ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/mcp23016.c b/wiringPi/mcp23016.c index e5cc632..928d9e5 100644 --- a/wiringPi/mcp23016.c +++ b/wiringPi/mcp23016.c @@ -146,7 +146,7 @@ int mcp23016Setup (const int pinBase, const int i2cAddress) struct wiringPiNodeStruct *node ; if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) - return fd ; + return FALSE ; wiringPiI2CWriteReg8 (fd, MCP23016_IOCON0, IOCON_INIT) ; wiringPiI2CWriteReg8 (fd, MCP23016_IOCON1, IOCON_INIT) ; @@ -160,5 +160,5 @@ int mcp23016Setup (const int pinBase, const int i2cAddress) node->data2 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT0) ; node->data3 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT1) ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/mcp23017.c b/wiringPi/mcp23017.c index 5174195..4c3952d 100644 --- a/wiringPi/mcp23017.c +++ b/wiringPi/mcp23017.c @@ -177,7 +177,7 @@ int mcp23017Setup (const int pinBase, const int i2cAddress) struct wiringPiNodeStruct *node ; if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) - return fd ; + return FALSE ; wiringPiI2CWriteReg8 (fd, MCP23x17_IOCON, IOCON_INIT) ; @@ -191,5 +191,5 @@ int mcp23017Setup (const int pinBase, const int i2cAddress) node->data2 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATA) ; node->data3 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATB) ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/mcp23s08.c b/wiringPi/mcp23s08.c index d0acb5e..f293f3a 100644 --- a/wiringPi/mcp23s08.c +++ b/wiringPi/mcp23s08.c @@ -167,11 +167,10 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) int mcp23s08Setup (const int pinBase, const int spiPort, const int devId) { - int x ; struct wiringPiNodeStruct *node ; - if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0) - return x ; + if (wiringPiSPISetup (spiPort, MCP_SPEED) < 0) + return FALSE ; writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ; @@ -185,5 +184,5 @@ int mcp23s08Setup (const int pinBase, const int spiPort, const int devId) node->digitalWrite = myDigitalWrite ; node->data2 = readByte (spiPort, devId, MCP23x08_OLAT) ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/mcp23s17.c b/wiringPi/mcp23s17.c index c2d1be3..42b0358 100644 --- a/wiringPi/mcp23s17.c +++ b/wiringPi/mcp23s17.c @@ -212,11 +212,10 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) int mcp23s17Setup (const int pinBase, const int spiPort, const int devId) { - int x ; struct wiringPiNodeStruct *node ; - if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0) - return x ; + if (wiringPiSPISetup (spiPort, MCP_SPEED) < 0) + return FALSE ; writeByte (spiPort, devId, MCP23x17_IOCON, IOCON_INIT | IOCON_HAEN) ; writeByte (spiPort, devId, MCP23x17_IOCONB, IOCON_INIT | IOCON_HAEN) ; @@ -232,5 +231,5 @@ int mcp23s17Setup (const int pinBase, const int spiPort, const int devId) node->data2 = readByte (spiPort, devId, MCP23x17_OLATA) ; node->data3 = readByte (spiPort, devId, MCP23x17_OLATB) ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/mcp3002.c b/wiringPi/mcp3002.c index 5d44940..8e191b6 100644 --- a/wiringPi/mcp3002.c +++ b/wiringPi/mcp3002.c @@ -65,12 +65,12 @@ int mcp3002Setup (const int pinBase, int spiChannel) struct wiringPiNodeStruct *node ; if (wiringPiSPISetup (spiChannel, 1000000) < 0) - return -1 ; + return FALSE ; node = wiringPiNewNode (pinBase, 2) ; node->fd = spiChannel ; node->analogRead = myAnalogRead ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/mcp3004.c b/wiringPi/mcp3004.c index 82c73dd..be8383e 100644 --- a/wiringPi/mcp3004.c +++ b/wiringPi/mcp3004.c @@ -65,12 +65,12 @@ int mcp3004Setup (const int pinBase, int spiChannel) struct wiringPiNodeStruct *node ; if (wiringPiSPISetup (spiChannel, 1000000) < 0) - return -1 ; + return FALSE ; node = wiringPiNewNode (pinBase, 8) ; node->fd = spiChannel ; node->analogRead = myAnalogRead ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/mcp3422.c b/wiringPi/mcp3422.c index 831aece..be14db6 100644 --- a/wiringPi/mcp3422.c +++ b/wiringPi/mcp3422.c @@ -1,8 +1,9 @@ /* * mcp3422.c: - * Extend wiringPi with the MCP3422 I2C ADC chip - * Also works for the MCP3423 and MCP3224 (4 channel) chips - * Copyright (c) 2013 Gordon Henderson + * Extend wiringPi with the MCP3422/3/4 I2C ADC chip + * This code assumes single-ended mode only. + * Tested on actual hardware: 20th Feb 2016. + * Copyright (c) 2013-2016 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -29,7 +30,6 @@ #include #include #include -#include #include #include @@ -37,6 +37,23 @@ #include "mcp3422.h" +/* + * waitForConversion: + * Common code to wait for the ADC to finish conversion + ********************************************************************************* + */ + +void waitForConversion (int fd, unsigned char *buffer, int n) +{ + for (;;) + { + read (fd, buffer, n) ; + if ((buffer [n-1] & 0x80) == 0) + break ; + delay (1) ; + } +} + /* * myAnalogRead: * Read a channel from the device @@ -48,37 +65,34 @@ int myAnalogRead (struct wiringPiNodeStruct *node, int chan) unsigned char config ; unsigned char buffer [4] ; int value = 0 ; + int realChan = (chan & 3) - node->pinBase ; // One-shot mode, trigger plus the other configs. - config = 0x80 | ((chan - node->pinBase) << 5) | (node->data0 << 2) | (node->data1) ; + config = 0x80 | (realChan << 5) | (node->data0 << 2) | (node->data1) ; wiringPiI2CWrite (node->fd, config) ; switch (node->data0) // Sample rate { case MCP3422_SR_3_75: // 18 bits - delay (270) ; - read (node->fd, buffer, 4) ; - value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [0] ; + waitForConversion (node->fd, &buffer [0], 4) ; + value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [2] ; break ; case MCP3422_SR_15: // 16 bits - delay ( 70) ; - read (node->fd, buffer, 3) ; + waitForConversion (node->fd, buffer, 3) ; value = (buffer [0] << 8) | buffer [1] ; break ; case MCP3422_SR_60: // 14 bits - delay ( 17) ; - read (node->fd, buffer, 3) ; + waitForConversion (node->fd, buffer, 3) ; value = ((buffer [0] & 0x3F) << 8) | buffer [1] ; break ; - case MCP3422_SR_240: // 12 bits - delay ( 5) ; - read (node->fd, buffer, 3) ; - value = ((buffer [0] & 0x0F) << 8) | buffer [0] ; + case MCP3422_SR_240: // 12 bits - default + waitForConversion (node->fd, buffer, 3) ; + value = ((buffer [0] & 0x0F) << 8) | buffer [1] ; break ; } @@ -98,13 +112,14 @@ int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain) struct wiringPiNodeStruct *node ; if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) - return fd ; + return FALSE ; node = wiringPiNewNode (pinBase, 4) ; + node->fd = fd ; node->data0 = sampleRate ; node->data1 = gain ; node->analogRead = myAnalogRead ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/mcp3422.h b/wiringPi/mcp3422.h index bb4692d..72647d4 100644 --- a/wiringPi/mcp3422.h +++ b/wiringPi/mcp3422.h @@ -1,6 +1,6 @@ /* - * mcp3422.c: - * Extend wiringPi with the MCP3422 I2C ADC chip + * mcp3422.h: + * Extend wiringPi with the MCP3422/3/4 I2C ADC chip *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -21,10 +21,10 @@ *********************************************************************** */ -#define MCP3422_SR_3_75 0 -#define MCP3422_SR_15 1 -#define MCP3422_SR_60 2 -#define MCP3422_SR_240 3 +#define MCP3422_SR_240 0 +#define MCP3422_SR_60 1 +#define MCP3422_SR_15 2 +#define MCP3422_SR_3_75 3 #define MCP3422_GAIN_1 0 #define MCP3422_GAIN_2 1 diff --git a/wiringPi/mcp4802.c b/wiringPi/mcp4802.c index 5c5c17a..ef104ed 100644 --- a/wiringPi/mcp4802.c +++ b/wiringPi/mcp4802.c @@ -65,12 +65,12 @@ int mcp4802Setup (const int pinBase, int spiChannel) struct wiringPiNodeStruct *node ; if (wiringPiSPISetup (spiChannel, 1000000) < 0) - return -1 ; + return FALSE ; node = wiringPiNewNode (pinBase, 2) ; node->fd = spiChannel ; node->analogWrite = myAnalogWrite ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/pcf8574.c b/wiringPi/pcf8574.c index c172d1c..e0b686a 100644 --- a/wiringPi/pcf8574.c +++ b/wiringPi/pcf8574.c @@ -112,7 +112,7 @@ int pcf8574Setup (const int pinBase, const int i2cAddress) struct wiringPiNodeStruct *node ; if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) - return fd ; + return FALSE ; node = wiringPiNewNode (pinBase, 8) ; @@ -122,5 +122,5 @@ int pcf8574Setup (const int pinBase, const int i2cAddress) node->digitalWrite = myDigitalWrite ; node->data2 = wiringPiI2CRead (fd) ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/pcf8591.c b/wiringPi/pcf8591.c index 0c86056..0c43d26 100644 --- a/wiringPi/pcf8591.c +++ b/wiringPi/pcf8591.c @@ -78,7 +78,7 @@ int pcf8591Setup (const int pinBase, const int i2cAddress) struct wiringPiNodeStruct *node ; if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) - return fd ; + return FALSE ; node = wiringPiNewNode (pinBase, 4) ; @@ -86,5 +86,5 @@ int pcf8591Setup (const int pinBase, const int i2cAddress) node->analogRead = myAnalogRead ; node->analogWrite = myAnalogWrite ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/sn3218.c b/wiringPi/sn3218.c index 7ceb156..d9b9113 100644 --- a/wiringPi/sn3218.c +++ b/wiringPi/sn3218.c @@ -55,7 +55,7 @@ int sn3218Setup (const int pinBase) struct wiringPiNodeStruct *node ; if ((fd = wiringPiI2CSetup (0x54)) < 0) - return fd ; + return FALSE ; // Setup the chip - initialise all 18 LEDs to off @@ -71,5 +71,5 @@ int sn3218Setup (const int pinBase) node->fd = fd ; node->analogWrite = myAnalogWrite ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/sr595.c b/wiringPi/sr595.c index 87210c2..8280618 100644 --- a/wiringPi/sr595.c +++ b/wiringPi/sr595.c @@ -105,5 +105,5 @@ int sr595Setup (const int pinBase, const int numPins, pinMode (clockPin, OUTPUT) ; pinMode (latchPin, OUTPUT) ; - return 0 ; + return TRUE ; } diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 03c97c5..dfd5de4 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -69,17 +69,13 @@ #include #include #include +#include #include "softPwm.h" #include "softTone.h" #include "wiringPi.h" -#ifndef TRUE -#define TRUE (1==1) -#define FALSE (1==2) -#endif - // Environment Variables #define ENV_DEBUG "WIRINGPI_DEBUG" @@ -219,7 +215,7 @@ const char *piModelNames [16] = "Alpha", // 5 "CM", // 6 "Unknown07", // 07 - "Unknown08", // 08 + "Pi 3", // 08 "Pi Zero", // 09 "Unknown10", // 10 "Unknown11", // 11 @@ -894,7 +890,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty) *c = 0 ; if (wiringPiDebug) - printf ("piboardId: Revision string: %s\n", line) ; + printf ("piBoardId: Revision string: %s\n", line) ; // Need to work out if it's using the new or old encoding scheme: @@ -1624,16 +1620,21 @@ void pwmToneWrite (int pin, int freq) /* * digitalWriteByte: + * digitalReadByte: * Pi Specific * Write an 8-bit byte to the first 8 GPIO pins - try to do it as * fast as possible. * However it still needs 2 operations to set the bits, so any external * hardware must not rely on seeing a change as there will be a change * to set the outputs bits to zero, then another change to set the 1's + * Reading is just bit fiddling. + * These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers + * 17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3 + * 17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, zero ********************************************************************************* */ -void digitalWriteByte (int value) +void digitalWriteByte (const int value) { uint32_t pinSet = 0 ; uint32_t pinClr = 0 ; @@ -1644,7 +1645,7 @@ void digitalWriteByte (int value) { for (pin = 0 ; pin < 8 ; ++pin) { - digitalWrite (pin, value & mask) ; + digitalWrite (pinToGpio [pin], value & mask) ; mask <<= 1 ; } return ; @@ -1666,6 +1667,83 @@ void digitalWriteByte (int value) } } +unsigned int digitalReadByte (void) +{ + int pin, x ; + uint32_t raw ; + uint32_t data = 0 ; + + /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) + { + for (pin = 0 ; pin < 8 ; ++pin) + { + x = digitalRead (pinToGpio [pin]) ; + data = (data << 1) | x ; + } + } + else + { + raw = *(gpio + gpioToGPLEV [0]) ; // First bank for these pins + for (pin = 0 ; pin < 8 ; ++pin) + { + x = pinToGpio [pin] ; + data = (data << 1) | (((raw & (1 << x)) == 0) ? 0 : 1) ; + } + } + return data ; +} + + +/* + * digitalWriteByte2: + * digitalReadByte2: + * Pi Specific + * Write an 8-bit byte to the second set of 8 GPIO pins. This is marginally + * faster than the first lot as these are consecutive BCM_GPIO pin numbers. + * However they overlap with the original read/write bytes. + ********************************************************************************* + */ + +void digitalWriteByte2 (const int value) +{ + register int mask = 1 ; + register int pin ; + + /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) + { + for (pin = 20 ; pin < 28 ; ++pin) + { + digitalWrite (pin, value & mask) ; + mask <<= 1 ; + } + return ; + } + else + { + *(gpio + gpioToGPCLR [0]) = 0x0FF00000 ; + *(gpio + gpioToGPSET [0]) = (value & 0xFF) << 20 ; + } +} + +unsigned int digitalReadByte2 (void) +{ + int pin, x ; + uint32_t data = 0 ; + + /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) + { + for (pin = 20 ; pin < 28 ; ++pin) + { + x = digitalRead (pin) ; + data = (data << 1) | x ; + } + } + else + data = ((*(gpio + gpioToGPLEV [0])) >> 20) & 0xFF ; // First bank for these pins + + return data ; +} + /* * waitForInterrupt: diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 7b9605f..e11a0be 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -1,7 +1,7 @@ /* - * wiringPi: - * Arduino compatable (ish) Wiring library for the Raspberry Pi - * Copyright (c) 2012 Gordon Henderson + * wiringPi.h: + * Arduino like Wiring library for the Raspberry Pi. + * Copyright (c) 2012-2016 Gordon Henderson *********************************************************************** * This file is part of wiringPi: * https://projects.drogon.net/raspberry-pi/wiringpi/ @@ -24,6 +24,14 @@ #ifndef __WIRING_PI_H__ #define __WIRING_PI_H__ +// C doesn't have true/false by default and I can never remember which +// way round they are, so ... + +#ifndef TRUE +# define TRUE (1==1) +# define FALSE (!TRUE) +#endif + // Handy defines // wiringPi modes @@ -77,7 +85,7 @@ #define PI_ALPHA 5 #define PI_MODEL_CM 6 #define PI_MODEL_07 7 -#define PI_MODEL_08 8 +#define PI_MODEL_3 8 #define PI_MODEL_ZERO 9 #define PI_VERSION_1 0 @@ -182,18 +190,19 @@ extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio // On-Board Raspberry Pi hardware specific stuff -extern int piBoardRev (void) ; -extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ; -extern int wpiPinToGpio (int wpiPin) ; -extern int physPinToGpio (int physPin) ; -extern void setPadDrive (int group, int value) ; -extern int getAlt (int pin) ; -extern void pwmToneWrite (int pin, int freq) ; -extern void digitalWriteByte (int value) ; -extern void pwmSetMode (int mode) ; -extern void pwmSetRange (unsigned int range) ; -extern void pwmSetClock (int divisor) ; -extern void gpioClockSet (int pin, int freq) ; +extern int piBoardRev (void) ; +extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ; +extern int wpiPinToGpio (int wpiPin) ; +extern int physPinToGpio (int physPin) ; +extern void setPadDrive (int group, int value) ; +extern int getAlt (int pin) ; +extern void pwmToneWrite (int pin, int freq) ; +extern void digitalWriteByte (int value) ; +extern unsigned int digitalReadByte (void) ; +extern void pwmSetMode (int mode) ; +extern void pwmSetRange (unsigned int range) ; +extern void pwmSetClock (int divisor) ; +extern void gpioClockSet (int pin, int freq) ; // Interrupts // (Also Pi hardware specific) diff --git a/wiringPi/wpiExtensions.c b/wiringPi/wpiExtensions.c index b25428c..62a92d7 100644 --- a/wiringPi/wpiExtensions.c +++ b/wiringPi/wpiExtensions.c @@ -64,11 +64,6 @@ static int verbose ; static char errorMessage [1024] ; -#ifndef TRUE -# define TRUE (1==1) -# define FALSE (1==2) -#endif - // Local structure to hold details struct extensionFunctionStruct @@ -119,7 +114,13 @@ static char *extractInt (char *progName, char *p, int *num) } *num = strtol (p, NULL, 0) ; - while (isdigit (*p)) + +// Increment p, but we need to check for hex 0x + + if ((*p == '0') && (*(p + 1) == 'x')) + p +=2 ; + + while (isxdigit (*p)) ++p ; return p ; @@ -702,7 +703,7 @@ int loadWPiExtension (char *progName, char *extensionData, int printErrors) char *p ; char *extension = extensionData ; struct extensionFunctionStruct *extensionFn ; - int pinBase = 0 ; + unsigned pinBase = 0 ; verbose = printErrors ; @@ -724,13 +725,13 @@ int loadWPiExtension (char *progName, char *extensionData, int printErrors) if (!isdigit (*p)) { - verbError ("%s: pinBase number expected after extension name", progName) ; + verbError ("%s: decimal pinBase number expected after extension name", progName) ; return FALSE ; } while (isdigit (*p)) { - if (pinBase > 1000000000) // Lets be realistic here... + if (pinBase > 2147483647) // 2^31-1 ... Lets be realistic here... { verbError ("%s: pinBase too large", progName) ; return FALSE ;