chiark / gitweb /
Added in a max5322 SPI D to A chip
[wiringPi.git] / devLib / piFaceOld.c
1 /*
2  * piFace.:
3  *      Arduino compatable (ish) Wiring library for the Raspberry Pi
4  *      Copyright (c) 2012-2013 Gordon Henderson
5  *
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/
11  *
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.
16  *
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.
21  *
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  ***********************************************************************
26  */
27
28
29 #include <stdio.h>
30 #include <stdint.h>
31
32 #include <wiringPi.h>
33 #include <wiringPiSPI.h>
34
35 #include "../wiringPi/mcp23x0817.h"
36
37 #include "piFace.h"
38
39 #define PIFACE_SPEED    4000000
40 #define PIFACE_DEVNO    0
41
42
43
44 /*
45  * writeByte:
46  *      Write a byte to a register on the MCP23S17 on the SPI bus.
47  *********************************************************************************
48  */
49
50 static void writeByte (uint8_t reg, uint8_t data)
51 {
52   uint8_t spiData [4] ;
53
54   spiData [0] = CMD_WRITE ;
55   spiData [1] = reg ;
56   spiData [2] = data ;
57
58   wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
59 }
60
61 /*
62  * readByte:
63  *      Read a byte from a register on the MCP23S17 on the SPI bus.
64  *********************************************************************************
65  */
66
67 static uint8_t readByte (uint8_t reg)
68 {
69   uint8_t spiData [4] ;
70
71   spiData [0] = CMD_READ ;
72   spiData [1] = reg ;
73
74   wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
75
76   return spiData [2] ;
77 }
78
79
80 /*
81  * myDigitalWrite:
82  *      Perform the digitalWrite function on the PiFace board
83  *********************************************************************************
84  */
85
86 void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
87 {
88   uint8_t mask, old ;
89
90   pin -= node->pinBase ;
91   mask = 1 << pin ;
92   old  = readByte (MCP23x17_GPIOA) ;
93
94   if (value == 0)
95     old &= (~mask) ;
96   else
97     old |=   mask ;
98
99   writeByte (MCP23x17_GPIOA, old) ;
100 }
101
102
103 /*
104  * myDigitalRead:
105  *      Perform the digitalRead function on the PiFace board
106  *********************************************************************************
107  */
108
109 int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
110 {
111   uint8_t mask, reg ;
112
113   mask = 1 << ((pin - node->pinBase) & 7) ;
114
115   if (pin < 8)
116     reg = MCP23x17_GPIOB ;      // Input regsiter
117   else
118     reg = MCP23x17_OLATA ;      // Output latch regsiter
119
120   if ((readByte (reg) & mask) != 0)
121     return HIGH ;
122   else
123     return LOW ;
124 }
125
126
127 /*
128  * myPullUpDnControl:
129  *      Perform the pullUpDnControl function on the PiFace board
130  *********************************************************************************
131  */
132
133 void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int pud)
134 {
135   uint8_t mask, old ;
136
137   mask = 1 << (pin - node->pinBase) ;
138   old  = readByte (MCP23x17_GPPUB) ;
139
140   if (pud == 0)
141     old &= (~mask) ;
142   else
143     old |=   mask ;
144
145   writeByte (MCP23x17_GPPUB, old) ;
146 }
147
148
149 /*
150  * piFaceSetup
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  *********************************************************************************
156  */
157
158 int piFaceSetup (const int pinBase)
159 {
160   int    x ;
161   struct wiringPiNodeStruct *node ;
162
163   if ((x = wiringPiSPISetup (PIFACE_DEVNO, PIFACE_SPEED)) < 0)
164     return x ;
165
166 // Setup the MCP23S17
167
168   writeByte (MCP23x17_IOCON,  IOCON_INIT) ;
169   writeByte (MCP23x17_IODIRA, 0x00) ;           // Port A -> Outputs
170   writeByte (MCP23x17_IODIRB, 0xFF) ;           // Port B -> Inputs
171
172   node = wiringPiNewNode (pinBase, 16) ;
173   node->digitalRead     = myDigitalRead ;
174   node->digitalWrite    = myDigitalWrite ;
175   node->pullUpDnControl = myPullUpDnControl ;
176
177   return 0 ;
178 }