Statistics
| Branch: | Tag: | Revision:

amiro-os / os / core / src / aos_system.c @ 3e1a9c79

History | View | Annotate | Download (34.255 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  Thomas Schöpping et al.
4

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