Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_system.c @ 5c9e9b9d

History | View | Annotate | Download (39.39 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 84f0ce9e Thomas Schöpping
Copyright (C) 2016..2019  Thomas Schöpping et al.
4 e545e620 Thomas Schöpping

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);