-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
318 lines (275 loc) · 8.73 KB
/
main.cpp
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
/*
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "ch.h"
#include "ch.hpp"
#include "hal.h"
#include "test.h"
#include "shell.h"
#include "drivers/debug.h"
#include "chprintf.h"
#include "drivers/usb_serial.h"
#include "drivers/reset.h"
#include "drivers/power.hpp"
#include "drivers/extievents.hpp"
using namespace chibios_rt;
/**
* Backup domain data
*/
#define BACKUP_CONFIG_VERSION (0xdeadbeef + 0) // Increment the + part each time the config struct changes
struct backup_domain_data_t
{
char apn[50];
char pin[10];
uint32_t config_version;
}; struct backup_domain_data_t * const backup_domain_data = (struct backup_domain_data_t *)BKPSRAM_BASE;
static bool backup_domain_data_is_sane(void)
{
return backup_domain_data->config_version == BACKUP_CONFIG_VERSION;
}
/*===========================================================================*/
/* Command line related. */
/*===========================================================================*/
static void cmd_bkp(BaseSequentialStream *chp, int argc, char *argv[])
{
if (argc < 2)
{
chprintf(chp, "Usage: bkp set member value | bkp get member\r\n");
return;
}
if (0 == strcmp(argv[0], "get"))
{
if (!backup_domain_data_is_sane())
{
chprintf(chp, "Backup data config version %x does not match expected %x\r\n", backup_domain_data->config_version, BACKUP_CONFIG_VERSION);
return;
}
if (0 == strcmp(argv[1], "apn"))
{
chprintf(chp, "backup_domain_data->%s=%s\r\n", argv[1], backup_domain_data->apn);
}
else if (0 == strcmp(argv[1], "pin"))
{
chprintf(chp, "backup_domain_data->%s=%s\r\n", argv[1], backup_domain_data->pin);
}
else
{
chprintf(chp, "Unsupported member %s\r\n", argv[1]);
}
}
else if (0 == strcmp(argv[0], "set"))
{
if (0 == strcmp(argv[1], "apn"))
{
strncpy(backup_domain_data->apn, argv[2], sizeof(backup_domain_data->apn));
// Set the signature (maybe someday it's a checksum)
backup_domain_data->config_version = BACKUP_CONFIG_VERSION;
}
else if (0 == strcmp(argv[1], "pin"))
{
strncpy(backup_domain_data->pin, argv[2], sizeof(backup_domain_data->pin));
// Set the signature (maybe someday it's a checksum)
backup_domain_data->config_version = BACKUP_CONFIG_VERSION;
}
else
{
chprintf(chp, "Unsupported member %s\r\n", argv[1]);
}
}
else
{
chprintf(chp, "Unsupported subcommand %s\r\n", argv[0]);
}
}
static void cmd_mem(BaseSequentialStream *chp, int argc, char *argv[])
{
size_t n, size;
(void)argv;
if (argc > 0)
{
chprintf(chp, "Usage: mem\r\n");
return;
}
n = chHeapStatus(NULL, &size);
chprintf(chp, "core free memory : %u bytes\r\n", chCoreStatus());
chprintf(chp, "heap fragments : %u\r\n", n);
chprintf(chp, "heap free total : %u bytes\r\n", size);
}
static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[])
{
static const char *states[] = {THD_STATE_NAMES};
Thread *tp;
(void)argv;
if (argc > 0)
{
chprintf(chp, "Usage: threads\r\n");
return;
}
chprintf(chp, " addr stack prio refs state time\r\n");
tp = chRegFirstThread();
do
{
chprintf(chp, "%.8lx %.8lx %4lu %4lu %9s %lu\r\n",
(uint32_t)tp, (uint32_t)tp->p_ctx.r13,
(uint32_t)tp->p_prio, (uint32_t)(tp->p_refs - 1),
states[tp->p_state], (uint32_t)tp->p_time);
tp = chRegNextThread(tp);
} while (tp != NULL);
#ifdef CORTEX_ENABLE_WFI_IDLE
chprintf(chp, "CORTEX_ENABLE_WFI_IDLE=%d\r\n", CORTEX_ENABLE_WFI_IDLE);
#endif
#ifdef ENABLE_WFI_IDLE
chprintf(chp, "ENABLE_WFI_IDLE=%d\r\n", ENABLE_WFI_IDLE);
#endif
}
#define TEST_WA_SIZE THD_WA_SIZE(256)
static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[])
{
Thread *tp;
(void)argv;
if (argc > 0)
{
chprintf(chp, "Usage: test\r\n");
return;
}
tp = chThdCreateFromHeap(NULL, TEST_WA_SIZE, chThdGetPriority(), TestThread, chp);
if (tp == NULL)
{
chprintf(chp, "out of memory\r\n");
return;
}
chThdWait(tp);
}
#define SHELL_WA_SIZE THD_WA_SIZE(2048)
static const ShellCommand commands[] =
{
{"mem", cmd_mem},
{"threads", cmd_threads},
{"test", cmd_test},
/*
{"stop", cmd_stop},
{"standby", cmd_standby},
*/
{"bkp", cmd_bkp},
{"reset", cmd_reset},
{"flash", cmd_flash},
{"power_request", cmd_power_request},
{"power_release", cmd_power_release},
{NULL, NULL}
};
static const ShellConfig shell_cfg1 =
{
(BaseSequentialStream *)&SDU,
commands
};
/*===========================================================================*/
/* Generic code. */
/*===========================================================================*/
/*
* Red LED blinker thread, times are in milliseconds.
*/
class blinker_thd : public BaseStaticThread<128>
{
protected:
virtual msg_t main(void)
{
systime_t time;
setName("blinker");
while (true)
{
// BTW: For some reason this will never become anything else after cable disconnect
if (SDU.config->usbp->state == USB_ACTIVE)
{
time = 250;
}
else
{
time = 500;
}
board_red_led(PAL_LOW);
sleep(time);
board_red_led(PAL_HIGH);
sleep(time);
}
// TODO: How to get rid of the compiler warning ? the demos do not return anything from their blinkers either...
// warning: no return statement in function returning non-void [-Wreturn-type]
}
public:
// Empty constructor
blinker_thd(void) { }
};
static blinker_thd blinky;
/*
* Application entry point.
*/
int main(void)
{
Thread *shelltp = NULL;
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
System::init();
/*
* Initializes a serial-over-USB CDC driver.
*/
usb_serial_init();
/*
* Shell manager initialization.
*/
shellInit();
/*
* Creates the blinker thread.
*/
blinky.start(NORMALPRIO);
palClearPad(GPIOB, GPIOB_LED2);
/*
* Normal main() thread activity, in this demo it does nothing except
* sleeping in a loop and check the USB state.
*/
// TODO: Not really important but would be interesting from learing POV, how to make the shell a dynamic c++ thread
while (TRUE)
{
// If USB is connected and we have no shell, create one
if ( !shelltp
&& (SDU.config->usbp->state == USB_ACTIVE))
{
board_green_led(PAL_HIGH);
shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO);
}
// If USB connection is lost, tell the shell to terminate, however it seems the state will always stay at active
if ( shelltp
&& !chThdTerminated(shelltp)
&& !(SDU.config->usbp->state == USB_ACTIVE))
{
chThdTerminate(shelltp);
}
// Wait for the thread to terminate and release the memory it used
if ( shelltp
&& chThdTerminated(shelltp))
{
chThdRelease(shelltp); /* Recovers memory of the previous shell. */
shelltp = NULL; /* Triggers spawning of a new shell. */
board_green_led(PAL_LOW);
}
BaseThread::sleep(MS2ST(500));
}
return 0;
}