Statistics
| Branch: | Tag: | Revision:

amiro-os / os / core / inc / aos_system.h @ 1e5f7648

History | View | Annotate | Download (6.603 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
#ifndef _AMIROOS_SYSTEM_H_
20
#define _AMIROOS_SYSTEM_H_
21
22 ba516b61 Thomas Schöpping
#include <aos_iostream.h>
23 e545e620 Thomas Schöpping
#include <amiro-lld.h>
24
#include <aos_shell.h>
25
#include <aos_time.h>
26
#include <chprintf.h>
27
28
/**
29 ba516b61 Thomas Schöpping
 * @brief   Resolution of the system time measurement.
30 e545e620 Thomas Schöpping
 */
31 ba516b61 Thomas Schöpping
#define AOS_SYSTEM_TIME_RESOLUTION              ((MICROSECONDS_PER_SECOND + CH_CFG_ST_FREQUENCY - 1) / CH_CFG_ST_FREQUENCY)
32 e545e620 Thomas Schöpping
33
/**
34 ba516b61 Thomas Schöpping
 * @brief   System event flag which is emitted when a shutdown was initiated.
35 e545e620 Thomas Schöpping
 */
36 ba516b61 Thomas Schöpping
#define AOS_SYSTEM_EVENTFLAGS_SHUTDOWN          (eventflags_t)(1 << 0)
37 e545e620 Thomas Schöpping
38
/**
39
 * @brief   System event flag which is emitted when a shutdown to transportation mode was initiated.
40
 */
41 ba516b61 Thomas Schöpping
#define AOS_SYSTEM_EVENTFLAGS_TRANSPORTATION    (AOS_SYSTEM_EVENTFLAGS_SHUTDOWN | (eventflags_t)(1 << 1))
42 e545e620 Thomas Schöpping
43
/**
44
 * @brief   System event flag which is emitted when a shutdown to deepsleep mode was initiated.
45
 */
46 ba516b61 Thomas Schöpping
#define AOS_SYSTEM_EVENTFLAGS_DEEPSLEEP         (AOS_SYSTEM_EVENTFLAGS_SHUTDOWN | (eventflags_t)(1 << 2))
47 e545e620 Thomas Schöpping
48
/**
49
 * @brief   System event flag which is emitted when a shutdown to hibernate mode was initiated.
50
 */
51 ba516b61 Thomas Schöpping
#define AOS_SYSTEM_EVENTFLAGS_HIBERNATE         (AOS_SYSTEM_EVENTFLAGS_SHUTDOWN | (eventflags_t)(1 << 3))
52 e545e620 Thomas Schöpping
53
/**
54
 * @brief   System event flag which is emitted when a system restart was initiated.
55
 */
56 ba516b61 Thomas Schöpping
#define AOS_SYSTEM_EVENTFLAGS_RESTART           (AOS_SYSTEM_EVENTFLAGS_SHUTDOWN | (eventflags_t)(1 << 4))
57 e545e620 Thomas Schöpping
58
/**
59
 * @brief   Major version of the implemented SSSP.
60
 */
61 933df08e Thomas Schöpping
#define AOS_SYSTEM_SSSP_VERSION_MAJOR           1
62 e545e620 Thomas Schöpping
63
/**
64
 * @brief   Minor version of the implemented SSSP.
65
 */
66 933df08e Thomas Schöpping
#define AOS_SYSTEM_SSSP_VERSION_MINOR           4
67
68
/**
69
 * @brief   Timeout delay according to SSSP.
70
 * @details SSSP defines timeouts to be ten times longer than the signal delay time.
71
 */
72
#define AOS_SYSTEM_SSSP_TIMEOUT                 (10 * AMIROOS_CFG_SSSP_SIGNALDELAY)
73 e545e620 Thomas Schöpping
74
/**
75
 * @brief   Enumerator to identify shutdown types.
76
 */
77
typedef enum aos_shutdown {
78
  AOS_SHUTDOWN_NONE,            /**< Default value if no shutdown action was initiated */
79
  AOS_SHUTDOWN_PASSIVE,         /**< Passive shutdown (initiated by another module). */
80
  AOS_SHUTDOWN_HIBERNATE,       /**< Active shutdown to hibernate mode. */
81
  AOS_SHUTDOWN_DEEPSLEEP,       /**< Active shutdown to deepsleep mode. */
82
  AOS_SHUTDOWN_TRANSPORTATION,  /**< Active shutdown to transportation mode. */
83
  AOS_SHUTDOWN_RESTART,         /**< Active saystem restart request. */
84
} aos_shutdown_t;
85
86
/**
87 933df08e Thomas Schöpping
 * @brief   Enumerator of the several stages of SSSP.
88 e545e620 Thomas Schöpping
 */
89
typedef enum aos_ssspstage {
90 933df08e Thomas Schöpping
  AOS_SSSP_STARTUP_1_1  = 0x10, /**< Identifier of SSSP startup phase stage 1-1. */
91
  AOS_SSSP_STARTUP_1_2  = 0x11, /**< Identifier of SSSP startup phase stage 1-2. */
92
  AOS_SSSP_STARTUP_1_3  = 0x12, /**< Identifier of SSSP startup phase stage 1-3. */
93
  AOS_SSSP_STARTUP_2_1  = 0x14, /**< Identifier of SSSP startup phase stage 2-1. */
94
  AOS_SSSP_STARTUP_2_2  = 0x15, /**< Identifier of SSSP startup phase stage 2-2. */
95
  AOS_SSSP_STARTUP_3_1  = 0x18, /**< Identifier of SSSP startup phase stage 3-1. */
96
  AOS_SSSP_STARTUP_3_2  = 0x19, /**< Identifier of SSSP startup phase stage 3-2. */
97
  AOS_SSSP_STARTUP_3_3  = 0x1A, /**< Identifier of SSSP startup phase stage 3-3. */
98
  AOS_SSSP_STARTUP_3_4  = 0x1B, /**< Identifier of SSSP startup phase stage 3-4. */
99
  AOS_SSSP_OPERATION    = 0x20, /**< Identifier of SSSP operation pahse. */
100
  AOS_SSSP_SHUTDOWN_1_1 = 0x30, /**< Identifier of SSSP shutdown phase stage 1-1. */
101
  AOS_SSSP_SHUTDOWN_1_2 = 0x31, /**< Identifier of SSSP shutdown phase stage 1-2. */
102
  AOS_SSSP_SHUTDOWN_1_3 = 0x32, /**< Identifier of SSSP shutdown phase stage 1-3. */
103
  AOS_SSSP_SHUTDOWN_2_1 = 0x34, /**< Identifier of SSSP shutdown phase stage 2-1. */
104
  AOS_SSSP_SHUTDOWN_2_2 = 0x35, /**< Identifier of SSSP shutdown phase stage 2-2. */
105
  AOS_SSSP_SHUTDOWN_3   = 0x38, /**< Identifier of SSSP shutdown phase stage 3. */
106 e545e620 Thomas Schöpping
} aos_ssspstage_t;
107
108
/**
109 933df08e Thomas Schöpping
 * @brief   Type to represent module IDs.
110
 */
111
typedef uint16_t aos_ssspmoduleid_t;
112
113
/**
114 e545e620 Thomas Schöpping
 * @brief   AMiRo-OS base system structure.
115
 */
116
typedef struct aos_system {
117 6b53f6bf Thomas Schöpping
118 e545e620 Thomas Schöpping
  /**
119 6b53f6bf Thomas Schöpping
   * @brief   SSSP relevant data.
120 e545e620 Thomas Schöpping
   */
121 6b53f6bf Thomas Schöpping
  struct {
122
    /**
123
     * @brief   Current SSSP stage of the system.
124
     */
125
    aos_ssspstage_t stage;
126 933df08e Thomas Schöpping
127
    /**
128
     * @brief   Module identifier.
129
     * @details A value of 0 indicates an uninitialized ID.
130
     *          The vlaues 0 and ~0 are reserved und must not be set.
131
     */
132
    aos_ssspmoduleid_t moduleId;
133 6b53f6bf Thomas Schöpping
  } sssp;
134 e545e620 Thomas Schöpping
135
  /**
136 ba516b61 Thomas Schöpping
   * @brief   System I/O stream.
137
   */
138
  AosIOStream iostream;
139
140
  /**
141 e545e620 Thomas Schöpping
   * @brief   Event structure.
142
   */
143
  struct {
144
145
    /**
146 6b53f6bf Thomas Schöpping
     * @brief   I/O event source.
147 e545e620 Thomas Schöpping
     */
148 6b53f6bf Thomas Schöpping
    event_source_t io;
149 e545e620 Thomas Schöpping
150
    /**
151 6b53f6bf Thomas Schöpping
     * @brief   OS event source.
152 e545e620 Thomas Schöpping
     */
153 6b53f6bf Thomas Schöpping
    event_source_t os;
154 e545e620 Thomas Schöpping
  } events;
155
156
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
157
  /**
158
   * @brief   Pointer to the shell object.
159
   */
160 6b53f6bf Thomas Schöpping
  aos_shell_t shell;
161 e545e620 Thomas Schöpping
#endif
162
163
} aos_system_t;
164
165
/**
166
 * @brief   Global system object.
167
 */
168
extern aos_system_t aos;
169
170 ba516b61 Thomas Schöpping
/**
171
 * @brief   Printf function that uses the default system I/O stream.
172 933df08e Thomas Schöpping
 *
173
 * @param[in] fmt   Formatted string to print.
174 ba516b61 Thomas Schöpping
 */
175
#define aosprintf(fmt, ...)                     chprintf((BaseSequentialStream*)&aos.iostream, fmt, ##__VA_ARGS__)
176
177 e545e620 Thomas Schöpping
#ifdef __cplusplus
178
extern "C" {
179
#endif
180 6b53f6bf Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true)
181
  void aosSysInit(const char* shellPrompt);
182
#else
183
  void aosSysInit(void);
184
#endif
185 e545e620 Thomas Schöpping
  void aosSysStart(void);
186
  eventmask_t aosSysSsspStartupOsInitSyncCheck(event_listener_t* syncEvtListener);
187
  void aosSysGetUptimeX(aos_timestamp_t* ut);
188 8399aeae Thomas Schöpping
  void aosSysGetDateTime(struct tm* dt);
189
  void aosSysSetDateTime(struct tm* dt);
190 e545e620 Thomas Schöpping
  void aosSysShutdownInit(aos_shutdown_t shutdown);
191
  void aosSysStop(void);
192
  void aosSysDeinit(void);
193 1e5f7648 Thomas Schöpping
  void aosSysShutdownFinal(aos_shutdown_t shutdown);
194 e545e620 Thomas Schöpping
#ifdef __cplusplus
195
}
196
#endif
197
198
/**
199
 * @brief   Retrieves the system uptime.
200
 *
201
 * @param[out] ut   The system uptime.
202
 */
203
static inline void aosSysGetUptime(aos_timestamp_t* ut)
204
{
205
  aosDbgCheck(ut != NULL);
206
207
  chSysLock();
208
  aosSysGetUptimeX(ut);
209
  chSysUnlock();
210
211
  return;
212
}
213
214
#endif /* _AMIROOS_SYSTEM_H_ */