Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (35.752 KB)

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