Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (38.603 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  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
/**
20
 * @file    aos_main.cpp
21
 * @brief   Main function.
22
 *
23
 * @addtogroup aos_system
24
 * @{
25
 */
26

    
27
#include <amiroos.h>
28

    
29
/*
30
 * hook to add further includes
31
 */
32
#if defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER)
33
#include AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER
34
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_INCLUDE_HEADER) */
35

    
36
/******************************************************************************/
37
/* LOCAL DEFINITIONS                                                          */
38
/******************************************************************************/
39

    
40
/**
41
 * @brief   Event mask to identify GPIO events.
42
 */
43
#define EVENTMASK_GPIO                          EVENT_MASK(0)
44

    
45
/**
46
 * @brief   Event mask to identify OS events.
47
 */
48
#define EVENTMASK_OS                            EVENT_MASK(1)
49

    
50
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
51

    
52

    
53
#if (AMIROOS_CFG_SSSP_MSI == true) || defined(__DOXYGEN__)
54

    
55
/**
56
 * @brief   Event mask to identify SSSP timeout events (MSI only).
57
 */
58
#define EVENTMASK_SSSPTIMEOUT                   EVENT_MASK(2)
59

    
60
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
61

    
62
/**
63
 * @brief   Event mask to identify SSSP delay events.
64
 */
65
#define EVENTMASK_SSSPDELAY                     EVENT_MASK(3)
66

    
67
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
68

    
69
/******************************************************************************/
70
/* EXPORTED VARIABLES                                                         */
71
/******************************************************************************/
72

    
73
/******************************************************************************/
74
/* LOCAL TYPES                                                                */
75
/******************************************************************************/
76

    
77
/******************************************************************************/
78
/* LOCAL VARIABLES                                                            */
79
/******************************************************************************/
80

    
81
/**
82
 * @brief   Listener object for GPIO events.
83
 */
84
static event_listener_t _eventListenerGPIO;
85

    
86
/**
87
 * @brief   Listener object for OS events.
88
 */
89
static event_listener_t _eventListenerOS;
90

    
91
#if defined(MODULE_HAL_PROGIF) || defined(__DOXYGEN__)
92

    
93
/**
94
 * @brief   I/O channel for the programmer interface.
95
 */
96
static AosIOChannel _stdiochannel;
97

    
98
#endif /* defined(MODULE_HAL_PROGIF) */
99

    
100
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
101

    
102
/**
103
 * @brief   I/O shell channel for the programmer interface.
104
 */
105
static AosShellChannel _stdshellchannel;
106

    
107
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
108

    
109
/*
110
 * hook to add further static variables
111
 */
112
#if defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES)
113
AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES
114
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_STATIC_VARIABLES) */
115

    
116
/******************************************************************************/
117
/* LOCAL FUNCTIONS                                                            */
118
/******************************************************************************/
119

    
120
/**
121
 * @brief   Prints an error message about an unexpected event.
122
 *
123
 * @param[in] mask    The event mask.
124
 * @param[in] flags   The event flags.
125
 */
126
inline void _unexpectedEventError(const eventmask_t mask, const eventflags_t flags)
127
{
128
#if (AMIROOS_CFG_DBG == true)
129
  aosDbgPrintf("CTRL: unexpected/unknown event received. mask: 0x%08X; flags: 0x%08X\n", mask, flags);
130
#else /* (AMIROOS_CFG_DBG == true) */
131
  (void)(mask);
132
  (void)(flags);
133
#endif /* (AMIROOS_CFG_DBG == true) */
134
  return;
135
}
136

    
137
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_RTC == TRUE)) || defined(__DOXYGEN__)
138
#if (AMIROOS_CFG_SSSP_MASTER == true) || defined(__DOXYGEN__)
139

    
140
/**
141
 * @brief   Converter function to encode a TM value to a single unsigned 64 bit integer.
142
 *
143
 * @details Contents of the TM struct are mapped as follows:
144
 *            bits  |63     62|61      53|52    50|49         26|25     22|21     17|16     12|11      6|5       0|
145
 *            #bits |       2 |        9 |      3 |          24 |       4 |       5 |       5 |       6 |       6 |
146
 *            value |   isdst |     yday |   wday |        year |     mon |    mday |    hour |     min |     sec |
147
 *            range | special | [0, 365] | [0, 6] | [1900, ...] | [0, 11] | [1, 31] | [0, 23] | [0, 59] | [0, 61] |
148
 *          The Daylight Saving Time Flag (isdsst) is encoded as follows:
149
 *            DST not in effect         -> 0
150
 *            DST in effect             -> 1
151
 *            no information available  -> 2
152
 *
153
 * @param[in] src   Pointer to the TM struct to encode.
154
 *
155
 * @return  An unsigned 64 bit integer, which holds the encoded time value.
156
 */
157
inline uint64_t _TM2U64(struct tm* src)
158
{
159
  aosDbgCheck(src != NULL);
160

    
161
  return (((uint64_t)(src->tm_sec  & 0x0000003F) << (0))               |
162
          ((uint64_t)(src->tm_min  & 0x0000003F) << (6))               |
163
          ((uint64_t)(src->tm_hour & 0x0000001F) << (12))              |
164
          ((uint64_t)(src->tm_mday & 0x0000001F) << (17))              |
165
          ((uint64_t)(src->tm_mon  & 0x0000000F) << (22))              |
166
          ((uint64_t)(src->tm_year & 0x00FFFFFF) << (26))              |
167
          ((uint64_t)(src->tm_wday & 0x00000007) << (50))              |
168
          ((uint64_t)(src->tm_yday & 0x000001FF) << (53))              |
169
          ((uint64_t)((src->tm_isdst == 0) ? 0 : (src->tm_isdst > 0) ? 1 : 2) << (62)));
170
}
171

    
172
/**
173
 * @brief   Serializes 64 bit unsigned integer input and stores it in a byte array.
174
 * @details Serialization is performed in big-endian fashion.
175
 *
176
 * @param[out]  dst   Pointer to the output buffer.
177
 * @param[in]   src   Data to be serialized.
178
 */
179
inline void _serializeU64(uint8_t* dst, const uint64_t src)
180
{
181
  aosDbgCheck(dst != NULL);
182

    
183
  for (uint8_t byte = 0; byte < sizeof(uint64_t); ++byte) {
184
    dst[byte] = ((src >> ((sizeof(uint64_t) - (byte+1)) * 8)) & 0xFF);
185
  }
186

    
187
  return;
188
}
189

    
190
#endif /* (AMIROOS_CFG_SSSP_MASTER == true) */
191
#if (AMIROOS_CFG_SSSP_MASTER != true) || defined(__DOXYGEN__)
192

    
193
/**
194
 * @brief   Deserialize 64 bit unsigned integer data from a buffer.
195
 * @details Data is assumed to be serialized in big-endian fashion.
196
 *
197
 * @param[in] src   Pointer to the buffer of data to be deserialzed.
198
 *
199
 * @return    Deserialized 64 bit data.
200
 */
201
inline uint64_t _deserializeU64(uint8_t* src)
202
{
203
  aosDbgCheck(src != NULL);
204

    
205
  uint64_t result = 0;
206
  for (uint8_t byte = 0; byte < sizeof(uint64_t); ++byte) {
207
    result |= ((uint64_t)src[byte] << ((sizeof(uint64_t) - (byte+1)) * 8));
208
  }
209

    
210
  return result;
211
}
212

    
213
/**
214
 * @brief   Converter functiomn to retrieve the encoded TM value from an unsigned 64 bit integer.
215
 *
216
 * @details For information on the encoding, please refer to @p _TM2U64 function.
217
 *
218
 * @param[out] dst  The TM struct to fill with the decoded values.
219
 * @param[in]  src  Unsigned 64 bit integer holding the encoded TM value.
220
 */
221
inline void _U642TM(struct tm* dst, const uint64_t src)
222
{
223
  aosDbgCheck(dst != NULL);
224

    
225
  dst->tm_sec  = (src >> 0)  & 0x0000003F;
226
  dst->tm_min  = (src >> 6)  & 0x0000003F;
227
  dst->tm_hour = (src >> 12) & 0x0000001F;
228
  dst->tm_mday = (src >> 17) & 0x0000001F;
229
  dst->tm_mon  = (src >> 22) & 0x0000000F;
230
  dst->tm_year = (src >> 26) & 0x00FFFFFF;
231
  dst->tm_wday = (src >> 50) & 0x00000007;
232
  dst->tm_yday = (src >> 53) & 0x000001FF;
233
  dst->tm_isdst = (((src >> 62) & 0x03) == 0) ? 0 : (((src >> 62) & 0x03) > 0) ? 1 : -1;
234

    
235
  return;
236
}
237

    
238
#endif /* (AMIROOS_CFG_SSSP_MASTER != true) */
239
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (HAL_USE_RTC == TRUE) */
240

    
241
/**
242
 * @brief   Application entry point.
243
 */
244
int main(void)
245
{
246
  // local variables
247
  eventmask_t eventmask = 0;
248
  eventflags_t eventflags = 0;
249
  aos_shutdown_t shutdown = AOS_SHUTDOWN_NONE;
250
#if defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES)
251
  AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES
252
#endif /* defined(AMIROOS_CFG_MAIN_EXTRA_THREAD_VARIABLES) */
253

    
254
  /*
255
   * ##########################################################################
256
   * # system initialization                                                  #
257
   * ##########################################################################
258
   */
259

    
260
  /* hardware, kernel, and operating system initialization */
261
  // ChibiOS/HAL and custom hal additions (if any)
262
  halInit();
263
#if defined(MODULE_INIT_HAL_EXTRA)
264
  MODULE_INIT_HAL_EXTRA();
265
#endif /* defined(MODULE_INIT_HAL_EXTRA) */
266

    
267
  // ChibiOS/RT kernel and custom kernel additions (if any)
268
  chSysInit();
269
#if defined(MODULE_INIT_KERNEL_EXTRA)
270
  MODULE_INIT_KERNEL_EXTRA();
271
#endif /* defined(MODULE_INIT_KERNEL_EXTRA) */
272

    
273
  // AMiRo-OS, additional interrupts and custom OS additions (if any)
274
#if (AMIROOS_CFG_SHELL_ENABLE == true)
275
  aosSysInit(moduleShellPrompt);
276
#else /* (AMIROOS_CFG_SHELL_ENABLE == true) */
277
  aosSysInit(NULL);
278
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
279
#if defined(MODULE_INIT_INTERRUPTS)
280
  MODULE_INIT_INTERRUPTS();
281
#endif
282
#if defined(MODULE_INIT_OS_EXTRA)
283
  MODULE_INIT_OS_EXTRA();
284
#endif /* defined(MODULE_INIT_OS_EXTRA) */
285

    
286
  /* event associations */
287
#if (AMIROOS_CFG_SSSP_ENABLE == true)
288
  {
289
    eventflags_t flagsmask = AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK | moduleSsspEventflagPD() | moduleSsspEventflagS();
290
#if (AMIROOS_CFG_SSSP_MSI == true)
291
#if (AMIROOS_CFG_SSSP_STACK_START != true)
292
    flagsmask |= moduleSsspEventflagDN();
293
#endif /* (AMIROOS_CFG_SSSP_STACK_START != true) */
294
#if (AMIROOS_CFG_SSSP_STACK_END != true)
295
    flagsmask |= moduleSsspEventflagUP();
296
#endif /* (AMIROOS_CFG_SSSP_STACK_END != true) */
297
#endif /* (AMIROOS_CFG_SSSP_MSI == true) */
298
    chEvtRegisterMaskWithFlags(&aos.events.gpio, &_eventListenerGPIO, EVENTMASK_GPIO, flagsmask);
299
  }
300
#else /* (AMIROOS_CFG_SSSP_ENABLE == true) */
301
  if (AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK) {
302
    chEvtRegisterMaskWithFlags(&aos.events.gpio, &_eventListenerGPIO, EVENTMASK_GPIO, AMIROOS_CFG_MAIN_LOOP_GPIOEVENT_FLAGSMASK);
303
  }
304
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
305
  chEvtRegisterMask(&aos.events.os, &_eventListenerOS, EVENTMASK_OS);
306

    
307
#if (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_STARTUP == true)
308

    
309
  /* perform SSSP basic initialization stage */
310

    
311
#if defined(MODULE_SSSP_BASICINIT_HOOK)
312
#if defined(MODULE_SSSP_BASICINIT_HOOK_ARGS)
313
  MODULE_SSSP_BASICINIT_HOOK(MODULE_SSSP_BASICINIT_HOOK_ARGS);
314
#else /* defined(MODULE_SSSP_BASICINIT_HOOK_ARGS) */
315
  MODULE_SSSP_BASICINIT_HOOK();
316
#endif /* defined(MODULE_SSSP_BASICINIT_HOOK_ARGS) */
317
#endif /* defined(MODULE_SSSP_BASICINIT_HOOK) */
318

    
319
  // proceed to startup stage 1.2
320
#if (AMIROOS_CFG_SSSP_MASTER == true)
321
  while ((shutdown == AOS_SHUTDOWN_NONE) && (aosSsspProceed(NULL, EVENTMASK_SSSPDELAY, &eventmask) != AOS_SUCCESS)) {
322
    /*
323
     * This code is executed if the received event was not about the delay.
324
     * The received event could be casued by any listener.
325
     */
326
    // GPIO event
327
    if (eventmask & _eventListenerGPIO.events) {
328
      eventflags = chEvtGetAndClearFlags(&_eventListenerGPIO);
329
      // PD event
330
      if (eventflags & moduleSsspEventflagPD()) {
331
        shutdown = AOS_SHUTDOWN_PASSIVE;
332
      } else {
333
#if defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK)
334
        MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK(eventmask, eventflags);
335
#else /* defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK) */
336
        /* silently ignore any other GPIO events */
337
#endif /* defined(MODULE_SSSP_STARTUP_1_1_GPIOEVENT_HOOK) */
338
      }
339
    }
340
    // OS event
341
    else if (eventmask & _eventListenerOS.events) {
342
      eventflags = chEvtGetAndClearFlags(&_eventListenerOS);
343
      _unexpectedEventError(eventmask, eventflags);
344
    }
345
    // unknown event
346
    else {
347
      _unexpectedEventError(eventmask, 0);
348
    }
349
  }
350
  if (shutdown == AOS_SHUTDOWN_NONE) {
351
    aosDbgAssert(aos.sssp.stage == AOS_SSSP_STAGE_STARTUP_1_2);
352
  }
3