-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
ioport_lpf2.c
368 lines (293 loc) · 10.2 KB
/
ioport_lpf2.c
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2019 David Lechner
// LEGO Power Functions 2 I/O port
#include <pbdrv/config.h>
#if PBDRV_CONFIG_IOPORT_LPF2
#include <stdbool.h>
#include <stdio.h>
#include <pbdrv/gpio.h>
#include <pbio/error.h>
#include <pbio/iodev.h>
#include "ioport_lpf2.h"
#include "sys/etimer.h"
#include "sys/process.h"
typedef enum _dev_id1_group_t {
DEV_ID1_GROUP_GND,
DEV_ID1_GROUP_VCC,
DEV_ID1_GROUP_PULL_DOWN,
DEV_ID1_GROUP_OPEN,
} dev_id1_group_t;
// Device connection manager state for each port
typedef struct _dcm_data_t {
dev_id1_group_t dev_id1_group;
pbio_iodev_type_id_t type_id;
pbio_iodev_type_id_t prev_type_id;
uint8_t prev_gpio_value;
uint8_t dev_id_match_count;
} dcm_data_t;
typedef struct {
const pbdrv_ioport_lpf2_platform_port_t *pins;
dcm_data_t dcm;
struct pt pt;
pbio_iodev_type_id_t connected_type_id;
pbio_iodev_type_id_t prev_type_id;
} ioport_dev_t;
static const pbio_iodev_type_id_t ioport_type_id_lookup[3][3] = {
[DEV_ID1_GROUP_GND] = {
[0] = PBIO_IODEV_TYPE_ID_LPF2_POWER,
[1] = PBIO_IODEV_TYPE_ID_LPF2_TURN,
[2] = PBIO_IODEV_TYPE_ID_LPF2_LIGHT2,
},
[DEV_ID1_GROUP_VCC] = {
[0] = PBIO_IODEV_TYPE_ID_LPF2_TRAIN,
[1] = PBIO_IODEV_TYPE_ID_LPF2_LMOTOR,
[2] = PBIO_IODEV_TYPE_ID_LPF2_LIGHT1,
},
[DEV_ID1_GROUP_PULL_DOWN] = {
[0] = PBIO_IODEV_TYPE_ID_LPF2_MMOTOR,
[1] = PBIO_IODEV_TYPE_ID_LPF2_XMOTOR,
[2] = PBIO_IODEV_TYPE_ID_LPF2_LIGHT,
},
};
static struct {
pbio_iodev_info_t info;
pbio_iodev_mode_t modes[PBIO_IODEV_MAX_NUM_MODES];
} ioport_info[PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS];
static pbio_iodev_t iodevs[PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS];
PROCESS(pbdrv_ioport_lpf2_process, "I/O port");
static ioport_dev_t ioport_devs[PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS] = {
#if PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS > 0
[0] = {
.pins = &pbdrv_ioport_lpf2_platform_port_0,
},
#endif
#if PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS > 1
[1] = {
.pins = &pbdrv_ioport_lpf2_platform_port_1,
},
#endif
#if PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS > 2
[2] = {
.pins = &pbdrv_ioport_lpf2_platform_port_2,
},
#endif
#if PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS > 3
[3] = {
.pins = &pbdrv_ioport_lpf2_platform_port_3,
},
#endif
#if PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS > 4
[4] = {
.pins = &pbdrv_ioport_lpf2_platform_port_4,
},
#endif
#if PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS > 5
[5] = {
.pins = &pbdrv_ioport_lpf2_platform_port_5,
},
#endif
};
static void ioport_enable_uart(ioport_dev_t *ioport) {
const pbdrv_ioport_lpf2_platform_port_t *pins = ioport->pins;
pbdrv_gpio_alt(&pins->uart_rx, pins->alt);
pbdrv_gpio_alt(&pins->uart_tx, pins->alt);
pbdrv_gpio_out_low(&pins->uart_buf);
}
static void init_one(uint8_t ioport) {
const pbdrv_ioport_lpf2_platform_port_t *pins = ioport_devs[ioport].pins;
PT_INIT(&ioport_devs[ioport].pt);
iodevs[ioport].port = PBDRV_CONFIG_IOPORT_LPF2_FIRST_PORT + ioport;
iodevs[ioport].info = &ioport_info[ioport].info;
pbdrv_gpio_input(&pins->id1);
pbdrv_gpio_input(&pins->id2);
pbdrv_gpio_input(&pins->uart_buf);
pbdrv_gpio_input(&pins->uart_tx);
pbdrv_gpio_input(&pins->uart_rx);
}
// TODO: This should be moved to a common ioport_core.c file or removed entirely
pbio_error_t pbdrv_ioport_get_iodev(pbio_port_t port, pbio_iodev_t **iodev) {
if (port < PBDRV_CONFIG_IOPORT_LPF2_FIRST_PORT || port > PBDRV_CONFIG_IOPORT_LPF2_LAST_PORT) {
return PBIO_ERROR_INVALID_PORT;
}
*iodev = &iodevs[port - PBDRV_CONFIG_IOPORT_LPF2_FIRST_PORT];
return PBIO_SUCCESS;
}
// This is the device connection manager (dcm). It monitors the ID1 and ID2 pins
// on the port to see when devices are connected or disconnected.
// It is expected for there to be a 2ms delay between calls to this function.
static PT_THREAD(poll_dcm(ioport_dev_t *ioport)) {
struct pt *pt = &ioport->pt;
dcm_data_t *data = &ioport->dcm;
const pbdrv_ioport_lpf2_platform_port_t pins = *ioport->pins;
uint8_t gpio_input;
PT_BEGIN(pt);
data->type_id = PBIO_IODEV_TYPE_ID_NONE;
data->dev_id1_group = DEV_ID1_GROUP_OPEN;
// set ID1 high
pbdrv_gpio_out_high(&pins.uart_tx);
pbdrv_gpio_out_low(&pins.uart_buf);
// set ID2 as input
pbdrv_gpio_input(&pins.id2);
PT_YIELD(pt);
// save current ID2 value
data->prev_gpio_value = pbdrv_gpio_input(&pins.id2);
// set ID1 low
pbdrv_gpio_out_low(&pins.uart_tx);
PT_YIELD(pt);
// read ID2
gpio_input = pbdrv_gpio_input(&pins.id2);
// if ID2 changed from high to low
if (data->prev_gpio_value == 1 && gpio_input == 0) {
// we have touch sensor
data->type_id = PBIO_IODEV_TYPE_ID_LPF2_TOUCH;
// set ID1 as input
pbdrv_gpio_out_high(&pins.uart_buf);
pbdrv_gpio_input(&pins.uart_tx);
PT_YIELD(pt);
// ID1 is inverse of touch sensor value
// TODO: save this value to sensor data
//sensor_data = !pbdrv_gpio_input(&pins.id1);
}
// if ID2 changed from low to high
else if (data->prev_gpio_value == 0 && gpio_input == 1) {
data->type_id = PBIO_IODEV_TYPE_ID_LPF2_TPOINT;
}
else {
// read ID1
data->prev_gpio_value = pbdrv_gpio_input(&pins.id1);
// set ID1 high
pbdrv_gpio_out_high(&pins.uart_tx);
PT_YIELD(pt);
// read ID1
gpio_input = pbdrv_gpio_input(&pins.id1);
// if ID1 did not change and is high
if (data->prev_gpio_value == 1 && gpio_input == 1) {
// we have ID1 == VCC
data->dev_id1_group = DEV_ID1_GROUP_VCC;
}
// if ID1 did not change and is low
else if (data->prev_gpio_value == 0 && gpio_input == 0) {
// we have ID1 == GND
data->dev_id1_group = DEV_ID1_GROUP_GND;
}
else {
// set ID1 as input
pbdrv_gpio_out_high(&pins.uart_buf);
pbdrv_gpio_input(&pins.uart_tx);
PT_YIELD(pt);
// read ID1
if (pbdrv_gpio_input(&pins.id1) == 1) {
// we have ID1 == open
data->dev_id1_group = DEV_ID1_GROUP_OPEN;
}
else {
// we have ID1 == pull down
data->dev_id1_group = DEV_ID1_GROUP_PULL_DOWN;
}
}
PT_YIELD(pt);
// set ID1 as input
pbdrv_gpio_out_high(&pins.uart_buf);
pbdrv_gpio_input(&pins.uart_tx);
// set ID2 high
pbdrv_gpio_out_high(&pins.id2);
PT_YIELD(pt);
// read ID1
data->prev_gpio_value = pbdrv_gpio_input(&pins.id1);
// set ID2 low
pbdrv_gpio_out_low(&pins.id2);
PT_YIELD(pt);
// read ID1
gpio_input = pbdrv_gpio_input(&pins.id1);
// if ID1 changed from high to low
if (data->prev_gpio_value == 1 && gpio_input == 0) {
// if we have ID1 = open
if (data->dev_id1_group == DEV_ID1_GROUP_OPEN) {
// then we have this
data->type_id = PBIO_IODEV_TYPE_ID_LPF2_3_PART;
}
}
// if ID1 changed from low to high
else if (data->prev_gpio_value == 0 && gpio_input == 1) {
// something might explode
data->type_id = PBIO_IODEV_TYPE_ID_LPF2_EXPLOD;
}
else {
// set ID1 high
pbdrv_gpio_out_high(&pins.uart_tx);
pbdrv_gpio_out_low(&pins.uart_buf);
// set ID2 high
pbdrv_gpio_out_high(&pins.id2);
PT_YIELD(pt);
// if ID2 is high
if (pbdrv_gpio_input(&pins.uart_rx) == 1) {
// set ID2 low
pbdrv_gpio_out_low(&pins.id2);
PT_YIELD(pt);
// if ID2 is low
if (pbdrv_gpio_input(&pins.uart_rx) == 0) {
if (data->dev_id1_group < 3) {
data->type_id = ioport_type_id_lookup[data->dev_id1_group][2];
}
}
else {
if (data->dev_id1_group < 3) {
data->type_id = ioport_type_id_lookup[data->dev_id1_group][1];
}
}
}
else {
// we know the device now
if (data->dev_id1_group < 3) {
data->type_id = ioport_type_id_lookup[data->dev_id1_group][0];
}
else {
data->type_id = PBIO_IODEV_TYPE_ID_LPF2_UNKNOWN_UART;
}
}
}
}
PT_YIELD(pt);
// set ID2 as input
pbdrv_gpio_input(&pins.id2);
// set ID1 low
pbdrv_gpio_out_low(&pins.uart_tx);
pbdrv_gpio_out_low(&pins.uart_buf);
if (data->type_id == data->prev_type_id) {
if (++data->dev_id_match_count >= 20) {
if (data->type_id != ioport->connected_type_id) {
ioport->connected_type_id = data->type_id;
}
// don't want to wrap around and re-trigger
data->dev_id_match_count--;
}
}
data->prev_type_id = data->type_id;
PT_END(pt);
}
PROCESS_THREAD(pbdrv_ioport_lpf2_process, ev, data) {
static struct etimer timer;
PROCESS_BEGIN();
etimer_set(&timer, clock_from_msec(2));
for (int i = 0; i < PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS; i++) {
init_one(i);
}
while (true) {
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));
etimer_reset(&timer);
for (int i = 0; i < PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS; i++) {
ioport_dev_t *ioport = &ioport_devs[i];
if (ioport->connected_type_id != PBIO_IODEV_TYPE_ID_LPF2_UNKNOWN_UART) {
poll_dcm(ioport);
}
if (ioport->connected_type_id != ioport->prev_type_id) {
ioport->prev_type_id = ioport->connected_type_id;
if (ioport->connected_type_id == PBIO_IODEV_TYPE_ID_LPF2_UNKNOWN_UART) {
ioport_enable_uart(ioport);
}
}
}
}
PROCESS_END();
}
#endif // PBDRV_CONFIG_IOPORT_LPF2