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