Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_system.c @ e03a021e

History | View | Annotate | Download (39.641 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 96621a83 Thomas Schöpping
Copyright (C) 2016..2020  Thomas Schöpping et al.
4 e545e620 Thomas Schöpping

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