Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_main.cpp @ efbf7cb1

History | View | Annotate | Download (45.046 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_main.cpp
21
 * @brief   Main function.
22
 * @details Main function with SSSP and initialization,
23
 *          extendable via hooks.
24
 *
25
 * @addtogroup aos_system
26
 * @{
27
 */
28
29 e2d7143f Thomas Schöpping
#include <aos_system.h>
30 cb835a3e Thomas Schöpping
#include <module.h>
31 e545e620 Thomas Schöpping
32 b6b45e4c Thomas Schöpping
/*
33
 * hook to add further includes
34
 */
35 512abac1 Thomas Schöpping
#if defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER)
36
#include AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER
37 b6b45e4c Thomas Schöpping
#endif
38
39 e545e620 Thomas Schöpping
/**
40
 * @brief   Event mask to identify I/O events.
41
 */
42 6b53f6bf Thomas Schöpping
#define IOEVENT_MASK                            EVENT_MASK(0)
43 e545e620 Thomas Schöpping
44
/**
45
 * @brief   Event mask to identify OS events.
46
 */
47 6b53f6bf Thomas Schöpping
#define OSEVENT_MASK                            EVENT_MASK(1)
48 e545e620 Thomas Schöpping
49
/**
50 933df08e Thomas Schöpping
 * @brief   Event mask to idetify CAN events.
51
 */
52
#define CANEVENT_MASK                           EVENT_MASK(2)
53
54
/**
55
 * @brief   Event mask to idetify timeout events.
56
 */
57
#define TIMEOUTEVENT_MASK                       EVENT_MASK(3)
58
59
/**
60
 * @brief   Event mask to idetify signal delay events.
61
 */
62
#define DELAYEVENT_MASK                         EVENT_MASK(4)
63
64 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
65
66 933df08e Thomas Schöpping
/**
67
 * @brief   CAN message identifier for initialization of the SSSP stack initialization sequence.
68
 */
69
#define SSSP_STACKINIT_CANMSGID_INIT            0x003
70
71
/**
72
 * @brief   CAN message identifier for transmitting module IDs during the SSSP stack initialization sequence.
73
 */
74
#define SSSP_STACKINIT_CANMSGID_MODULEID        0x002
75
76
/**
77
 * @brief   CAN message identifier for abortion of the SSSP stack initialization sequence.
78
 */
79
#define SSSP_STACKINIT_CANMSGID_ABORT           0x001
80
81 1d3e002f Thomas Schöpping
#else /* AMIROOS_CFG_SSSP_ENABLE == false */
82
83
/**
84
 * @brief   Default shutdown mode if SSSP is unavailable.
85
 */
86
#define AOS_SHUTDOWN_DEFAULT                    AOS_SHUTDOWN_DEEPSLEEP
87
88
#endif /* AMIROOS_CFG_SSSP_ENABLE */
89 9ebb11a9 Thomas Schöpping
90 933df08e Thomas Schöpping
/**
91 9461fadc Thomas Schöpping
 * @brief   CAN message identifier for calender synchronization message.
92
 */
93
#define CALENDERSYNC_CANMSGID                   0x004
94
95
/**
96 e545e620 Thomas Schöpping
 * @brief   Listener object for I/O events.
97
 */
98
static event_listener_t _eventListenerIO;
99
100
/**
101
 * @brief   Listener object for OS events.
102
 */
103
static event_listener_t _eventListenerOS;
104
105
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
106
/**
107 ba516b61 Thomas Schöpping
 * @brief   I/O channel for the programmer interface.
108 e545e620 Thomas Schöpping
 */
109 ba516b61 Thomas Schöpping
static AosIOChannel _stdiochannel;
110
111 2dd2e257 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
112 ba516b61 Thomas Schöpping
/**
113
 * @brief   I/O shell channel for the programmer interface.
114
 */
115
static AosShellChannel _stdshellchannel;
116 2dd2e257 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)*/
117 1d3e002f Thomas Schöpping
#endif /* defined(MODULE_HAL_PROGIF) */
118 e545e620 Thomas Schöpping
119 b6b45e4c Thomas Schöpping
/*
120
 * hook to add further static variables
121
 */
122
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
123
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
124
#endif
125
126 e545e620 Thomas Schöpping
/**
127
 * @brief   Prints an error message about an unexpected event.
128
 *
129
 * @param[in] mask    The event mask.
130
 * @param[in] flags   The event flags.
131
 */
132 933df08e Thomas Schöpping
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
133 e545e620 Thomas Schöpping
{
134 6b53f6bf Thomas Schöpping
#if (AMIROOS_CFG_DBG == true)
135 1e5f7648 Thomas Schöpping
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
136 6b53f6bf Thomas Schöpping
#else
137 933df08e Thomas Schöpping
  (void)(mask);
138
  (void)(flags);
139 6b53f6bf Thomas Schöpping
#endif
140 3e1a9c79 Thomas Schöpping
  return;
141 933df08e Thomas Schöpping
}
142
143 9ebb11a9 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
144 933df08e Thomas Schöpping
/**
145
 * @brief   Callback function to be used during SSSP stack initialization sequence.
146
 *
147
 * @param[in] par   A pointer to an @p event_source_t to be fired.
148
 */
149
static void _ssspTimerCallback(void* par)
150
{
151
  aosDbgCheck(par != NULL);
152
153
  chSysLockFromISR();
154
  chEvtBroadcastI((event_source_t*)par);
155
  chSysUnlockFromISR();
156
157 e545e620 Thomas Schöpping
  return;
158
}
159 9ebb11a9 Thomas Schöpping
#endif /* AMIROOS_CFG_SSSP_ENABLE == true */
160 e545e620 Thomas Schöpping
161
/**
162 9461fadc Thomas Schöpping
 * @brief   Helper function to serialize data.
163 933df08e Thomas Schöpping
 *
164
 * @param[out]  dst   Pointer to the output buffer.
165 9461fadc Thomas Schöpping
 * @param[in]   src   Data to be serialized.
166
 * @param[in]   n     Number of bytes to serialize.
167 933df08e Thomas Schöpping
 */
168 9461fadc Thomas Schöpping
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n)
169 933df08e Thomas Schöpping
{
170
  aosDbgCheck(dst != NULL);
171 9461fadc Thomas Schöpping
  aosDbgCheck(n > 0 && n <= 8);
172 933df08e Thomas Schöpping
173 9461fadc Thomas Schöpping
  for (uint8_t byte = 0; byte < n; ++byte) {
174 933df08e Thomas Schöpping
    dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF);
175
  }
176
177
  return;
178
}
179
180
/**
181 9461fadc Thomas Schöpping
 * @brief   Helper function to deserialize data.
182 933df08e Thomas Schöpping
 *
183
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
184 9461fadc Thomas Schöpping
 * @param[in] n     Number of bytes to deserialize.
185 933df08e Thomas Schöpping
 *
186
 * @return    The deserialized 32 bit data.
187
 */
188 9461fadc Thomas Schöpping
inline uint64_t _deserialize(uint8_t* src, const uint8_t n)
189 933df08e Thomas Schöpping
{
190
  aosDbgCheck(src != NULL);
191 9461fadc Thomas Schöpping
  aosDbgCheck(n > 0 && n <= 8);
192 933df08e Thomas Schöpping
193 9461fadc Thomas Schöpping
  uint64_t result = 0;
194
  for (uint8_t byte = 0; byte < n; ++byte) {
195
    result |= ((uint64_t)src[byte]) << (byte * 8);
196
  }
197
198
  return result;
199
}
200
201
/**
202
 * @brief   Converter function to encode a TM value to a single unsigned 64 bit integer.
203
 *
204
 * @details Contents of the TM struct are mapped as follows:
205
 *            bits  |63     62|61      53|52    50|49         26|25     22|21     17|16     12|11      6|5       0|
206
 *            #bits |       2 |        9 |      3 |          24 |       4 |       5 |       5 |       6 |       6 |
207
 *            value |   isdst |     yday |   wday |        year |     mon |    mday |    hour |     min |     sec |
208
 *            range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
209
 *          The Daylight Saving Time Flag (isdsst) is encoded as follows:
210
 *            DST not in effect         -> 0
211
 *            DST in effect             -> 1
212
 *            no information available  -> 2
213
 *
214
 * @param[in] src   Pointer to the TM struct to encode.
215
 *