소스 검색

Added debug routines.

Michael Hope 4 년 전
부모
커밋
b0368f7af0
9개의 변경된 파일156개의 추가작업 그리고 186개의 파일을 삭제
  1. 8 0
      .gitignore
  2. 28 0
      HACKING
  3. 30 14
      Makefile.am
  4. 15 0
      lib/timer.h
  5. 6 0
      roverif/board.h
  6. 32 165
      roverif/main.cc
  7. 29 0
      roverif/roverif.h
  8. 2 2
      roverif/start.cc
  9. 6 5
      roverif/supervisor.h

+ 8 - 0
.gitignore 파일 보기

@@ -8,3 +8,11 @@
8 8
 build
9 9
 .deps
10 10
 .dirstamp
11
+Makefile.in
12
+aclocal.m4
13
+autom4te.cache/
14
+configure
15
+depcomp
16
+install-sh
17
+missing
18
+*~

+ 28 - 0
HACKING 파일 보기

@@ -45,3 +45,31 @@ Hardware interface
45 45
 Events are generated by the HAL, timers, and other functions.  Events
46 46
 are recorded in a Switcher and then dispatched in priority order.
47 47
 Event handlers are short and run to completion.
48
+
49
+Status
50
+------
51
+Always out of date, but still.
52
+
53
+STM32:
54
+
55
+* Basic startup
56
+* System tick driver
57
+* GPIO driver
58
+* USART driver (for GPS)
59
+* PWM input driver (for reading the receiver demands)
60
+* PWM output driver (for driving the servo)
61
+* USB ACM driver
62
+* Watchdog driver
63
+* Serial log based debugging
64
+
65
+roverif:
66
+
67
+* Application level timers
68
+* Switcher for dispatching events
69
+* Status LED
70
+* Supervisor
71
+* Fail safe configuration
72
+* Software fault fail safe
73
+* GPS driver
74
+* Accelerometer driver
75
+* Gyro driver

+ 30 - 14
Makefile.am 파일 보기

@@ -1,16 +1,16 @@
1
-bin_PROGRAMS = blink.elf roverif.elf
1
+bin_PROGRAMS = roverif.elf
2 2
 
3 3
 blink_elf_SOURCES = \
4 4
 	experiments/blink.c
5 5
 
6 6
 roverif_elf_SOURCES = \
7 7
 	roverif/main.cc \
8
-	roverif/init.cc \
9
-	roverif/start.cc \
10 8
 	roverif/pwmin.cc \
11
-	roverif/supervisor.cc
9
+	roverif/supervisor.cc \
10
+	roverif/hal-posix.cc
12 11
 
13
-roverif_elf_LDADD = libnppilot.a libmaple.a libmaple-usb.a
12
+#roverif_elf_LDADD = libnppilot.a libmaple.a libmaple-usb.a
13
+roverif_elf_LDADD = libnppilot.a
14 14
 
15 15
 check_PROGRAMS = \
16 16
 	tests/runner
@@ -18,9 +18,12 @@ check_PROGRAMS = \
18 18
 tests_runner_LDADD = libnppilot.a
19 19
 
20 20
 lib_LIBRARIES = \
21
-	libmaple.a \
22
-	libmaple-usb.a \
23
-	libnppilot.a
21
+	libnppilot.a \
22
+	libplatform-posix.a
23
+
24
+#	libmaple.a \
25
+#	libmaple-usb.a \
26
+#
24 27
 
25 28
 libnppilot_a_SOURCES = \
26 29
 	lib/cobs.cc \
@@ -28,6 +31,17 @@ libnppilot_a_SOURCES = \
28 31
 	lib/timer.cc \
29 32
 	lib/blinker.cc \
30 33
 	lib/switch.cc \
34
+	lib/debug.cc \
35
+	platform/posix/bitarray.cc \
36
+	platform/posix/timer.cc \
37
+	platform/posix/usblink.cc
38
+
39
+libplatform_posix_a_SOURCES = \
40
+	platform/posix/bitarray.cc \
41
+	platform/posix/timer.cc \
42
+	platform/posix/usblink.cc
43
+
44
+libplatform_stm32_a_SOURCES = \
31 45
 	platform/stm32/bitarray.cc \
32 46
 	platform/stm32/timer.cc \
33 47
 	platform/stm32/usblink.cc
@@ -79,19 +93,21 @@ LIBMAPLE = $(srcdir)/external/libmaple
79 93
 tests/runner.cc: $(wildcard $(srcdir)/tests/*.h)
80 94
 	$(srcdir)/external/cxxtest/bin/cxxtestgen --error-printer -o $@ $^
81 95
 
82
-CROSS_COMPILE = arm-none-eabi-
96
+#CROSS_COMPILE = arm-none-eabi-
83 97
 CC = $(CROSS_COMPILE)gcc
84 98
 CXX = $(CROSS_COMPILE)g++
85 99
 OBJCOPY = $(CROSS_COMPILE)objcopy
86 100
 
87 101
 FLAGS =
88 102
 FLAGS += -O1 -g -Wall -fno-common -fno-section-anchors -fno-exceptions
89
-FLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections
103
+#FLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections
90 104
 FLAGS += -I$(srcdir)/lib -I$(srcdir) -I$(srcdir)/external/cxxtest
91
-FLAGS += -I$(LIBMAPLE)/libmaple/include -I$(LIBMAPLE)/libmaple -I$(LIBMAPLE)/libmaple/usb/usb_lib -I$(LIBMAPLE)/libmaple/stm32f1/include -I$(LIBMAPLE)  -I$(srcdir)/platform/stm32
92
-FLAGS += -mcpu=cortex-m3 -mthumb -save-temps=obj
93
-FLAGS += -DMCU_STM32F103CB
94
-FLAGS += -nostartfiles -T$(srcdir)/platform/coptercontrol/flash.ld
105
+FLAGS += -save-temps=obj
106
+
107
+#FLAGS += -I$(LIBMAPLE)/libmaple/include -I$(LIBMAPLE)/libmaple -I$(LIBMAPLE)/libmaple/usb/usb_lib -I$(LIBMAPLE)/libmaple/stm32f1/include -I$(LIBMAPLE)  -I$(srcdir)/platform/stm32
108
+#FLAGS += -mcpu=cortex-m3 -mthumb
109
+#FLAGS += -DMCU_STM32F103CB
110
+#FLAGS += -nostartfiles -T$(srcdir)/platform/coptercontrol/flash.ld
95 111
 
96 112
 AM_CFLAGS = $(FLAGS) -std=gnu99
97 113
 AM_CXXFLAGS = $(FLAGS) -std=gnu++0x -fno-rtti

+ 15 - 0
lib/timer.h 파일 보기

@@ -41,6 +41,7 @@ public:
41 41
     /** Ticks all system timers, firing those that elapse. */
42 42
     static void tick_all();
43 43
 
44
+    static void bind(Timer& timer, const Timer::Fixed& fixed);
44 45
 private:
45 46
     static const uint16_t Reserved = 0xFFFE;
46 47
 
@@ -50,6 +51,18 @@ private:
50 51
     uint16_t _remain;
51 52
 };
52 53
 
54
+#ifndef TIMER_IN_SECTIONS
55
+#define CONCAT_IMPL( x, y ) x##y
56
+#define MACRO_CONCAT( x, y ) CONCAT_IMPL( x, y )
57
+
58
+#define MAKE_TIMER(_name, _id, _period)               \
59
+    Timer _name __attribute__((section(".timers")));  \
60
+    Timer::Fixed _name##_fixed                        \
61
+                       = { _period - 1, _id };\
62
+    __attribute__((constructor)) void MACRO_CONCAT(init, __LINE__) () { \
63
+        Timer::bind(_name, _name##_fixed); }
64
+
65
+#else
53 66
 /**
54 67
  * Make a timer called `_name` that triggers the thread `_id` and
55 68
  * has the period `_period`.  See Switcher::None and
@@ -60,3 +73,5 @@ private:
60 73
     Timer::Fixed _name##_fixed                        \
61 74
     __attribute__((section(".timers.ro"))) \
62 75
                        = { _period - 1, _id };
76
+
77
+#endif

+ 6 - 0
roverif/board.h 파일 보기

@@ -1,5 +1,11 @@
1
+
1 2
 struct Board
2 3
 {
3 4
     static const int AHBClock = 72000000;
4 5
     static const int APB1Clock = AHBClock / 2;
6
+    static const int APB2Clock = AHBClock / 1;
7
+    static const int LSIClock = 40000;
8
+    static const int DebugBaudRate = 115200;
9
+
10
+    static const int Ticks = 1000;
5 11
 };

+ 32 - 165
roverif/main.cc 파일 보기

@@ -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, &regs.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, &regs.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, &regs.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, &regs.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, &regs.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, &regs.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
-

+ 29 - 0
roverif/roverif.h 파일 보기

@@ -1,3 +1,15 @@
1
+#include <switcher.h>
2
+#include <timer.h>
3
+#include <cobs.h>
4
+#include <usblink.h>
5
+#include <switch.h>
6
+#include "supervisor.h"
7
+#include <debug.h>
8
+
9
+#include "protocol.h"
10
+#include "pwmin.h"
11
+#include "blinker.h"
12
+
1 13
 extern void systick();
2 14
 void irq_timer4ch1();
3 15
 void irq_timer3ch2();
@@ -16,3 +28,20 @@ enum ThreadID {
16 28
     PollID,
17 29
     SupervisorID,
18 30
 };
31
+
32
+class RoverIf
33
+{
34
+public:
35
+    static void init();
36
+    static void run();
37
+
38
+    static void poll();
39
+    static void tick();
40
+
41
+    static PWMIn pwmin;
42
+    static Switcher switcher;
43
+    static USBLink usblink;
44
+    static COBSLink link;
45
+    static Blinker blinker;
46
+    static Supervisor supervisor;
47
+};

+ 2 - 2
roverif/start.cc 파일 보기

@@ -29,14 +29,14 @@ void _start()
29 29
   memcpy(&__data_start, &__data_load, &__data_end - &__data_start);
30 30
   memset(&__bss_start, 0, &__bss_end - &__bss_start);
31 31
 
32
-  init();
32
+  RoverIf::init();
33 33
 
34 34
   for (init_function* i = &__init_array_start; i < &__init_array_end; i++) {
35 35
       (**i)();
36 36
   }
37 37
 
38 38
   nvic_globalirq_enable();
39
-  main();
39
+  RoverIf::run();
40 40
 
41 41
   for (;;) {
42 42
   }

+ 6 - 5
roverif/supervisor.h 파일 보기

@@ -16,7 +16,7 @@ public:
16 16
         Remote,
17 17
         RemoteArmed,
18 18
         Pilot,
19
-        Shutdown
19
+        Shutdown,
20 20
     };
21 21
 
22 22
     Supervisor();
@@ -28,6 +28,11 @@ public:
28 28
 
29 29
     void expired();
30 30
 
31
+    static Timer remote_seen_;
32
+    static Timer::Fixed remote_seen__fixed;
33
+    static Timer pilot_seen_;
34
+    static Timer::Fixed pilot_seen__fixed;
35
+
31 36
 private:
32 37
     static const int LostThrottle = 8296;
33 38
     static const int ThrottleChannel = 2;
@@ -49,8 +54,4 @@ private:
49 54
     Switch mode_;
50 55
 
51 56
     static const Switch::Fixed mode_fixed_;
52
-    static Timer remote_seen_;
53
-    static Timer::Fixed remote_seen__fixed;
54
-    static Timer pilot_seen_;
55
-    static Timer::Fixed pilot_seen__fixed;
56 57
 };