Updated to new WiringPi

This commit is contained in:
Philip Howard
2013-05-21 22:25:01 +01:00
parent 97a4f5131e
commit 17c28c2a48
107 changed files with 9837 additions and 1448 deletions

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.

View File

@@ -35,7 +35,7 @@ DYNAMIC=libwiringPi.so.$(VERSION)
DEBUG = -O2
CC = gcc
INCLUDE = -I.
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe -fPIC
CFLAGS = $(DEBUG) -Wformat=2 -Wall $(INCLUDE) -Winline -pipe -fPIC
LIBS =
@@ -47,10 +47,12 @@ SRC = wiringPi.c \
piHiPri.c piThread.c \
wiringPiSPI.c wiringPiI2C.c \
softPwm.c softTone.c \
mcp23s08.c mcp23008.c \
mcp23s17.c mcp23017.c sr595.c \
piFace.c gertboard.c \
piNes.c ds1302.c lcd.c
mcp23008.c mcp23016.c mcp23017.c \
mcp23s08.c mcp23s17.c \
sr595.c \
pcf8574.c pcf8591.c \
mcp3002.c mcp4802.c mcp3422.c \
drc.c
OBJ = $(SRC:.c=.o)
@@ -74,42 +76,50 @@ $(DYNAMIC): $(OBJ)
.PHONEY: clean
clean:
rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.*
@echo "[Clean]"
@rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.*
.PHONEY: tags
tags: $(SRC)
@echo [ctags]
@ctags $(SRC)
.PHONEY: install
install: $(DYNAMIC)
@echo "[Install]"
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
.PHONEY: install-headers
install-headers:
@echo "[Install Headers]"
@install -m 0755 -d $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringPi.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringSerial.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringShift.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 gertboard.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 piFace.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 piNes.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 ds1302.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 softPwm.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 mcp23008.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 mcp23016.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 mcp3002.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 mcp4802.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 mcp3422.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 sr595.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 pcf8574.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 pcf8591.h $(DESTDIR)$(PREFIX)/include
.PHONEY: install
install: $(DYNAMIC) install-headers
@echo "[Install Dynamic Lib]"
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
@install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION)
@ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so
@ldconfig
.PHONEY: install-static
install-static: $(STATIC)
@echo "[Install Static]"
install-static: $(STATIC) install-headers
@echo "[Install Static Lib]"
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
@install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib
.PHONEY: uninstall
@@ -118,20 +128,21 @@ uninstall:
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPi.h
@rm -f $(DESTDIR)$(PREFIX)/include/wiringSerial.h
@rm -f $(DESTDIR)$(PREFIX)/include/wiringShift.h
@rm -f $(DESTDIR)$(PREFIX)/include/gertboard.h
@rm -f $(DESTDIR)$(PREFIX)/include/piFace.h
@rm -f $(DESTDIR)$(PREFIX)/include/piNes.h
@rm -f $(DESTDIR)$(PREFIX)/include/ds1302.h
@rm -f $(DESTDIR)$(PREFIX)/include/softPwm.h
@rm -f $(DESTDIR)$(PREFIX)/include/softTone.h
@rm -f $(DESTDIR)$(PREFIX)/include/lcd.h
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23008.h
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23016.h
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h
@rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3422.h
@rm -f $(DESTDIR)$(PREFIX)/include/sr595.h
@rm -f $(DESTDIR)$(PREFIX)/include/pcf8574.h
@rm -f $(DESTDIR)$(PREFIX)/include/pcf8591.h
@rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.*
@ldconfig
@@ -147,17 +158,19 @@ wiringSerial.o: wiringSerial.h
wiringShift.o: wiringPi.h wiringShift.h
piHiPri.o: wiringPi.h
piThread.o: wiringPi.h
wiringPiSPI.o: wiringPiSPI.h
wiringPiSPI.o: wiringPi.h wiringPiSPI.h
wiringPiI2C.o: wiringPi.h wiringPiI2C.h
softPwm.o: wiringPi.h softPwm.h
softTone.o: wiringPi.h softTone.h
mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h
mcp23008.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23008.h
mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h
mcp23016.o: wiringPi.h wiringPiI2C.h mcp23016.h mcp23016reg.h
mcp23017.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23017.h
mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h
mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h
sr595.o: wiringPi.h sr595.h
piFace.o: wiringPi.h wiringPiSPI.h piFace.h mcp23x0817.h
gertboard.o: wiringPi.h wiringPiSPI.h gertboard.h
piNes.o: wiringPi.h piNes.h
ds1302.o: wiringPi.h ds1302.h
lcd.o: wiringPi.h lcd.h
pcf8574.o: wiringPi.h wiringPiI2C.h pcf8574.h
pcf8591.o: wiringPi.h wiringPiI2C.h pcf8591.h
mcp3002.o: wiringPi.h wiringPiSPI.h mcp3002.h
mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h
mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h
drc.o: wiringPi.h wiringSerial.h drc.h

203
WiringPi/wiringPi/drc.c Normal file
View File

@@ -0,0 +1,203 @@
/*
* drc.c:
* Extend wiringPi with the DRC control protocll to Arduino
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include "wiringPi.h"
#include "wiringSerial.h"
#include "drc.h"
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
/*
* myPinMode:
* Change the pin mode on the remote DRC device
*********************************************************************************
*/
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
{
/**/ if (mode == OUTPUT)
serialPutchar (node->fd, 'o') ; // Input
else if (mode == PWM_OUTPUT)
serialPutchar (node->fd, 'p') ; // PWM
else
serialPutchar (node->fd, 'i') ; // Default to input
serialPutchar (node->fd, pin - node->pinBase) ;
}
/*
* myPullUpDnControl:
* ATmegas only have pull-up's on of off. No pull-downs.
*********************************************************************************
*/
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
{
// Force pin into input mode
serialPutchar (node->fd, 'i' ) ;
serialPutchar (node->fd, pin) ;
/**/ if (mode == PUD_UP)
{
serialPutchar (node->fd, '1') ;
serialPutchar (node->fd, pin - node->pinBase) ;
}
else if (mode == PUD_OFF)
{
serialPutchar (node->fd, '0') ;
serialPutchar (node->fd, pin - node->pinBase) ;
}
}
/*
* myDigitalWrite:
*********************************************************************************
*/
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
serialPutchar (node->fd, value == 0 ? '0' : '1') ;
serialPutchar (node->fd, pin - node->pinBase) ;
}
/*
* myPwmWrite:
*********************************************************************************
*/
static void myPwmWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
serialPutchar (node->fd, 'v') ;
serialPutchar (node->fd, pin - node->pinBase) ;
serialPutchar (node->fd, value & 0xFF) ;
}
/*
* myAnalogRead:
*********************************************************************************
*/
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
{
int vHi, vLo ;
serialPutchar (node->fd, 'a') ;
serialPutchar (node->fd, pin - node->pinBase) ;
vHi = serialGetchar (node->fd) ;
vLo = serialGetchar (node->fd) ;
return (vHi << 8) | vLo ;
}
/*
* myDigitalRead:
*********************************************************************************
*/
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
{
serialPutchar (node->fd, 'r') ; // Send read command
serialPutchar (node->fd, pin - node->pinBase) ;
return (serialGetchar (node->fd) == '0') ? 0 : 1 ;
}
/*
* drcSetup:
* Create a new instance of an DRC GPIO interface.
* Could be a variable nunber of pins here - we might not know in advance
* if it's an ATmega with 14 pins, or something with less or more!
*********************************************************************************
*/
int drcSetup (const int pinBase, const int numPins, const char *device)
{
int fd ;
int ok, tries ;
time_t then ;
struct wiringPiNodeStruct *node ;
if ((fd = serialOpen (device, 115200)) < 0)
return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ;
delay (10) ; // May need longer if it's an Uno that reboots on the open...
// Flush any pending input
while (serialDataAvail (fd))
(void)serialGetchar (fd) ;
ok = FALSE ;
for (tries = 1 ; tries < 5 ; ++tries)
{
serialPutchar (fd, '@') ;
then = time (NULL) + 2 ;
while (time (NULL) < then)
if (serialDataAvail (fd))
{
if (serialGetchar (fd) == '@')
{
ok = TRUE ;
break ;
}
}
if (ok)
break ;
}
if (!ok)
{
serialClose (fd) ;
return wiringPiFailure (WPI_FATAL, "Unable to communidate with DRC device") ;
}
node = wiringPiNewNode (pinBase, numPins) ;
node->fd = fd ;
node->pinMode = myPinMode ;
node->pullUpDnControl = myPullUpDnControl ;
node->analogRead = myAnalogRead ;
node->digitalRead = myDigitalRead ;
node->digitalWrite = myDigitalWrite ;
node->pwmWrite = myPwmWrite ;
return 0 ;
}

View File

@@ -1,11 +1,7 @@
/*
* gertboard.h:
* Access routines for the SPI devices on the Gertboard
* Copyright (c) 2012 Gordon Henderson
*
* The Gertboard has an MCP4802 dual-channel D to A convertor
* connected to the SPI bus, selected via chip-select B.
*
* drc.h:
* Extend wiringPi with the DRC control protocll to Arduino
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -30,10 +26,8 @@
extern "C" {
#endif
extern void gertboardAnalogWrite (int chan, int value) ;
extern int gertboardAnalogRead (int chan) ;
extern int gertboardSPISetup (void) ;
extern int gertboardAnalogSetup (int pinBase) ;
int drcSetup (const int pinBase, const int numPins, const char *device) ;
#ifdef __cplusplus
}

View File

@@ -1,239 +0,0 @@
/*
* ds1302.c:
* Real Time clock
*
* Copyright (c) 2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include "wiringPi.h"
#include "ds1302.h"
// Register defines
#define RTC_SECS 0
#define RTC_MINS 1
#define RTC_HOURS 2
#define RTC_DATE 3
#define RTC_MONTH 4
#define RTC_DAY 5
#define RTC_YEAR 6
#define RTC_WP 7
#define RTC_TC 8
#define RTC_BM 31
// Locals
static int dPin, cPin, sPin ;
/*
* dsShiftIn:
* Shift a number in from the chip, LSB first. Note that the data is
* sampled on the trailing edge of the last clock, so it's valid immediately.
*********************************************************************************
*/
unsigned int dsShiftIn (void)
{
uint8_t value = 0 ;
int i ;
pinMode (dPin, INPUT) ; delayMicroseconds (1) ;
for (i = 0 ; i < 8 ; ++i)
{
value |= (digitalRead (dPin) << i) ;
digitalWrite (cPin, HIGH) ; delayMicroseconds (1) ;
digitalWrite (cPin, LOW) ; delayMicroseconds (1) ;
}
return value;
}
/*
* dsShiftOut:
* A normal LSB-first shift-out, just slowed down a bit - the Pi is
* a bit faster than the chip can handle.
*********************************************************************************
*/
void dsShiftOut (unsigned int data)
{
int i ;
pinMode (dPin, OUTPUT) ;
for (i = 0 ; i < 8 ; ++i)
{
digitalWrite (dPin, data & (1 << i)) ; delayMicroseconds (1) ;
digitalWrite (cPin, HIGH) ; delayMicroseconds (1) ;
digitalWrite (cPin, LOW) ; delayMicroseconds (1) ;
}
}
/*
* ds1302regRead: ds1302regWrite:
* Read/Write a value to an RTC Register or RAM location on the chip
*********************************************************************************
*/
static unsigned int ds1302regRead (int reg)
{
unsigned int data ;
digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
dsShiftOut (reg) ;
data = dsShiftIn () ;
digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
return data ;
}
static void ds1302regWrite (int reg, unsigned int data)
{
digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
dsShiftOut (reg) ;
dsShiftOut (data) ;
digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
}
/*
* ds1302rtcWrite: ds1302rtcRead:
* Writes/Reads the data to/from the RTC register
*********************************************************************************
*/
unsigned int ds1302rtcRead (int reg)
{
return ds1302regRead (0x81 | ((reg & 0x1F) << 1)) ;
}
void ds1302rtcWrite (int reg, unsigned int data)
{
ds1302regWrite (0x80 | ((reg & 0x1F) << 1), data) ;
}
/*
* ds1302ramWrite: ds1302ramRead:
* Writes/Reads the data to/from the RTC register
*********************************************************************************
*/
unsigned int ds1302ramRead (int addr)
{
return ds1302regRead (0xC1 | ((addr & 0x1F) << 1)) ;
}
void ds1302ramWrite (int addr, unsigned int data)
{
ds1302regWrite ( 0xC0 | ((addr & 0x1F) << 1), data) ;
}
/*
* ds1302clockRead:
* Read all 8 bytes of the clock in a single operation
*********************************************************************************
*/
void ds1302clockRead (int clockData [8])
{
int i ;
unsigned int regVal = 0x81 | ((RTC_BM & 0x1F) << 1) ;
digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
dsShiftOut (regVal) ;
for (i = 0 ; i < 8 ; ++i)
clockData [i] = dsShiftIn () ;
digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
}
/*
* ds1302clockWrite:
* Write all 8 bytes of the clock in a single operation
*********************************************************************************
*/
void ds1302clockWrite (int clockData [8])
{
int i ;
unsigned int regVal = 0x80 | ((RTC_BM & 0x1F) << 1) ;
digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
dsShiftOut (regVal) ;
for (i = 0 ; i < 8 ; ++i)
dsShiftOut (clockData [i]) ;
digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
}
/*
* ds1302trickleCharge:
* Set the bits on the trickle charger.
* Probably best left alone...
*********************************************************************************
*/
void ds1302trickleCharge (int diodes, int resistors)
{
if (diodes + resistors == 0)
ds1302rtcWrite (RTC_TC, 0x5C) ; // Disabled
else
ds1302rtcWrite (RTC_TC, 0xA0 | ((diodes & 3) << 2) | (resistors & 3)) ;
}
/*
* ds1302setup:
* Initialise the chip & remember the pins we're using
*********************************************************************************
*/
void ds1302setup (int clockPin, int dataPin, int csPin)
{
dPin = dataPin ;
cPin = clockPin ;
sPin = csPin ;
digitalWrite (dPin, LOW) ;
digitalWrite (cPin, LOW) ;
digitalWrite (sPin, LOW) ;
pinMode (dPin, OUTPUT) ;
pinMode (cPin, OUTPUT) ;
pinMode (sPin, OUTPUT) ;
ds1302rtcWrite (RTC_WP, 0) ; // Remove write-protect
}

View File

@@ -1,44 +0,0 @@
/*
* ds1302.h:
* Real Time clock
*
* Copyright (c) 2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#ifdef __cplusplus
extern "C" {
#endif
extern unsigned int ds1302rtcRead (int reg) ;
extern void ds1302rtcWrite (int reg, unsigned int data) ;
extern unsigned int ds1302ramRead (int addr) ;
extern void ds1302ramWrite (int addr, unsigned int data) ;
extern void ds1302clockRead (int clockData [8]) ;
extern void ds1302clockWrite (int clockData [8]) ;
extern void ds1302trickleCharge (int diodes, int resistors) ;
extern void ds1302setup (int clockPin, int dataPin, int csPin) ;
#ifdef __cplusplus
}
#endif

View File

@@ -1,166 +0,0 @@
/*
* gertboard.c:
* Access routines for the SPI devices on the Gertboard
* Copyright (c) 2012 Gordon Henderson
*
* The Gertboard has:
*
* An MCP3002 dual-channel A to D convertor connected
* to the SPI bus, selected by chip-select A, and:
*
* An MCP4802 dual-channel D to A convertor connected
* to the SPI bus, selected via chip-select B.
*
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include "wiringPi.h"
#include "wiringPiSPI.h"
#include "gertboard.h"
// The A-D convertor won't run at more than 1MHz @ 3.3v
#define SPI_ADC_SPEED 1000000
#define SPI_DAC_SPEED 1000000
#define SPI_A2D 0
#define SPI_D2A 1
/*
* gertboardAnalogWrite:
* Write an 8-bit data value to the MCP4802 Analog to digital
* convertor on the Gertboard.
*********************************************************************************
*/
void gertboardAnalogWrite (int chan, int value)
{
uint8_t spiData [2] ;
uint8_t chanBits, dataBits ;
if (chan == 0)
chanBits = 0x30 ;
else
chanBits = 0xB0 ;
chanBits |= ((value >> 4) & 0x0F) ;
dataBits = ((value << 4) & 0xF0) ;
spiData [0] = chanBits ;
spiData [1] = dataBits ;
wiringPiSPIDataRW (SPI_D2A, spiData, 2) ;
}
/*
* gertboardAnalogRead:
* Return the analog value of the given channel (0/1).
* The A/D is a 10-bit device
*********************************************************************************
*/
int gertboardAnalogRead (int chan)
{
uint8_t spiData [2] ;
uint8_t chanBits ;
if (chan == 0)
chanBits = 0b11010000 ;
else
chanBits = 0b11110000 ;
spiData [0] = chanBits ;
spiData [1] = 0 ;
wiringPiSPIDataRW (SPI_A2D, spiData, 2) ;
return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
}
/*
* gertboardSPISetup:
* Initialise the SPI bus, etc.
*********************************************************************************
*/
int gertboardSPISetup (void)
{
if (wiringPiSPISetup (SPI_A2D, SPI_ADC_SPEED) < 0)
return -1 ;
if (wiringPiSPISetup (SPI_D2A, SPI_DAC_SPEED) < 0)
return -1 ;
return 0 ;
}
/*
* New wiringPi node extension methods.
*********************************************************************************
*/
int gbWiringPiAnalogRead (struct wiringPiNodeStruct *node, int chan)
{
chan -= node->pinBase ;
return gertboardAnalogRead (chan) ;
}
void gbWiringPiAnalogWrite (struct wiringPiNodeStruct *node, int chan, int value)
{
chan -= node->pinBase ;
gertboardAnalogWrite (chan, value) ;
}
/*
* gertboardAnalogSetup:
* Create a new wiringPi device node for the analog devices on the
* Gertboard. We create one node with 2 pins - each pin being read
* and write - although the operations actually go to different
* hardware devices.
*********************************************************************************
*/
int gertboardAnalogSetup (int pinBase)
{
struct wiringPiNodeStruct *node ;
int x ;
if (( x = gertboardSPISetup ()) != 0)
return x;
node = wiringPiNewNode (pinBase, 2) ;
node->analogRead = gbWiringPiAnalogRead ;
node->analogWrite = gbWiringPiAnalogWrite ;
return 0 ;
}

View File

@@ -1,380 +0,0 @@
/*
* lcd.c:
* Text-based LCD driver.
* This is designed to drive the parallel interface LCD drivers
* based in the Hitachi HD44780U controller and compatables.
*
* Copyright (c) 2012 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include "wiringPi.h"
#include "lcd.h"
// Commands
#define LCD_CLEAR 0x01
#define LCD_HOME 0x02
#define LCD_ENTRY 0x04
#define LCD_ON_OFF 0x08
#define LCD_CDSHIFT 0x10
#define LCD_FUNC 0x20
#define LCD_CGRAM 0x40
#define LCD_DGRAM 0x80
#define LCD_ENTRY_SH 0x01
#define LCD_ENTRY_ID 0x02
#define LCD_ON_OFF_B 0x01
#define LCD_ON_OFF_C 0x02
#define LCD_ON_OFF_D 0x04
#define LCD_FUNC_F 0x04
#define LCD_FUNC_N 0x08
#define LCD_FUNC_DL 0x10
#define LCD_CDSHIFT_RL 0x04
struct lcdDataStruct
{
uint8_t bits, rows, cols ;
uint8_t rsPin, strbPin ;
uint8_t dataPins [8] ;
} ;
struct lcdDataStruct *lcds [MAX_LCDS] ;
/*
* strobe:
* Toggle the strobe (Really the "E") pin to the device.
* According to the docs, data is latched on the falling edge.
*********************************************************************************
*/
static void strobe (struct lcdDataStruct *lcd)
{
// Note timing changes for new version of delayMicroseconds ()
digitalWrite (lcd->strbPin, 1) ; delayMicroseconds (50) ;
digitalWrite (lcd->strbPin, 0) ; delayMicroseconds (50) ;
}
/*
* sentDataCmd:
* Send an data or command byte to the display.
*********************************************************************************
*/
static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data)
{
uint8_t i, d4 ;
if (lcd->bits == 4)
{
d4 = (data >> 4) & 0x0F;
for (i = 0 ; i < 4 ; ++i)
{
digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
d4 >>= 1 ;
}
strobe (lcd) ;
d4 = data & 0x0F ;
for (i = 0 ; i < 4 ; ++i)
{
digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
d4 >>= 1 ;
}
}
else
{
for (i = 0 ; i < 8 ; ++i)
{
digitalWrite (lcd->dataPins [i], (data & 1)) ;
data >>= 1 ;
}
}
strobe (lcd) ;
}
/*
* putCommand:
* Send a command byte to the display
*********************************************************************************
*/
static void putCommand (struct lcdDataStruct *lcd, uint8_t command)
{
digitalWrite (lcd->rsPin, 0) ;
sendDataCmd (lcd, command) ;
}
static void put4Command (struct lcdDataStruct *lcd, uint8_t command)
{
uint8_t i ;
digitalWrite (lcd->rsPin, 0) ;
for (i = 0 ; i < 4 ; ++i)
{
digitalWrite (lcd->dataPins [i], (command & 1)) ;
command >>= 1 ;
}
strobe (lcd) ;
}
/*
*********************************************************************************
* User Code below here
*********************************************************************************
*/
/*
* lcdHome: lcdClear:
* Home the cursor or clear the screen.
*********************************************************************************
*/
void lcdHome (int fd)
{
struct lcdDataStruct *lcd = lcds [fd] ;
putCommand (lcd, LCD_HOME) ;
}
void lcdClear (int fd)
{
struct lcdDataStruct *lcd = lcds [fd] ;
putCommand (lcd, LCD_CLEAR) ;
}
/*
* lcdSendCommand:
* Send any arbitary command to the display
*********************************************************************************
*/
void lcdSendCommand (int fd, uint8_t command)
{
struct lcdDataStruct *lcd = lcds [fd] ;
putCommand (lcd, command) ;
}
/*
* lcdPosition:
* Update the position of the cursor on the display
*********************************************************************************
*/
void lcdPosition (int fd, int x, int y)
{
static uint8_t rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ;
struct lcdDataStruct *lcd = lcds [fd] ;
putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ;
}
/*
* lcdPutchar:
* Send a data byte to be displayed on the display
*********************************************************************************
*/
void lcdPutchar (int fd, uint8_t data)
{
struct lcdDataStruct *lcd = lcds [fd] ;
digitalWrite (lcd->rsPin, 1) ;
sendDataCmd (lcd, data) ;
}
/*
* lcdPuts:
* Send a string to be displayed on the display
*********************************************************************************
*/
void lcdPuts (int fd, char *string)
{
while (*string)
lcdPutchar (fd, *string++) ;
}
/*
* lcdPrintf:
* Printf to an LCD display
*********************************************************************************
*/
void lcdPrintf (int fd, char *message, ...)
{
va_list argp ;
char buffer [1024] ;
va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;
lcdPuts (fd, buffer) ;
}
/*
* lcdInit:
* Take a lot of parameters and initialise the LCD, and return a handle to
* that LCD, or -1 if any error.
*********************************************************************************
*/
int lcdInit (int rows, int cols, int bits, int rs, int strb,
int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
{
static int initialised = 0 ;
uint8_t func ;
int i ;
int lcdFd = -1 ;
struct lcdDataStruct *lcd ;
if (initialised == 0)
{
initialised = 1 ;
for (i = 0 ; i < MAX_LCDS ; ++i)
lcds [i] = NULL ;
}
// Simple sanity checks
if (! ((bits == 4) || (bits == 8)))
return -1 ;
if ((rows < 0) || (rows > 20))
return -1 ;
if ((cols < 0) || (cols > 20))
return -1 ;
// Create a new LCD:
for (i = 0 ; i < MAX_LCDS ; ++i)
{
if (lcds [i] == NULL)
{
lcdFd = i ;
break ;
}
}
if (lcdFd == -1)
return -1 ;
lcd = malloc (sizeof (struct lcdDataStruct)) ;
if (lcd == NULL)
return -1 ;
lcd->rsPin = rs ;
lcd->strbPin = strb ;
lcd->bits = 8 ; // For now - we'll set it properly later.
lcd->rows = rows ;
lcd->cols = cols ;
lcd->dataPins [0] = d0 ;
lcd->dataPins [1] = d1 ;
lcd->dataPins [2] = d2 ;
lcd->dataPins [3] = d3 ;
lcd->dataPins [4] = d4 ;
lcd->dataPins [5] = d5 ;
lcd->dataPins [6] = d6 ;
lcd->dataPins [7] = d7 ;
lcds [lcdFd] = lcd ;
digitalWrite (lcd->rsPin, 0) ; pinMode (lcd->rsPin, OUTPUT) ;
digitalWrite (lcd->strbPin, 0) ; pinMode (lcd->strbPin, OUTPUT) ;
for (i = 0 ; i < bits ; ++i)
{
digitalWrite (lcd->dataPins [i], 0) ;
pinMode (lcd->dataPins [i], OUTPUT) ;
}
delay (35) ; // mS
// 4-bit mode?
// OK. This is a PIG and it's not at all obvious from the documentation I had,
// so I guess some others have worked through either with better documentation
// or more trial and error... Anyway here goes:
//
// It seems that the controller needs to see the FUNC command at least 3 times
// consecutively - in 8-bit mode. If you're only using 8-bit mode, then it appears
// that you can get away with one func-set, however I'd not rely on it...
//
// So to set 4-bit mode, you need to send the commands one nibble at a time,
// the same three times, but send the command to set it into 8-bit mode those
// three times, then send a final 4th command to set it into 4-bit mode, and only
// then can you flip the switch for the rest of the library to work in 4-bit
// mode which sends the commands as 2 x 4-bit values.
if (bits == 4)
{
func = LCD_FUNC | LCD_FUNC_DL ; // Set 8-bit mode 3 times
put4Command (lcd, func >> 4) ; delay (35) ;
put4Command (lcd, func >> 4) ; delay (35) ;
put4Command (lcd, func >> 4) ; delay (35) ;
func = LCD_FUNC ; // 4th set: 4-bit mode
put4Command (lcd, func >> 4) ; delay (35) ;
lcd->bits = 4 ;
}
else
{
func = LCD_FUNC | LCD_FUNC_DL ;
putCommand (lcd, func ) ; delay (35) ;
putCommand (lcd, func ) ; delay (35) ;
putCommand (lcd, func ) ; delay (35) ;
}
if (lcd->rows > 1)
{
func |= LCD_FUNC_N ;
putCommand (lcd, func) ; delay (35) ;
}
// Rest of the initialisation sequence
putCommand (lcd, LCD_ON_OFF | LCD_ON_OFF_D) ; delay (2) ;
putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ; delay (2) ;
putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ; delay (2) ;
putCommand (lcd, LCD_CLEAR) ; delay (5) ;
return lcdFd ;
}

View File

@@ -1,46 +0,0 @@
/*
* lcd.h:
* Text-based LCD driver.
* This is designed to drive the parallel interface LCD drivers
* based in the Hitachi HD44780U controller and compatables.
*
* Copyright (c) 2012 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#define MAX_LCDS 8
#ifdef __cplusplus
extern "C" {
#endif
extern void lcdHome (int fd) ;
extern void lcdClear (int fd) ;
extern void lcdSendCommand (int fd, uint8_t command) ;
extern void lcdPosition (int fd, int x, int y) ;
extern void lcdPutchar (int fd, uint8_t data) ;
extern void lcdPuts (int fd, char *string) ;
extern void lcdPrintf (int fd, char *message, ...) ;
extern int lcdInit (int rows, int cols, int bits, int rs, int strb,
int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) ;
#ifdef __cplusplus
}
#endif

View File

@@ -39,19 +39,18 @@
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, ddr ;
int mask, old, reg ;
pin -= node->pinBase ;
ddr = MCP23x08_IODIR ;
mask = 1 << pin ;
old = wiringPiI2CReadReg8 (node->fd, ddr) ;
reg = MCP23x08_IODIR ;
mask = 1 << (pin - node->pinBase) ;
old = wiringPiI2CReadReg8 (node->fd, reg) ;
if (mode == OUTPUT)
old &= (~mask) ;
else
old |= mask ;
wiringPiI2CWriteReg8 (node->fd, ddr, old) ;
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
}
@@ -62,20 +61,19 @@ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, pud ;
int mask, old, reg ;
pin -= node->pinBase ;
pud = MCP23x08_GPPU ;
mask = 1 << pin ;
reg = MCP23x08_GPPU ;
mask = 1 << (pin - node->pinBase) ;
old = wiringPiI2CReadReg8 (node->fd, pud) ;
old = wiringPiI2CReadReg8 (node->fd, reg) ;
if (mode == PUD_UP)
old |= mask ;
else
old &= (~mask) ;
wiringPiI2CWriteReg8 (node->fd, pud, old) ;
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
}
@@ -88,8 +86,7 @@ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
int bit, old ;
pin -= node->pinBase ;
bit = 1 << (pin & 7) ;
bit = 1 << ((pin - node->pinBase) & 7) ;
old = node->data2 ;
if (value == LOW)
@@ -109,13 +106,10 @@ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
{
int mask, value, gpio ;
int mask, value ;
pin -= node->pinBase ;
gpio = MCP23x08_GPIO ;
mask = 1 << pin ;
value = wiringPiI2CReadReg8 (node->fd, gpio) ;
mask = 1 << ((pin - node->pinBase) & 7) ;
value = wiringPiI2CReadReg8 (node->fd, MCP23x08_GPIO) ;
if ((value & mask) == 0)
return LOW ;
@@ -127,12 +121,12 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
/*
* mcp23008Setup:
* Create a new instance of an MCP23008 I2C GPIO interface. We know it
* has 16 pins, so all we need to know here is the I2C address and the
* has 8 pins, so all we need to know here is the I2C address and the
* user-defined pin base.
*********************************************************************************
*/
int mcp23008Setup (int pinBase, int i2cAddress)
int mcp23008Setup (const int pinBase, const int i2cAddress)
{
int fd ;
struct wiringPiNodeStruct *node ;
@@ -142,7 +136,7 @@ int mcp23008Setup (int pinBase, int i2cAddress)
wiringPiI2CWriteReg8 (fd, MCP23x08_IOCON, IOCON_INIT) ;
node = wiringPiNewNode (pinBase, 16) ;
node = wiringPiNewNode (pinBase, 8) ;
node->fd = fd ;
node->pinMode = myPinMode ;

View File

@@ -26,7 +26,7 @@
extern "C" {
#endif
extern int mcp23008Setup (int pinBase, int i2cAddress) ;
extern int mcp23008Setup (const int pinBase, const int i2cAddress) ;
#ifdef __cplusplus
}

View File

@@ -0,0 +1,164 @@
/*
* mcp23016.c:
* Extend wiringPi with the MCP 23016 I2C GPIO expander chip
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <pthread.h>
#include "wiringPi.h"
#include "wiringPiI2C.h"
#include "mcp23016.h"
#include "mcp23016reg.h"
/*
* myPinMode:
*********************************************************************************
*/
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, reg ;
pin -= node->pinBase ;
if (pin < 8) // Bank A
reg = MCP23016_IODIR0 ;
else
{
reg = MCP23016_IODIR1 ;
pin &= 0x07 ;
}
mask = 1 << pin ;
old = wiringPiI2CReadReg8 (node->fd, reg) ;
if (mode == OUTPUT)
old &= (~mask) ;
else
old |= mask ;
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
}
/*
* myDigitalWrite:
*********************************************************************************
*/
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
int bit, old ;
pin -= node->pinBase ; // Pin now 0-15
bit = 1 << (pin & 7) ;
if (pin < 8) // Bank A
{
old = node->data2 ;
if (value == LOW)
old &= (~bit) ;
else
old |= bit ;
wiringPiI2CWriteReg8 (node->fd, MCP23016_GP0, old) ;
node->data2 = old ;
}
else // Bank B
{
old = node->data3 ;
if (value == LOW)
old &= (~bit) ;
else
old |= bit ;
wiringPiI2CWriteReg8 (node->fd, MCP23016_GP1, old) ;
node->data3 = old ;
}
}
/*
* myDigitalRead:
*********************************************************************************
*/
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
{
int mask, value, gpio ;
pin -= node->pinBase ;
if (pin < 8) // Bank A
gpio = MCP23016_GP0 ;
else
{
gpio = MCP23016_GP1 ;
pin &= 0x07 ;
}
mask = 1 << pin ;
value = wiringPiI2CReadReg8 (node->fd, gpio) ;
if ((value & mask) == 0)
return LOW ;
else
return HIGH ;
}
/*
* mcp23016Setup:
* Create a new instance of an MCP23016 I2C GPIO interface. We know it
* has 16 pins, so all we need to know here is the I2C address and the
* user-defined pin base.
*********************************************************************************
*/
int mcp23016Setup (const int pinBase, const int i2cAddress)
{
int fd ;
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
wiringPiI2CWriteReg8 (fd, MCP23016_IOCON0, IOCON_INIT) ;
wiringPiI2CWriteReg8 (fd, MCP23016_IOCON1, IOCON_INIT) ;
node = wiringPiNewNode (pinBase, 16) ;
node->fd = fd ;
node->pinMode = myPinMode ;
node->digitalRead = myDigitalRead ;
node->digitalWrite = myDigitalWrite ;
node->data2 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT0) ;
node->data3 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT1) ;
return 0 ;
}

View File

@@ -0,0 +1,33 @@
/*
* mcp23016.h:
* Extend wiringPi with the MCP 23016 I2C GPIO expander chip
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#ifdef __cplusplus
extern "C" {
#endif
extern int mcp23016Setup (const int pinBase, const int i2cAddress) ;
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,48 @@
/*
* mcp23016:
* Copyright (c) 2012-2013 Gordon Henderson
*
* Header file for code using the MCP23016 GPIO expander
* chip.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
// MCP23016 Registers
#define MCP23016_GP0 0x00
#define MCP23016_GP1 0x01
#define MCP23016_OLAT0 0x02
#define MCP23016_OLAT1 0x03
#define MCP23016_IPOL0 0x04
#define MCP23016_IPOL1 0x05
#define MCP23016_IODIR0 0x06
#define MCP23016_IODIR1 0x07
#define MCP23016_INTCAP0 0x08
#define MCP23016_INTCAP1 0x09
#define MCP23016_IOCON0 0x0A
#define MCP23016_IOCON1 0x0B
// Bits in the IOCON register
#define IOCON_IARES 0x01
// Default initialisation mode
#define IOCON_INIT 0

View File

@@ -39,27 +39,27 @@
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, ddr ;
int mask, old, reg ;
pin -= node->pinBase ;
if (pin < 8) // Bank A
ddr = MCP23x17_IODIRA ;
reg = MCP23x17_IODIRA ;
else
{
ddr = MCP23x17_IODIRB ;
reg = MCP23x17_IODIRB ;
pin &= 0x07 ;
}
mask = 1 << pin ;
old = wiringPiI2CReadReg8 (node->fd, ddr) ;
old = wiringPiI2CReadReg8 (node->fd, reg) ;
if (mode == OUTPUT)
old &= (~mask) ;
else
old |= mask ;
wiringPiI2CWriteReg8 (node->fd, ddr, old) ;
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
}
@@ -70,27 +70,27 @@ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, pud ;
int mask, old, reg ;
pin -= node->pinBase ;
if (pin < 8) // Bank A
pud = MCP23x17_GPPUA ;
reg = MCP23x17_GPPUA ;
else
{
pud = MCP23x17_GPPUB ;
pin &= 0x07 ;
reg = MCP23x17_GPPUB ;
pin &= 0x07 ;
}
mask = 1 << pin ;
old = wiringPiI2CReadReg8 (node->fd, pud) ;
old = wiringPiI2CReadReg8 (node->fd, reg) ;
if (mode == PUD_UP)
old |= mask ;
else
old &= (~mask) ;
wiringPiI2CWriteReg8 (node->fd, pud, old) ;
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
}
@@ -146,7 +146,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
pin -= node->pinBase ;
if (pin < 8) // Bank A
gpio = MCP23x17_GPIOA ;
gpio = MCP23x17_GPIOA ;
else
{
gpio = MCP23x17_GPIOB ;
@@ -171,7 +171,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
*********************************************************************************
*/
int mcp23017Setup (int pinBase, int i2cAddress)
int mcp23017Setup (const int pinBase, const int i2cAddress)
{
int fd ;
struct wiringPiNodeStruct *node ;

View File

@@ -26,7 +26,7 @@
extern "C" {
#endif
extern int mcp23017Setup (int pinBase, int i2cAddress) ;
extern int mcp23017Setup (const int pinBase, const int i2cAddress) ;
#ifdef __cplusplus
}

View File

@@ -78,19 +78,18 @@ static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, ddr ;
int mask, old, reg ;
pin -= node->pinBase ;
ddr = MCP23x08_IODIR ;
mask = 1 << pin ;
old = readByte (node->data0, node->data1, ddr) ;
reg = MCP23x08_IODIR ;
mask = 1 << (pin - node->pinBase) ;
old = readByte (node->data0, node->data1, reg) ;
if (mode == OUTPUT)
old &= (~mask) ;
else
old |= mask ;
writeByte (node->data0, node->data1, ddr, old) ;
writeByte (node->data0, node->data1, reg, old) ;
}
@@ -101,20 +100,19 @@ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, pud ;
int mask, old, reg ;
pin -= node->pinBase ;
pud = MCP23x08_GPPU ;
mask = 1 << pin ;
reg = MCP23x08_GPPU ;
mask = 1 << (pin - node->pinBase) ;
old = readByte (node->data0, node->data1, pud) ;
old = readByte (node->data0, node->data1, reg) ;
if (mode == PUD_UP)
old |= mask ;
else
old &= (~mask) ;
writeByte (node->data0, node->data1, pud, old) ;
writeByte (node->data0, node->data1, reg, old) ;
}
@@ -127,8 +125,7 @@ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
int bit, old ;
pin -= node->pinBase ;
bit = 1 << pin ;
bit = 1 << ((pin - node->pinBase) & 7) ;
old = node->data2 ;
if (value == LOW)
@@ -148,13 +145,10 @@ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
{
int mask, value, gpio ;
int mask, value ;
pin -= node->pinBase ;
gpio = MCP23x08_GPIO ;
mask = 1 << pin ;
value = readByte (node->data0, node->data1, gpio) ;
mask = 1 << ((pin - node->pinBase) & 7) ;
value = readByte (node->data0, node->data1, MCP23x08_GPIO) ;
if ((value & mask) == 0)
return LOW ;
@@ -166,12 +160,12 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
/*
* mcp23s08Setup:
* Create a new instance of an MCP23s08 SPI GPIO interface. We know it
* has 16 pins, so all we need to know here is the SPI address and the
* has 8 pins, so all we need to know here is the SPI address and the
* user-defined pin base.
*********************************************************************************
*/
int mcp23s08Setup (int pinBase, int spiPort, int devId)
int mcp23s08Setup (const int pinBase, const int spiPort, const int devId)
{
int x ;
struct wiringPiNodeStruct *node ;
@@ -181,7 +175,7 @@ int mcp23s08Setup (int pinBase, int spiPort, int devId)
writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
node = wiringPiNewNode (pinBase, 16) ;
node = wiringPiNewNode (pinBase, 8) ;
node->data0 = spiPort ;
node->data1 = devId ;

View File

@@ -26,7 +26,7 @@
extern "C" {
#endif
extern int mcp23s08Setup (int pinBase, int spiPort, int devId) ;
extern int mcp23s08Setup (const int pinBase, const int spiPort, const int devId) ;
#ifdef __cplusplus
}

View File

@@ -78,27 +78,27 @@ static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, ddr ;
int mask, old, reg ;
pin -= node->pinBase ;
if (pin < 8) // Bank A
ddr = MCP23x17_IODIRA ;
reg = MCP23x17_IODIRA ;
else
{
ddr = MCP23x17_IODIRB ;
reg = MCP23x17_IODIRB ;
pin &= 0x07 ;
}
mask = 1 << pin ;
old = readByte (node->data0, node->data1, ddr) ;
old = readByte (node->data0, node->data1, reg) ;
if (mode == OUTPUT)
old &= (~mask) ;
else
old |= mask ;
writeByte (node->data0, node->data1, ddr, old) ;
writeByte (node->data0, node->data1, reg, old) ;
}
@@ -109,27 +109,27 @@ static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, pud ;
int mask, old, reg ;
pin -= node->pinBase ;
if (pin < 8) // Bank A
pud = MCP23x17_GPPUA ;
reg = MCP23x17_GPPUA ;
else
{
pud = MCP23x17_GPPUB ;
pin &= 0x07 ;
reg = MCP23x17_GPPUB ;
pin &= 0x07 ;
}
mask = 1 << pin ;
old = readByte (node->data0, node->data1, pud) ;
old = readByte (node->data0, node->data1, reg) ;
if (mode == PUD_UP)
old |= mask ;
else
old &= (~mask) ;
writeByte (node->data0, node->data1, pud, old) ;
writeByte (node->data0, node->data1, reg, old) ;
}
@@ -185,7 +185,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
pin -= node->pinBase ;
if (pin < 8) // Bank A
gpio = MCP23x17_GPIOA ;
gpio = MCP23x17_GPIOA ;
else
{
gpio = MCP23x17_GPIOB ;
@@ -210,7 +210,7 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
*********************************************************************************
*/
int mcp23s17Setup (int pinBase, int spiPort, int devId)
int mcp23s17Setup (const int pinBase, const int spiPort, const int devId)
{
int x ;
struct wiringPiNodeStruct *node ;

View File

@@ -0,0 +1,76 @@
/*
* mcp3002.c:
* Extend wiringPi with the MCP3002 SPI Analog to Digital convertor
* Copyright (c) 2012-2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include "mcp3002.h"
/*
* myAnalogRead:
* Return the analog value of the given pin
*********************************************************************************
*/
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
{
unsigned char spiData [2] ;
unsigned char chanBits ;
int chan = pin - node->pinBase ;
if (chan == 0)
chanBits = 0b11010000 ;
else
chanBits = 0b11110000 ;
spiData [0] = chanBits ;
spiData [1] = 0 ;
wiringPiSPIDataRW (node->fd, spiData, 2) ;
return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
}
/*
* mcp3002Setup:
* Create a new wiringPi device node for an mcp2003 on the Pi's
* SPI interface.
*********************************************************************************
*/
int mcp3002Setup (const int pinBase, int spiChannel)
{
struct wiringPiNodeStruct *node ;
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
return -1 ;
node = wiringPiNewNode (pinBase, 2) ;
node->fd = spiChannel ;
node->analogRead = myAnalogRead ;
return 0 ;
}

View File

@@ -1,23 +1,24 @@
/*
* piFace.h:
* Control the PiFace Interface board for the Raspberry Pi
* mcp3002.c:
* Extend wiringPi with the MCP3002 SPI Analog to Digital convertor
* Copyright (c) 2012-2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 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/>.
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
@@ -25,7 +26,7 @@
extern "C" {
#endif
extern int piFaceSetup (int pinBase) ;
extern int mcp3002Setup (int pinBase, int spiChannel) ;
#ifdef __cplusplus
}

116
WiringPi/wiringPi/mcp3422.c Normal file
View File

@@ -0,0 +1,116 @@
/*
* mcp3422.c:
* Extend wiringPi with the MCP3422 I2C ADC chip
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include "mcp3422.h"
/*
* myAnalogRead:
* Read a channel from the device
*********************************************************************************
*/
int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
{
unsigned char config, b0, b1, b2, b3 ;
int value = 0 ;
// One-shot mode, trigger plus the other configs.
config = 0x80 | ((chan - node->pinBase) << 5) | (node->data0 << 2) | (node->data1) ;
wiringPiI2CWrite (node->fd, config) ;
switch (node->data0) // Sample rate
{
case MCP3422_SR_3_75: // 18 bits
delay (270) ;
b0 = wiringPiI2CRead (node->fd) ;
b1 = wiringPiI2CRead (node->fd) ;
b2 = wiringPiI2CRead (node->fd) ;
b3 = wiringPiI2CRead (node->fd) ;
value = ((b0 & 3) << 16) | (b1 << 8) | b2 ;
break ;
case MCP3422_SR_15: // 16 bits
delay ( 70) ;
b0 = wiringPiI2CRead (node->fd) ;
b1 = wiringPiI2CRead (node->fd) ;
b2 = wiringPiI2CRead (node->fd) ;
value = (b0 << 8) | b1 ;
break ;
case MCP3422_SR_60: // 14 bits
delay ( 17) ;
b0 = wiringPiI2CRead (node->fd) ;
b1 = wiringPiI2CRead (node->fd) ;
b2 = wiringPiI2CRead (node->fd) ;
value = ((b0 & 0x3F) << 8) | b1 ;
break ;
case MCP3422_SR_240: // 12 bits
delay ( 5) ;
b0 = wiringPiI2CRead (node->fd) ;
b1 = wiringPiI2CRead (node->fd) ;
b2 = wiringPiI2CRead (node->fd) ;
value = ((b0 & 0x0F) << 8) | b1 ;
break ;
}
return value ;
}
/*
* mcp3422Setup:
* Create a new wiringPi device node for the mcp3422
*********************************************************************************
*/
int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int gain)
{
int fd ;
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
node = wiringPiNewNode (pinBase, channels) ;
node->data0 = sampleRate ;
node->data1 = gain ;
node->analogRead = myAnalogRead ;
return 0 ;
}

View File

@@ -1,7 +1,6 @@
/*
* piNes.h:
* Driver for the NES Joystick controller on the Raspberry Pi
* Copyright (c) 2012 Gordon Henderson
* mcp3422.c:
* Extend wiringPi with the MCP3422 I2C ADC chip
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -22,23 +21,22 @@
***********************************************************************
*/
#define MAX_NES_JOYSTICKS 8
#define MCP3422_SR_3_75 0
#define MCP3422_SR_15 1
#define MCP3422_SR_60 2
#define MCP3422_SR_240 3
#define MCP3422_GAIN_1 0
#define MCP3422_GAIN_2 1
#define MCP3422_GAIN_4 2
#define MCP3422_GAIN_8 3
#define NES_RIGHT 0x01
#define NES_LEFT 0x02
#define NES_DOWN 0x04
#define NES_UP 0x08
#define NES_START 0x10
#define NES_SELECT 0x20
#define NES_B 0x40
#define NES_A 0x80
#ifdef __cplusplus
extern "C" {
#endif
extern int setupNesJoystick (int dPin, int cPin, int lPin) ;
extern unsigned int readNesJoystick (int joystick) ;
extern int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int gain) ;
#ifdef __cplusplus
}

View File

@@ -0,0 +1,76 @@
/*
* mcp4802.c:
* Extend wiringPi with the MCP4802 SPI Digital to Analog convertor
* Copyright (c) 2012-2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include "mcp4802.h"
/*
* myAnalogWrite:
* Write analog value on the given pin
*********************************************************************************
*/
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
unsigned char spiData [2] ;
unsigned char chanBits, dataBits ;
int chan = pin - node->pinBase ;
if (chan == 0)
chanBits = 0x30 ;
else
chanBits = 0xB0 ;
chanBits |= ((value >> 4) & 0x0F) ;
dataBits = ((value << 4) & 0xF0) ;
spiData [0] = chanBits ;
spiData [1] = dataBits ;
wiringPiSPIDataRW (node->fd, spiData, 2) ;
}
/*
* mcp4802Setup:
* Create a new wiringPi device node for an mcp4802 on the Pi's
* SPI interface.
*********************************************************************************
*/
int mcp4802Setup (const int pinBase, int spiChannel)
{
struct wiringPiNodeStruct *node ;
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
return -1 ;
node = wiringPiNewNode (pinBase, 2) ;
node->fd = spiChannel ;
node->analogWrite = myAnalogWrite ;
return 0 ;
}

View File

@@ -0,0 +1,33 @@
/*
* mcp4802.c:
* Extend wiringPi with the MCP4802 SPI Digital to Analog convertor
* Copyright (c) 2012-2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#ifdef __cplusplus
extern "C" {
#endif
extern int mcp4802Setup (int pinBase, int spiChannel) ;
#ifdef __cplusplus
}
#endif

126
WiringPi/wiringPi/pcf8574.c Normal file
View File

@@ -0,0 +1,126 @@
/*
* pcf8574.c:
* Extend wiringPi with the PCF8574 I2C GPIO expander chip
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <pthread.h>
#include "wiringPi.h"
#include "wiringPiI2C.h"
#include "pcf8574.h"
/*
* myPinMode:
* The PCF8574 is an odd chip - the pins are effectively bi-directional,
* however the pins should be drven high when used as an input pin...
* So, we're effectively copying digitalWrite...
*********************************************************************************
*/
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
{
int bit, old ;
bit = 1 << ((pin - node->pinBase) & 7) ;
old = node->data2 ;
if (mode == OUTPUT)
old &= (~bit) ; // Write bit to 0
else
old |= bit ; // Write bit to 1
wiringPiI2CWrite (node->fd, old) ;
node->data2 = old ;
}
/*
* myDigitalWrite:
*********************************************************************************
*/
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
int bit, old ;
bit = 1 << ((pin - node->pinBase) & 7) ;
old = node->data2 ;
if (value == LOW)
old &= (~bit) ;
else
old |= bit ;
wiringPiI2CWrite (node->fd, old) ;
node->data2 = old ;
}
/*
* myDigitalRead:
*********************************************************************************
*/
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
{
int mask, value ;
mask = 1 << ((pin - node->pinBase) & 7) ;
value = wiringPiI2CRead (node->fd) ;
if ((value & mask) == 0)
return LOW ;
else
return HIGH ;
}
/*
* pcf8574Setup:
* Create a new instance of a PCF8574 I2C GPIO interface. We know it
* has 8 pins, so all we need to know here is the I2C address and the
* user-defined pin base.
*********************************************************************************
*/
int pcf8574Setup (const int pinBase, const int i2cAddress)
{
int fd ;
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
node = wiringPiNewNode (pinBase, 8) ;
node->fd = fd ;
node->pinMode = myPinMode ;
node->digitalRead = myDigitalRead ;
node->digitalWrite = myDigitalWrite ;
node->data2 = wiringPiI2CRead (fd) ;
return 0 ;
}

View File

@@ -0,0 +1,33 @@
/*
* pcf8574.h:
* Extend wiringPi with the PCF8574 I2C GPIO expander chip
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#ifdef __cplusplus
extern "C" {
#endif
extern int pcf8574Setup (const int pinBase, const int i2cAddress) ;
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,90 @@
/*
* pcf8591.c:
* Extend wiringPi with the PCF8591 I2C GPIO Analog expander chip
* The chip has 1 8-bit DAC and 4 x 8-bit ADCs
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <unistd.h>
#include "wiringPi.h"
#include "wiringPiI2C.h"
#include "pcf8591.h"
/*
* myAnalogWrite:
*********************************************************************************
*/
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
unsigned char b [2] ;
b [0] = 0x40 ;
b [1] = value & 0xFF ;
write (node->fd, b, 2) ;
}
/*
* myAnalogRead:
*********************************************************************************
*/
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
{
int x ;
wiringPiI2CWrite (node->fd, 0x40 | ((pin - node->pinBase) & 3)) ;
x = wiringPiI2CRead (node->fd) ; // Throw away the first read
x = wiringPiI2CRead (node->fd) ;
return x ;
}
/*
* pcf8591Setup:
* Create a new instance of a PCF8591 I2C GPIO interface. We know it
* has 4 pins, (4 analog inputs and 1 analog output which we'll shadow
* input 0) so all we need to know here is the I2C address and the
* user-defined pin base.
*********************************************************************************
*/
int pcf8591Setup (const int pinBase, const int i2cAddress)
{
int fd ;
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
node = wiringPiNewNode (pinBase, 4) ;
node->fd = fd ;
node->analogRead = myAnalogRead ;
node->analogWrite = myAnalogWrite ;
return 0 ;
}

View File

@@ -0,0 +1,33 @@
/*
* pcf8591.h:
* Extend wiringPi with the PCF8591 I2C GPIO Analog expander chip
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#ifdef __cplusplus
extern "C" {
#endif
extern int pcf8591Setup (const int pinBase, const int i2cAddress) ;
#ifdef __cplusplus
}
#endif

View File

@@ -1,179 +0,0 @@
/*
* piFace.:
* Arduino compatable (ish) Wiring library for the Raspberry Pi
* Copyright (c) 2012-2013 Gordon Henderson
*
* This file to interface with the PiFace peripheral device which
* has an MCP23S17 GPIO device connected via the SPI bus.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdint.h>
#include "wiringPi.h"
#include "wiringPiSPI.h"
#include "piFace.h"
#define PIFACE_SPEED 4000000
#define PIFACE_DEVNO 0
#include "mcp23x0817.h"
/*
* writeByte:
* Write a byte to a register on the MCP23S17 on the SPI bus.
*********************************************************************************
*/
static void writeByte (uint8_t reg, uint8_t data)
{
uint8_t spiData [4] ;
spiData [0] = CMD_WRITE ;
spiData [1] = reg ;
spiData [2] = data ;
wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
}
/*
* readByte:
* Read a byte from a register on the MCP23S17 on the SPI bus.
*********************************************************************************
*/
static uint8_t readByte (uint8_t reg)
{
uint8_t spiData [4] ;
spiData [0] = CMD_READ ;
spiData [1] = reg ;
wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
return spiData [2] ;
}
/*
* digitalWrite:
* Perform the digitalWrite function on the PiFace board
*********************************************************************************
*/
void digitalWritePiFace (struct wiringPiNodeStruct *node, int pin, int value)
{
uint8_t mask, old ;
pin -= node->pinBase ;
mask = 1 << pin ;
old = readByte (MCP23x17_GPIOA) ;
if (value == 0)
old &= (~mask) ;
else
old |= mask ;
writeByte (MCP23x17_GPIOA, old) ;
}
/*
* digitalReadPiFace:
* Perform the digitalRead function on the PiFace board
*********************************************************************************
*/
int digitalReadPiFace (struct wiringPiNodeStruct *node, int pin)
{
uint8_t mask, reg ;
pin -= node->pinBase ;
mask = 1 << (pin & 7) ;
if (pin < 8)
reg = MCP23x17_GPIOB ; // Input regsiter
else
reg = MCP23x17_OLATA ; // Output latch regsiter
if ((readByte (reg) & mask) != 0)
return HIGH ;
else
return LOW ;
}
/*
* pullUpDnControlPiFace:
* Perform the pullUpDnControl function on the PiFace board
*********************************************************************************
*/
void pullUpDnControlPiFace (struct wiringPiNodeStruct *node, int pin, int pud)
{
uint8_t mask, old ;
pin -= node->pinBase ;
mask = 1 << pin ;
old = readByte (MCP23x17_GPPUB) ;
if (pud == 0)
old &= (~mask) ;
else
old |= mask ;
writeByte (MCP23x17_GPPUB, old) ;
}
/*
* piFaceSetup
* Setup the SPI interface and initialise the MCP23S17 chip
* We create one node with 16 pins - each if the first 8 pins being read
* and write - although the operations actually go to different
* hardware ports. The top 8 let you read the state of the output register.
*********************************************************************************
*/
int piFaceSetup (int pinBase)
{
int x ;
struct wiringPiNodeStruct *node ;
if ((x = wiringPiSPISetup (PIFACE_DEVNO, PIFACE_SPEED)) < 0)
return x ;
// Setup the MCP23S17
writeByte (MCP23x17_IOCON, IOCON_INIT) ;
writeByte (MCP23x17_IODIRA, 0x00) ; // Port A -> Outputs
writeByte (MCP23x17_IODIRB, 0xFF) ; // Port B -> Inputs
node = wiringPiNewNode (pinBase, 16) ;
node->digitalRead = digitalReadPiFace ;
node->digitalWrite = digitalWritePiFace ;
node->pullUpDnControl = pullUpDnControlPiFace ;
return 0 ;
}

View File

@@ -36,15 +36,16 @@
*********************************************************************************
*/
int piHiPri (int pri)
int piHiPri (const int pri)
{
struct sched_param sched ;
memset (&sched, 0, sizeof(sched)) ;
if (pri > sched_get_priority_max (SCHED_RR))
pri = sched_get_priority_max (SCHED_RR) ;
sched.sched_priority = sched_get_priority_max (SCHED_RR) ;
else
sched.sched_priority = pri ;
sched.sched_priority = pri ;
return sched_setscheduler (0, SCHED_RR, &sched) ;
}

View File

@@ -1,113 +0,0 @@
/*
* piNes.c:
* Driver for the NES Joystick controller on the Raspberry Pi
* Copyright (c) 2012 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include "wiringPi.h"
#include "piNes.h"
#define MAX_NES_JOYSTICKS 8
#define NES_RIGHT 0x01
#define NES_LEFT 0x02
#define NES_DOWN 0x04
#define NES_UP 0x08
#define NES_START 0x10
#define NES_SELECT 0x20
#define NES_B 0x40
#define NES_A 0x80
#define PULSE_TIME 25
// Data to store the pins for each controller
struct nesPinsStruct
{
unsigned int cPin, dPin, lPin ;
} ;
static struct nesPinsStruct nesPins [MAX_NES_JOYSTICKS] ;
static int joysticks = 0 ;
/*
* setupNesJoystick:
* Create a new NES joystick interface, program the pins, etc.
*********************************************************************************
*/
int setupNesJoystick (int dPin, int cPin, int lPin)
{
if (joysticks == MAX_NES_JOYSTICKS)
return -1 ;
nesPins [joysticks].dPin = dPin ;
nesPins [joysticks].cPin = cPin ;
nesPins [joysticks].lPin = lPin ;
digitalWrite (lPin, LOW) ;
digitalWrite (cPin, LOW) ;
pinMode (lPin, OUTPUT) ;
pinMode (cPin, OUTPUT) ;
pinMode (dPin, INPUT) ;
return joysticks++ ;
}
/*
* readNesJoystick:
* Do a single scan of the NES Joystick.
*********************************************************************************
*/
unsigned int readNesJoystick (int joystick)
{
unsigned int value = 0 ;
int i ;
struct nesPinsStruct *pins = &nesPins [joystick] ;
// Toggle Latch - which presents the first bit
digitalWrite (pins->lPin, HIGH) ; delayMicroseconds (PULSE_TIME) ;
digitalWrite (pins->lPin, LOW) ; delayMicroseconds (PULSE_TIME) ;
// Read first bit
value = digitalRead (pins->dPin) ;
// Now get the next 7 bits with the clock
for (i = 0 ; i < 7 ; ++i)
{
digitalWrite (pins->cPin, HIGH) ; delayMicroseconds (PULSE_TIME) ;
digitalWrite (pins->cPin, LOW) ; delayMicroseconds (PULSE_TIME) ;
value = (value << 1) | digitalRead (pins->dPin) ;
}
return value ^ 0xFF ;
}

View File

@@ -28,7 +28,7 @@
#include "wiringPi.h"
#include "softPwm.h"
#define MAX_PINS 64
#define MAX_PINS 1024
// The PWM Frequency is derived from the "pulse time" below. Essentially,
// the frequency is a function of the range and this pulse time.
@@ -93,7 +93,7 @@ static PI_THREAD (softPwmThread)
void softPwmWrite (int pin, int value)
{
pin &= 63 ;
pin &= (MAX_PINS - 1) ;
/**/ if (value < 0)
value = 0 ;

View File

@@ -82,7 +82,8 @@ static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
*********************************************************************************
*/
int sr595Setup (int pinBase, int numPins, int dataPin, int clockPin, int latchPin)
int sr595Setup (const int pinBase, const int numPins,
const int dataPin, const int clockPin, const int latchPin)
{
struct wiringPiNodeStruct *node ;

View File

@@ -26,7 +26,8 @@
extern "C" {
#endif
extern int sr595Setup (int pinBase, int numPins, int dataPin, int clockPin, int latchPin) ;
extern int sr595Setup (const int pinBase, const int numPins,
const int dataPin, const int clockPin, const int latchPin) ;
#ifdef __cplusplus
}

View File

@@ -202,11 +202,13 @@ static uint64_t epochMilli, epochMicro ;
// Misc
static int wiringPiMode = WPI_MODE_UNINITIALISED ;
static volatile int pinPass = -1 ;
static pthread_mutex_t pinMutex ;
// Debugging & Return codes
int wiringPiDebug = FALSE ;
int wiringPiCodes = FALSE ;
int wiringPiDebug = FALSE ;
int wiringPiReturnCodes = FALSE ;
// sysFds:
// Map a file descriptor from the /sys/class/gpio/gpioX/value
@@ -512,16 +514,19 @@ static uint8_t gpioToClkDiv [] =
*********************************************************************************
*/
int wiringPiFailure (char *message, ...)
int wiringPiFailure (int fatal, const char *message, ...)
{
va_list argp ;
char buffer [1024] ;
if (!fatal && wiringPiReturnCodes)
return -1 ;
va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;
fprintf (stderr,"%s", buffer) ;
fprintf (stderr, "%s", buffer) ;
exit (EXIT_FAILURE) ;
return 0 ;
@@ -531,7 +536,7 @@ int wiringPiFailure (char *message, ...)
/*
* piBoardRev:
* Return a number representing the hardware revision of the board.
* Revision is currently 1 or 2. -1 is returned on error.
* Revision is currently 1 or 2.
*
* Much confusion here )-:
* Seems there are some boards with 0000 in them (mistake in manufacture)
@@ -554,7 +559,7 @@ int wiringPiFailure (char *message, ...)
*********************************************************************************
*/
static void piBoardRevOops (char *why)
static void piBoardRevOops (const char *why)
{
fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
fprintf (stderr, " -> %s\n", why) ;
@@ -658,7 +663,7 @@ void setPadDrive (int group, int value)
{
uint32_t wrVal ;
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_GPIO))
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
{
if ((group < 0) || (group > 2))
return ;
@@ -712,7 +717,7 @@ int getAlt (int pin)
void pwmSetMode (int mode)
{
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_GPIO) || (wiringPiMode == WPI_MODE_PHYS))
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
{
if (mode == PWM_MODE_MS)
*(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
@@ -731,7 +736,7 @@ void pwmSetMode (int mode)
void pwmSetRange (unsigned int range)
{
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_GPIO) || (wiringPiMode == WPI_MODE_PHYS))
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
{
*(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
*(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
@@ -752,7 +757,7 @@ void pwmSetClock (int divisor)
uint32_t pwm_control ;
divisor &= 4095 ;
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_GPIO) || (wiringPiMode == WPI_MODE_PHYS))
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
{
if (wiringPiDebug)
printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
@@ -864,17 +869,17 @@ struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
// Minimum pin base is 64
if (pinBase < 64)
(void)wiringPiFailure ("wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
(void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
// Check all pins in-case there is overlap:
for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
if (wiringPiFindNode (pin) != NULL)
(void)wiringPiFailure ("wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
(void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
node = calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
if (node == NULL)
(void)wiringPiFailure ("wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
(void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
node->pinBase = pinBase ;
node->pinMax = pinBase + numPins - 1 ;
@@ -999,15 +1004,13 @@ void pullUpDnControl (int pin, int pud)
else if (wiringPiMode != WPI_MODE_GPIO)
return ;
pud &= 3 ;
*(gpio + GPPUD) = pud ; delayMicroseconds (5) ;
*(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
*(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
*(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
*(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
}
else
else // Extension module
{
if ((node = wiringPiFindNode (pin)) != NULL)
node->pullUpDnControl (node, pin, pud) ;
@@ -1034,8 +1037,8 @@ int digitalRead (int pin)
if (sysFds [pin] == -1)
return LOW ;
lseek (sysFds [pin], 0L, SEEK_SET) ;
read (sysFds [pin], &c, 1) ;
lseek (sysFds [pin], 0L, SEEK_SET) ;
read (sysFds [pin], &c, 1) ;
return (c == '0') ? LOW : HIGH ;
}
else if (wiringPiMode == WPI_MODE_PINS)
@@ -1080,6 +1083,7 @@ void digitalWrite (int pin, int value)
else
write (sysFds [pin], "1\n", 2) ;
}
return ;
}
else if (wiringPiMode == WPI_MODE_PINS)
pin = pinToGpio [pin] ;
@@ -1194,6 +1198,7 @@ void digitalWriteByte (int value)
digitalWrite (pin, value & mask) ;
mask <<= 1 ;
}
return ;
}
else
{
@@ -1234,7 +1239,7 @@ int waitForInterrupt (int pin, int mS)
else if (wiringPiMode == WPI_MODE_PHYS)
pin = physToGpio [pin] ;
if ((fd = sysFds [pin & 63]) == -1)
if ((fd = sysFds [pin]) == -1)
return -2 ;
// Setup poll structure
@@ -1265,10 +1270,13 @@ int waitForInterrupt (int pin, int mS)
static void *interruptHandler (void *arg)
{
int myPin = *(int *)arg ;
int myPin ;
(void)piHiPri (55) ; // Only effective if we run as root
myPin = pinPass ;
pinPass = -1 ;
for (;;)
if (waitForInterrupt (myPin, -1) > 0)
isrFunctions [myPin] () ;
@@ -1288,21 +1296,24 @@ static void *interruptHandler (void *arg)
int wiringPiISR (int pin, int mode, void (*function)(void))
{
pthread_t threadId ;
const char *modeS ;
char fName [64] ;
char *modeS ;
char pinS [8] ;
pid_t pid ;
int count, i ;
uint8_t c ;
char c ;
int bcmGpioPin ;
pin &= 63 ;
/**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
(void)wiringPiFailure ("wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
else if (wiringPiMode == WPI_MODE_PINS)
pin = pinToGpio [pin] ;
bcmGpioPin = pinToGpio [pin] ;
else if (wiringPiMode == WPI_MODE_PHYS)
pin = physToGpio [pin] ;
bcmGpioPin = physToGpio [pin] ;
else
bcmGpioPin = pin ;
// Now export the pin and set the right edge
// We're going to use the gpio program to do this, so it assumes
@@ -1319,7 +1330,7 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
else
modeS = "both" ;
sprintf (pinS, "%d", pin) ;
sprintf (pinS, "%d", bcmGpioPin) ;
if ((pid = fork ()) < 0) // Fail
return pid ;
@@ -1334,23 +1345,29 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
}
// Now pre-open the /sys/class node - it may already be open if
// we are in Sys mode, but this will do no harm.
// we are in Sys mode or if we call here twice, if-so, we'll close it.
sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
if ((sysFds [pin] = open (fName, O_RDWR)) < 0)
if (sysFds [bcmGpioPin] != -1)
close (sysFds [bcmGpioPin]) ;
sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
return -1 ;
// Clear any initial pending interrupt
ioctl (sysFds [pin], FIONREAD, &count) ;
ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
for (i = 0 ; i < count ; ++i)
read (sysFds [pin], &c, 1) ;
read (sysFds [bcmGpioPin], &c, 1) ;
isrFunctions [pin] = function ;
pthread_create (&threadId, NULL, interruptHandler, &pin) ;
delay (1) ;
pthread_mutex_lock (&pinMutex) ;
pinPass = pin ;
pthread_create (&threadId, NULL, interruptHandler, NULL) ;
while (pinPass != -1)
delay (1) ;
pthread_mutex_unlock (&pinMutex) ;
return 0 ;
}
@@ -1492,10 +1509,10 @@ int wiringPiSetup (void)
wiringPiDebug = TRUE ;
if (getenv (ENV_CODES) != NULL)
wiringPiCodes = TRUE ;
wiringPiReturnCodes = TRUE ;
if (geteuid () != 0)
(void)wiringPiFailure ("wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
(void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
if (wiringPiDebug)
printf ("wiringPi: wiringPiSetup called\n") ;
@@ -1516,38 +1533,38 @@ int wiringPiSetup (void)
// Open the master /dev/memory device
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
(void)wiringPiFailure ("wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
// GPIO:
gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
if ((int32_t)gpio == -1)
(void)wiringPiFailure ("wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
// PWM
pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
if ((int32_t)pwm == -1)
(void)wiringPiFailure ("wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
// Clock control (needed for PWM)
clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ;
if ((int32_t)clk == -1)
(void)wiringPiFailure ("wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
// The drive pads
pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
if ((int32_t)pads == -1)
(void)wiringPiFailure ("wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
#ifdef USE_TIMER
// The system timer
timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
if ((int32_t)timer == -1)
(void)wiringPiFailure ("wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
// Set the timer to free-running, 1MHz.
// 0xF9 is 249, the timer divide is base clock / (divide+1)
@@ -1629,7 +1646,7 @@ int wiringPiSetupSys (void)
wiringPiDebug = TRUE ;
if (getenv (ENV_CODES) != NULL)
wiringPiCodes = TRUE ;
wiringPiReturnCodes = TRUE ;
if (wiringPiDebug)
printf ("wiringPi: wiringPiSetupSys called\n") ;

View File

@@ -21,6 +21,9 @@
***********************************************************************
*/
#ifndef __WIRING_PI_H__
#define __WIRING_PI_H__
// Handy defines
// Deprecated
@@ -65,6 +68,12 @@
#define PI_THREAD(X) void *X (void *dummy)
// Failure modes
#define WPI_FATAL (1==1)
#define WPI_ALMOST (1==2)
// wiringPiNodeStruct:
// This describes additional device nodes in the extended wiringPi
// 2.0 scheme of things.
@@ -104,6 +113,9 @@ struct wiringPiNodeStruct
extern "C" {
#endif
// Internal
extern int wiringPiFailure (int fatal, const char *message, ...) ;
// Core wiringPi functions
@@ -132,6 +144,7 @@ extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio
extern int piBoardRev (void) ;
extern int wpiPinToGpio (int wpiPin) ;
extern int physPinToGpio (int physPin) ;
extern void setPadDrive (int group, int value) ;
extern int getAlt (int pin) ;
extern void digitalWriteByte (int value) ;
@@ -154,7 +167,7 @@ extern void piUnlock (int key) ;
// Schedulling priority
extern int piHiPri (int pri) ;
extern int piHiPri (const int pri) ;
// Extras from arduino land
@@ -166,3 +179,5 @@ extern unsigned int micros (void) ;
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -48,6 +48,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
@@ -148,7 +150,7 @@ int wiringPiI2CReadReg16 (int fd, int reg)
if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data))
return -1 ;
else
return data.byte & 0xFF ;
return data.word & 0xFFFF ;
}
@@ -188,35 +190,38 @@ int wiringPiI2CWriteReg16 (int fd, int reg, int value)
/*
* wiringPiI2CSetup:
* Open the I2C device, and regsiter the target device
* wiringPiI2CSetupInterface:
* Undocumented access to set the interface explicitly - might be used
* for the Pi's 2nd I2C interface...
*********************************************************************************
*/
int wiringPiI2CSetupInterface (char *device, int devId)
int wiringPiI2CSetupInterface (const char *device, int devId)
{
int fd ;
if ((fd = open (device, O_RDWR)) < 0)
return -1 ;
return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ;
if (ioctl (fd, I2C_SLAVE, devId) < 0)
return -1 ;
return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ;
return fd ;
}
int wiringPiI2CSetup (int devId)
/*
* wiringPiI2CSetup:
* Open the I2C device, and regsiter the target device
*********************************************************************************
*/
int wiringPiI2CSetup (const int devId)
{
int rev ;
char *device ;
const char *device ;
if ((rev = piBoardRev ()) < 0)
{
fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ;
exit (1) ;
}
rev = piBoardRev () ;
if (rev == 1)
device = "/dev/i2c-0" ;

View File

@@ -34,8 +34,8 @@ extern int wiringPiI2CWrite (int fd, int data) ;
extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ;
extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ;
extern int wiringPiI2CSetupInterface (char *device, int devId) ;
extern int wiringPiI2CSetup (int devId) ;
extern int wiringPiI2CSetupInterface (const char *device, int devId) ;
extern int wiringPiI2CSetup (const int devId) ;
#ifdef __cplusplus
}

View File

@@ -25,6 +25,8 @@
#include <stdint.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
@@ -36,11 +38,11 @@
// The SPI bus parameters
// Variables as they need to be passed as pointers later on
static char *spiDev0 = "/dev/spidev0.0" ;
static char *spiDev1 = "/dev/spidev0.1" ;
static uint8_t spiMode = 0 ;
static uint8_t spiBPW = 8 ;
static uint16_t spiDelay = 0;
const static char *spiDev0 = "/dev/spidev0.0" ;
const static char *spiDev1 = "/dev/spidev0.1" ;
const static uint8_t spiMode = 0 ;
const static uint8_t spiBPW = 8 ;
const static uint16_t spiDelay = 0 ;
static uint32_t spiSpeeds [2] ;
static int spiFds [2] ;
@@ -97,7 +99,7 @@ int wiringPiSPISetup (int channel, int speed)
channel &= 1 ;
if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
return -1 ;
return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
spiSpeeds [channel] = speed ;
spiFds [channel] = fd ;
@@ -106,14 +108,14 @@ int wiringPiSPISetup (int channel, int speed)
// Why are we reading it afterwriting it? I've no idea, but for now I'm blindly
// copying example code I've seen online...
if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0)
return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ;
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ;
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ;
return fd ;
}

View File

@@ -41,7 +41,7 @@
*********************************************************************************
*/
int serialOpen (char *device, int baud)
int serialOpen (const char *device, const int baud)
{
struct termios options ;
speed_t myBaud ;
@@ -117,7 +117,7 @@ int serialOpen (char *device, int baud)
*********************************************************************************
*/
void serialFlush (int fd)
void serialFlush (const int fd)
{
tcflush (fd, TCIOFLUSH) ;
}
@@ -129,7 +129,7 @@ void serialFlush (int fd)
*********************************************************************************
*/
void serialClose (int fd)
void serialClose (const int fd)
{
close (fd) ;
}
@@ -141,7 +141,7 @@ void serialClose (int fd)
*********************************************************************************
*/
void serialPutchar (int fd, unsigned char c)
void serialPutchar (const int fd, const unsigned char c)
{
write (fd, &c, 1) ;
}
@@ -153,7 +153,7 @@ void serialPutchar (int fd, unsigned char c)
*********************************************************************************
*/
void serialPuts (int fd, char *s)
void serialPuts (const int fd, const char *s)
{
write (fd, s, strlen (s)) ;
}
@@ -164,7 +164,7 @@ void serialPuts (int fd, char *s)
*********************************************************************************
*/
void serialPrintf (int fd, char *message, ...)
void serialPrintf (const int fd, const char *message, ...)
{
va_list argp ;
char buffer [1024] ;
@@ -183,7 +183,7 @@ void serialPrintf (int fd, char *message, ...)
*********************************************************************************
*/
int serialDataAvail (int fd)
int serialDataAvail (const int fd)
{
int result ;
@@ -202,7 +202,7 @@ int serialDataAvail (int fd)
*********************************************************************************
*/
int serialGetchar (int fd)
int serialGetchar (const int fd)
{
uint8_t x ;

View File

@@ -24,14 +24,14 @@
extern "C" {
#endif
extern int serialOpen (char *device, int baud) ;
extern void serialClose (int fd) ;
extern void serialFlush (int fd) ;
extern void serialPutchar (int fd, unsigned char c) ;
extern void serialPuts (int fd, char *s) ;
extern void serialPrintf (int fd, char *message, ...) ;
extern int serialDataAvail (int fd) ;
extern int serialGetchar (int fd) ;
extern int serialOpen (const char *device, const int baud) ;
extern void serialClose (const int fd) ;
extern void serialFlush (const int fd) ;
extern void serialPutchar (const int fd, const unsigned char c) ;
extern void serialPuts (const int fd, const char *s) ;
extern void serialPrintf (const int fd, const char *message, ...) ;
extern int serialDataAvail (const int fd) ;
extern int serialGetchar (const int fd) ;
#ifdef __cplusplus
}