Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_system.c @ 1a8fb642

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