3 * Arduino compatable (ish) Wiring library for the Raspberry Pi
4 * Copyright (c) 2012-2013 Gordon Henderson
6 * This file to interface with the PiFace peripheral device which
7 * has an MCP23S17 GPIO device connected via the SPI bus.
8 ***********************************************************************
9 * This file is part of wiringPi:
10 * https://projects.drogon.net/raspberry-pi/wiringpi/
12 * wiringPi is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
17 * wiringPi is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with wiringPi.
24 * If not, see <http://www.gnu.org/licenses/>.
25 ***********************************************************************
33 #include <wiringPiSPI.h>
35 #include "../wiringPi/mcp23x0817.h"
39 #define PIFACE_SPEED 4000000
40 #define PIFACE_DEVNO 0
46 * Write a byte to a register on the MCP23S17 on the SPI bus.
47 *********************************************************************************
50 static void writeByte (uint8_t reg, uint8_t data)
54 spiData [0] = CMD_WRITE ;
58 wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
63 * Read a byte from a register on the MCP23S17 on the SPI bus.
64 *********************************************************************************
67 static uint8_t readByte (uint8_t reg)
71 spiData [0] = CMD_READ ;
74 wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
82 * Perform the digitalWrite function on the PiFace board
83 *********************************************************************************
86 void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
90 pin -= node->pinBase ;
92 old = readByte (MCP23x17_GPIOA) ;
99 writeByte (MCP23x17_GPIOA, old) ;
105 * Perform the digitalRead function on the PiFace board
106 *********************************************************************************
109 int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
113 mask = 1 << ((pin - node->pinBase) & 7) ;
116 reg = MCP23x17_GPIOB ; // Input regsiter
118 reg = MCP23x17_OLATA ; // Output latch regsiter
120 if ((readByte (reg) & mask) != 0)
129 * Perform the pullUpDnControl function on the PiFace board
130 *********************************************************************************
133 void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int pud)
137 mask = 1 << (pin - node->pinBase) ;
138 old = readByte (MCP23x17_GPPUB) ;
145 writeByte (MCP23x17_GPPUB, old) ;
151 * Setup the SPI interface and initialise the MCP23S17 chip
152 * We create one node with 16 pins - each if the first 8 pins being read
153 * and write - although the operations actually go to different
154 * hardware ports. The top 8 let you read the state of the output register.
155 *********************************************************************************
158 int piFaceSetup (const int pinBase)
161 struct wiringPiNodeStruct *node ;
163 if ((x = wiringPiSPISetup (PIFACE_DEVNO, PIFACE_SPEED)) < 0)
166 // Setup the MCP23S17
168 writeByte (MCP23x17_IOCON, IOCON_INIT) ;
169 writeByte (MCP23x17_IODIRA, 0x00) ; // Port A -> Outputs
170 writeByte (MCP23x17_IODIRB, 0xFF) ; // Port B -> Inputs
172 node = wiringPiNewNode (pinBase, 16) ;
173 node->digitalRead = myDigitalRead ;
174 node->digitalWrite = myDigitalWrite ;
175 node->pullUpDnControl = myPullUpDnControl ;