Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_system.c @ 872e1ebd

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