Statistics
| Branch: | Tag: | Revision:

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

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