amiro-os / core / src / aos_system.c @ 9506caa7
History | View | Annotate | Download (39.11 KB)
1 |
/*
|
---|---|
2 |
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2020 Thomas Schöpping et al.
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify
|
6 |
it under the terms of the GNU General Public License as published by
|
7 |
the Free Software Foundation, either version 3 of the License, or
|
8 |
(at your option) any later version.
|
9 |
|
10 |
This program is distributed in the hope that it will be useful,
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
GNU General Public License for more details.
|
14 |
|
15 |
You should have received a copy of the GNU General Public License
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
*/
|
18 |
|
19 |
/**
|
20 |
* @file aos_system.c
|
21 |
* @brief System code.
|
22 |
* @details Contains system initialization and shutdown routines
|
23 |
* and system shell commands.
|
24 |
*
|
25 |
* @addtogroup aos_system
|
26 |
* @{
|
27 |
*/
|
28 |
|
29 |
#include <amiroos.h> |
30 |
#include <stdarg.h> |
31 |
#include <string.h> |
32 |
#include <stdlib.h> |
33 |
|
34 |
#if (AMIROOS_CFG_TESTS_ENABLE == true) |
35 |
#include <ch_test.h> |
36 |
#include <rt_test_root.h> |
37 |
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */ |
38 |
|
39 |
/******************************************************************************/
|
40 |
/* LOCAL DEFINITIONS */
|
41 |
/******************************************************************************/
|
42 |
|
43 |
/**
|
44 |
* @brief Period of the system timer.
|
45 |
*/
|
46 |
#define SYSTIMER_PERIOD (TIME_MAX_SYSTIME - CH_CFG_ST_TIMEDELTA)
|
47 |
|
48 |
/**
|
49 |
* @brief Width of the printable system info text.
|
50 |
*/
|
51 |
#define SYSTEM_INFO_WIDTH 80 |
52 |
|
53 |
/**
|
54 |
* @brief Width of the name column of the system info table.
|
55 |
*/
|
56 |
#define SYSTEM_INFO_NAMEWIDTH 20 |
57 |
|
58 |
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
59 |
|
60 |
/**
|
61 |
* @brief Number of entries in the system shell input buffer.
|
62 |
*/
|
63 |
#define SYSTEM_SHELL_BUFFERENTRIES (1 + AMIROOS_CFG_SHELL_HISTLENGTH) |
64 |
|
65 |
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
66 |
|
67 |
/******************************************************************************/
|
68 |
/* EXPORTED VARIABLES */
|
69 |
/******************************************************************************/
|
70 |
|
71 |
/**
|
72 |
* @brief Global system object.
|
73 |
*/
|
74 |
aos_system_t aos; |
75 |
|
76 |
/******************************************************************************/
|
77 |
/* LOCAL TYPES */
|
78 |
/******************************************************************************/
|
79 |
|
80 |
/******************************************************************************/
|
81 |
/* LOCAL VARIABLES */
|
82 |
/******************************************************************************/
|
83 |
|
84 |
/*
|
85 |
* forward declarations
|
86 |
*/
|
87 |
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
88 |
static int _shellcmd_configcb(BaseSequentialStream* stream, int argc, char* argv[]); |
89 |
static int _shellcmd_infocb(BaseSequentialStream* stream, int argc, char* argv[]); |
90 |
static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* argv[]); |
91 |
#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE)) || defined(__DOXYGEN__)
|
92 |
static int _shellcmd_cpuloadcb(BaseSequentialStream* stream, int argc, char* argv[]); |
93 |
#endif /* (CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE) */ |
94 |
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
95 |
static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char* argv[]); |
96 |
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */ |
97 |
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
98 |
|
99 |
/**
|
100 |
* @brief Timer to accumulate system uptime.
|
101 |
*/
|
102 |
static virtual_timer_t _systimer;
|
103 |
|
104 |
/**
|
105 |
* @brief Accumulated system uptime.
|
106 |
*/
|
107 |
static aos_timestamp_t _uptime;
|
108 |
|
109 |
/**
|
110 |
* @brief Timer register value of last accumulation.
|
111 |
*/
|
112 |
static systime_t _synctime;
|
113 |
|
114 |
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
115 |
|
116 |
/**
|
117 |
* @brief System shell name.
|
118 |
*/
|
119 |
static const char _shell_name[] = "system shell"; |
120 |
|
121 |
/**
|
122 |
* @brief Shell thread working area.
|
123 |
*/
|
124 |
static THD_WORKING_AREA(_shell_wa, AMIROOS_CFG_SHELL_STACKSIZE);
|
125 |
|
126 |
/**
|
127 |
* @brief Shell input buffer.
|
128 |
*/
|
129 |
static char _shell_buffer[SYSTEM_SHELL_BUFFERENTRIES * AMIROOS_CFG_SHELL_LINEWIDTH]; |
130 |
|
131 |
/**
|
132 |
* @brief Shell command to retrieve system information.
|
133 |
*/
|
134 |
static AOS_SHELL_COMMAND(_shellcmd_info, "module:info", _shellcmd_infocb); |
135 |
|
136 |
/**
|
137 |
* @brief Shell command to set or retrieve system configuration.
|
138 |
*/
|
139 |
static AOS_SHELL_COMMAND(_shellcmd_config, "module:config", _shellcmd_configcb); |
140 |
|
141 |
/**
|
142 |
* @brief Shell command to shutdown the system.
|
143 |
*/
|
144 |
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__) |
145 |
static AOS_SHELL_COMMAND(_shellcmd_shutdown, "system:shutdown", _shellcmd_shutdowncb); |
146 |
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
147 |
static AOS_SHELL_COMMAND(_shellcmd_shutdown, "module:shutdown", _shellcmd_shutdowncb); |
148 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
149 |
|
150 |
#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE)) || defined(__DOXYGEN__)
|
151 |
|
152 |
/**
|
153 |
* @brief Shell command to read out CPU load.
|
154 |
*/
|
155 |
static AOS_SHELL_COMMAND(_shellcmd_cpuload, "module:cpuload", _shellcmd_cpuloadcb); |
156 |
|
157 |
#endif /* (CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE) */ |
158 |
|
159 |
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
160 |
|
161 |
/**
|
162 |
* @brief Shell command to run a test of the ChibiOS/RT kernel.
|
163 |
*/
|
164 |
static AOS_SHELL_COMMAND(_shellcmd_kerneltest, "kernel:test", _shellcmd_kerneltestcb); |
165 |
|
166 |
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */ |
167 |
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
168 |
|
169 |
/******************************************************************************/
|
170 |
/* LOCAL FUNCTIONS */
|
171 |
/******************************************************************************/
|
172 |
|
173 |
/**
|
174 |
* @brief Print a separator line.
|
175 |
*
|
176 |
* @param[in] stream Stream to print to or NULL to print to all system streams.
|
177 |
* @param[in] c Character to use.
|
178 |
* @param[in] n Length of the separator line.
|
179 |
*
|
180 |
* @return Number of characters printed.
|
181 |
*/
|
182 |
static unsigned int _printSystemInfoSeparator(BaseSequentialStream* stream, const char c, const unsigned int n) |
183 |
{ |
184 |
aosDbgCheck(stream != NULL);
|
185 |
|
186 |
// print the specified character n times
|
187 |
for (unsigned int i = 0; i < n; ++i) { |
188 |
streamPut(stream, (uint8_t)c); |
189 |
} |
190 |
streamPut(stream, '\n');
|
191 |
|
192 |
return n+1; |
193 |
} |
194 |
|
195 |
/**
|
196 |
* @brief Print a system information line.
|
197 |
* @details Prints a system information line with the following format:
|
198 |
* "<name>[spaces]fmt"
|
199 |
* The combined width of "<name>[spaces]" can be specified in order to align <fmt> on multiple lines.
|
200 |
* Note that there is not trailing newline added implicitely.
|
201 |
*
|
202 |
* @param[in] stream Stream to print to or NULL to print to all system streams.
|
203 |
* @param[in] name Name of the entry/line.
|
204 |
* @param[in] namewidth Minimum width of the name column.
|
205 |
* @param[in] fmt Formatted string of information content.
|
206 |
*
|
207 |
* @return Number of characters printed.
|
208 |
*/
|
209 |
static unsigned int _printSystemInfoLine(BaseSequentialStream* stream, const char* name, const unsigned int namewidth, const char* fmt, ...) |
210 |
{ |
211 |
aosDbgCheck(stream != NULL);
|
212 |
aosDbgCheck(name != NULL);
|
213 |
|
214 |
unsigned int n = 0; |
215 |
va_list ap; |
216 |
|
217 |
va_start(ap, fmt); |
218 |
n += (unsigned int)chprintf(stream, name); |
219 |
// print at least a single space character
|
220 |
do {
|
221 |
streamPut(stream, ' ');
|
222 |
++n; |
223 |
} while (n < namewidth);
|
224 |
n += (unsigned int)chvprintf(stream, fmt, ap); |
225 |
va_end(ap); |
226 |
|
227 |
streamPut(stream, '\n');
|
228 |
++n; |
229 |
|
230 |
return n;
|
231 |
} |
232 |
|
233 |
/**
|
234 |
* @brief Prints information about the system.
|
235 |
*
|
236 |
* @param[in] stream Stream to print to.
|
237 |
*/
|
238 |
static void _printSystemInfo(BaseSequentialStream* stream) |
239 |
{ |
240 |
aosDbgCheck(stream != NULL);
|
241 |
|
242 |
// local variables
|
243 |
#if (HAL_USE_RTC == TRUE)
|
244 |
struct tm dt;
|
245 |
aosSysGetDateTime(&dt); |
246 |
#endif /* (HAL_USE_RTC == TRUE) */ |
247 |
|
248 |
// print static information about module and operating system
|
249 |
_printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
|
250 |
_printSystemInfoLine(stream, "Module", SYSTEM_INFO_NAMEWIDTH, "%s", BOARD_NAME); |
251 |
#if defined(PLATFORM_NAME)
|
252 |
_printSystemInfoLine(stream, "Platform", SYSTEM_INFO_NAMEWIDTH, "%s", PLATFORM_NAME); |
253 |
#endif /* defined(PLATFORM_NAME) */ |
254 |
#if defined(PORT_CORE_VARIANT_NAME)
|
255 |
_printSystemInfoLine(stream, "Core Variant", SYSTEM_INFO_NAMEWIDTH, "%s", PORT_CORE_VARIANT_NAME); |
256 |
#endif /* defined(PORT_CORE_VARIANT_NAME) */ |
257 |
_printSystemInfoLine(stream, "Architecture", SYSTEM_INFO_NAMEWIDTH, "%s", PORT_ARCHITECTURE_NAME); |
258 |
_printSystemInfoLine(stream, "Core Frequency", SYSTEM_INFO_NAMEWIDTH, "%u MHz", SystemCoreClock / 1000000); |
259 |
_printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
|
260 |
#if (AMIROOS_CFG_SSSP_ENABLE == true) |
261 |
_printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE, AOS_SSSP_VERSION_MAJOR, AOS_SSSP_VERSION_MINOR); |
262 |
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
263 |
_printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE); |
264 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
265 |
_printSystemInfoLine(stream, "AMiRo-LLD" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (periphAL %u.%u)", AMIROLLD_VERSION_MAJOR, AMIROLLD_VERSION_MINOR, AMIROLLD_VERSION_PATCH, AMIROLLD_RELEASE_TYPE, PERIPHAL_VERSION_MAJOR, PERIPHAL_VERSION_MINOR); |
266 |
_printSystemInfoLine(stream, "ChibiOS/RT" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s", CH_KERNEL_MAJOR, CH_KERNEL_MINOR, CH_KERNEL_PATCH, (CH_KERNEL_STABLE == 1) ? "stable" : "non-stable"); |
267 |
_printSystemInfoLine(stream, "ChibiOS/HAL", SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s", CH_HAL_MAJOR, CH_HAL_MINOR, CH_HAL_PATCH, (CH_HAL_STABLE == 1) ? "stable" : "non-stable"); |
268 |
_printSystemInfoLine(stream, "build type", SYSTEM_INFO_NAMEWIDTH,"%s", (AMIROOS_CFG_DBG == true) ? "debug" : "release"); |
269 |
_printSystemInfoLine(stream, "Compiler" , SYSTEM_INFO_NAMEWIDTH, "%s %u.%u.%u", "GCC", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); // TODO: support other compilers than GCC |
270 |
_printSystemInfoLine(stream, "Compiled" , SYSTEM_INFO_NAMEWIDTH, "%s - %s", __DATE__, __TIME__); |
271 |
|
272 |
// print static information about the bootloader
|
273 |
#if (AMIROOS_CFG_BOOTLOADER != AOS_BOOTLOADER_NONE)
|
274 |
_printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
|
275 |
#endif /* (AMIROOS_CFG_BOOTLOADER != AOS_BOOTLOADER_NONE) */ |
276 |
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
|
277 |
if (BL_CALLBACK_TABLE_ADDRESS->magicNumber == BL_MAGIC_NUMBER) {
|
278 |
_printSystemInfoLine(stream, "AMiRo-BLT", SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)", BL_CALLBACK_TABLE_ADDRESS->vBootloader.major, BL_CALLBACK_TABLE_ADDRESS->vBootloader.minor, BL_CALLBACK_TABLE_ADDRESS->vBootloader.patch, |
279 |
(BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Release) ? "stable" :
|
280 |
(BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_ReleaseCandidate) ? "release candidate" :
|
281 |
(BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Beta) ? "beta" :
|
282 |
(BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Alpha) ? "alpha" :
|
283 |
(BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_PreAlpha) ? "pre-alpha" :
|
284 |
"<release type unknown>",
|
285 |
BL_CALLBACK_TABLE_ADDRESS->vSSSP.major, BL_CALLBACK_TABLE_ADDRESS->vSSSP.minor); |
286 |
#if (AMIROOS_CFG_SSSP_ENABLE == true) |
287 |
if (BL_CALLBACK_TABLE_ADDRESS->vSSSP.major != AOS_SSSP_VERSION_MAJOR) {
|
288 |
const char* msg = "WARNING: AMiRo-BLT and AMiRo-OS implement incompatible SSSP versions!\n"; |
289 |
stream ? chprintf(stream, msg) : aosprintf(msg); |
290 |
} |
291 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
292 |
_printSystemInfoLine(stream, "Compiler", SYSTEM_INFO_NAMEWIDTH, "%s %u.%u.%u", (BL_CALLBACK_TABLE_ADDRESS->vCompiler.identifier == BL_VERSION_ID_GCC) ? "GCC" : "<compiler unknown>", BL_CALLBACK_TABLE_ADDRESS->vCompiler.major, BL_CALLBACK_TABLE_ADDRESS->vCompiler.minor, BL_CALLBACK_TABLE_ADDRESS->vCompiler.patch); // TODO: support other compilers than GCC |
293 |
} else {
|
294 |
const char* msg = "WARNING: AMiRo-BLT incompatible or not available.\n"; |
295 |
stream ? chprintf(stream, "%s", msg) : aosprintf("%s", msg); |
296 |
} |
297 |
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */ |
298 |
|
299 |
#if defined(AMIROOS_CFG_SYSINFO_HOOK)
|
300 |
// print module specific information
|
301 |
_printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
|
302 |
AMIROOS_CFG_SYSINFO_HOOK(); |
303 |
#endif /* defined(AMIROOS_CFG_SYSINFO_HOOK) */ |
304 |
|
305 |
// print dynamic information about the module
|
306 |
_printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
|
307 |
#if (AMIROOS_CFG_SSSP_ENABLE == true) |
308 |
if (aos.sssp.moduleId != 0) { |
309 |
_printSystemInfoLine(stream, "Module ID", SYSTEM_INFO_NAMEWIDTH, "%u", aos.sssp.moduleId); |
310 |
} else {
|
311 |
_printSystemInfoLine(stream, "Module ID", SYSTEM_INFO_NAMEWIDTH, "not available"); |
312 |
} |
313 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
314 |
#if (HAL_USE_RTC == TRUE)
|
315 |
_printSystemInfoLine(stream, "Date", SYSTEM_INFO_NAMEWIDTH, "%04u-%02u-%02u (%s)", |
316 |
dt.tm_year + 1900,
|
317 |
dt.tm_mon + 1,
|
318 |
dt.tm_mday, |
319 |
(dt.tm_wday == 0) ? "Sunday" : (dt.tm_wday == 1) ? "Monday" : (dt.tm_wday == 2) ? "Tuesday" : (dt.tm_wday == 3) ? "Wednesday" : (dt.tm_wday == 4) ? "Thursday" : (dt.tm_wday == 5) ? "Friday" : "Saturday"); |
320 |
_printSystemInfoLine(stream, "Time", SYSTEM_INFO_NAMEWIDTH, "%02u:%02u:%02u", dt.tm_hour, dt.tm_min, dt.tm_sec); |
321 |
#endif /* (HAL_USE_RTC == TRUE) */ |
322 |
|
323 |
_printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
|
324 |
|
325 |
return;
|
326 |
} |
327 |
|
328 |
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
329 |
/**
|
330 |
* @brief Callback function for the system:config shell command.
|
331 |
*
|
332 |
* @param[in] stream The I/O stream to use.
|
333 |
* @param[in] argc Number of arguments.
|
334 |
* @param[in] argv List of pointers to the arguments.
|
335 |
*
|
336 |
* @return An exit status.
|
337 |
* @retval AOS_OK The command was executed successfuly.
|
338 |
* @retval AOS_INVALIDARGUMENTS There was an issue with the arguemnts.
|
339 |
*/
|
340 |
static int _shellcmd_configcb(BaseSequentialStream* stream, int argc, char* argv[]) |
341 |
{ |
342 |
aosDbgCheck(stream != NULL);
|
343 |
|
344 |
// local variables
|
345 |
int retval = AOS_INVALIDARGUMENTS;
|
346 |
|
347 |
// if there are additional arguments
|
348 |
if (argc > 1) { |
349 |
// if the user wants to set or retrieve the shell configuration
|
350 |
if (strcmp(argv[1], "--shell") == 0) { |
351 |
// if the user wants to modify the shell configuration
|
352 |
if (argc > 2) { |
353 |
// if the user wants to modify the prompt
|
354 |
if (strcmp(argv[2], "prompt") == 0) { |
355 |
// there must be a further argument
|
356 |
if (argc > 3) { |
357 |
// handle the option
|
358 |
if (strcmp(argv[3], "text") == 0) { |
359 |
aos.shell.config &= ~AOS_SHELL_CONFIG_PROMPT_MINIMAL; |
360 |
retval = AOS_OK; |
361 |
} |
362 |
else if (strcmp(argv[3], "minimal") == 0) { |
363 |
aos.shell.config |= AOS_SHELL_CONFIG_PROMPT_MINIMAL; |
364 |
retval = AOS_OK; |
365 |
} |
366 |
else if (strcmp(argv[3], "notime") == 0) { |
367 |
aos.shell.config &= ~(AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME); |
368 |
retval = AOS_OK; |
369 |
} |
370 |
else if (strcmp(argv[3], "uptime") == 0) { |
371 |
aos.shell.config &= ~AOS_SHELL_CONFIG_PROMPT_DATETIME; |
372 |
aos.shell.config |= AOS_SHELL_CONFIG_PROMPT_UPTIME; |
373 |
retval = AOS_OK; |
374 |
} |
375 |
else if (strcmp(argv[3], "date&time") == 0) { |
376 |
aos.shell.config &= ~AOS_SHELL_CONFIG_PROMPT_UPTIME; |
377 |
aos.shell.config |= AOS_SHELL_CONFIG_PROMPT_DATETIME; |
378 |
retval = AOS_OK; |
379 |
} |
380 |
else {
|
381 |
chprintf(stream, "unknown option '%s'\n", argv[3]); |
382 |
return AOS_INVALIDARGUMENTS;
|
383 |
} |
384 |
} |
385 |
} |
386 |
// if the user wants to modify the string matching
|
387 |
else if (strcmp(argv[2], "match") == 0) { |
388 |
// there must be a further argument
|
389 |
if (argc > 3) { |
390 |
if (strcmp(argv[3], "casesensitive") == 0) { |
391 |
aos.shell.config |= AOS_SHELL_CONFIG_MATCH_CASE; |
392 |
retval = AOS_OK; |
393 |
} |
394 |
else if (strcmp(argv[3], "caseinsensitive") == 0) { |
395 |
aos.shell.config &= ~AOS_SHELL_CONFIG_MATCH_CASE; |
396 |
retval = AOS_OK; |
397 |
} |
398 |
} |
399 |
} |
400 |
} |
401 |
// if the user wants to retrieve the shell configuration
|
402 |
else {
|
403 |
chprintf(stream, "current shell configuration:\n");
|
404 |
chprintf(stream, "\tprompt text: %s\n",
|
405 |
(aos.shell.prompt != NULL) ? aos.shell.prompt : "n/a"); |
406 |
char time[10]; |
407 |
switch (aos.shell.config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
|
408 |
case AOS_SHELL_CONFIG_PROMPT_UPTIME:
|
409 |
strcpy(time, "uptime"); break; |
410 |
case AOS_SHELL_CONFIG_PROMPT_DATETIME:
|
411 |
strcpy(time, "date&time"); break; |
412 |
default:
|
413 |
strcpy(time, "no time"); break; |
414 |
} |
415 |
chprintf(stream, "\tprompt style: %s, %s\n",
|
416 |
(aos.shell.config & AOS_SHELL_CONFIG_PROMPT_MINIMAL) ? "minimal" : "text", |
417 |
time); |
418 |
chprintf(stream, "\tinput method: %s\n",
|
419 |
(aos.shell.config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) ? "replace" : "insert"); |
420 |
chprintf(stream, "\ttext matching: %s\n",
|
421 |
(aos.shell.config & AOS_SHELL_CONFIG_MATCH_CASE) ? "case sensitive" : "case insensitive"); |
422 |
retval = AOS_OK; |
423 |
} |
424 |
} |
425 |
# if (HAL_USE_RTC == TRUE)
|
426 |
// if the user wants to configure the date or time
|
427 |
else if (strcmp(argv[1], "--date&time") == 0 && argc == 4) { |
428 |
struct tm dt;
|
429 |
aosSysGetDateTime(&dt); |
430 |
int val = atoi(argv[3]); |
431 |
if (strcmp(argv[2], "year") == 0 && val >= 1900) { |
432 |
dt.tm_year = val - 1900;
|
433 |
} |
434 |
else if (strcmp(argv[2], "month") == 0 && val > 0 && val <= 12) { |
435 |
dt.tm_mon = val - 1;
|
436 |
} |
437 |
else if (strcmp(argv[2], "day") == 0 && val > 0 && val <= 31) { |
438 |
dt.tm_mday = val; |
439 |
} |
440 |
else if (strcmp(argv[2], "hour") == 0 && val >= 0 && val < 24) { |
441 |
dt.tm_hour = val; |
442 |
} |
443 |
else if (strcmp(argv[2], "minute") == 0 && val >= 0 && val < 60) { |
444 |
dt.tm_min = val; |
445 |
} |
446 |
else if (strcmp(argv[2], "second") == 0 && val >= 0 && val < 60) { |
447 |
dt.tm_sec = val; |
448 |
} |
449 |
else {
|
450 |
chprintf(stream, "unknown option '%s' or invalid value '%s'\n", argv[2], argv[3]); |
451 |
return AOS_INVALIDARGUMENTS;
|
452 |
} |
453 |
dt.tm_wday = aosTimeDayOfWeekFromDate((uint16_t)dt.tm_mday, (uint8_t)dt.tm_mon+1, (uint16_t)dt.tm_year+1900) % DAYS_PER_WEEK; |
454 |
aosSysSetDateTime(&dt); |
455 |
|
456 |
// read and print new date and time
|
457 |
aosSysGetDateTime(&dt); |
458 |
chprintf(stream, "date/time set to %04u-%02u-%02u %02u:%02u:%02u\n",
|
459 |
dt.tm_year+1900, dt.tm_mon+1, dt.tm_mday, |
460 |
dt.tm_hour, dt.tm_min, dt.tm_sec); |
461 |
|
462 |
retval = AOS_OK; |
463 |
} |
464 |
#endif /* (HAL_USE_RTC == TRUE) */ |
465 |
} |
466 |
|
467 |
// print help, if required
|
468 |
if (retval == AOS_INVALIDARGUMENTS) {
|
469 |
chprintf(stream, "Usage: %s OPTION\n", argv[0]); |
470 |
chprintf(stream, "Options:\n");
|
471 |
chprintf(stream, " --help\n");
|
472 |
chprintf(stream, " Print this help text.\n");
|
473 |
chprintf(stream, " --shell [OPT [VAL]]\n");
|
474 |
chprintf(stream, " Set or retrieve shell configuration.\n");
|
475 |
chprintf(stream, " Possible OPTs and VALs are:\n");
|
476 |
chprintf(stream, " prompt text|minimal|uptime|date&time|notime\n");
|
477 |
chprintf(stream, " Configures the prompt.\n");
|
478 |
chprintf(stream, " match casesensitive|caseinsenitive\n");
|
479 |
chprintf(stream, " Configures string matching.\n");
|
480 |
#if (HAL_USE_RTC == TRUE)
|
481 |
chprintf(stream, " --date&time OPT VAL\n");
|
482 |
chprintf(stream, " Set the date/time value of OPT to VAL.\n");
|
483 |
chprintf(stream, " Possible OPTs are:\n");
|
484 |
chprintf(stream, " year\n");
|
485 |
chprintf(stream, " month\n");
|
486 |
chprintf(stream, " day\n");
|
487 |
chprintf(stream, " hour\n");
|
488 |
chprintf(stream, " minute\n");
|
489 |
chprintf(stream, " second\n");
|
490 |
#endif /* (HAL_USE_RTC == TRUE) */ |
491 |
} |
492 |
|
493 |
return (argc > 1 && strcmp(argv[1], "--help") == 0) ? AOS_OK : retval; |
494 |
} |
495 |
|
496 |
/**
|
497 |
* @brief Callback function for the system:info shell command.
|
498 |
*
|
499 |
* @param[in] stream The I/O stream to use.
|
500 |
* @param[in] argc Number of arguments.
|
501 |
* @param[in] argv List of pointers to the arguments.
|
502 |
*
|
503 |
* @return An exit status.
|
504 |
* @retval AOS_OK The command was executed successfully.
|
505 |
*/
|
506 |
static int _shellcmd_infocb(BaseSequentialStream* stream, int argc, char* argv[]) |
507 |
{ |
508 |
aosDbgCheck(stream != NULL);
|
509 |
|
510 |
(void)argc;
|
511 |
(void)argv;
|
512 |
|
513 |
// print system information
|
514 |
_printSystemInfo(stream); |
515 |
|
516 |
// print time measurement precision
|
517 |
chprintf(stream, "module time resolution: %uus\n", AOS_SYSTEM_TIME_RESOLUTION);
|
518 |
|
519 |
// print system uptime
|
520 |
aos_timestamp_t uptime; |
521 |
aosSysGetUptime(&uptime); |
522 |
chprintf(stream, "The system is running for\n");
|
523 |
chprintf(stream, "%10u days\n", (uint32_t)(uptime / MICROSECONDS_PER_DAY));
|
524 |
chprintf(stream, "%10u hours\n", (uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR));
|
525 |
chprintf(stream, "%10u minutes\n", (uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE));
|
526 |
chprintf(stream, "%10u seconds\n", (uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND));
|
527 |
chprintf(stream, "%10u milliseconds\n", (uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND));
|
528 |
chprintf(stream, "%10u microseconds\n", (uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND));
|
529 |
#if (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true) |
530 |
chprintf(stream, "SSSP synchronization offset: %.3fus per %uus\n", (double)aosSsspGetSyncSkew(), AMIROOS_CFG_SSSP_SYSSYNCPERIOD); |
531 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MASTER != true) && (AMIROOS_CFG_PROFILE == true) */ |
532 |
_printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
|
533 |
|
534 |
// print shell info
|
535 |
chprintf(stream, "System shell information:\n");
|
536 |
chprintf(stream, "\tcommands available: %u\n", aosShellCountCommands(&aos.shell));
|
537 |
chprintf(stream, "\tinput width: %u characters\n", aos.shell.input.linewidth);
|
538 |
chprintf(stream, "\tmaximum arguments: %u\n", aos.shell.input.nargs);
|
539 |
chprintf(stream, "\thistory size: %u\n", aos.shell.input.nentries - 1); |
540 |
#if (AMIROOS_CFG_DBG == true) |
541 |
chprintf(stream, "\tthread stack size: %u bytes\n", aosThdGetStacksize(aos.shell.thread));
|
542 |
#if (CH_DBG_FILL_THREADS == TRUE)
|
543 |
chprintf(stream, "\tstack peak utilization: %u bytes (%.2f%%)\n", aosThdGetStackPeakUtilization(aos.shell.thread), (double)((float)(aosThdGetStackPeakUtilization(aos.shell.thread)) / (float)(aosThdGetStacksize(aos.shell.thread)) * 100.0f)); |
544 |
#endif /* (CH_DBG_FILL_THREADS == TRUE) */ |
545 |
#endif /* (AMIROOS_CFG_DBG == true) */ |
546 |
_printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
|
547 |
|
548 |
return AOS_OK;
|
549 |
} |
550 |
|
551 |
/**
|
552 |
* @brief Callback function for the sytem:shutdown or module:shutdown shell command.
|
553 |
*
|
554 |
* @param[in] stream The I/O stream to use.
|
555 |
* @param[in] argc Number of arguments.
|
556 |
* @param[in] argv List of pointers to the arguments.
|
557 |
*
|
558 |
* @return An exit status.
|
559 |
* @retval AOS_OK The command was executed successfully.
|
560 |
* @retval AOS_INVALIDARGUMENTS There was an issue with the arguments.
|
561 |
*/
|
562 |
static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* argv[]) |
563 |
{ |
564 |
aosDbgCheck(stream != NULL);
|
565 |
|
566 |
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
|
567 |
|
568 |
(void)argv;
|
569 |
|
570 |
if (argc != 1) { |
571 |
// error
|
572 |
chprintf(stream, "ERROR: no arguments allowed.\n");
|
573 |
return AOS_INVALIDARGUMENTS;
|
574 |
} else {
|
575 |
// broadcast shutdown event
|
576 |
chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_MASK); |
577 |
// set terminate flag so no further prompt will be printed
|
578 |
chThdTerminate(chThdGetSelfX()); |
579 |
return AOS_OK;
|
580 |
} |
581 |
|
582 |
#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
|
583 |
|
584 |
// print help text
|
585 |
if (argc != 2 || strcmp(argv[1], "--help") == 0) { |
586 |
chprintf(stream, "Usage: %s OPTION\n", argv[0]); |
587 |
chprintf(stream, "Options:\n");
|
588 |
chprintf(stream, " --help\n");
|
589 |
chprintf(stream, " Print this help text.\n");
|
590 |
chprintf(stream, " --hibernate, -h\n");
|
591 |
chprintf(stream, " Shutdown to hibernate mode.\n");
|
592 |
chprintf(stream, " Least energy saving, but allows charging via pins.\n");
|
593 |
chprintf(stream, " --deepsleep, -d\n");
|
594 |
chprintf(stream, " Shutdown to deepsleep mode.\n");
|
595 |
chprintf(stream, " Minimum energy consumption while allowing charging via plug.\n");
|
596 |
chprintf(stream, " --transportation, -t\n");
|
597 |
chprintf(stream, " Shutdown to transportation mode.\n");
|
598 |
chprintf(stream, " Minimum energy consumption with all interrupts disabled (no charging).\n");
|
599 |
chprintf(stream, " --restart, -r\n");
|
600 |
chprintf(stream, " Shutdown and restart system.\n");
|
601 |
|
602 |
return (argc != 2) ? AOS_INVALIDARGUMENTS : AOS_OK; |
603 |
} |
604 |
// handle argument
|
605 |
else {
|
606 |
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--hibernate") == 0) { |
607 |
// broadcast shutdown event
|
608 |
chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_HIBERNATE); |
609 |
// set terminate flag so no further prompt will be printed
|
610 |
chThdTerminate(chThdGetSelfX()); |
611 |
return AOS_OK;
|
612 |
} |
613 |
else if (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--deepsleep") == 0) { |
614 |
// broadcast shutdown event
|
615 |
chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_DEEPSLEEP); |
616 |
// set terminate flag so no further prompt will be printed
|
617 |
chThdTerminate(chThdGetSelfX()); |
618 |
return AOS_OK;
|
619 |
} |
620 |
else if (strcmp(argv[1], "-t") == 0 || strcmp(argv[1], "--transportation") == 0) { |
621 |
// broadcast shutdown event
|
622 |
chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_TRANSPORTATION); |
623 |
// set terminate flag so no further prompt will be printed
|
624 |
chThdTerminate(chThdGetSelfX()); |
625 |
return AOS_OK;
|
626 |
} |
627 |
else if (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--restart") == 0) { |
628 |
// broadcast shutdown event
|
629 |
chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_RESTART); |
630 |
// set terminate flag so no further prompt will be printed
|
631 |
chThdTerminate(chThdGetSelfX()); |
632 |
return AOS_OK;
|
633 |
} |
634 |
else {
|
635 |
chprintf(stream, "unknown argument %s\n", argv[1]); |
636 |
return AOS_INVALIDARGUMENTS;
|
637 |
} |
638 |
} |
639 |
|
640 |
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */ |
641 |
} |
642 |
|
643 |
#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE)) || defined(__DOXYGEN__)
|
644 |
|
645 |
/**
|
646 |
* @brief Callback function for the module:cpuload shell command.
|
647 |
*
|
648 |
* @param[in] stream The I/O stream to use.
|
649 |
* @param[in] argc Number of arguments.
|
650 |
* @param[in] argv List of pointers to arguments.
|
651 |
*
|
652 |
* @return An exit status.
|
653 |
*/
|
654 |
static int _shellcmd_cpuloadcb(BaseSequentialStream* stream, int argc, char* argv[]) |
655 |
{ |
656 |
aosDbgCheck(stream != NULL);
|
657 |
|
658 |
(void)argc;
|
659 |
(void)argv;
|
660 |
|
661 |
// local variables
|
662 |
rttime_t sum = 0;
|
663 |
thread_t* thd = chRegFirstThread(); |
664 |
|
665 |
// accumulate system load
|
666 |
do {
|
667 |
sum += thd->stats.cumulative; |
668 |
thd = chRegNextThread(thd); |
669 |
} while (thd);
|
670 |
sum += ch.kernel_stats.m_crit_thd.cumulative; |
671 |
sum += ch.kernel_stats.m_crit_isr.cumulative; |
672 |
|
673 |
// retreive, calculate and print performance measures
|
674 |
chprintf(stream, "threads & critical zones:\n");
|
675 |
thd = chRegFirstThread(); |
676 |
do {
|
677 |
chprintf(stream, "\t%22s: %6.2f%%\n",
|
678 |
(thd->name != NULL) ? thd->name : "<unnamed thread>", |
679 |
(double)((float)thd->stats.cumulative / (float)sum * 100.f)); |
680 |
thd = chRegNextThread(thd); |
681 |
} while (thd);
|
682 |
chprintf(stream, "\t%22s: %6.2f%%\n",
|
683 |
"thread critical zones",
|
684 |
(double)((float)ch.kernel_stats.m_crit_thd.cumulative / (float)sum * 100.f)); |
685 |
chprintf(stream, "\t%22s: %6.2f%%\n",
|
686 |
"ISR critical zones",
|
687 |
(double)((float)ch.kernel_stats.m_crit_isr.cumulative / (float)sum * 100.f)); |
688 |
|
689 |
// retreive further real-time statistics
|
690 |
chprintf(stream, "\nworst critical zones:\n");
|
691 |
chprintf(stream, "\tthreads: %uus (%u clock cycles)\n",
|
692 |
RTC2US(SystemCoreClock, ch.kernel_stats.m_crit_thd.worst), |
693 |
ch.kernel_stats.m_crit_thd.worst); |
694 |
chprintf(stream, "\t ISRs: %uus (%u clock cycles)\n",
|
695 |
RTC2US(SystemCoreClock, ch.kernel_stats.m_crit_isr.worst), |
696 |
ch.kernel_stats.m_crit_isr.worst); |
697 |
|
698 |
return 0; |
699 |
} |
700 |
|
701 |
#endif /* (CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE) */ |
702 |
|
703 |
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
704 |
|
705 |
/**
|
706 |
* @brief Callback function for the kernel:test shell command.
|
707 |
*
|
708 |
* @param[in] stream The I/O stream to use.
|
709 |
* @param[in] argc Number of arguments.
|
710 |
* @param[in] argv List of pointers to the arguments.
|
711 |
*
|
712 |
* @return An exit status.
|
713 |
*/
|
714 |
static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char* argv[]) |
715 |
{ |
716 |
aosDbgCheck(stream != NULL);
|
717 |
|
718 |
(void)argc;
|
719 |
(void)argv;
|
720 |
|
721 |
msg_t retval = test_execute(stream, &rt_test_suite); |
722 |
|
723 |
return retval;
|
724 |
} |
725 |
|
726 |
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */ |
727 |
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
728 |
|
729 |
// suppress warning in case no interrupt GPIOs are defined
|
730 |
#pragma GCC diagnostic push
|
731 |
#pragma GCC diagnostic ignored "-Wunused-function" |
732 |
/**
|
733 |
* @brief Generic callback function for GPIO interrupts.
|
734 |
*
|
735 |
* @param[in] args Pointer to the GPIO line identifier.
|
736 |
*/
|
737 |
static void _gpioCallback(void* args) |
738 |
{ |
739 |
aosDbgCheck((args != NULL) && (*((ioline_t*)args) != PAL_NOLINE) && (PAL_PAD(*((ioline_t*)args)) < sizeof(eventflags_t) * 8)); |
740 |
|
741 |
chSysLockFromISR(); |
742 |
chEvtBroadcastFlagsI(&aos.events.gpio, AOS_GPIOEVENT_FLAG(PAL_PAD(*((ioline_t*)args)))); |
743 |
chSysUnlockFromISR(); |
744 |
|
745 |
return;
|
746 |
} |
747 |
#pragma GCC diagnostic pop
|
748 |
|
749 |
/**
|
750 |
* @brief Callback function for the uptime accumulation timer.
|
751 |
*
|
752 |
* @param[in] par Generic parameter.
|
753 |
*/
|
754 |
static void _uptimeCallback(void* par) |
755 |
{ |
756 |
(void)par;
|
757 |
|
758 |
chSysLockFromISR(); |
759 |
// read current time in system ticks
|
760 |
register const systime_t st = chVTGetSystemTimeX(); |
761 |
// update the uptime variables
|
762 |
_uptime += chTimeI2US(chTimeDiffX(_synctime, st)); |
763 |
_synctime = st; |
764 |
// enable the timer again
|
765 |
chVTSetI(&_systimer, SYSTIMER_PERIOD, &_uptimeCallback, NULL);
|
766 |
chSysUnlockFromISR(); |
767 |
|
768 |
return;
|
769 |
} |
770 |
|
771 |
/**
|
772 |
* @brief Retreive the number of active threads.
|
773 |
*
|
774 |
* @return Number of active threads.
|
775 |
*/
|
776 |
static size_t _numActiveThreads(void) |
777 |
{ |
778 |
size_t threads = 0;
|
779 |
thread_t* tp = chRegFirstThread(); |
780 |
while (tp) {
|
781 |
threads += (tp->state == CH_STATE_FINAL) ? 1 : 0; |
782 |
tp = chRegNextThread(tp); |
783 |
} |
784 |
|
785 |
return threads;
|
786 |
} |
787 |
|
788 |
/******************************************************************************/
|
789 |
/* EXPORTED FUNCTIONS */
|
790 |
/******************************************************************************/
|
791 |
|
792 |
/**
|
793 |
* @brief AMiRo-OS system initialization.
|
794 |
* @note Must be called from the system control thread (usually main thread).
|
795 |
*
|
796 |
* @param[in] shellPrompt String to be printed as prompt of the system shell.
|
797 |
*/
|
798 |
void aosSysInit(const char* shellPrompt) |
799 |
{ |
800 |
aosDbgCheck(shellPrompt != NULL || !AMIROOS_CFG_SHELL_ENABLE);
|
801 |
|
802 |
/* set control thread to maximum priority */
|
803 |
chThdSetPriority(AOS_THD_CTRLPRIO); |
804 |
|
805 |
#if (AMIROOS_CFG_SSSP_ENABLE == true) |
806 |
aosSsspInit(&_uptime); |
807 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
808 |
|
809 |
/* set local variables */
|
810 |
chVTObjectInit(&_systimer); |
811 |
#if (AMIROOS_CFG_SSSP_ENABLE == true) |
812 |
// uptime counter is started when SSSP proceeds to operation phase
|
813 |
_synctime = 0;
|
814 |
_uptime = 0;
|
815 |
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
816 |
// start the uptime counter
|
817 |
chSysLock(); |
818 |
aosSysStartUptimeS(); |
819 |
chSysUnlock(); |
820 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
821 |
|
822 |
/* initialize aos configuration */
|
823 |
aosIOStreamInit(&aos.iostream); |
824 |
chEvtObjectInit(&aos.events.gpio); |
825 |
chEvtObjectInit(&aos.events.os); |
826 |
|
827 |
#if (AMIROOS_CFG_SHELL_ENABLE == true) |
828 |
|
829 |
/* init shell */
|
830 |
aosShellInit(&aos.shell, |
831 |
_shell_name, |
832 |
shellPrompt, |
833 |
_shell_buffer, |
834 |
SYSTEM_SHELL_BUFFERENTRIES, |
835 |
AMIROOS_CFG_SHELL_LINEWIDTH, |
836 |
AMIROOS_CFG_SHELL_MAXARGS); |
837 |
// add system commands
|
838 |
aosShellAddCommand(&aos.shell, &_shellcmd_config); |
839 |
aosShellAddCommand(&aos.shell, &_shellcmd_info); |
840 |
aosShellAddCommand(&aos.shell, &_shellcmd_shutdown); |
841 |
#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE)) || defined(__DOXYGEN__)
|
842 |
aosShellAddCommand(&aos.shell, &_shellcmd_cpuload); |
843 |
#endif /* (CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE) */ |
844 |
#if (AMIROOS_CFG_TESTS_ENABLE == true) |
845 |
aosShellAddCommand(&aos.shell, &_shellcmd_kerneltest); |
846 |
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */ |
847 |
|
848 |
#else /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
849 |
|
850 |
// suppress unused variable warnings
|
851 |
(void)shellPrompt;
|
852 |
|
853 |
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
854 |
|
855 |
return;
|
856 |
} |
857 |
|
858 |
/**
|
859 |
* @brief Starts the system and all system threads.
|
860 |
*/
|
861 |
void aosSysStart(void) |
862 |
{ |
863 |
// print system information;
|
864 |
_printSystemInfo((BaseSequentialStream*)&aos.iostream); |
865 |
aosprintf("\n");
|
866 |
|
867 |
#if (AMIROOS_CFG_SHELL_ENABLE == true) |
868 |
// start system shell thread
|
869 |
#if (CH_CFG_USE_THREADHIERARCHY == TRUE)
|
870 |
aos.shell.thread = chThdCreateStatic(_shell_wa, sizeof(_shell_wa), AMIROOS_CFG_SHELL_THREADPRIO, aosShellThread, &aos.shell, &ch.mainthread);
|
871 |
#else /* (CH_CFG_USE_THREADHIERARCHY == TRUE) */ |
872 |
aos.shell.thread = chThdCreateStatic(_shell_wa, sizeof(_shell_wa), AMIROOS_CFG_SHELL_THREADPRIO, aosShellThread, &aos.shell);
|
873 |
#endif /* (CH_CFG_USE_THREADHIERARCHY == TRUE) */ |
874 |
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
875 |
|
876 |
return;
|
877 |
} |
878 |
|
879 |
/**
|
880 |
* @brief Start the system uptime measurement.
|
881 |
* @note Must be called from a locked context.
|
882 |
*/
|
883 |
void aosSysStartUptimeS(void) |
884 |
{ |
885 |
chDbgCheckClassS(); |
886 |
|
887 |
// start the uptime aggregation counter
|
888 |
_synctime = chVTGetSystemTimeX(); |
889 |
_uptime = 0;
|
890 |
chVTSetI(&_systimer, SYSTIMER_PERIOD, &_uptimeCallback, NULL);
|
891 |
|
892 |
return;
|
893 |
} |
894 |
|
895 |
/**
|
896 |
* @brief Retrieves the system uptime.
|
897 |
*
|
898 |
* @param[out] ut The system uptime.
|
899 |
*/
|
900 |
void aosSysGetUptimeX(aos_timestamp_t* ut)
|
901 |
{ |
902 |
aosDbgCheck(ut != NULL);
|
903 |
|
904 |
*ut = _uptime + chTimeI2US(chTimeDiffX(_synctime, chVTGetSystemTimeX())); |
905 |
|
906 |
return;
|
907 |
} |
908 |
|
909 |
#if (HAL_USE_RTC == TRUE) || defined(__DOXYGEN__)
|
910 |
|
911 |
/**
|
912 |
* @brief retrieves the date and time from the MCU clock.
|
913 |
*
|
914 |
* @param[out] td The date and time.
|
915 |
*/
|
916 |
void aosSysGetDateTime(struct tm* dt) |
917 |
{ |
918 |
aosDbgCheck(dt != NULL);
|
919 |
|
920 |
RTCDateTime rtc; |
921 |
rtcGetTime(&MODULE_HAL_RTC, &rtc); |
922 |
rtcConvertDateTimeToStructTm(&rtc, dt, NULL);
|
923 |
|
924 |
return;
|
925 |
} |
926 |
|
927 |
/**
|
928 |
* @brief set the date and time of the MCU clock.
|
929 |
*
|
930 |
* @param[in] dt The date and time to set.
|
931 |
*/
|
932 |
void aosSysSetDateTime(struct tm* dt) |
933 |
{ |
934 |
aosDbgCheck(dt != NULL);
|
935 |
|
936 |
RTCDateTime rtc; |
937 |
rtcConvertStructTmToDateTime(dt, 0, &rtc);
|
938 |
rtcSetTime(&MODULE_HAL_RTC, &rtc); |
939 |
|
940 |
return;
|
941 |
} |
942 |
|
943 |
#endif /* (HAL_USE_RTC == TRUE) */ |
944 |
|
945 |
/**
|
946 |
* @brief Initializes/Acknowledges a system shutdown/restart request.
|
947 |
* @note This functions should be called from the thread with highest priority.
|
948 |
*
|
949 |
* @param[in] shutdown Type of shutdown.
|
950 |
*/
|
951 |
void aosSysShutdownInit(aos_shutdown_t shutdown)
|
952 |
{ |
953 |
// check arguments
|
954 |
aosDbgCheck(shutdown != AOS_SHUTDOWN_NONE); |
955 |
|
956 |
#if (AMIROOS_CFG_SSSP_ENABLE == true) |
957 |
|
958 |
// activate the PD signal only if this module initiated the shutdown
|
959 |
if (shutdown != AOS_SHUTDOWN_PASSIVE) {
|
960 |
aosSsspShutdownInit(true);
|
961 |
} |
962 |
|
963 |
switch (shutdown) {
|
964 |
case AOS_SHUTDOWN_PASSIVE:
|
965 |
chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_PASSIVE); |
966 |
aosprintf("shutdown request received...\n");
|
967 |
break;
|
968 |
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_NONE)
|
969 |
case AOS_SHUTDOWN_ACTIVE:
|
970 |
aosprintf("shutdown initiated...\n");
|
971 |
break;
|
972 |
#elif (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
|
973 |
case AOS_SHUTDOWN_HIBERNATE:
|
974 |
aosprintf("shutdown to hibernate mode...\n");
|
975 |
break;
|
976 |
case AOS_SHUTDOWN_DEEPSLEEP:
|
977 |
aosprintf("shutdown to deepsleep mode...\n");
|
978 |
break;
|
979 |
case AOS_SHUTDOWN_TRANSPORTATION:
|
980 |
aosprintf("shutdown to transportation mode...\n");
|
981 |
break;
|
982 |
case AOS_SHUTDOWN_RESTART:
|
983 |
aosprintf("restarting system...\n");
|
984 |
break;
|
985 |
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */ |
986 |
case AOS_SHUTDOWN_NONE:
|
987 |
// must never occur
|
988 |
aosDbgAssert(false);
|
989 |
break;
|
990 |
} |
991 |
|
992 |
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
993 |
|
994 |
aosprintf("shutdown initiated...\n");
|
995 |
|
996 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
997 |
|
998 |
return;
|
999 |
} |
1000 |
|
1001 |
/**
|
1002 |
* @brief Stops the system and all related threads (not the thread this function is called from).
|
1003 |
*/
|
1004 |
void aosSysStop(void) |
1005 |
{ |
1006 |
// wait until the calling thread is the only remaining active thread
|
1007 |
#if (CH_CFG_NO_IDLE_THREAD == TRUE)
|
1008 |
while (_numActiveThreads() > 1) { |
1009 |
#else /* (CH_CFG_NO_IDLE_THREAD == TRUE) */ |
1010 |
while (_numActiveThreads() > 2) { |
1011 |
#endif /* (CH_CFG_NO_IDLE_THREAD == TRUE) */ |
1012 |
aosDbgPrintf("waiting for all threads to terminate...\n");
|
1013 |
chThdYield(); |
1014 |
} |
1015 |
|
1016 |
return;
|
1017 |
} |
1018 |
|
1019 |
/**
|
1020 |
* @brief Deinitialize all system variables.
|
1021 |
*/
|
1022 |
void aosSysDeinit(void) |
1023 |
{ |
1024 |
return;
|
1025 |
} |
1026 |
|
1027 |
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_SHUTDOWN != true)) || \ |
1028 |
((AMIROOS_CFG_SSSP_ENABLE != true) && (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)) || \
|
1029 |
defined(__DOXYGEN__) |
1030 |
|
1031 |
/**
|
1032 |
* @brief Finally shuts down the system and calls the bootloader callback function.
|
1033 |
* @note This function should be called from the thtead with highest priority.
|
1034 |
*
|
1035 |
* @param[in] shutdown Type of shutdown.
|
1036 |
*/
|
1037 |
void aosSysShutdownToBootloader(aos_shutdown_t shutdown)
|
1038 |
{ |
1039 |
// check arguments
|
1040 |
aosDbgCheck(shutdown != AOS_SHUTDOWN_NONE); |
1041 |
|
1042 |
// disable all interrupts
|
1043 |
irqDeinit(); |
1044 |
|
1045 |
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
|
1046 |
// validate AMiRo-BLT
|
1047 |
if ((BL_CALLBACK_TABLE_ADDRESS->magicNumber == BL_MAGIC_NUMBER) &&
|
1048 |
(BL_CALLBACK_TABLE_ADDRESS->vBootloader.major == BL_VERSION_MAJOR) && |
1049 |
(BL_CALLBACK_TABLE_ADDRESS->vBootloader.minor >= BL_VERSION_MINOR)) { |
1050 |
// call bootloader callback depending on arguments
|
1051 |
switch (shutdown) {
|
1052 |
#if (AMIROOS_CFG_SSSP_ENABLE == true) |
1053 |
case AOS_SHUTDOWN_PASSIVE:
|
1054 |
BL_CALLBACK_TABLE_ADDRESS->cbHandleShutdownRequest(); |
1055 |
break;
|
1056 |
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */ |
1057 |
case AOS_SHUTDOWN_HIBERNATE:
|
1058 |
BL_CALLBACK_TABLE_ADDRESS->cbShutdownHibernate(); |
1059 |
break;
|
1060 |
case AOS_SHUTDOWN_DEEPSLEEP:
|
1061 |
BL_CALLBACK_TABLE_ADDRESS->cbShutdownDeepsleep(); |
1062 |
break;
|
1063 |
case AOS_SHUTDOWN_TRANSPORTATION:
|
1064 |
BL_CALLBACK_TABLE_ADDRESS->cbShutdownTransportation(); |
1065 |
break;
|
1066 |
case AOS_SHUTDOWN_RESTART:
|
1067 |
BL_CALLBACK_TABLE_ADDRESS->cbShutdownRestart(); |
1068 |
break;
|
1069 |
// must never occur
|
1070 |
case AOS_SHUTDOWN_NONE:
|
1071 |
default:
|
1072 |
break;
|
1073 |
} |
1074 |
} else {
|
1075 |
// fallback if AMiRo-BLT was found to be invalid
|
1076 |
aosprintf("ERROR: AMiRo-BLT incompatible or not available!\n");
|
1077 |
aosThdMSleep(10);
|
1078 |
chSysDisable(); |
1079 |
} |
1080 |
#endif /* (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT) */ |
1081 |
|
1082 |
return;
|
1083 |
} |
1084 |
|
1085 |
#endif /* ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_SHUTDOWN != true)) || ((AMIROOS_CFG_SSSP_ENABLE != true) && (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)) */ |
1086 |
|
1087 |
/**
|
1088 |
* @brief Generic callback function for GPIO interrupts.
|
1089 |
*
|
1090 |
* @return Pointer to the callback function.
|
1091 |
*/
|
1092 |
palcallback_t aosSysGetStdGpioCallback(void)
|
1093 |
{ |
1094 |
return _gpioCallback;
|
1095 |
} |
1096 |
|
1097 |
/** @} */
|