Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_system.c @ 7368d8da

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