-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain_gs.c
126 lines (94 loc) · 2.11 KB
/
main_gs.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
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/can.h>
#include <libopencm3/stm32/iwdg.h>
#include <libopencm3/cm3/cortex.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "led.h"
#include "tick.h"
#include "clock.h"
#include "usb.h"
#include "usb_gs.h"
#include "timer.h"
#include "can.h"
static void gs_usb_read(void)
{
can_message_t msg;
uint8_t ch = 2;
if (usb_gs_msg_pop(&ch, &msg)) {
if (ch == 0)
can_msg_push(can_get_hscan(), &msg);
if (ch == 1)
can_msg_push(can_get_mscan(), &msg);
}
}
static void gs_can_read(struct can_t * can, uint8_t ch)
{
can_message_t msg;
uint8_t r = can_msg_pop(can, &msg);
if (r)
usb_gs_msg_push(ch, &msg);
//ack tx
r = can_echo_pop(can, &msg);
if (r)
usb_gs_msg_push(ch, &msg);
can_tx_process(can);
}
int main(void)
{
cm_disable_interrupts();
clock_setup();
timer_setup();
led_setup();
systick_setup();
iwdg_set_period_ms(1000);
usb_setup(&gs_cb);
can_setup();
cm_enable_interrupts();
uint32_t hscan_rx_cnts = 0;
uint32_t hscan_tx_cnts = 0;
uint32_t mscan_rx_cnts = 0;
uint32_t mscan_tx_cnts = 0;
while(1)
{
iwdg_reset();
gs_usb_read();
gs_can_read(can_get_hscan(), 0);
gs_can_read(can_get_mscan(), 1);
if (tick.flag_tick) {
tick.flag_tick = 0;
if (tick.flag_100ms) {
bool activity = false;
uint32_t cnts = can_get_rx_cnts(can_get_hscan());
if (cnts != hscan_rx_cnts)
activity = true;
hscan_rx_cnts = cnts;
cnts = can_get_tx_cnts(can_get_hscan());
if (cnts != hscan_tx_cnts)
activity = true;
hscan_tx_cnts = cnts;
cnts = can_get_rx_cnts(can_get_mscan());
if (cnts != mscan_rx_cnts)
activity = true;
mscan_rx_cnts = cnts;
cnts = can_get_tx_cnts(can_get_mscan());
if (cnts != mscan_tx_cnts)
activity = true;
mscan_tx_cnts = cnts;
if (activity)
led4_blink();
tick.flag_100ms = 0;
}
if (tick.flag_250ms) {
tick.flag_250ms = 0;
led4_blink();
}
}
}
return 0;
}