aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/os/hal/osal/os-less/ARMCMx/osal.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/os/hal/osal/os-less/ARMCMx/osal.h')
-rw-r--r--lib/chibios/os/hal/osal/os-less/ARMCMx/osal.h754
1 files changed, 754 insertions, 0 deletions
diff --git a/lib/chibios/os/hal/osal/os-less/ARMCMx/osal.h b/lib/chibios/os/hal/osal/os-less/ARMCMx/osal.h
new file mode 100644
index 000000000..0ec4ee199
--- /dev/null
+++ b/lib/chibios/os/hal/osal/os-less/ARMCMx/osal.h
@@ -0,0 +1,754 @@
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 "cmparams.h"
33
34#include "osalconf.h"
35
36/*===========================================================================*/
37/* Module constants. */
38/*===========================================================================*/
39
40/**
41 * @name Common constants
42 * @{
43 */
44#if !defined(FALSE) || defined(__DOXYGEN__)
45#define FALSE 0
46#endif
47
48#if !defined(TRUE) || defined(__DOXYGEN__)
49#define TRUE 1
50#endif
51
52#define OSAL_SUCCESS false
53#define OSAL_FAILED true
54/** @} */
55
56/**
57 * @name Messages
58 * @{
59 */
60#define MSG_OK (msg_t)0
61#define MSG_RESET (msg_t)-1
62#define MSG_TIMEOUT (msg_t)-2
63#define MSG_WAIT (msg_t)-10
64/** @} */
65
66/**
67 * @name Special time constants
68 * @{
69 */
70#define TIME_IMMEDIATE ((sysinterval_t)0)
71#define TIME_INFINITE ((sysinterval_t)-1)
72/** @} */
73
74/**
75 * @name Systick modes.
76 * @{
77 */
78#define OSAL_ST_MODE_NONE 0
79#define OSAL_ST_MODE_PERIODIC 1
80#define OSAL_ST_MODE_FREERUNNING 2
81/** @} */
82
83/**
84 * @name Systick parameters.
85 * @{
86 */
87/**
88 * @brief Size in bits of the @p systick_t type.
89 */
90#define OSAL_ST_RESOLUTION 32
91
92/**
93 * @brief Systick mode required by the underlying OS.
94 */
95#define OSAL_ST_MODE OSAL_ST_MODE_PERIODIC
96/** @} */
97
98/**
99 * @name IRQ-related constants
100 * @{
101 */
102/**
103 * @brief Total priority levels.
104 */
105#define OSAL_IRQ_PRIORITY_LEVELS (1U << CORTEX_PRIORITY_BITS)
106
107/**
108 * @brief Highest IRQ priority for HAL drivers.
109 */
110#if (CORTEX_MODEL == 0) || defined(__DOXYGEN__)
111#define OSAL_IRQ_MAXIMUM_PRIORITY 0
112#else
113#define OSAL_IRQ_MAXIMUM_PRIORITY 1
114#endif
115
116/**
117 * @brief Converts from numeric priority to BASEPRI register value.
118 */
119#define OSAL_BASEPRI(priority) ((priority) << (8U - CORTEX_PRIORITY_BITS))
120/** @} */
121
122/*===========================================================================*/
123/* Module pre-compile time settings. */
124/*===========================================================================*/
125
126/**
127 * @brief Frequency in Hertz of the system tick.
128 */
129#if !defined(OSAL_ST_FREQUENCY) || defined(__DOXYGEN__)
130#define OSAL_ST_FREQUENCY 1000
131#endif
132
133/**
134 * @brief Enables OSAL assertions.
135 */
136#if !defined(OSAL_DBG_ENABLE_ASSERTS) || defined(__DOXYGEN__)
137#define OSAL_DBG_ENABLE_ASSERTS FALSE
138#endif
139
140/**
141 * @brief Enables OSAL functions parameters checks.
142 */
143#if !defined(OSAL_DBG_ENABLE_CHECKS) || defined(__DOXYGEN__)
144#define OSAL_DBG_ENABLE_CHECKS FALSE
145#endif
146
147/**
148 * @brief OSAL initialization hook.
149 */
150#if !defined(OSAL_INIT_HOOK) || defined(__DOXYGEN__)
151#define OSAL_INIT_HOOK()
152#endif
153
154/**
155 * @brief Idle loop hook macro.
156 */
157#if !defined(OSAL_IDLE_HOOK) || defined(__DOXYGEN__)
158#define OSAL_IDLE_HOOK()
159#endif
160
161/*===========================================================================*/
162/* Derived constants and error checks. */
163/*===========================================================================*/
164
165/*===========================================================================*/
166/* Module data structures and types. */
167/*===========================================================================*/
168
169/**
170 * @brief Type of a system status word.
171 */
172typedef uint32_t syssts_t;
173
174/**
175 * @brief Type of a message.
176 */
177typedef int32_t msg_t;
178
179/**
180 * @brief Type of system time counter.
181 */
182typedef uint32_t systime_t;
183
184/**
185 * @brief Type of system time interval.
186 */
187typedef uint32_t sysinterval_t;
188
189/**
190 * @brief Type of realtime counter.
191 */
192typedef uint32_t rtcnt_t;
193
194/**
195 * @brief Type of a thread.
196 * @note The content of this structure is not part of the API and should
197 * not be relied upon. Implementers may define this structure in
198 * an entirely different way.
199 */
200typedef struct {
201 volatile msg_t message;
202} thread_t;
203
204/**
205 * @brief Type of a thread reference.
206 */
207typedef thread_t * thread_reference_t;
208
209/**
210 * @brief Type of an event flags mask.
211 */
212typedef uint32_t eventflags_t;
213
214/**
215 * @brief Type of an event flags object.
216 * @note The content of this structure is not part of the API and should
217 * not be relied upon. Implementers may define this structure in
218 * an entirely different way.
219 * @note Retrieval and clearing of the flags are not defined in this
220 * API and are implementation-dependent.
221 */
222typedef struct event_source event_source_t;
223
224/**
225 * @brief Type of an event source callback.
226 * @note This type is not part of the OSAL API and is provided
227 * exclusively as an example and for convenience.
228 */
229typedef void (*eventcallback_t)(event_source_t *esp);
230
231/**
232 * @brief Events source object.
233 * @note The content of this structure is not part of the API and should
234 * not be relied upon. Implementers may define this structure in
235 * an entirely different way.
236 * @note Retrieval and clearing of the flags are not defined in this
237 * API and are implementation-dependent.
238 */
239struct event_source {
240 volatile eventflags_t flags; /**< @brief Stored event flags. */
241 eventcallback_t cb; /**< @brief Event source callback. */
242 void *param; /**< @brief User defined field. */
243};
244
245/**
246 * @brief Type of a mutex.
247 * @note If the OS does not support mutexes or there is no OS then them
248 * mechanism can be simulated.
249 */
250typedef uint32_t mutex_t;
251
252/**
253 * @brief Type of a thread queue.
254 * @details A thread queue is a queue of sleeping threads, queued threads
255 * can be dequeued one at time or all together.
256 * @note If the OSAL is implemented on a bare metal machine without RTOS
257 * then the queue can be implemented as a single thread reference.
258 */
259typedef struct {
260 thread_reference_t tr;
261} threads_queue_t;
262
263/*===========================================================================*/
264/* Module macros. */
265/*===========================================================================*/
266
267/**
268 * @name Debug related macros
269 * @{
270 */
271/**
272 * @brief Condition assertion.
273 * @details If the condition check fails then the OSAL panics with a
274 * message and halts.
275 * @note The condition is tested only if the @p OSAL_ENABLE_ASSERTIONS
276 * switch is enabled.
277 * @note The remark string is not currently used except for putting a
278 * comment in the code about the assertion.
279 *
280 * @param[in] c the condition to be verified to be true
281 * @param[in] remark a remark string
282 *
283 * @api
284 */
285#define osalDbgAssert(c, remark) do { \
286 /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
287 if (OSAL_DBG_ENABLE_ASSERTS != FALSE) { \
288 if (!(c)) { \
289 /*lint -restore*/ \
290 osalSysHalt(__func__); \
291 } \
292 } \
293} while (false)
294
295/**
296 * @brief Function parameters check.
297 * @details If the condition check fails then the OSAL panics and halts.
298 * @note The condition is tested only if the @p OSAL_ENABLE_CHECKS switch
299 * is enabled.
300 *
301 * @param[in] c the condition to be verified to be true
302 *
303 * @api
304 */
305#define osalDbgCheck(c) do { \
306 /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
307 if (OSAL_DBG_ENABLE_CHECKS != FALSE) { \
308 if (!(c)) { \
309 /*lint -restore*/ \
310 osalSysHalt(__func__); \
311 } \
312 } \
313} while (false)
314
315/**
316 * @brief I-Class state check.
317 * @note Implementation is optional.
318 */
319#define osalDbgCheckClassI()
320
321/**
322 * @brief S-Class state check.
323 * @note Implementation is optional.
324 */
325#define osalDbgCheckClassS()
326/** @} */
327
328/**
329 * @name IRQ service routines wrappers
330 * @{
331 */
332/**
333 * @brief Priority level verification macro.
334 */
335#define OSAL_IRQ_IS_VALID_PRIORITY(n) \
336 (((n) >= OSAL_IRQ_MAXIMUM_PRIORITY) && ((n) < OSAL_IRQ_PRIORITY_LEVELS))
337
338/**
339 * @brief IRQ prologue code.
340 * @details This macro must be inserted at the start of all IRQ handlers.
341 */
342#define OSAL_IRQ_PROLOGUE()
343
344/**
345 * @brief IRQ epilogue code.
346 * @details This macro must be inserted at the end of all IRQ handlers.
347 */
348#define OSAL_IRQ_EPILOGUE()
349
350/**
351 * @brief IRQ handler function declaration.
352 * @details This macro hides the details of an ISR function declaration.
353 *
354 * @param[in] id a vector name as defined in @p vectors.s
355 */
356#define OSAL_IRQ_HANDLER(id) void id(void)
357/** @} */
358
359/**
360 * @name Time conversion utilities
361 * @{
362 */
363/**
364 * @brief Seconds to system ticks.
365 * @details Converts from seconds to system ticks number.
366 * @note The result is rounded upward to the next tick boundary.
367 *
368 * @param[in] secs number of seconds
369 * @return The number of ticks.
370 *
371 * @api
372 */
373#define OSAL_S2I(secs) \
374 ((sysinterval_t)((uint32_t)(secs) * (uint32_t)OSAL_ST_FREQUENCY))
375
376/**
377 * @brief Milliseconds to system ticks.
378 * @details Converts from milliseconds to system ticks number.
379 * @note The result is rounded upward to the next tick boundary.
380 *
381 * @param[in] msecs number of milliseconds
382 * @return The number of ticks.
383 *
384 * @api
385 */
386#define OSAL_MS2I(msecs) \
387 ((sysinterval_t)((((((uint32_t)(msecs)) * \
388 ((uint32_t)OSAL_ST_FREQUENCY)) - 1UL) / 1000UL) + 1UL))
389
390/**
391 * @brief Microseconds to system ticks.
392 * @details Converts from microseconds to system ticks number.
393 * @note The result is rounded upward to the next tick boundary.
394 *
395 * @param[in] usecs number of microseconds
396 * @return The number of ticks.
397 *
398 * @api
399 */
400#define OSAL_US2I(usecs) \
401 ((sysinterval_t)((((((uint32_t)(usecs)) * \
402 ((uint32_t)OSAL_ST_FREQUENCY)) - 1UL) / 1000000UL) + 1UL))
403/** @} */
404
405/**
406 * @name Time conversion utilities for the realtime counter
407 * @{
408 */
409/**
410 * @brief Seconds to realtime counter.
411 * @details Converts from seconds to realtime counter cycles.
412 * @note The macro assumes that @p freq >= @p 1.
413 *
414 * @param[in] freq clock frequency, in Hz, of the realtime counter
415 * @param[in] sec number of seconds
416 * @return The number of cycles.
417 *
418 * @api
419 */
420#define OSAL_S2RTC(freq, sec) ((freq) * (sec))
421
422/**
423 * @brief Milliseconds to realtime counter.
424 * @details Converts from milliseconds to realtime counter cycles.
425 * @note The result is rounded upward to the next millisecond boundary.
426 * @note The macro assumes that @p freq >= @p 1000.
427 *
428 * @param[in] freq clock frequency, in Hz, of the realtime counter
429 * @param[in] msec number of milliseconds
430 * @return The number of cycles.
431 *
432 * @api
433 */
434#define OSAL_MS2RTC(freq, msec) (rtcnt_t)((((freq) + 999UL) / 1000UL) * (msec))
435
436/**
437 * @brief Microseconds to realtime counter.
438 * @details Converts from microseconds to realtime counter cycles.
439 * @note The result is rounded upward to the next microsecond boundary.
440 * @note The macro assumes that @p freq >= @p 1000000.
441 *
442 * @param[in] freq clock frequency, in Hz, of the realtime counter
443 * @param[in] usec number of microseconds
444 * @return The number of cycles.
445 *
446 * @api
447 */
448#define OSAL_US2RTC(freq, usec) (rtcnt_t)((((freq) + 999999UL) / 1000000UL) * (usec))
449/** @} */
450
451/**
452 * @name Sleep macros using absolute time
453 * @{
454 */
455/**
456 * @brief Delays the invoking thread for the specified number of seconds.
457 * @note The specified time is rounded up to a value allowed by the real
458 * system tick clock.
459 * @note The maximum specifiable value is implementation dependent.
460 *
461 * @param[in] secs time in seconds, must be different from zero
462 *
463 * @api
464 */
465#define osalThreadSleepSeconds(secs) osalThreadSleep(OSAL_S2I(secs))
466
467/**
468 * @brief Delays the invoking thread for the specified number of
469 * milliseconds.
470 * @note The specified time is rounded up to a value allowed by the real
471 * system tick clock.
472 * @note The maximum specifiable value is implementation dependent.
473 *
474 * @param[in] msecs time in milliseconds, must be different from zero
475 *
476 * @api
477 */
478#define osalThreadSleepMilliseconds(msecs) osalThreadSleep(OSAL_MS2I(msecs))
479
480/**
481 * @brief Delays the invoking thread for the specified number of
482 * microseconds.
483 * @note The specified time is rounded up to a value allowed by the real
484 * system tick clock.
485 * @note The maximum specifiable value is implementation dependent.
486 *
487 * @param[in] usecs time in microseconds, must be different from zero
488 *
489 * @api
490 */
491#define osalThreadSleepMicroseconds(usecs) osalThreadSleep(OSAL_US2I(usecs))
492/** @} */
493
494/*===========================================================================*/
495/* External declarations. */
496/*===========================================================================*/
497
498extern const char *osal_halt_msg;
499
500#ifdef __cplusplus
501extern "C" {
502#endif
503 void osalInit(void);
504 void osalSysHalt(const char *reason);
505 void osalSysPolledDelayX(rtcnt_t cycles);
506 void osalOsTimerHandlerI(void);
507 void osalOsRescheduleS(void);
508 systime_t osalOsGetSystemTimeX(void);
509 void osalThreadSleepS(sysinterval_t time);
510 void osalThreadSleep(sysinterval_t time);
511 msg_t osalThreadSuspendS(thread_reference_t *trp);
512 msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, sysinterval_t timeout);
513 void osalThreadResumeI(thread_reference_t *trp, msg_t msg);
514 void osalThreadResumeS(thread_reference_t *trp, msg_t msg);
515 msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout);
516 void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg);
517 void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg);
518 void osalEventBroadcastFlagsI(event_source_t *esp, eventflags_t flags);
519 void osalEventBroadcastFlags(event_source_t *esp, eventflags_t flags);
520 void osalEventSetCallback(event_source_t *esp,
521 eventcallback_t cb,
522 void *param);
523 void osalMutexLock(mutex_t *mp);
524 void osalMutexUnlock(mutex_t *mp);
525#ifdef __cplusplus
526}
527#endif
528
529/*===========================================================================*/
530/* Module inline functions. */
531/*===========================================================================*/
532
533/**
534 * @brief Disables interrupts globally.
535 *
536 * @special
537 */
538static inline void osalSysDisable(void) {
539
540 __disable_irq();
541}
542
543/**
544 * @brief Enables interrupts globally.
545 *
546 * @special
547 */
548static inline void osalSysEnable(void) {
549
550 __enable_irq();
551}
552
553/**
554 * @brief Enters a critical zone from thread context.
555 * @note This function cannot be used for reentrant critical zones.
556 *
557 * @special
558 */
559static inline void osalSysLock(void) {
560
561#if CORTEX_MODEL == 0
562 __disable_irq();
563#else
564 __set_BASEPRI(OSAL_BASEPRI(OSAL_IRQ_MAXIMUM_PRIORITY));
565#endif
566}
567
568/**
569 * @brief Leaves a critical zone from thread context.
570 * @note This function cannot be used for reentrant critical zones.
571 *
572 * @special
573 */
574static inline void osalSysUnlock(void) {
575
576#if CORTEX_MODEL == 0
577 __enable_irq();
578#else
579 __set_BASEPRI(0);
580#endif
581}
582
583/**
584 * @brief Enters a critical zone from ISR context.
585 * @note This function cannot be used for reentrant critical zones.
586 *
587 * @special
588 */
589static inline void osalSysLockFromISR(void) {
590
591#if CORTEX_MODEL == 0
592 __disable_irq();
593#else
594 __set_BASEPRI(OSAL_BASEPRI(OSAL_IRQ_MAXIMUM_PRIORITY));
595#endif
596}
597
598/**
599 * @brief Leaves a critical zone from ISR context.
600 * @note This function cannot be used for reentrant critical zones.
601 *
602 * @special
603 */
604static inline void osalSysUnlockFromISR(void) {
605
606#if CORTEX_MODEL == 0
607 __enable_irq();
608#else
609 __set_BASEPRI(0);
610#endif
611}
612
613/**
614 * @brief Returns the execution status and enters a critical zone.
615 * @details This functions enters into a critical zone and can be called
616 * from any context. Because its flexibility it is less efficient
617 * than @p chSysLock() which is preferable when the calling context
618 * is known.
619 * @post The system is in a critical zone.
620 *
621 * @return The previous system status, the encoding of this
622 * status word is architecture-dependent and opaque.
623 *
624 * @xclass
625 */
626static inline syssts_t osalSysGetStatusAndLockX(void) {
627 syssts_t sts;
628
629#if CORTEX_MODEL == 0
630 sts = (syssts_t)__get_PRIMASK();
631 __disable_irq();
632#else
633 sts = (syssts_t)__get_BASEPRI();
634 __set_BASEPRI(OSAL_BASEPRI(OSAL_IRQ_MAXIMUM_PRIORITY));
635#endif
636 return sts;
637}
638
639/**
640 * @brief Restores the specified execution status and leaves a critical zone.
641 * @note A call to @p chSchRescheduleS() is automatically performed
642 * if exiting the critical zone and if not in ISR context.
643 *
644 * @param[in] sts the system status to be restored.
645 *
646 * @xclass
647 */
648static inline void osalSysRestoreStatusX(syssts_t sts) {
649
650#if CORTEX_MODEL == 0
651 if ((sts & (syssts_t)1) == (syssts_t)0) {
652 __enable_irq();
653 }
654#else
655 __set_BASEPRI(sts);
656#endif
657}
658
659/**
660 * @brief Adds an interval to a system time returning a system time.
661 *
662 * @param[in] systime base system time
663 * @param[in] interval interval to be added
664 * @return The new system time.
665 *
666 * @xclass
667 */
668static inline systime_t osalTimeAddX(systime_t systime,
669 sysinterval_t interval) {
670
671 return systime + (systime_t)interval;
672}
673
674/**
675 * @brief Subtracts two system times returning an interval.
676 *
677 * @param[in] start first system time
678 * @param[in] end second system time
679 * @return The interval representing the time difference.
680 *
681 * @xclass
682 */
683static inline sysinterval_t osalTimeDiffX(systime_t start, systime_t end) {
684
685 return (sysinterval_t)((systime_t)(end - start));
686}
687
688/**
689 * @brief Checks if the specified time is within the specified time window.
690 * @note When start==end then the function returns always false because the
691 * time window has zero size.
692 * @note This function can be called from any context.
693 *
694 * @param[in] time the time to be verified
695 * @param[in] start the start of the time window (inclusive)
696 * @param[in] end the end of the time window (non inclusive)
697 * @retval true current time within the specified time window.
698 * @retval false current time not within the specified time window.
699 *
700 * @xclass
701 */
702static inline bool osalTimeIsInRangeX(systime_t time,
703 systime_t start,
704 systime_t end) {
705
706 return (bool)((systime_t)((systime_t)time - (systime_t)start) <
707 (systime_t)((systime_t)end - (systime_t)start));
708}
709
710/**
711 * @brief Initializes a threads queue object.
712 *
713 * @param[out] tqp pointer to the threads queue object
714 *
715 * @init
716 */
717static inline void osalThreadQueueObjectInit(threads_queue_t *tqp) {
718
719 osalDbgCheck(tqp != NULL);
720}
721
722/**
723 * @brief Initializes an event source object.
724 *
725 * @param[out] esp pointer to the event source object
726 *
727 * @init
728 */
729static inline void osalEventObjectInit(event_source_t *esp) {
730
731 osalDbgCheck(esp != NULL);
732
733 esp->flags = (eventflags_t)0;
734 esp->cb = NULL;
735 esp->param = NULL;
736}
737
738/**
739 * @brief Initializes s @p mutex_t object.
740 *
741 * @param[out] mp pointer to the @p mutex_t object
742 *
743 * @init
744 */
745static inline void osalMutexObjectInit(mutex_t *mp) {
746
747 osalDbgCheck(mp != NULL);
748
749 *mp = 0;
750}
751
752#endif /* OSAL_H */
753
754/** @} */