Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (43.012 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 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 e545e620 Thomas Schöpping
#include <amiroos.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
/**
65
 * @brief   CAN message identifier for initialization of the SSSP stack initialization sequence.
66
 */
67
#define SSSP_STACKINIT_CANMSGID_INIT            0x003
68
69
/**
70
 * @brief   CAN message identifier for transmitting module IDs during the SSSP stack initialization sequence.
71
 */
72
#define SSSP_STACKINIT_CANMSGID_MODULEID        0x002
73
74
/**
75
 * @brief   CAN message identifier for abortion of the SSSP stack initialization sequence.
76
 */
77
#define SSSP_STACKINIT_CANMSGID_ABORT           0x001
78
79
/**
80 9461fadc Thomas Schöpping
 * @brief   CAN message identifier for calender synchronization message.
81
 */
82
#define CALENDERSYNC_CANMSGID                   0x004
83
84
/**
85 e545e620 Thomas Schöpping
 * @brief   Listener object for I/O events.
86
 */
87
static event_listener_t _eventListenerIO;
88
89
/**
90
 * @brief   Listener object for OS events.
91
 */
92
static event_listener_t _eventListenerOS;
93
94
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
95
/**
96 ba516b61 Thomas Schöpping
 * @brief   I/O channel for the programmer interface.
97 e545e620 Thomas Schöpping
 */
98 ba516b61 Thomas Schöpping
static AosIOChannel _stdiochannel;
99
100 b8268050 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
101 ba516b61 Thomas Schöpping
/**
102
 * @brief   I/O shell channel for the programmer interface.
103
 */
104
static AosShellChannel _stdshellchannel;
105 e545e620 Thomas Schöpping
#endif
106 b8268050 Thomas Schöpping
#endif
107 e545e620 Thomas Schöpping
108 b6b45e4c Thomas Schöpping
/*
109
 * hook to add further static variables
110
 */
111
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
112
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
113
#endif
114
115 e545e620 Thomas Schöpping
/**
116
 * @brief   Prints an error message about an unexpected event.
117
 *
118
 * @param[in] mask    The event mask.
119
 * @param[in] flags   The event flags.
120
 */
121 933df08e Thomas Schöpping
static inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
122 e545e620 Thomas Schöpping
{
123 6b53f6bf Thomas Schöpping
#if (AMIROOS_CFG_DBG == true)
124 1e5f7648 Thomas Schöpping
  aosprintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
125 6b53f6bf Thomas Schöpping
#else
126 933df08e Thomas Schöpping
  (void)(mask);
127
  (void)(flags);
128 6b53f6bf Thomas Schöpping
#endif
129 3e1a9c79 Thomas Schöpping
  return;
130 933df08e Thomas Schöpping
}
131
132
/**
133
 * @brief   Callback function to be used during SSSP stack initialization sequence.
134
 *
135
 * @param[in] par   A pointer to an @p event_source_t to be fired.
136
 */
137
static void _ssspTimerCallback(void* par)
138
{
139
  aosDbgCheck(par != NULL);
140
141
  chSysLockFromISR();
142
  chEvtBroadcastI((event_source_t*)par);
143
  chSysUnlockFromISR();
144
145 e545e620 Thomas Schöpping
  return;
146
}
147
148
/**
149 9461fadc Thomas Schöpping
 * @brief   Helper function to serialize data.
150 933df08e Thomas Schöpping
 *
151
 * @param[out]  dst   Pointer to the output buffer.
152 9461fadc Thomas Schöpping
 * @param[in]   src   Data to be serialized.
153
 * @param[in]   n     Number of bytes to serialize.
154 933df08e Thomas Schöpping
 */
155 9461fadc Thomas Schöpping
inline void _serialize(uint8_t* dst, const uint64_t src, const uint8_t n)
156 933df08e Thomas Schöpping
{
157
  aosDbgCheck(dst != NULL);
158 9461fadc Thomas Schöpping
  aosDbgCheck(n > 0 && n <= 8);
159 933df08e Thomas Schöpping
160 9461fadc Thomas Schöpping
  for (uint8_t byte = 0; byte < n; ++byte) {
161 933df08e Thomas Schöpping
    dst[byte] = (uint8_t)((src >> (byte * 8)) & 0xFF);
162
  }
163
164
  return;
165
}
166
167
/**
168 9461fadc Thomas Schöpping
 * @brief   Helper function to deserialize data.
169 933df08e Thomas Schöpping
 *
170
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
171 9461fadc Thomas Schöpping
 * @param[in] n     Number of bytes to deserialize.
172 933df08e Thomas Schöpping
 *
173
 * @return    The deserialized 32 bit data.
174
 */
175 9461fadc Thomas Schöpping
inline uint64_t _deserialize(uint8_t* src, const uint8_t n)
176 933df08e Thomas Schöpping
{
177
  aosDbgCheck(src != NULL);
178 9461fadc Thomas Schöpping
  aosDbgCheck(n > 0 && n <= 8);
179 933df08e Thomas Schöpping
180 9461fadc Thomas Schöpping
  uint64_t result = 0;
181
  for (uint8_t byte = 0; byte < n; ++byte) {
182
    result |= ((uint64_t)src[byte]) << (byte * 8);
183
  }
184
185
  return result;
186
}
187
188
/**
189
 * @brief   Converter function to encode a TM value to a single unsigned 64 bit integer.
190
 *
191
 * @details Contents of the TM struct are mapped as follows:
192
 *            bits  |63     62|61      53|52    50|49         26|25     22|21     17|16     12|11      6|5       0|
193
 *            #bits |       2 |        9 |      3 |          24 |       4 |       5 |       5 |       6 |       6 |
194
 *            value |   isdst |     yday |   wday |        year |     mon |    mday |    hour |     min |     sec |
195
 *            range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
196
 *          The Daylight Saving Time Flag (isdsst) is encoded as follows:
197
 *            DST not in effect         -> 0
198
 *            DST in effect             -> 1
199
 *            no information available  -> 2
200
 *
201
 * @param[in] src   Pointer to the TM struct to encode.
202
 *
203
 * @return  An unsigned 64 bit integer, which holds the encoded time value.
204
 */
205
inline uint64_t _TM2U64(struct tm* src)
206
{
207
  aosDbgCheck(src != NULL);
208
209
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
210
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
211
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
212
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
213
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
214
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
215
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
216
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
217
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
218
}
219
220
/**
221