-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSC18IS602.cpp
More file actions
278 lines (207 loc) · 4.73 KB
/
SC18IS602.cpp
File metadata and controls
278 lines (207 loc) · 4.73 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Description:
This is a example code for Sandbox Electronics' I2C/SPI to UART bridge module.
You can get one of those products on
http://sandboxelectronics.com
Version:
V0.1
Release Date:
2014-02-16
Author:
Tiequan Shao info@sandboxelectronics.com
Lisence:
CC BY-NC-SA 3.0
Please keep the above information when you use this code in your project.
*/
#include <Wire.h>
#include <SC18IS602.h>
#include <SPI.h>
#ifdef __AVR__
#define WIRE Wire
#else // Arduino Due
#define WIRE Wire1
#endif
SC18IS602::SC18IS602(uint8_t addr)
{
device_address = (addr>>1);
}
void SC18IS602::ResetDevice(void)
{
reg_f0_config = 0x00; //configuration register 0xF0
reg_f4_gpio_w = 0x00; //gpio write register
reg_f5_gpio_r = 0x0F; //gpio read register
reg_f6_gpio_e = 0x00; //gpio enable register
reg_f7_gpio_d = 0xAA; //gpio direction
WriteRegister(0xf0,reg_f0_config);
WriteRegister(0xf4,reg_f4_gpio_w);
// WriteRegister(reg_f5_gpio_r,0x0F);
WriteRegister(0xf6,reg_f6_gpio_e);
WriteRegister(0xf7,reg_f7_gpio_d);
pinMode(0,INPUT);
pinMode(1,INPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
GPIOEnable(0);
GPIOEnable(1);
GPIOEnable(2);
GPIOEnable(3);
}
void SC18IS602::GPIOEnable(uint8_t pin)
{
reg_f6_gpio_e |= ( 0x01<<pin );
WriteRegister(0xf6,reg_f6_gpio_e);
}
void SC18IS602::SSEnable(uint8_t pin)
{
reg_f6_gpio_e &= (uint8_t)(~(0x01<<pin ));
WriteRegister(0xf6,reg_f6_gpio_e);
}
void SC18IS602::begin(void)
{
WIRE.begin();
ResetDevice();
ss_pin = SC18IS602_SS_0;
}
void SC18IS602::begin(uint8_t pin)
{
WIRE.begin();
ResetDevice();
ss_pin = SC18IS602_SS_0;
SSEnable(pin);
ss_pin = pin;
}
uint8_t SC18IS602::transfer(uint8_t value)
{
uint8_t result = 0;
WriteBytes(&value,1);
ReadBytes(&result,1);
return result;
}
uint8_t SC18IS602::transfer(uint8_t* buffer, uint8_t length)
{
WriteBytes(buffer,length);
return (ReadBytes(buffer,length));
}
void SC18IS602::pinMode(uint8_t pin, uint8_t i_o)
{
reg_f7_gpio_d &= (((uint8_t)~(0x03))<<(pin<1)); //clear the two control bit of the pin
if (i_o == INPUT) {
reg_f7_gpio_d |= (0x02<<(pin<<1)); //10 for input
} else {
reg_f7_gpio_d |= (0x01<<(pin<<1)); //01 for output
}
WriteRegister(0xF7, reg_f7_gpio_d);
}
void SC18IS602::digitalWrite(uint8_t pin, uint8_t value)
{
if ( value==0 ) {
reg_f4_gpio_w &= (((uint8_t)~(0x01))<<pin);
} else {
reg_f4_gpio_w |= (0x01<<pin);
}
WriteRegister(0xF4, reg_f4_gpio_w);
}
uint8_t SC18IS602::digitalRead(uint8_t pin)
{
WriteRegister(0xF5, reg_f5_gpio_r);
reg_f5_gpio_r = ReadByte();
if (reg_f5_gpio_r & (0x01<<pin) ) {
return 1;
}
return 0;
}
void SC18IS602::WriteBytes(uint8_t* buffer, uint8_t length)
{
uint8_t i;
do {
WIRE.beginTransmission(device_address);
WIRE.write(0x01<<ss_pin);
for (i=0;i<length;i++) {
WIRE.write(buffer[i]);
}
} while ( WIRE.endTransmission(1) == 2) ; //keep trying if the device does not ack on it's device address
//WIRE.endTransmission();
delay(10);
return;
}
uint8_t SC18IS602::ReadBytes(uint8_t* buffer, uint8_t length)
{
uint8_t i;
uint8_t count;
//Serial.println("*******readING*********");
while (WIRE.requestFrom(device_address,length) == 0);
//Serial.println("*******read DONE*********");
count = WIRE.available();
for(i=0;i<count;i++) {
buffer[i] = WIRE.read();
}
//Serial.println("*******read count*********");
//Serial.println(count,DEC);
return count;
}
uint8_t SC18IS602::ReadByte(void)
{
while (WIRE.requestFrom(device_address,(uint8_t)1) == 0);
return WIRE.read();
}
void SC18IS602::WriteRegister(uint8_t reg_addr, uint8_t val)
{
do {
WIRE.beginTransmission(device_address);
WIRE.write(reg_addr);
WIRE.write(val);
} while ( WIRE.endTransmission(1) == 2) ; //keep trying if the device does not ack on it's device address
//WIRE.endTransmission(true);
delay(10);
return ;
}
void SC18IS602::setBitOrder(uint8_t order)
{
reg_f0_config &= 0xDF;
if ( order == LSBFIRST) {
reg_f0_config |= 0x20;
} else {
reg_f0_config &= 0xEF;
}
WriteRegister(0xF0, reg_f0_config);
return;
}
//LSBFIRST MSBFIRST
void SC18IS602::setClockDivider(uint8_t divider)
{
reg_f0_config &= 0xFC;
reg_f0_config |= divider;
WriteRegister(0xF0, reg_f0_config);
return;
}
void SC18IS602::setDataMode(uint8_t mode)
{
reg_f0_config &= 0xF3;
switch (mode) {
case SC18IS602_SPI_MODE0:
break;
case SC18IS602_SPI_MODE1:
reg_f0_config |=0x04;
break;
case SC18IS602_SPI_MODE2:
reg_f0_config |=0x08;
break;
case SC18IS602_SPI_MODE3:
reg_f0_config |=0x0C;
break;
default:
break;
}
WriteRegister(0xF0, reg_f0_config);
return;
}
void SC18IS602::InterruptClear(void)
{
uint8_t isr_clear = 0xf1;
WriteBytes(&isr_clear, 1);
return;
}
void SC18IS602::end(void)
{
return;
}