Statistics
| Branch: | Tag: | Revision:

amiro-os / os / core / src / aos_system.c @ 2674917e

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