Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (35.813 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 3940ba8a Thomas Schöpping
#include <amiroos.h>
30
#include <stdarg.h>
31 e545e620 Thomas Schöpping
#include <string.h>
32 8399aeae Thomas Schöpping
#include <stdlib.h>
33 3940ba8a Thomas Schöpping
34 e545e620 Thomas Schöpping
#if (AMIROOS_CFG_TESTS_ENABLE == true)
35
#include <ch_test.h>
36 0128be0f Marc Rothmann
#include <rt_test_root.h>
37 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
38 e545e620 Thomas Schöpping
39 f3ac1c96 Thomas Schöpping
/******************************************************************************/
40
/* LOCAL DEFINITIONS                                                          */
41
/******************************************************************************/
42
43 e545e620 Thomas Schöpping
/**
44
 * @brief   Period of the system timer.
45
 */
46 1e5f7648 Thomas Schöpping
#define SYSTIMER_PERIOD               (TIME_MAX_SYSTIME - CH_CFG_ST_TIMEDELTA)
47 e545e620 Thomas Schöpping
48
/**
49
 * @brief   Width of the printable system info text.
50
 */
51 08d86900 Thomas Schöpping
#define SYSTEM_INFO_WIDTH             80
52 e545e620 Thomas Schöpping
53 ba516b61 Thomas Schöpping
/**
54
 * @brief   Width of the name column of the system info table.
55
 */
56 08d86900 Thomas Schöpping
#define SYSTEM_INFO_NAMEWIDTH         20
57 ba516b61 Thomas Schöpping
58 f3ac1c96 Thomas Schöpping
/******************************************************************************/
59
/* EXPORTED VARIABLES                                                         */
60
/******************************************************************************/
61
62
/**
63
 * @brief   Global system object.
64
 */
65
aos_system_t aos;
66
67
/******************************************************************************/
68
/* LOCAL TYPES                                                                */
69
/******************************************************************************/
70
71
/******************************************************************************/
72
/* LOCAL VARIABLES                                                            */
73
/******************************************************************************/
74
75
/*
76
 * forward declarations
77
 */
78 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
79 e545e620 Thomas Schöpping
static int _shellcmd_configcb(BaseSequentialStream* stream, int argc, char* argv[]);
80
static int _shellcmd_infocb(BaseSequentialStream* stream, int argc, char* argv[]);
81
static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* argv[]);
82 f3ac1c96 Thomas Schöpping
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
83 e545e620 Thomas Schöpping
static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char* argv[]);
84 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
85 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
86 e545e620 Thomas Schöpping
87
/**
88
 * @brief   Timer to accumulate system uptime.
89
 */
90
static virtual_timer_t _systimer;
91
92
/**
93
 * @brief   Accumulated system uptime.
94
 */
95
static aos_timestamp_t _uptime;
96
97
/**
98
 * @brief   Timer register value of last accumulation.
99
 */
100
static systime_t _synctime;
101
102 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
103 3e1a9c79 Thomas Schöpping
104 e545e620 Thomas Schöpping
/**
105
 * @brief   Shell thread working area.
106
 */
107
THD_WORKING_AREA(_shell_wa, AMIROOS_CFG_SHELL_STACKSIZE);
108
109
/**
110
 * @brief   Shell input buffer.
111
 */
112
static char _shell_line[AMIROOS_CFG_SHELL_LINEWIDTH];
113
114
/**
115
 * @brief   Shell argument buffer.
116
 */
117
static char* _shell_arglist[AMIROOS_CFG_SHELL_MAXARGS];
118
119
/**
120
 * @brief   Shell command to retrieve system information.
121
 */
122 4c72a54c Thomas Schöpping
static AOS_SHELL_COMMAND(_shellcmd_info, "module:info", _shellcmd_infocb);
123 e545e620 Thomas Schöpping
124
/**
125
 * @brief   Shell command to set or retrieve system configuration.
126
 */
127 4c72a54c Thomas Schöpping
static AOS_SHELL_COMMAND(_shellcmd_config, "module:config", _shellcmd_configcb);
128 e545e620 Thomas Schöpping
129
/**
130
 * @brief   Shell command to shutdown the system.
131
 */
132 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
133 4c72a54c Thomas Schöpping
static AOS_SHELL_COMMAND(_shellcmd_shutdown, "system:shutdown", _shellcmd_shutdowncb);
134 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
135 4c72a54c Thomas Schöpping
static AOS_SHELL_COMMAND(_shellcmd_shutdown, "module:shutdown", _shellcmd_shutdowncb);
136 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
137 e545e620 Thomas Schöpping
138
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
139 cda14729 Thomas Schöpping
140 e545e620 Thomas Schöpping
/**
141
 * @brief   Shell kommand to run a test of the ChibiOS/RT kernel.
142
 */
143 4c72a54c Thomas Schöpping
static AOS_SHELL_COMMAND(_shellcmd_kerneltest, "kernel:test", _shellcmd_kerneltestcb);
144 cda14729 Thomas Schöpping
145 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
146 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
147 e545e620 Thomas Schöpping
148 f3ac1c96 Thomas Schöpping
/******************************************************************************/
149
/* LOCAL FUNCTIONS                                                            */
150
/******************************************************************************/
151 e545e620 Thomas Schöpping
152
/**
153
 * @brief   Print a separator line.
154
 *
155 ba516b61 Thomas Schöpping
 * @param[in] stream    Stream to print to or NULL to print to all system streams.
156 e545e620 Thomas Schöpping
 * @param[in] c         Character to use.
157
 * @param[in] n         Length of the separator line.
158
 *
159
 * @return  Number of characters printed.
160
 */
161
static unsigned int _printSystemInfoSeparator(BaseSequentialStream* stream, const char c, const unsigned int n)
162
{
163
  aosDbgCheck(stream != NULL);
164
165
  // print the specified character n times
166
  for (unsigned int i = 0; i < n; ++i) {
167 83e58975 Thomas Schöpping
    streamPut(stream, (uint8_t)c);
168 e545e620 Thomas Schöpping
  }
169
  streamPut(stream, '\n');
170
171
  return n+1;
172
}
173
174
/**
175
 * @brief   Print a system information line.
176
 * @details Prints a system information line with the following format:
177
 *            "<name>[spaces]fmt"
178
 *          The combined width of "<name>[spaces]" can be specified in order to align <fmt> on multiple lines.
179
 *          Note that there is not trailing newline added implicitely.
180
 *
181 ba516b61 Thomas Schöpping
 * @param[in] stream      Stream to print to or NULL to print to all system streams.
182 e545e620 Thomas Schöpping
 * @param[in] name        Name of the entry/line.
183 08d86900 Thomas Schöpping
 * @param[in] namewidth   Minimum width of the name column.
184 e545e620 Thomas Schöpping
 * @param[in] fmt         Formatted string of information content.
185
 *
186
 * @return  Number of characters printed.
187
 */
188
static unsigned int _printSystemInfoLine(BaseSequentialStream* stream, const char* name, const unsigned int namewidth, const char* fmt, ...)
189
{
190
  aosDbgCheck(stream != NULL);
191 ba516b61 Thomas Schöpping
  aosDbgCheck(name != NULL);
192 e545e620 Thomas Schöpping
193
  unsigned int n = 0;
194 ba516b61 Thomas Schöpping
  va_list ap;
195 e545e620 Thomas Schöpping
196 ba516b61 Thomas Schöpping
  va_start(ap, fmt);
197 83e58975 Thomas Schöpping
  n += (unsigned int)chprintf(stream, name);
198 08d86900 Thomas Schöpping
  // print at least a single space character
199
  do {
200 e545e620 Thomas Schöpping
    streamPut(stream, ' ');
201
    ++n;
202 08d86900 Thomas Schöpping
  } while (n < namewidth);
203 83e58975 Thomas Schöpping
  n += (unsigned int)chvprintf(stream, fmt, ap);
204 e545e620 Thomas Schöpping
  va_end(ap);