-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts4231.cpp
329 lines (302 loc) · 9.55 KB
/
ts4231.cpp
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*******************************************************************
Copyright (C) 2017 Triad Semiconductor
ts4231.h - Library for configuring the Triad Semiconductor TS4231 Light
to Digital converter.
Created by: John Seibel
*******************************************************************/
#include "ts4231.h"
#include <Arduino.h>
//IMPORTANT NOTES:
//1) If porting the TS4231 library code to a non-Arduino architecture,
// be sure that the INPUT ports assigned to the E and D signals are configured as
// floating inputs with NO pull-up or pull-down function. Using a pull-up or
// pull-down function on the inputs will cause the TS4231 to operate incorrectly.
//2) The TS4231 library omits delays between E and D signal transitions when going
// from S3_STATE to WATCH_STATE or SLEEP_STATE to WATCH_STATE in function
// goToWatch() for the purpose of transitioning into WATCH_STATE as quickly as
// possible. If a microcontroller is being used that can change states on
// the E and D outputs faster than approximately 100ns, the TS4231 datasheet
// must be consulted to verify timing parameters are not being violated to
// assure proper TS4231 operation. A suitable solution would be to include
// a short delay in function ts_digitalWrite() to allow enough time between
// output pin signal changes to meet the TS4231 timing parameters as stated
// in the datasheet. See the ts_digitalWrite() function for more information.
TS4231::TS4231(int device_E_pin, int device_D_pin) {
configured = false;
E_pin = device_E_pin;
D_pin = device_D_pin;
ts_pinMode(E_pin, INPUT);
ts_pinMode(D_pin, INPUT);
}
void TS4231::ts_delayUs(unsigned int delay_val) {
delayMicroseconds(delay_val);
}
void TS4231::ts_pinMode(int pin, uint8_t mode) {
pinMode(pin, mode);
}
uint8_t TS4231::ts_digitalRead(int pin) {
uint8_t read_val;
read_val = digitalRead(pin);
return read_val;
}
void TS4231::ts_digitalWrite(int pin, uint8_t write_val) {
digitalWrite(pin, write_val);
//A short delay function can be inserted here to extend the time between writes to
//the E and D outputs if TS4231 timing parameters are being violated. Consult
//the TS4231 datasheet for more information on timing parameters. It is recommended
//that any added delay be no longer than approximately 1us.
}
unsigned long TS4231::ts_millis() {
unsigned long current_time;
current_time = millis();
return current_time;
}
//Function waitForLight() should be executed after power-up and prior to
//configuring the device. Upon power-up, D is a 0 and will output a 1
//when light is detected. D will return to 0 at the end of light detection.
//This funciton looks for the falling edge of D to indicate that the end of
//light detection has occurred.
bool TS4231::waitForLight(uint16_t light_timeout) {
bool light = false;
bool exit = false;
unsigned long time0;
if (checkBus() == S0_STATE) {
time0 = ts_millis();
while (exit == false) {
if (ts_digitalRead(D_pin) > 0) {
while (exit == false) {
if (ts_digitalRead(D_pin) == 0) {
exit = true;
light = true;
}
else if (ts_millis() > (time0 + light_timeout)) {
exit = true;
light = false;
}
else {
exit = false;
light = false;
}
}
}
else if (ts_millis() > (time0 + light_timeout)) {
exit = true;
light = false;
}
else {
exit = false;
light = false;
}
}
}
else light = true; //if not in state S0_state, light has already been detected
return light;
}
bool TS4231::goToSleep(void) {
bool sleep_success;
if (configured == false) sleep_success = false;
else {
switch (checkBus()) {
case S0_STATE:
sleep_success = false;
break;
case SLEEP_STATE:
sleep_success = true;
break;
case WATCH_STATE:
ts_digitalWrite(E_pin, LOW);
ts_pinMode(E_pin, OUTPUT);
ts_delayUs(BUS_DRV_DLY);
ts_pinMode(E_pin, INPUT);
ts_delayUs(BUS_DRV_DLY);
if (checkBus() == SLEEP_STATE) sleep_success = true;
else sleep_success = false;
break;
case S3_STATE:
sleep_success = false;
break;
default:
sleep_success = false;
break;
}
}
return sleep_success;
}
uint8_t TS4231::configDevice(uint16_t config_val) {
uint8_t config_success = 0x00;
uint16_t readback;
configured = false;
ts_pinMode(D_pin, INPUT);
ts_pinMode(E_pin, INPUT);
ts_digitalWrite(D_pin, LOW);
ts_digitalWrite(E_pin, LOW);
ts_pinMode(E_pin, OUTPUT);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_pinMode(D_pin, OUTPUT);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(D_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_pinMode(E_pin, INPUT);
ts_pinMode(D_pin, INPUT);
if (checkBus() == S3_STATE) {
writeConfig(config_val);
readback = readConfig();
if (readback == config_val) {
configured = true;
if (goToWatch()) config_success = CONFIG_PASS;
else config_success = WATCH_FAIL;
}
else config_success = VERIFY_FAIL;
}
else config_success = BUS_FAIL;
return config_success;
}
void TS4231::writeConfig(uint16_t config_val) {
ts_digitalWrite(E_pin, HIGH);
ts_digitalWrite(D_pin, HIGH);
ts_pinMode(E_pin, OUTPUT);
ts_pinMode(D_pin, OUTPUT);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(D_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
for (uint8_t i = 0; i < 15; i++) {
config_val = config_val << 1;
if ((config_val & 0x8000) > 0) ts_digitalWrite(D_pin, HIGH);
else ts_digitalWrite(D_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
}
ts_digitalWrite(D_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(D_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_pinMode(E_pin, INPUT);
ts_pinMode(D_pin, INPUT);
}
uint16_t TS4231::readConfig(void) {
uint16_t readback;
readback = 0x0000;
ts_digitalWrite(E_pin, HIGH);
ts_digitalWrite(D_pin, HIGH);
ts_pinMode(E_pin, OUTPUT);
ts_pinMode(D_pin, OUTPUT);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(D_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(D_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_pinMode(D_pin, INPUT);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
for (uint8_t i = 0; i < 14; i++) {
ts_digitalWrite(E_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
readback = (readback << 1) | (ts_digitalRead(D_pin) & 0x0001);
ts_digitalWrite(E_pin, LOW);
ts_delayUs(BUS_DRV_DLY);
}
ts_digitalWrite(D_pin, LOW);
ts_pinMode(D_pin, OUTPUT);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(E_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_digitalWrite(D_pin, HIGH);
ts_delayUs(BUS_DRV_DLY);
ts_pinMode(E_pin, INPUT);
ts_pinMode(D_pin, INPUT);
return readback;
}
//checkBus() performs a voting function where the bus is sampled 3 times
//to find 2 identical results. This is necessary since light detection is
//asynchronous and can indicate a false state.
uint8_t TS4231::checkBus(void) {
uint8_t state;
uint8_t E_state;
uint8_t D_state;
uint8_t S0_count = 0;
uint8_t SLEEP_count = 0;
uint8_t WATCH_count = 0;
uint8_t S3_count = 0;
for (uint8_t i=0; i<3; i++) {
E_state = ts_digitalRead(E_pin);
D_state = ts_digitalRead(D_pin);
if (D_state == HIGH) {
if (E_state == HIGH) S3_count++;
else SLEEP_count++;
}
else {
if (E_state == HIGH) WATCH_count++;
else S0_count++;
}
ts_delayUs(BUS_CHECK_DLY);
}
if (SLEEP_count >= 2) state = SLEEP_STATE;
else if (WATCH_count >= 2) state = WATCH_STATE;
else if (S3_count >= 2) state = S3_STATE;
else if (S0_count >= 2) state = S0_STATE;
else state = UNKNOWN_STATE;
return state;
}
bool TS4231::goToWatch(void) {
bool watch_success;
if (configured == false) watch_success = false;
else {
switch (checkBus()) {
case S0_STATE:
watch_success = false;
break;
case SLEEP_STATE:
ts_digitalWrite(D_pin, HIGH);
ts_pinMode(D_pin, OUTPUT);
ts_digitalWrite(E_pin, LOW);
ts_pinMode(E_pin, OUTPUT);
ts_digitalWrite(D_pin, LOW);
ts_pinMode(D_pin, INPUT);
ts_digitalWrite(E_pin, HIGH);
ts_pinMode(E_pin, INPUT);
ts_delayUs(SLEEP_RECOVERY);
if (checkBus() == WATCH_STATE) watch_success = true;
else watch_success = false;
break;
case WATCH_STATE:
watch_success = true;
break;
case S3_STATE:
ts_digitalWrite(E_pin, HIGH);
ts_pinMode(E_pin, OUTPUT);
ts_digitalWrite(D_pin, HIGH);
ts_pinMode(D_pin, OUTPUT);
ts_digitalWrite(E_pin, LOW);
ts_digitalWrite(D_pin, LOW);
ts_pinMode(D_pin, INPUT);
ts_digitalWrite(E_pin, HIGH);
ts_pinMode(E_pin, INPUT);
ts_delayUs(SLEEP_RECOVERY);
if (checkBus() == WATCH_STATE) watch_success = true;
else watch_success = false;
break;
default:
watch_success = false;
break;
}
}
return watch_success;
}