Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_system.c @ 960338cc

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