-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.c
425 lines (325 loc) · 8.39 KB
/
main.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
419
420
421
422
423
424
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/stm32/syscfg.h>
#include <libopencm3/stm32/iwdg.h>
#include <string.h>
#include "can.h"
#include "led.h"
#include "tick.h"
#include "strings.h"
#include "msg.h"
#include "ring.h"
#include "hdlc.h"
#include "bl.h"
/* set STM32 to clock by 48MHz from HSI oscillator */
static void clock_setup(void)
{
rcc_clock_setup_in_hsi_out_48mhz();
/* Enable clocks to the GPIO subsystems */
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_USART2);
}
static void usart_setup(int id, uint32_t speed)
{
/* Setup USART parameters. */
usart_set_baudrate(id, speed);
usart_set_databits(id, 8);
usart_set_parity(id, USART_PARITY_NONE);
usart_set_stopbits(id, USART_CR2_STOP_1_0BIT);
usart_set_mode(id, USART_MODE_TX_RX);
usart_set_flow_control(id, USART_FLOWCONTROL_NONE);
/* Enable USART Receive interrupt. */
USART_CR1(id) |= USART_CR1_RXNEIE;
/* Finally enable the USART. */
usart_enable(id);
}
static void gpio_setup(void)
{
led_setup();
/* Setup GPIO pins for USART2 transmit. */
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);
/* Setup USART2 TX pin as alternate function. */
gpio_set_af(GPIOA, GPIO_AF1, GPIO2);
/* Setup GPIO pin GPIO_USART2_RX on GPIO port A for receive. */
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3);
/* Setup USART2 RX pin as alternate function. */
gpio_set_af(GPIOA, GPIO_AF1, GPIO3);
led_red_off();
led_yellow_off();
led_green_off();
}
typedef struct
{
volatile uint16_t flag_tick;
volatile uint16_t flag_40ms;
volatile uint16_t flag_1000ms;
} tick_t;
volatile tick_t timer = { 0, 0, 0 };
static void systick_setup(void)
{
/* 48MHz / 8 => 6000000 counts per second */
systick_set_clocksource(STK_CSR_CLKSOURCE_EXT);
/* clear counter so it starts right away */
STK_CVR = 0;
systick_set_reload(6000000 / TICK_HZ);
systick_interrupt_enable();
/* Start counting. */
systick_counter_enable();
}
static uint16_t div_40ms = 0;
static uint16_t div_1000ms = 0;
void sys_tick_handler(void)
{
timer.flag_tick = 1;
if (++div_40ms >= MSEC_TO_TICK(40)) {
div_40ms = 0;
timer.flag_40ms = 1;
}
if (++div_1000ms >= SEC_TO_TICK(1)) {
div_1000ms = 0;
timer.flag_1000ms = 1;
}
led_tick();
}
void usart_send_string(uint32_t id, const char * str, uint32_t size)
{
uint32_t i;
for (i = 0; i < size; i++)
usart_send_blocking(id, str[i]);
}
struct ring tx_ring2;
uint8_t tx_ring_buffer2[2560];
struct ring rx_ring2;
uint8_t rx_ring_buffer2[256];
void usart2_isr(void)
{
/* Check if we were called because of RXNE. */
if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) &&
((USART_ISR(USART2) & USART_ISR_RXNE) != 0)) {
/* Retrieve the data from the peripheral. */
ring_write_ch(&rx_ring2, usart_recv(USART2));
}
/* Check if we were called because of TXE. */
if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) &&
((USART_ISR(USART2) & USART_ISR_TXE) != 0)) {
uint8_t ch;
if (!ring_read_ch(&tx_ring2, &ch)) {
/* Disable the TXE interrupt, it's no longer needed. */
USART_CR1(USART2) &= ~USART_CR1_TXEIE;
} else {
/* Put data into the transmit register. */
usart_send(USART2, ch);
}
}
}
int usart2_write(const uint8_t * ptr, int len)
{
int ret = ring_write(&tx_ring2, (uint8_t *)ptr, len);
USART_CR1(USART2) |= USART_CR1_TXEIE;
return ret;
}
void usart_put_debug(const char * ptr)
{
uint16_t len = strlen(ptr);
uint8_t buf[len + 2];
struct msg_t * msg = (msg_t *)buf;
msg->len = sizeof(msg_t) + sizeof(msg_debug_t) + len;
msg->type = e_type_debug;
struct msg_debug_t * dbg = (struct msg_debug_t *)msg->data;
dbg->len = len;
for (uint8_t i = 0; i < len; i++) {
dbg->data[i] = ptr[i];
}
hdlc_put_msg(msg);
}
#define sei() __asm__ __volatile__ ("cpsie i")
#define cli() __asm__ __volatile__ ("cpsid i")
void start_boot(void)
{
//wait usart transfer
while (USART_CR1(USART2) & USART_CR1_TXEIE)
;
cli();
systick_interrupt_disable();
nvic_disable_irq(NVIC_USART2_IRQ);
nvic_disable_irq(NVIC_CEC_CAN_IRQ);
//hw reset
iwdg_set_period_ms(100);
iwdg_start();
iwdg_reset();
while(1)
;
}
#define CAN_FILTER_ID_NUMS 4
uint32_t can_filter_id_cnt = 0;
uint32_t can_filter_id[CAN_FILTER_ID_NUMS];
e_speed_t speed = e_speed_125;
uint8_t autoselect_speed = 1;
void usart_process(void)
{
uint8_t ch;
if (!ring_read_ch(&rx_ring2, &ch))
return;
struct msg_t * msg = hdlc_get_msg(ch);
if (!msg)
return;
led_green_on();
if ((msg->type == e_type_ping) && (msg->len == sizeof(struct msg_t))) {
msg->type = e_type_pong;
hdlc_put_msg(msg);
}
else if ((msg->type == e_type_can) && (msg->len == (sizeof(struct msg_t) + sizeof(struct msg_can_t)))) {
struct msg_can_t * m = (struct msg_can_t *)msg->data;
led_yellow_on();
can_snd_msg(m);
msg->len = sizeof(struct msg_t);
hdlc_put_msg(msg);
}
else if ((msg->type == e_type_start) && (msg->len == sizeof(struct msg_t))) {
hdlc_put_msg(msg);
start_boot();
}
else if ((msg->type == e_type_cmd) && (msg->len >= (sizeof(struct msg_t) + 1))) {
uint8_t * cmd = (uint8_t *)msg->data;
//unset filter
if (cmd[0] == e_cmd_unset_filter) {
can_filter_id_cnt = 0;
}
//set filter
else if ((cmd[0] == e_cmd_set_filter) && (msg->len == (sizeof(struct msg_t) + 5))) {
if (can_filter_id_cnt < CAN_FILTER_ID_NUMS) {
uint32_t id = 0;
//align bug?
memcpy(&id, (msg->data + 1), 4);
uint8_t found = 0;
for (uint8_t i = 0; i < can_filter_id_cnt; i++)
if (id == can_filter_id[i])
found = 1;
if (!found) {
can_filter_id[can_filter_id_cnt] = id;
can_filter_id_cnt++;
}
}
}
//set speed
else if ((cmd[0] == e_cmd_set_speed) && (msg->len == (sizeof(struct msg_t) + 2))) {
autoselect_speed = 0;
speed = cmd[1];
if (speed > e_speed_1000) {
speed = e_speed_125;
autoselect_speed = 1;
}
}
}
else {
msg->type = e_type_unknown;
msg->len = sizeof(struct msg_t);
hdlc_put_msg(msg);
}
}
void can_autoselect_speed(void)
{
if (!autoselect_speed)
return;
if (can_get_msgs_num())
return;
if (++speed > e_speed_1000)
speed = e_speed_125;
can_set_speed(speed);
}
/*
* 38400 kbit/sec = 3840 kbytes/sec
*/
static uint32_t can_cycle = 0;
void can_process(void)
{
uint8_t msgs_num = can_get_msgs_num();
if (!msgs_num)
return;
can_cycle++;
uint8_t odd_mask = (can_cycle & 1) ? e_can_odd : 0;
for (uint8_t i = 0; i < msgs_num; i++) {
uint8_t buf[64];
struct msg_can_t msg;
if (can_get_msg(&msg, i)) {
uint8_t id_in_filter = 0;
if (can_filter_id_cnt) {
for (uint8_t j = 0; j < can_filter_id_cnt; j++) {
if (can_filter_id[j] == msg.id)
id_in_filter = 1;
}
}
if (can_filter_id_cnt && !id_in_filter)
continue;
led_yellow_on();
msg.type |= odd_mask;
struct msg_t * m = (struct msg_t *)buf;
uint8_t len = sizeof(struct msg_can_t);
uint8_t * p = (uint8_t *)&msg;
for (int j = 0; j < len; j++)
m->data[j] = p[j];
m->type = e_type_can;
m->len = sizeof(struct msg_t) + len;
hdlc_put_msg(m);
}
}
}
void snd_status(void)
{
uint8_t buf[sizeof(struct msg_t) + sizeof(struct msg_status_t)];
struct msg_t * msg = (struct msg_t *)buf;
msg->len = sizeof(buf);
msg->type = e_type_status;
msg_status_t * st = (msg_status_t *)msg->data;
st->mode = e_mode_sniffer;
st->version = 4;
st->speed = speed;
st->num_ids = 0;
st->num_bytes = can_cycle;
hdlc_put_msg(msg);
}
int main(void)
{
// Copy interrupt vector table to the RAM.
volatile uint32_t *vtable = (volatile uint32_t *)0x20000000;
uint32_t i;
for (i = 0; i < 48; i++)
{
vtable[i] = *(volatile uint32_t *)(ADDR_APP + (i << 2));
}
SYSCFG_CFGR1 |= SYSCFG_CFGR1_MEM_MODE_SRAM;
clock_setup();
gpio_setup();
systick_setup();
can_setup();
ring_init(&rx_ring2, rx_ring_buffer2, sizeof(rx_ring_buffer2));
ring_init(&tx_ring2, tx_ring_buffer2, sizeof(tx_ring_buffer2));
/* Enable the USART2 interrupt. */
nvic_enable_irq(NVIC_USART2_IRQ);
usart_setup(USART2, 38400);
sei();
while (1) {
usart_process();
if (timer.flag_tick) {
timer.flag_tick = 0;
if (timer.flag_40ms) {
timer.flag_40ms = 0;
if (can_filter_id_cnt)
can_process();
can_autoselect_speed();
}
if (timer.flag_1000ms) {
led_red_on();
snd_status();
can_process();
timer.flag_1000ms = 0;
}
}
}
return 0;
}