amiro-os / os / core / src / aos_system.c @ 6b53f6bf
History | View | Annotate | Download (32.537 KB)
1 | e545e620 | Thomas Schöpping | /*
|
---|---|---|---|
2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
3 | Copyright (C) 2016..2018 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 | #include <aos_system.h> |
||
20 | |||
21 | #include <amiroos.h> |
||
22 | #include <amiroblt.h> |
||
23 | 8399aeae | Thomas Schöpping | #include <module.h> |
24 | e545e620 | Thomas Schöpping | #include <string.h> |
25 | 8399aeae | Thomas Schöpping | #include <stdlib.h> |
26 | e545e620 | Thomas Schöpping | #if (AMIROOS_CFG_TESTS_ENABLE == true) |
27 | #include <ch_test.h> |
||
28 | #endif
|
||
29 | |||
30 | /**
|
||
31 | * @brief Period of the system timer.
|
||
32 | */
|
||
33 | #define SYSTIMER_PERIOD (TIME_MAXIMUM - CH_CFG_ST_TIMEDELTA)
|
||
34 | |||
35 | /**
|
||
36 | * @brief Width of the printable system info text.
|
||
37 | */
|
||
38 | #define SYSTEM_INFO_WIDTH 70 |
||
39 | |||
40 | ba516b61 | Thomas Schöpping | /**
|
41 | * @brief Width of the name column of the system info table.
|
||
42 | */
|
||
43 | #define SYSTEM_INFO_NAMEWIDTH 14 |
||
44 | |||
45 | e545e620 | Thomas Schöpping | /* forward declarations */
|
46 | static void _printSystemInfo(BaseSequentialStream* stream); |
||
47 | #if (AMIROOS_CFG_SHELL_ENABLE == true) |
||
48 | static int _shellcmd_configcb(BaseSequentialStream* stream, int argc, char* argv[]); |
||
49 | static int _shellcmd_infocb(BaseSequentialStream* stream, int argc, char* argv[]); |
||
50 | static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* argv[]); |
||
51 | #endif /* AMIROOS_CFG_SHELL_ENABLE == true */ |
||
52 | #if (AMIROOS_CFG_TESTS_ENABLE == true) |
||
53 | static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char* argv[]); |
||
54 | #endif /* AMIROOS_CFG_TESTS_ENABLE == true */ |
||
55 | |||
56 | /**
|
||
57 | * @brief Timer to accumulate system uptime.
|
||
58 | */
|
||
59 | static virtual_timer_t _systimer;
|
||
60 | |||
61 | /**
|
||
62 | * @brief Accumulated system uptime.
|
||
63 | */
|
||
64 | static aos_timestamp_t _uptime;
|
||
65 | |||
66 | /**
|
||
67 | * @brief Timer register value of last accumulation.
|
||
68 | */
|
||
69 | static systime_t _synctime;
|
||
70 | |||
71 | #if (AMIROOS_CFG_SSSP_MASTER == true) || defined(__DOXYGEN__) |
||
72 | /**
|
||
73 | * @brief Timer to drive the SYS_SYNC signal for system wide time synchronization according to SSSP.
|
||
74 | */
|
||
75 | static virtual_timer_t _syssynctimer;
|
||
76 | |||
77 | /**
|
||
78 | * @brief Last uptime of system wide time synchronization.
|
||
79 | */
|
||
80 | static aos_timestamp_t _syssynctime;
|
||
81 | #endif
|
||
82 | |||
83 | #if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
||
84 | /**
|
||
85 | * @brief Shell thread working area.
|
||
86 | */
|
||
87 | THD_WORKING_AREA(_shell_wa, AMIROOS_CFG_SHELL_STACKSIZE); |
||
88 | |||
89 | /**
|
||
90 | * @brief Shell input buffer.
|
||
91 | */
|
||
92 | static char _shell_line[AMIROOS_CFG_SHELL_LINEWIDTH]; |
||
93 | |||
94 | /**
|
||
95 | * @brief Shell argument buffer.
|
||
96 | */
|
||
97 | static char* _shell_arglist[AMIROOS_CFG_SHELL_MAXARGS]; |
||
98 | |||
99 | /**
|
||
100 | * @brief Shell command to retrieve system information.
|
||
101 | */
|
||
102 | static aos_shellcommand_t _shellcmd_info = {
|
||
103 | /* name */ "module:info", |
||
104 | /* callback */ _shellcmd_infocb,
|
||
105 | /* next */ NULL, |
||
106 | }; |
||
107 | |||
108 | /**
|
||
109 | * @brief Shell command to set or retrieve system configuration.
|
||
110 | */
|
||
111 | static aos_shellcommand_t _shellcmd_config = {
|
||
112 | /* name */ "module:config", |
||
113 | /* callback */ _shellcmd_configcb,
|
||
114 | /* next */ NULL, |
||
115 | }; |
||
116 | |||
117 | /**
|
||
118 | * @brief Shell command to shutdown the system.
|
||
119 | */
|
||
120 | static aos_shellcommand_t _shellcmd_shutdown = {
|
||
121 | /* name */ "system:shutdown", |
||
122 | /* callback */ _shellcmd_shutdowncb,
|
||
123 | /* next */ NULL, |
||
124 | }; |
||
125 | #endif /* AMIROOS_CFG_SHELL_ENABLE == true */ |
||
126 | |||
127 | #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
||
128 | /**
|
||
129 | * @brief Shell kommand to run a test of the ChibiOS/RT kernel.
|
||
130 | */
|
||
131 | static aos_shellcommand_t _shellcmd_kerneltest = {
|
||
132 | /* name */ "kernel:test", |
||
133 | /* callback */ _shellcmd_kerneltestcb,
|
||
134 | /* next */ NULL, |
||
135 | }; |
||
136 | #endif /* AMIROOS_CFG_TESTS_ENABLE == true */ |
||
137 | |||
138 | /**
|
||
139 | * @brief Global system object.
|
||
140 | */
|
||
141 | 6b53f6bf | Thomas Schöpping | aos_system_t aos; |
142 | e545e620 | Thomas Schöpping | |
143 | /**
|
||
144 | * @brief Print a separator line.
|
||
145 | *
|
||
146 | ba516b61 | Thomas Schöpping | * @param[in] stream Stream to print to or NULL to print to all system streams.
|
147 | e545e620 | Thomas Schöpping | * @param[in] c Character to use.
|
148 | * @param[in] n Length of the separator line.
|
||
149 | *
|
||
150 | * @return Number of characters printed.
|
||
151 | */
|
||
152 | static unsigned int _printSystemInfoSeparator(BaseSequentialStream* stream, const char c, const unsigned int n) |
||
153 | { |
||
154 | aosDbgCheck(stream != NULL);
|
||
155 | |||
156 | // print the specified character n times
|
||
157 | for (unsigned int i = 0; i < n; ++i) { |
||
158 | streamPut(stream, c); |
||
159 | } |
||
160 | streamPut(stream, '\n');
|
||
161 | |||
162 | return n+1; |
||
163 | } |
||
164 | |||
165 | /**
|
||
166 | * @brief Print a system information line.
|
||
167 | * @details Prints a system information line with the following format:
|
||
168 | * "<name>[spaces]fmt"
|
||
169 | * The combined width of "<name>[spaces]" can be specified in order to align <fmt> on multiple lines.
|
||
170 | * Note that there is not trailing newline added implicitely.
|
||
171 | *
|
||
172 | ba516b61 | Thomas Schöpping | * @param[in] stream Stream to print to or NULL to print to all system streams.
|
173 | e545e620 | Thomas Schöpping | * @param[in] name Name of the entry/line.
|
174 | * @param[in] namewidth Width of the name column.
|
||
175 | * @param[in] fmt Formatted string of information content.
|
||
176 | *
|
||
177 | * @return Number of characters printed.
|
||
178 | */
|
||
179 | static unsigned int _printSystemInfoLine(BaseSequentialStream* stream, const char* name, const unsigned int namewidth, const char* fmt, ...) |
||
180 | { |
||
181 | aosDbgCheck(stream != NULL);
|
||
182 | ba516b61 | Thomas Schöpping | aosDbgCheck(name != NULL);
|
183 | e545e620 | Thomas Schöpping | |
184 | unsigned int n = 0; |
||
185 | ba516b61 | Thomas Schöpping | va_list ap; |
186 | e545e620 | Thomas Schöpping | |
187 | ba516b61 | Thomas Schöpping | va_start(ap, fmt); |
188 | e545e620 | Thomas Schöpping | n += chprintf(stream, name); |
189 | while (n < namewidth) {
|
||
190 | streamPut(stream, ' ');
|
||
191 | ++n; |
||
192 | } |
||
193 | n += chvprintf(stream, fmt, ap); |
||
194 | va_end(ap); |
||
195 | |||
196 | return n;
|
||
197 | } |
||
198 | |||
199 | /**
|
||
200 | * @brief Prints information about the system.
|
||
201 | *
|
||
202 | * @param[in] stream Stream to print to.
|
||
203 | */
|
||
204 | ba516b61 | Thomas Schöpping | static void _printSystemInfo(BaseSequentialStream* stream) |
205 | e545e620 | Thomas Schöpping | { |
206 | aosDbgCheck(stream != NULL);
|
||
207 | |||
208 | _printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
|
||
209 | ba516b61 | Thomas Schöpping | _printSystemInfoLine(stream, "Module", SYSTEM_INFO_NAMEWIDTH, "%s (v%s)\n", BOARD_NAME, BOARD_VERSION); |
210 | e545e620 | Thomas Schöpping | #ifdef PLATFORM_NAME
|
211 | ba516b61 | Thomas Schöpping | _printSystemInfoLine(stream, "Platform", SYSTEM_INFO_NAMEWIDTH, "%s\n", PLATFORM_NAME); |
212 | e545e620 | Thomas Schöpping | #endif
|
213 | #ifdef PORT_CORE_VARIANT_NAME
|
||
214 | ba516b61 | Thomas Schöpping | _printSystemInfoLine(stream, "Core Variant", SYSTEM_INFO_NAMEWIDTH, "%s\n", PORT_CORE_VARIANT_NAME); |
215 | e545e620 | Thomas Schöpping | #endif
|
216 | ba516b61 | Thomas Schöpping | _printSystemInfoLine(stream, "Architecture", SYSTEM_INFO_NAMEWIDTH, "%s\n", PORT_ARCHITECTURE_NAME); |
217 | e545e620 | Thomas Schöpping | _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
|
218 | 8399aeae | Thomas Schöpping | _printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)\n", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE, AOS_SYSTEM_SSSP_MAJOR, AOS_SYSTEM_SSSP_MINOR); |
219 | _printSystemInfoLine(stream, "AMiRo-LLD" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (periphAL %u.%u)\n", AMIRO_LLD_VERSION_MAJOR, AMIRO_LLD_VERSION_MINOR, AMIRO_LLD_VERSION_PATCH, AMIRO_LLD_RELEASE_TYPE, PERIPHAL_VERSION_MAJOR, PERIPHAL_VERSION_MINOR); |
||
220 | _printSystemInfoLine(stream, "ChibiOS/RT" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s\n", CH_KERNEL_MAJOR, CH_KERNEL_MINOR, CH_KERNEL_PATCH, (CH_KERNEL_STABLE == 1) ? "stable" : "non-stable"); |
||
221 | _printSystemInfoLine(stream, "ChibiOS/HAL", SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s\n", CH_HAL_MAJOR, CH_HAL_MINOR, CH_HAL_PATCH, (CH_HAL_STABLE == 1) ? "stable" : "non-stable"); |
||
222 | _printSystemInfoLine(stream, "build type", SYSTEM_INFO_NAMEWIDTH,"%s\n", (AMIROOS_CFG_DBG == true) ? "debug" : "release"); |
||
223 | _printSystemInfoLine(stream, "Compiler" , SYSTEM_INFO_NAMEWIDTH, "%s %u.%u.%u\n", "GCC", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); // TODO: support other compilers than GCC |
||
224 | _printSystemInfoLine(stream, "Compiled" , SYSTEM_INFO_NAMEWIDTH, "%s - %s\n", __DATE__, __TIME__); |
||
225 | e545e620 | Thomas Schöpping | _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
|
226 | if (BL_CALLBACK_TABLE_ADDRESS->magicNumber == BL_MAGIC_NUMBER) {
|
||
227 | ba516b61 | Thomas Schöpping | _printSystemInfoLine(stream, "AMiRo-BLT", SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)\n", BL_CALLBACK_TABLE_ADDRESS->vBootloader.major, BL_CALLBACK_TABLE_ADDRESS->vBootloader.minor, BL_CALLBACK_TABLE_ADDRESS->vBootloader.patch, |
228 | e545e620 | Thomas Schöpping | (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Release) ? "stable" :
|
229 | (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_ReleaseCandidate) ? "release candidate" :
|
||
230 | (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Beta) ? "beta" :
|
||
231 | (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Alpha) ? "alpha" :
|
||
232 | (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_PreAlpha) ? "pre-alpha" :
|
||
233 | "<release type unknown>",
|
||
234 | BL_CALLBACK_TABLE_ADDRESS->vSSSP.major, BL_CALLBACK_TABLE_ADDRESS->vSSSP.minor); |
||
235 | ba516b61 | Thomas Schöpping | if (BL_CALLBACK_TABLE_ADDRESS->vSSSP.major != AOS_SYSTEM_SSSP_MAJOR) {
|
236 | if (stream) {
|
||
237 | chprintf(stream, "WARNING: Bootloader and AMiRo-OS implement incompatible SSSP versions!\n");
|
||
238 | } else {
|
||
239 | aosprintf("WARNING: Bootloader and AMiRo-OS implement incompatible SSSP versions!\n");
|
||
240 | } |
||
241 | } |
||
242 | _printSystemInfoLine(stream, "Compiler", SYSTEM_INFO_NAMEWIDTH, "%s %u.%u.%u\n", (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 |
||
243 | e545e620 | Thomas Schöpping | } else {
|
244 | ba516b61 | Thomas Schöpping | if (stream) {
|
245 | chprintf(stream, "Bootloader incompatible or not available.\n");
|
||
246 | } else {
|
||
247 | aosprintf("Bootloader incompatible or not available.\n");
|
||
248 | } |
||
249 | e545e620 | Thomas Schöpping | } |
250 | 8399aeae | Thomas Schöpping | _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
|
251 | struct tm dt;
|
||
252 | aosSysGetDateTime(&dt); |
||
253 | _printSystemInfoLine(stream, "Date", SYSTEM_INFO_NAMEWIDTH, "%s %02u-%02u-%04u\n", (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", |
||
254 | dt.tm_mday, |
||
255 | dt.tm_mon + 1,
|
||
256 | dt.tm_year + 1900);
|
||
257 | _printSystemInfoLine(stream, "Time", SYSTEM_INFO_NAMEWIDTH, "%02u:%02u:%02u\n", dt.tm_hour, dt.tm_min, dt.tm_sec); |
||
258 | e545e620 | Thomas Schöpping | _printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
|
259 | |||
260 | return;
|
||
261 | } |
||
262 | |||
263 | #if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
||
264 | /**
|
||
265 | * @brief Callback function for the system:config shell command.
|
||
266 | *
|
||
267 | * @param[in] stream The I/O stream to use.
|
||
268 | * @param[in] argc Number of arguments.
|
||
269 | * @param[in] argv List of pointers to the arguments.
|
||
270 | *
|
||
271 | * @return An exit status.
|
||
272 | * @retval AOS_OK The command was executed successfuly.
|
||
273 | * @retval AOS_INVALID_ARGUMENTS There was an issue with the arguemnts.
|
||
274 | */
|
||
275 | static int _shellcmd_configcb(BaseSequentialStream* stream, int argc, char* argv[]) |
||
276 | { |
||
277 | ba516b61 | Thomas Schöpping | aosDbgCheck(stream != NULL);
|
278 | |||
279 | e545e620 | Thomas Schöpping | // local variables
|
280 | int retval = AOS_INVALID_ARGUMENTS;
|
||
281 | |||
282 | // if there are additional arguments
|
||
283 | if (argc > 1) { |
||
284 | // if the user wants to set or retrieve the shell configuration
|
||
285 | if (strcmp(argv[1], "--shell") == 0) { |
||
286 | // if the user wants to modify the shell configuration
|
||
287 | if (argc > 2) { |
||
288 | // if the user wants to modify the prompt
|
||
289 | if (strcmp(argv[2], "prompt") == 0) { |
||
290 | // there must be a further argument
|
||
291 | if (argc > 3) { |
||
292 | // handle the option
|
||
293 | if (strcmp(argv[3], "text") == 0) { |
||
294 | 6b53f6bf | Thomas Schöpping | aos.shell.config &= ~AOS_SHELL_CONFIG_PROMPT_MINIMAL; |
295 | e545e620 | Thomas Schöpping | retval = AOS_OK; |
296 | } |
||
297 | else if (strcmp(argv[3], "minimal") == 0) { |
||
298 | 6b53f6bf | Thomas Schöpping | aos.shell.config |= AOS_SHELL_CONFIG_PROMPT_MINIMAL; |
299 | e545e620 | Thomas Schöpping | retval = AOS_OK; |
300 | } |
||
301 | else if (strcmp(argv[3], "notime") == 0) { |
||
302 | 6b53f6bf | Thomas Schöpping | aos.shell.config &= ~(AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME); |
303 | e545e620 | Thomas Schöpping | retval = AOS_OK; |
304 | } |
||
305 | else if (strcmp(argv[3], "uptime") == 0) { |
||
306 | 6b53f6bf | Thomas Schöpping | aos.shell.config &= ~AOS_SHELL_CONFIG_PROMPT_DATETIME; |
307 | aos.shell.config |= AOS_SHELL_CONFIG_PROMPT_UPTIME; |
||
308 | e545e620 | Thomas Schöpping | retval = AOS_OK; |
309 | } |
||
310 | 8399aeae | Thomas Schöpping | else if (strcmp(argv[3], "date&time") == 0) { |
311 | 6b53f6bf | Thomas Schöpping | aos.shell.config &= ~AOS_SHELL_CONFIG_PROMPT_UPTIME; |
312 | aos.shell.config |= AOS_SHELL_CONFIG_PROMPT_DATETIME; |
||
313 | 8399aeae | Thomas Schöpping | retval = AOS_OK; |
314 | } |
||
315 | else {
|
||
316 | chprintf(stream, "unknown option '%s'\n", argv[3]); |
||
317 | return AOS_INVALID_ARGUMENTS;
|
||
318 | } |
||
319 | e545e620 | Thomas Schöpping | } |
320 | } |
||
321 | // if the user wants to modify the string matching
|
||
322 | else if (strcmp(argv[2], "match") == 0) { |
||
323 | // there must be a further argument
|
||
324 | if (argc > 3) { |
||
325 | if (strcmp(argv[3], "casesensitive") == 0) { |
||
326 | 6b53f6bf | Thomas Schöpping | aos.shell.config |= AOS_SHELL_CONFIG_MATCH_CASE; |
327 | e545e620 | Thomas Schöpping | retval = AOS_OK; |
328 | } |
||
329 | else if (strcmp(argv[3], "caseinsensitive") == 0) { |
||
330 | 6b53f6bf | Thomas Schöpping | aos.shell.config &= ~AOS_SHELL_CONFIG_MATCH_CASE; |
331 | e545e620 | Thomas Schöpping | retval = AOS_OK; |
332 | } |
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | // if the user wants to retrieve the shell configuration
|
||
337 | else {
|
||
338 | chprintf(stream, "current shell configuration:\n");
|
||
339 | chprintf(stream, " prompt text: %s\n",
|
||
340 | 6b53f6bf | Thomas Schöpping | (aos.shell.prompt != NULL) ? aos.shell.prompt : "n/a"); |
341 | 8399aeae | Thomas Schöpping | char time[10]; |
342 | 6b53f6bf | Thomas Schöpping | switch (aos.shell.config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
|
343 | 8399aeae | Thomas Schöpping | case AOS_SHELL_CONFIG_PROMPT_UPTIME:
|
344 | strcpy(time, "uptime"); break; |
||
345 | case AOS_SHELL_CONFIG_PROMPT_DATETIME:
|
||
346 | strcpy(time, "date&time"); break; |
||
347 | default:
|
||
348 | strcpy(time, "no time"); break; |
||
349 | } |
||
350 | e545e620 | Thomas Schöpping | chprintf(stream, " prompt style: %s, %s\n",
|
351 | 6b53f6bf | Thomas Schöpping | (aos.shell.config & AOS_SHELL_CONFIG_PROMPT_MINIMAL) ? "minimal" : "text", |
352 | 8399aeae | Thomas Schöpping | time); |
353 | e545e620 | Thomas Schöpping | chprintf(stream, " input method: %s\n",
|
354 | 6b53f6bf | Thomas Schöpping | (aos.shell.config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) ? "replace" : "insert"); |
355 | e545e620 | Thomas Schöpping | chprintf(stream, " text matching: %s\n",
|
356 | 6b53f6bf | Thomas Schöpping | (aos.shell.config & AOS_SHELL_CONFIG_MATCH_CASE) ? "case sensitive" : "case insensitive"); |
357 | e545e620 | Thomas Schöpping | retval = AOS_OK; |
358 | } |
||
359 | } |
||
360 | 8399aeae | Thomas Schöpping | // if the user wants to configure the date or time
|
361 | else if (strcmp(argv[1], "--date&time") == 0 && argc == 4) { |
||
362 | struct tm dt;
|
||
363 | aosSysGetDateTime(&dt); |
||
364 | unsigned int val = atoi(argv[3]); |
||
365 | if (strcmp(argv[2], "year") == 0) { |
||
366 | dt.tm_year = val - 1900;
|
||
367 | } |
||
368 | else if (strcmp(argv[2], "month") == 0 && val <= 12) { |
||
369 | dt.tm_mon = val - 1;
|
||
370 | } |
||
371 | else if (strcmp(argv[2], "day") == 0 && val <= 31) { |
||
372 | dt.tm_mday = val; |
||
373 | } |
||
374 | else if (strcmp(argv[2], "hour") == 0 && val < 24) { |
||
375 | dt.tm_hour = val; |
||
376 | } |
||
377 | else if (strcmp(argv[2], "minute") == 0 && val < 60) { |
||
378 | dt.tm_min = val; |
||
379 | } |
||
380 | else if (strcmp(argv[2], "second") == 0 && val < 60) { |
||
381 | dt.tm_sec = val; |
||
382 | } |
||
383 | else {
|
||
384 | chprintf(stream, "unknown option '%s' or value '%s'\n", argv[2], argv[3]); |
||
385 | return AOS_INVALID_ARGUMENTS;
|
||
386 | } |
||
387 | dt.tm_wday = aosTimeDayOfWeekFromDate(dt.tm_mday, dt.tm_mon+1, dt.tm_year+1900) % 7; |
||
388 | aosSysSetDateTime(&dt); |
||
389 | |||
390 | // read and print new date and time
|
||
391 | aosSysGetDateTime(&dt); |
||
392 | chprintf(stream, "date/time set to %02u:%02u:%02u @ %02u-%02u-%04u\n",
|
||
393 | dt.tm_hour, dt.tm_min, dt.tm_sec, |
||
394 | dt.tm_mday, dt.tm_mon+1, dt.tm_year+1900); |
||
395 | |||
396 | retval = AOS_OK; |
||
397 | } |
||
398 | e545e620 | Thomas Schöpping | } |
399 | |||
400 | // print help, if required
|
||
401 | if (retval == AOS_INVALID_ARGUMENTS) {
|
||
402 | chprintf(stream, "Usage: %s OPTION\n", argv[0]); |
||
403 | chprintf(stream, "Options:\n");
|
||
404 | chprintf(stream, " --help\n");
|
||
405 | chprintf(stream, " Print this help text.\n");
|
||
406 | chprintf(stream, " --shell [OPT [VAL]]\n");
|
||
407 | chprintf(stream, " Set or retrieve shell configuration.\n");
|
||
408 | chprintf(stream, " Possible OPTs and VALs are:\n");
|
||
409 | 8399aeae | Thomas Schöpping | chprintf(stream, " prompt text|minimal|uptime|date&time|notime\n");
|
410 | e545e620 | Thomas Schöpping | chprintf(stream, " Configures the prompt.\n");
|
411 | chprintf(stream, " match casesensitive|caseinsenitive\n");
|
||
412 | chprintf(stream, " Configures string matching.\n");
|
||
413 | 8399aeae | Thomas Schöpping | chprintf(stream, " --date&time OPT VAL\n");
|
414 | chprintf(stream, " Set the date/time value of OPT to VAL.\n");
|
||
415 | chprintf(stream, " Possible OPTs are:\n");
|
||
416 | chprintf(stream, " year\n");
|
||
417 | chprintf(stream, " month\n");
|
||
418 | chprintf(stream, " day\n");
|
||
419 | chprintf(stream, " hour\n");
|
||
420 | chprintf(stream, " minute\n");
|
||
421 | chprintf(stream, " second\n");
|
||
422 | e545e620 | Thomas Schöpping | } |
423 | |||
424 | return (argc > 1 && strcmp(argv[1], "--help") == 0) ? AOS_OK : retval; |
||
425 | } |
||
426 | |||
427 | /**
|
||
428 | * @brief Callback function for the system:info shell command.
|
||
429 | *
|
||
430 | * @param[in] stream The I/O stream to use.
|
||
431 | * @param[in] argc Number of arguments.
|
||
432 | * @param[in] argv List of pointers to the arguments.
|
||
433 | *
|
||
434 | * @return An exit status.
|
||
435 | * @retval AOS_OK The command was executed successfully.
|
||
436 | */
|
||
437 | static int _shellcmd_infocb(BaseSequentialStream* stream, int argc, char* argv[]) |
||
438 | { |
||
439 | ba516b61 | Thomas Schöpping | aosDbgCheck(stream != NULL);
|
440 | |||
441 | e545e620 | Thomas Schöpping | (void)argc;
|
442 | (void)argv;
|
||
443 | |||
444 | // print system information
|
||
445 | _printSystemInfo(stream); |
||
446 | |||
447 | // print time measurement precision
|
||
448 | chprintf(stream, "system time resolution: %uus\n", AOS_SYSTEM_TIME_RESOLUTION);
|
||
449 | |||
450 | // print system uptime
|
||
451 | aos_timestamp_t uptime; |
||
452 | aosSysGetUptime(&uptime); |
||
453 | chprintf(stream, "The system is running for\n");
|
||
454 | chprintf(stream, "%10u days\n", (uint32_t)(uptime / MICROSECONDS_PER_DAY));
|
||
455 | chprintf(stream, "%10u hours\n", (uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR));
|
||
456 | chprintf(stream, "%10u minutes\n", (uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE));
|
||
457 | chprintf(stream, "%10u seconds\n", (uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND));
|
||
458 | chprintf(stream, "%10u milliseconds\n", (uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND));
|
||
459 | chprintf(stream, "%10u microseconds\n", (uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND));
|
||
460 | |||
461 | return AOS_OK;
|
||
462 | } |
||
463 | |||
464 | /**
|
||
465 | * @brief Callback function for the sytem:shutdown shell command.
|
||
466 | *
|
||
467 | * @param[in] stream The I/O stream to use.
|
||
468 | * @param[in] argc Number of arguments.
|
||
469 | * @param[in] argv List of pointers to the arguments.
|
||
470 | *
|
||
471 | * @return An exit status.
|
||
472 | * @retval AOS_OK The command was executed successfully.
|
||
473 | * @retval AOS_INVALID_ARGUMENTS There was an issue with the arguments.
|
||
474 | */
|
||
475 | static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* argv[]) |
||
476 | { |
||
477 | ba516b61 | Thomas Schöpping | aosDbgCheck(stream != NULL);
|
478 | |||
479 | e545e620 | Thomas Schöpping | // print help text
|
480 | if (argc != 2 || strcmp(argv[1], "--help") == 0) { |
||
481 | chprintf(stream, "Usage: %s OPTION\n", argv[0]); |
||
482 | chprintf(stream, "Options:\n");
|
||
483 | chprintf(stream, " --help\n");
|
||
484 | chprintf(stream, " Print this help text.\n");
|
||
485 | chprintf(stream, " --hibernate, -h\n");
|
||
486 | chprintf(stream, " Shutdown to hibernate mode.\n");
|
||
487 | chprintf(stream, " Least energy saving, but allows charging via pins.\n");
|
||
488 | chprintf(stream, " --deepsleep, -d\n");
|
||
489 | chprintf(stream, " Shutdown to deepsleep mode.\n");
|
||
490 | chprintf(stream, " Minimum energy consumption while allowing charging via plug.\n");
|
||
491 | chprintf(stream, " --transportation, -t\n");
|
||
492 | chprintf(stream, " Shutdown to transportation mode.\n");
|
||
493 | chprintf(stream, " Minimum energy consumption with all interrupts disabled (no charging).\n");
|
||
494 | chprintf(stream, " --restart, -r\n");
|
||
495 | chprintf(stream, " Shutdown and restart system.\n");
|
||
496 | |||
497 | return (argc != 2) ? AOS_INVALID_ARGUMENTS : AOS_OK; |
||
498 | } |
||
499 | // handle argument
|
||
500 | else {
|
||
501 | if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--hibernate") == 0) { |
||
502 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_HIBERNATE); |
503 | ba516b61 | Thomas Schöpping | chThdTerminate(currp); |
504 | e545e620 | Thomas Schöpping | return AOS_OK;
|
505 | } |
||
506 | else if (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--deepsleep") == 0) { |
||
507 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP); |
508 | ba516b61 | Thomas Schöpping | chThdTerminate(currp); |
509 | e545e620 | Thomas Schöpping | return AOS_OK;
|
510 | } |
||
511 | else if (strcmp(argv[1], "-t") == 0 || strcmp(argv[1], "--transportation") == 0) { |
||
512 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION); |
513 | ba516b61 | Thomas Schöpping | chThdTerminate(currp); |
514 | e545e620 | Thomas Schöpping | return AOS_OK;
|
515 | } |
||
516 | else if (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--restart") == 0) { |
||
517 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_RESTART); |
518 | ba516b61 | Thomas Schöpping | chThdTerminate(currp); |
519 | e545e620 | Thomas Schöpping | return AOS_OK;
|
520 | } |
||
521 | else {
|
||
522 | chprintf(stream, "unknown argument %s\n", argv[1]); |
||
523 | return AOS_INVALID_ARGUMENTS;
|
||
524 | } |
||
525 | } |
||
526 | } |
||
527 | #endif /* AMIROOS_CFG_SHELL_ENABLE == true */ |
||
528 | |||
529 | #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
||
530 | /**
|
||
531 | * @brief Callback function for the kernel:test shell command.
|
||
532 | *
|
||
533 | * @param[in] stream The I/O stream to use.
|
||
534 | * @param[in] argc Number of arguments.
|
||
535 | * @param[in] argv List of pointers to the arguments.
|
||
536 | *
|
||
537 | * @return An exit status.
|
||
538 | */
|
||
539 | static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char* argv[]) |
||
540 | { |
||
541 | ba516b61 | Thomas Schöpping | aosDbgCheck(stream != NULL);
|
542 | |||
543 | e545e620 | Thomas Schöpping | (void)argc;
|
544 | (void)argv;
|
||
545 | |||
546 | tprio_t prio = chThdGetPriorityX(); |
||
547 | chThdSetPriority(HIGHPRIO - 5); // some tests increase priorirty by 5, so this is the maximum priority permitted |
||
548 | msg_t retval = test_execute(stream); |
||
549 | chThdSetPriority(prio); |
||
550 | |||
551 | return retval;
|
||
552 | } |
||
553 | #endif /* AMIROOS_CFG_TESTS_ENABLE == true */ |
||
554 | |||
555 | /**
|
||
556 | * @brief Callback function for the PD signal interrupt.
|
||
557 | *
|
||
558 | * @param[in] extp Pointer to the interrupt driver object.
|
||
559 | * @param[in] channel Interrupt channel identifier.
|
||
560 | */
|
||
561 | static void _signalPdCallback(EXTDriver* extp, expchannel_t channel) |
||
562 | { |
||
563 | (void)extp;
|
||
564 | |||
565 | chSysLockFromISR(); |
||
566 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlagsI(&aos.events.io, (1 << channel));
|
567 | e545e620 | Thomas Schöpping | chSysUnlockFromISR(); |
568 | |||
569 | return;
|
||
570 | } |
||
571 | |||
572 | /**
|
||
573 | * @brief Callback function for the Sync signal interrupt.
|
||
574 | *
|
||
575 | * @param[in] extp Pointer to the interrupt driver object.
|
||
576 | * @param[in] channel Interrupt channel identifier.
|
||
577 | */
|
||
578 | static void _signalSyncCallback(EXTDriver* extp, expchannel_t channel) |
||
579 | { |
||
580 | (void)extp;
|
||
581 | |||
582 | #if (AMIROOS_CFG_SSSP_MASTER == true) |
||
583 | chSysLockFromISR(); |
||
584 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlagsI(&aos.events.io, (1 << channel));
|
585 | e545e620 | Thomas Schöpping | chSysUnlockFromISR(); |
586 | #else
|
||
587 | apalControlGpioState_t s_state; |
||
588 | aos_timestamp_t uptime; |
||
589 | |||
590 | chSysLockFromISR(); |
||
591 | // get current uptime
|
||
592 | aosSysGetUptimeX(&uptime); |
||
593 | // read signal S
|
||
594 | 6b53f6bf | Thomas Schöpping | apalControlGpioGet(&moduleSsspGpioSync, &s_state); |
595 | e545e620 | Thomas Schöpping | // if S was toggled from on to off during SSSP operation phase
|
596 | 6b53f6bf | Thomas Schöpping | if (aos.sssp.stage == AOS_SSSP_OPERATION && s_state == APAL_GPIO_OFF) {
|
597 | e545e620 | Thomas Schöpping | // align the uptime with the synchronization period
|
598 | if (uptime % AMIROOS_CFG_SSSP_SYSSYNCPERIOD < AMIROOS_CFG_SSSP_SYSSYNCPERIOD / 2) { |
||
599 | _uptime -= uptime % AMIROOS_CFG_SSSP_SYSSYNCPERIOD; |
||
600 | } else {
|
||
601 | _uptime += AMIROOS_CFG_SSSP_SYSSYNCPERIOD - (uptime % AMIROOS_CFG_SSSP_SYSSYNCPERIOD); |
||
602 | } |
||
603 | } |
||
604 | // broadcast event
|
||
605 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlagsI(&aos.events.io, (1 << channel));
|
606 | e545e620 | Thomas Schöpping | chSysUnlockFromISR(); |
607 | #endif
|
||
608 | |||
609 | return;
|
||
610 | } |
||
611 | |||
612 | /**
|
||
613 | * @brief Callback function for the uptime accumulation timer.
|
||
614 | *
|
||
615 | * @param[in] par Generic parameter.
|
||
616 | */
|
||
617 | static void _uptimeCallback(void* par) |
||
618 | { |
||
619 | (void)par;
|
||
620 | |||
621 | chSysLockFromISR(); |
||
622 | // read current time in system ticks
|
||
623 | register const systime_t st = chVTGetSystemTimeX(); |
||
624 | // update the uptime variables
|
||
625 | _uptime += LL_ST2US(st - _synctime); |
||
626 | _synctime = st; |
||
627 | // enable the timer again
|
||
628 | chVTSetI(&_systimer, SYSTIMER_PERIOD, &_uptimeCallback, NULL);
|
||
629 | chSysUnlockFromISR(); |
||
630 | |||
631 | return;
|
||
632 | } |
||
633 | |||
634 | #if (AMIROOS_CFG_SSSP_MASTER == true) || defined (__DOXYGEN__) |
||
635 | /**
|
||
636 | * @brief Periodic system synchronization callback function.
|
||
637 | * @details Toggles the SYS_SYNC signal and reconfigures the system synchronization timer.
|
||
638 | *
|
||
639 | * @param[in] par Unuesed parameters.
|
||
640 | */
|
||
641 | static void _sysSyncTimerCallback(void* par) |
||
642 | { |
||
643 | (void)par;
|
||
644 | |||
645 | apalControlGpioState_t s_state; |
||
646 | aos_timestamp_t uptime; |
||
647 | |||
648 | chSysLockFromISR(); |
||
649 | // read and toggle signal S
|
||
650 | 6b53f6bf | Thomas Schöpping | apalControlGpioGet(&moduleSsspGpioSync, &s_state); |
651 | e545e620 | Thomas Schöpping | s_state = (s_state == APAL_GPIO_ON) ? APAL_GPIO_OFF : APAL_GPIO_ON; |
652 | 6b53f6bf | Thomas Schöpping | apalControlGpioSet(&moduleSsspGpioSync, s_state); |
653 | e545e620 | Thomas Schöpping | // if S was toggled from off to on
|
654 | if (s_state == APAL_GPIO_ON) {
|
||
655 | // reconfigure the timer precisely, because the logically falling edge (next interrupt) snychronizes the system time
|
||
656 | _syssynctime += AMIROOS_CFG_SSSP_SYSSYNCPERIOD; |
||
657 | aosSysGetUptimeX(&uptime); |
||
658 | chVTSetI(&_syssynctimer, LL_US2ST(_syssynctime - uptime), _sysSyncTimerCallback, NULL);
|
||
659 | } |
||
660 | // if S was toggled from on to off
|
||
661 | else /* if (s_state == APAL_GPIO_OFF) */ { |
||
662 | // reconfigure the timer (lazy)
|
||
663 | chVTSetI(&_syssynctimer, LL_US2ST(AMIROOS_CFG_SSSP_SYSSYNCPERIOD / 2), _sysSyncTimerCallback, NULL); |
||
664 | } |
||
665 | chSysUnlockFromISR(); |
||
666 | |||
667 | return;
|
||
668 | } |
||
669 | #endif
|
||
670 | |||
671 | /**
|
||
672 | * @brief AMiRo-OS system initialization.
|
||
673 | * @note Must be called from the system control thread (usually main thread).
|
||
674 | *
|
||
675 | * @param[in] shellPrompt String to be printed as prompt of the system shell.
|
||
676 | */
|
||
677 | 6b53f6bf | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__) |
678 | void aosSysInit(const char* shellPrompt) |
||
679 | #else
|
||
680 | void aosSysInit(void) |
||
681 | #endif
|
||
682 | e545e620 | Thomas Schöpping | { |
683 | // set control thread to maximum priority
|
||
684 | 512abac1 | Thomas Schöpping | chThdSetPriority(AOS_THD_CTRLPRIO); |
685 | e545e620 | Thomas Schöpping | |
686 | // set local variables
|
||
687 | chVTObjectInit(&_systimer); |
||
688 | _synctime = 0;
|
||
689 | _uptime = 0;
|
||
690 | #if (AMIROOS_CFG_SSSP_MASTER == true) |
||
691 | chVTObjectInit(&_syssynctimer); |
||
692 | _syssynctime = 0;
|
||
693 | #endif
|
||
694 | |||
695 | // set aos configuration
|
||
696 | 6b53f6bf | Thomas Schöpping | aos.sssp.stage = AOS_SSSP_STARTUP_2_1; |
697 | aos.sssp.moduleId = 0;
|
||
698 | ba516b61 | Thomas Schöpping | aosIOStreamInit(&aos.iostream); |
699 | 6b53f6bf | Thomas Schöpping | chEvtObjectInit(&aos.events.io); |
700 | chEvtObjectInit(&aos.events.os); |
||
701 | e545e620 | Thomas Schöpping | |
702 | // setup external interrupt system
|
||
703 | 6b53f6bf | Thomas Schöpping | moduleHalExtConfig.channels[moduleSsspGpioPd.gpio->pad].cb = _signalPdCallback; |
704 | moduleHalExtConfig.channels[moduleSsspGpioSync.gpio->pad].cb = _signalSyncCallback; |
||
705 | extStart(&MODULE_HAL_EXT, &moduleHalExtConfig); |
||
706 | e545e620 | Thomas Schöpping | |
707 | #if (AMIROOS_CFG_SHELL_ENABLE == true) |
||
708 | // init shell
|
||
709 | 6b53f6bf | Thomas Schöpping | aosShellInit(&aos.shell, |
710 | &aos.events.os, |
||
711 | e545e620 | Thomas Schöpping | shellPrompt, |
712 | _shell_line, |
||
713 | AMIROOS_CFG_SHELL_LINEWIDTH, |
||
714 | _shell_arglist, |
||
715 | AMIROOS_CFG_SHELL_MAXARGS); |
||
716 | // add system commands
|
||
717 | 6b53f6bf | Thomas Schöpping | aosShellAddCommand(&aos.shell, &_shellcmd_config); |
718 | aosShellAddCommand(&aos.shell, &_shellcmd_info); |
||
719 | aosShellAddCommand(&aos.shell, &_shellcmd_shutdown); |
||
720 | e545e620 | Thomas Schöpping | #if (AMIROOS_CFG_TESTS_ENABLE == true) |
721 | 6b53f6bf | Thomas Schöpping | aosShellAddCommand(&aos.shell, &_shellcmd_kerneltest); |
722 | e545e620 | Thomas Schöpping | #endif
|
723 | #endif
|
||
724 | |||
725 | return;
|
||
726 | } |
||
727 | |||
728 | /**
|
||
729 | * @brief Starts the system and all system threads.
|
||
730 | */
|
||
731 | inline void aosSysStart(void) |
||
732 | { |
||
733 | // update the system SSSP stage
|
||
734 | 6b53f6bf | Thomas Schöpping | aos.sssp.stage = AOS_SSSP_OPERATION; |
735 | e545e620 | Thomas Schöpping | |
736 | ba516b61 | Thomas Schöpping | // print system information;
|
737 | _printSystemInfo((BaseSequentialStream*)&aos.iostream); |
||
738 | e545e620 | Thomas Schöpping | aosprintf("\n");
|
739 | |||
740 | #if (AMIROOS_CFG_SHELL_ENABLE == true) |
||
741 | // start system shell thread
|
||
742 | 6b53f6bf | Thomas Schöpping | aos.shell.thread = chThdCreateStatic(_shell_wa, sizeof(_shell_wa), AMIROOS_CFG_SHELL_THREADPRIO, aosShellThread, &aos.shell);
|
743 | e545e620 | Thomas Schöpping | #endif
|
744 | |||
745 | return;
|
||
746 | } |
||
747 | |||
748 | /**
|
||
749 | * @brief Implements the SSSP startup synchronization step.
|
||
750 | *
|
||
751 | * @param[in] syncEvtListener Event listener that receives the Sync event.
|
||
752 | *
|
||
753 | * @return If another event that the listener is interested in was received, its mask is returned.
|
||
754 | * Otherwise an empty mask (0) is returned.
|
||
755 | */
|
||
756 | eventmask_t aosSysSsspStartupOsInitSyncCheck(event_listener_t* syncEvtListener) |
||
757 | { |
||
758 | aosDbgCheck(syncEvtListener != NULL);
|
||
759 | |||
760 | // local variables
|
||
761 | eventmask_t m; |
||
762 | eventflags_t f; |
||
763 | apalControlGpioState_t s; |
||
764 | |||
765 | // update the system SSSP stage
|
||
766 | 6b53f6bf | Thomas Schöpping | aos.sssp.stage = AOS_SSSP_STARTUP_2_2; |
767 | e545e620 | Thomas Schöpping | |
768 | // deactivate the sync signal to indicate that the module is ready (SSSPv1 stage 2.1 of startup phase)
|
||
769 | 6b53f6bf | Thomas Schöpping | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_OFF); |
770 | e545e620 | Thomas Schöpping | |
771 | // wait for any event to occur (do not apply any filter in order not to miss any event)
|
||
772 | m = chEvtWaitOne(ALL_EVENTS); |
||
773 | f = chEvtGetAndClearFlags(syncEvtListener); |
||
774 | 6b53f6bf | Thomas Schöpping | apalControlGpioGet(&moduleSsspGpioSync, &s); |
775 | e545e620 | Thomas Schöpping | |
776 | // if the event was a system event,
|
||
777 | // and it was fired because of the SysSync control signal,
|
||
778 | // and the SysSync control signal has been deactivated
|
||
779 | if (m & syncEvtListener->events &&
|
||
780 | 6b53f6bf | Thomas Schöpping | f == MODULE_SSSP_EVENTFLAGS_SYNC && |
781 | e545e620 | Thomas Schöpping | s == APAL_GPIO_OFF) { |
782 | chSysLock(); |
||
783 | #if (AMIROOS_CFG_SSSP_MASTER == true) |
||
784 | // start the systen synchronization counter
|
||
785 | chVTSetI(&_syssynctimer, LL_US2ST(AMIROOS_CFG_SSSP_SYSSYNCPERIOD / 2), &_sysSyncTimerCallback, NULL); |
||
786 | #endif
|
||
787 | // start the uptime counter
|
||
788 | _synctime = chVTGetSystemTimeX(); |
||
789 | _uptime = 0;
|
||
790 | chVTSetI(&_systimer, SYSTIMER_PERIOD, &_uptimeCallback, NULL);
|
||
791 | chSysUnlock(); |
||
792 | |||
793 | return 0; |
||
794 | } |
||
795 | // an unexpected event occurred
|
||
796 | else {
|
||
797 | // reassign the flags to the event and return the event mask
|
||
798 | syncEvtListener->flags |= f; |
||
799 | return m;
|
||
800 | } |
||
801 | } |
||
802 | |||
803 | /**
|
||
804 | * @brief Retrieves the system uptime.
|
||
805 | *
|
||
806 | * @param[out] ut The system uptime.
|
||
807 | */
|
||
808 | inline void aosSysGetUptimeX(aos_timestamp_t* ut) |
||
809 | { |
||
810 | aosDbgCheck(ut != NULL);
|
||
811 | |||
812 | *ut = _uptime + LL_ST2US(chVTGetSystemTimeX() - _synctime); |
||
813 | |||
814 | return;
|
||
815 | } |
||
816 | |||
817 | /**
|
||
818 | 8399aeae | Thomas Schöpping | * @brief retrieves the date and time from the MCU clock.
|
819 | *
|
||
820 | * @param[out] td The date and time.
|
||
821 | */
|
||
822 | void aosSysGetDateTime(struct tm* dt) |
||
823 | { |
||
824 | aosDbgCheck(dt != NULL);
|
||
825 | |||
826 | RTCDateTime rtc; |
||
827 | rtcGetTime(&MODULE_HAL_RTC, &rtc); |
||
828 | rtcConvertDateTimeToStructTm(&rtc, dt, NULL);
|
||
829 | |||
830 | return;
|
||
831 | } |
||
832 | |||
833 | /**
|
||
834 | * @brief set the date and time of the MCU clock.
|
||
835 | *
|
||
836 | * @param[in] dt The date and time to set.
|
||
837 | */
|
||
838 | void aosSysSetDateTime(struct tm* dt) |
||
839 | { |
||
840 | aosDbgCheck(dt != NULL);
|
||
841 | |||
842 | RTCDateTime rtc; |
||
843 | rtcConvertStructTmToDateTime(dt, 0, &rtc);
|
||
844 | rtcSetTime(&MODULE_HAL_RTC, &rtc); |
||
845 | |||
846 | return;
|
||
847 | } |
||
848 | |||
849 | /**
|
||
850 | e545e620 | Thomas Schöpping | * @brief Initializes/Acknowledges a system shutdown/restart request.
|
851 | * @note This functions should be called from the thread with highest priority.
|
||
852 | *
|
||
853 | * @param[in] shutdown Type of shutdown.
|
||
854 | */
|
||
855 | void aosSysShutdownInit(aos_shutdown_t shutdown)
|
||
856 | { |
||
857 | // check arguments
|
||
858 | aosDbgCheck(shutdown != AOS_SHUTDOWN_NONE); |
||
859 | |||
860 | #if (AMIROOS_CFG_SSSP_MASTER == true) |
||
861 | // deactivate the system synchronization timer
|
||
862 | chVTReset(&_syssynctimer); |
||
863 | #endif
|
||
864 | |||
865 | // update the system SSSP stage
|
||
866 | 6b53f6bf | Thomas Schöpping | aos.sssp.stage = AOS_SSSP_SHUTDOWN_1_1; |
867 | e545e620 | Thomas Schöpping | |
868 | // activate the SYS_PD control signal only, if this module initiated the shutdown
|
||
869 | chSysLock(); |
||
870 | if (shutdown != AOS_SHUTDOWN_PASSIVE) {
|
||
871 | 6b53f6bf | Thomas Schöpping | apalControlGpioSet(&moduleSsspGpioPd, APAL_GPIO_ON); |
872 | e545e620 | Thomas Schöpping | } |
873 | // activate the SYS_SYNC signal
|
||
874 | 6b53f6bf | Thomas Schöpping | apalControlGpioSet(&moduleSsspGpioSync, APAL_GPIO_ON); |
875 | e545e620 | Thomas Schöpping | chSysUnlock(); |
876 | |||
877 | switch (shutdown) {
|
||
878 | case AOS_SHUTDOWN_PASSIVE:
|
||
879 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_SHUTDOWN); |
880 | e545e620 | Thomas Schöpping | aosprintf("shutdown request received...\n");
|
881 | break;
|
||
882 | case AOS_SHUTDOWN_HIBERNATE:
|
||
883 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_HIBERNATE); |
884 | e545e620 | Thomas Schöpping | aosprintf("shutdown to hibernate mode...\n");
|
885 | break;
|
||
886 | case AOS_SHUTDOWN_DEEPSLEEP:
|
||
887 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP); |
888 | e545e620 | Thomas Schöpping | aosprintf("shutdown to deepsleep mode...\n");
|
889 | break;
|
||
890 | case AOS_SHUTDOWN_TRANSPORTATION:
|
||
891 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION); |
892 | e545e620 | Thomas Schöpping | aosprintf("shutdown to transportation mode...\n");
|
893 | break;
|
||
894 | case AOS_SHUTDOWN_RESTART:
|
||
895 | 6b53f6bf | Thomas Schöpping | chEvtBroadcastFlags(&aos.events.os, AOS_SYSTEM_EVENTFLAGS_RESTART); |
896 | e545e620 | Thomas Schöpping | aosprintf("restarting system...\n");
|
897 | break;
|
||
898 | // must never occur
|
||
899 | case AOS_SHUTDOWN_NONE:
|
||
900 | default:
|
||
901 | break;
|
||
902 | } |
||
903 | |||
904 | // update the system SSSP stage
|
||
905 | 6b53f6bf | Thomas Schöpping | aos.sssp.stage = AOS_SSSP_SHUTDOWN_1_2; |
906 | e545e620 | Thomas Schöpping | |
907 | return;
|
||
908 | } |
||
909 | |||
910 | /**
|
||
911 | * @brief Stops the system and all related threads (not the thread this function is called from).
|
||
912 | */
|
||
913 | void aosSysStop(void) |
||
914 | { |
||
915 | #if (AMIROOS_CFG_SHELL_ENABLE == true) |
||
916 | 6b53f6bf | Thomas Schöpping | chThdWait(aos.shell.thread); |
917 | e545e620 | Thomas Schöpping | #endif
|
918 | |||
919 | return;
|
||
920 | } |
||
921 | |||
922 | /**
|
||
923 | * @brief Deinitialize all system variables.
|
||
924 | */
|
||
925 | void aosSysDeinit(void) |
||
926 | { |
||
927 | return;
|
||
928 | } |
||
929 | |||
930 | /**
|
||
931 | * @brief Finally shuts down the system and calls the bootloader callback function.
|
||
932 | * @note This function should be called from the thtead with highest priority.
|
||
933 | *
|
||
934 | * @param[in] extDrv Pointer to the interrupt driver.
|
||
935 | * @param[in] shutdown Type of shutdown.
|
||
936 | */
|
||
937 | void aosSysShutdownFinal(EXTDriver* extDrv, aos_shutdown_t shutdown)
|
||
938 | { |
||
939 | // check arguments
|
||
940 | aosDbgCheck(extDrv != NULL);
|
||
941 | aosDbgCheck(shutdown != AOS_SHUTDOWN_NONE); |
||
942 | |||
943 | // stop external interrupt system
|
||
944 | extStop(extDrv); |
||
945 | |||
946 | // update the system SSSP stage
|
||
947 | 6b53f6bf | Thomas Schöpping | aos.sssp.stage = AOS_SSSP_SHUTDOWN_1_3; |
948 | e545e620 | Thomas Schöpping | |
949 | // call bootloader callback depending on arguments
|
||
950 | switch (shutdown) {
|
||
951 | case AOS_SHUTDOWN_PASSIVE:
|
||
952 | BL_CALLBACK_TABLE_ADDRESS->cbHandleShutdownRequest(); |
||
953 | break;
|
||
954 | case AOS_SHUTDOWN_HIBERNATE:
|
||
955 | BL_CALLBACK_TABLE_ADDRESS->cbShutdownHibernate(); |
||
956 | break;
|
||
957 | case AOS_SHUTDOWN_DEEPSLEEP:
|
||
958 | BL_CALLBACK_TABLE_ADDRESS->cbShutdownDeepsleep(); |
||
959 | break;
|
||
960 | case AOS_SHUTDOWN_TRANSPORTATION:
|
||
961 | BL_CALLBACK_TABLE_ADDRESS->cbShutdownTransportation(); |
||
962 | break;
|
||
963 | case AOS_SHUTDOWN_RESTART:
|
||
964 | BL_CALLBACK_TABLE_ADDRESS->cbShutdownRestart(); |
||
965 | break;
|
||
966 | // must never occur
|
||
967 | case AOS_SHUTDOWN_NONE:
|
||
968 | default:
|
||
969 | break;
|
||
970 | } |
||
971 | |||
972 | return;
|
||
973 | } |