Updated from git.drogon.net

This commit is contained in:
Phil Howard
2016-02-27 16:03:10 +00:00
parent ddf1b3ffaa
commit 26c7fe3332
151 changed files with 17346 additions and 1343 deletions

View File

@@ -1,8 +1,8 @@
# ;
#
# Makefile:
# wiringPi - Wiring Compatable library for the Raspberry Pi
#
# Copyright (c) 2012 Gordon Henderson
# Copyright (c) 2012-2015 Gordon Henderson
#################################################################################
# This file is part of wiringPi:
# https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -21,12 +21,15 @@
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
#################################################################################
DYN_VERS_MAJ=1
DYN_VERS_MIN=0
VERSION=$(shell cat ../VERSION)
DESTDIR?=/usr
PREFIX?=/local
VERSION=$(DYN_VERS_MAJ).$(DYN_VERS_MIN)
DESTDIR=/usr
PREFIX=/local
LDCONFIG?=ldconfig
ifneq ($V,1)
Q ?= @
endif
STATIC=libwiringPi.a
DYNAMIC=libwiringPi.so.$(VERSION)
@@ -35,118 +38,145 @@ DYNAMIC=libwiringPi.so.$(VERSION)
DEBUG = -O2
CC = gcc
INCLUDE = -I.
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe -fPIC
DEFS = -D_GNU_SOURCE
CFLAGS = $(DEBUG) $(DEFS) -Wformat=2 -Wall -Winline $(INCLUDE) -pipe -fPIC
LIBS =
# Should not alter anything below this line
###############################################################################
SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \
gertboard.c \
piNes.c \
lcd.c piHiPri.c piThread.c \
wiringPiSPI.c \
softPwm.c softServo.c softTone.c
SRC = wiringPi.c \
wiringSerial.c wiringShift.c \
piHiPri.c piThread.c \
wiringPiSPI.c wiringPiI2C.c \
softPwm.c softTone.c \
mcp23008.c mcp23016.c mcp23017.c \
mcp23s08.c mcp23s17.c \
sr595.c \
pcf8574.c pcf8591.c \
mcp3002.c mcp3004.c mcp4802.c mcp3422.c \
max31855.c max5322.c \
sn3218.c \
drcSerial.c \
wpiExtensions.c
HEADERS = wiringPi.h \
wiringSerial.h wiringShift.h \
wiringPiSPI.h wiringPiI2C.h \
softPwm.h softTone.h \
mcp23008.h mcp23016.h mcp23017.h \
mcp23s08.h mcp23s17.h \
sr595.h \
pcf8574.h pcf8591.h \
mcp3002.h mcp3004.h mcp4802.h mcp3422.h \
max31855.h max5322.h \
sn3218.h \
drcSerial.h \
wpiExtensions.h
SRC_I2C = wiringPiI2C.c
OBJ = $(SRC:.c=.o)
OBJ_I2C = $(SRC_I2C:.c=.o)
all: $(DYNAMIC)
static: $(STATIC)
$(STATIC): $(OBJ)
@echo "[Link (Static)]"
@ar rcs $(STATIC) $(OBJ)
@ranlib $(STATIC)
$Q echo "[Link (Static)]"
$Q ar rcs $(STATIC) $(OBJ)
$Q ranlib $(STATIC)
# @size $(STATIC)
$(DYNAMIC): $(OBJ)
@echo "[Link (Dynamic)]"
@$(CC) -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ)
i2c: $(OBJ) $(OBJ_I2C)
@echo "[Link (Dynamic + I2C)]"
@$(CC) -shared -Wl,-soname,libwiringPi.so.1 -o libwiringPi.so.1.0 -lpthread $(OBJ) $(OBJ_I2C)
$Q echo "[Link (Dynamic)]"
$Q $(CC) -shared -Wl,-soname,libwiringPi.so$(WIRINGPI_SONAME_SUFFIX) -o libwiringPi.so.$(VERSION) -lpthread $(OBJ)
.c.o:
@echo [Compile] $<
@$(CC) -c $(CFLAGS) $< -o $@
$Q echo [Compile] $<
$Q $(CC) -c $(CFLAGS) $< -o $@
.PHONEY: clean
.PHONY: clean
clean:
rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.*
$Q echo "[Clean]"
$Q rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.*
.PHONEY: tags
.PHONY: tags
tags: $(SRC)
@echo [ctags]
@ctags $(SRC)
$Q echo [ctags]
$Q ctags $(SRC)
.PHONEY: install
.PHONY: install
install: $(DYNAMIC)
@echo "[Install]"
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
@install -m 0755 -d $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringPi.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringSerial.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringShift.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 gertboard.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 piNes.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 softPwm.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 softServo.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include
@install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include
@install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib
@ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so
@ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so.1
@ldconfig
$Q echo "[Install Headers]"
$Q install -m 0755 -d $(DESTDIR)$(PREFIX)/include
$Q install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include
$Q echo "[Install Dynamic Lib]"
$Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
$Q install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION)
$Q ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so
$Q $(LDCONFIG)
.PHONEY: install-static
.PHONY: install-static
install-static: $(STATIC)
@echo "[Install Static]"
@install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib
$Q echo "[Install Headers]"
$Q install -m 0755 -d $(DESTDIR)$(PREFIX)/include
$Q install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include
$Q echo "[Install Static Lib]"
$Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
$Q install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib
.PHONEY: uninstall
.PHONY: install-deb
install-deb: $(DYNAMIC)
$Q echo "[Install Headers: deb]"
$Q install -m 0755 -d ~/wiringPi/debian-template/wiringPi/usr/include
$Q install -m 0644 $(HEADERS) ~/wiringPi/debian-template/wiringPi/usr/include
$Q echo "[Install Dynamic Lib: deb]"
install -m 0755 -d ~/wiringPi/debian-template/wiringPi/usr/lib
install -m 0755 libwiringPi.so.$(VERSION) ~/wiringPi/debian-template/wiringPi/usr/lib/libwiringPi.so.$(VERSION)
ln -sf ~/wiringPi/debian-template/wiringPi/usr/lib/libwiringPi.so.$(VERSION) ~/wiringPi/debian-template/wiringPi/usr/lib/libwiringPi.so
.PHONY: uninstall
uninstall:
@echo "[UnInstall]"
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPi.h
@rm -f $(DESTDIR)$(PREFIX)/include/wiringSerial.h
@rm -f $(DESTDIR)$(PREFIX)/include/wiringShift.h
@rm -f $(DESTDIR)$(PREFIX)/include/gertboard.h
@rm -f $(DESTDIR)$(PREFIX)/include/piNes.h
@rm -f $(DESTDIR)$(PREFIX)/include/softPwm.h
@rm -f $(DESTDIR)$(PREFIX)/include/softServo.h
@rm -f $(DESTDIR)$(PREFIX)/include/softTone.h
@rm -f $(DESTDIR)$(PREFIX)/include/lcd.h
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h
@rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.*
@ldconfig
$Q echo "[UnInstall]"
$Q cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS)
$Q cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPi.*
$Q $(LDCONFIG)
.PHONEY: depend
.PHONY: depend
depend:
makedepend -Y $(SRC) $(SRC_I2C)
# DO NOT DELETE
wiringPi.o: wiringPi.h
wiringPiFace.o: wiringPi.h
wiringPi.o: softPwm.h softTone.h wiringPi.h
wiringSerial.o: wiringSerial.h
wiringShift.o: wiringPi.h wiringShift.h
gertboard.o: wiringPiSPI.h gertboard.h
piNes.o: wiringPi.h piNes.h
lcd.o: wiringPi.h lcd.h
piHiPri.o: wiringPi.h
piThread.o: wiringPi.h
wiringPiSPI.o: wiringPiSPI.h
softPwm.o: wiringPi.h softPwm.h
softServo.o: wiringPi.h softServo.h
softTone.o: wiringPi.h softTone.h
wiringPiSPI.o: wiringPi.h wiringPiSPI.h
wiringPiI2C.o: wiringPi.h wiringPiI2C.h
softPwm.o: wiringPi.h softPwm.h
softTone.o: wiringPi.h softTone.h
mcp23008.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23008.h
mcp23016.o: wiringPi.h wiringPiI2C.h mcp23016.h mcp23016reg.h
mcp23017.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23017.h
mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h
mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h
sr595.o: wiringPi.h sr595.h
pcf8574.o: wiringPi.h wiringPiI2C.h pcf8574.h
pcf8591.o: wiringPi.h wiringPiI2C.h pcf8591.h
mcp3002.o: wiringPi.h wiringPiSPI.h mcp3002.h
mcp3004.o: wiringPi.h wiringPiSPI.h mcp3004.h
mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h
mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h
max31855.o: wiringPi.h wiringPiSPI.h max31855.h
max5322.o: wiringPi.h wiringPiSPI.h max5322.h
sn3218.o: wiringPi.h wiringPiI2C.h sn3218.h
drcSerial.o: wiringPi.h wiringSerial.h drcSerial.h
wpiExtensions.o: wiringPi.h mcp23008.h mcp23016.h mcp23017.h mcp23s08.h
wpiExtensions.o: mcp23s17.h sr595.h pcf8574.h pcf8591.h mcp3002.h mcp3004.h
wpiExtensions.o: mcp4802.h mcp3422.h max31855.h max5322.h sn3218.h
wpiExtensions.o: drcSerial.h wpiExtensions.h

201
wiringPi/drcSerial.c Normal file
View File

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

33
wiringPi/drcSerial.h Normal file
View File

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

99
wiringPi/max31855.c Normal file
View File

@@ -0,0 +1,99 @@
/*
* max31855.c:
* Extend wiringPi with the max31855 SPI Analog to Digital convertor
* Copyright (c) 2012-2015 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <byteswap.h>
#include <stdint.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include "max31855.h"
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
{
uint32_t spiData ;
int temp ;
int chan = pin - node->pinBase ;
wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
spiData = __bswap_32(spiData) ;
switch (chan)
{
case 0: // Existing read - return raw value * 4
spiData >>= 18 ;
temp = spiData & 0x1FFF ; // Bottom 13 bits
if ((spiData & 0x2000) != 0) // Negative
temp = -temp ;
return temp ;
case 1: // Return error bits
return spiData & 0x7 ;
case 2: // Return temp in C * 10
spiData >>= 18 ;
temp = spiData & 0x1FFF ; // Bottom 13 bits
if ((spiData & 0x2000) != 0) // Negative
temp = -temp ;
return (int)((((double)temp * 25) + 0.5) / 10.0) ;
case 3: // Return temp in F * 10
spiData >>= 18 ;
temp = spiData & 0x1FFF ; // Bottom 13 bits
if ((spiData & 0x2000) != 0) // Negative
temp = -temp ;
return (int)((((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 100.0) + 0.5) / 10.0) ;
default: // Who knows...
return 0 ;
}
}
/*
* max31855Setup:
* Create a new wiringPi device node for an max31855 on the Pi's
* SPI interface.
*********************************************************************************
*/
int max31855Setup (const int pinBase, int spiChannel)
{
struct wiringPiNodeStruct *node ;
if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
return -1 ;
node = wiringPiNewNode (pinBase, 4) ;
node->fd = spiChannel ;
node->analogRead = myAnalogRead ;
return 0 ;
}

33
wiringPi/max31855.h Normal file
View File

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

84
wiringPi/max5322.c Normal file
View File

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

33
wiringPi/max5322.h Normal file
View File

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

149
wiringPi/mcp23008.c Normal file
View File

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

33
wiringPi/mcp23008.h Normal file
View File

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

164
wiringPi/mcp23016.c Normal file
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 ;
}

33
wiringPi/mcp23016.h Normal file
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

48
wiringPi/mcp23016reg.h Normal file
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

195
wiringPi/mcp23017.c Normal file
View File

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

33
wiringPi/mcp23017.h Normal file
View File

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

189
wiringPi/mcp23s08.c Normal file
View File

@@ -0,0 +1,189 @@
/*
* mcp23s08.c:
* Extend wiringPi with the MCP 23s08 SPI GPIO expander chip
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdint.h>
#include "wiringPi.h"
#include "wiringPiSPI.h"
#include "mcp23x0817.h"
#include "mcp23s08.h"
#define MCP_SPEED 4000000
/*
* writeByte:
* Write a byte to a register on the MCP23s08 on the SPI bus.
*********************************************************************************
*/
static void writeByte (uint8_t spiPort, uint8_t devId, uint8_t reg, uint8_t data)
{
uint8_t spiData [4] ;
spiData [0] = CMD_WRITE | ((devId & 7) << 1) ;
spiData [1] = reg ;
spiData [2] = data ;
wiringPiSPIDataRW (spiPort, spiData, 3) ;
}
/*
* readByte:
* Read a byte from a register on the MCP23s08 on the SPI bus.
*********************************************************************************
*/
static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
{
uint8_t spiData [4] ;
spiData [0] = CMD_READ | ((devId & 7) << 1) ;
spiData [1] = reg ;
wiringPiSPIDataRW (spiPort, spiData, 3) ;
return spiData [2] ;
}
/*
* myPinMode:
*********************************************************************************
*/
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, reg ;
reg = MCP23x08_IODIR ;
mask = 1 << (pin - node->pinBase) ;
old = readByte (node->data0, node->data1, reg) ;
if (mode == OUTPUT)
old &= (~mask) ;
else
old |= mask ;
writeByte (node->data0, node->data1, reg, old) ;
}
/*
* myPullUpDnControl:
*********************************************************************************
*/
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, reg ;
reg = MCP23x08_GPPU ;
mask = 1 << (pin - node->pinBase) ;
old = readByte (node->data0, node->data1, reg) ;
if (mode == PUD_UP)
old |= mask ;
else
old &= (~mask) ;
writeByte (node->data0, node->data1, reg, old) ;
}
/*
* myDigitalWrite:
*********************************************************************************
*/
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
int bit, old ;
bit = 1 << ((pin - node->pinBase) & 7) ;
old = node->data2 ;
if (value == LOW)
old &= (~bit) ;
else
old |= bit ;
writeByte (node->data0, node->data1, MCP23x08_GPIO, old) ;
node->data2 = old ;
}
/*
* myDigitalRead:
*********************************************************************************
*/
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
{
int mask, value ;
mask = 1 << ((pin - node->pinBase) & 7) ;
value = readByte (node->data0, node->data1, MCP23x08_GPIO) ;
if ((value & mask) == 0)
return LOW ;
else
return HIGH ;
}
/*
* mcp23s08Setup:
* Create a new instance of an MCP23s08 SPI GPIO interface. We know it
* has 8 pins, so all we need to know here is the SPI address and the
* user-defined pin base.
*********************************************************************************
*/
int mcp23s08Setup (const int pinBase, const int spiPort, const int devId)
{
int x ;
struct wiringPiNodeStruct *node ;
if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0)
return x ;
writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
node = wiringPiNewNode (pinBase, 8) ;
node->data0 = spiPort ;
node->data1 = devId ;
node->pinMode = myPinMode ;
node->pullUpDnControl = myPullUpDnControl ;
node->digitalRead = myDigitalRead ;
node->digitalWrite = myDigitalWrite ;
node->data2 = readByte (spiPort, devId, MCP23x08_OLAT) ;
return 0 ;
}

33
wiringPi/mcp23s08.h Normal file
View File

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

236
wiringPi/mcp23s17.c Normal file
View File

@@ -0,0 +1,236 @@
/*
* mcp23s17.c:
* Extend wiringPi with the MCP 23s17 SPI GPIO expander chip
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdint.h>
#include "wiringPi.h"
#include "wiringPiSPI.h"
#include "mcp23x0817.h"
#include "mcp23s17.h"
#define MCP_SPEED 4000000
/*
* writeByte:
* Write a byte to a register on the MCP23s17 on the SPI bus.
*********************************************************************************
*/
static void writeByte (uint8_t spiPort, uint8_t devId, uint8_t reg, uint8_t data)
{
uint8_t spiData [4] ;
spiData [0] = CMD_WRITE | ((devId & 7) << 1) ;
spiData [1] = reg ;
spiData [2] = data ;
wiringPiSPIDataRW (spiPort, spiData, 3) ;
}
/*
* readByte:
* Read a byte from a register on the MCP23s17 on the SPI bus.
*********************************************************************************
*/
static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
{
uint8_t spiData [4] ;
spiData [0] = CMD_READ | ((devId & 7) << 1) ;
spiData [1] = reg ;
wiringPiSPIDataRW (spiPort, spiData, 3) ;
return spiData [2] ;
}
/*
* myPinMode:
*********************************************************************************
*/
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, reg ;
pin -= node->pinBase ;
if (pin < 8) // Bank A
reg = MCP23x17_IODIRA ;
else
{
reg = MCP23x17_IODIRB ;
pin &= 0x07 ;
}
mask = 1 << pin ;
old = readByte (node->data0, node->data1, reg) ;
if (mode == OUTPUT)
old &= (~mask) ;
else
old |= mask ;
writeByte (node->data0, node->data1, reg, old) ;
}
/*
* myPullUpDnControl:
*********************************************************************************
*/
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
{
int mask, old, reg ;
pin -= node->pinBase ;
if (pin < 8) // Bank A
reg = MCP23x17_GPPUA ;
else
{
reg = MCP23x17_GPPUB ;
pin &= 0x07 ;
}
mask = 1 << pin ;
old = readByte (node->data0, node->data1, reg) ;
if (mode == PUD_UP)
old |= mask ;
else
old &= (~mask) ;
writeByte (node->data0, node->data1, reg, old) ;
}
/*
* myDigitalWrite:
*********************************************************************************
*/
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
int bit, old ;
pin -= node->pinBase ; // Pin now 0-15
bit = 1 << (pin & 7) ;
if (pin < 8) // Bank A
{
old = node->data2 ;
if (value == LOW)
old &= (~bit) ;
else
old |= bit ;
writeByte (node->data0, node->data1, MCP23x17_GPIOA, old) ;
node->data2 = old ;
}
else // Bank B
{
old = node->data3 ;
if (value == LOW)
old &= (~bit) ;
else
old |= bit ;
writeByte (node->data0, node->data1, MCP23x17_GPIOB, old) ;
node->data3 = old ;
}
}
/*
* myDigitalRead:
*********************************************************************************
*/
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
{
int mask, value, gpio ;
pin -= node->pinBase ;
if (pin < 8) // Bank A
gpio = MCP23x17_GPIOA ;
else
{
gpio = MCP23x17_GPIOB ;
pin &= 0x07 ;
}
mask = 1 << pin ;
value = readByte (node->data0, node->data1, gpio) ;
if ((value & mask) == 0)
return LOW ;
else
return HIGH ;
}
/*
* mcp23s17Setup:
* Create a new instance of an MCP23s17 SPI GPIO interface. We know it
* has 16 pins, so all we need to know here is the SPI address and the
* user-defined pin base.
*********************************************************************************
*/
int mcp23s17Setup (const int pinBase, const int spiPort, const int devId)
{
int x ;
struct wiringPiNodeStruct *node ;
if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0)
return x ;
writeByte (spiPort, devId, MCP23x17_IOCON, IOCON_INIT | IOCON_HAEN) ;
writeByte (spiPort, devId, MCP23x17_IOCONB, IOCON_INIT | IOCON_HAEN) ;
node = wiringPiNewNode (pinBase, 16) ;
node->data0 = spiPort ;
node->data1 = devId ;
node->pinMode = myPinMode ;
node->pullUpDnControl = myPullUpDnControl ;
node->digitalRead = myDigitalRead ;
node->digitalWrite = myDigitalWrite ;
node->data2 = readByte (spiPort, devId, MCP23x17_OLATA) ;
node->data3 = readByte (spiPort, devId, MCP23x17_OLATB) ;
return 0 ;
}

33
wiringPi/mcp23s17.h Normal file
View File

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

73
wiringPi/mcp23x08.h Normal file
View File

@@ -0,0 +1,73 @@
/*
* mcp23x17:
* Copyright (c) 2012-2013 Gordon Henderson
*
* Header file for code using the MCP23x17 GPIO expander chip.
* This comes in 2 flavours: MCP23017 which has an I2C interface,
* an the MXP23S17 which has an SPI interface.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
// MCP23x17 Registers
#define IODIRA 0x00
#define IPOLA 0x02
#define GPINTENA 0x04
#define DEFVALA 0x06
#define INTCONA 0x08
#define IOCON 0x0A
#define GPPUA 0x0C
#define INTFA 0x0E
#define INTCAPA 0x10
#define GPIOA 0x12
#define OLATA 0x14
#define IODIRB 0x01
#define IPOLB 0x03
#define GPINTENB 0x05
#define DEFVALB 0x07
#define INTCONB 0x09
#define IOCONB 0x0B
#define GPPUB 0x0D
#define INTFB 0x0F
#define INTCAPB 0x11
#define GPIOB 0x13
#define OLATB 0x15
// Bits in the IOCON register
#define IOCON_UNUSED 0x01
#define IOCON_INTPOL 0x02
#define IOCON_ODR 0x04
#define IOCON_HAEN 0x08
#define IOCON_DISSLW 0x10
#define IOCON_SEQOP 0x20
#define IOCON_MIRROR 0x40
#define IOCON_BANK_MODE 0x80
// Default initialisation mode
#define IOCON_INIT (IOCON_SEQOP)
// SPI Command codes
#define CMD_WRITE 0x40
#define CMD_READ 0x41

87
wiringPi/mcp23x0817.h Normal file
View File

@@ -0,0 +1,87 @@
/*
* mcp23xxx:
* Copyright (c) 2012-2013 Gordon Henderson
*
* Header file for code using the MCP23x08 and 17 GPIO expander
* chips.
* This comes in 2 flavours: MCP230xx (08/17) which has an I2C
* interface, and the MXP23Sxx (08/17) which has an SPI interface.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
// MCP23x08 Registers
#define MCP23x08_IODIR 0x00
#define MCP23x08_IPOL 0x01
#define MCP23x08_GPINTEN 0x02
#define MCP23x08_DEFVAL 0x03
#define MCP23x08_INTCON 0x04
#define MCP23x08_IOCON 0x05
#define MCP23x08_GPPU 0x06
#define MCP23x08_INTF 0x07
#define MCP23x08_INTCAP 0x08
#define MCP23x08_GPIO 0x09
#define MCP23x08_OLAT 0x0A
// MCP23x17 Registers
#define MCP23x17_IODIRA 0x00
#define MCP23x17_IPOLA 0x02
#define MCP23x17_GPINTENA 0x04
#define MCP23x17_DEFVALA 0x06
#define MCP23x17_INTCONA 0x08
#define MCP23x17_IOCON 0x0A
#define MCP23x17_GPPUA 0x0C
#define MCP23x17_INTFA 0x0E
#define MCP23x17_INTCAPA 0x10
#define MCP23x17_GPIOA 0x12
#define MCP23x17_OLATA 0x14
#define MCP23x17_IODIRB 0x01
#define MCP23x17_IPOLB 0x03
#define MCP23x17_GPINTENB 0x05
#define MCP23x17_DEFVALB 0x07
#define MCP23x17_INTCONB 0x09
#define MCP23x17_IOCONB 0x0B
#define MCP23x17_GPPUB 0x0D
#define MCP23x17_INTFB 0x0F
#define MCP23x17_INTCAPB 0x11
#define MCP23x17_GPIOB 0x13
#define MCP23x17_OLATB 0x15
// Bits in the IOCON register
#define IOCON_UNUSED 0x01
#define IOCON_INTPOL 0x02
#define IOCON_ODR 0x04
#define IOCON_HAEN 0x08
#define IOCON_DISSLW 0x10
#define IOCON_SEQOP 0x20
#define IOCON_MIRROR 0x40
#define IOCON_BANK_MODE 0x80
// Default initialisation mode
#define IOCON_INIT (IOCON_SEQOP)
// SPI Command codes
#define CMD_WRITE 0x40
#define CMD_READ 0x41

76
wiringPi/mcp3002.c Normal file
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 mcp3002 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 ;
}

33
wiringPi/mcp3002.h Normal file
View File

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

76
wiringPi/mcp3004.c Normal file
View File

@@ -0,0 +1,76 @@
/*
* mcp3004.c:
* Extend wiringPi with the MCP3004 SPI Analog to Digital convertor
* Copyright (c) 2012-2013 Gordon Henderson
*
* Thanks also to "ShorTie" on IRC for some remote debugging help!
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* 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 "mcp3004.h"
/*
* myAnalogRead:
* Return the analog value of the given pin
*********************************************************************************
*/
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
{
unsigned char spiData [3] ;
unsigned char chanBits ;
int chan = pin - node->pinBase ;
chanBits = 0b10000000 | (chan << 4) ;
spiData [0] = 1 ; // Start bit
spiData [1] = chanBits ;
spiData [2] = 0 ;
wiringPiSPIDataRW (node->fd, spiData, 3) ;
return ((spiData [1] << 8) | spiData [2]) & 0x3FF ;
}
/*
* mcp3004Setup:
* Create a new wiringPi device node for an mcp3004 on the Pi's
* SPI interface.
*********************************************************************************
*/
int mcp3004Setup (const int pinBase, int spiChannel)
{
struct wiringPiNodeStruct *node ;
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
return -1 ;
node = wiringPiNewNode (pinBase, 8) ;
node->fd = spiChannel ;
node->analogRead = myAnalogRead ;
return 0 ;
}

33
wiringPi/mcp3004.h Normal file
View File

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

110
wiringPi/mcp3422.c Normal file
View File

@@ -0,0 +1,110 @@
/*
* mcp3422.c:
* Extend wiringPi with the MCP3422 I2C ADC chip
* Also works for the MCP3423 and MCP3224 (4 channel) chips
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* 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 <unistd.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 ;
unsigned char buffer [4] ;
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) ;
read (node->fd, buffer, 4) ;
value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [0] ;
break ;
case MCP3422_SR_15: // 16 bits
delay ( 70) ;
read (node->fd, buffer, 3) ;
value = (buffer [0] << 8) | buffer [1] ;
break ;
case MCP3422_SR_60: // 14 bits
delay ( 17) ;
read (node->fd, buffer, 3) ;
value = ((buffer [0] & 0x3F) << 8) | buffer [1] ;
break ;
case MCP3422_SR_240: // 12 bits
delay ( 5) ;
read (node->fd, buffer, 3) ;
value = ((buffer [0] & 0x0F) << 8) | buffer [0] ;
break ;
}
return value ;
}
/*
* mcp3422Setup:
* Create a new wiringPi device node for the mcp3422
*********************************************************************************
*/
int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain)
{
int fd ;
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
node = wiringPiNewNode (pinBase, 4) ;
node->data0 = sampleRate ;
node->data1 = gain ;
node->analogRead = myAnalogRead ;
return 0 ;
}

43
wiringPi/mcp3422.h Normal file
View File

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

76
wiringPi/mcp4802.c Normal file
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 ;
}

33
wiringPi/mcp4802.h Normal file
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/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 ;
}

33
wiringPi/pcf8574.h Normal file
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

90
wiringPi/pcf8591.c Normal file
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 ;
}

33
wiringPi/pcf8591.h Normal file
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

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

75
wiringPi/sn3218.c Normal file
View File

@@ -0,0 +1,75 @@
/*
* sn3218.c:
* Extend wiringPi with the SN3218 I2C LEd Driver
* Copyright (c) 2012-2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include "sn3218.h"
/*
* myAnalogWrite:
* Write analog value on the given pin
*********************************************************************************
*/
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
int fd = node->fd ;
int chan = 0x01 + (pin - node->pinBase) ;
wiringPiI2CWriteReg8 (fd, chan, value & 0xFF) ; // Value
wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update
}
/*
* sn3218Setup:
* Create a new wiringPi device node for an sn3218 on the Pi's
* SPI interface.
*********************************************************************************
*/
int sn3218Setup (const int pinBase)
{
int fd ;
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (0x54)) < 0)
return fd ;
// Setup the chip - initialise all 18 LEDs to off
//wiringPiI2CWriteReg8 (fd, 0x17, 0) ; // Reset
wiringPiI2CWriteReg8 (fd, 0x00, 1) ; // Not Shutdown
wiringPiI2CWriteReg8 (fd, 0x13, 0x3F) ; // Enable LEDs 0- 5
wiringPiI2CWriteReg8 (fd, 0x14, 0x3F) ; // Enable LEDs 6-11
wiringPiI2CWriteReg8 (fd, 0x15, 0x3F) ; // Enable LEDs 12-17
wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update
node = wiringPiNewNode (pinBase, 18) ;
node->fd = fd ;
node->analogWrite = myAnalogWrite ;
return 0 ;
}

33
wiringPi/sn3218.h Normal file
View File

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

View File

@@ -1,7 +1,7 @@
/*
* softPwm.c:
* Provide 2 channels of software driven PWM.
* Copyright (c) 2012 Gordon Henderson
* Copyright (c) 2012-2014 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -28,17 +28,22 @@
#include "wiringPi.h"
#include "softPwm.h"
#define MAX_PINS 64
// MAX_PINS:
// This is more than the number of Pi pins because we can actually softPwm
// pins that are on GPIO expanders. It's not that efficient and more than 1 or
// 2 pins on e.g. (SPI) mcp23s17 won't really be that effective, however...
#define MAX_PINS 1024
// The PWM Frequency is derived from the "pulse time" below. Essentially,
// the frequency is a function of the range and this pulse time.
// The total period will be range * pulse time in uS, so a pulse time
// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 uS
// The total period will be range * pulse time in µS, so a pulse time
// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 µS
// which is a frequency of 100Hz.
//
// It's possible to get a higher frequency by lowering the pulse time,
// however CPU uage will skyrocket as wiringPi uses a hard-loop to time
// periods under 100uS - this is because the Linux timer calls are just
// periods under 100µS - this is because the Linux timer calls are just
// accurate at all, and have an overhead.
//
// Another way to increase the frequency is to reduce the range - however
@@ -46,10 +51,10 @@
#define PULSE_TIME 100
static int marks [MAX_PINS] ;
static int range [MAX_PINS] ;
int newPin = -1 ;
static volatile int marks [MAX_PINS] ;
static volatile int range [MAX_PINS] ;
static volatile pthread_t threads [MAX_PINS] ;
static volatile int newPin = -1 ;
/*
@@ -61,11 +66,15 @@ int newPin = -1 ;
static PI_THREAD (softPwmThread)
{
int pin, mark, space ;
struct sched_param param ;
param.sched_priority = sched_get_priority_max (SCHED_RR) ;
pthread_setschedparam (pthread_self (), SCHED_RR, &param) ;
pin = newPin ;
newPin = -1 ;
piHiPri (50) ;
piHiPri (90) ;
for (;;)
{
@@ -93,7 +102,7 @@ static PI_THREAD (softPwmThread)
void softPwmWrite (int pin, int value)
{
pin &= 63 ;
pin &= (MAX_PINS - 1) ;
/**/ if (value < 0)
value = 0 ;
@@ -106,13 +115,20 @@ void softPwmWrite (int pin, int value)
/*
* softPwmCreate:
* Create a new PWM thread.
* Create a new softPWM thread.
*********************************************************************************
*/
int softPwmCreate (int pin, int initialValue, int pwmRange)
{
int res ;
pthread_t myThread ;
if (range [pin] != 0) // Already running on this pin
return -1 ;
if (range <= 0)
return -1 ;
pinMode (pin, OUTPUT) ;
digitalWrite (pin, LOW) ;
@@ -121,10 +137,30 @@ int softPwmCreate (int pin, int initialValue, int pwmRange)
range [pin] = pwmRange ;
newPin = pin ;
res = piThreadCreate (softPwmThread) ;
res = pthread_create (&myThread, NULL, softPwmThread, NULL) ;
while (newPin != -1)
delay (1) ;
threads [pin] = myThread ;
return res ;
}
/*
* softPwmStop:
* Stop an existing softPWM thread
*********************************************************************************
*/
void softPwmStop (int pin)
{
if (range [pin] != 0)
{
pthread_cancel (threads [pin]) ;
pthread_join (threads [pin], NULL) ;
range [pin] = 0 ;
digitalWrite (pin, LOW) ;
}
}

View File

@@ -28,6 +28,7 @@ extern "C" {
extern int softPwmCreate (int pin, int value, int range) ;
extern void softPwmWrite (int pin, int value) ;
extern void softPwmStop (int pin) ;
#ifdef __cplusplus
}

View File

@@ -36,7 +36,8 @@
#define PULSE_TIME 100
static int frewqs [MAX_PINS] ;
static int freqs [MAX_PINS] ;
static pthread_t threads [MAX_PINS] ;
static int newPin = -1 ;
@@ -49,7 +50,11 @@ static int newPin = -1 ;
static PI_THREAD (softToneThread)
{
int pin, frewq, halfPeriod ;
int pin, freq, halfPeriod ;
struct sched_param param ;
param.sched_priority = sched_get_priority_max (SCHED_RR) ;
pthread_setschedparam (pthread_self (), SCHED_RR, &param) ;
pin = newPin ;
newPin = -1 ;
@@ -58,12 +63,12 @@ static PI_THREAD (softToneThread)
for (;;)
{
frewq = frewqs [pin] ;
if (frewq == 0)
freq = freqs [pin] ;
if (freq == 0)
delay (1) ;
else
{
halfPeriod = 500000 / frewq ;
halfPeriod = 500000 / freq ;
digitalWrite (pin, HIGH) ;
delayMicroseconds (halfPeriod) ;
@@ -83,16 +88,16 @@ static PI_THREAD (softToneThread)
*********************************************************************************
*/
void softToneWrite (int pin, int frewq)
void softToneWrite (int pin, int freq)
{
pin &= 63 ;
/**/ if (frewq < 0)
frewq = 0 ;
else if (frewq > 5000) // Max 5KHz
frewq = 5000 ;
/**/ if (freq < 0)
freq = 0 ;
else if (freq > 5000) // Max 5KHz
freq = 5000 ;
frewqs [pin] = frewq ;
freqs [pin] = freq ;
}
@@ -105,17 +110,41 @@ void softToneWrite (int pin, int frewq)
int softToneCreate (int pin)
{
int res ;
pthread_t myThread ;
pinMode (pin, OUTPUT) ;
digitalWrite (pin, LOW) ;
frewqs [pin] = 0 ;
if (threads [pin] != 0)
return -1 ;
freqs [pin] = 0 ;
newPin = pin ;
res = piThreadCreate (softToneThread) ;
res = pthread_create (&myThread, NULL, softToneThread, NULL) ;
while (newPin != -1)
delay (1) ;
threads [pin] = myThread ;
return res ;
}
/*
* softToneStop:
* Stop an existing softTone thread
*********************************************************************************
*/
void softToneStop (int pin)
{
if (threads [pin] != 0)
{
pthread_cancel (threads [pin]) ;
pthread_join (threads [pin], NULL) ;
threads [pin] = 0 ;
digitalWrite (pin, LOW) ;
}
}

View File

@@ -31,7 +31,8 @@ extern "C" {
#endif
extern int softToneCreate (int pin) ;
extern void softToneWrite (int pin, int frewq) ;
extern void softToneStop (int pin) ;
extern void softToneWrite (int pin, int freq) ;
#ifdef __cplusplus
}

109
wiringPi/sr595.c Normal file
View File

@@ -0,0 +1,109 @@
/*
* sr595.c:
* Extend wiringPi with the 74x595 shift register as a GPIO
* expander chip.
* Note that the code can cope with a number of 595's
* daisy-chained together - up to 4 for now as we're storing
* the output "register" in a single unsigned int.
*
* Copyright (c) 2013 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdint.h>
#include "wiringPi.h"
#include "sr595.h"
/*
* myDigitalWrite:
*********************************************************************************
*/
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
{
unsigned int mask ;
int dataPin, clockPin, latchPin ;
int bit, bits, output ;
pin -= node->pinBase ; // Normalise pin number
bits = node->pinMax - node->pinBase + 1 ; // ie. number of clock pulses
dataPin = node->data0 ;
clockPin = node->data1 ;
latchPin = node->data2 ;
output = node->data3 ;
mask = 1 << pin ;
if (value == LOW)
output &= (~mask) ;
else
output |= mask ;
node->data3 = output ;
// A low -> high latch transition copies the latch to the output pins
digitalWrite (latchPin, LOW) ; delayMicroseconds (1) ;
for (bit = bits - 1 ; bit >= 0 ; --bit)
{
digitalWrite (dataPin, output & (1 << bit)) ;
digitalWrite (clockPin, HIGH) ; delayMicroseconds (1) ;
digitalWrite (clockPin, LOW) ; delayMicroseconds (1) ;
}
digitalWrite (latchPin, HIGH) ; delayMicroseconds (1) ;
}
/*
* sr595Setup:
* Create a new instance of a 74x595 shift register GPIO expander.
*********************************************************************************
*/
int sr595Setup (const int pinBase, const int numPins,
const int dataPin, const int clockPin, const int latchPin)
{
struct wiringPiNodeStruct *node ;
node = wiringPiNewNode (pinBase, numPins) ;
node->data0 = dataPin ;
node->data1 = clockPin ;
node->data2 = latchPin ;
node->data3 = 0 ; // Output register
node->digitalWrite = myDigitalWrite ;
// Initialise the underlying hardware
digitalWrite (dataPin, LOW) ;
digitalWrite (clockPin, LOW) ;
digitalWrite (latchPin, HIGH) ;
pinMode (dataPin, OUTPUT) ;
pinMode (clockPin, OUTPUT) ;
pinMode (latchPin, OUTPUT) ;
return 0 ;
}

34
wiringPi/sr595.h Normal file
View File

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

File diff suppressed because it is too large Load Diff

View File

@@ -21,15 +21,18 @@
***********************************************************************
*/
#ifndef __WIRING_PI_H__
#define __WIRING_PI_H__
// Handy defines
// Deprecated
#define NUM_PINS 17
// wiringPi modes
#define WPI_MODE_PINS 0
#define WPI_MODE_GPIO 1
#define WPI_MODE_GPIO_SYS 2
#define WPI_MODE_PIFACE 3
#define WPI_MODE_PHYS 3
#define WPI_MODE_PIFACE 4
#define WPI_MODE_UNINITIALISED -1
// Pin modes
@@ -38,6 +41,9 @@
#define OUTPUT 1
#define PWM_OUTPUT 2
#define GPIO_CLOCK 3
#define SOFT_PWM_OUTPUT 4
#define SOFT_TONE_OUTPUT 5
#define PWM_TONE_OUTPUT 6
#define LOW 0
#define HIGH 1
@@ -60,10 +66,80 @@
#define INT_EDGE_RISING 2
#define INT_EDGE_BOTH 3
// Pi model types and version numbers
// Intended for the GPIO program Use at your own risk.
#define PI_MODEL_A 0
#define PI_MODEL_B 1
#define PI_MODEL_AP 2
#define PI_MODEL_BP 3
#define PI_MODEL_2 4
#define PI_ALPHA 5
#define PI_MODEL_CM 6
#define PI_MODEL_07 7
#define PI_MODEL_08 8
#define PI_MODEL_ZERO 9
#define PI_VERSION_1 0
#define PI_VERSION_1_1 1
#define PI_VERSION_1_2 2
#define PI_VERSION_2 3
#define PI_MAKER_SONY 0
#define PI_MAKER_EGOMAN 1
#define PI_MAKER_MBEST 2
#define PI_MAKER_UNKNOWN 3
extern const char *piModelNames [16] ;
extern const char *piRevisionNames [16] ;
extern const char *piMakerNames [16] ;
extern const int piMemorySize [ 8] ;
// Intended for the GPIO program Use at your own risk.
// Threads
#define PI_THREAD(X) void *X (void *dummy)
// Failure modes
#define WPI_FATAL (1==1)
#define WPI_ALMOST (1==2)
// wiringPiNodeStruct:
// This describes additional device nodes in the extended wiringPi
// 2.0 scheme of things.
// It's a simple linked list for now, but will hopefully migrate to
// a binary tree for efficiency reasons - but then again, the chances
// of more than 1 or 2 devices being added are fairly slim, so who
// knows....
struct wiringPiNodeStruct
{
int pinBase ;
int pinMax ;
int fd ; // Node specific
unsigned int data0 ; // ditto
unsigned int data1 ; // ditto
unsigned int data2 ; // ditto
unsigned int data3 ; // ditto
void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ;
void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ;
int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ;
void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ;
void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
struct wiringPiNodeStruct *next ;
} ;
extern struct wiringPiNodeStruct *wiringPiNodes ;
// Function prototypes
// c++ wrappers thanks to a comment by Nick Lott
@@ -73,46 +149,67 @@
extern "C" {
#endif
// Basic wiringPi functions
// Data
// Internal
extern int wiringPiFailure (int fatal, const char *message, ...) ;
// Core wiringPi functions
extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ;
extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
extern int wiringPiSetup (void) ;
extern int wiringPiSetupSys (void) ;
extern int wiringPiSetupGpio (void) ;
extern int wiringPiSetupPhys (void) ;
extern void pinModeAlt (int pin, int mode) ;
extern void pinMode (int pin, int mode) ;
extern void pullUpDnControl (int pin, int pud) ;
extern int digitalRead (int pin) ;
extern void digitalWrite (int pin, int value) ;
extern void pwmWrite (int pin, int value) ;
extern int analogRead (int pin) ;
extern void analogWrite (int pin, int value) ;
// PiFace specifics
// (Deprecated)
extern int wiringPiSetupPiFace (void) ;
extern int piBoardRev (void) ;
extern int wpiPinToGpio (int wpiPin) ;
extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only
extern void (*pinMode) (int pin, int mode) ;
extern int (*getAlt) (int pin) ;
extern void (*pullUpDnControl) (int pin, int pud) ;
extern void (*digitalWrite) (int pin, int value) ;
extern void (*digitalWriteByte) (int value) ;
extern void (*gpioClockSet) (int pin, int freq) ;
extern void (*pwmWrite) (int pin, int value) ;
extern void (*setPadDrive) (int group, int value) ;
extern int (*digitalRead) (int pin) ;
extern void (*pwmSetMode) (int mode) ;
extern void (*pwmSetRange) (unsigned int range) ;
extern void (*pwmSetClock) (int divisor) ;
// On-Board Raspberry Pi hardware specific stuff
extern int piBoardRev (void) ;
extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ;
extern int wpiPinToGpio (int wpiPin) ;
extern int physPinToGpio (int physPin) ;
extern void setPadDrive (int group, int value) ;
extern int getAlt (int pin) ;
extern void pwmToneWrite (int pin, int freq) ;
extern void digitalWriteByte (int value) ;
extern void pwmSetMode (int mode) ;
extern void pwmSetRange (unsigned int range) ;
extern void pwmSetClock (int divisor) ;
extern void gpioClockSet (int pin, int freq) ;
// Interrupts
// (Also Pi hardware specific)
extern int (*waitForInterrupt) (int pin, int mS) ;
extern int waitForInterrupt (int pin, int mS) ;
extern int wiringPiISR (int pin, int mode, void (*function)(void)) ;
// Threads
extern int piThreadCreate (void *(*fn)(void *)) ;
extern void piLock (int key) ;
extern void piUnlock (int key) ;
extern int piThreadCreate (void *(*fn)(void *)) ;
extern void piLock (int key) ;
extern void piUnlock (int key) ;
// Schedulling priority
extern int piHiPri (int pri) ;
extern int piHiPri (const int pri) ;
// Extras from arduino land
@@ -124,3 +221,5 @@ extern unsigned int micros (void) ;
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -22,15 +22,93 @@
***********************************************************************
*/
/*
* Notes:
* The Linux I2C code is actually the same (almost) as the SMBus code.
* SMBus is System Management Bus - and in essentially I2C with some
* additional functionality added, and stricter controls on the electrical
* specifications, etc. however I2C does work well with it and the
* protocols work over both.
*
* I'm directly including the SMBus functions here as some Linux distros
* lack the correct header files, and also some header files are GPLv2
* rather than the LGPL that wiringPi is released under - presumably because
* originally no-one expected I2C/SMBus to be used outside the kernel -
* however enter the Raspberry Pi with people now taking directly to I2C
* devices without going via the kernel...
*
* This may ultimately reduce the flexibility of this code, but it won't be
* hard to maintain it and keep it current, should things change.
*
* Information here gained from: kernel/Documentation/i2c/dev-interface
* as well as other online resources.
*********************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include "wiringPi.h"
#include "wiringPiI2C.h"
// I2C definitions
#define I2C_SLAVE 0x0703
#define I2C_SMBUS 0x0720 /* SMBus-level access */
#define I2C_SMBUS_READ 1
#define I2C_SMBUS_WRITE 0
// SMBus transaction types
#define I2C_SMBUS_QUICK 0
#define I2C_SMBUS_BYTE 1
#define I2C_SMBUS_BYTE_DATA 2
#define I2C_SMBUS_WORD_DATA 3
#define I2C_SMBUS_PROC_CALL 4
#define I2C_SMBUS_BLOCK_DATA 5
#define I2C_SMBUS_I2C_BLOCK_BROKEN 6
#define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
#define I2C_SMBUS_I2C_BLOCK_DATA 8
// SMBus messages
#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
#define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
// Structures used in the ioctl() calls
union i2c_smbus_data
{
uint8_t byte ;
uint16_t word ;
uint8_t block [I2C_SMBUS_BLOCK_MAX + 2] ; // block [0] is used for length + one more for PEC
} ;
struct i2c_smbus_ioctl_data
{
char read_write ;
uint8_t command ;
int size ;
union i2c_smbus_data *data ;
} ;
static inline int i2c_smbus_access (int fd, char rw, uint8_t command, int size, union i2c_smbus_data *data)
{
struct i2c_smbus_ioctl_data args ;
args.read_write = rw ;
args.command = command ;
args.size = size ;
args.data = data ;
return ioctl (fd, I2C_SMBUS, &args) ;
}
/*
* wiringPiI2CRead:
@@ -40,7 +118,12 @@
int wiringPiI2CRead (int fd)
{
return i2c_smbus_read_byte (fd) ;
union i2c_smbus_data data ;
if (i2c_smbus_access (fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
return -1 ;
else
return data.byte & 0xFF ;
}
@@ -52,12 +135,22 @@ int wiringPiI2CRead (int fd)
int wiringPiI2CReadReg8 (int fd, int reg)
{
return i2c_smbus_read_byte_data (fd, reg) ;
union i2c_smbus_data data;
if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data))
return -1 ;
else
return data.byte & 0xFF ;
}
int wiringPiI2CReadReg16 (int fd, int reg)
{
return i2c_smbus_read_word_data (fd, reg) ;
union i2c_smbus_data data;
if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data))
return -1 ;
else
return data.word & 0xFFFF ;
}
@@ -69,7 +162,7 @@ int wiringPiI2CReadReg16 (int fd, int reg)
int wiringPiI2CWrite (int fd, int data)
{
return i2c_smbus_write_byte (fd, data) ;
return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ;
}
@@ -79,14 +172,41 @@ int wiringPiI2CWrite (int fd, int data)
*********************************************************************************
*/
int wiringPiI2CWriteReg8 (int fd, int reg, int data)
int wiringPiI2CWriteReg8 (int fd, int reg, int value)
{
return i2c_smbus_write_byte_data (fd, reg, data) ;
union i2c_smbus_data data ;
data.byte = value ;
return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data) ;
}
int wiringPiI2CWriteReg16 (int fd, int reg, int data)
int wiringPiI2CWriteReg16 (int fd, int reg, int value)
{
return i2c_smbus_write_word_data (fd, reg, data) ;
union i2c_smbus_data data ;
data.word = value ;
return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ;
}
/*
* wiringPiI2CSetupInterface:
* Undocumented access to set the interface explicitly - might be used
* for the Pi's 2nd I2C interface...
*********************************************************************************
*/
int wiringPiI2CSetupInterface (const char *device, int devId)
{
int fd ;
if ((fd = open (device, O_RDWR)) < 0)
return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ;
if (ioctl (fd, I2C_SLAVE, devId) < 0)
return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ;
return fd ;
}
@@ -96,27 +216,17 @@ int wiringPiI2CWriteReg16 (int fd, int reg, int data)
*********************************************************************************
*/
int wiringPiI2CSetup (int devId)
int wiringPiI2CSetup (const int devId)
{
int rev, fd ;
char *device ;
int rev ;
const char *device ;
if ((rev = piBoardRev ()) < 0)
{
fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ;
exit (1) ;
}
rev = piBoardRev () ;
if (rev == 1)
device = "/dev/i2c-0" ;
else
device = "/dev/i2c-1" ;
if ((fd = open (device, O_RDWR)) < 0)
return -1 ;
if (ioctl (fd, I2C_SLAVE, devId) < 0)
return -1 ;
return fd ;
return wiringPiI2CSetupInterface (device, devId) ;
}

View File

@@ -26,15 +26,16 @@
extern "C" {
#endif
extern int wiringPiI2CRead (int fd) ;
extern int wiringPiI2CReadReg8 (int fd, int reg) ;
extern int wiringPiI2CReadReg16 (int fd, int reg) ;
extern int wiringPiI2CRead (int fd) ;
extern int wiringPiI2CReadReg8 (int fd, int reg) ;
extern int wiringPiI2CReadReg16 (int fd, int reg) ;
extern int wiringPiI2CWrite (int fd, int data) ;
extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ;
extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ;
extern int wiringPiI2CWrite (int fd, int data) ;
extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ;
extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ;
int wiringPiI2CSetup (int devId) ;
extern int wiringPiI2CSetupInterface (const char *device, int devId) ;
extern int wiringPiI2CSetup (const int devId) ;
#ifdef __cplusplus
}

View File

@@ -1,7 +1,7 @@
/*
* wiringPiSPI.c:
* Simplified SPI access routines
* Copyright (c) 2012 Gordon Henderson
* Copyright (c) 2012-2015 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -25,20 +25,23 @@
#include <stdint.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include "wiringPi.h"
#include "wiringPiSPI.h"
// The SPI bus parameters
// Variables as they need to be passed as pointers later on
static char *spiDev0 = "/dev/spidev0.0" ;
static char *spiDev1 = "/dev/spidev0.1" ;
static uint8_t spiMode = 0 ;
static uint8_t spiBPW = 8 ;
static uint16_t spiDelay = 0;
const static char *spiDev0 = "/dev/spidev0.0" ;
const static char *spiDev1 = "/dev/spidev0.1" ;
const static uint8_t spiBPW = 8 ;
const static uint16_t spiDelay = 0 ;
static uint32_t spiSpeeds [2] ;
static int spiFds [2] ;
@@ -71,6 +74,11 @@ int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
channel &= 1 ;
// Mentioned in spidev.h but not used in the original kernel documentation
// test program )-:
memset (&spi, 0, sizeof (spi)) ;
spi.tx_buf = (unsigned long)data ;
spi.rx_buf = (unsigned long)data ;
spi.len = len ;
@@ -83,35 +91,46 @@ int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
/*
* wiringPiSPISetup:
* Open the SPI device, and set it up, etc.
* wiringPiSPISetupMode:
* Open the SPI device, and set it up, with the mode, etc.
*********************************************************************************
*/
int wiringPiSPISetup (int channel, int speed)
int wiringPiSPISetupMode (int channel, int speed, int mode)
{
int fd ;
channel &= 1 ;
mode &= 3 ; // Mode is 0, 1, 2 or 3
channel &= 1 ; // Channel is 0 or 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 ;
// Set SPI parameters.
// Why are we reading it afterwriting it? I've no idea, but for now I'm blindly
// copying example code I've seen online...
if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_WR_MODE, &mode) < 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 ;
}
/*
* wiringPiSPISetup:
* Open the SPI device, and set it up, etc. in the default MODE 0
*********************************************************************************
*/
int wiringPiSPISetup (int channel, int speed)
{
return wiringPiSPISetupMode (channel, speed, 0) ;
}

View File

@@ -1,7 +1,7 @@
/*
* wiringPiSPI.h:
* Simplified SPI access routines
* Copyright (c) 2012 Gordon Henderson
* Copyright (c) 2012-2015 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -26,9 +26,10 @@
extern "C" {
#endif
int wiringPiSPIGetFd (int channel) ;
int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ;
int wiringPiSPISetup (int channel, int speed) ;
int wiringPiSPIGetFd (int channel) ;
int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ;
int wiringPiSPISetupMode (int channel, int speed, int mode) ;
int wiringPiSPISetup (int channel, int speed) ;
#ifdef __cplusplus
}

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 ;
@@ -60,6 +60,7 @@ int serialOpen (char *device, int baud)
case 1200: myBaud = B1200 ; break ;
case 1800: myBaud = B1800 ; break ;
case 2400: myBaud = B2400 ; break ;
case 4800: myBaud = B4800 ; break ;
case 9600: myBaud = B9600 ; break ;
case 19200: myBaud = B19200 ; break ;
case 38400: myBaud = B38400 ; break ;
@@ -116,7 +117,7 @@ int serialOpen (char *device, int baud)
*********************************************************************************
*/
void serialFlush (int fd)
void serialFlush (const int fd)
{
tcflush (fd, TCIOFLUSH) ;
}
@@ -128,7 +129,7 @@ void serialFlush (int fd)
*********************************************************************************
*/
void serialClose (int fd)
void serialClose (const int fd)
{
close (fd) ;
}
@@ -140,7 +141,7 @@ void serialClose (int fd)
*********************************************************************************
*/
void serialPutchar (int fd, unsigned char c)
void serialPutchar (const int fd, const unsigned char c)
{
write (fd, &c, 1) ;
}
@@ -152,7 +153,7 @@ void serialPutchar (int fd, unsigned char c)
*********************************************************************************
*/
void serialPuts (int fd, char *s)
void serialPuts (const int fd, const char *s)
{
write (fd, s, strlen (s)) ;
}
@@ -163,7 +164,7 @@ void serialPuts (int fd, char *s)
*********************************************************************************
*/
void serialPrintf (int fd, char *message, ...)
void serialPrintf (const int fd, const char *message, ...)
{
va_list argp ;
char buffer [1024] ;
@@ -182,7 +183,7 @@ void serialPrintf (int fd, char *message, ...)
*********************************************************************************
*/
int serialDataAvail (int fd)
int serialDataAvail (const int fd)
{
int result ;
@@ -201,7 +202,7 @@ int serialDataAvail (int fd)
*********************************************************************************
*/
int serialGetchar (int fd)
int serialGetchar (const int fd)
{
uint8_t x ;

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
}

View File

@@ -56,7 +56,6 @@ uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order)
return value;
}
/*
* shiftOut:
* Shift data out to a clocked source

View File

@@ -33,8 +33,8 @@
extern "C" {
#endif
extern uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order) ;
extern void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) ;
extern uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order) ;
extern void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) ;
#ifdef __cplusplus
}

731
wiringPi/wpiExtensions.c Normal file
View File

@@ -0,0 +1,731 @@
/*
* extensions.c:
* Originally part of the GPIO program to test, peek, poke and otherwise
* noodle with the GPIO hardware on the Raspberry Pi.
* Now used as a general purpose library to allow systems to dynamically
* add in new devices into wiringPi at program run-time.
* Copyright (c) 2012-2015 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <wiringPi.h>
#include "mcp23008.h"
#include "mcp23016.h"
#include "mcp23017.h"
#include "mcp23s08.h"
#include "mcp23s17.h"
#include "sr595.h"
#include "pcf8574.h"
#include "pcf8591.h"
#include "mcp3002.h"
#include "mcp3004.h"
#include "mcp4802.h"
#include "mcp3422.h"
#include "max31855.h"
#include "max5322.h"
#include "sn3218.h"
#include "drcSerial.h"
#include "wpiExtensions.h"
extern int wiringPiDebug ;
static int verbose ;
static char errorMessage [1024] ;
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
// Local structure to hold details
struct extensionFunctionStruct
{
const char *name ;
int (*function)(char *progName, int pinBase, char *params) ;
} ;
/*
* verbError:
* Convenient error handling
*********************************************************************************
*/
static void verbError (const char *message, ...)
{
va_list argp ;
va_start (argp, message) ;
vsnprintf (errorMessage, 1023, message, argp) ;
va_end (argp) ;
if (verbose)
fprintf (stderr, "%s\n", errorMessage) ;
}
/*
* extractInt:
* Check & return an integer at the given location (prefixed by a :)
*********************************************************************************
*/
static char *extractInt (char *progName, char *p, int *num)
{
if (*p != ':')
{
verbError ("%s: colon expected", progName) ;
return NULL ;
}
++p ;
if (!isdigit (*p))
{
verbError ("%s: digit expected", progName) ;
return NULL ;
}
*num = strtol (p, NULL, 0) ;
while (isdigit (*p))
++p ;
return p ;
}
/*
* extractStr:
* Check & return a string at the given location (prefixed by a :)
*********************************************************************************
*/
static char *extractStr (char *progName, char *p, char **str)
{
char *q, *r ;
if (*p != ':')
{
verbError ("%s: colon expected", progName) ;
return NULL ;
}
++p ;
if (!isprint (*p))
{
verbError ("%s: character expected", progName) ;
return NULL ;
}
q = p ;
while ((*q != 0) && (*q != ':'))
++q ;
*str = r = calloc (q - p + 2, 1) ; // Zeros it
while (p != q)
*r++ = *p++ ;
return p ;
}
/*
* doExtensionMcp23008:
* MCP23008 - 8-bit I2C GPIO expansion chip
* mcp23002:base:i2cAddr
*********************************************************************************
*/
static int doExtensionMcp23008 (char *progName, int pinBase, char *params)
{
int i2c ;
if ((params = extractInt (progName, params, &i2c)) == NULL)
return FALSE ;
if ((i2c < 0x01) || (i2c > 0x77))
{
verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
return FALSE ;
}
mcp23008Setup (pinBase, i2c) ;
return TRUE ;
}
/*
* doExtensionMcp23016:
* MCP230016- 16-bit I2C GPIO expansion chip
* mcp23016:base:i2cAddr
*********************************************************************************
*/
static int doExtensionMcp23016 (char *progName, int pinBase, char *params)
{
int i2c ;
if ((params = extractInt (progName, params, &i2c)) == NULL)
return FALSE ;
if ((i2c < 0x03) || (i2c > 0x77))
{
verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
return FALSE ;
}
mcp23016Setup (pinBase, i2c) ;
return TRUE ;
}
/*
* doExtensionMcp23017:
* MCP230017- 16-bit I2C GPIO expansion chip
* mcp23017:base:i2cAddr
*********************************************************************************
*/
static int doExtensionMcp23017 (char *progName, int pinBase, char *params)
{
int i2c ;
if ((params = extractInt (progName, params, &i2c)) == NULL)
return FALSE ;
if ((i2c < 0x03) || (i2c > 0x77))
{
verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
return FALSE ;
}
mcp23017Setup (pinBase, i2c) ;
return TRUE ;
}
/*
* doExtensionMcp23s08:
* MCP23s08 - 8-bit SPI GPIO expansion chip
* mcp23s08:base:spi:port
*********************************************************************************
*/
static int doExtensionMcp23s08 (char *progName, int pinBase, char *params)
{
int spi, port ;
if ((params = extractInt (progName, params, &spi)) == NULL)
return FALSE ;
if ((spi < 0) || (spi > 1))
{
verbError ("%s: SPI address (%d) out of range", progName, spi) ;
return FALSE ;
}
if ((params = extractInt (progName, params, &port)) == NULL)
return FALSE ;
if ((port < 0) || (port > 7))
{
verbError ("%s: port address (%d) out of range", progName, port) ;
return FALSE ;
}
mcp23s08Setup (pinBase, spi, port) ;
return TRUE ;
}
/*
* doExtensionMcp23s17:
* MCP23s17 - 16-bit SPI GPIO expansion chip
* mcp23s17:base:spi:port
*********************************************************************************
*/
static int doExtensionMcp23s17 (char *progName, int pinBase, char *params)
{
int spi, port ;
if ((params = extractInt (progName, params, &spi)) == NULL)
return FALSE ;
if ((spi < 0) || (spi > 1))
{
verbError ("%s: SPI address (%d) out of range", progName, spi) ;
return FALSE ;
}
if ((params = extractInt (progName, params, &port)) == NULL)
return FALSE ;
if ((port < 0) || (port > 7))
{
verbError ("%s: port address (%d) out of range", progName, port) ;
return FALSE ;
}
mcp23s17Setup (pinBase, spi, port) ;
return TRUE ;
}
/*
* doExtensionSr595:
* Shift Register 74x595
* sr595:base:pins:data:clock:latch
*********************************************************************************
*/
static int doExtensionSr595 (char *progName, int pinBase, char *params)
{
int pins, data, clock, latch ;
// Extract pins
if ((params = extractInt (progName, params, &pins)) == NULL)
return FALSE ;
if ((pins < 8) || (pins > 32))
{
verbError ("%s: pin count (%d) out of range - 8-32 expected.", progName, pins) ;
return FALSE ;
}
if ((params = extractInt (progName, params, &data)) == NULL)
return FALSE ;
if ((params = extractInt (progName, params, &clock)) == NULL)
return FALSE ;
if ((params = extractInt (progName, params, &latch)) == NULL)
return FALSE ;
sr595Setup (pinBase, pins, data, clock, latch) ;
return TRUE ;
}
/*
* doExtensionPcf8574:
* Digital IO (Crude!)
* pcf8574:base:i2cAddr
*********************************************************************************
*/
static int doExtensionPcf8574 (char *progName, int pinBase, char *params)
{
int i2c ;
if ((params = extractInt (progName, params, &i2c)) == NULL)
return FALSE ;
if ((i2c < 0x03) || (i2c > 0x77))
{
verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
return FALSE ;
}
pcf8574Setup (pinBase, i2c) ;
return TRUE ;
}
/*
* doExtensionPcf8591:
* Analog IO
* pcf8591:base:i2cAddr
*********************************************************************************
*/
static int doExtensionPcf8591 (char *progName, int pinBase, char *params)
{
int i2c ;
if ((params = extractInt (progName, params, &i2c)) == NULL)
return FALSE ;
if ((i2c < 0x03) || (i2c > 0x77))
{
verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
return FALSE ;
}
pcf8591Setup (pinBase, i2c) ;
return TRUE ;
}
/*
* doExtensionMax31855:
* Analog IO
* max31855:base:spiChan
*********************************************************************************
*/
static int doExtensionMax31855 (char *progName, int pinBase, char *params)
{
int spi ;
if ((params = extractInt (progName, params, &spi)) == NULL)
return FALSE ;
if ((spi < 0) || (spi > 1))
{
verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
return FALSE ;
}
max31855Setup (pinBase, spi) ;
return TRUE ;
}
/*
* doExtensionMcp3002:
* Analog IO
* mcp3002:base:spiChan
*********************************************************************************
*/
static int doExtensionMcp3002 (char *progName, int pinBase, char *params)
{
int spi ;
if ((params = extractInt (progName, params, &spi)) == NULL)
return FALSE ;
if ((spi < 0) || (spi > 1))
{
verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
return FALSE ;
}
mcp3002Setup (pinBase, spi) ;
return TRUE ;
}
/*
* doExtensionMcp3004:
* Analog IO
* mcp3004:base:spiChan
*********************************************************************************
*/
static int doExtensionMcp3004 (char *progName, int pinBase, char *params)
{
int spi ;
if ((params = extractInt (progName, params, &spi)) == NULL)
return FALSE ;
if ((spi < 0) || (spi > 1))
{
verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
return FALSE ;
}
mcp3004Setup (pinBase, spi) ;
return TRUE ;
}
/*
* doExtensionMax5322:
* Analog O
* max5322:base:spiChan
*********************************************************************************
*/
static int doExtensionMax5322 (char *progName, int pinBase, char *params)
{
int spi ;
if ((params = extractInt (progName, params, &spi)) == NULL)
return FALSE ;
if ((spi < 0) || (spi > 1))
{
verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
return FALSE ;
}
max5322Setup (pinBase, spi) ;
return TRUE ;
}
/*
* doExtensionMcp4802:
* Analog IO
* mcp4802:base:spiChan
*********************************************************************************
*/
static int doExtensionMcp4802 (char *progName, int pinBase, char *params)
{
int spi ;
if ((params = extractInt (progName, params, &spi)) == NULL)
return FALSE ;
if ((spi < 0) || (spi > 1))
{
verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
return FALSE ;
}
mcp4802Setup (pinBase, spi) ;
return TRUE ;
}
/*
* doExtensionSn3218:
* Analog Output (LED Driver)
* sn3218:base
*********************************************************************************
*/
static int doExtensionSn3218 (char *progName, int pinBase, char *params)
{
sn3218Setup (pinBase) ;
return TRUE ;
}
/*
* doExtensionMcp3422:
* Analog IO
* mcp3422:base:i2cAddr
*********************************************************************************
*/
static int doExtensionMcp3422 (char *progName, int pinBase, char *params)
{
int i2c, sampleRate, gain ;
if ((params = extractInt (progName, params, &i2c)) == NULL)
return FALSE ;
if ((i2c < 0x03) || (i2c > 0x77))
{
verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
return FALSE ;
}
if ((params = extractInt (progName, params, &sampleRate)) == NULL)
return FALSE ;
if ((sampleRate < 0) || (sampleRate > 3))
{
verbError ("%s: sample rate (%d) out of range", progName, sampleRate) ;
return FALSE ;
}
if ((params = extractInt (progName, params, &gain)) == NULL)
return FALSE ;
if ((gain < 0) || (gain > 3))
{
verbError ("%s: gain (%d) out of range", progName, gain) ;
return FALSE ;
}
mcp3422Setup (pinBase, i2c, sampleRate, gain) ;
return TRUE ;
}
/*
* doExtensionDrcS:
* Interface to a DRC Serial system
* drcs:base:pins:serialPort:baud
*********************************************************************************
*/
static int doExtensionDrcS (char *progName, int pinBase, char *params)
{
char *port ;
int pins, baud ;
if ((params = extractInt (progName, params, &pins)) == NULL)
return FALSE ;
if ((pins < 1) || (pins > 100))
{
verbError ("%s: pins (%d) out of range (2-100)", progName, pins) ;
return FALSE ;
}
if ((params = extractStr (progName, params, &port)) == NULL)
return FALSE ;
if (strlen (port) == 0)
{
verbError ("%s: serial port device name required", progName) ;
return FALSE ;
}
if ((params = extractInt (progName, params, &baud)) == NULL)
return FALSE ;
if ((baud < 1) || (baud > 4000000))
{
verbError ("%s: baud rate (%d) out of range", progName, baud) ;
return FALSE ;
}
drcSetupSerial (pinBase, pins, port, baud) ;
return TRUE ;
}
/*
* Function list
*********************************************************************************
*/
static struct extensionFunctionStruct extensionFunctions [] =
{
{ "mcp23008", &doExtensionMcp23008 },
{ "mcp23016", &doExtensionMcp23016 },
{ "mcp23017", &doExtensionMcp23017 },
{ "mcp23s08", &doExtensionMcp23s08 },
{ "mcp23s17", &doExtensionMcp23s17 },
{ "sr595", &doExtensionSr595 },
{ "pcf8574", &doExtensionPcf8574 },
{ "pcf8591", &doExtensionPcf8591 },
{ "mcp3002", &doExtensionMcp3002 },
{ "mcp3004", &doExtensionMcp3004 },
{ "mcp4802", &doExtensionMcp4802 },
{ "mcp3422", &doExtensionMcp3422 },
{ "max31855", &doExtensionMax31855 },
{ "max5322", &doExtensionMax5322 },
{ "sn3218", &doExtensionSn3218 },
{ "drcs", &doExtensionDrcS },
{ NULL, NULL },
} ;
/*
* loadWPiExtension:
* Load in a wiringPi extension
* The extensionData always starts with the name, a colon then the pinBase
* number. Other parameters after that are decoded by the module in question.
*********************************************************************************
*/
int loadWPiExtension (char *progName, char *extensionData, int printErrors)
{
char *p ;
char *extension = extensionData ;
struct extensionFunctionStruct *extensionFn ;
int pinBase = 0 ;
verbose = printErrors ;
// Get the extension name by finding the first colon
p = extension ;
while (*p != ':')
{
if (!*p) // ran out of characters
{
verbError ("%s: extension name not terminated by a colon", progName) ;
return FALSE ;
}
++p ;
}
*p++ = 0 ;
// Simple ATOI code
if (!isdigit (*p))
{
verbError ("%s: pinBase number expected after extension name", progName) ;
return FALSE ;
}
while (isdigit (*p))
{
if (pinBase > 1000000000) // Lets be realistic here...
{
verbError ("%s: pinBase too large", progName) ;
return FALSE ;
}
pinBase = pinBase * 10 + (*p - '0') ;
++p ;
}
if (pinBase < 64)
{
verbError ("%s: pinBase (%d) too small. Minimum is 64.", progName, pinBase) ;
return FALSE ;
}
// Search for extensions:
for (extensionFn = extensionFunctions ; extensionFn->name != NULL ; ++extensionFn)
{
if (strcmp (extensionFn->name, extension) == 0)
return extensionFn->function (progName, pinBase, p) ;
}
verbError ("%s: extension %s not found", progName, extension) ;
return FALSE ;
}

26
wiringPi/wpiExtensions.h Normal file
View File

@@ -0,0 +1,26 @@
/*
* extensions.h:
* Part of the GPIO program to test, peek, poke and otherwise
* noodle with the GPIO hardware on the Raspberry Pi.
* Copyright (c) 2012-2015 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
extern int loadWPiExtension (char *progName, char *extensionData, int verbose) ;