Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_system.c @ 96621a83

History | View | Annotate | Download (36.139 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  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
/**
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
#include <amiroos.h>
30
#include <stdarg.h>
31
#include <string.h>
32
#include <stdlib.h>
33

    
34
#if (AMIROOS_CFG_TESTS_ENABLE == true)
35
#include <ch_test.h>
36
#include <rt_test_root.h>
37
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
38

    
39
/******************************************************************************/
40
/* LOCAL DEFINITIONS                                                          */
41
/******************************************************************************/
42

    
43
/**
44
 * @brief   Period of the system timer.
45
 */
46
#define SYSTIMER_PERIOD               (TIME_MAX_SYSTIME - CH_CFG_ST_TIMEDELTA)
47

    
48
/**
49
 * @brief   Width of the printable system info text.
50
 */
51
#define SYSTEM_INFO_WIDTH             80
52

    
53
/**
54
 * @brief   Width of the name column of the system info table.
55
 */
56
#define SYSTEM_INFO_NAMEWIDTH         20
57

    
58
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
59

    
60
/**
61
 * @brief   Number of entries in the system shell input buffer.
62
 */
63
#define SYSTEM_SHELL_BUFFERENTRIES    (1 + AMIROOS_CFG_SHELL_HISTLENGTH)
64

    
65
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
66

    
67
/******************************************************************************/
68
/* EXPORTED VARIABLES                                                         */
69
/******************************************************************************/
70

    
71
/**
72
 * @brief   Global system object.
73
 */
74
aos_system_t aos;
75

    
76
/******************************************************************************/
77
/* LOCAL TYPES                                                                */
78
/******************************************************************************/
79

    
80
/******************************************************************************/
81
/* LOCAL VARIABLES                                                            */
82
/******************************************************************************/
83

    
84
/*
85
 * forward declarations
86
 */
87
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
88
static int _shellcmd_configcb(BaseSequentialStream* stream, int argc, char* argv[]);
89
static int _shellcmd_infocb(BaseSequentialStream* stream, int argc, char* argv[]);
90
static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* argv[]);
91
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
92
static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char* argv[]);
93
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
94
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
95

    
96
/**
97
 * @brief   Timer to accumulate system uptime.
98
 */
99
static virtual_timer_t _systimer;
100

    
101
/**
102
 * @brief   Accumulated system uptime.
103
 */
104
static aos_timestamp_t _uptime;
105

    
106
/**
107
 * @brief   Timer register value of last accumulation.
108
 */
109
static systime_t _synctime;
110

    
111
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
112

    
113
/**
114
 * @brief   Shell thread working area.
115
 */
116
static THD_WORKING_AREA(_shell_wa, AMIROOS_CFG_SHELL_STACKSIZE);
117

    
118
/**
119
 * @brief   Shell input buffer.
120
 */
121
static char _shell_buffer[SYSTEM_SHELL_BUFFERENTRIES * AMIROOS_CFG_SHELL_LINEWIDTH];
122

    
123
/**
124
 * @brief   Shell command to retrieve system information.
125
 */
126
static AOS_SHELL_COMMAND(_shellcmd_info, "module:info", _shellcmd_infocb);
127

    
128
/**
129
 * @brief   Shell command to set or retrieve system configuration.
130
 */
131
static AOS_SHELL_COMMAND(_shellcmd_config, "module:config", _shellcmd_configcb);
132

    
133
/**
134
 * @brief   Shell command to shutdown the system.
135
 */
136
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
137
static AOS_SHELL_COMMAND(_shellcmd_shutdown, "system:shutdown", _shellcmd_shutdowncb);
138
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
139
static AOS_SHELL_COMMAND(_shellcmd_shutdown, "module:shutdown", _shellcmd_shutdowncb);
140
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
141

    
142
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
143

    
144
/**
145
 * @brief   Shell kommand to run a test of the ChibiOS/RT kernel.
146
 */
147
static AOS_SHELL_COMMAND(_shellcmd_kerneltest, "kernel:test", _shellcmd_kerneltestcb);
148

    
149
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
150
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
151

    
152
/******************************************************************************/
153
/* LOCAL FUNCTIONS                                                            */
154
/******************************************************************************/
155

    
156
/**
157
 * @brief   Print a separator line.
158
 *
159
 * @param[in] stream    Stream to print to or NULL to print to all system streams.
160
 * @param[in] c         Character to use.
161
 * @param[in] n         Length of the separator line.
162
 *
163
 * @return  Number of characters printed.
164
 */
165
static unsigned int _printSystemInfoSeparator(BaseSequentialStream* stream, const char c, const unsigned int n)
166
{
167
  aosDbgCheck(stream != NULL);
168

    
169
  // print the specified character n times
170
  for (unsigned int i = 0; i < n; ++i) {
171
    streamPut(stream, (uint8_t)c);
172
  }
173
  streamPut(stream, '\n');
174

    
175
  return n+1;
176
}
177

    
178
/**
179
 * @brief   Print a system information line.
180
 * @details Prints a system information line with the following format:
181
 *            "<name>[spaces]fmt"
182
 *          The combined width of "<name>[spaces]" can be specified in order to align <fmt> on multiple lines.
183
 *          Note that there is not trailing newline added implicitely.
184
 *
185
 * @param[in] stream      Stream to print to or NULL to print to all system streams.
186
 * @param[in] name        Name of the entry/line.
187
 * @param[in] namewidth   Minimum width of the name column.
188
 * @param[in] fmt         Formatted string of information content.
189
 *
190
 * @return  Number of characters printed.
191
 */
192
static unsigned int _printSystemInfoLine(BaseSequentialStream* stream, const char* name, const unsigned int namewidth, const char* fmt, ...)
193
{
194
  aosDbgCheck(stream != NULL);
195
  aosDbgCheck(name != NULL);
196

    
197
  unsigned int n = 0;
198
  va_list ap;
199

    
200
  va_start(ap, fmt);
201
  n += (unsigned int)chprintf(stream, name);
202
  // print at least a single space character
203
  do {
204
    streamPut(stream, ' ');
205
    ++n;
206
  } while (n < namewidth);
207
  n += (unsigned int)chvprintf(stream, fmt, ap);
208
  va_end(ap);
209

    
210
  streamPut(stream, '\n');
211
  ++n;
212

    
213
  return n;
214
}
215

    
216
/**
217
 * @brief   Prints information about the system.
218
 *
219
 * @param[in] stream    Stream to print to.
220
 */
221
static void _printSystemInfo(BaseSequentialStream* stream)
222
{
223
  aosDbgCheck(stream != NULL);
224

    
225
  // local variables
226
#if (HAL_USE_RTC == TRUE)
227
  struct tm dt;
228
  aosSysGetDateTime(&dt);
229
#endif /* (HAL_USE_RTC == TRUE) */
230

    
231
  // print static information about module and operating system
232
  _printSystemInfoSeparator(stream, '=', SYSTEM_INFO_WIDTH);
233
  _printSystemInfoLine(stream, "Module", SYSTEM_INFO_NAMEWIDTH, "%s", BOARD_NAME);
234
#if defined(PLATFORM_NAME)
235
  _printSystemInfoLine(stream, "Platform", SYSTEM_INFO_NAMEWIDTH, "%s", PLATFORM_NAME);
236
#endif /* defined(PLATFORM_NAME) */
237
#if defined(PORT_CORE_VARIANT_NAME)
238
  _printSystemInfoLine(stream, "Core Variant", SYSTEM_INFO_NAMEWIDTH, "%s", PORT_CORE_VARIANT_NAME);
239
#endif /* defined(PORT_CORE_VARIANT_NAME) */
240
  _printSystemInfoLine(stream, "Architecture", SYSTEM_INFO_NAMEWIDTH, "%s", PORT_ARCHITECTURE_NAME);
241
  _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
242
#if (AMIROOS_CFG_SSSP_ENABLE == true)
243
  _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_SSSP_VERSION_MAJOR, AOS_SSSP_VERSION_MINOR);
244
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
245
  _printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE);
246
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
247
  _printSystemInfoLine(stream, "AMiRo-LLD" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (periphAL %u.%u)", AMIROLLD_VERSION_MAJOR, AMIROLLD_VERSION_MINOR, AMIROLLD_VERSION_PATCH, AMIROLLD_RELEASE_TYPE, PERIPHAL_VERSION_MAJOR, PERIPHAL_VERSION_MINOR);
248
  _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");
249
  _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");
250
  _printSystemInfoLine(stream, "build type", SYSTEM_INFO_NAMEWIDTH,"%s", (AMIROOS_CFG_DBG == true) ? "debug" : "release");
251
  _printSystemInfoLine(stream, "Compiler" , SYSTEM_INFO_NAMEWIDTH, "%s %u.%u.%u", "GCC", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); // TODO: support other compilers than GCC
252
  _printSystemInfoLine(stream, "Compiled" , SYSTEM_INFO_NAMEWIDTH, "%s - %s", __DATE__, __TIME__);
253

    
254
  // print static information about the bootloader
255
#if (AMIROOS_CFG_BOOTLOADER != AOS_BOOTLOADER_NONE)
256
  _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
257
#endif /* (AMIROOS_CFG_BOOTLOADER != AOS_BOOTLOADER_NONE) */
258
#if (AMIROOS_CFG_BOOTLOADER == AOS_BOOTLOADER_AMiRoBLT)
259
  if (BL_CALLBACK_TABLE_ADDRESS->magicNumber == BL_MAGIC_NUMBER) {
260
    _printSystemInfoLine(stream, "AMiRo-BLT", SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)", BL_CALLBACK_TABLE_ADDRESS->vBootloader.major, BL_CALLBACK_TABLE_ADDRESS->vBootloader.minor, BL_CALLBACK_TABLE_ADDRESS->vBootloader.patch,
261
                         (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Release) ? "stable" :
262
                         (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_ReleaseCandidate) ? "release candidate" :
263
                         (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Beta) ? "beta" :
264
                         (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_Alpha) ? "alpha" :
265
                         (BL_CALLBACK_TABLE_ADDRESS->vBootloader.identifier == BL_VERSION_ID_AMiRoBLT_PreAlpha) ? "pre-alpha" :
266
                         "<release type unknown>",
267
                         BL_CALLBACK_TABLE_ADDRESS->vSSSP.major, BL_CALLBACK_TABLE_ADDRESS->vSSSP.minor);
268
#if (AMIROOS_CFG_SSSP_ENABLE == true)
269
    if (BL_CALLBACK_TABLE_ADDRESS->vSSSP.major != AOS_SSSP_VERSION_MAJOR) {
270
      const char* msg = "WARNING: AMiRo-BLT and AMiRo-OS implement incompatible SSSP versions!\n";
271
      stream ? chprintf(stream, msg) : aosprintf(msg);
272
    }
273
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
274
    _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
275
  } else {
276
    const char* msg = "WARNING: AMiRo-BLT incompatible or not available.\n";
277
    stream ? chprintf(stream, "%s", msg) : aosprintf("%s", msg);
278
  }
279
#endif /* (AMIROOS_CFG_BOOTLOADER == X) */
280

    
281
#if defined(AMIROOS_CFG_SYSINFO_HOOK)
282
  // print module specific information
283
  _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
284
  AMIROOS_CFG_SYSINFO_HOOK();
285
#endif /* defined(AMIROOS_CFG_SYSINFO_HOOK) */
286

    
287
  // print dynamic information about the module
288
  _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
289
#if (AMIROOS_CFG_SSSP_ENABLE == true)
290
  if (aos.sssp.moduleId != 0) {
291
    _printSystemInfoLine(stream, "Module ID", SYSTEM_INFO_NAMEWIDTH, "%u", aos.sssp.moduleId);
292
  } else {
293
    _printSystemInfoLine(stream, "Module ID", SYSTEM_INFO_NAMEWIDTH, "not available");
294
  }
295
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
296
#if (HAL_USE_RTC == TRUE)
297
  _printSystemInfoLine(stream, "Date", SYSTEM_INFO_NAMEWIDTH, "%04u-%02u-%02u (%s)",
298
                       dt.tm_year + 1900,
299
                       dt.tm_mon + 1,
300
                       dt.tm_mday,
301
                       (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");
302
  _printSystemInfoLine(stream, "Time"</