initial cleanup and mapping
This commit is contained in:
165
wiringPi/COPYING.LESSER
Normal file
165
wiringPi/COPYING.LESSER
Normal file
@@ -0,0 +1,165 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
196
wiringPi/Makefile
Normal file
196
wiringPi/Makefile
Normal file
@@ -0,0 +1,196 @@
|
||||
# ;
|
||||
# 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.
|
||||
DEFS = -D_GNU_SOURCE
|
||||
CFLAGS = $(DEBUG) $(DEFS) -Wformat=2 -Wall -Winline $(INCLUDE) -pipe -fPIC
|
||||
|
||||
LIBS =
|
||||
|
||||
# Should not alter anything below this line
|
||||
###############################################################################
|
||||
|
||||
SRC = wiringPi.c \
|
||||
wiringSerial.c wiringShift.c \
|
||||
piHiPri.c piThread.c \
|
||||
wiringPiSPI.c wiringPiI2C.c \
|
||||
softPwm.c softTone.c softServo.c \
|
||||
mcp23008.c mcp23016.c mcp23017.c \
|
||||
mcp23s08.c mcp23s17.c \
|
||||
sr595.c \
|
||||
pcf8574.c pcf8591.c \
|
||||
mcp3002.c mcp3004.c mcp4802.c mcp3422.c \
|
||||
max31855.c max5322.c \
|
||||
sn3218.c \
|
||||
drcSerial.c
|
||||
|
||||
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:
|
||||
@echo "[Clean]"
|
||||
@rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPi.*
|
||||
|
||||
.PHONEY: tags
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
|
||||
.PHONEY: install-headers
|
||||
install-headers:
|
||||
@echo "[Install Headers]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringPi.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringSerial.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringShift.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 softPwm.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 softServo.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 drcSerial.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23008.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23016.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 max31855.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 max5322.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp3002.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp3004.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp4802.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp3422.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 sr595.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 pcf8574.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 pcf8591.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 sn3218.h $(DESTDIR)$(PREFIX)/include
|
||||
|
||||
.PHONEY: install
|
||||
install: $(DYNAMIC) install-headers
|
||||
@echo "[Install Dynamic Lib]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
|
||||
@install -m 0755 libwiringPi.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION)
|
||||
@ln -sf $(DESTDIR)$(PREFIX)/lib/libwiringPi.so.$(VERSION) $(DESTDIR)/lib/libwiringPi.so
|
||||
@ldconfig
|
||||
|
||||
.PHONEY: install-static
|
||||
install-static: $(STATIC) install-headers
|
||||
@echo "[Install Static Lib]"
|
||||
@install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
|
||||
@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/softPwm.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/softTone.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/softServo.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/drcSerial.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23008.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23016.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/max31855.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/max5322.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3004.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3422.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/sr595.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/pcf8574.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/pcf8591.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/sn3218.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/lib/libwiringPi.*
|
||||
@ldconfig
|
||||
|
||||
|
||||
.PHONEY: depend
|
||||
depend:
|
||||
makedepend -Y $(SRC) $(SRC_I2C)
|
||||
|
||||
# DO NOT DELETE
|
||||
|
||||
wiringPi.o: softPwm.h softTone.h wiringPi.h
|
||||
wiringSerial.o: wiringSerial.h
|
||||
wiringShift.o: wiringPi.h wiringShift.h
|
||||
piHiPri.o: wiringPi.h
|
||||
piThread.o: wiringPi.h
|
||||
wiringPiSPI.o: wiringPi.h wiringPiSPI.h
|
||||
wiringPiI2C.o: wiringPi.h wiringPiI2C.h
|
||||
softPwm.o: wiringPi.h softPwm.h
|
||||
softTone.o: wiringPi.h softTone.h
|
||||
softServo.o: wiringPi.h softServo.h
|
||||
mcp23008.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23008.h
|
||||
mcp23016.o: wiringPi.h wiringPiI2C.h mcp23016.h mcp23016reg.h
|
||||
mcp23017.o: wiringPi.h wiringPiI2C.h mcp23x0817.h mcp23017.h
|
||||
mcp23s08.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s08.h
|
||||
mcp23s17.o: wiringPi.h wiringPiSPI.h mcp23x0817.h mcp23s17.h
|
||||
sr595.o: wiringPi.h sr595.h
|
||||
pcf8574.o: wiringPi.h wiringPiI2C.h pcf8574.h
|
||||
pcf8591.o: wiringPi.h wiringPiI2C.h pcf8591.h
|
||||
mcp3002.o: wiringPi.h wiringPiSPI.h mcp3002.h
|
||||
mcp3004.o: wiringPi.h wiringPiSPI.h mcp3004.h
|
||||
mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h
|
||||
mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h
|
||||
max31855.o: wiringPi.h wiringPiSPI.h max31855.h
|
||||
max5322.o: wiringPi.h wiringPiSPI.h max5322.h
|
||||
sn3218.o: wiringPi.h wiringPiI2C.h sn3218.h
|
||||
drcSerial.o: wiringPi.h wiringSerial.h drcSerial.h
|
201
wiringPi/drcSerial.c
Normal file
201
wiringPi/drcSerial.c
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* drcSerial.c:
|
||||
* Extend wiringPi with the DRC Serial protocol (e.g. to Arduino)
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringSerial.h"
|
||||
|
||||
#include "drcSerial.h"
|
||||
|
||||
#ifndef TRUE
|
||||
# define TRUE (1==1)
|
||||
# define FALSE (1==2)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* myPinMode:
|
||||
* Change the pin mode on the remote DRC device
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
/**/ if (mode == OUTPUT)
|
||||
serialPutchar (node->fd, 'o') ; // Input
|
||||
else if (mode == PWM_OUTPUT)
|
||||
serialPutchar (node->fd, 'p') ; // PWM
|
||||
else
|
||||
serialPutchar (node->fd, 'i') ; // Default to input
|
||||
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
* ATmegas only have pull-up's on of off. No pull-downs.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
|
||||
// Force pin into input mode
|
||||
|
||||
serialPutchar (node->fd, 'i' ) ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
|
||||
/**/ if (mode == PUD_UP)
|
||||
{
|
||||
serialPutchar (node->fd, '1') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
}
|
||||
else if (mode == PUD_OFF)
|
||||
{
|
||||
serialPutchar (node->fd, '0') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
serialPutchar (node->fd, value == 0 ? '0' : '1') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPwmWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPwmWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
serialPutchar (node->fd, 'v') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
serialPutchar (node->fd, value & 0xFF) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myAnalogRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int vHi, vLo ;
|
||||
|
||||
serialPutchar (node->fd, 'a') ;
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
vHi = serialGetchar (node->fd) ;
|
||||
vLo = serialGetchar (node->fd) ;
|
||||
|
||||
return (vHi << 8) | vLo ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
serialPutchar (node->fd, 'r') ; // Send read command
|
||||
serialPutchar (node->fd, pin - node->pinBase) ;
|
||||
return (serialGetchar (node->fd) == '0') ? 0 : 1 ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* drcSetup:
|
||||
* Create a new instance of an DRC GPIO interface.
|
||||
* Could be a variable nunber of pins here - we might not know in advance
|
||||
* if it's an ATmega with 14 pins, or something with less or more!
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int drcSetupSerial (const int pinBase, const int numPins, const char *device, const int baud)
|
||||
{
|
||||
int fd ;
|
||||
int ok, tries ;
|
||||
time_t then ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = serialOpen (device, baud)) < 0)
|
||||
return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ;
|
||||
|
||||
delay (10) ; // May need longer if it's an Uno that reboots on the open...
|
||||
|
||||
// Flush any pending input
|
||||
|
||||
while (serialDataAvail (fd))
|
||||
(void)serialGetchar (fd) ;
|
||||
|
||||
ok = FALSE ;
|
||||
for (tries = 1 ; (tries < 5) && (!ok) ; ++tries)
|
||||
{
|
||||
serialPutchar (fd, '@') ; // Ping
|
||||
then = time (NULL) + 2 ;
|
||||
while (time (NULL) < then)
|
||||
if (serialDataAvail (fd))
|
||||
{
|
||||
if (serialGetchar (fd) == '@')
|
||||
{
|
||||
ok = TRUE ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
serialClose (fd) ;
|
||||
return wiringPiFailure (WPI_FATAL, "Unable to communicate with DRC serial device") ;
|
||||
}
|
||||
|
||||
node = wiringPiNewNode (pinBase, numPins) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->pinMode = myPinMode ;
|
||||
node->pullUpDnControl = myPullUpDnControl ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->pwmWrite = myPwmWrite ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/drcSerial.h
Normal file
33
wiringPi/drcSerial.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* drcSerial.h:
|
||||
* Extend wiringPi with the DRC Serial protocol (e.g. to Arduino)
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int drcSetupSerial (const int pinBase, const int numPins, const char *device, const int baud) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
81
wiringPi/max31855.c
Normal file
81
wiringPi/max31855.c
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* max31855.c:
|
||||
* Extend wiringPi with the max31855 SPI Analog to Digital convertor
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "max31855.h"
|
||||
|
||||
/*
|
||||
* myAnalogRead:
|
||||
* Return the analog value of the given pin
|
||||
* Note: The chip really only has one read "channel", but we're faking it
|
||||
* here so we can read the error registers. Channel 0 will be the data
|
||||
* channel, and 1 is the error register code.
|
||||
* Note: Temperature returned is temp in C * 4, so divide result by 4
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
unsigned int spiData ;
|
||||
int temp ;
|
||||
int chan = pin - node->pinBase ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
|
||||
|
||||
if (chan == 0) // Read temp in C
|
||||
{
|
||||
spiData >>= 18 ;
|
||||
temp = spiData & 0x3FFF ; // Bottom 13 bits
|
||||
if ((spiData & 0x2000) != 0) // Negative
|
||||
temp = -temp ;
|
||||
return temp ;
|
||||
}
|
||||
else // Return error bits
|
||||
return spiData & 0x7 ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* max31855Setup:
|
||||
* Create a new wiringPi device node for an max31855 on the Pi's
|
||||
* SPI interface.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int max31855Setup (const int pinBase, int spiChannel)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
|
||||
return -1 ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 2) ;
|
||||
|
||||
node->fd = spiChannel ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/max31855.h
Normal file
33
wiringPi/max31855.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* max31855.c:
|
||||
* Extend wiringPi with the MAX31855 SPI Thermocouple driver
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int max31855Setup (int pinBase, int spiChannel) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
84
wiringPi/max5322.c
Normal file
84
wiringPi/max5322.c
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* max5322.c:
|
||||
* Extend wiringPi with the MAX5322 SPI Digital to Analog convertor
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "max5322.h"
|
||||
|
||||
/*
|
||||
* myAnalogWrite:
|
||||
* Write analog value on the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
unsigned char spiData [2] ;
|
||||
unsigned char chanBits, dataBits ;
|
||||
int chan = pin - node->pinBase ;
|
||||
|
||||
if (chan == 0)
|
||||
chanBits = 0b01000000 ;
|
||||
else
|
||||
chanBits = 0b01010000 ;
|
||||
|
||||
chanBits |= ((value >> 12) & 0x0F) ;
|
||||
dataBits = ((value ) & 0xFF) ;
|
||||
|
||||
spiData [0] = chanBits ;
|
||||
spiData [1] = dataBits ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, spiData, 2) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* max5322Setup:
|
||||
* Create a new wiringPi device node for an max5322 on the Pi's
|
||||
* SPI interface.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int max5322Setup (const int pinBase, int spiChannel)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
unsigned char spiData [2] ;
|
||||
|
||||
if (wiringPiSPISetup (spiChannel, 8000000) < 0) // 10MHz Max
|
||||
return -1 ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 2) ;
|
||||
|
||||
node->fd = spiChannel ;
|
||||
node->analogWrite = myAnalogWrite ;
|
||||
|
||||
// Enable both DACs
|
||||
|
||||
spiData [0] = 0b11100000 ;
|
||||
spiData [1] = 0 ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, spiData, 2) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/max5322.h
Normal file
33
wiringPi/max5322.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* max5322.h:
|
||||
* Extend wiringPi with the MAX5322 SPI Digital to Analog convertor
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int max5322Setup (int pinBase, int spiChannel) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
149
wiringPi/mcp23008.c
Normal file
149
wiringPi/mcp23008.c
Normal file
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* mcp23008.c:
|
||||
* Extend wiringPi with the MCP 23008 I2C GPIO expander chip
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiI2C.h"
|
||||
#include "mcp23x0817.h"
|
||||
|
||||
#include "mcp23008.h"
|
||||
|
||||
|
||||
/*
|
||||
* myPinMode:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, reg ;
|
||||
|
||||
reg = MCP23x08_IODIR ;
|
||||
mask = 1 << (pin - node->pinBase) ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, reg ;
|
||||
|
||||
reg = MCP23x08_GPPU ;
|
||||
mask = 1 << (pin - node->pinBase) ;
|
||||
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
bit = 1 << ((pin - node->pinBase) & 7) ;
|
||||
|
||||
old = node->data2 ;
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, MCP23x08_GPIO, old) ;
|
||||
node->data2 = old ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int mask, value ;
|
||||
|
||||
mask = 1 << ((pin - node->pinBase) & 7) ;
|
||||
value = wiringPiI2CReadReg8 (node->fd, MCP23x08_GPIO) ;
|
||||
|
||||
if ((value & mask) == 0)
|
||||
return LOW ;
|
||||
else
|
||||
return HIGH ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp23008Setup:
|
||||
* Create a new instance of an MCP23008 I2C GPIO interface. We know it
|
||||
* has 8 pins, so all we need to know here is the I2C address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23008Setup (const int pinBase, const int i2cAddress)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
||||
return fd ;
|
||||
|
||||
wiringPiI2CWriteReg8 (fd, MCP23x08_IOCON, IOCON_INIT) ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 8) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->pinMode = myPinMode ;
|
||||
node->pullUpDnControl = myPullUpDnControl ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->data2 = wiringPiI2CReadReg8 (fd, MCP23x08_OLAT) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/mcp23008.h
Normal file
33
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 (const int pinBase, const int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
164
wiringPi/mcp23016.c
Normal file
164
wiringPi/mcp23016.c
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* mcp23016.c:
|
||||
* Extend wiringPi with the MCP 23016 I2C GPIO expander chip
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiI2C.h"
|
||||
#include "mcp23016.h"
|
||||
|
||||
#include "mcp23016reg.h"
|
||||
|
||||
|
||||
/*
|
||||
* myPinMode:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
reg = MCP23016_IODIR0 ;
|
||||
else
|
||||
{
|
||||
reg = MCP23016_IODIR1 ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
pin -= node->pinBase ; // Pin now 0-15
|
||||
|
||||
bit = 1 << (pin & 7) ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
{
|
||||
old = node->data2 ;
|
||||
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, MCP23016_GP0, old) ;
|
||||
node->data2 = old ;
|
||||
}
|
||||
else // Bank B
|
||||
{
|
||||
old = node->data3 ;
|
||||
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, MCP23016_GP1, old) ;
|
||||
node->data3 = old ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int mask, value, gpio ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
gpio = MCP23016_GP0 ;
|
||||
else
|
||||
{
|
||||
gpio = MCP23016_GP1 ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
value = wiringPiI2CReadReg8 (node->fd, gpio) ;
|
||||
|
||||
if ((value & mask) == 0)
|
||||
return LOW ;
|
||||
else
|
||||
return HIGH ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp23016Setup:
|
||||
* Create a new instance of an MCP23016 I2C GPIO interface. We know it
|
||||
* has 16 pins, so all we need to know here is the I2C address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23016Setup (const int pinBase, const int i2cAddress)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
||||
return fd ;
|
||||
|
||||
wiringPiI2CWriteReg8 (fd, MCP23016_IOCON0, IOCON_INIT) ;
|
||||
wiringPiI2CWriteReg8 (fd, MCP23016_IOCON1, IOCON_INIT) ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 16) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->pinMode = myPinMode ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->data2 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT0) ;
|
||||
node->data3 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT1) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/mcp23016.h
Normal file
33
wiringPi/mcp23016.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* mcp23016.h:
|
||||
* Extend wiringPi with the MCP 23016 I2C GPIO expander chip
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int mcp23016Setup (const int pinBase, const int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
48
wiringPi/mcp23016reg.h
Normal file
48
wiringPi/mcp23016reg.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* mcp23016:
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
*
|
||||
* Header file for code using the MCP23016 GPIO expander
|
||||
* chip.
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
// MCP23016 Registers
|
||||
|
||||
#define MCP23016_GP0 0x00
|
||||
#define MCP23016_GP1 0x01
|
||||
#define MCP23016_OLAT0 0x02
|
||||
#define MCP23016_OLAT1 0x03
|
||||
#define MCP23016_IPOL0 0x04
|
||||
#define MCP23016_IPOL1 0x05
|
||||
#define MCP23016_IODIR0 0x06
|
||||
#define MCP23016_IODIR1 0x07
|
||||
#define MCP23016_INTCAP0 0x08
|
||||
#define MCP23016_INTCAP1 0x09
|
||||
#define MCP23016_IOCON0 0x0A
|
||||
#define MCP23016_IOCON1 0x0B
|
||||
|
||||
// Bits in the IOCON register
|
||||
|
||||
#define IOCON_IARES 0x01
|
||||
|
||||
// Default initialisation mode
|
||||
|
||||
#define IOCON_INIT 0
|
195
wiringPi/mcp23017.c
Normal file
195
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, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
reg = MCP23x17_IODIRA ;
|
||||
else
|
||||
{
|
||||
reg = MCP23x17_IODIRB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
reg = MCP23x17_GPPUA ;
|
||||
else
|
||||
{
|
||||
reg = MCP23x17_GPPUB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = wiringPiI2CReadReg8 (node->fd, reg) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
pin -= node->pinBase ; // Pin now 0-15
|
||||
|
||||
bit = 1 << (pin & 7) ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
{
|
||||
old = node->data2 ;
|
||||
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, MCP23x17_GPIOA, old) ;
|
||||
node->data2 = old ;
|
||||
}
|
||||
else // Bank B
|
||||
{
|
||||
old = node->data3 ;
|
||||
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
wiringPiI2CWriteReg8 (node->fd, MCP23x17_GPIOB, old) ;
|
||||
node->data3 = old ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int mask, value, gpio ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
gpio = MCP23x17_GPIOA ;
|
||||
else
|
||||
{
|
||||
gpio = MCP23x17_GPIOB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
value = wiringPiI2CReadReg8 (node->fd, gpio) ;
|
||||
|
||||
if ((value & mask) == 0)
|
||||
return LOW ;
|
||||
else
|
||||
return HIGH ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp23017Setup:
|
||||
* Create a new instance of an MCP23017 I2C GPIO interface. We know it
|
||||
* has 16 pins, so all we need to know here is the I2C address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23017Setup (const int pinBase, const int i2cAddress)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
||||
return fd ;
|
||||
|
||||
wiringPiI2CWriteReg8 (fd, MCP23x17_IOCON, IOCON_INIT) ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 16) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->pinMode = myPinMode ;
|
||||
node->pullUpDnControl = myPullUpDnControl ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->data2 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATA) ;
|
||||
node->data3 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATB) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/mcp23017.h
Normal file
33
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 (const int pinBase, const int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
189
wiringPi/mcp23s08.c
Normal file
189
wiringPi/mcp23s08.c
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* mcp23s08.c:
|
||||
* Extend wiringPi with the MCP 23s08 SPI GPIO expander chip
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiSPI.h"
|
||||
#include "mcp23x0817.h"
|
||||
|
||||
#include "mcp23s08.h"
|
||||
|
||||
#define MCP_SPEED 4000000
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* writeByte:
|
||||
* Write a byte to a register on the MCP23s08 on the SPI bus.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void writeByte (uint8_t spiPort, uint8_t devId, uint8_t reg, uint8_t data)
|
||||
{
|
||||
uint8_t spiData [4] ;
|
||||
|
||||
spiData [0] = CMD_WRITE | ((devId & 7) << 1) ;
|
||||
spiData [1] = reg ;
|
||||
spiData [2] = data ;
|
||||
|
||||
wiringPiSPIDataRW (spiPort, spiData, 3) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* readByte:
|
||||
* Read a byte from a register on the MCP23s08 on the SPI bus.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
|
||||
{
|
||||
uint8_t spiData [4] ;
|
||||
|
||||
spiData [0] = CMD_READ | ((devId & 7) << 1) ;
|
||||
spiData [1] = reg ;
|
||||
|
||||
wiringPiSPIDataRW (spiPort, spiData, 3) ;
|
||||
|
||||
return spiData [2] ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPinMode:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, reg ;
|
||||
|
||||
reg = MCP23x08_IODIR ;
|
||||
mask = 1 << (pin - node->pinBase) ;
|
||||
old = readByte (node->data0, node->data1, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
writeByte (node->data0, node->data1, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, reg ;
|
||||
|
||||
reg = MCP23x08_GPPU ;
|
||||
mask = 1 << (pin - node->pinBase) ;
|
||||
|
||||
old = readByte (node->data0, node->data1, reg) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
writeByte (node->data0, node->data1, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
bit = 1 << ((pin - node->pinBase) & 7) ;
|
||||
|
||||
old = node->data2 ;
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
writeByte (node->data0, node->data1, MCP23x08_GPIO, old) ;
|
||||
node->data2 = old ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int mask, value ;
|
||||
|
||||
mask = 1 << ((pin - node->pinBase) & 7) ;
|
||||
value = readByte (node->data0, node->data1, MCP23x08_GPIO) ;
|
||||
|
||||
if ((value & mask) == 0)
|
||||
return LOW ;
|
||||
else
|
||||
return HIGH ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp23s08Setup:
|
||||
* Create a new instance of an MCP23s08 SPI GPIO interface. We know it
|
||||
* has 8 pins, so all we need to know here is the SPI address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23s08Setup (const int pinBase, const int spiPort, const int devId)
|
||||
{
|
||||
int x ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0)
|
||||
return x ;
|
||||
|
||||
writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 8) ;
|
||||
|
||||
node->data0 = spiPort ;
|
||||
node->data1 = devId ;
|
||||
node->pinMode = myPinMode ;
|
||||
node->pullUpDnControl = myPullUpDnControl ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->data2 = readByte (spiPort, devId, MCP23x08_OLAT) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/mcp23s08.h
Normal file
33
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 (const int pinBase, const int spiPort, const int devId) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
236
wiringPi/mcp23s17.c
Normal file
236
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, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
reg = MCP23x17_IODIRA ;
|
||||
else
|
||||
{
|
||||
reg = MCP23x17_IODIRB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = readByte (node->data0, node->data1, reg) ;
|
||||
|
||||
if (mode == OUTPUT)
|
||||
old &= (~mask) ;
|
||||
else
|
||||
old |= mask ;
|
||||
|
||||
writeByte (node->data0, node->data1, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myPullUpDnControl:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int mask, old, reg ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
reg = MCP23x17_GPPUA ;
|
||||
else
|
||||
{
|
||||
reg = MCP23x17_GPPUB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
old = readByte (node->data0, node->data1, reg) ;
|
||||
|
||||
if (mode == PUD_UP)
|
||||
old |= mask ;
|
||||
else
|
||||
old &= (~mask) ;
|
||||
|
||||
writeByte (node->data0, node->data1, reg, old) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
pin -= node->pinBase ; // Pin now 0-15
|
||||
|
||||
bit = 1 << (pin & 7) ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
{
|
||||
old = node->data2 ;
|
||||
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
writeByte (node->data0, node->data1, MCP23x17_GPIOA, old) ;
|
||||
node->data2 = old ;
|
||||
}
|
||||
else // Bank B
|
||||
{
|
||||
old = node->data3 ;
|
||||
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
writeByte (node->data0, node->data1, MCP23x17_GPIOB, old) ;
|
||||
node->data3 = old ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int mask, value, gpio ;
|
||||
|
||||
pin -= node->pinBase ;
|
||||
|
||||
if (pin < 8) // Bank A
|
||||
gpio = MCP23x17_GPIOA ;
|
||||
else
|
||||
{
|
||||
gpio = MCP23x17_GPIOB ;
|
||||
pin &= 0x07 ;
|
||||
}
|
||||
|
||||
mask = 1 << pin ;
|
||||
value = readByte (node->data0, node->data1, gpio) ;
|
||||
|
||||
if ((value & mask) == 0)
|
||||
return LOW ;
|
||||
else
|
||||
return HIGH ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp23s17Setup:
|
||||
* Create a new instance of an MCP23s17 SPI GPIO interface. We know it
|
||||
* has 16 pins, so all we need to know here is the SPI address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp23s17Setup (const int pinBase, const int spiPort, const int devId)
|
||||
{
|
||||
int x ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0)
|
||||
return x ;
|
||||
|
||||
writeByte (spiPort, devId, MCP23x17_IOCON, IOCON_INIT | IOCON_HAEN) ;
|
||||
writeByte (spiPort, devId, MCP23x17_IOCONB, IOCON_INIT | IOCON_HAEN) ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 16) ;
|
||||
|
||||
node->data0 = spiPort ;
|
||||
node->data1 = devId ;
|
||||
node->pinMode = myPinMode ;
|
||||
node->pullUpDnControl = myPullUpDnControl ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->data2 = readByte (spiPort, devId, MCP23x17_OLATA) ;
|
||||
node->data3 = readByte (spiPort, devId, MCP23x17_OLATB) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/mcp23s17.h
Normal file
33
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/mcp23x08.h
Normal file
73
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/mcp23x0817.h
Normal file
87
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
|
76
wiringPi/mcp3002.c
Normal file
76
wiringPi/mcp3002.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* mcp3002.c:
|
||||
* Extend wiringPi with the MCP3002 SPI Analog to Digital convertor
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "mcp3002.h"
|
||||
|
||||
/*
|
||||
* myAnalogRead:
|
||||
* Return the analog value of the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
unsigned char spiData [2] ;
|
||||
unsigned char chanBits ;
|
||||
int chan = pin - node->pinBase ;
|
||||
|
||||
if (chan == 0)
|
||||
chanBits = 0b11010000 ;
|
||||
else
|
||||
chanBits = 0b11110000 ;
|
||||
|
||||
spiData [0] = chanBits ;
|
||||
spiData [1] = 0 ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, spiData, 2) ;
|
||||
|
||||
return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp3002Setup:
|
||||
* Create a new wiringPi device node for an mcp3002 on the Pi's
|
||||
* SPI interface.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp3002Setup (const int pinBase, int spiChannel)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
|
||||
return -1 ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 2) ;
|
||||
|
||||
node->fd = spiChannel ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/mcp3002.h
Normal file
33
wiringPi/mcp3002.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* mcp3002.c:
|
||||
* Extend wiringPi with the MCP3002 SPI Analog to Digital convertor
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int mcp3002Setup (int pinBase, int spiChannel) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
76
wiringPi/mcp3004.c
Normal file
76
wiringPi/mcp3004.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* mcp3004.c:
|
||||
* Extend wiringPi with the MCP3004 SPI Analog to Digital convertor
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
*
|
||||
* Thanks also to "ShorTie" on IRC for some remote debugging help!
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "mcp3004.h"
|
||||
|
||||
/*
|
||||
* myAnalogRead:
|
||||
* Return the analog value of the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
unsigned char spiData [3] ;
|
||||
unsigned char chanBits ;
|
||||
int chan = pin - node->pinBase ;
|
||||
|
||||
chanBits = 0b10000000 | (chan << 4) ;
|
||||
|
||||
spiData [0] = 1 ; // Start bit
|
||||
spiData [1] = chanBits ;
|
||||
spiData [2] = 0 ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, spiData, 3) ;
|
||||
|
||||
return ((spiData [1] << 8) | spiData [2]) & 0x3FF ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp3004Setup:
|
||||
* Create a new wiringPi device node for an mcp3004 on the Pi's
|
||||
* SPI interface.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp3004Setup (const int pinBase, int spiChannel)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
|
||||
return -1 ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 8) ;
|
||||
|
||||
node->fd = spiChannel ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/mcp3004.h
Normal file
33
wiringPi/mcp3004.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* mcp3004.c:
|
||||
* Extend wiringPi with the MCP3004 SPI Analog to Digital convertor
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int mcp3004Setup (int pinBase, int spiChannel) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
110
wiringPi/mcp3422.c
Normal file
110
wiringPi/mcp3422.c
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* mcp3422.c:
|
||||
* Extend wiringPi with the MCP3422 I2C ADC chip
|
||||
* Also works for the MCP3423 and MCP3224 (4 channel) chips
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/spi/spidev.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiI2C.h>
|
||||
|
||||
#include "mcp3422.h"
|
||||
|
||||
|
||||
/*
|
||||
* myAnalogRead:
|
||||
* Read a channel from the device
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
|
||||
{
|
||||
unsigned char config ;
|
||||
unsigned char buffer [4] ;
|
||||
int value = 0 ;
|
||||
|
||||
// One-shot mode, trigger plus the other configs.
|
||||
|
||||
config = 0x80 | ((chan - node->pinBase) << 5) | (node->data0 << 2) | (node->data1) ;
|
||||
|
||||
wiringPiI2CWrite (node->fd, config) ;
|
||||
|
||||
switch (node->data0) // Sample rate
|
||||
{
|
||||
case MCP3422_SR_3_75: // 18 bits
|
||||
delay (270) ;
|
||||
read (node->fd, buffer, 4) ;
|
||||
value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [0] ;
|
||||
break ;
|
||||
|
||||
case MCP3422_SR_15: // 16 bits
|
||||
delay ( 70) ;
|
||||
read (node->fd, buffer, 3) ;
|
||||
value = (buffer [0] << 8) | buffer [1] ;
|
||||
break ;
|
||||
|
||||
case MCP3422_SR_60: // 14 bits
|
||||
delay ( 17) ;
|
||||
read (node->fd, buffer, 3) ;
|
||||
value = ((buffer [0] & 0x3F) << 8) | buffer [1] ;
|
||||
break ;
|
||||
|
||||
case MCP3422_SR_240: // 12 bits
|
||||
delay ( 5) ;
|
||||
read (node->fd, buffer, 3) ;
|
||||
value = ((buffer [0] & 0x0F) << 8) | buffer [0] ;
|
||||
break ;
|
||||
}
|
||||
|
||||
return value ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* mcp3422Setup:
|
||||
* Create a new wiringPi device node for the mcp3422
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
||||
return fd ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 4) ;
|
||||
|
||||
node->data0 = sampleRate ;
|
||||
node->data1 = gain ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
|
||||
return 0 ;
|
||||
}
|
43
wiringPi/mcp3422.h
Normal file
43
wiringPi/mcp3422.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* mcp3422.c:
|
||||
* Extend wiringPi with the MCP3422 I2C ADC chip
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#define MCP3422_SR_3_75 0
|
||||
#define MCP3422_SR_15 1
|
||||
#define MCP3422_SR_60 2
|
||||
#define MCP3422_SR_240 3
|
||||
|
||||
#define MCP3422_GAIN_1 0
|
||||
#define MCP3422_GAIN_2 1
|
||||
#define MCP3422_GAIN_4 2
|
||||
#define MCP3422_GAIN_8 3
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
76
wiringPi/mcp4802.c
Normal file
76
wiringPi/mcp4802.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* mcp4802.c:
|
||||
* Extend wiringPi with the MCP4802 SPI Digital to Analog convertor
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#include "mcp4802.h"
|
||||
|
||||
/*
|
||||
* myAnalogWrite:
|
||||
* Write analog value on the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
unsigned char spiData [2] ;
|
||||
unsigned char chanBits, dataBits ;
|
||||
int chan = pin - node->pinBase ;
|
||||
|
||||
if (chan == 0)
|
||||
chanBits = 0x30 ;
|
||||
else
|
||||
chanBits = 0xB0 ;
|
||||
|
||||
chanBits |= ((value >> 4) & 0x0F) ;
|
||||
dataBits = ((value << 4) & 0xF0) ;
|
||||
|
||||
spiData [0] = chanBits ;
|
||||
spiData [1] = dataBits ;
|
||||
|
||||
wiringPiSPIDataRW (node->fd, spiData, 2) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* mcp4802Setup:
|
||||
* Create a new wiringPi device node for an mcp4802 on the Pi's
|
||||
* SPI interface.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int mcp4802Setup (const int pinBase, int spiChannel)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
|
||||
return -1 ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 2) ;
|
||||
|
||||
node->fd = spiChannel ;
|
||||
node->analogWrite = myAnalogWrite ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/mcp4802.h
Normal file
33
wiringPi/mcp4802.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* mcp4802.c:
|
||||
* Extend wiringPi with the MCP4802 SPI Digital to Analog convertor
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int mcp4802Setup (int pinBase, int spiChannel) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
126
wiringPi/pcf8574.c
Normal file
126
wiringPi/pcf8574.c
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* pcf8574.c:
|
||||
* Extend wiringPi with the PCF8574 I2C GPIO expander chip
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiI2C.h"
|
||||
|
||||
#include "pcf8574.h"
|
||||
|
||||
|
||||
/*
|
||||
* myPinMode:
|
||||
* The PCF8574 is an odd chip - the pins are effectively bi-directional,
|
||||
* however the pins should be drven high when used as an input pin...
|
||||
* So, we're effectively copying digitalWrite...
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
bit = 1 << ((pin - node->pinBase) & 7) ;
|
||||
|
||||
old = node->data2 ;
|
||||
if (mode == OUTPUT)
|
||||
old &= (~bit) ; // Write bit to 0
|
||||
else
|
||||
old |= bit ; // Write bit to 1
|
||||
|
||||
wiringPiI2CWrite (node->fd, old) ;
|
||||
node->data2 = old ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int bit, old ;
|
||||
|
||||
bit = 1 << ((pin - node->pinBase) & 7) ;
|
||||
|
||||
old = node->data2 ;
|
||||
if (value == LOW)
|
||||
old &= (~bit) ;
|
||||
else
|
||||
old |= bit ;
|
||||
|
||||
wiringPiI2CWrite (node->fd, old) ;
|
||||
node->data2 = old ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int mask, value ;
|
||||
|
||||
mask = 1 << ((pin - node->pinBase) & 7) ;
|
||||
value = wiringPiI2CRead (node->fd) ;
|
||||
|
||||
if ((value & mask) == 0)
|
||||
return LOW ;
|
||||
else
|
||||
return HIGH ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* pcf8574Setup:
|
||||
* Create a new instance of a PCF8574 I2C GPIO interface. We know it
|
||||
* has 8 pins, so all we need to know here is the I2C address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int pcf8574Setup (const int pinBase, const int i2cAddress)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
||||
return fd ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 8) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->pinMode = myPinMode ;
|
||||
node->digitalRead = myDigitalRead ;
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
node->data2 = wiringPiI2CRead (fd) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/pcf8574.h
Normal file
33
wiringPi/pcf8574.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* pcf8574.h:
|
||||
* Extend wiringPi with the PCF8574 I2C GPIO expander chip
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int pcf8574Setup (const int pinBase, const int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
90
wiringPi/pcf8591.c
Normal file
90
wiringPi/pcf8591.c
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* pcf8591.c:
|
||||
* Extend wiringPi with the PCF8591 I2C GPIO Analog expander chip
|
||||
* The chip has 1 8-bit DAC and 4 x 8-bit ADCs
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiI2C.h"
|
||||
|
||||
#include "pcf8591.h"
|
||||
|
||||
|
||||
/*
|
||||
* myAnalogWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
unsigned char b [2] ;
|
||||
b [0] = 0x40 ;
|
||||
b [1] = value & 0xFF ;
|
||||
write (node->fd, b, 2) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* myAnalogRead:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int x ;
|
||||
|
||||
wiringPiI2CWrite (node->fd, 0x40 | ((pin - node->pinBase) & 3)) ;
|
||||
|
||||
x = wiringPiI2CRead (node->fd) ; // Throw away the first read
|
||||
x = wiringPiI2CRead (node->fd) ;
|
||||
|
||||
return x ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* pcf8591Setup:
|
||||
* Create a new instance of a PCF8591 I2C GPIO interface. We know it
|
||||
* has 4 pins, (4 analog inputs and 1 analog output which we'll shadow
|
||||
* input 0) so all we need to know here is the I2C address and the
|
||||
* user-defined pin base.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int pcf8591Setup (const int pinBase, const int i2cAddress)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
||||
return fd ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, 4) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->analogRead = myAnalogRead ;
|
||||
node->analogWrite = myAnalogWrite ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/pcf8591.h
Normal file
33
wiringPi/pcf8591.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* pcf8591.h:
|
||||
* Extend wiringPi with the PCF8591 I2C GPIO Analog expander chip
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int pcf8591Setup (const int pinBase, const int i2cAddress) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
51
wiringPi/piHiPri.c
Normal file
51
wiringPi/piHiPri.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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 (const int pri)
|
||||
{
|
||||
struct sched_param sched ;
|
||||
|
||||
memset (&sched, 0, sizeof(sched)) ;
|
||||
|
||||
if (pri > sched_get_priority_max (SCHED_RR))
|
||||
sched.sched_priority = sched_get_priority_max (SCHED_RR) ;
|
||||
else
|
||||
sched.sched_priority = pri ;
|
||||
|
||||
return sched_setscheduler (0, SCHED_RR, &sched) ;
|
||||
}
|
63
wiringPi/piThread.c
Normal file
63
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]) ;
|
||||
}
|
||||
|
75
wiringPi/sn3218.c
Normal file
75
wiringPi/sn3218.c
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* sn3218.c:
|
||||
* Extend wiringPi with the SN3218 I2C LEd Driver
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiI2C.h>
|
||||
|
||||
#include "sn3218.h"
|
||||
|
||||
/*
|
||||
* myAnalogWrite:
|
||||
* Write analog value on the given pin
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int fd = node->fd ;
|
||||
int chan = 0x01 + (pin - node->pinBase) ;
|
||||
|
||||
wiringPiI2CWriteReg8 (fd, chan, value & 0xFF) ; // Value
|
||||
wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update
|
||||
}
|
||||
|
||||
/*
|
||||
* sn3218Setup:
|
||||
* Create a new wiringPi device node for an sn3218 on the Pi's
|
||||
* SPI interface.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int sn3218Setup (const int pinBase)
|
||||
{
|
||||
int fd ;
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
if ((fd = wiringPiI2CSetup (0x54)) < 0)
|
||||
return fd ;
|
||||
|
||||
// Setup the chip - initialise all 18 LEDs to off
|
||||
|
||||
//wiringPiI2CWriteReg8 (fd, 0x17, 0) ; // Reset
|
||||
wiringPiI2CWriteReg8 (fd, 0x00, 1) ; // Not Shutdown
|
||||
wiringPiI2CWriteReg8 (fd, 0x13, 0x3F) ; // Enable LEDs 0- 5
|
||||
wiringPiI2CWriteReg8 (fd, 0x14, 0x3F) ; // Enable LEDs 6-11
|
||||
wiringPiI2CWriteReg8 (fd, 0x15, 0x3F) ; // Enable LEDs 12-17
|
||||
wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update
|
||||
|
||||
node = wiringPiNewNode (pinBase, 18) ;
|
||||
|
||||
node->fd = fd ;
|
||||
node->analogWrite = myAnalogWrite ;
|
||||
|
||||
return 0 ;
|
||||
}
|
33
wiringPi/sn3218.h
Normal file
33
wiringPi/sn3218.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* sn3218.c:
|
||||
* Extend wiringPi with the SN3218 I2C LED driver board.
|
||||
* Copyright (c) 2012-2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int sn3218Setup (int pinBase) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
167
wiringPi/softPwm.c
Normal file
167
wiringPi/softPwm.c
Normal file
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* softPwm.c:
|
||||
* Provide 2 channels of software driven PWM.
|
||||
* Copyright (c) 2012-2014 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "softPwm.h"
|
||||
|
||||
// MAX_PINS:
|
||||
// This is more than the number of Pi pins because we can actually softPwm
|
||||
// pins that are on GPIO expanders. It's not that efficient and more than 1 or
|
||||
// 2 pins on e.g. (SPI) mcp23s17 won't really be that effective, however...
|
||||
|
||||
#define MAX_PINS 1024
|
||||
|
||||
// The PWM Frequency is derived from the "pulse time" below. Essentially,
|
||||
// the frequency is a function of the range and this pulse time.
|
||||
// The total period will be range * pulse time in µS, so a pulse time
|
||||
// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 µS
|
||||
// which is a frequency of 100Hz.
|
||||
//
|
||||
// It's possible to get a higher frequency by lowering the pulse time,
|
||||
// however CPU uage will skyrocket as wiringPi uses a hard-loop to time
|
||||
// periods under 100µS - this is because the Linux timer calls are just
|
||||
// accurate at all, and have an overhead.
|
||||
//
|
||||
// Another way to increase the frequency is to reduce the range - however
|
||||
// that reduces the overall output accuracy...
|
||||
|
||||
#define PULSE_TIME 100
|
||||
|
||||
static int marks [MAX_PINS] ;
|
||||
static int range [MAX_PINS] ;
|
||||
static pthread_t threads [MAX_PINS] ;
|
||||
|
||||
int newPin = -1 ;
|
||||
|
||||
|
||||
/*
|
||||
* softPwmThread:
|
||||
* Thread to do the actual PWM output
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static PI_THREAD (softPwmThread)
|
||||
{
|
||||
int pin, mark, space ;
|
||||
struct sched_param param ;
|
||||
|
||||
param.sched_priority = sched_get_priority_max (SCHED_RR) ;
|
||||
pthread_setschedparam (pthread_self (), SCHED_RR, ¶m) ;
|
||||
|
||||
pin = newPin ;
|
||||
newPin = -1 ;
|
||||
|
||||
piHiPri (90) ;
|
||||
|
||||
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 &= (MAX_PINS - 1) ;
|
||||
|
||||
/**/ if (value < 0)
|
||||
value = 0 ;
|
||||
else if (value > range [pin])
|
||||
value = range [pin] ;
|
||||
|
||||
marks [pin] = value ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softPwmCreate:
|
||||
* Create a new softPWM thread.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int softPwmCreate (int pin, int initialValue, int pwmRange)
|
||||
{
|
||||
int res ;
|
||||
pthread_t myThread ;
|
||||
|
||||
if (range [pin] != 0) // Already running on this pin
|
||||
return -1 ;
|
||||
|
||||
if (range <= 0)
|
||||
return -1 ;
|
||||
|
||||
pinMode (pin, OUTPUT) ;
|
||||
digitalWrite (pin, LOW) ;
|
||||
|
||||
marks [pin] = initialValue ;
|
||||
range [pin] = pwmRange ;
|
||||
|
||||
newPin = pin ;
|
||||
res = pthread_create (&myThread, NULL, softPwmThread, NULL) ;
|
||||
|
||||
while (newPin != -1)
|
||||
delay (1) ;
|
||||
|
||||
threads [pin] = myThread ;
|
||||
|
||||
return res ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softPwmStop:
|
||||
* Stop an existing softPWM thread
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void softPwmStop (int pin)
|
||||
{
|
||||
if (range [pin] != 0)
|
||||
{
|
||||
pthread_cancel (threads [pin]) ;
|
||||
pthread_join (threads [pin], NULL) ;
|
||||
range [pin] = 0 ;
|
||||
digitalWrite (pin, LOW) ;
|
||||
}
|
||||
}
|
35
wiringPi/softPwm.h
Normal file
35
wiringPi/softPwm.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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) ;
|
||||
extern void softPwmStop (int pin) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
211
wiringPi/softServo.c
Normal file
211
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/softServo.h
Normal file
35
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
|
150
wiringPi/softTone.c
Normal file
150
wiringPi/softTone.c
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* 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 pthread_t threads [MAX_PINS] ;
|
||||
|
||||
static int newPin = -1 ;
|
||||
|
||||
|
||||
/*
|
||||
* softToneThread:
|
||||
* Thread to do the actual PWM output
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static PI_THREAD (softToneThread)
|
||||
{
|
||||
int pin, freq, halfPeriod ;
|
||||
struct sched_param param ;
|
||||
|
||||
param.sched_priority = sched_get_priority_max (SCHED_RR) ;
|
||||
pthread_setschedparam (pthread_self (), SCHED_RR, ¶m) ;
|
||||
|
||||
pin = newPin ;
|
||||
newPin = -1 ;
|
||||
|
||||
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 ;
|
||||
pthread_t myThread ;
|
||||
|
||||
pinMode (pin, OUTPUT) ;
|
||||
digitalWrite (pin, LOW) ;
|
||||
|
||||
if (threads [pin] != 0)
|
||||
return -1 ;
|
||||
|
||||
freqs [pin] = 0 ;
|
||||
|
||||
newPin = pin ;
|
||||
res = pthread_create (&myThread, NULL, softToneThread, NULL) ;
|
||||
|
||||
while (newPin != -1)
|
||||
delay (1) ;
|
||||
|
||||
threads [pin] = myThread ;
|
||||
|
||||
return res ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* softToneStop:
|
||||
* Stop an existing softTone thread
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void softToneStop (int pin)
|
||||
{
|
||||
if (threads [pin] != 0)
|
||||
{
|
||||
pthread_cancel (threads [pin]) ;
|
||||
pthread_join (threads [pin], NULL) ;
|
||||
threads [pin] = 0 ;
|
||||
digitalWrite (pin, LOW) ;
|
||||
}
|
||||
}
|
39
wiringPi/softTone.h
Normal file
39
wiringPi/softTone.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 softToneStop (int pin) ;
|
||||
extern void softToneWrite (int pin, int freq) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
109
wiringPi/sr595.c
Normal file
109
wiringPi/sr595.c
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* sr595.c:
|
||||
* Extend wiringPi with the 74x595 shift register as a GPIO
|
||||
* expander chip.
|
||||
* Note that the code can cope with a number of 595's
|
||||
* daisy-chained together - up to 4 for now as we're storing
|
||||
* the output "register" in a single unsigned int.
|
||||
*
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
|
||||
#include "sr595.h"
|
||||
|
||||
|
||||
/*
|
||||
* myDigitalWrite:
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
unsigned int mask ;
|
||||
int dataPin, clockPin, latchPin ;
|
||||
int bit, bits, output ;
|
||||
|
||||
pin -= node->pinBase ; // Normalise pin number
|
||||
bits = node->pinMax - node->pinBase + 1 ; // ie. number of clock pulses
|
||||
dataPin = node->data0 ;
|
||||
clockPin = node->data1 ;
|
||||
latchPin = node->data2 ;
|
||||
output = node->data3 ;
|
||||
|
||||
mask = 1 << pin ;
|
||||
|
||||
if (value == LOW)
|
||||
output &= (~mask) ;
|
||||
else
|
||||
output |= mask ;
|
||||
|
||||
node->data3 = output ;
|
||||
|
||||
// A low -> high latch transition copies the latch to the output pins
|
||||
|
||||
digitalWrite (latchPin, LOW) ; delayMicroseconds (1) ;
|
||||
for (bit = bits - 1 ; bit >= 0 ; --bit)
|
||||
{
|
||||
digitalWrite (dataPin, output & (1 << bit)) ;
|
||||
|
||||
digitalWrite (clockPin, HIGH) ; delayMicroseconds (1) ;
|
||||
digitalWrite (clockPin, LOW) ; delayMicroseconds (1) ;
|
||||
}
|
||||
digitalWrite (latchPin, HIGH) ; delayMicroseconds (1) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* sr595Setup:
|
||||
* Create a new instance of a 74x595 shift register GPIO expander.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int sr595Setup (const int pinBase, const int numPins,
|
||||
const int dataPin, const int clockPin, const int latchPin)
|
||||
{
|
||||
struct wiringPiNodeStruct *node ;
|
||||
|
||||
node = wiringPiNewNode (pinBase, numPins) ;
|
||||
|
||||
node->data0 = dataPin ;
|
||||
node->data1 = clockPin ;
|
||||
node->data2 = latchPin ;
|
||||
node->data3 = 0 ; // Output register
|
||||
node->digitalWrite = myDigitalWrite ;
|
||||
|
||||
// Initialise the underlying hardware
|
||||
|
||||
digitalWrite (dataPin, LOW) ;
|
||||
digitalWrite (clockPin, LOW) ;
|
||||
digitalWrite (latchPin, HIGH) ;
|
||||
|
||||
pinMode (dataPin, OUTPUT) ;
|
||||
pinMode (clockPin, OUTPUT) ;
|
||||
pinMode (latchPin, OUTPUT) ;
|
||||
|
||||
return 0 ;
|
||||
}
|
34
wiringPi/sr595.h
Normal file
34
wiringPi/sr595.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* sr595.h:
|
||||
* Extend wiringPi with the 74x595 shift registers.
|
||||
* Copyright (c) 2013 Gordon Henderson
|
||||
***********************************************************************
|
||||
* This file is part of wiringPi:
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
*
|
||||
* wiringPi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* wiringPi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with wiringPi.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int sr595Setup (const int pinBase, const int numPins,
|
||||
const int dataPin, const int clockPin, const int latchPin) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
2145
wiringPi/wiringPi.c
Normal file
2145
wiringPi/wiringPi.c
Normal file
File diff suppressed because it is too large
Load Diff
225
wiringPi/wiringPi.h
Normal file
225
wiringPi/wiringPi.h
Normal file
@@ -0,0 +1,225 @@
|
||||
/*
|
||||
* 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/>.
|
||||
***********************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __WIRING_PI_H__
|
||||
#define __WIRING_PI_H__
|
||||
|
||||
// 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 SOFT_PWM_OUTPUT 4
|
||||
#define SOFT_TONE_OUTPUT 5
|
||||
#define PWM_TONE_OUTPUT 6
|
||||
|
||||
|
||||
#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
|
||||
|
||||
// Pi model types and version numbers
|
||||
// Intended for the GPIO program Use at your own risk.
|
||||
|
||||
#define PI_MODEL_M1 0
|
||||
|
||||
#define PI_VERSION_UNKNOWN 0
|
||||
#define PI_VERSION_1 1
|
||||
#define PI_VERSION_1_1 2
|
||||
#define PI_VERSION_1_2 3
|
||||
#define PI_VERSION_2 4
|
||||
|
||||
#define PI_MAKER_UNKNOWN 0
|
||||
#define PI_MAKER_EGOMAN 1
|
||||
#define PI_MAKER_SONY 2
|
||||
#define PI_MAKER_QISDA 3
|
||||
#define PI_MAKER_LEMAKER 4 //add for BananaPro by LeMaker team
|
||||
|
||||
#define BPRVER 3 //add for BananaPro by lemaker team
|
||||
|
||||
extern const char *piModelNames [6] ;
|
||||
extern const char *piRevisionNames [5] ;
|
||||
extern const char *piMakerNames [5] ;
|
||||
|
||||
|
||||
// Intended for the GPIO program Use at your own risk.
|
||||
|
||||
// Threads
|
||||
|
||||
#define PI_THREAD(X) void *X (void *dummy)
|
||||
|
||||
// Failure modes
|
||||
|
||||
#define WPI_FATAL (1==1)
|
||||
#define WPI_ALMOST (1==2)
|
||||
|
||||
|
||||
// wiringPiNodeStruct:
|
||||
// This describes additional device nodes in the extended wiringPi
|
||||
// 2.0 scheme of things.
|
||||
// It's a simple linked list for now, but will hopefully migrate to
|
||||
// a binary tree for efficiency reasons - but then again, the chances
|
||||
// of more than 1 or 2 devices being added are fairly slim, so who
|
||||
// knows....
|
||||
|
||||
struct wiringPiNodeStruct
|
||||
{
|
||||
int pinBase ;
|
||||
int pinMax ;
|
||||
|
||||
int fd ; // Node specific
|
||||
unsigned int data0 ; // ditto
|
||||
unsigned int data1 ; // ditto
|
||||
unsigned int data2 ; // ditto
|
||||
unsigned int data3 ; // ditto
|
||||
|
||||
void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ;
|
||||
void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ;
|
||||
int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ;
|
||||
void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
|
||||
void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
|
||||
int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ;
|
||||
void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
|
||||
|
||||
struct wiringPiNodeStruct *next ;
|
||||
} ;
|
||||
|
||||
extern struct wiringPiNodeStruct *wiringPiNodes ;
|
||||
|
||||
|
||||
// Function prototypes
|
||||
// c++ wrappers thanks to a comment by Nick Lott
|
||||
// (and others on the Raspberry Pi forums)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Data
|
||||
|
||||
//extern const char *piModelNames [] ;
|
||||
//extern const char *piRevisionNames[] ;
|
||||
|
||||
// Internal
|
||||
|
||||
extern int wiringPiFailure (int fatal, const char *message, ...) ;
|
||||
|
||||
// Core wiringPi functions
|
||||
|
||||
extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ;
|
||||
extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
|
||||
|
||||
extern int wiringPiSetup (void) ;
|
||||
extern int wiringPiSetupSys (void) ;
|
||||
extern int wiringPiSetupGpio (void) ;
|
||||
extern int wiringPiSetupPhys (void) ;
|
||||
|
||||
extern void pinModeAlt (int pin, int mode) ;
|
||||
extern void pinMode (int pin, int mode) ;
|
||||
extern void pullUpDnControl (int pin, int pud) ;
|
||||
extern int digitalRead (int pin) ;
|
||||
extern void digitalWrite (int pin, int value) ;
|
||||
extern void pwmWrite (int pin, int value) ;
|
||||
extern int analogRead (int pin) ;
|
||||
extern void analogWrite (int pin, int value) ;
|
||||
|
||||
// PiFace specifics
|
||||
// (Deprecated)
|
||||
|
||||
extern int wiringPiSetupPiFace (void) ;
|
||||
extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only
|
||||
|
||||
// On-Board Raspberry Pi hardware specific stuff
|
||||
|
||||
extern int piBoardRev (void) ;
|
||||
extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ;
|
||||
extern int wpiPinToGpio (int wpiPin) ;
|
||||
extern int physPinToGpio (int physPin) ;
|
||||
extern int physPinToPin (int physPin); //add by LeMaker team for Bananapi
|
||||
extern void setPadDrive (int group, int value) ;
|
||||
extern int getAlt (int pin) ;
|
||||
extern void pwmToneWrite (int pin, int freq) ;
|
||||
extern void digitalWriteByte (int value) ;
|
||||
extern void pwmSetMode (int mode) ;
|
||||
extern void pwmSetRange (unsigned int range) ;
|
||||
extern void pwmSetClock (int divisor) ;
|
||||
extern void gpioClockSet (int pin, int freq) ;
|
||||
|
||||
// Interrupts
|
||||
// (Also Pi hardware specific)
|
||||
|
||||
extern int waitForInterrupt (int pin, int mS) ;
|
||||
extern int 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 (const 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
|
||||
|
||||
#endif
|
236
wiringPi/wiringPiI2C.c
Normal file
236
wiringPi/wiringPiI2C.c
Normal file
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* 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 <errno.h>
|
||||
#include <string.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.word & 0xFFFF ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wiringPiI2CSetupInterface:
|
||||
* Undocumented access to set the interface explicitly - might be used
|
||||
* for the Pi's 2nd I2C interface...
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiI2CSetupInterface (const char *device, int devId)
|
||||
{
|
||||
int fd ;
|
||||
|
||||
if ((fd = open (device, O_RDWR)) < 0)
|
||||
return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ;
|
||||
|
||||
if (ioctl (fd, I2C_SLAVE, devId) < 0)
|
||||
return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ;
|
||||
|
||||
return fd ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wiringPiI2CSetup:
|
||||
* Open the I2C device, and regsiter the target device
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int wiringPiI2CSetup (const int devId)
|
||||
{
|
||||
int rev ;
|
||||
const char *device ;
|
||||
|
||||
rev = piBoardRev () ;
|
||||
/*modify for BananaPro by LeMaker team*/
|
||||
if (rev == 1)
|
||||
device = "/dev/i2c-0" ;
|
||||
else if (rev == 2)
|
||||
device = "/dev/i2c-1" ;
|
||||
else if (rev == 3)
|
||||
device = "/dev/i2c-0"; // guenter fuer orange pi device = "/dev/i2c-2";
|
||||
else
|
||||
device = "/dev/i2c-3" ;
|
||||
|
||||
return wiringPiI2CSetupInterface (device, devId) ;
|
||||
}
|
42
wiringPi/wiringPiI2C.h
Normal file
42
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 (const char *device, int devId) ;
|
||||
extern int wiringPiI2CSetup (const int devId) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
121
wiringPi/wiringPiSPI.c
Normal file
121
wiringPi/wiringPiSPI.c
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* 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 <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/spi/spidev.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
|
||||
#include "wiringPiSPI.h"
|
||||
|
||||
|
||||
// The SPI bus parameters
|
||||
// Variables as they need to be passed as pointers later on
|
||||
|
||||
const static char *spiDev0 = "/dev/spidev0.0" ;
|
||||
const static char *spiDev1 = "/dev/spidev0.1" ;
|
||||
const static uint8_t spiMode = 0 ;
|
||||
const static uint8_t spiBPW = 8 ;
|
||||
const static uint16_t spiDelay = 0 ;
|
||||
|
||||
static uint32_t spiSpeeds [2] ;
|
||||
static int spiFds [2] ;
|
||||
|
||||
|
||||
/*
|
||||
* 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 wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
|
||||
|
||||
spiSpeeds [channel] = speed ;
|
||||
spiFds [channel] = fd ;
|
||||
|
||||
// Set SPI parameters.
|
||||
// Why are we reading it afterwriting it? I've no idea, but for now I'm blindly
|
||||
// copying example code I've seen online...
|
||||
|
||||
if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0)
|
||||
return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ;
|
||||
|
||||
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
|
||||
return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ;
|
||||
|
||||
if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
|
||||
return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ;
|
||||
|
||||
return fd ;
|
||||
}
|
35
wiringPi/wiringPiSPI.h
Normal file
35
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/wiringSerial.c
Normal file
213
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 (const char *device, const 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 (const int fd)
|
||||
{
|
||||
tcflush (fd, TCIOFLUSH) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialClose:
|
||||
* Release the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialClose (const int fd)
|
||||
{
|
||||
close (fd) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialPutchar:
|
||||
* Send a single character to the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPutchar (const int fd, const unsigned char c)
|
||||
{
|
||||
write (fd, &c, 1) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialPuts:
|
||||
* Send a string to the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPuts (const int fd, const char *s)
|
||||
{
|
||||
write (fd, s, strlen (s)) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* serialPrintf:
|
||||
* Printf over Serial
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPrintf (const int fd, const 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 (const 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 (const int fd)
|
||||
{
|
||||
uint8_t x ;
|
||||
|
||||
if (read (fd, &x, 1) != 1)
|
||||
return -1 ;
|
||||
|
||||
return ((int)x) & 0xFF ;
|
||||
}
|
38
wiringPi/wiringSerial.h
Normal file
38
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 (const char *device, const int baud) ;
|
||||
extern void serialClose (const int fd) ;
|
||||
extern void serialFlush (const int fd) ;
|
||||
extern void serialPutchar (const int fd, const unsigned char c) ;
|
||||
extern void serialPuts (const int fd, const char *s) ;
|
||||
extern void serialPrintf (const int fd, const char *message, ...) ;
|
||||
extern int serialDataAvail (const int fd) ;
|
||||
extern int serialGetchar (const int fd) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
83
wiringPi/wiringShift.c
Normal file
83
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/wiringShift.h
Normal file
41
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