Browse Source

New timing methods

pull/1/head
Gordon Drogon 12 years ago
committed by Philip Howard
parent
commit
d24a8fc4cb
24 changed files with 2307 additions and 391 deletions
  1. +165
    -0
      COPYING.LESSER
  2. +165
    -0
      examples/COPYING.LESSER
  3. +15
    -7
      examples/Makefile
  4. +3
    -3
      examples/lcd.c
  5. +53
    -0
      examples/piface.c
  6. +4
    -2
      examples/speed.c
  7. +158
    -0
      examples/wfi.c
  8. +165
    -0
      gpio/COPYING.LESSER
  9. +3
    -3
      gpio/Makefile
  10. +55
    -21
      gpio/gpio.1
  11. +198
    -34
      gpio/gpio.c
  12. +165
    -0
      wiringPi/COPYING.LESSER
  13. +19
    -11
      wiringPi/Makefile
  14. +3
    -3
      wiringPi/lcd.c
  15. +3
    -3
      wiringPi/lcd.h
  16. +50
    -0
      wiringPi/piHiPri.c
  17. +63
    -0
      wiringPi/piThread.c
  18. +381
    -252
      wiringPi/wiringPi.c
  19. +35
    -12
      wiringPi/wiringPi.h
  20. +355
    -0
      wiringPi/wiringPiFace.c
  21. +206
    -0
      wiringPi/wiringSerial.c
  22. +37
    -0
      wiringPi/wiringSerial.h
  23. +3
    -36
      wiringPi/wiringShift.c
  24. +3
    -4
      wiringPi/wiringShift.h

+ 165
- 0
COPYING.LESSER View File

@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
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.

+ 165
- 0
examples/COPYING.LESSER View File

@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
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.

+ 15
- 7
examples/Makefile View File

@@ -9,16 +9,16 @@
# Wiring Compatable library for the Raspberry Pi
#
# wiringPi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wiringPi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# You should have received a copy of the GNU Lesser General Public License
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
#################################################################################

@@ -35,11 +35,11 @@ LIBS = -lwiringPi
# Should not alter anything below this line
###############################################################################

SRC = test1.c test2.c speed.c lcd.c
SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c

OBJ = test1.o test2.o speed.o lcd.o
OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o

all: test1 test2 speed lcd
all: test1 test2 speed lcd wfi piface

test1: test1.o
@echo [link]
@@ -57,13 +57,21 @@ lcd: lcd.o
@echo [link]
$(CC) -o $@ lcd.o $(LDFLAGS) $(LIBS)

wfi: wfi.o
@echo [link]
$(CC) -o $@ wfi.o $(LDFLAGS) $(LIBS) -lpthread

piface: piface.o
@echo [link]
$(CC) -o $@ piface.o $(LDFLAGS) $(LIBS) -lpthread


.c.o:
@echo [CC] $<
@$(CC) -c $(CFLAGS) $< -o $@

clean:
rm -f $(OBJ) *~ core tags test1 test2 speed lcd
rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface

tags: $(SRC)
@echo [ctags]


+ 3
- 3
examples/lcd.c View File

@@ -10,16 +10,16 @@
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/


+ 53
- 0
examples/piface.c View File

@@ -0,0 +1,53 @@

/*
* piface.c:
* Simple test for the PiFace
*
* Read the buttons and output the same to the LEDs
*/

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

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

+ 4
- 2
examples/speed.c View File

@@ -52,7 +52,8 @@ int main (void)

printf ("Native GPIO method: (%8d iterations)\n", FAST_COUNT) ;

wiringPiGpioMode (WPI_MODE_GPIO) ;
if (wiringPiSetupGpio () == -1)
exit (1) ;

pinMode (17, OUTPUT) ;

@@ -77,7 +78,8 @@ int main (void)

// Switch to SYS mode:

wiringPiSetupSys () ;
if (wiringPiSetupSys () == -1)
exit (1) ;

printf ("/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ;



+ 158
- 0
examples/wfi.c View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>

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

+ 165
- 0
gpio/COPYING.LESSER View File

@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
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.

+ 3
- 3
gpio/Makefile View File

@@ -9,16 +9,16 @@
# Wiring Compatable library for the Raspberry Pi
#
# wiringPi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wiringPi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# You should have received a copy of the GNU Lesser General Public License
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
#################################################################################



+ 55
- 21
gpio/gpio.1 View File

@@ -1,15 +1,23 @@
.TH "GPIO" "14 June 2012" "Command-Line access to Raspberry Pi GPIO"
.TH "GPIO" "14 June 2012" "Command-Line access to Raspberry Pi and PiFace GPIO"

.SH NAME
gpio \- Command-line access to Raspberry Pi GPIO
gpio \- Command-line access to Raspberry Pi and PiFace GPIO

.SH SYNOPSIS
.TP
.B gpio
.RB [ \-v ]
.TP
.B gpio
.RB [ \-g ]
.RB < read/write/pwm/mode ...>
.TP
.B gpio
.RB < export/unexport/exports ...>
.RB [ \-p ]
.RB < read/write/mode ...>
.TP
.B gpio
.RB < export/edge/unexport/unexportall/exports ...>

.SH DESCRIPTION

@@ -19,15 +27,23 @@ on the Raspberry Pi. It's designed for simple testing and diagnostic
purposes, but can be used in shell scripts for general if somewhat slow
control of the GPIO pins.

Additionally, it can be used to set the exports in the /sys/class/gpio
system directory to allow subsequent programs to use the /sys/class/gpio
Additionally, it can be used to set the exports in the \fI/sys/class/gpio\fR
system directory to allow subsequent programs to use the \fR/sys/class/gpio\fR
interface without needing to be run as root.

.SH OPTIONS

.TP
.B \-v
Output the current version

.TP
.B \-g
Use the BCM_GPIO pins numbers rather than WiringPi pin numbers.
Use the BCM_GPIO pins numbers rather than wiringPi pin numbers.

.TP
.B \-p
Use the PiFace interface board and its corresponding pin numbers.

.TP
.B read
@@ -44,28 +60,46 @@ Write a PWM value (0-1023) to the given pin.

.TP
.B mode
Set a pin into input, output or pwm mode. Can also use the literals up, down or tri
to set the internal pull-up, pull-down or tristate controls.
Set a pin into \fIinput\fR, \fIoutput\fR or \fIpwm\fR mode. Can also
use the literals \fIup\fR, \fIdown\fR or \fItri\fR to set the internal
pull-up, pull-down or tristate (off) controls.

.TP
.B export
Export a GPIO pin in the /sys/class/gpio directory. Use like the mode command above
however only in and out are supported at this time.
Export a GPIO pin in the \fI/sys/class/gpio\fR directory. Use like the
mode command above however only \fIin\fR and \fIout\fR are supported at
this time. Note that the pin number is the \fBBCM_GPIO\fR number and
not the wiringPi number.

Once a GPIO pin has been exported, the \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.

Once a GPIO pin has been exported, the
.B gpio
program changes the ownership of the /sys/class/gpio/gpioX/value pseudo file to
that of the user running the
.B gpio
program. This means that you can have a small script of gpio exports to setup
the gpio pins as your program requires without the need to run anything as
root, or with the sudo command.
.TP
.B edge
This exports a GPIO pin in the \fI/sys/class/gpio\fR directory, set
the direction to input and set the edge interrupt method to \fInone\fR,
\fIrising\fR, \fIfalling\fR or \fIboth\fR. Use like the export command
above and note that \fBBCM_GPIO\fR pin number is used not not wiringPi pin
numbering.

Like the export commands abovem ownership is set to that of the
calling user, allowing subsequent access from user programs without
requiring root/sudo.

.TP
.B unexport
Un-Export a GPIO pin in the /sys/class/gpio directory.

.TP
.B unexportall
Un-Export all the GPIO pins in the /sys/class/gpio directory.

.TP
.B exports
Print a list (if any) of all the exported GPIO pins and their current values.

@@ -119,9 +153,9 @@ gpio -g read 0 # Read GPIO Pin 0 (SDA0)

.SH "NOTES"

When using the export or unexport commands, the pin numbers are
.B always
native GPIO numbers and never wiringPi pin numbers.
When using the \fIexport\fR, \fIedge\fR or \fIunexport\fR commands, the
pin numbers are \fBalways\fR native BCM_GPIO numbers and never wiringPi
pin numbers.

.SH "SEE ALSO"



+ 198
- 34
gpio/gpio.c View File

@@ -7,16 +7,16 @@
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
@@ -37,10 +37,10 @@

static int wpMode ;


char *usage = "Usage: gpio -v\n"
" gpio [-g] <read/write/pwm/mode/> ...\n"
" gpio <export/unexport/exports> ..." ;
" gpio [-g] <read/write/pwm/mode> ...\n"
" gpio [-p] <read/write/mode> ...\n"
" gpio <export/edge/unexport/unexportall/exports> ..." ;


/*
@@ -54,7 +54,7 @@ void doExports (void)
int fd ;
int i, l, first ;
char fName [128] ;
char dir, val ;
char buf [16] ;

// Rather crude, but who knows what others are up to...

@@ -75,19 +75,14 @@ void doExports (void)

printf ("%4d: ", i) ;

if ((l = read (fd, &dir, 1)) == 0)
{
printf ("Empty direction file (why?)\n") ;
close (fd) ;
continue ;
}
if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ;
buf [l] = 0 ;
if ((buf [strlen (buf) - 1]) == '\n')
buf [strlen (buf) - 1] = 0 ;

/**/ if (dir == 'o')
printf ("Output ") ;
else if (dir == 'i')
printf ("Input ") ;
else
printf ("Wrong ") ;
printf ("%-3s", buf) ;

close (fd) ;

@@ -100,19 +95,32 @@ void doExports (void)
continue ;
}

if ((l = read (fd, &val, 1)) == 0)
if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ;

buf [l] = 0 ;
if ((buf [strlen (buf) - 1]) == '\n')
buf [strlen (buf) - 1] = 0 ;

printf (" %s", buf) ;

// Read any edge trigger file

sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
if ((fd = open (fName, O_RDONLY)) == -1)
{
printf ("Empty Value file (why?)\n") ;
close (fd) ;
printf ("\n") ;
continue ;
}

/**/ if (val == '0' )
printf ("(0)\n") ;
else if (val == '1')
printf ("(1)\n") ;
else
printf ("(?)\n") ;
if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ;

buf [l] = 0 ;
if ((buf [strlen (buf) - 1]) == '\n')
buf [strlen (buf) - 1] = 0 ;

printf (" %-8s\n", buf) ;

close (fd) ;
}
@@ -161,9 +169,9 @@ void doExport (int argc, char *argv [])
exit (1) ;
}

/**/ if (strcasecmp (mode, "in") == 0)
/**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
fprintf (fd, "in\n") ;
else if (strcasecmp (mode, "out") == 0)
else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
fprintf (fd, "out\n") ;
else
{
@@ -184,7 +192,113 @@ void doExport (int argc, char *argv [])
fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
exit (1) ;
}

// Also change ownership of the edge file - if it exists

sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
if (chown (fName, uid, gid) != 0)
{
if (errno != ENOENT) // Silently ignore File not found - older kernel
{
fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
exit (1) ;
}
}

}


/*
* doEdge:
* gpio edge pin mode
* Easy access to changing the edge trigger on a GPIO pin
* This uses the /sys/class/gpio device interface.
*********************************************************************************
*/

void doEdge (int argc, char *argv [])
{
FILE *fd ;
int pin ;
char *mode ;
char fName [128] ;
uid_t uid ;
gid_t gid ;

if (argc != 4)
{
fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
exit (1) ;
}

pin = atoi (argv [2]) ;

mode = argv [3] ;

// Export the pin and set direction to input

if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
exit (1) ;
}

fprintf (fd, "%d\n", pin) ;
fclose (fd) ;

sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
if ((fd = fopen (fName, "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
exit (1) ;
}

fprintf (fd, "in\n") ;
fclose (fd) ;

sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
if ((fd = fopen (fName, "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
exit (1) ;
}

/**/ if (strcasecmp (mode, "none") == 0)
fprintf (fd, "none\n") ;
else if (strcasecmp (mode, "rising") == 0)
fprintf (fd, "rising\n") ;
else if (strcasecmp (mode, "falling") == 0)
fprintf (fd, "falling\n") ;
else if (strcasecmp (mode, "both") == 0)
fprintf (fd, "both\n") ;
else
{
fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
exit (1) ;
}

// Change ownership so the current user can actually use it!

uid = getuid () ;
gid = getgid () ;

sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
if (chown (fName, uid, gid) != 0)
{
fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
exit (1) ;
}

// Also change ownership of the edge file

sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
if (chown (fName, uid, gid) != 0)
{
fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
exit (1) ;
}

fclose (fd) ;
}


@@ -218,6 +332,33 @@ void doUnexport (int argc, char *argv [])
fclose (fd) ;
}


/*
* doUnexportAll:
* gpio unexportall
* Un-Export all the GPIO pins.
* This uses the /sys/class/gpio device interface.
*********************************************************************************
*/

void doUnexportall (int argc, char *argv [])
{
FILE *fd ;
int pin ;

for (pin = 0 ; pin < 63 ; ++pin)
{
if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
exit (1) ;
}
fprintf (fd, "%d\n", pin) ;
fclose (fd) ;
}
}


/*
* doMode:
* gpio mode pin mode ...
@@ -381,11 +522,15 @@ int main (int argc, char *argv [])
// Initial test for /sys/class/gpio operations:

/**/ if (strcasecmp (argv [1], "exports" ) == 0)
{ doExports () ; return 0 ; }
{ doExports () ; return 0 ; }
else if (strcasecmp (argv [1], "export" ) == 0)
{ doExport (argc, argv) ; return 0 ; }
{ doExport (argc, argv) ; return 0 ; }
else if (strcasecmp (argv [1], "edge" ) == 0)
{ doEdge (argc, argv) ; return 0 ; }
else if (strcasecmp (argv [1], "unexportall") == 0)
{ doUnexportall (argc, argv) ; return 0 ; }
else if (strcasecmp (argv [1], "unexport") == 0)
{ doUnexport (argc, argv) ; return 0 ; }
{ doUnexport (argc, argv) ; return 0 ; }

// Check for -g argument

@@ -402,6 +547,25 @@ int main (int argc, char *argv [])
--argc ;
wpMode = WPI_MODE_GPIO ;
}

// Check for -p argument for PiFace

else if (strcasecmp (argv [1], "-p") == 0)
{
if (wiringPiSetupPiFaceForGpioProg () == -1)
{
fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
exit (1) ;
}

for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
wpMode = WPI_MODE_PIFACE ;
}

// Default to wiringPi mode

else
{
if (wiringPiSetup () == -1)


+ 165
- 0
wiringPi/COPYING.LESSER View File

@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
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.

+ 19
- 11
wiringPi/Makefile View File

@@ -8,16 +8,16 @@
# https://projects.drogon.net/raspberry-pi/wiringpi/
#
# wiringPi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wiringPi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# You should have received a copy of the GNU Lesser General Public License
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
#################################################################################

@@ -35,16 +35,17 @@ LIBS =
# Should not alter anything below this line
###############################################################################

SRC = wiringPi.c serial.c wiringShift.c lcd.c
SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c lcd.c piHiPri.c piThread.c

OBJ = wiringPi.o serial.o wiringShift.o lcd.o
OBJ = wiringPi.o wiringPiFace.o wiringSerial.o wiringShift.o lcd.o piHiPri.o piThread.o

all: $(TARGET)

$(TARGET): $(OBJ)
@echo [AR] $(OBJ)
@ar rcs $(TARGET) $(OBJ)
@size $(TARGET)
@ranlib $(TARGET)
@size $(TARGET)

.c.o:
@echo [CC] $<
@@ -64,13 +65,16 @@ install: $(TARGET)
@echo [install]
install -m 0755 -d /usr/local/lib
install -m 0755 -d /usr/local/include
install -m 0644 wiringPi.h /usr/local/include
install -m 0644 serial.h /usr/local/include
install -m 0644 wiringShift.h /usr/local/include
install -m 0644 libwiringPi.a /usr/local/lib
install -m 0644 wiringPi.h /usr/local/include
install -m 0644 wiringSerial.h /usr/local/include
install -m 0644 wiringShift.h /usr/local/include
install -m 0644 lcd.h /usr/local/include
install -m 0644 libwiringPi.a /usr/local/lib

uninstall:
@echo [uninstall]
rm -f /usr/local/include/lcd.h
rm -f /usr/local/include/wiringShift.h
rm -f /usr/local/include/wiringPi.h
rm -f /usr/local/lib/libwiringPi.a

@@ -79,5 +83,9 @@ uninstall:
# DO NOT DELETE

wiringPi.o: wiringPi.h
serial.o: serial.h
wiringPiFace.o: wiringPi.h
wiringSerial.o: wiringSerial.h
wiringShift.o: wiringPi.h wiringShift.h
lcd.o: wiringPi.h lcd.h
piHiPri.o: wiringPi.h
piThread.o: wiringPi.h

+ 3
- 3
wiringPi/lcd.c View File

@@ -10,16 +10,16 @@
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/


+ 3
- 3
wiringPi/lcd.h View File

@@ -10,16 +10,16 @@
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/


+ 50
- 0
wiringPi/piHiPri.c View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <sched.h>
#include <string.h>

#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) ;
}

+ 63
- 0
wiringPi/piThread.c View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <pthread.h>
#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]) ;
}


+ 381
- 252
wiringPi/wiringPi.c View File

@@ -11,21 +11,32 @@
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/

// Revisions:
// 19 Jul 2012:
// Moved to the LGPL
// Added an abstraction layer to the main routines to save a tiny
// bit of run-time and make the clode a little cleaner (if a little
// larger)
// Added waitForInterrupt code
// Added piHiPri code
//
// 9 Jul 2012:
// Added in support to use the /sys/class/gpio interface.
// 2 Jul 2012:
// Fixed a few more bugs to do with range-checking when in GPIO mode.
// 11 Jun 2012:
@@ -45,6 +56,7 @@

#include <stdio.h>
#include <stdint.h>
#include <poll.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
@@ -55,9 +67,17 @@
#include <sys/types.h>
#include <sys/stat.h>

#include "wiringPi.h"

// Function stubs

void (*pinMode) (int pin, int mode) ;
void (*pullUpDnControl) (int pin, int pud) ;
void (*digitalWrite) (int pin, int value) ;
void (*pwmWrite) (int pin, int value) ;
int (*digitalRead) (int pin) ;
int (*waitForInterrupt) (int pin, int mS) ;

#include "wiringPi.h"

#ifndef TRUE
#define TRUE (1==1)
@@ -77,7 +97,7 @@
#define FSEL_ALT5 0b010

// Access from ARM Running Linux
// Take from Gerts code. Some of this is not in the manual
// Take from Gert/Doms code. Some of this is not in the manual
// that I can find )-:

#define BCM2708_PERI_BASE 0x20000000
@@ -143,14 +163,10 @@ static volatile uint32_t *clk ;
// X / 10 + ((X % 10) * 3)

// sysFds:
// Map a file descriptor from the /sys/class/gpio/gpioX/value file
// Map a file descriptor from the /sys/class/gpio/gpioX/value

static int sysFds [64] ;

// Mode

static int gpioPinMode ;

// Doing it the Arduino way with lookup tables...
// Yes, it's probably more innefficient than all the bit-twidling, but it
// does tend to make it all a bit clearer. At least to me!
@@ -158,13 +174,19 @@ static int gpioPinMode ;
// pinToGpio:
// Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin

static int pinToGpio [] =
static int pinToGpio [64] =
{
17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
0, 1, // I2C - SDA0, SCL0
8, 7, // SPI - CE1, CE0
10, 9, 11, // SPI - MOSI, MISO, SCLK
14, 15, // UART - Tx, Rx

// Padding:

-1, -1, -1,-1,-1,-1,-1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
} ;

// gpioToGPFSEL:
@@ -219,6 +241,35 @@ static uint8_t gpioToGPLEV [] =
14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
} ;

#ifdef notYetReady
// gpioToEDS
// (Word) offset to the Event Detect Status

static uint8_t gpioToEDS [] =
{
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
} ;

// gpioToREN
// (Word) offset to the Rising edgde ENable register

static uint8_t gpioToREN [] =
{
19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
} ;

// gpioToFEN
// (Word) offset to the Falling edgde ENable register

static uint8_t gpioToFEN [] =
{
22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
} ;
#endif

// gpioToPUDCLK
// (Word) offset to the Pull Up Down Clock regsiter

@@ -265,17 +316,305 @@ static unsigned long long epoch ;


/*
* wiringPiGpioMode:
* Set the mode - use Pin numbers (0-16) or GPIO number (seemingly random)
* pinMode:
* Sets the mode of a pin to be input, output or PWM output
*********************************************************************************
*/

void 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) = 0x5A000000 | (32<<12) ; // set pwm div to 32 (19.2/3 = 600KHz)
*(clk + PWMCLK_CNTL) = 0x5A000011 ; // Source=osc and enable
digitalWrite (pin, LOW) ;
*(pwm + PWM_CONTROL) = 0 ; // Disable PWM
delayMicroseconds (10) ;
*(pwm + PWM0_RANGE) = 0x400 ;
delayMicroseconds (10) ;
*(pwm + PWM1_RANGE) = 0x400 ;
delayMicroseconds (10) ;

// Enable PWMs

*(pwm + PWM0_DATA) = 512 ;
*(pwm + PWM1_DATA) = 512 ;

*(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
}

}

// When we change mode of any pin, we remove the pull up/downs

pullUpDnControl (pin, PUD_OFF) ;
}

void pinModeWPi (int pin, int mode)
{
pinModeGpio (pinToGpio [pin & 63], mode) ;
}

void pinModeSys (int pin, int mode)
{
return ;
}


#ifdef notYetReady
/*
* pinED01:
* pinED10:
* Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
* Pin must already be in input mode with appropriate pull up/downs set.
*********************************************************************************
*/

void pinEnableED01Pi (int pin)
{
pin = pinToGpio [pin & 63] ;
}
#endif



/*
* digitalWrite:
* Set an output bit
*********************************************************************************
*/

void digitalWriteWPi (int pin, int value)
{
int gpioPin = pinToGpio [pin & 63] ;

if (value == LOW)
*(gpio + gpioToGPCLR [gpioPin]) = 1 << gpioPin ;
else
*(gpio + gpioToGPSET [gpioPin]) = 1 << gpioPin ;
}

void digitalWriteGpio (int pin, int value)
{
pin &= 63 ;

if (value == LOW)
*(gpio + gpioToGPCLR [pin]) = 1 << pin ;
else
*(gpio + gpioToGPSET [pin]) = 1 << pin ;
}

void digitalWriteSys (int pin, int value)
{
pin &= 63 ;

if (sysFds [pin] != -1)
{
if (value == LOW)
write (sysFds [pin], "0\n", 2) ;
else
write (sysFds [pin], "1\n", 2) ;
}
}


/*
* pwnWrite:
* Set an output PWM value
*********************************************************************************
*/

void pwmWriteWPi (int pin, int value)
{
int port, gpioPin ;

gpioPin = pinToGpio [pin & 63] ;
port = gpioToPwmPort [gpioPin] ;

*(pwm + port) = value & 0x3FF ;
}

void pwmWriteGpio (int pin, int value)
{
int port, gpioPin ;

gpioPin = pin & 63 ;
port = gpioToPwmPort [gpioPin] ;

*(pwm + port) = value & 0x3FF ;
}


void pwmWriteSys (int pin, int value)
{
return ;
}


/*
* digitalRead:
* Read the value of a given Pin, returning HIGH or LOW
*********************************************************************************
*/

int digitalReadWPi (int pin)
{
int gpioPin ;

pin &= 63 ;

gpioPin = pinToGpio [pin] ;

if ((*(gpio + gpioToGPLEV [gpioPin]) & (1 << gpioPin)) != 0)
return HIGH ;
else
return LOW ;
}

int digitalReadGpio (int pin)
{
pin &= 63 ;

if ((*(gpio + gpioToGPLEV [pin]) & (1 << pin)) != 0)
return HIGH ;
else
return LOW ;
}

int digitalReadSys (int pin)
{
char c ;

pin &= 63 ;

if (sysFds [pin] == -1)
return 0 ;

lseek (sysFds [pin], 0L, SEEK_SET) ;
read (sysFds [pin], &c, 1) ;
return (c == '0') ? 0 : 1 ;
}


/*
* pullUpDownCtrl:
* Control the internal pull-up/down resistors on a GPIO pin
* The Arduino only has pull-ups and these are enabled by writing 1
* to a port when in input mode - this paradigm doesn't quite apply
* here though.
*********************************************************************************
*/

void pullUpDnControlWPi (int pin, int pud)
{
pin = pinToGpio [pin & 63] ;

*(gpio + 37) = pud ;
delayMicroseconds (10) ;
*(gpio + gpioToPUDCLK [pin]) = 1 << pin ;
delayMicroseconds (10) ;
*(gpio + 37) = 0 ;
*(gpio + gpioToPUDCLK [pin]) = 0 ;
}

void pullUpDnControlGpio (int pin, int pud)
{
pin &= 63 ;

*(gpio + 37) = pud ;
delayMicroseconds (10) ;
*(gpio + gpioToPUDCLK [pin]) = 1 << pin ;
delayMicroseconds (10) ;
*(gpio + 37) = 0 ;
*(gpio + gpioToPUDCLK [pin]) = 0 ;
}

void pullUpDnControlSys (int pin, int pud)
{
return ;
}


/*
* waitForInterrupt:
* Wait for Interrupt on a GPIO pin.
* This is actually done via the /sys/class/gpio interface regardless of
* the wiringPi access mode in-use. Maybe sometime it might get a better
* way for a bit more efficiency.
*********************************************************************************
*/

void wiringPiGpioMode (int mode)
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)
{
gpioPinMode = mode ;
return waitForInterruptSys (pin, mS) ;
}



/*
* wiringPiSetup:
* Must be called once at the start of your program execution.
@@ -296,9 +635,12 @@ int wiringPiSetup (void)
uint32_t *pads ;
#endif

// Set Pin mode by default

wiringPiGpioMode (WPI_MODE_PINS) ;
pinMode = pinModeWPi ;
pullUpDnControl = pullUpDnControlWPi ;
digitalWrite = digitalWriteWPi ;
pwmWrite = pwmWriteWPi ;
digitalRead = digitalReadWPi ;
waitForInterrupt = waitForInterruptWPi ;
// Open the master /dev/memory device

@@ -417,7 +759,13 @@ int wiringPiSetupGpio (void)
if (x != 0)
return x ;

wiringPiGpioMode (WPI_MODE_GPIO) ;
pinMode = pinModeGpio ;
pullUpDnControl = pullUpDnControlGpio ;
digitalWrite = digitalWriteGpio ;
pwmWrite = pwmWriteGpio ;
digitalRead = digitalReadGpio ;
waitForInterrupt = waitForInterruptGpio ;

return 0 ;
}

@@ -433,26 +781,28 @@ int wiringPiSetupGpio (void)

int wiringPiSetupSys (void)
{
int fd, pin ;
int pin ;
struct timeval tv ;
char fName [128] ;

// Set GPIO_SYS mode by default

wiringPiGpioMode (WPI_MODE_GPIO_SYS) ;
pinMode = pinModeSys ;
pullUpDnControl = pullUpDnControlSys ;
digitalWrite = digitalWriteSys ;
pwmWrite = pwmWriteSys ;
digitalRead = digitalReadSys ;
waitForInterrupt = waitForInterruptSys ;

// Open and scan the directory, looking for exported GPIOs, and pre-open
// the 'value' part to speed things up for later
// the 'value' interface to speed things up for later
for (pin = 0 ; pin < 64 ; ++pin)
{
sysFds [pin] = -1 ;
sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
if ((fd = open (fName, O_RDWR)) == -1)
continue ;
sysFds [pin] = fd ;
sysFds [pin] = open (fName, O_RDWR) ;
}

// Initialise the epoch for mills() ...

gettimeofday (&tv, NULL) ;
epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;

@@ -460,228 +810,7 @@ int wiringPiSetupSys (void)
}


/*
* pinMode:
* Sets the mode of a pin to be input, output or PWM output
*********************************************************************************
*/

void pinMode (int pin, int mode)
{
static int pwmRunning = FALSE ;

int gpioPin, fSel, shift ;
int alt ;

// We can't change the mode in GPIO_SYS mode

if (gpioPinMode == WPI_MODE_GPIO_SYS)
return ;

if (gpioPinMode == WPI_MODE_PINS)
{
if ((pin < 0) || (pin >= NUM_PINS))
return ;
gpioPin = pinToGpio [pin] ;
}
else
gpioPin = pin ;

fSel = gpioToGPFSEL [gpioPin] ;
shift = gpioToShift [gpioPin] ;

/**/ if (mode == INPUT)
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
else if (mode == OUTPUT)
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
else if (mode == PWM_OUTPUT)
{
if ((alt = gpioToPwmALT [gpioPin]) == 0) // Not a PWM pin
return ;

// Set pin to PWM mode

*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;

// We didn't initialise the PWM hardware at setup time - because it's possible that
// something else is using the PWM - e.g. the Audio systems! So if we use PWM
// here, then we're assuming that nothing else is, otherwise things are going
// to sound a bit funny...

if (!pwmRunning)
{

// Gert/Doms Values
*(clk + PWMCLK_DIV) = 0x5A000000 | (32<<12) ; // set pwm div to 32 (19.2/3 = 600KHz)
*(clk + PWMCLK_CNTL) = 0x5A000011 ; // Source=osc and enable
digitalWrite (pin, LOW) ;
*(pwm + PWM_CONTROL) = 0 ; // Disable PWM
delayMicroseconds (10) ;
*(pwm + PWM0_RANGE) = 0x400 ;
delayMicroseconds (10) ;
*(pwm + PWM1_RANGE) = 0x400 ;
delayMicroseconds (10) ;

// Enable PWMs

*(pwm + PWM0_DATA) = 512 ;
*(pwm + PWM1_DATA) = 512 ;

*(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
}

}

// When we change mode of any pin, we remove the pull up/downs

pullUpDnControl (pin, PUD_OFF) ;
}


/*
* digitalWrite:
* Set an output bit
*********************************************************************************
*/

void digitalWrite (int pin, int value)
{
int gpioPin ;

if (gpioPinMode == WPI_MODE_PINS)
{
if ((pin < 0) || (pin >= NUM_PINS))
return ;
gpioPin = pinToGpio [pin] ;
}
else
gpioPin = pin ;

if (gpioPinMode == WPI_MODE_GPIO_SYS)
{
if (sysFds [gpioPin] != -1)
{
if (value == LOW)
write (sysFds [gpioPin], "0\n", 2) ;
else
write (sysFds [gpioPin], "1\n", 2) ;
}
}
else
{
if (value == LOW)
*(gpio + gpioToGPCLR [gpioPin]) = 1 << gpioPin ;
else
*(gpio + gpioToGPSET [gpioPin]) = 1 << gpioPin ;
}
}


/*
* pwnWrite:
* Set an output PWM value
*********************************************************************************
*/

void pwmWrite (int pin, int value)
{
int port, gpioPin ;

// We can't do this in GPIO_SYS mode

if (gpioPinMode == WPI_MODE_GPIO_SYS)
return ;

if (gpioPinMode == WPI_MODE_PINS)
{
if ((pin < 0) || (pin >= NUM_PINS))
return ;
gpioPin = pinToGpio [pin] ;
}
else
gpioPin = pin ;

port = gpioToPwmPort [gpioPin] ;

*(pwm + port) = value & ~0x400 ;
}


/*
* digitalRead:
* Read the value of a given Pin, returning HIGH or LOW
*********************************************************************************
*/

int digitalRead (int pin)
{
int gpioPin ;
char c ;

if (gpioPinMode == WPI_MODE_PINS)
{
if ((pin < 0) || (pin >= NUM_PINS))
return 0 ;
gpioPin = pinToGpio [pin] ;
}
else
gpioPin = pin ;

if (gpioPinMode == WPI_MODE_GPIO_SYS)
{
if (sysFds [gpioPin] == -1)
return 0 ;
else
{
lseek (sysFds [gpioPin], 0L, SEEK_SET) ;
read (sysFds [gpioPin], &c, 1) ;
return (c == '0') ? 0 : 1 ;
}
}
else
{
if ((*(gpio + gpioToGPLEV [gpioPin]) & (1 << gpioPin)) != 0)
return HIGH ;
else
return LOW ;
}
}

/*
* pullUpDownCtrl:
* Control the internal pull-up/down resistors on a GPIO pin
* The Arduino only has pull-ups and these are enabled by writing 1
* to a port when in input mode - this paradigm doesn't quite apply
* here though.
*********************************************************************************
*/

void pullUpDnControl (int pin, int pud)
{
int gpioPin ;

// We can't do this in GPIO_SYS mode

if (gpioPinMode == WPI_MODE_GPIO_SYS)
return ;

if (gpioPinMode == WPI_MODE_PINS)
{
if ((pin < 0) || (pin >= NUM_PINS))
return ;
gpioPin = pinToGpio [pin] ;
}
else
gpioPin = pin ;

*(gpio + 37) = pud ;
delayMicroseconds (10) ;
*(gpio + gpioToPUDCLK [gpioPin]) = 1 << gpioPin ;
delayMicroseconds (10) ;
*(gpio + 37) = 0 ;
*(gpio + gpioToPUDCLK [gpioPin]) = 0 ;
}


/*


+ 35
- 12
wiringPi/wiringPi.h View File

@@ -7,16 +7,16 @@
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
@@ -28,6 +28,7 @@
#define WPI_MODE_PINS 0
#define WPI_MODE_GPIO 1
#define WPI_MODE_GPIO_SYS 2
#define WPI_MODE_PIFACE 3

#define INPUT 0
#define OUTPUT 1
@@ -48,17 +49,39 @@
extern "C" {
#endif

extern int wiringPiSetup (void) ;
extern int wiringPiSetupSys (void) ;
extern int wiringPiSetupGpio (void) ;
// Basic wiringPi functions

extern void wiringPiGpioMode (int mode) ;
extern int wiringPiSetup (void) ;
extern int wiringPiSetupSys (void) ;
extern int wiringPiSetupGpio (void) ;
extern int wiringPiSetupPiFace (void) ;

extern void pullUpDnControl (int pin, int pud) ;
extern void pinMode (int pin, int mode) ;
extern void digitalWrite (int pin, int value) ;
extern void pwmWrite (int pin, int value) ;
extern int digitalRead (int pin) ;
extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only

extern void (*pinMode) (int pin, int mode) ;
extern void (*pullUpDnControl) (int pin, int pud) ;
extern void (*digitalWrite) (int pin, int value) ;
extern void (*pwmWrite) (int pin, int value) ;
extern int (*digitalRead) (int pin) ;

// Interrupts

extern int (*waitForInterrupt) (int pin, int mS) ;

// Threads

#define PI_THREAD(X) void *X (void *dummy)

extern int piThreadCreate (void *(*fn)(void *)) ;
extern void piLock (int key) ;
extern void piUnlock (int key) ;

// Schedulling priority

extern int piHiPri (int pri) ;


// Extras from arduino land

extern void delay (unsigned int howLong) ;
extern void delayMicroseconds (unsigned int howLong) ;


+ 355
- 0
wiringPi/wiringPiFace.c View File

@@ -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 <http://www.gnu.org/licenses/>.
***********************************************************************
*/


#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/spi/spidev.h>

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

+ 206
- 0
wiringPi/wiringSerial.c View File

@@ -0,0 +1,206 @@
/*
* wiringSerial.c:
* Handle a serial port
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#undef DEBUG

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "wiringSerial.h"

/*
* serialOpen:
* Open and initialise the serial port, setting all the right
* port parameters - or as many as are required - hopefully!
*********************************************************************************
*/

int serialOpen (char *device, int baud)
{
struct termios options ;
speed_t myBaud ;
int status, fd ;

#ifdef DEBUG
printf ("openSerialPort: <%s> baud: $d\n", device, baud) ;
#endif

switch (baud)
{
case 50: myBaud = B50 ; break ;
case 75: myBaud = B75 ; break ;
case 110: myBaud = B110 ; break ;
case 134: myBaud = B134 ; break ;
case 150: myBaud = B150 ; break ;
case 200: myBaud = B200 ; break ;
case 300: myBaud = B300 ; break ;
case 600: myBaud = B600 ; break ;
case 1200: myBaud = B1200 ; break ;
case 1800: myBaud = B1800 ; break ;
case 2400: myBaud = B2400 ; break ;
case 9600: myBaud = B9600 ; break ;
case 19200: myBaud = B19200 ; break ;
case 38400: myBaud = B38400 ; break ;
case 57600: myBaud = B57600 ; break ;
case 115200: myBaud = B115200 ; break ;
case 230400: myBaud = B230400 ; break ;

default:
return -2 ;
}

if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1 ;

fcntl (fd, F_SETFL, O_RDWR) ;

// Get and modify current options:

tcgetattr (fd, &options) ;

cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;

options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;

options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)

tcsetattr (fd, TCSANOW, &options) ;

ioctl (fd, TIOCMGET, &status);

status |= TIOCM_DTR ;
status |= TIOCM_RTS ;

ioctl (fd, TIOCMSET, &status);

usleep (10000) ; // 10mS

return fd ;
}


/*
* serialClose:
* Release the serial port
*********************************************************************************
*/

void serialClose (int fd)
{
close (fd) ;
}


/*
* serialPutchar:
* Send a single character to the serial port
*********************************************************************************
*/

void serialPutchar (int fd, unsigned char c)
{
write (fd, &c, 1) ;
}


/*
* serialPuts:
* Send a string to the serial port
*********************************************************************************
*/

void serialPuts (int fd, char *s)
{
write (fd, s, strlen (s)) ;
}

/*
* serialPrintf:
* Printf over Serial
*********************************************************************************
*/

void serialPrintf (int fd, char *message, ...)
{
va_list argp ;
char buffer [1024] ;

va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;

serialPuts (fd, buffer) ;
}


/*
* serialDataAvail:
* Return the number of bytes of data avalable to be read in the serial port
*********************************************************************************
*/

int serialDataAvail (int fd)
{
int result ;

if (ioctl (fd, FIONREAD, &result) == -1)
return -1 ;

return result ;
}


/*
* serialGetchar:
* Get a single character from the serial device.
* Note: Zero is a valid character and this function will time-out after
* 10 seconds.
*********************************************************************************
*/

int serialGetchar (int fd)
{
uint8_t x ;

if (read (fd, &x, 1) != 1)
return -1 ;

return ((int)x) & 0xFF ;
}

+ 37
- 0
wiringPi/wiringSerial.h View File

@@ -0,0 +1,37 @@
/*
* wiringSerial.h:
* Handle a serial port
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#ifdef __cplusplus
extern "C" {
#endif

extern int serialOpen (char *device, int baud) ;
extern void serialClose (int fd) ;
extern void serialPutchar (int fd, unsigned char c) ;
extern void serialPuts (int fd, char *s) ;
extern void serialPrintf (int fd, char *message, ...) ;
extern int serialDataAvail (int fd) ;
extern int serialGetchar (int fd) ;

#ifdef __cplusplus
}
#endif

+ 3
- 36
wiringPi/wiringShift.c View File

@@ -8,16 +8,16 @@
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
@@ -82,36 +82,3 @@ void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val)
digitalWrite (cPin, LOW) ;
}
}


/*
* shiftOut:
* Shift data out to a clocked source
*********************************************************************************
*/

void shiftOutWithDelay (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val, int delay)
{
int8_t i;

if (order == MSBFIRST)
for (i = 7 ; i >= 0 ; --i)
{
digitalWrite (dPin, val & (1 << i)) ;
delayMicroseconds (delay) ;
digitalWrite (cPin, HIGH) ;
delayMicroseconds (delay) ;
digitalWrite (cPin, LOW) ;
delayMicroseconds (delay) ;
}
else
for (i = 0 ; i < 8 ; ++i)
{
digitalWrite (dPin, val & (1 << i)) ;
delayMicroseconds (delay) ;
digitalWrite (cPin, HIGH) ;
delayMicroseconds (delay) ;
digitalWrite (cPin, LOW) ;
delayMicroseconds (delay) ;
}
}

+ 3
- 4
wiringPi/wiringShift.h View File

@@ -8,16 +8,16 @@
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
@@ -35,7 +35,6 @@ extern "C" {

extern uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order) ;
extern void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) ;
extern void shiftOutWithDelay (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val, int delay) ;

#ifdef __cplusplus
}


Loading…
Cancel
Save