diff options
Diffstat (limited to 'lib/chibios/os/hal/osal/rt-nil/osal.h')
-rw-r--r-- | lib/chibios/os/hal/osal/rt-nil/osal.h | 1092 |
1 files changed, 1092 insertions, 0 deletions
diff --git a/lib/chibios/os/hal/osal/rt-nil/osal.h b/lib/chibios/os/hal/osal/rt-nil/osal.h new file mode 100644 index 000000000..25ac4de9b --- /dev/null +++ b/lib/chibios/os/hal/osal/rt-nil/osal.h | |||
@@ -0,0 +1,1092 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file osal.h | ||
19 | * @brief OSAL module header. | ||
20 | * | ||
21 | * @addtogroup OSAL | ||
22 | * @{ | ||
23 | */ | ||
24 | |||
25 | #ifndef OSAL_H | ||
26 | #define OSAL_H | ||
27 | |||
28 | #include <stddef.h> | ||
29 | #include <stdint.h> | ||
30 | #include <stdbool.h> | ||
31 | |||
32 | #include "ch.h" | ||
33 | |||
34 | /*===========================================================================*/ | ||
35 | /* Module constants. */ | ||
36 | /*===========================================================================*/ | ||
37 | |||
38 | /** | ||
39 | * @name Common constants | ||
40 | * @{ | ||
41 | */ | ||
42 | #if !defined(FALSE) || defined(__DOXYGEN__) | ||
43 | #define FALSE 0 | ||
44 | #endif | ||
45 | |||
46 | #if !defined(TRUE) || defined(__DOXYGEN__) | ||
47 | #define TRUE 1 | ||
48 | #endif | ||
49 | |||
50 | #define OSAL_SUCCESS false | ||
51 | #define OSAL_FAILED true | ||
52 | /** @} */ | ||
53 | |||
54 | #if 0 | ||
55 | /** | ||
56 | * @name Messages | ||
57 | * @{ | ||
58 | */ | ||
59 | #define MSG_OK (msg_t)0 | ||
60 | #define MSG_TIMEOUT (msg_t)-1 | ||
61 | #define MSG_RESET (msg_t)-2 | ||
62 | /** @} */ | ||
63 | #endif | ||
64 | |||
65 | #if 0 | ||
66 | /** | ||
67 | * @name Special time constants | ||
68 | * @{ | ||
69 | */ | ||
70 | #define TIME_IMMEDIATE ((sysinterval_t)0) | ||
71 | #define TIME_INFINITE ((sysinterval_t)-1) | ||
72 | /** @} */ | ||
73 | #endif | ||
74 | |||
75 | /** | ||
76 | * @name Systick modes. | ||
77 | * @{ | ||
78 | */ | ||
79 | #define OSAL_ST_MODE_NONE 0 | ||
80 | #define OSAL_ST_MODE_PERIODIC 1 | ||
81 | #define OSAL_ST_MODE_FREERUNNING 2 | ||
82 | /** @} */ | ||
83 | |||
84 | /** | ||
85 | * @name Systick parameters. | ||
86 | * @{ | ||
87 | */ | ||
88 | /** | ||
89 | * @brief Size in bits of the @p systick_t type. | ||
90 | */ | ||
91 | #define OSAL_ST_RESOLUTION CH_CFG_ST_RESOLUTION | ||
92 | |||
93 | /** | ||
94 | * @brief Required systick frequency or resolution. | ||
95 | */ | ||
96 | #define OSAL_ST_FREQUENCY CH_CFG_ST_FREQUENCY | ||
97 | |||
98 | /** | ||
99 | * @brief Systick mode required by the underlying OS. | ||
100 | */ | ||
101 | #if (CH_CFG_ST_TIMEDELTA == 0) || defined(__DOXYGEN__) | ||
102 | #define OSAL_ST_MODE OSAL_ST_MODE_PERIODIC | ||
103 | #else | ||
104 | #define OSAL_ST_MODE OSAL_ST_MODE_FREERUNNING | ||
105 | #endif | ||
106 | /** @} */ | ||
107 | |||
108 | /*===========================================================================*/ | ||
109 | /* Module pre-compile time settings. */ | ||
110 | /*===========================================================================*/ | ||
111 | |||
112 | /*===========================================================================*/ | ||
113 | /* Derived constants and error checks. */ | ||
114 | /*===========================================================================*/ | ||
115 | |||
116 | #if !(OSAL_ST_MODE == OSAL_ST_MODE_NONE) && \ | ||
117 | !(OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) && \ | ||
118 | !(OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING) | ||
119 | #error "invalid OSAL_ST_MODE setting in osal.h" | ||
120 | #endif | ||
121 | |||
122 | #if (OSAL_ST_RESOLUTION != 16) && (OSAL_ST_RESOLUTION != 32) && \ | ||
123 | (OSAL_ST_RESOLUTION != 64) | ||
124 | #error "invalid OSAL_ST_RESOLUTION, must be 16, 32 or 64" | ||
125 | #endif | ||
126 | |||
127 | /*===========================================================================*/ | ||
128 | /* Module data structures and types. */ | ||
129 | /*===========================================================================*/ | ||
130 | |||
131 | #if 0 | ||
132 | /** | ||
133 | * @brief Type of a system status word. | ||
134 | */ | ||
135 | typedef uint32_t syssts_t; | ||
136 | #endif | ||
137 | |||
138 | #if 0 | ||
139 | /** | ||
140 | * @brief Type of a message. | ||
141 | */ | ||
142 | typedef int32_t msg_t; | ||
143 | #endif | ||
144 | |||
145 | #if 0 | ||
146 | /** | ||
147 | * @brief Type of system time counter. | ||
148 | */ | ||
149 | typedef uint32_t systime_t; | ||
150 | #endif | ||
151 | |||
152 | #if 0 | ||
153 | /** | ||
154 | * @brief Type of system time interval. | ||
155 | */ | ||
156 | typedef uint32_t sysinterval_t; | ||
157 | #endif | ||
158 | |||
159 | #if 0 | ||
160 | /** | ||
161 | * @brief Type of time conversion variable. | ||
162 | * @note This type must have double width than other time types, it is | ||
163 | * only used internally for conversions. | ||
164 | */ | ||
165 | typedef uint64_t time_conv_t; | ||
166 | #endif | ||
167 | |||
168 | #if 0 | ||
169 | /** | ||
170 | * @brief Type of realtime counter. | ||
171 | */ | ||
172 | typedef uint32_t rtcnt_t; | ||
173 | #endif | ||
174 | |||
175 | #if 0 | ||
176 | /** | ||
177 | * @brief Type of a thread reference. | ||
178 | */ | ||
179 | typedef thread_t * thread_reference_t; | ||
180 | #endif | ||
181 | |||
182 | #if 0 | ||
183 | /** | ||
184 | * @brief Type of an event flags mask. | ||
185 | */ | ||
186 | typedef uint32_t eventflags_t; | ||
187 | #endif | ||
188 | |||
189 | #if (CH_CFG_USE_EVENTS == FALSE) || defined(__DOXYGEN__) | ||
190 | /** | ||
191 | * @brief Type of an event flags object. | ||
192 | * @note The content of this structure is not part of the API and should | ||
193 | * not be relied upon. Implementers may define this structure in | ||
194 | * an entirely different way. | ||
195 | * @note Retrieval and clearing of the flags are not defined in this | ||
196 | * API and are implementation-dependent. | ||
197 | */ | ||
198 | typedef struct event_source event_source_t; | ||
199 | |||
200 | /** | ||
201 | * @brief Type of an event source callback. | ||
202 | * @note This type is not part of the OSAL API and is provided | ||
203 | * exclusively as an example and for convenience. | ||
204 | */ | ||
205 | typedef void (*eventcallback_t)(event_source_t *esp); | ||
206 | |||
207 | /** | ||
208 | * @brief Events source object. | ||
209 | * @note The content of this structure is not part of the API and should | ||
210 | * not be relied upon. Implementers may define this structure in | ||
211 | * an entirely different way. | ||
212 | * @note Retrieval and clearing of the flags are not defined in this | ||
213 | * API and are implementation-dependent. | ||
214 | */ | ||
215 | struct event_source { | ||
216 | volatile eventflags_t flags; /**< @brief Stored event flags. */ | ||
217 | eventcallback_t cb; /**< @brief Event source callback. */ | ||
218 | void *param; /**< @brief User defined field. */ | ||
219 | }; | ||
220 | #endif /* CH_CFG_USE_EVENTS == FALSE */ | ||
221 | |||
222 | /** | ||
223 | * @brief Type of a mutex. | ||
224 | * @note If the OS does not support mutexes or there is no OS then the | ||
225 | * mechanism can be simulated. | ||
226 | */ | ||
227 | #if CH_CFG_USE_MUTEXES || defined(__DOXYGEN__) | ||
228 | #elif CH_CFG_USE_SEMAPHORES | ||
229 | typedef semaphore_t mutex_t; | ||
230 | #else | ||
231 | typedef uint32_t mutex_t; | ||
232 | #endif | ||
233 | |||
234 | #if 0 | ||
235 | /** | ||
236 | * @brief Type of a thread queue. | ||
237 | * @details A thread queue is a queue of sleeping threads, queued threads | ||
238 | * can be dequeued one at time or all together. | ||
239 | * @note In this implementation it is implemented as a single reference | ||
240 | * because there are no real threads. | ||
241 | */ | ||
242 | typedef struct { | ||
243 | thread_reference_t tr; | ||
244 | } threads_queue_t; | ||
245 | #endif | ||
246 | |||
247 | /*===========================================================================*/ | ||
248 | /* Module macros. */ | ||
249 | /*===========================================================================*/ | ||
250 | |||
251 | /** | ||
252 | * @name Debug related macros | ||
253 | * @{ | ||
254 | */ | ||
255 | /** | ||
256 | * @brief Condition assertion. | ||
257 | * @details If the condition check fails then the OSAL panics with a | ||
258 | * message and halts. | ||
259 | * @note The condition is tested only if the @p OSAL_ENABLE_ASSERTIONS | ||
260 | * switch is enabled. | ||
261 | * @note The remark string is not currently used except for putting a | ||
262 | * comment in the code about the assertion. | ||
263 | * | ||
264 | * @param[in] c the condition to be verified to be true | ||
265 | * @param[in] remark a remark string | ||
266 | * | ||
267 | * @api | ||
268 | */ | ||
269 | #define osalDbgAssert(c, remark) chDbgAssert(c, remark) | ||
270 | |||
271 | /** | ||
272 | * @brief Function parameters check. | ||
273 | * @details If the condition check fails then the OSAL panics and halts. | ||
274 | * @note The condition is tested only if the @p OSAL_ENABLE_CHECKS switch | ||
275 | * is enabled. | ||
276 | * | ||
277 | * @param[in] c the condition to be verified to be true | ||
278 | * | ||
279 | * @api | ||
280 | */ | ||
281 | #define osalDbgCheck(c) chDbgCheck(c) | ||
282 | |||
283 | /** | ||
284 | * @brief I-Class state check. | ||
285 | * @note Not implemented in this simplified OSAL. | ||
286 | */ | ||
287 | #define osalDbgCheckClassI() chDbgCheckClassI() | ||
288 | |||
289 | /** | ||
290 | * @brief S-Class state check. | ||
291 | * @note Not implemented in this simplified OSAL. | ||
292 | */ | ||
293 | #define osalDbgCheckClassS() chDbgCheckClassS() | ||
294 | /** @} */ | ||
295 | |||
296 | /** | ||
297 | * @name IRQ service routines wrappers | ||
298 | * @{ | ||
299 | */ | ||
300 | /** | ||
301 | * @brief Priority level verification macro. | ||
302 | */ | ||
303 | #define OSAL_IRQ_IS_VALID_PRIORITY(n) CH_IRQ_IS_VALID_KERNEL_PRIORITY(n) | ||
304 | |||
305 | /** | ||
306 | * @brief IRQ prologue code. | ||
307 | * @details This macro must be inserted at the start of all IRQ handlers. | ||
308 | */ | ||
309 | #define OSAL_IRQ_PROLOGUE() CH_IRQ_PROLOGUE() | ||
310 | |||
311 | /** | ||
312 | * @brief IRQ epilogue code. | ||
313 | * @details This macro must be inserted at the end of all IRQ handlers. | ||
314 | */ | ||
315 | #define OSAL_IRQ_EPILOGUE() CH_IRQ_EPILOGUE() | ||
316 | |||
317 | /** | ||
318 | * @brief IRQ handler function declaration. | ||
319 | * @details This macro hides the details of an ISR function declaration. | ||
320 | * | ||
321 | * @param[in] id a vector name as defined in @p vectors.s | ||
322 | */ | ||
323 | #define OSAL_IRQ_HANDLER(id) CH_IRQ_HANDLER(id) | ||
324 | /** @} */ | ||
325 | |||
326 | /** | ||
327 | * @name Time conversion utilities | ||
328 | * @{ | ||
329 | */ | ||
330 | /** | ||
331 | * @brief Seconds to time interval. | ||
332 | * @details Converts from seconds to system ticks number. | ||
333 | * @note The result is rounded upward to the next tick boundary. | ||
334 | * @note Use of this macro for large values is not secure because | ||
335 | * integer overflows, make sure your value can be correctly | ||
336 | * converted. | ||
337 | * | ||
338 | * @param[in] secs number of seconds | ||
339 | * @return The number of ticks. | ||
340 | * | ||
341 | * @api | ||
342 | */ | ||
343 | #define OSAL_S2I(secs) TIME_S2I(secs) | ||
344 | |||
345 | /** | ||
346 | * @brief Milliseconds to time interval. | ||
347 | * @details Converts from milliseconds to system ticks number. | ||
348 | * @note The result is rounded upward to the next tick boundary. | ||
349 | * @note Use of this macro for large values is not secure because | ||
350 | * integer overflows, make sure your value can be correctly | ||
351 | * converted. | ||
352 | * | ||
353 | * @param[in] msecs number of milliseconds | ||
354 | * @return The number of ticks. | ||
355 | * | ||
356 | * @api | ||
357 | */ | ||
358 | #define OSAL_MS2I(msecs) TIME_MS2I(msecs) | ||
359 | |||
360 | /** | ||
361 | * @brief Microseconds to time interval. | ||
362 | * @details Converts from microseconds to system ticks number. | ||
363 | * @note The result is rounded upward to the next tick boundary. | ||
364 | * @note Use of this macro for large values is not secure because | ||
365 | * integer overflows, make sure your value can be correctly | ||
366 | * converted. | ||
367 | * | ||
368 | * @param[in] usecs number of microseconds | ||
369 | * @return The number of ticks. | ||
370 | * | ||
371 | * @api | ||
372 | */ | ||
373 | #define OSAL_US2I(usecs) TIME_US2I(usecs) | ||
374 | |||
375 | /** | ||
376 | * @brief Time interval to seconds. | ||
377 | * @details Converts from system ticks number to seconds. | ||
378 | * @note The result is rounded up to the next second boundary. | ||
379 | * @note Use of this macro for large values is not secure because | ||
380 | * integer overflows, make sure your value can be correctly | ||
381 | * converted. | ||
382 | * | ||
383 | * @param[in] interval interval in ticks | ||
384 | * @return The number of seconds. | ||
385 | * | ||
386 | * @api | ||
387 | */ | ||
388 | #define OSAL_I2S(interval) TIME_I2S(interval) | ||
389 | |||
390 | /** | ||
391 | * @brief Time interval to milliseconds. | ||
392 | * @details Converts from system ticks number to milliseconds. | ||
393 | * @note The result is rounded up to the next millisecond boundary. | ||
394 | * @note Use of this macro for large values is not secure because | ||
395 | * integer overflows, make sure your value can be correctly | ||
396 | * converted. | ||
397 | * | ||
398 | * @param[in] interval interval in ticks | ||
399 | * @return The number of milliseconds. | ||
400 | * | ||
401 | * @api | ||
402 | */ | ||
403 | #define OSAL_I2MS(interval) TIME_I2MS(interval) | ||
404 | |||
405 | /** | ||
406 | * @brief Time interval to microseconds. | ||
407 | * @details Converts from system ticks number to microseconds. | ||
408 | * @note The result is rounded up to the next microsecond boundary. | ||
409 | * @note Use of this macro for large values is not secure because | ||
410 | * integer overflows, make sure your value can be correctly | ||
411 | * converted. | ||
412 | * | ||
413 | * @param[in] interval interval in ticks | ||
414 | * @return The number of microseconds. | ||
415 | * | ||
416 | * @api | ||
417 | */ | ||
418 | #define OSAL_I2US(interval) TIME_I2US(interval) | ||
419 | /** @} */ | ||
420 | |||
421 | /** | ||
422 | * @name Time conversion utilities for the realtime counter | ||
423 | * @{ | ||
424 | */ | ||
425 | /** | ||
426 | * @brief Seconds to realtime counter. | ||
427 | * @details Converts from seconds to realtime counter cycles. | ||
428 | * @note The macro assumes that @p freq >= @p 1. | ||
429 | * | ||
430 | * @param[in] freq clock frequency, in Hz, of the realtime counter | ||
431 | * @param[in] sec number of seconds | ||
432 | * @return The number of cycles. | ||
433 | * | ||
434 | * @api | ||
435 | */ | ||
436 | #define OSAL_S2RTC(freq, sec) S2RTC(freq, sec) | ||
437 | |||
438 | /** | ||
439 | * @brief Milliseconds to realtime counter. | ||
440 | * @details Converts from milliseconds to realtime counter cycles. | ||
441 | * @note The result is rounded upward to the next millisecond boundary. | ||
442 | * @note The macro assumes that @p freq >= @p 1000. | ||
443 | * | ||
444 | * @param[in] freq clock frequency, in Hz, of the realtime counter | ||
445 | * @param[in] msec number of milliseconds | ||
446 | * @return The number of cycles. | ||
447 | * | ||
448 | * @api | ||
449 | */ | ||
450 | #define OSAL_MS2RTC(freq, msec) MS2RTC(freq, msec) | ||
451 | |||
452 | /** | ||
453 | * @brief Microseconds to realtime counter. | ||
454 | * @details Converts from microseconds to realtime counter cycles. | ||
455 | * @note The result is rounded upward to the next microsecond boundary. | ||
456 | * @note The macro assumes that @p freq >= @p 1000000. | ||
457 | * | ||
458 | * @param[in] freq clock frequency, in Hz, of the realtime counter | ||
459 | * @param[in] usec number of microseconds | ||
460 | * @return The number of cycles. | ||
461 | * | ||
462 | * @api | ||
463 | */ | ||
464 | #define OSAL_US2RTC(freq, usec) US2RTC(freq, usec) | ||
465 | /** @} */ | ||
466 | |||
467 | /** | ||
468 | * @name Sleep macros using absolute time | ||
469 | * @{ | ||
470 | */ | ||
471 | /** | ||
472 | * @brief Delays the invoking thread for the specified number of seconds. | ||
473 | * @note The specified time is rounded up to a value allowed by the real | ||
474 | * system tick clock. | ||
475 | * @note The maximum specifiable value is implementation dependent. | ||
476 | * | ||
477 | * @param[in] secs time in seconds, must be different from zero | ||
478 | * | ||
479 | * @api | ||
480 | */ | ||
481 | #define osalThreadSleepSeconds(secs) osalThreadSleep(OSAL_S2I(secs)) | ||
482 | |||
483 | /** | ||
484 | * @brief Delays the invoking thread for the specified number of | ||
485 | * milliseconds. | ||
486 | * @note The specified time is rounded up to a value allowed by the real | ||
487 | * system tick clock. | ||
488 | * @note The maximum specifiable value is implementation dependent. | ||
489 | * | ||
490 | * @param[in] msecs time in milliseconds, must be different from zero | ||
491 | * | ||
492 | * @api | ||
493 | */ | ||
494 | #define osalThreadSleepMilliseconds(msecs) osalThreadSleep(OSAL_MS2I(msecs)) | ||
495 | |||
496 | /** | ||
497 | * @brief Delays the invoking thread for the specified number of | ||
498 | * microseconds. | ||
499 | * @note The specified time is rounded up to a value allowed by the real | ||
500 | * system tick clock. | ||
501 | * @note The maximum specifiable value is implementation dependent. | ||
502 | * | ||
503 | * @param[in] usecs time in microseconds, must be different from zero | ||
504 | * | ||
505 | * @api | ||
506 | */ | ||
507 | #define osalThreadSleepMicroseconds(usecs) osalThreadSleep(OSAL_US2I(usecs)) | ||
508 | /** @} */ | ||
509 | |||
510 | /*===========================================================================*/ | ||
511 | /* External declarations. */ | ||
512 | /*===========================================================================*/ | ||
513 | |||
514 | #ifdef __cplusplus | ||
515 | extern "C" { | ||
516 | #endif | ||
517 | |||
518 | #ifdef __cplusplus | ||
519 | } | ||
520 | #endif | ||
521 | |||
522 | /*===========================================================================*/ | ||
523 | /* Module inline functions. */ | ||
524 | /*===========================================================================*/ | ||
525 | |||
526 | /** | ||
527 | * @brief OSAL module initialization. | ||
528 | * | ||
529 | * @api | ||
530 | */ | ||
531 | static inline void osalInit(void) { | ||
532 | |||
533 | } | ||
534 | |||
535 | /** | ||
536 | * @brief System halt with error message. | ||
537 | * | ||
538 | * @param[in] reason the halt message pointer | ||
539 | * | ||
540 | * @api | ||
541 | */ | ||
542 | static inline void osalSysHalt(const char *reason) { | ||
543 | |||
544 | chSysHalt(reason); | ||
545 | } | ||
546 | |||
547 | /** | ||
548 | * @brief Disables interrupts globally. | ||
549 | * | ||
550 | * @special | ||
551 | */ | ||
552 | static inline void osalSysDisable(void) { | ||
553 | |||
554 | chSysDisable(); | ||
555 | } | ||
556 | |||
557 | /** | ||
558 | * @brief Enables interrupts globally. | ||
559 | * | ||
560 | * @special | ||
561 | */ | ||
562 | static inline void osalSysEnable(void) { | ||
563 | |||
564 | chSysEnable(); | ||
565 | } | ||
566 | |||
567 | /** | ||
568 | * @brief Enters a critical zone from thread context. | ||
569 | * @note This function cannot be used for reentrant critical zones. | ||
570 | * | ||
571 | * @special | ||
572 | */ | ||
573 | static inline void osalSysLock(void) { | ||
574 | |||
575 | chSysLock(); | ||
576 | } | ||
577 | |||
578 | /** | ||
579 | * @brief Leaves a critical zone from thread context. | ||
580 | * @note This function cannot be used for reentrant critical zones. | ||
581 | * | ||
582 | * @special | ||
583 | */ | ||
584 | static inline void osalSysUnlock(void) { | ||
585 | |||
586 | chSysUnlock(); | ||
587 | } | ||
588 | |||
589 | /** | ||
590 | * @brief Enters a critical zone from ISR context. | ||
591 | * @note This function cannot be used for reentrant critical zones. | ||
592 | * | ||
593 | * @special | ||
594 | */ | ||
595 | static inline void osalSysLockFromISR(void) { | ||
596 | |||
597 | chSysLockFromISR(); | ||
598 | } | ||
599 | |||
600 | /** | ||
601 | * @brief Leaves a critical zone from ISR context. | ||
602 | * @note This function cannot be used for reentrant critical zones. | ||
603 | * | ||
604 | * @special | ||
605 | */ | ||
606 | static inline void osalSysUnlockFromISR(void) { | ||
607 | |||
608 | chSysUnlockFromISR(); | ||
609 | } | ||
610 | |||
611 | /** | ||
612 | * @brief Returns the execution status and enters a critical zone. | ||
613 | * @details This functions enters into a critical zone and can be called | ||
614 | * from any context. Because its flexibility it is less efficient | ||
615 | * than @p chSysLock() which is preferable when the calling context | ||
616 | * is known. | ||
617 | * @post The system is in a critical zone. | ||
618 | * | ||
619 | * @return The previous system status, the encoding of this | ||
620 | * status word is architecture-dependent and opaque. | ||
621 | * | ||
622 | * @xclass | ||
623 | */ | ||
624 | static inline syssts_t osalSysGetStatusAndLockX(void) { | ||
625 | |||
626 | return chSysGetStatusAndLockX(); | ||
627 | } | ||
628 | |||
629 | /** | ||
630 | * @brief Restores the specified execution status and leaves a critical zone. | ||
631 | * @note A call to @p chSchRescheduleS() is automatically performed | ||
632 | * if exiting the critical zone and if not in ISR context. | ||
633 | * | ||
634 | * @param[in] sts the system status to be restored. | ||
635 | * | ||
636 | * @xclass | ||
637 | */ | ||
638 | static inline void osalSysRestoreStatusX(syssts_t sts) { | ||
639 | |||
640 | chSysRestoreStatusX(sts); | ||
641 | } | ||
642 | |||
643 | /** | ||
644 | * @brief Polled delay. | ||
645 | * @note The real delay is always few cycles in excess of the specified | ||
646 | * value. | ||
647 | * | ||
648 | * @param[in] cycles number of cycles | ||
649 | * | ||
650 | * @xclass | ||
651 | */ | ||
652 | #if (PORT_SUPPORTS_RT == TRUE) || defined(__DOXYGEN__) | ||
653 | static inline void osalSysPolledDelayX(rtcnt_t cycles) { | ||
654 | |||
655 | chSysPolledDelayX(cycles); | ||
656 | } | ||
657 | #endif | ||
658 | |||
659 | /** | ||
660 | * @brief Systick callback for the underlying OS. | ||
661 | * @note This callback is only defined if the OSAL requires such a | ||
662 | * service from the HAL. | ||
663 | */ | ||
664 | #if (OSAL_ST_MODE != OSAL_ST_MODE_NONE) || defined(__DOXYGEN__) | ||
665 | static inline void osalOsTimerHandlerI(void) { | ||
666 | |||
667 | chSysTimerHandlerI(); | ||
668 | } | ||
669 | #endif | ||
670 | |||
671 | /** | ||
672 | * @brief Checks if a reschedule is required and performs it. | ||
673 | * @note I-Class functions invoked from thread context must not reschedule | ||
674 | * by themselves, an explicit reschedule using this function is | ||
675 | * required in this scenario. | ||
676 | * @note Not implemented in this simplified OSAL. | ||
677 | * | ||
678 | * @sclass | ||
679 | */ | ||
680 | static inline void osalOsRescheduleS(void) { | ||
681 | |||
682 | chSchRescheduleS(); | ||
683 | } | ||
684 | |||
685 | /** | ||
686 | * @brief Current system time. | ||
687 | * @details Returns the number of system ticks since the @p osalInit() | ||
688 | * invocation. | ||
689 | * @note The counter can reach its maximum and then restart from zero. | ||
690 | * @note This function can be called from any context but its atomicity | ||
691 | * is not guaranteed on architectures whose word size is less than | ||
692 | * @p systime_t size. | ||
693 | * | ||
694 | * @return The system time in ticks. | ||
695 | * | ||
696 | * @xclass | ||
697 | */ | ||
698 | static inline systime_t osalOsGetSystemTimeX(void) { | ||
699 | |||
700 | return chVTGetSystemTimeX(); | ||
701 | } | ||
702 | |||
703 | /** | ||
704 | * @brief Adds an interval to a system time returning a system time. | ||
705 | * | ||
706 | * @param[in] systime base system time | ||
707 | * @param[in] interval interval to be added | ||
708 | * @return The new system time. | ||
709 | * | ||
710 | * @xclass | ||
711 | */ | ||
712 | static inline systime_t osalTimeAddX(systime_t systime, | ||
713 | sysinterval_t interval) { | ||
714 | |||
715 | return chTimeAddX(systime, interval); | ||
716 | } | ||
717 | |||
718 | /** | ||
719 | * @brief Subtracts two system times returning an interval. | ||
720 | * | ||
721 | * @param[in] start first system time | ||
722 | * @param[in] end second system time | ||
723 | * @return The interval representing the time difference. | ||
724 | * | ||
725 | * @xclass | ||
726 | */ | ||
727 | static inline sysinterval_t osalTimeDiffX(systime_t start, systime_t end) { | ||
728 | |||
729 | return chTimeDiffX(start, end); | ||
730 | } | ||
731 | |||
732 | /** | ||
733 | * @brief Checks if the specified time is within the specified time window. | ||
734 | * @note When start==end then the function returns always true because the | ||
735 | * whole time range is specified. | ||
736 | * @note This function can be called from any context. | ||
737 | * | ||
738 | * @param[in] time the time to be verified | ||
739 | * @param[in] start the start of the time window (inclusive) | ||
740 | * @param[in] end the end of the time window (non inclusive) | ||
741 | * @retval true current time within the specified time window. | ||
742 | * @retval false current time not within the specified time window. | ||
743 | * | ||
744 | * @xclass | ||
745 | */ | ||
746 | static inline bool osalTimeIsInRangeX(systime_t time, | ||
747 | systime_t start, | ||
748 | systime_t end) { | ||
749 | |||
750 | return chTimeIsInRangeX(time, start, end); | ||
751 | } | ||
752 | |||
753 | /** | ||
754 | * @brief Suspends the invoking thread for the specified time. | ||
755 | * | ||
756 | * @param[in] delay the delay in system ticks, the special values are | ||
757 | * handled as follow: | ||
758 | * - @a TIME_INFINITE is allowed but interpreted as a | ||
759 | * normal time specification. | ||
760 | * - @a TIME_IMMEDIATE this value is not allowed. | ||
761 | * . | ||
762 | * | ||
763 | * @sclass | ||
764 | */ | ||
765 | static inline void osalThreadSleepS(sysinterval_t delay) { | ||
766 | |||
767 | chThdSleepS(delay); | ||
768 | } | ||
769 | |||
770 | /** | ||
771 | * @brief Suspends the invoking thread for the specified time. | ||
772 | * | ||
773 | * @param[in] delay the delay in system ticks, the special values are | ||
774 | * handled as follow: | ||
775 | * - @a TIME_INFINITE is allowed but interpreted as a | ||
776 | * normal time specification. | ||
777 | * - @a TIME_IMMEDIATE this value is not allowed. | ||
778 | * . | ||
779 | * | ||
780 | * @api | ||
781 | */ | ||
782 | static inline void osalThreadSleep(sysinterval_t delay) { | ||
783 | |||
784 | chThdSleep(delay); | ||
785 | } | ||
786 | |||
787 | /** | ||
788 | * @brief Sends the current thread sleeping and sets a reference variable. | ||
789 | * @note This function must reschedule, it can only be called from thread | ||
790 | * context. | ||
791 | * | ||
792 | * @param[in] trp a pointer to a thread reference object | ||
793 | * @return The wake up message. | ||
794 | * | ||
795 | * @sclass | ||
796 | */ | ||
797 | static inline msg_t osalThreadSuspendS(thread_reference_t *trp) { | ||
798 | |||
799 | return chThdSuspendTimeoutS(trp, TIME_INFINITE); | ||
800 | } | ||
801 | |||
802 | /** | ||
803 | * @brief Sends the current thread sleeping and sets a reference variable. | ||
804 | * @note This function must reschedule, it can only be called from thread | ||
805 | * context. | ||
806 | * | ||
807 | * @param[in] trp a pointer to a thread reference object | ||
808 | * @param[in] timeout the timeout in system ticks, the special values are | ||
809 | * handled as follow: | ||
810 | * - @a TIME_INFINITE the thread enters an infinite sleep | ||
811 | * state. | ||
812 | * - @a TIME_IMMEDIATE the thread is not enqueued and | ||
813 | * the function returns @p MSG_TIMEOUT as if a timeout | ||
814 | * occurred. | ||
815 | * . | ||
816 | * @return The wake up message. | ||
817 | * @retval MSG_TIMEOUT if the operation timed out. | ||
818 | * | ||
819 | * @sclass | ||
820 | */ | ||
821 | static inline msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, | ||
822 | sysinterval_t timeout) { | ||
823 | |||
824 | return chThdSuspendTimeoutS(trp, timeout); | ||
825 | } | ||
826 | |||
827 | /** | ||
828 | * @brief Wakes up a thread waiting on a thread reference object. | ||
829 | * @note This function must not reschedule because it can be called from | ||
830 | * ISR context. | ||
831 | * | ||
832 | * @param[in] trp a pointer to a thread reference object | ||
833 | * @param[in] msg the message code | ||
834 | * | ||
835 | * @iclass | ||
836 | */ | ||
837 | static inline void osalThreadResumeI(thread_reference_t *trp, msg_t msg) { | ||
838 | |||
839 | chThdResumeI(trp, msg); | ||
840 | } | ||
841 | |||
842 | /** | ||
843 | * @brief Wakes up a thread waiting on a thread reference object. | ||
844 | * @note This function must reschedule, it can only be called from thread | ||
845 | * context. | ||
846 | * | ||
847 | * @param[in] trp a pointer to a thread reference object | ||
848 | * @param[in] msg the message code | ||
849 | * | ||
850 | * @iclass | ||
851 | */ | ||
852 | static inline void osalThreadResumeS(thread_reference_t *trp, msg_t msg) { | ||
853 | |||
854 | chThdResumeS(trp, msg); | ||
855 | } | ||
856 | |||
857 | /** | ||
858 | * @brief Initializes a threads queue object. | ||
859 | * | ||
860 | * @param[out] tqp pointer to the threads queue object | ||
861 | * | ||
862 | * @init | ||
863 | */ | ||
864 | static inline void osalThreadQueueObjectInit(threads_queue_t *tqp) { | ||
865 | |||
866 | chThdQueueObjectInit(tqp); | ||
867 | } | ||
868 | |||
869 | /** | ||
870 | * @brief Enqueues the caller thread. | ||
871 | * @details The caller thread is enqueued and put to sleep until it is | ||
872 | * dequeued or the specified timeouts expires. | ||
873 | * | ||
874 | * @param[in] tqp pointer to the threads queue object | ||
875 | * @param[in] timeout the timeout in system ticks, the special values are | ||
876 | * handled as follow: | ||
877 | * - @a TIME_INFINITE the thread enters an infinite sleep | ||
878 | * state. | ||
879 | * - @a TIME_IMMEDIATE the thread is not enqueued and | ||
880 | * the function returns @p MSG_TIMEOUT as if a timeout | ||
881 | * occurred. | ||
882 | * . | ||
883 | * @return The message from @p osalQueueWakeupOneI() or | ||
884 | * @p osalQueueWakeupAllI() functions. | ||
885 | * @retval MSG_TIMEOUT if the thread has not been dequeued within the | ||
886 | * specified timeout or if the function has been | ||
887 | * invoked with @p TIME_IMMEDIATE as timeout | ||
888 | * specification. | ||
889 | * | ||
890 | * @sclass | ||
891 | */ | ||
892 | static inline msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, | ||
893 | sysinterval_t timeout) { | ||
894 | |||
895 | return chThdEnqueueTimeoutS(tqp, timeout); | ||
896 | } | ||
897 | |||
898 | /** | ||
899 | * @brief Dequeues and wakes up one thread from the queue, if any. | ||
900 | * | ||
901 | * @param[in] tqp pointer to the threads queue object | ||
902 | * @param[in] msg the message code | ||
903 | * | ||
904 | * @iclass | ||
905 | */ | ||
906 | static inline void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg) { | ||
907 | |||
908 | chThdDequeueNextI(tqp, msg); | ||
909 | } | ||
910 | |||
911 | /** | ||
912 | * @brief Dequeues and wakes up all threads from the queue. | ||
913 | * | ||
914 | * @param[in] tqp pointer to the threads queue object | ||
915 | * @param[in] msg the message code | ||
916 | * | ||
917 | * @iclass | ||
918 | */ | ||
919 | static inline void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg) { | ||
920 | |||
921 | chThdDequeueAllI(tqp, msg); | ||
922 | } | ||
923 | |||
924 | #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__) | ||
925 | /** | ||
926 | * @brief Initializes an event source object. | ||
927 | * | ||
928 | * @param[out] esp pointer to the event source object | ||
929 | * | ||
930 | * @init | ||
931 | */ | ||
932 | static inline void osalEventObjectInit(event_source_t *esp) { | ||
933 | |||
934 | chEvtObjectInit(esp); | ||
935 | } | ||
936 | #else | ||
937 | static inline void osalEventObjectInit(event_source_t *esp) { | ||
938 | |||
939 | osalDbgCheck(esp != NULL); | ||
940 | |||
941 | esp->flags = (eventflags_t)0; | ||
942 | esp->cb = NULL; | ||
943 | esp->param = NULL; | ||
944 | } | ||
945 | #endif | ||
946 | |||
947 | #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__) | ||
948 | /** | ||
949 | * @brief Add flags to an event source object. | ||
950 | * | ||
951 | * @param[in] esp pointer to the event flags object | ||
952 | * @param[in] flags flags to be ORed to the flags mask | ||
953 | * | ||
954 | * @iclass | ||
955 | */ | ||
956 | static inline void osalEventBroadcastFlagsI(event_source_t *esp, | ||
957 | eventflags_t flags) { | ||
958 | |||
959 | chEvtBroadcastFlagsI(esp, flags); | ||
960 | } | ||
961 | #else | ||
962 | static inline void osalEventBroadcastFlagsI(event_source_t *esp, | ||
963 | eventflags_t flags) { | ||
964 | |||
965 | osalDbgCheck(esp != NULL); | ||
966 | |||
967 | esp->flags |= flags; | ||
968 | if (esp->cb != NULL) { | ||
969 | esp->cb(esp); | ||
970 | } | ||
971 | } | ||
972 | #endif | ||
973 | |||
974 | #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__) | ||
975 | /** | ||
976 | * @brief Add flags to an event source object. | ||
977 | * | ||
978 | * @param[in] esp pointer to the event flags object | ||
979 | * @param[in] flags flags to be ORed to the flags mask | ||
980 | * | ||
981 | * @iclass | ||
982 | */ | ||
983 | static inline void osalEventBroadcastFlags(event_source_t *esp, | ||
984 | eventflags_t flags) { | ||
985 | |||
986 | chEvtBroadcastFlags(esp, flags); | ||
987 | } | ||
988 | #else | ||
989 | static inline void osalEventBroadcastFlags(event_source_t *esp, | ||
990 | eventflags_t flags) { | ||
991 | |||
992 | osalDbgCheck(esp != NULL); | ||
993 | |||
994 | osalSysLock(); | ||
995 | esp->flags |= flags; | ||
996 | if (esp->cb != NULL) { | ||
997 | esp->cb(esp); | ||
998 | } | ||
999 | osalSysUnlock(); | ||
1000 | } | ||
1001 | #endif | ||
1002 | |||
1003 | #if (CH_CFG_USE_EVENTS == FALSE) || defined(__DOXYGEN__) | ||
1004 | /** | ||
1005 | * @brief Event callback setup. | ||
1006 | * @note The callback is invoked from ISR context and can | ||
1007 | * only invoke I-Class functions. The callback is meant | ||
1008 | * to wakeup the task that will handle the event by | ||
1009 | * calling @p osalEventGetAndClearFlagsI(). | ||
1010 | * | ||
1011 | * @param[in] esp pointer to the event flags object | ||
1012 | * @param[in] cb pointer to the callback function | ||
1013 | * @param[in] param parameter to be passed to the callback function | ||
1014 | * | ||
1015 | * @api | ||
1016 | */ | ||
1017 | static inline void osalEventSetCallback(event_source_t *esp, | ||
1018 | eventcallback_t cb, | ||
1019 | void *param) { | ||
1020 | |||
1021 | osalDbgCheck(esp != NULL); | ||
1022 | |||
1023 | esp->cb = cb; | ||
1024 | esp->param = param; | ||
1025 | } | ||
1026 | #endif | ||
1027 | |||
1028 | /** | ||
1029 | * @brief Initializes s @p mutex_t object. | ||
1030 | * | ||
1031 | * @param[out] mp pointer to the @p mutex_t object | ||
1032 | * | ||
1033 | * @init | ||
1034 | */ | ||
1035 | static inline void osalMutexObjectInit(mutex_t *mp) { | ||
1036 | |||
1037 | #if CH_CFG_USE_MUTEXES | ||
1038 | chMtxObjectInit(mp); | ||
1039 | #elif CH_CFG_USE_SEMAPHORES | ||
1040 | chSemObjectInit((semaphore_t *)mp, 1); | ||
1041 | #else | ||
1042 | *mp = 0; | ||
1043 | #endif | ||
1044 | } | ||
1045 | |||
1046 | /** | ||
1047 | * @brief Locks the specified mutex. | ||
1048 | * @post The mutex is locked and inserted in the per-thread stack of owned | ||
1049 | * mutexes. | ||
1050 | * | ||
1051 | * @param[in,out] mp pointer to the @p mutex_t object | ||
1052 | * | ||
1053 | * @api | ||
1054 | */ | ||
1055 | static inline void osalMutexLock(mutex_t *mp) { | ||
1056 | |||
1057 | #if CH_CFG_USE_MUTEXES | ||
1058 | chMtxLock(mp); | ||
1059 | #elif CH_CFG_USE_SEMAPHORES | ||
1060 | chSemWait((semaphore_t *)mp); | ||
1061 | #else | ||
1062 | *mp = 1; | ||
1063 | #endif | ||
1064 | } | ||
1065 | |||
1066 | /** | ||
1067 | * @brief Unlocks the specified mutex. | ||
1068 | * @note The HAL guarantees to release mutex in reverse lock order. The | ||
1069 | * mutex being unlocked is guaranteed to be the last locked mutex | ||
1070 | * by the invoking thread. | ||
1071 | * The implementation can rely on this behavior and eventually | ||
1072 | * ignore the @p mp parameter which is supplied in order to support | ||
1073 | * those OSes not supporting a stack of the owned mutexes. | ||
1074 | * | ||
1075 | * @param[in,out] mp pointer to the @p mutex_t object | ||
1076 | * | ||
1077 | * @api | ||
1078 | */ | ||
1079 | static inline void osalMutexUnlock(mutex_t *mp) { | ||
1080 | |||
1081 | #if CH_CFG_USE_MUTEXES | ||
1082 | chMtxUnlock(mp); | ||
1083 | #elif CH_CFG_USE_SEMAPHORES | ||
1084 | chSemSignal((semaphore_t *)mp); | ||
1085 | #else | ||
1086 | *mp = 0; | ||
1087 | #endif | ||
1088 | } | ||
1089 | |||
1090 | #endif /* OSAL_H */ | ||
1091 | |||
1092 | /** @} */ | ||