Statistics
| Branch: | Tag: | Revision:

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

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