mirror of
git://soft.sys114.com/WiringPi2-Python
synced 2026-02-06 21:19:48 +09:00
WiringPi V2.0 for Python2 & Python3
This commit is contained in:
163
WiringPi/wiringPi/Makefile
Normal file
163
WiringPi/wiringPi/Makefile
Normal file
@@ -0,0 +1,163 @@
|
||||
# ;
|
||||
# Makefile:
|
||||
# wiringPi - Wiring Compatable library for the Raspberry Pi
|
||||
#
|
||||
# Copyright (c) 2012 Gordon Henderson
|
||||
#################################################################################
|
||||
# This file is part of wiringPi:
|
||||
# https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
#
|
||||
# wiringPi is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# wiringPi is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
#################################################################################
|
||||
|
||||
DYN_VERS_MAJ=2
|
||||
DYN_VERS_MIN=0
|
||||
|
||||
VERSION=$(DYN_VERS_MAJ).$(DYN_VERS_MIN)
|
||||
DESTDIR=/usr
|
||||
PREFIX=/local
|
||||
|
||||
STATIC=libwiringPi.a
|
||||
DYNAMIC=libwiringPi.so.$(VERSION)
|
||||
|
||||
#DEBUG = -g -O0
|
||||
DEBUG = -O2
|
||||
CC = gcc
|
||||
INCLUDE = -I.
|
||||
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe -fPIC
|
||||
|
||||
LIBS =
|
||||
|
||||
# Should not alter anything below this line
|
||||
###############################################################################
|
||||
|
||||
SRC = wiringPi.c \
|
||||
wiringSerial.c wiringShift.c \
|
||||
piHiPri.c piThread.c \
|
||||
wiringPiSPI.c wiringPiI2C.c \
|
||||
softPwm.c softTone.c \
|
||||
mcp23s08.c mcp23008.c \
|
||||
mcp23s17.c mcp23017.c sr595.c \
|
||||
piFace.c gertboard.c \
|
||||
piNes.c ds1302.c lcd.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
all: $(DYNAMIC)
|
||||
|
||||
static: $(STATIC)
|
||||
|
||||
$(STATIC): $(OBJ)
|
||||
@echo "[Link (Static)]"
|
||||
@ar rcs $(STATIC) $(OBJ)
|
||||
@ranlib $(STATIC)
|
||||
# @size $(STATIC)
|
||||
|
||||
$(DYNAMIC): $(OBJ)
|
||||
@echo "[Link (Dynamic)]"
|
||||
@$(CC) -shared -Wl,-soname,libwiringPi.so -o libwiringPi.so.$(VERSION) -lpthread $(OBJ)
|
||||
|
||||
.c.o:
|
||||
@echo [Compile] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
.PHONEY: clean
|
||||
clean:
|
||||
rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.*
|
||||
|
||||
.PHONEY: tags
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
.PHONEY: install
|
||||
install: $(DYNAMIC)
|
||||
@echo "[Install]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringPi.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringSerial.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringShift.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 gertboard.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 piFace.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 piNes.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 ds1302.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 softPwm.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 lcd.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23008.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 sr595.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION)
|
||||
@ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so
|
||||
@ldconfig
|
||||
|
||||
.PHONEY: install-static
|
||||
install-static: $(STATIC)
|
||||
@echo "[Install Static]"
|
||||
@install -m 0755 libwiringPi.a $(DESTDIR)$(PREFIX)/lib
|
||||
|
||||
.PHONEY: 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/piFace.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/piNes.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/ds1302.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/softPwm.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/softTone.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/lcd.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23008.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/sr595.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.*
|
||||
@ldconfig
|
||||
|
||||
|
||||
.PHONEY: depend
|
||||
depend:
|
||||
makedepend -Y $(SRC) $(SRC_I2C)
|
||||
|
||||
# DO NOT DELETE
|
||||
|
||||
wiringPi.o: wiringPi.h
|
||||
wiringSerial.o: wiringSerial.h
|
||||
wiringShift.o: wiringPi.h wiringShift.h
|
||||
piHiPri.o: wiringPi.h
|
||||
piThread.o: wiringPi.h
|
||||
wiringPiSPI.o: wiringPiSPI.h
|
||||
wiringPiI2C.o: wiringPi.h wiringPiI2C.h
|
||||
softPwm.o: wiringPi.h softPwm.h
|
||||
softTone.o: wiringPi.h softTone.h
|
||||
mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h
|
||||
mcp23008.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23008.h
|
||||
mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h
|
||||
mcp23017.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23017.h
|
||||
sr595.o: wiringPi.h sr595.h
|
||||
piFace.o: wiringPi.h wiringPiSPI.h piFace.h mcp23x0817.h
|
||||
gertboard.o: wiringPi.h wiringPiSPI.h gertboard.h
|
||||
piNes.o: wiringPi.h piNes.h
|
||||
ds1302.o: wiringPi.h ds1302.h
|
||||
lcd.o: wiringPi.h lcd.h
|
||||
239
WiringPi/wiringPi/ds1302.c
Normal file
239
WiringPi/wiringPi/ds1302.c
Normal file
@@ -0,0 +1,239 @@
|
||||
/*
|
||||
* ds1302.c:
|
||||
* Real Time clock
|
||||
*
|
||||
* Copyright (c) 2013 Gordon Henderson.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "ds1302.h"
|
||||
|
||||
// Register defines
|
||||
|
||||
#define RTC_SECS 0
|
||||
#define RTC_MINS 1
|
||||
#define RTC_HOURS 2
|
||||
#define RTC_DATE 3
|
||||
#define RTC_MONTH 4
|
||||
#define RTC_DAY 5
|
||||
#define RTC_YEAR 6
|
||||
#define RTC_WP 7
|
||||
#define RTC_TC 8
|
||||
#define RTC_BM 31
|
||||
|
||||
|
||||
// Locals
|
||||
|
||||
static int dPin, cPin, sPin ;
|
||||
|
||||
/*
|
||||
* dsShiftIn:
|
||||
* Shift a number in from the chip, LSB first. Note that the data is
|
||||
* sampled on the trailing edge of the last clock, so it's valid immediately.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
unsigned int dsShiftIn (void)
|
||||
{
|
||||
uint8_t value = 0 ;
|
||||
int i ;
|
||||
|
||||
pinMode (dPin, INPUT) ; delayMicroseconds (1) ;
|
||||
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
{
|
||||
value |= (digitalRead (dPin) << i) ;
|
||||
digitalWrite (cPin, HIGH) ; delayMicroseconds (1) ;
|
||||
digitalWrite (cPin, LOW) ; delayMicroseconds (1) ;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* dsShiftOut:
|
||||
* A normal LSB-first shift-out, just slowed down a bit - the Pi is
|
||||
* a bit faster than the chip can handle.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void dsShiftOut (unsigned int data)
|
||||
{
|
||||
int i ;
|
||||
|
||||
pinMode (dPin, OUTPUT) ;
|
||||
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
{
|
||||
digitalWrite (dPin, data & (1 << i)) ; delayMicroseconds (1) ;
|
||||
digitalWrite (cPin, HIGH) ; delayMicroseconds (1) ;
|
||||
digitalWrite (cPin, LOW) ; delayMicroseconds (1) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ds1302regRead: ds1302regWrite:
|
||||
* Read/Write a value to an RTC Register or RAM location on the chip
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static unsigned int ds1302regRead (int reg)
|
||||
{
|
||||
unsigned int data ;
|
||||
|
||||
digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
|
||||
dsShiftOut (reg) ;
|
||||
data = dsShiftIn () ;
|
||||
digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
|
||||
|
||||
return data ;
|
||||
}
|
||||
|
||||
static void ds1302regWrite (int reg, unsigned int data)
|
||||
{
|
||||
digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
|
||||
dsShiftOut (reg) ;
|
||||
dsShiftOut (data) ;
|
||||
digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ds1302rtcWrite: ds1302rtcRead:
|
||||
* Writes/Reads the data to/from the RTC register
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
unsigned int ds1302rtcRead (int reg)
|
||||
{
|
||||
return ds1302regRead (0x81 | ((reg & 0x1F) << 1)) ;
|
||||
}
|
||||
|
||||
void ds1302rtcWrite (int reg, unsigned int data)
|
||||
{
|
||||
ds1302regWrite (0x80 | ((reg & 0x1F) << 1), data) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ds1302ramWrite: ds1302ramRead:
|
||||
* Writes/Reads the data to/from the RTC register
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
unsigned int ds1302ramRead (int addr)
|
||||
{
|
||||
return ds1302regRead (0xC1 | ((addr & 0x1F) << 1)) ;
|
||||
}
|
||||
|
||||
void ds1302ramWrite (int addr, unsigned int data)
|
||||
{
|
||||
ds1302regWrite ( 0xC0 | ((addr & 0x1F) << 1), data) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* ds1302clockRead:
|
||||
* Read all 8 bytes of the clock in a single operation
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void ds1302clockRead (int clockData [8])
|
||||
{
|
||||
int i ;
|
||||
unsigned int regVal = 0x81 | ((RTC_BM & 0x1F) << 1) ;
|
||||
|
||||
digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
|
||||
|
||||
dsShiftOut (regVal) ;
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
clockData [i] = dsShiftIn () ;
|
||||
|
||||
digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ds1302clockWrite:
|
||||
* Write all 8 bytes of the clock in a single operation
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void ds1302clockWrite (int clockData [8])
|
||||
{
|
||||
int i ;
|
||||
unsigned int regVal = 0x80 | ((RTC_BM & 0x1F) << 1) ;
|
||||
|
||||
digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
|
||||
|
||||
dsShiftOut (regVal) ;
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
dsShiftOut (clockData [i]) ;
|
||||
|
||||
digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ds1302trickleCharge:
|
||||
* Set the bits on the trickle charger.
|
||||
* Probably best left alone...
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void ds1302trickleCharge (int diodes, int resistors)
|
||||
{
|
||||
if (diodes + resistors == 0)
|
||||
ds1302rtcWrite (RTC_TC, 0x5C) ; // Disabled
|
||||
else
|
||||
ds1302rtcWrite (RTC_TC, 0xA0 | ((diodes & 3) << 2) | (resistors & 3)) ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* ds1302setup:
|
||||
* Initialise the chip & remember the pins we're using
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void ds1302setup (int clockPin, int dataPin, int csPin)
|
||||
{
|
||||
dPin = dataPin ;
|
||||
cPin = clockPin ;
|
||||
sPin = csPin ;
|
||||
|
||||
digitalWrite (dPin, LOW) ;
|
||||
digitalWrite (cPin, LOW) ;
|
||||
digitalWrite (sPin, LOW) ;
|
||||
|
||||
pinMode (dPin, OUTPUT) ;
|
||||
pinMode (cPin, OUTPUT) ;
|
||||
pinMode (sPin, OUTPUT) ;
|
||||
|
||||
ds1302rtcWrite (RTC_WP, 0) ; // Remove write-protect
|
||||
}
|
||||
44
WiringPi/wiringPi/ds1302.h
Normal file
44
WiringPi/wiringPi/ds1302.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* ds1302.h:
|
||||
* Real Time clock
|
||||
*
|
||||
* Copyright (c) 2013 Gordon Henderson.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern unsigned int ds1302rtcRead (int reg) ;
|
||||
extern void ds1302rtcWrite (int reg, unsigned int data) ;
|
||||
|
||||
extern unsigned int ds1302ramRead (int addr) ;
|
||||
extern void ds1302ramWrite (int addr, unsigned int data) ;
|
||||
|
||||
extern void ds1302clockRead (int clockData [8]) ;
|
||||
extern void ds1302clockWrite (int clockData [8]) ;
|
||||
|
||||
extern void ds1302trickleCharge (int diodes, int resistors) ;
|
||||
|
||||
extern void ds1302setup (int clockPin, int dataPin, int csPin) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
166
WiringPi/wiringPi/gertboard.c
Normal file
166
WiringPi/wiringPi/gertboard.c
Normal file
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* gertboard.c:
|
||||
* Access routines for the SPI devices on the Gertboard
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
*
|
||||
* The Gertboard has:
|
||||
*
|
||||
* An MCP3002 dual-channel A to D convertor connected
|
||||
* to the SPI bus, selected by chip-select A, and:
|
||||
*
|
||||
* An MCP4802 dual-channel D to A convertor connected
|
||||
* to the SPI bus, selected via chip-select B.
|
||||
*
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/spi/spidev.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiSPI.h"
|
||||
|
||||
#include "gertboard.h"
|
||||
|
||||
// The A-D convertor won't run at more than 1MHz @ 3.3v
|
||||
|
||||
#define SPI_ADC_SPEED 1000000
|
||||
#define SPI_DAC_SPEED 1000000
|
||||
#define SPI_A2D 0
|
||||
#define SPI_D2A 1
|
||||
|
||||
|
||||
/*
|
||||
* gertboardAnalogWrite:
|
||||
* Write an 8-bit data value to the MCP4802 Analog to digital
|
||||
* convertor on the Gertboard.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void gertboardAnalogWrite (int chan, int value)
|
||||
{
|
||||
uint8_t spiData [2] ;
|
||||
uint8_t chanBits, dataBits ;
|
||||
|
||||
if (chan == 0)
|
||||
chanBits = 0x30 ;
|
||||
else
|
||||
chanBits = 0xB0 ;
|
||||
|
||||
chanBits |= ((value >> 4) & 0x0F) ;
|
||||
dataBits = ((value << 4) & 0xF0) ;
|
||||
|
||||
spiData [0] = chanBits ;
|
||||
spiData [1] = dataBits ;
|
||||
|
||||
wiringPiSPIDataRW (SPI_D2A, spiData, 2) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* gertboardAnalogRead:
|
||||
* Return the analog value of the given channel (0/1).
|
||||
* The A/D is a 10-bit device
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int gertboardAnalogRead (int chan)
|
||||
{
|
||||
uint8_t spiData [2] ;
|
||||
|
||||
uint8_t chanBits ;
|
||||
|
||||
if (chan == 0)
|
||||
chanBits = 0b11010000 ;
|
||||
else
|
||||
chanBits = 0b11110000 ;
|
||||
|
||||
spiData [0] = chanBits ;
|
||||
spiData [1] = 0 ;
|
||||
|
||||
wiringPiSPIDataRW (SPI_A2D, spiData, 2) ;
|
||||
|
||||
return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* gertboardSPISetup:
|
||||
* Initialise the SPI bus, etc.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int gertboardSPISetup (void)
|
||||
{
|
||||
if (wiringPiSPISetup (SPI_A2D, SPI_ADC_SPEED) < 0)
|
||||
return -1 ;
|
||||
|
||||
if (wiringPiSPISetup (SPI_D2A, SPI_DAC_SPEED) < 0)
|
||||
return -1 ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* New wiringPi node extension methods.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int gbWiringPiAnalogRead (struct wiringPiNodeStruct *node, int chan)
|
||||
{
|
||||
chan -= node->pinBase ;
|
||||
return gertboardAnalogRead (chan) ;
|
||||
}
|
||||
|
||||
void gbWiringPiAnalogWrite (struct wiringPiNodeStruct *node, int chan, int value)
|
||||
{
|
||||
chan -= node->pinBase ;
|
||||
gertboardAnalogWrite (chan, value) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* gertboardAnalogSetup:
|
||||
* Create a new wiringPi device node for the analog devices on the
|
||||
* Gertboard. We create one node with 2 pins - each pin being read
|
||||
* and write - although the operations actually go to different
|
||||
* hardware devices.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int gertboardAnalogSetup (int pinBase)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
int x ;
|
||||
|
||||
if (( x = gertboardSPISetup ()) != 0)
|
||||
return x;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 2) ;
|
||||
node->analogRead = gbWiringPiAnalogRead ;
|
||||
node->analogWrite = gbWiringPiAnalogWrite ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
40
WiringPi/wiringPi/gertboard.h
Normal file
40
WiringPi/wiringPi/gertboard.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* gertboard.h:
|
||||
* Access routines for the SPI devices on the Gertboard
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
*
|
||||
* The Gertboard has an MCP4802 dual-channel D to A convertor
|
||||
* connected to the SPI bus, selected via chip-select B.
|
||||
*
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void gertboardAnalogWrite (int chan, int value) ;
|
||||
extern int gertboardAnalogRead (int chan) ;
|
||||
extern int gertboardSPISetup (void) ;
|
||||
extern int gertboardAnalogSetup (int pinBase) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
380
WiringPi/wiringPi/lcd.c
Normal file
380
WiringPi/wiringPi/lcd.c
Normal file
@@ -0,0 +1,380 @@
|
||||
/*
|
||||
* lcd.c:
|
||||
* Text-based LCD driver.
|
||||
* This is designed to drive the parallel interface LCD drivers
|
||||
* based in the Hitachi HD44780U controller and compatables.
|
||||
*
|
||||
* Copyright (c) 2012 Gordon Henderson.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "lcd.h"
|
||||
|
||||
// Commands
|
||||
|
||||
#define LCD_CLEAR 0x01
|
||||
#define LCD_HOME 0x02
|
||||
#define LCD_ENTRY 0x04
|
||||
#define LCD_ON_OFF 0x08
|
||||
#define LCD_CDSHIFT 0x10
|
||||
#define LCD_FUNC 0x20
|
||||
#define LCD_CGRAM 0x40
|
||||
#define LCD_DGRAM 0x80
|
||||
|
||||
#define LCD_ENTRY_SH 0x01
|
||||
#define LCD_ENTRY_ID 0x02
|
||||
|
||||
#define LCD_ON_OFF_B 0x01
|
||||
#define LCD_ON_OFF_C 0x02
|
||||
#define LCD_ON_OFF_D 0x04
|
||||
|
||||
#define LCD_FUNC_F 0x04
|
||||
#define LCD_FUNC_N 0x08
|
||||
#define LCD_FUNC_DL 0x10
|
||||
|
||||
#define LCD_CDSHIFT_RL 0x04
|
||||
|
||||
struct lcdDataStruct
|
||||
{
|
||||
uint8_t bits, rows, cols ;
|
||||
uint8_t rsPin, strbPin ;
|
||||
uint8_t dataPins [8] ;
|
||||
} ;
|
||||
|
||||
struct lcdDataStruct *lcds [MAX_LCDS] ;
|
||||
|
||||
|
||||
/*
|
||||
* strobe:
|
||||
* Toggle the strobe (Really the "E") pin to the device.
|
||||
* According to the docs, data is latched on the falling edge.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void strobe (struct lcdDataStruct *lcd)
|
||||
{
|
||||
|
||||
// Note timing changes for new version of delayMicroseconds ()
|
||||
|
||||
digitalWrite (lcd->strbPin, 1) ; delayMicroseconds (50) ;
|
||||
digitalWrite (lcd->strbPin, 0) ; delayMicroseconds (50) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* sentDataCmd:
|
||||
* Send an data or command byte to the display.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void sendDataCmd (struct lcdDataStruct *lcd, uint8_t data)
|
||||
{
|
||||
uint8_t i, d4 ;
|
||||
|
||||
if (lcd->bits == 4)
|
||||
{
|
||||
d4 = (data >> 4) & 0x0F;
|
||||
for (i = 0 ; i < 4 ; ++i)
|
||||
{
|
||||
digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
|
||||
d4 >>= 1 ;
|
||||
}
|
||||
strobe (lcd) ;
|
||||
|
||||
d4 = data & 0x0F ;
|
||||
for (i = 0 ; i < 4 ; ++i)
|
||||
{
|
||||
digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
|
||||
d4 >>= 1 ;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
{
|
||||
digitalWrite (lcd->dataPins [i], (data & 1)) ;
|
||||
data >>= 1 ;
|
||||
}
|
||||
}
|
||||
strobe (lcd) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* putCommand:
|
||||
* Send a command byte to the display
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void putCommand (struct lcdDataStruct *lcd, uint8_t command)
|
||||
{
|
||||
digitalWrite (lcd->rsPin, 0) ;
|
||||
sendDataCmd (lcd, command) ;
|
||||
}
|
||||
|
||||
static void put4Command (struct lcdDataStruct *lcd, uint8_t command)
|
||||
{
|
||||
uint8_t i ;
|
||||
|
||||
digitalWrite (lcd->rsPin, 0) ;
|
||||
|
||||
for (i = 0 ; i < 4 ; ++i)
|
||||
{
|
||||
digitalWrite (lcd->dataPins [i], (command & 1)) ;
|
||||
command >>= 1 ;
|
||||
}
|
||||
strobe (lcd) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************
|
||||
* User Code below here
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
/*
|
||||
* lcdHome: lcdClear:
|
||||
* Home the cursor or clear the screen.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdHome (int fd)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
putCommand (lcd, LCD_HOME) ;
|
||||
}
|
||||
|
||||
void lcdClear (int fd)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
putCommand (lcd, LCD_CLEAR) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcdSendCommand:
|
||||
* Send any arbitary command to the display
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdSendCommand (int fd, uint8_t command)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
putCommand (lcd, command) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* lcdPosition:
|
||||
* Update the position of the cursor on the display
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
|
||||
void lcdPosition (int fd, int x, int y)
|
||||
{
|
||||
static uint8_t rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ;
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
|
||||
putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcdPutchar:
|
||||
* Send a data byte to be displayed on the display
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdPutchar (int fd, uint8_t data)
|
||||
{
|
||||
struct lcdDataStruct *lcd = lcds [fd] ;
|
||||
|
||||
digitalWrite (lcd->rsPin, 1) ;
|
||||
sendDataCmd (lcd, data) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcdPuts:
|
||||
* Send a string to be displayed on the display
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdPuts (int fd, char *string)
|
||||
{
|
||||
while (*string)
|
||||
lcdPutchar (fd, *string++) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcdPrintf:
|
||||
* Printf to an LCD display
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void lcdPrintf (int fd, char *message, ...)
|
||||
{
|
||||
va_list argp ;
|
||||
char buffer [1024] ;
|
||||
|
||||
va_start (argp, message) ;
|
||||
vsnprintf (buffer, 1023, message, argp) ;
|
||||
va_end (argp) ;
|
||||
|
||||
lcdPuts (fd, buffer) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* lcdInit:
|
||||
* Take a lot of parameters and initialise the LCD, and return a handle to
|
||||
* that LCD, or -1 if any error.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int lcdInit (int rows, int cols, int bits, int rs, int strb,
|
||||
int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
|
||||
{
|
||||
static int initialised = 0 ;
|
||||
|
||||
uint8_t func ;
|
||||
int i ;
|
||||
int lcdFd = -1 ;
|
||||
struct lcdDataStruct *lcd ;
|
||||
|
||||
if (initialised == 0)
|
||||
{
|
||||
initialised = 1 ;
|
||||
for (i = 0 ; i < MAX_LCDS ; ++i)
|
||||
lcds [i] = NULL ;
|
||||
}
|
||||
|
||||
// Simple sanity checks
|
||||
|
||||
if (! ((bits == 4) || (bits == 8)))
|
||||
return -1 ;
|
||||
|
||||
if ((rows < 0) || (rows > 20))
|
||||
return -1 ;
|
||||
|
||||
if ((cols < 0) || (cols > 20))
|
||||
return -1 ;
|
||||
|
||||
// Create a new LCD:
|
||||
|
||||
for (i = 0 ; i < MAX_LCDS ; ++i)
|
||||
{
|
||||
if (lcds [i] == NULL)
|
||||
{
|
||||
lcdFd = i ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
||||
if (lcdFd == -1)
|
||||
return -1 ;
|
||||
|
||||
lcd = malloc (sizeof (struct lcdDataStruct)) ;
|
||||
if (lcd == NULL)
|
||||
return -1 ;
|
||||
|
||||
lcd->rsPin = rs ;
|
||||
lcd->strbPin = strb ;
|
||||
lcd->bits = 8 ; // For now - we'll set it properly later.
|
||||
lcd->rows = rows ;
|
||||
lcd->cols = cols ;
|
||||
|
||||
lcd->dataPins [0] = d0 ;
|
||||
lcd->dataPins [1] = d1 ;
|
||||
lcd->dataPins [2] = d2 ;
|
||||
lcd->dataPins [3] = d3 ;
|
||||
lcd->dataPins [4] = d4 ;
|
||||
lcd->dataPins [5] = d5 ;
|
||||
lcd->dataPins [6] = d6 ;
|
||||
lcd->dataPins [7] = d7 ;
|
||||
|
||||
lcds [lcdFd] = lcd ;
|
||||
|
||||
digitalWrite (lcd->rsPin, 0) ; pinMode (lcd->rsPin, OUTPUT) ;
|
||||
digitalWrite (lcd->strbPin, 0) ; pinMode (lcd->strbPin, OUTPUT) ;
|
||||
|
||||
for (i = 0 ; i < bits ; ++i)
|
||||
{
|
||||
digitalWrite (lcd->dataPins [i], 0) ;
|
||||
pinMode (lcd->dataPins [i], OUTPUT) ;
|
||||
}
|
||||
delay (35) ; // mS
|
||||
|
||||
|
||||
// 4-bit mode?
|
||||
// OK. This is a PIG and it's not at all obvious from the documentation I had,
|
||||
// so I guess some others have worked through either with better documentation
|
||||
// or more trial and error... Anyway here goes:
|
||||
//
|
||||
// It seems that the controller needs to see the FUNC command at least 3 times
|
||||
// consecutively - in 8-bit mode. If you're only using 8-bit mode, then it appears
|
||||
// that you can get away with one func-set, however I'd not rely on it...
|
||||
//
|
||||
// So to set 4-bit mode, you need to send the commands one nibble at a time,
|
||||
// the same three times, but send the command to set it into 8-bit mode those
|
||||
// three times, then send a final 4th command to set it into 4-bit mode, and only
|
||||
// then can you flip the switch for the rest of the library to work in 4-bit
|
||||
// mode which sends the commands as 2 x 4-bit values.
|
||||
|
||||
if (bits == 4)
|
||||
{
|
||||
func = LCD_FUNC | LCD_FUNC_DL ; // Set 8-bit mode 3 times
|
||||
put4Command (lcd, func >> 4) ; delay (35) ;
|
||||
put4Command (lcd, func >> 4) ; delay (35) ;
|
||||
put4Command (lcd, func >> 4) ; delay (35) ;
|
||||
func = LCD_FUNC ; // 4th set: 4-bit mode
|
||||
put4Command (lcd, func >> 4) ; delay (35) ;
|
||||
lcd->bits = 4 ;
|
||||
}
|
||||
else
|
||||
{
|
||||
func = LCD_FUNC | LCD_FUNC_DL ;
|
||||
putCommand (lcd, func ) ; delay (35) ;
|
||||
putCommand (lcd, func ) ; delay (35) ;
|
||||
putCommand (lcd, func ) ; delay (35) ;
|
||||
}
|
||||
|
||||
if (lcd->rows > 1)
|
||||
{
|
||||
func |= LCD_FUNC_N ;
|
||||
putCommand (lcd, func) ; delay (35) ;
|
||||
}
|
||||
|
||||
// Rest of the initialisation sequence
|
||||
|
||||
putCommand (lcd, LCD_ON_OFF | LCD_ON_OFF_D) ; delay (2) ;
|
||||
putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ; delay (2) ;
|
||||
putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ; delay (2) ;
|
||||
putCommand (lcd, LCD_CLEAR) ; delay (5) ;
|
||||
|
||||
return lcdFd ;
|
||||
}
|
||||
46
WiringPi/wiringPi/lcd.h
Normal file
46
WiringPi/wiringPi/lcd.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* lcd.h:
|
||||
* Text-based LCD driver.
|
||||
* This is designed to drive the parallel interface LCD drivers
|
||||
* based in the Hitachi HD44780U controller and compatables.
|
||||
*
|
||||
* Copyright (c) 2012 Gordon Henderson.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#define MAX_LCDS 8
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void lcdHome (int fd) ;
|
||||
extern void lcdClear (int fd) ;
|
||||
extern void lcdSendCommand (int fd, uint8_t command) ;
|
||||
extern void lcdPosition (int fd, int x, int y) ;
|
||||
extern void lcdPutchar (int fd, uint8_t data) ;
|
||||
extern void lcdPuts (int fd, char *string) ;
|
||||
extern void lcdPrintf (int fd, char *message, ...) ;
|
||||
|
||||
extern int lcdInit (int rows, int cols, int bits, int rs, int strb,
|
||||
int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
155
WiringPi/wiringPi/mcp23008.c
Normal file
155
WiringPi/wiringPi/mcp23008.c
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* 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, ddr ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
ddr = MCP23x08_IODIR ;
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, ddr) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, ddr, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, pud ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
pud = MCP23x08_GPPU ;
|
||||
mask = 1 << pin ;
|
||||
|
||||
old = wiringPiI2CReadReg8 (node->fd, pud) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, pud, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
bit = 1 << (pin & 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, gpio ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
gpio = MCP23x08_GPIO ;
|
||||
mask = 1 << pin ;
|
||||
|
||||
value = wiringPiI2CReadReg8 (node->fd, 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 16 pins, so all we need to know here is the I2C address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23008Setup (int pinBase, int i2cAddress)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
||||
return fd ;
|
||||
|
||||
wiringPiI2CWriteReg8 (fd, MCP23x08_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, MCP23x08_OLAT) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
33
WiringPi/wiringPi/mcp23008.h
Normal file
33
WiringPi/wiringPi/mcp23008.h
Normal 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 (int pinBase, int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
195
WiringPi/wiringPi/mcp23017.c
Normal file
195
WiringPi/wiringPi/mcp23017.c
Normal 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, ddr ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
ddr = MCP23x17_IODIRA ;
|
||||
else
|
||||
{
|
||||
ddr = MCP23x17_IODIRB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, ddr) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, ddr, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, pud ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
pud = MCP23x17_GPPUA ;
|
||||
else
|
||||
{
|
||||
pud = MCP23x17_GPPUB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, pud) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, pud, 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 (int pinBase, 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/wiringPi/mcp23017.h
Normal file
33
WiringPi/wiringPi/mcp23017.h
Normal 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 (int pinBase, int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
195
WiringPi/wiringPi/mcp23s08.c
Normal file
195
WiringPi/wiringPi/mcp23s08.c
Normal file
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* 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, ddr ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
ddr = MCP23x08_IODIR ;
|
||||
mask = 1 << pin ;
|
||||
old = readByte (node->data0, node->data1, ddr) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
writeByte (node->data0, node->data1, ddr, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, pud ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
pud = MCP23x08_GPPU ;
|
||||
mask = 1 << pin ;
|
||||
|
||||
old = readByte (node->data0, node->data1, pud) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
writeByte (node->data0, node->data1, pud, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
bit = 1 << pin ;
|
||||
|
||||
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, gpio ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
gpio = MCP23x08_GPIO ;
|
||||
mask = 1 << pin ;
|
||||
|
||||
value = readByte (node->data0, node->data1, 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 16 pins, so all we need to know here is the SPI address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23s08Setup (int pinBase, int spiPort, int devId)
|
||||
{
|
||||
int x ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0)
|
||||
return x ;
|
||||
|
||||
writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
|
||||
|
||||
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, MCP23x08_OLAT) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
33
WiringPi/wiringPi/mcp23s08.h
Normal file
33
WiringPi/wiringPi/mcp23s08.h
Normal 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 (int pinBase, int spiPort, int devId) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
236
WiringPi/wiringPi/mcp23s17.c
Normal file
236
WiringPi/wiringPi/mcp23s17.c
Normal 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, ddr ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
ddr = MCP23x17_IODIRA ;
|
||||
else
|
||||
{
|
||||
ddr = MCP23x17_IODIRB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = readByte (node->data0, node->data1, ddr) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
writeByte (node->data0, node->data1, ddr, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, pud ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
pud = MCP23x17_GPPUA ;
|
||||
else
|
||||
{
|
||||
pud = MCP23x17_GPPUB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = readByte (node->data0, node->data1, pud) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
writeByte (node->data0, node->data1, pud, 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 (int pinBase, int spiPort, 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/wiringPi/mcp23s17.h
Normal file
33
WiringPi/wiringPi/mcp23s17.h
Normal 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/wiringPi/mcp23x08.h
Normal file
73
WiringPi/wiringPi/mcp23x08.h
Normal 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/wiringPi/mcp23x0817.h
Normal file
87
WiringPi/wiringPi/mcp23x0817.h
Normal 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
|
||||
179
WiringPi/wiringPi/piFace.c
Normal file
179
WiringPi/wiringPi/piFace.c
Normal file
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* piFace.:
|
||||
* Arduino compatable (ish) Wiring library for the Raspberry Pi
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
*
|
||||
* This file to interface with the PiFace peripheral device which
|
||||
* has an MCP23S17 GPIO device connected via the SPI bus.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiSPI.h"
|
||||
|
||||
#include "piFace.h"
|
||||
|
||||
#define PIFACE_SPEED 4000000
|
||||
#define PIFACE_DEVNO 0
|
||||
|
||||
#include "mcp23x0817.h"
|
||||
|
||||
|
||||
/*
|
||||
* writeByte:
|
||||
* Write a byte to a register on the MCP23S17 on the SPI bus.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void writeByte (uint8_t reg, uint8_t data)
|
||||
{
|
||||
uint8_t spiData [4] ;
|
||||
|
||||
spiData [0] = CMD_WRITE ;
|
||||
spiData [1] = reg ;
|
||||
spiData [2] = data ;
|
||||
|
||||
wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* readByte:
|
||||
* Read a byte from a register on the MCP23S17 on the SPI bus.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static uint8_t readByte (uint8_t reg)
|
||||
{
|
||||
uint8_t spiData [4] ;
|
||||
|
||||
spiData [0] = CMD_READ ;
|
||||
spiData [1] = reg ;
|
||||
|
||||
wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
|
||||
|
||||
return spiData [2] ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* digitalWrite:
|
||||
* Perform the digitalWrite function on the PiFace board
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void digitalWritePiFace (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
uint8_t mask, old ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
mask = 1 << pin ;
|
||||
old = readByte (MCP23x17_GPIOA) ;
|
||||
|
||||
if (value == 0)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
writeByte (MCP23x17_GPIOA, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* digitalReadPiFace:
|
||||
* Perform the digitalRead function on the PiFace board
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int digitalReadPiFace (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
uint8_t mask, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
mask = 1 << (pin & 7) ;
|
||||
|
||||
if (pin < 8)
|
||||
reg = MCP23x17_GPIOB ; // Input regsiter
|
||||
else
|
||||
reg = MCP23x17_OLATA ; // Output latch regsiter
|
||||
|
||||
if ((readByte (reg) & mask) != 0)
|
||||
return HIGH ;
|
||||
else
|
||||
return LOW ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* pullUpDnControlPiFace:
|
||||
* Perform the pullUpDnControl function on the PiFace board
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void pullUpDnControlPiFace (struct wiringPiNodeStruct *node, int pin, int pud)
|
||||
{
|
||||
uint8_t mask, old ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
mask = 1 << pin ;
|
||||
old = readByte (MCP23x17_GPPUB) ;
|
||||
|
||||
if (pud == 0)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
writeByte (MCP23x17_GPPUB, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* piFaceSetup
|
||||
* Setup the SPI interface and initialise the MCP23S17 chip
|
||||
* We create one node with 16 pins - each if the first 8 pins being read
|
||||
* and write - although the operations actually go to different
|
||||
* hardware ports. The top 8 let you read the state of the output register.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int piFaceSetup (int pinBase)
|
||||
{
|
||||
int x ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((x = wiringPiSPISetup (PIFACE_DEVNO, PIFACE_SPEED)) < 0)
|
||||
return x ;
|
||||
|
||||
// Setup the MCP23S17
|
||||
|
||||
writeByte (MCP23x17_IOCON, IOCON_INIT) ;
|
||||
writeByte (MCP23x17_IODIRA, 0x00) ; // Port A -> Outputs
|
||||
writeByte (MCP23x17_IODIRB, 0xFF) ; // Port B -> Inputs
|
||||
|
||||
node = wiringPiNewNode (pinBase, 16) ;
|
||||
node->digitalRead = digitalReadPiFace ;
|
||||
node->digitalWrite = digitalWritePiFace ;
|
||||
node->pullUpDnControl = pullUpDnControlPiFace ;
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
32
WiringPi/wiringPi/piFace.h
Normal file
32
WiringPi/wiringPi/piFace.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* piFace.h:
|
||||
* Control the PiFace Interface board for the Raspberry Pi
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int piFaceSetup (int pinBase) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
50
WiringPi/wiringPi/piHiPri.c
Normal file
50
WiringPi/wiringPi/piHiPri.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* piHiPri:
|
||||
* Simple way to get your program running at high priority
|
||||
* with realtime schedulling.
|
||||
*
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <sched.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
|
||||
|
||||
/*
|
||||
* piHiPri:
|
||||
* Attempt to set a high priority schedulling for the running program
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int piHiPri (int pri)
|
||||
{
|
||||
struct sched_param sched ;
|
||||
|
||||
memset (&sched, 0, sizeof(sched)) ;
|
||||
|
||||
if (pri > sched_get_priority_max (SCHED_RR))
|
||||
pri = sched_get_priority_max (SCHED_RR) ;
|
||||
|
||||
sched.sched_priority = pri ;
|
||||
return sched_setscheduler (0, SCHED_RR, &sched) ;
|
||||
}
|
||||
113
WiringPi/wiringPi/piNes.c
Normal file
113
WiringPi/wiringPi/piNes.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* piNes.c:
|
||||
* Driver for the NES Joystick controller on the Raspberry Pi
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <wiringPi.h>
|
||||
|
||||
#include "piNes.h"
|
||||
|
||||
#define MAX_NES_JOYSTICKS 8
|
||||
|
||||
#define NES_RIGHT 0x01
|
||||
#define NES_LEFT 0x02
|
||||
#define NES_DOWN 0x04
|
||||
#define NES_UP 0x08
|
||||
#define NES_START 0x10
|
||||
#define NES_SELECT 0x20
|
||||
#define NES_B 0x40
|
||||
#define NES_A 0x80
|
||||
|
||||
|
||||
#define PULSE_TIME 25
|
||||
|
||||
// Data to store the pins for each controller
|
||||
|
||||
struct nesPinsStruct
|
||||
{
|
||||
unsigned int cPin, dPin, lPin ;
|
||||
} ;
|
||||
|
||||
static struct nesPinsStruct nesPins [MAX_NES_JOYSTICKS] ;
|
||||
|
||||
static int joysticks = 0 ;
|
||||
|
||||
|
||||
/*
|
||||
* setupNesJoystick:
|
||||
* Create a new NES joystick interface, program the pins, etc.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int setupNesJoystick (int dPin, int cPin, int lPin)
|
||||
{
|
||||
if (joysticks == MAX_NES_JOYSTICKS)
|
||||
return -1 ;
|
||||
|
||||
nesPins [joysticks].dPin = dPin ;
|
||||
nesPins [joysticks].cPin = cPin ;
|
||||
nesPins [joysticks].lPin = lPin ;
|
||||
|
||||
digitalWrite (lPin, LOW) ;
|
||||
digitalWrite (cPin, LOW) ;
|
||||
|
||||
pinMode (lPin, OUTPUT) ;
|
||||
pinMode (cPin, OUTPUT) ;
|
||||
pinMode (dPin, INPUT) ;
|
||||
|
||||
return joysticks++ ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* readNesJoystick:
|
||||
* Do a single scan of the NES Joystick.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
unsigned int readNesJoystick (int joystick)
|
||||
{
|
||||
unsigned int value = 0 ;
|
||||
int i ;
|
||||
|
||||
struct nesPinsStruct *pins = &nesPins [joystick] ;
|
||||
|
||||
// Toggle Latch - which presents the first bit
|
||||
|
||||
digitalWrite (pins->lPin, HIGH) ; delayMicroseconds (PULSE_TIME) ;
|
||||
digitalWrite (pins->lPin, LOW) ; delayMicroseconds (PULSE_TIME) ;
|
||||
|
||||
// Read first bit
|
||||
|
||||
value = digitalRead (pins->dPin) ;
|
||||
|
||||
// Now get the next 7 bits with the clock
|
||||
|
||||
for (i = 0 ; i < 7 ; ++i)
|
||||
{
|
||||
digitalWrite (pins->cPin, HIGH) ; delayMicroseconds (PULSE_TIME) ;
|
||||
digitalWrite (pins->cPin, LOW) ; delayMicroseconds (PULSE_TIME) ;
|
||||
value = (value << 1) | digitalRead (pins->dPin) ;
|
||||
}
|
||||
|
||||
return value ^ 0xFF ;
|
||||
}
|
||||
45
WiringPi/wiringPi/piNes.h
Normal file
45
WiringPi/wiringPi/piNes.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* piNes.h:
|
||||
* Driver for the NES Joystick controller on the Raspberry Pi
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#define MAX_NES_JOYSTICKS 8
|
||||
|
||||
#define NES_RIGHT 0x01
|
||||
#define NES_LEFT 0x02
|
||||
#define NES_DOWN 0x04
|
||||
#define NES_UP 0x08
|
||||
#define NES_START 0x10
|
||||
#define NES_SELECT 0x20
|
||||
#define NES_B 0x40
|
||||
#define NES_A 0x80
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int setupNesJoystick (int dPin, int cPin, int lPin) ;
|
||||
extern unsigned int readNesJoystick (int joystick) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
63
WiringPi/wiringPi/piThread.c
Normal file
63
WiringPi/wiringPi/piThread.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* piThread.c:
|
||||
* Provide a simplified interface to pthreads
|
||||
*
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include "wiringPi.h"
|
||||
|
||||
static pthread_mutex_t piMutexes [4] ;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* piThreadCreate:
|
||||
* Create and start a thread
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int piThreadCreate (void *(*fn)(void *))
|
||||
{
|
||||
pthread_t myThread ;
|
||||
|
||||
return pthread_create (&myThread, NULL, fn, NULL) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* piLock: piUnlock:
|
||||
* Activate/Deactivate a mutex.
|
||||
* We're keeping things simple here and only tracking 4 mutexes which
|
||||
* is more than enough for out entry-level pthread programming
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void piLock (int key)
|
||||
{
|
||||
pthread_mutex_lock (&piMutexes [key]) ;
|
||||
}
|
||||
|
||||
void piUnlock (int key)
|
||||
{
|
||||
pthread_mutex_unlock (&piMutexes [key]) ;
|
||||
}
|
||||
|
||||
130
WiringPi/wiringPi/softPwm.c
Normal file
130
WiringPi/wiringPi/softPwm.c
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* softPwm.c:
|
||||
* Provide 2 channels of software driven PWM.
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "softPwm.h"
|
||||
|
||||
#define MAX_PINS 64
|
||||
|
||||
// The PWM Frequency is derived from the "pulse time" below. Essentially,
|
||||
// the frequency is a function of the range and this pulse time.
|
||||
// The total period will be range * pulse time in uS, so a pulse time
|
||||
// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 uS
|
||||
// which is a frequency of 100Hz.
|
||||
//
|
||||
// It's possible to get a higher frequency by lowering the pulse time,
|
||||
// however CPU uage will skyrocket as wiringPi uses a hard-loop to time
|
||||
// periods under 100uS - this is because the Linux timer calls are just
|
||||
// accurate at all, and have an overhead.
|
||||
//
|
||||
// Another way to increase the frequency is to reduce the range - however
|
||||
// that reduces the overall output accuracy...
|
||||
|
||||
#define PULSE_TIME 100
|
||||
|
||||
static int marks [MAX_PINS] ;
|
||||
static int range [MAX_PINS] ;
|
||||
|
||||
int newPin = -1 ;
|
||||
|
||||
|
||||
/*
|
||||
* softPwmThread:
|
||||
* Thread to do the actual PWM output
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static PI_THREAD (softPwmThread)
|
||||
{
|
||||
int pin, mark, space ;
|
||||
|
||||
pin = newPin ;
|
||||
newPin = -1 ;
|
||||
|
||||
piHiPri (50) ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
mark = marks [pin] ;
|
||||
space = range [pin] - mark ;
|
||||
|
||||
if (mark != 0)
|
||||
digitalWrite (pin, HIGH) ;
|
||||
delayMicroseconds (mark * 100) ;
|
||||
|
||||
if (space != 0)
|
||||
digitalWrite (pin, LOW) ;
|
||||
delayMicroseconds (space * 100) ;
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softPwmWrite:
|
||||
* Write a PWM value to the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void softPwmWrite (int pin, int value)
|
||||
{
|
||||
pin &= 63 ;
|
||||
|
||||
/**/ if (value < 0)
|
||||
value = 0 ;
|
||||
else if (value > range [pin])
|
||||
value = range [pin] ;
|
||||
|
||||
marks [pin] = value ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softPwmCreate:
|
||||
* Create a new PWM thread.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int softPwmCreate (int pin, int initialValue, int pwmRange)
|
||||
{
|
||||
int res ;
|
||||
|
||||
pinMode (pin, OUTPUT) ;
|
||||
digitalWrite (pin, LOW) ;
|
||||
|
||||
marks [pin] = initialValue ;
|
||||
range [pin] = pwmRange ;
|
||||
|
||||
newPin = pin ;
|
||||
res = piThreadCreate (softPwmThread) ;
|
||||
|
||||
while (newPin != -1)
|
||||
delay (1) ;
|
||||
|
||||
return res ;
|
||||
}
|
||||
34
WiringPi/wiringPi/softPwm.h
Normal file
34
WiringPi/wiringPi/softPwm.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* softPwm.h:
|
||||
* Provide 2 channels of software driven PWM.
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int softPwmCreate (int pin, int value, int range) ;
|
||||
extern void softPwmWrite (int pin, int value) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
211
WiringPi/wiringPi/softServo.c
Normal file
211
WiringPi/wiringPi/softServo.c
Normal file
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
* softServo.c:
|
||||
* Provide N channels of software driven PWM suitable for RC
|
||||
* servo motors.
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
//#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "softServo.h"
|
||||
|
||||
// RC Servo motors are a bit of an oddity - designed in the days when
|
||||
// radio control was experimental and people were tryin to make
|
||||
// things as simple as possible as it was all very expensive...
|
||||
//
|
||||
// So... To drive an RC Servo motor, you need to send it a modified PWM
|
||||
// signal - it needs anything from 1ms to 2ms - with 1ms meaning
|
||||
// to move the server fully left, and 2ms meaning to move it fully
|
||||
// right. Then you need a long gap before sending the next pulse.
|
||||
// The reason for this is that you send a multiplexed stream of these
|
||||
// pulses up the radio signal into the reciever which de-multiplexes
|
||||
// them into the signals for each individual servo. Typically there
|
||||
// might be 8 channels, so you need at least 8 "slots" of 2mS pulses
|
||||
// meaning the entire frame must fit into a 16mS slot - which would
|
||||
// then be repeated...
|
||||
//
|
||||
// In practice we have a total slot width of about 20mS - so we're sending 50
|
||||
// updates per second to each servo.
|
||||
//
|
||||
// In this code, we don't need to be too fussy about the gap as we're not doing
|
||||
// the multipexing, but it does need to be at least 10mS, and preferably 16
|
||||
// from what I've been able to determine.
|
||||
|
||||
// WARNING:
|
||||
// This code is really experimental. It was written in response to some people
|
||||
// asking for a servo driver, however while it works, there is too much
|
||||
// jitter to successfully drive a small servo - I have tried it with a micro
|
||||
// servo and it worked, but the servo ran hot due to the jitter in the signal
|
||||
// being sent to it.
|
||||
//
|
||||
// If you want servo control for the Pi, then use the servoblaster kernel
|
||||
// module.
|
||||
|
||||
#define MAX_SERVOS 8
|
||||
|
||||
static int pinMap [MAX_SERVOS] ; // Keep track of our pins
|
||||
static int pulseWidth [MAX_SERVOS] ; // microseconds
|
||||
|
||||
|
||||
/*
|
||||
* softServoThread:
|
||||
* Thread to do the actual Servo PWM output
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static PI_THREAD (softServoThread)
|
||||
{
|
||||
register int i, j, k, m, tmp ;
|
||||
int lastDelay, pin, servo ;
|
||||
|
||||
int myDelays [MAX_SERVOS] ;
|
||||
int myPins [MAX_SERVOS] ;
|
||||
|
||||
struct timeval tNow, tStart, tPeriod, tGap, tTotal ;
|
||||
struct timespec tNs ;
|
||||
|
||||
tTotal.tv_sec = 0 ;
|
||||
tTotal.tv_usec = 8000 ;
|
||||
|
||||
piHiPri (50) ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
gettimeofday (&tStart, NULL) ;
|
||||
|
||||
memcpy (myDelays, pulseWidth, sizeof (myDelays)) ;
|
||||
memcpy (myPins, pinMap, sizeof (myPins)) ;
|
||||
|
||||
// Sort the delays (& pins), shortest first
|
||||
|
||||
for (m = MAX_SERVOS / 2 ; m > 0 ; m /= 2 )
|
||||
for (j = m ; j < MAX_SERVOS ; ++j)
|
||||
for (i = j - m ; i >= 0 ; i -= m)
|
||||
{
|
||||
k = i + m ;
|
||||
if (myDelays [k] >= myDelays [i])
|
||||
break ;
|
||||
else // Swap
|
||||
{
|
||||
tmp = myDelays [i] ; myDelays [i] = myDelays [k] ; myDelays [k] = tmp ;
|
||||
tmp = myPins [i] ; myPins [i] = myPins [k] ; myPins [k] = tmp ;
|
||||
}
|
||||
}
|
||||
|
||||
// All on
|
||||
|
||||
lastDelay = 0 ;
|
||||
for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
|
||||
{
|
||||
if ((pin = myPins [servo]) == -1)
|
||||
continue ;
|
||||
|
||||
digitalWrite (pin, HIGH) ;
|
||||
myDelays [servo] = myDelays [servo] - lastDelay ;
|
||||
lastDelay += myDelays [servo] ;
|
||||
}
|
||||
|
||||
// Now loop, turning them all off as required
|
||||
|
||||
for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
|
||||
{
|
||||
if ((pin = myPins [servo]) == -1)
|
||||
continue ;
|
||||
|
||||
delayMicroseconds (myDelays [servo]) ;
|
||||
digitalWrite (pin, LOW) ;
|
||||
}
|
||||
|
||||
// Wait until the end of an 8mS time-slot
|
||||
|
||||
gettimeofday (&tNow, NULL) ;
|
||||
timersub (&tNow, &tStart, &tPeriod) ;
|
||||
timersub (&tTotal, &tPeriod, &tGap) ;
|
||||
tNs.tv_sec = tGap.tv_sec ;
|
||||
tNs.tv_nsec = tGap.tv_usec * 1000 ;
|
||||
nanosleep (&tNs, NULL) ;
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softServoWrite:
|
||||
* Write a Servo value to the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void softServoWrite (int servoPin, int value)
|
||||
{
|
||||
int servo ;
|
||||
|
||||
servoPin &= 63 ;
|
||||
|
||||
/**/ if (value < -250)
|
||||
value = -250 ;
|
||||
else if (value > 1250)
|
||||
value = 1250 ;
|
||||
|
||||
for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
|
||||
if (pinMap [servo] == servoPin)
|
||||
pulseWidth [servo] = value + 1000 ; // uS
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softServoSetup:
|
||||
* Setup the software servo system
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int softServoSetup (int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7)
|
||||
{
|
||||
int servo ;
|
||||
|
||||
if (p0 != -1) { pinMode (p0, OUTPUT) ; digitalWrite (p0, LOW) ; }
|
||||
if (p1 != -1) { pinMode (p1, OUTPUT) ; digitalWrite (p1, LOW) ; }
|
||||
if (p2 != -1) { pinMode (p2, OUTPUT) ; digitalWrite (p2, LOW) ; }
|
||||
if (p3 != -1) { pinMode (p3, OUTPUT) ; digitalWrite (p3, LOW) ; }
|
||||
if (p4 != -1) { pinMode (p4, OUTPUT) ; digitalWrite (p4, LOW) ; }
|
||||
if (p5 != -1) { pinMode (p5, OUTPUT) ; digitalWrite (p5, LOW) ; }
|
||||
if (p6 != -1) { pinMode (p6, OUTPUT) ; digitalWrite (p6, LOW) ; }
|
||||
if (p7 != -1) { pinMode (p7, OUTPUT) ; digitalWrite (p7, LOW) ; }
|
||||
|
||||
pinMap [0] = p0 ;
|
||||
pinMap [1] = p1 ;
|
||||
pinMap [2] = p2 ;
|
||||
pinMap [3] = p3 ;
|
||||
pinMap [4] = p4 ;
|
||||
pinMap [5] = p5 ;
|
||||
pinMap [6] = p6 ;
|
||||
pinMap [7] = p7 ;
|
||||
|
||||
for (servo = 0 ; servo < MAX_SERVOS ; ++servo)
|
||||
pulseWidth [servo] = 1500 ; // Mid point
|
||||
|
||||
return piThreadCreate (softServoThread) ;
|
||||
}
|
||||
35
WiringPi/wiringPi/softServo.h
Normal file
35
WiringPi/wiringPi/softServo.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* softServo.h:
|
||||
* Provide N channels of software driven PWM suitable for RC
|
||||
* servo motors.
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void softServoWrite (int pin, int value) ;
|
||||
extern int softServoSetup (int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
121
WiringPi/wiringPi/softTone.c
Normal file
121
WiringPi/wiringPi/softTone.c
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* softTone.c:
|
||||
* For that authentic retro sound...
|
||||
* Er... A little experiment to produce tones out of a Pi using
|
||||
* one (or 2) GPIO pins and a piezeo "speaker" element.
|
||||
* (Or a high impedance speaker, but don'y blame me if you blow-up
|
||||
* the GPIO pins!)
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "softTone.h"
|
||||
|
||||
#define MAX_PINS 64
|
||||
|
||||
#define PULSE_TIME 100
|
||||
|
||||
static int freqs [MAX_PINS] ;
|
||||
|
||||
static int newPin = -1 ;
|
||||
|
||||
|
||||
/*
|
||||
* softToneThread:
|
||||
* Thread to do the actual PWM output
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static PI_THREAD (softToneThread)
|
||||
{
|
||||
int pin, freq, halfPeriod ;
|
||||
|
||||
pin = newPin ;
|
||||
newPin = -1 ;
|
||||
|
||||
piHiPri (50) ;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
freq = freqs [pin] ;
|
||||
if (freq == 0)
|
||||
delay (1) ;
|
||||
else
|
||||
{
|
||||
halfPeriod = 500000 / freq ;
|
||||
|
||||
digitalWrite (pin, HIGH) ;
|
||||
delayMicroseconds (halfPeriod) ;
|
||||
|
||||
digitalWrite (pin, LOW) ;
|
||||
delayMicroseconds (halfPeriod) ;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softToneWrite:
|
||||
* Write a frequency value to the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void softToneWrite (int pin, int freq)
|
||||
{
|
||||
pin &= 63 ;
|
||||
|
||||
/**/ if (freq < 0)
|
||||
freq = 0 ;
|
||||
else if (freq > 5000) // Max 5KHz
|
||||
freq = 5000 ;
|
||||
|
||||
freqs [pin] = freq ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softToneCreate:
|
||||
* Create a new tone thread.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int softToneCreate (int pin)
|
||||
{
|
||||
int res ;
|
||||
|
||||
pinMode (pin, OUTPUT) ;
|
||||
digitalWrite (pin, LOW) ;
|
||||
|
||||
freqs [pin] = 0 ;
|
||||
|
||||
newPin = pin ;
|
||||
res = piThreadCreate (softToneThread) ;
|
||||
|
||||
while (newPin != -1)
|
||||
delay (1) ;
|
||||
|
||||
return res ;
|
||||
}
|
||||
38
WiringPi/wiringPi/softTone.h
Normal file
38
WiringPi/wiringPi/softTone.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* softTone.c:
|
||||
* For that authentic retro sound...
|
||||
* Er... A little experiment to produce tones out of a Pi using
|
||||
* one (or 2) GPIO pins and a piezeo "speaker" element.
|
||||
* (Or a high impedance speaker, but don'y blame me if you blow-up
|
||||
* the GPIO pins!)
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int softToneCreate (int pin) ;
|
||||
extern void softToneWrite (int pin, int freq) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
108
WiringPi/wiringPi/sr595.c
Normal file
108
WiringPi/wiringPi/sr595.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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 (int pinBase, int numPins, int dataPin, int clockPin, 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 ;
|
||||
}
|
||||
33
WiringPi/wiringPi/sr595.h
Normal file
33
WiringPi/wiringPi/sr595.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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 (int pinBase, int numPins, int dataPin, int clockPin, int latchPin) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
1664
WiringPi/wiringPi/wiringPi.c
Normal file
1664
WiringPi/wiringPi/wiringPi.c
Normal file
File diff suppressed because it is too large
Load Diff
168
WiringPi/wiringPi/wiringPi.h
Normal file
168
WiringPi/wiringPi/wiringPi.h
Normal file
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* wiringPi:
|
||||
* Arduino compatable (ish) Wiring library for the Raspberry Pi
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
// Handy defines
|
||||
|
||||
// Deprecated
|
||||
#define NUM_PINS 17
|
||||
|
||||
#define WPI_MODE_PINS 0
|
||||
#define WPI_MODE_GPIO 1
|
||||
#define WPI_MODE_GPIO_SYS 2
|
||||
#define WPI_MODE_PHYS 3
|
||||
#define WPI_MODE_PIFACE 4
|
||||
#define WPI_MODE_UNINITIALISED -1
|
||||
|
||||
// Pin modes
|
||||
|
||||
#define INPUT 0
|
||||
#define OUTPUT 1
|
||||
#define PWM_OUTPUT 2
|
||||
#define GPIO_CLOCK 3
|
||||
|
||||
#define LOW 0
|
||||
#define HIGH 1
|
||||
|
||||
// Pull up/down/none
|
||||
|
||||
#define PUD_OFF 0
|
||||
#define PUD_DOWN 1
|
||||
#define PUD_UP 2
|
||||
|
||||
// PWM
|
||||
|
||||
#define PWM_MODE_MS 0
|
||||
#define PWM_MODE_BAL 1
|
||||
|
||||
// Interrupt levels
|
||||
|
||||
#define INT_EDGE_SETUP 0
|
||||
#define INT_EDGE_FALLING 1
|
||||
#define INT_EDGE_RISING 2
|
||||
#define INT_EDGE_BOTH 3
|
||||
|
||||
// Threads
|
||||
|
||||
#define PI_THREAD(X) void *X (void *dummy)
|
||||
|
||||
// wiringPiNodeStruct:
|
||||
// This describes additional device nodes in the extended wiringPi
|
||||
// 2.0 scheme of things.
|
||||
// It's a simple linked list for now, but will hopefully migrate to
|
||||
// a binary tree for efficiency reasons - but then again, the chances
|
||||
// of more than 1 or 2 devices being added are fairly slim, so who
|
||||
// knows....
|
||||
|
||||
struct wiringPiNodeStruct
|
||||
{
|
||||
int pinBase ;
|
||||
int pinMax ;
|
||||
|
||||
int fd ; // Node specific
|
||||
unsigned int data0 ; // ditto
|
||||
unsigned int data1 ; // ditto
|
||||
unsigned int data2 ; // ditto
|
||||
unsigned int data3 ; // ditto
|
||||
|
||||
void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ;
|
||||
void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ;
|
||||
int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ;
|
||||
void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
|
||||
void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
|
||||
int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ;
|
||||
void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
|
||||
|
||||
struct wiringPiNodeStruct *next ;
|
||||
} ;
|
||||
|
||||
|
||||
// Function prototypes
|
||||
// c++ wrappers thanks to a comment by Nick Lott
|
||||
// (and others on the Raspberry Pi forums)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// Core wiringPi functions
|
||||
|
||||
extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
|
||||
|
||||
extern int wiringPiSetup (void) ;
|
||||
extern int wiringPiSetupSys (void) ;
|
||||
extern int wiringPiSetupGpio (void) ;
|
||||
extern int wiringPiSetupPhys (void) ;
|
||||
|
||||
extern void pinMode (int pin, int mode) ;
|
||||
extern void pullUpDnControl (int pin, int pud) ;
|
||||
extern int digitalRead (int pin) ;
|
||||
extern void digitalWrite (int pin, int value) ;
|
||||
extern void pwmWrite (int pin, int value) ;
|
||||
extern int analogRead (int pin) ;
|
||||
extern void analogWrite (int pin, int value) ;
|
||||
|
||||
// PiFace specifics
|
||||
// (Deprecated)
|
||||
|
||||
extern int wiringPiSetupPiFace (void) ;
|
||||
extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only
|
||||
|
||||
// On-Board Raspberry Pi hardware specific stuff
|
||||
|
||||
extern int piBoardRev (void) ;
|
||||
extern int wpiPinToGpio (int wpiPin) ;
|
||||
extern void setPadDrive (int group, int value) ;
|
||||
extern int getAlt (int pin) ;
|
||||
extern void digitalWriteByte (int value) ;
|
||||
extern void pwmSetMode (int mode) ;
|
||||
extern void pwmSetRange (unsigned int range) ;
|
||||
extern void pwmSetClock (int divisor) ;
|
||||
extern void gpioClockSet (int pin, int freq) ;
|
||||
|
||||
// Interrupts
|
||||
// (Also Pi hardware specific)
|
||||
|
||||
extern int waitForInterrupt (int pin, int mS) ;
|
||||
extern int 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) ;
|
||||
|
||||
// Schedulling priority
|
||||
|
||||
extern int piHiPri (int pri) ;
|
||||
|
||||
// Extras from arduino land
|
||||
|
||||
extern void delay (unsigned int howLong) ;
|
||||
extern void delayMicroseconds (unsigned int howLong) ;
|
||||
extern unsigned int millis (void) ;
|
||||
extern unsigned int micros (void) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
227
WiringPi/wiringPi/wiringPiI2C.c
Normal file
227
WiringPi/wiringPi/wiringPiI2C.c
Normal file
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* wiringPiI2C.c:
|
||||
* Simplified I2C access routines
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 <fcntl.h>
|
||||
#include <sys/ioctl.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:
|
||||
* Simple device read
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiI2CRead (int 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 ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wiringPiI2CReadReg8: wiringPiI2CReadReg16:
|
||||
* Read an 8 or 16-bit value from a regsiter on the device
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiI2CReadReg8 (int fd, int 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)
|
||||
{
|
||||
union i2c_smbus_data data;
|
||||
|
||||
if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data))
|
||||
return -1 ;
|
||||
else
|
||||
return data.byte & 0xFF ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wiringPiI2CWrite:
|
||||
* Simple device write
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiI2CWrite (int fd, int data)
|
||||
{
|
||||
return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
|
||||
* Write an 8 or 16-bit value to the given register
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiI2CWriteReg8 (int fd, int reg, int value)
|
||||
{
|
||||
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 value)
|
||||
{
|
||||
union i2c_smbus_data data ;
|
||||
|
||||
data.word = value ;
|
||||
return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wiringPiI2CSetup:
|
||||
* Open the I2C device, and regsiter the target device
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiI2CSetupInterface (char *device, int devId)
|
||||
{
|
||||
int fd ;
|
||||
|
||||
if ((fd = open (device, O_RDWR)) < 0)
|
||||
return -1 ;
|
||||
|
||||
if (ioctl (fd, I2C_SLAVE, devId) < 0)
|
||||
return -1 ;
|
||||
|
||||
return fd ;
|
||||
}
|
||||
|
||||
|
||||
int wiringPiI2CSetup (int devId)
|
||||
{
|
||||
int rev ;
|
||||
char *device ;
|
||||
|
||||
if ((rev = piBoardRev ()) < 0)
|
||||
{
|
||||
fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ;
|
||||
exit (1) ;
|
||||
}
|
||||
|
||||
if (rev == 1)
|
||||
device = "/dev/i2c-0" ;
|
||||
else
|
||||
device = "/dev/i2c-1" ;
|
||||
|
||||
return wiringPiI2CSetupInterface (device, devId) ;
|
||||
}
|
||||
42
WiringPi/wiringPi/wiringPiI2C.h
Normal file
42
WiringPi/wiringPi/wiringPiI2C.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* wiringPiI2C.h:
|
||||
* Simplified I2C access routines
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int wiringPiI2CRead (int fd) ;
|
||||
extern int wiringPiI2CReadReg8 (int fd, int reg) ;
|
||||
extern int wiringPiI2CReadReg16 (int fd, int reg) ;
|
||||
|
||||
extern int wiringPiI2CWrite (int fd, int data) ;
|
||||
extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ;
|
||||
extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ;
|
||||
|
||||
extern int wiringPiI2CSetupInterface (char *device, int devId) ;
|
||||
extern int wiringPiI2CSetup (int devId) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
119
WiringPi/wiringPi/wiringPiSPI.c
Normal file
119
WiringPi/wiringPi/wiringPiSPI.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* wiringPiSPI.c:
|
||||
* Simplified SPI access routines
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <fcntl.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;
|
||||
|
||||
static uint32_t spiSpeeds [2] ;
|
||||
static int spiFds [2] ;
|
||||
|
||||
|
||||
/*
|
||||
* wiringPiSPIGetFd:
|
||||
* Return the file-descriptor for the given channel
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiSPIGetFd (int channel)
|
||||
{
|
||||
return spiFds [channel & 1] ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wiringPiSPIDataRW:
|
||||
* Write and Read a block of data over the SPI bus.
|
||||
* Note the data ia being read into the transmit buffer, so will
|
||||
* overwrite it!
|
||||
* This is also a full-duplex operation.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
|
||||
{
|
||||
struct spi_ioc_transfer spi ;
|
||||
|
||||
channel &= 1 ;
|
||||
|
||||
spi.tx_buf = (unsigned long)data ;
|
||||
spi.rx_buf = (unsigned long)data ;
|
||||
spi.len = len ;
|
||||
spi.delay_usecs = spiDelay ;
|
||||
spi.speed_hz = spiSpeeds [channel] ;
|
||||
spi.bits_per_word = spiBPW ;
|
||||
|
||||
return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wiringPiSPISetup:
|
||||
* Open the SPI device, and set it up, etc.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiSPISetup (int channel, int speed)
|
||||
{
|
||||
int fd ;
|
||||
|
||||
channel &= 1 ;
|
||||
|
||||
if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
|
||||
return -1 ;
|
||||
|
||||
spiSpeeds [channel] = speed ;
|
||||
spiFds [channel] = fd ;
|
||||
|
||||
// Set SPI parameters.
|
||||
// Why are we reading it afterwriting it? I've no idea, but for now I'm blindly
|
||||
// copying example code I've seen online...
|
||||
|
||||
if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) return -1 ;
|
||||
if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0) return -1 ;
|
||||
|
||||
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
|
||||
if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
|
||||
|
||||
if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) return -1 ;
|
||||
if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) return -1 ;
|
||||
|
||||
return fd ;
|
||||
}
|
||||
35
WiringPi/wiringPi/wiringPiSPI.h
Normal file
35
WiringPi/wiringPi/wiringPiSPI.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* wiringPiSPI.h:
|
||||
* Simplified SPI access routines
|
||||
* Copyright (c) 2012 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int wiringPiSPIGetFd (int channel) ;
|
||||
int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ;
|
||||
int wiringPiSPISetup (int channel, int speed) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
213
WiringPi/wiringPi/wiringSerial.c
Normal file
213
WiringPi/wiringPi/wiringSerial.c
Normal file
@@ -0,0 +1,213 @@
|
||||
/*
|
||||
* wiringSerial.c:
|
||||
* Handle a serial port
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "wiringSerial.h"
|
||||
|
||||
/*
|
||||
* serialOpen:
|
||||
* Open and initialise the serial port, setting all the right
|
||||
* port parameters - or as many as are required - hopefully!
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int serialOpen (char *device, int baud)
|
||||
{
|
||||
struct termios options ;
|
||||
speed_t myBaud ;
|
||||
int status, fd ;
|
||||
|
||||
switch (baud)
|
||||
{
|
||||
case 50: myBaud = B50 ; break ;
|
||||
case 75: myBaud = B75 ; break ;
|
||||
case 110: myBaud = B110 ; break ;
|
||||
case 134: myBaud = B134 ; break ;
|
||||
case 150: myBaud = B150 ; break ;
|
||||
case 200: myBaud = B200 ; break ;
|
||||
case 300: myBaud = B300 ; break ;
|
||||
case 600: myBaud = B600 ; break ;
|
||||
case 1200: myBaud = B1200 ; break ;
|
||||
case 1800: myBaud = B1800 ; break ;
|
||||
case 2400: myBaud = B2400 ; break ;
|
||||
case 4800: myBaud = B4800 ; break ;
|
||||
case 9600: myBaud = B9600 ; break ;
|
||||
case 19200: myBaud = B19200 ; break ;
|
||||
case 38400: myBaud = B38400 ; break ;
|
||||
case 57600: myBaud = B57600 ; break ;
|
||||
case 115200: myBaud = B115200 ; break ;
|
||||
case 230400: myBaud = B230400 ; break ;
|
||||
|
||||
default:
|
||||
return -2 ;
|
||||
}
|
||||
|
||||
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
|
||||
return -1 ;
|
||||
|
||||
fcntl (fd, F_SETFL, O_RDWR) ;
|
||||
|
||||
// Get and modify current options:
|
||||
|
||||
tcgetattr (fd, &options) ;
|
||||
|
||||
cfmakeraw (&options) ;
|
||||
cfsetispeed (&options, myBaud) ;
|
||||
cfsetospeed (&options, myBaud) ;
|
||||
|
||||
options.c_cflag |= (CLOCAL | CREAD) ;
|
||||
options.c_cflag &= ~PARENB ;
|
||||
options.c_cflag &= ~CSTOPB ;
|
||||
options.c_cflag &= ~CSIZE ;
|
||||
options.c_cflag |= CS8 ;
|
||||
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
|
||||
options.c_oflag &= ~OPOST ;
|
||||
|
||||
options.c_cc [VMIN] = 0 ;
|
||||
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
|
||||
|
||||
tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;
|
||||
|
||||
ioctl (fd, TIOCMGET, &status);
|
||||
|
||||
status |= TIOCM_DTR ;
|
||||
status |= TIOCM_RTS ;
|
||||
|
||||
ioctl (fd, TIOCMSET, &status);
|
||||
|
||||
usleep (10000) ; // 10mS
|
||||
|
||||
return fd ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialFlush:
|
||||
* Flush the serial buffers (both tx & rx)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialFlush (int fd)
|
||||
{
|
||||
tcflush (fd, TCIOFLUSH) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialClose:
|
||||
* Release the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialClose (int fd)
|
||||
{
|
||||
close (fd) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialPutchar:
|
||||
* Send a single character to the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPutchar (int fd, unsigned char c)
|
||||
{
|
||||
write (fd, &c, 1) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialPuts:
|
||||
* Send a string to the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPuts (int fd, char *s)
|
||||
{
|
||||
write (fd, s, strlen (s)) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* serialPrintf:
|
||||
* Printf over Serial
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPrintf (int fd, char *message, ...)
|
||||
{
|
||||
va_list argp ;
|
||||
char buffer [1024] ;
|
||||
|
||||
va_start (argp, message) ;
|
||||
vsnprintf (buffer, 1023, message, argp) ;
|
||||
va_end (argp) ;
|
||||
|
||||
serialPuts (fd, buffer) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialDataAvail:
|
||||
* Return the number of bytes of data avalable to be read in the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int serialDataAvail (int fd)
|
||||
{
|
||||
int result ;
|
||||
|
||||
if (ioctl (fd, FIONREAD, &result) == -1)
|
||||
return -1 ;
|
||||
|
||||
return result ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialGetchar:
|
||||
* Get a single character from the serial device.
|
||||
* Note: Zero is a valid character and this function will time-out after
|
||||
* 10 seconds.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int serialGetchar (int fd)
|
||||
{
|
||||
uint8_t x ;
|
||||
|
||||
if (read (fd, &x, 1) != 1)
|
||||
return -1 ;
|
||||
|
||||
return ((int)x) & 0xFF ;
|
||||
}
|
||||
38
WiringPi/wiringPi/wiringSerial.h
Normal file
38
WiringPi/wiringPi/wiringSerial.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* wiringSerial.h:
|
||||
* Handle a serial port
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int serialOpen (char *device, int baud) ;
|
||||
extern void serialClose (int fd) ;
|
||||
extern void serialFlush (int fd) ;
|
||||
extern void serialPutchar (int fd, unsigned char c) ;
|
||||
extern void serialPuts (int fd, char *s) ;
|
||||
extern void serialPrintf (int fd, char *message, ...) ;
|
||||
extern int serialDataAvail (int fd) ;
|
||||
extern int serialGetchar (int fd) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
83
WiringPi/wiringPi/wiringShift.c
Normal file
83
WiringPi/wiringPi/wiringShift.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* wiringShift.c:
|
||||
* Emulate some of the Arduino wiring functionality.
|
||||
*
|
||||
* Copyright (c) 2009-2012 Gordon Henderson.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringShift.h"
|
||||
|
||||
/*
|
||||
* shiftIn:
|
||||
* Shift data in from a clocked source
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order)
|
||||
{
|
||||
uint8_t value = 0 ;
|
||||
int8_t i ;
|
||||
|
||||
if (order == MSBFIRST)
|
||||
for (i = 7 ; i >= 0 ; --i)
|
||||
{
|
||||
digitalWrite (cPin, HIGH) ;
|
||||
value |= digitalRead (dPin) << i ;
|
||||
digitalWrite (cPin, LOW) ;
|
||||
}
|
||||
else
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
{
|
||||
digitalWrite (cPin, HIGH) ;
|
||||
value |= digitalRead (dPin) << i ;
|
||||
digitalWrite (cPin, LOW) ;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/*
|
||||
* shiftOut:
|
||||
* Shift data out to a clocked source
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val)
|
||||
{
|
||||
int8_t i;
|
||||
|
||||
if (order == MSBFIRST)
|
||||
for (i = 7 ; i >= 0 ; --i)
|
||||
{
|
||||
digitalWrite (dPin, val & (1 << i)) ;
|
||||
digitalWrite (cPin, HIGH) ;
|
||||
digitalWrite (cPin, LOW) ;
|
||||
}
|
||||
else
|
||||
for (i = 0 ; i < 8 ; ++i)
|
||||
{
|
||||
digitalWrite (dPin, val & (1 << i)) ;
|
||||
digitalWrite (cPin, HIGH) ;
|
||||
digitalWrite (cPin, LOW) ;
|
||||
}
|
||||
}
|
||||
41
WiringPi/wiringPi/wiringShift.h
Normal file
41
WiringPi/wiringPi/wiringShift.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* wiringShift.h:
|
||||
* Emulate some of the Arduino wiring functionality.
|
||||
*
|
||||
* Copyright (c) 2009-2012 Gordon Henderson.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#define LSBFIRST 0
|
||||
#define MSBFIRST 1
|
||||
|
||||
#ifndef _STDINT_H
|
||||
# include <stdint.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order) ;
|
||||
extern void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user