-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathecu_msg.c
418 lines (355 loc) · 8.96 KB
/
ecu_msg.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "app_simple_timer.h"
#include "ble_nus.h"
#include "app_uart.h"
#include "nrf_gpio.h"
#include "ecu_msg.h"
#define DASH_DISCONNECTED 0
#define DASH_CONNECTED 1
#define RUUVI_LED_RED 17
#define RUUVI_LED_GREEN 19
#define BREAK_TYPE_LO_BAUD 0
#define WAIT_BEFORE_PULSE 10
#define WAIT_PULSE 70
#define WAIT_AFTER_PULSE 130
#define WAIT_AFTER_WAKEUP 50
#define INTERVAL_MAIN 50
// app_timer configured to run at 250 kHz
#define TIMER_MS(ms) ((250000 * ms) / 1000)
static const unsigned char REQ_WAKEUP[] = {0xfe, 0x04, 0xff, 0xff};
static const unsigned char REQ_INIT[] = {0x72, 0x05, 0x00, 0xf0, 0x99};
static const unsigned char REQ_READ_TABLE_11[] = {0x72, 0x05, 0x71, 0x11, 0x07};
static const unsigned char REQ_READ_TABLE_D1[] = {0x72, 0x05, 0x71, 0xd1, 0x47};
static const char TO_HEX[] = "0123456789ABCDEF";
static int main_state = MAIN_STM_NONE;
static int main_watchdog = 0;
static int msg_state = MSG_STM_IDLE;
static int msg_index = 0;
static int msg_length = 0;
static unsigned char msg_buf[32];
static char str_buf[64];
static int blink_comms = 0;
#define DBG(...) {\
snprintf(str_buf, sizeof(str_buf), __VA_ARGS__);\
write_upstream(str_buf, strlen(str_buf));\
}\
static void init_timer_handler(void * p_context);
#if BREAK_TYPE_LO_BAUD == 1
// BREAK using very low baudrate
static void break_downstream(int state)
{
if (state)
{
NRF_UART0->BAUDRATE = BREAK_BAUDRATE;
app_uart_put(0x00);
}
else
{
NRF_UART0->BAUDRATE = ECU_BAUDRATE;
}
}
#else
// BREAK using TX pin as GPIO
static void break_downstream(int state)
{
if (state)
{
NRF_UART0->PSELTXD = UART_PIN_DISCONNECTED;
nrf_gpio_pin_write(RUUVI_UART_TX, 0);
}
else
{
nrf_gpio_pin_write(RUUVI_UART_TX, 1);
NRF_UART0->PSELTXD = RUUVI_UART_TX;
}
}
#endif
static void write_upstream(const char *msg, int n)
{
int len = n;
uint8_t *ptr = (uint8_t *)msg;
while (len)
{
int sz = len < BLE_NUS_MAX_DATA_LEN ? len : BLE_NUS_MAX_DATA_LEN;
ble_nus_string_send(nus_get_service(), ptr, sz);
len -= sz;
ptr += sz;
}
}
static void write_downstream(const unsigned char *msg, int n)
{
for (int i = 0; i < n; i++)
{
while(app_uart_put(msg[i]) != NRF_SUCCESS);
}
}
static int to_hex(char *ptr, unsigned char v)
{
ptr[0] = TO_HEX[v>>4];
ptr[1] = TO_HEX[v&0xf];
return 2;
}
static int verify_msg_csum(unsigned char *msg)
{
//DBG("verify_msg_csum");
int csum = 0;
int len = msg[1];
for (int i = 0; i < len-1; i++)
{
csum += msg[i];
}
csum = (0x100 - csum) & 0xff;
return csum == msg[len-1];
}
static void reset_msg_stm(void)
{
//DBG("reset_msg_stm");
msg_index = 0;
msg_length = 0;
msg_state = MSG_STM_IDLE;
}
static int do_msg_stm(unsigned char rx)
{
int res = MSG_STATUS_NONE;
switch (msg_state)
{
case MSG_STM_IDLE:
blink_comms = 1;
msg_buf[msg_index++] = rx;
msg_state = MSG_STM_LENGTH;
break;
case MSG_STM_LENGTH:
msg_length = rx;
if (msg_length >= 3 && msg_length <= sizeof(msg_buf))
{
msg_buf[msg_index++] = rx;
msg_state = MSG_STM_DATA;
}
else
{
res = MSG_STATUS_ERR;
reset_msg_stm();
}
break;
case MSG_STM_DATA:
msg_buf[msg_index++] = rx;
if (msg_index >= msg_length)
{
res = verify_msg_csum(msg_buf) ? MSG_STATUS_OK : MSG_STATUS_ERR;
reset_msg_stm();
}
break;
}
return res;
}
static void ecu_send_req(const unsigned char *msg)
{
write_downstream(msg, msg[1]);
}
static void dash_send_msg(const unsigned char *msg)
{
char *ptr = str_buf;
*ptr++ = ':';
for (int i = 0; i < msg[1]; i++)
{
ptr += to_hex(ptr, msg[i]);
}
write_upstream(str_buf, 1+msg[1]*2);
}
static int ecu_process_msg(unsigned char *msg)
{
// Ecu response
if (msg[0] == 0x02)
{
//DBG("valid message");
switch (msg[2])
{
// Init OK
case 0x00:
ecu_send_req(REQ_READ_TABLE_11);
break;
// Table contents
case 0x71:
dash_send_msg(msg);
if (msg[3] == 0x11)
{
ecu_send_req(REQ_READ_TABLE_D1);
}
if (msg[3] == 0xD1)
{
return MAIN_STM_REQ_11;
}
break;
}
}
return MAIN_STM_RUN;
}
int do_main_stm(int reason, unsigned char rx)
{
static int cnt = 0;
// State machine init
if (reason == MAIN_REASON_INIT)
{
main_state = MAIN_STM_NONE;
main_watchdog = 0;
cnt = 0;
return 1;
}
// Periodic actions every 2.5 seconds when in running state
if (reason == MAIN_REASON_NONE && main_state >= MAIN_STM_RUN)
{
if (++cnt < 50)
{
return 1;
}
cnt = 0;
}
switch (main_state)
{
case MAIN_STM_NONE:
// Empty
break;
case MAIN_STM_RUN:
if (reason == MAIN_REASON_RX)
{
int res = do_msg_stm(rx);
if (res == MSG_STATUS_OK)
{
main_state = ecu_process_msg(msg_buf);
main_watchdog = 0;
}
if (res == MSG_STATUS_ERR)
{
main_state = MAIN_STM_REQ_11;
main_watchdog = 0;
}
}
else // every ~2.5 seconds
{
if (++main_watchdog > MAIN_WATCHDOG_MAX)
{
DBG("#reinit");
return 0;
}
}
break;
case MAIN_STM_REQ_11:
// after ~2.5 seconds
if (reason == MAIN_REASON_NONE)
{
ecu_send_req(REQ_READ_TABLE_11);
main_state = MAIN_STM_RUN;
}
break;
}
return 1;
}
static void blink_status(void)
{
static int cnt = 0;
if (nus_get_conn_handle() == BLE_CONN_HANDLE_INVALID)
{
if (cnt == 0)
{
nrf_gpio_pin_set(RUUVI_LED_RED);
}
if (cnt == 98)
{
nrf_gpio_pin_clear(RUUVI_LED_RED);
}
}
else
{
if (cnt == 0)
{
nrf_gpio_pin_set(RUUVI_LED_RED);
}
if (cnt == 90)
{
nrf_gpio_pin_clear(RUUVI_LED_RED);
}
}
cnt = (cnt+1) % 100;
// Blink green if communicating with ECU
if (blink_comms)
{
nrf_gpio_pin_clear(RUUVI_LED_GREEN);
blink_comms = 0;
}
else
{
nrf_gpio_pin_set(RUUVI_LED_GREEN);
}
}
static void main_timer_handler(void * p_context)
{
static int prev_state = DASH_DISCONNECTED;
blink_status();
if (nus_get_conn_handle() != BLE_CONN_HANDLE_INVALID)
{
if (prev_state == DASH_DISCONNECTED)
{
do_main_stm(MAIN_REASON_INIT, 0);
app_simple_timer_start(APP_SIMPLE_TIMER_MODE_SINGLE_SHOT, init_timer_handler, TIMER_MS(WAIT_BEFORE_PULSE), (void *) 0);
}
else
{
// Restart if main state machine returns 0
if (!do_main_stm(MAIN_REASON_NONE, 0))
{
prev_state = DASH_DISCONNECTED;
return;
}
}
prev_state = DASH_CONNECTED;
}
else
{
prev_state = DASH_DISCONNECTED;
}
}
static void init_timer_handler(void * p_context)
{
int state = (int) p_context;
switch (state)
{
case 0:
break_downstream(1);
app_simple_timer_start(APP_SIMPLE_TIMER_MODE_SINGLE_SHOT, init_timer_handler, TIMER_MS(WAIT_PULSE), (void *) 1);
break;
case 1:
break_downstream(0);
app_simple_timer_start(APP_SIMPLE_TIMER_MODE_SINGLE_SHOT, init_timer_handler, TIMER_MS(WAIT_AFTER_PULSE), (void *) 2);
break;
case 2:
ecu_send_req(REQ_WAKEUP);
app_simple_timer_start(APP_SIMPLE_TIMER_MODE_SINGLE_SHOT, init_timer_handler, TIMER_MS(WAIT_AFTER_WAKEUP), (void *) 3);
break;
case 3:
ecu_send_req(REQ_INIT);
main_state = MAIN_STM_RUN;
app_simple_timer_start(APP_SIMPLE_TIMER_MODE_REPEATED, main_timer_handler, TIMER_MS(INTERVAL_MAIN), NULL);
break;
}
}
void ecu_init(void)
{
nrf_gpio_cfg_input(RUUVI_UART_RX, NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg(
RUUVI_UART_TX,
NRF_GPIO_PIN_DIR_OUTPUT,
NRF_GPIO_PIN_INPUT_DISCONNECT,
NRF_GPIO_PIN_NOPULL,
NRF_GPIO_PIN_H0S1,
NRF_GPIO_PIN_NOSENSE);
nrf_gpio_pin_write(RUUVI_UART_TX, 1);
nrf_gpio_cfg_output(RUUVI_LED_RED);
nrf_gpio_cfg_output(RUUVI_LED_GREEN);
nrf_gpio_pin_set(RUUVI_LED_RED);
nrf_gpio_pin_set(RUUVI_LED_GREEN);
// Start main timer
app_simple_timer_init();
app_simple_timer_start(APP_SIMPLE_TIMER_MODE_REPEATED, main_timer_handler, TIMER_MS(INTERVAL_MAIN), NULL);
}