Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_system.c @ 1d3e002f

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