|
|
@@ -1,155 +1,54 @@
|
|
1
|
1
|
#include <cstring>
|
|
2
|
2
|
#include <cstdint>
|
|
3
|
|
-
|
|
4
|
|
-
|
|
5
|
|
-#include "libmaple/scb.h"
|
|
6
|
|
-#include "libmaple/timer.h"
|
|
7
|
|
-#include "libmaple/usart.h"
|
|
8
|
|
-#include "libmaple/gpio.h"
|
|
9
|
|
-#include "libmaple/iwdg.h"
|
|
10
|
|
-#include "libmaple/usb_cdcacm.h"
|
|
11
|
|
-#include "libmaple/usb/stm32f1/usb_reg_map.h"
|
|
12
|
|
-#include "libmaple/systick.h"
|
|
13
|
|
-
|
|
14
|
|
-#include <switcher.h>
|
|
15
|
|
-#include <timer.h>
|
|
16
|
|
-#include <cobs.h>
|
|
17
|
|
-#include <usblink.h>
|
|
18
|
|
-#include <platform/stm32/vectors.h>
|
|
19
|
|
-#include <switch.h>
|
|
20
|
|
-#include "supervisor.h"
|
|
|
3
|
+#include <cstdio>
|
|
21
|
4
|
|
|
22
|
5
|
#include "roverif.h"
|
|
23
|
|
-#include "protocol.h"
|
|
24
|
|
-#include "pwmin.h"
|
|
25
|
|
-#include "blinker.h"
|
|
26
|
|
-
|
|
27
|
|
-volatile int stuck;
|
|
28
|
|
-
|
|
29
|
|
-void init();
|
|
30
|
|
-int main();
|
|
31
|
|
-
|
|
32
|
|
-static PWMIn pwmin;
|
|
33
|
|
-static Switcher switcher;
|
|
34
|
|
-static USBLink usblink;
|
|
35
|
|
-static COBSLink link;
|
|
36
|
|
-static Blinker blinker;
|
|
37
|
|
-static Supervisor supervisor;
|
|
38
|
|
-
|
|
39
|
|
-void irq_timer4ch1()
|
|
40
|
|
-{
|
|
41
|
|
- timer_gen_reg_map& regs = *TIMER4->regs.gen;
|
|
42
|
|
- pwmin.irq(0, regs.CCR1, ®s.CCER, TIMER_CCER_CC1P);
|
|
43
|
|
-}
|
|
44
|
|
-
|
|
45
|
|
-void irq_timer3ch2()
|
|
46
|
|
-{
|
|
47
|
|
- timer_gen_reg_map& regs = *TIMER3->regs.gen;
|
|
48
|
|
- pwmin.irq(1, regs.CCR2, ®s.CCER, TIMER_CCER_CC2P);
|
|
49
|
|
-}
|
|
50
|
|
-
|
|
51
|
|
-void irq_timer3ch3()
|
|
52
|
|
-{
|
|
53
|
|
- timer_gen_reg_map& regs = *TIMER3->regs.gen;
|
|
54
|
|
- pwmin.irq(2, regs.CCR3, ®s.CCER, TIMER_CCER_CC3P);
|
|
55
|
|
-}
|
|
56
|
|
-
|
|
57
|
|
-void irq_timer3ch4()
|
|
58
|
|
-{
|
|
59
|
|
- timer_gen_reg_map& regs = *TIMER3->regs.gen;
|
|
60
|
|
- pwmin.irq(3, regs.CCR4, ®s.CCER, TIMER_CCER_CC4P);
|
|
61
|
|
-}
|
|
62
|
|
-
|
|
63
|
|
-void irq_timer2ch1()
|
|
64
|
|
-{
|
|
65
|
|
- timer_gen_reg_map& regs = *TIMER2->regs.gen;
|
|
66
|
|
- pwmin.irq(4, regs.CCR1, ®s.CCER, TIMER_CCER_CC1P);
|
|
67
|
|
-}
|
|
68
|
|
-
|
|
69
|
|
-void irq_timer2ch2()
|
|
70
|
|
-{
|
|
71
|
|
- timer_gen_reg_map& regs = *TIMER2->regs.gen;
|
|
72
|
|
- pwmin.irq(5, regs.CCR2, ®s.CCER, TIMER_CCER_CC2P);
|
|
73
|
|
-}
|
|
74
|
|
-
|
|
75
|
|
-static void put_hex(uint32_t v)
|
|
76
|
|
-{
|
|
77
|
|
- static const char* chars = "0123456789abcedf";
|
|
78
|
|
-
|
|
79
|
|
- for (int i = 32-4; i >= 0; i -= 4) {
|
|
80
|
|
- usart_putc(USART2, chars[(v >> i) & 0x0f]);
|
|
81
|
|
- }
|
|
82
|
|
-}
|
|
83
|
|
-
|
|
84
|
|
-__attribute__((noinline))
|
|
85
|
|
-static void _systick(uint32_t* sp)
|
|
86
|
|
-{
|
|
87
|
|
- if (++stuck == 1000) {
|
|
88
|
|
- usart_putstr(USART2, "\r\nstuck at ");
|
|
89
|
|
- put_hex(sp[6]);
|
|
90
|
|
- usart_putstr(USART2, "\r\n");
|
|
91
|
|
- }
|
|
92
|
|
-
|
|
93
|
|
- switcher.trigger(SysTickID);
|
|
94
|
|
-}
|
|
95
|
|
-
|
|
96
|
|
-void systick()
|
|
97
|
|
-{
|
|
98
|
|
- uint32_t* sp;
|
|
99
|
|
- asm volatile ("mov %0, sp" : "=r" (sp));
|
|
100
|
|
- _systick(sp);
|
|
101
|
|
-}
|
|
|
6
|
+#include "hal.h"
|
|
102
|
7
|
|
|
|
8
|
+PWMIn RoverIf::pwmin;
|
|
|
9
|
+Switcher RoverIf::switcher;
|
|
|
10
|
+USBLink RoverIf::usblink;
|
|
|
11
|
+COBSLink RoverIf::link;
|
|
|
12
|
+Blinker RoverIf::blinker;
|
|
|
13
|
+Supervisor RoverIf::supervisor;
|
|
103
|
14
|
|
|
104
|
15
|
void COBSLink::write(const uint8_t* p, int length)
|
|
105
|
16
|
{
|
|
106
|
|
- usblink.write(p, length);
|
|
|
17
|
+ RoverIf::usblink.write(p, length);
|
|
107
|
18
|
}
|
|
108
|
19
|
|
|
109
|
20
|
void Timer::dispatch(int id)
|
|
110
|
21
|
{
|
|
111
|
|
- switcher.trigger(id);
|
|
|
22
|
+ RoverIf::switcher.trigger(id);
|
|
112
|
23
|
}
|
|
113
|
24
|
|
|
114
|
25
|
void COBSLink::dispatch(int id, const void* msg, int length)
|
|
115
|
26
|
{
|
|
116
|
|
- usart_putstr(USART2, "COBSLink::dispatch\r\n");
|
|
|
27
|
+ Debug::info("COBSLink::dispatch");
|
|
117
|
28
|
}
|
|
118
|
29
|
|
|
119
|
30
|
static void heartbeat()
|
|
120
|
31
|
{
|
|
121
|
32
|
Protocol::Heartbeat msg = { .version = 1, .device_id = 2 };
|
|
122
|
|
- link.send('h', &msg, sizeof(msg));
|
|
|
33
|
+ RoverIf::link.send('h', &msg, sizeof(msg));
|
|
123
|
34
|
}
|
|
124
|
35
|
|
|
125
|
36
|
void Blinker::update(bool level)
|
|
126
|
37
|
{
|
|
127
|
|
- if (level) {
|
|
128
|
|
- GPIOA_BASE->ODR &= ~(1 << 6);
|
|
129
|
|
- } else {
|
|
130
|
|
- GPIOA_BASE->ODR |= 1 << 6;
|
|
131
|
|
- }
|
|
|
38
|
+ HAL::set_status_led(level);
|
|
132
|
39
|
}
|
|
133
|
40
|
|
|
134
|
41
|
void Supervisor::changed()
|
|
135
|
42
|
{
|
|
136
|
|
- switch (supervisor.state()) {
|
|
137
|
|
- case Supervisor::State::Remote:
|
|
138
|
|
- blinker.set(0b100000101);
|
|
139
|
|
- break;
|
|
140
|
|
- case Supervisor::State::RemoteArmed:
|
|
141
|
|
- blinker.set(0b100001101);
|
|
142
|
|
- break;
|
|
143
|
|
- case Supervisor::State::Pilot:
|
|
144
|
|
- blinker.set(0b111111110);
|
|
145
|
|
- break;
|
|
146
|
|
- case Supervisor::State::Shutdown:
|
|
147
|
|
- blinker.set(0b100000001);
|
|
148
|
|
- break;
|
|
149
|
|
- default:
|
|
150
|
|
- blinker.set(0b101);
|
|
151
|
|
- break;
|
|
152
|
|
- }
|
|
|
43
|
+ static const uint16_t patterns[] = {
|
|
|
44
|
+ [State::None] = 0b101,
|
|
|
45
|
+ [State::Remote] = 0b100000101,
|
|
|
46
|
+ [State::RemoteArmed] = 0b100001101,
|
|
|
47
|
+ [State::Pilot] = 0b111111110,
|
|
|
48
|
+ [State::Shutdown] = 0b100000001,
|
|
|
49
|
+ };
|
|
|
50
|
+
|
|
|
51
|
+ RoverIf::blinker.set(patterns[(int)state()]);
|
|
153
|
52
|
}
|
|
154
|
53
|
|
|
155
|
54
|
static void tick()
|
|
|
@@ -160,14 +59,8 @@ static void tick()
|
|
160
|
59
|
MAKE_TIMER(timer_expired, -1, Timer::Stopped);
|
|
161
|
60
|
MAKE_TIMER(blinker_tick, BlinkerID, 100);
|
|
162
|
61
|
|
|
163
|
|
-static void poll()
|
|
|
62
|
+void RoverIf::poll()
|
|
164
|
63
|
{
|
|
165
|
|
- static int at;
|
|
166
|
|
- static int direction = 1;
|
|
167
|
|
-
|
|
168
|
|
- iwdg_feed();
|
|
169
|
|
- stuck = 0;
|
|
170
|
|
-
|
|
171
|
64
|
Protocol::Inputs msg = { };
|
|
172
|
65
|
|
|
173
|
66
|
for (int i = 0; i < pwmin.count(); i++) {
|
|
|
@@ -175,33 +68,17 @@ static void poll()
|
|
175
|
68
|
}
|
|
176
|
69
|
|
|
177
|
70
|
supervisor.set_remote(msg.channel, 6);
|
|
178
|
|
-
|
|
179
|
|
- int ch1 = pwmin.value(0) - 8450;
|
|
180
|
|
- int out = 0;
|
|
181
|
|
- // 10550 8450 6300
|
|
182
|
|
-
|
|
183
|
|
- timer_set_compare(TIMER4, 4, 8450 + ch1);
|
|
184
|
|
-
|
|
185
|
71
|
link.send('i', &msg, sizeof(msg));
|
|
186
|
|
-
|
|
187
|
|
- if (usb_cdcacm_data_available()) {
|
|
188
|
|
- uint8_t rx[32];
|
|
189
|
|
- int got = usb_cdcacm_rx(rx, sizeof(rx));
|
|
190
|
|
-
|
|
191
|
|
- link.feed(rx, got);
|
|
192
|
|
- }
|
|
193
|
72
|
}
|
|
194
|
73
|
|
|
195
|
74
|
MAKE_TIMER(timer_heartbeat, HeartbeatID, 1000);
|
|
196
|
75
|
MAKE_TIMER(timer_poll, PollID, 20);
|
|
197
|
76
|
|
|
198
|
|
-// 8296 counts as throttle cut
|
|
199
|
|
-
|
|
200
|
77
|
void Switcher::dispatch(int id)
|
|
201
|
78
|
{
|
|
202
|
79
|
switch (id) {
|
|
203
|
80
|
case BlinkerID:
|
|
204
|
|
- blinker.tick();
|
|
|
81
|
+ RoverIf::blinker.tick();
|
|
205
|
82
|
break;
|
|
206
|
83
|
case SysTickID:
|
|
207
|
84
|
tick();
|
|
|
@@ -210,10 +87,10 @@ void Switcher::dispatch(int id)
|
|
210
|
87
|
heartbeat();
|
|
211
|
88
|
break;
|
|
212
|
89
|
case PollID:
|
|
213
|
|
- poll();
|
|
|
90
|
+ RoverIf::poll();
|
|
214
|
91
|
break;
|
|
215
|
92
|
case SupervisorID:
|
|
216
|
|
- supervisor.expired();
|
|
|
93
|
+ RoverIf::supervisor.expired();
|
|
217
|
94
|
break;
|
|
218
|
95
|
default:
|
|
219
|
96
|
assert(false);
|
|
|
@@ -221,31 +98,21 @@ void Switcher::dispatch(int id)
|
|
221
|
98
|
}
|
|
222
|
99
|
}
|
|
223
|
100
|
|
|
224
|
|
-extern "C" usart_dev* __lm_enable_error_usart(void)
|
|
|
101
|
+void RoverIf::init()
|
|
225
|
102
|
{
|
|
226
|
|
- return USART2;
|
|
|
103
|
+ HAL::init();
|
|
227
|
104
|
}
|
|
228
|
105
|
|
|
229
|
|
-__attribute__((noinline))
|
|
230
|
|
-void stick()
|
|
|
106
|
+void RoverIf::run()
|
|
231
|
107
|
{
|
|
232
|
|
- for (;;) {}
|
|
233
|
|
-}
|
|
|
108
|
+ HAL::start();
|
|
|
109
|
+ Debug::info("roverif: start");
|
|
234
|
110
|
|
|
235
|
|
-int main()
|
|
236
|
|
-{
|
|
237
|
111
|
blinker.set(0b101);
|
|
238
|
|
-// stick();
|
|
239
|
|
-
|
|
240
|
|
- usart_putstr(USART2, "\r\n\r\ngo\r\n");
|
|
241
|
|
- usart_putudec(USART2, *(uint32_t*)0xE000ED00);
|
|
242
|
112
|
|
|
243
|
113
|
for (;;) {
|
|
244
|
114
|
switcher.next();
|
|
245
|
115
|
usblink.flush();
|
|
246
|
|
- asm ("wfi");
|
|
|
116
|
+ HAL::wait();
|
|
247
|
117
|
}
|
|
248
|
|
-
|
|
249
|
|
- return 0;
|
|
250
|
118
|
}
|
|
251
|
|
-
|