mcp23s08.c
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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 ;
}