123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- /*
- ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- /**
- * @file osal.h
- * @brief OSAL module header.
- *
- * @addtogroup OSAL
- * @{
- */
- #ifndef OSAL_H
- #define OSAL_H
- #include <stddef.h>
- #include <stdint.h>
- #include <stdbool.h>
- /*===========================================================================*/
- /* Module constants. */
- /*===========================================================================*/
- /**
- * @name Common constants
- * @{
- */
- #if !defined(FALSE) || defined(__DOXYGEN__)
- #define FALSE 0
- #endif
- #if !defined(TRUE) || defined(__DOXYGEN__)
- #define TRUE 1
- #endif
- #define OSAL_SUCCESS false
- #define OSAL_FAILED true
- /** @} */
- /**
- * @name Messages
- * @{
- */
- #define MSG_OK (msg_t)0
- #define MSG_TIMEOUT (msg_t)-1
- #define MSG_RESET (msg_t)-2
- /** @} */
- /**
- * @name Special time constants
- * @{
- */
- #define TIME_IMMEDIATE ((sysinterval_t)0)
- #define TIME_INFINITE ((sysinterval_t)-1)
- /** @} */
- /**
- * @name Systick modes.
- * @{
- */
- #define OSAL_ST_MODE_NONE 0
- #define OSAL_ST_MODE_PERIODIC 1
- #define OSAL_ST_MODE_FREERUNNING 2
- /** @} */
- /**
- * @name Systick parameters.
- * @{
- */
- /**
- * @brief Size in bits of the @p systick_t type.
- */
- #define OSAL_ST_RESOLUTION 32
- /**
- * @brief Required systick frequency or resolution.
- */
- #define OSAL_ST_FREQUENCY 1000
- /**
- * @brief Systick mode required by the underlying OS.
- */
- #define OSAL_ST_MODE OSAL_ST_MODE_PERIODIC
- /** @} */
- /**
- * @name IRQ-related constants
- * @{
- */
- /**
- * @brief Total priority levels.
- * @brief Implementation not mandatory.
- */
- #define OSAL_IRQ_PRIORITY_LEVELS 16U
- /**
- * @brief Highest IRQ priority for HAL drivers.
- * @brief Implementation not mandatory.
- */
- #define OSAL_IRQ_MAXIMUM_PRIORITY 0U
- /** @} */
- /*===========================================================================*/
- /* Module pre-compile time settings. */
- /*===========================================================================*/
- /**
- * @brief Enables OSAL assertions.
- */
- #if !defined(OSAL_DBG_ENABLE_ASSERTS) || defined(__DOXYGEN__)
- #define OSAL_DBG_ENABLE_ASSERTS FALSE
- #endif
- /**
- * @brief Enables OSAL functions parameters checks.
- */
- #if !defined(OSAL_DBG_ENABLE_CHECKS) || defined(__DOXYGEN__)
- #define OSAL_DBG_ENABLE_CHECKS FALSE
- #endif
- /*===========================================================================*/
- /* Derived constants and error checks. */
- /*===========================================================================*/
- #if !(OSAL_ST_MODE == OSAL_ST_MODE_NONE) && \
- !(OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) && \
- !(OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING)
- #error "invalid OSAL_ST_MODE setting in osal.h"
- #endif
- #if (OSAL_ST_RESOLUTION != 16) && (OSAL_ST_RESOLUTION != 32)
- #error "invalid OSAL_ST_RESOLUTION, must be 16 or 32"
- #endif
- /*===========================================================================*/
- /* Module data structures and types. */
- /*===========================================================================*/
- /**
- * @brief Type of a system status word.
- */
- typedef uint32_t syssts_t;
- /**
- * @brief Type of a message.
- */
- typedef int32_t msg_t;
- /**
- * @brief Type of system time counter.
- */
- typedef uint32_t systime_t;
- /**
- * @brief Type of system time interval.
- */
- typedef uint32_t sysinterval_t;
- /**
- * @brief Type of realtime counter.
- */
- typedef uint32_t rtcnt_t;
- /**
- * @brief Type of a thread reference.
- */
- typedef void * thread_reference_t;
- /**
- * @brief Type of an event flags mask.
- */
- typedef uint32_t eventflags_t;
- /**
- * @brief Type of an event flags object.
- * @note The content of this structure is not part of the API and should
- * not be relied upon. Implementers may define this structure in
- * an entirely different way.
- * @note Retrieval and clearing of the flags are not defined in this
- * API and are implementation-dependent.
- */
- typedef struct event_source event_source_t;
- /**
- * @brief Type of an event source callback.
- * @note This type is not part of the OSAL API and is provided
- * exclusively as an example and for convenience.
- */
- typedef void (*eventcallback_t)(event_source_t *esp);
- /**
- * @brief Events source object.
- * @note The content of this structure is not part of the API and should
- * not be relied upon. Implementers may define this structure in
- * an entirely different way.
- * @note Retrieval and clearing of the flags are not defined in this
- * API and are implementation-dependent.
- */
- struct event_source {
- volatile eventflags_t flags; /**< @brief Stored event flags. */
- eventcallback_t cb; /**< @brief Event source callback. */
- void *param; /**< @brief User defined field. */
- };
- /**
- * @brief Type of a mutex.
- * @note If the OS does not support mutexes or there is no OS then them
- * mechanism can be simulated.
- */
- typedef uint32_t mutex_t;
- /**
- * @brief Type of a thread queue.
- * @details A thread queue is a queue of sleeping threads, queued threads
- * can be dequeued one at time or all together.
- * @note If the OSAL is implemented on a bare metal machine without RTOS
- * then the queue can be implemented as a single thread reference.
- */
- typedef struct {
- thread_reference_t tr;
- } threads_queue_t;
- /*===========================================================================*/
- /* Module macros. */
- /*===========================================================================*/
- /**
- * @name Debug related macros
- * @{
- */
- /**
- * @brief Condition assertion.
- * @details If the condition check fails then the OSAL panics with a
- * message and halts.
- * @note The condition is tested only if the @p OSAL_ENABLE_ASSERTIONS
- * switch is enabled.
- * @note The remark string is not currently used except for putting a
- * comment in the code about the assertion.
- *
- * @param[in] c the condition to be verified to be true
- * @param[in] remark a remark string
- *
- * @api
- */
- #define osalDbgAssert(c, remark) do { \
- /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
- if (OSAL_DBG_ENABLE_ASSERTS != FALSE) { \
- if (!(c)) { \
- /*lint -restore*/ \
- osalSysHalt(__func__); \
- } \
- } \
- } while (false)
- /**
- * @brief Function parameters check.
- * @details If the condition check fails then the OSAL panics and halts.
- * @note The condition is tested only if the @p OSAL_ENABLE_CHECKS switch
- * is enabled.
- *
- * @param[in] c the condition to be verified to be true
- *
- * @api
- */
- #define osalDbgCheck(c) do { \
- /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
- if (OSAL_DBG_ENABLE_CHECKS != FALSE) { \
- if (!(c)) { \
- /*lint -restore*/ \
- osalSysHalt(__func__); \
- } \
- } \
- } while (false)
- /**
- * @brief I-Class state check.
- * @note Implementation is optional.
- */
- #define osalDbgCheckClassI()
- /**
- * @brief S-Class state check.
- * @note Implementation is optional.
- */
- #define osalDbgCheckClassS()
- /** @} */
- /**
- * @name IRQ service routines wrappers
- * @{
- */
- /**
- * @brief Priority level verification macro.
- */
- #define OSAL_IRQ_IS_VALID_PRIORITY(n) \
- (((n) >= OSAL_IRQ_MAXIMUM_PRIORITY) && ((n) < OSAL_IRQ_PRIORITY_LEVELS))
- /**
- * @brief IRQ prologue code.
- * @details This macro must be inserted at the start of all IRQ handlers.
- */
- #define OSAL_IRQ_PROLOGUE()
- /**
- * @brief IRQ epilogue code.
- * @details This macro must be inserted at the end of all IRQ handlers.
- */
- #define OSAL_IRQ_EPILOGUE()
- /**
- * @brief IRQ handler function declaration.
- * @details This macro hides the details of an ISR function declaration.
- *
- * @param[in] id a vector name as defined in @p vectors.s
- */
- #define OSAL_IRQ_HANDLER(id) void id(void)
- /** @} */
- /**
- * @name Time conversion utilities
- * @{
- */
- /**
- * @brief Seconds to system ticks.
- * @details Converts from seconds to system ticks number.
- * @note The result is rounded upward to the next tick boundary.
- *
- * @param[in] secs number of seconds
- * @return The number of ticks.
- *
- * @api
- */
- #define OSAL_S2I(secs) \
- ((sysinterval_t)((uint32_t)(secs) * (uint32_t)OSAL_ST_FREQUENCY))
- /**
- * @brief Milliseconds to system ticks.
- * @details Converts from milliseconds to system ticks number.
- * @note The result is rounded upward to the next tick boundary.
- *
- * @param[in] msecs number of milliseconds
- * @return The number of ticks.
- *
- * @api
- */
- #define OSAL_MS2I(msecs) \
- ((sysinterval_t)((((((uint32_t)(msecs)) * \
- ((uint32_t)OSAL_ST_FREQUENCY)) - 1UL) / 1000UL) + 1UL))
- /**
- * @brief Microseconds to system ticks.
- * @details Converts from microseconds to system ticks number.
- * @note The result is rounded upward to the next tick boundary.
- *
- * @param[in] usecs number of microseconds
- * @return The number of ticks.
- *
- * @api
- */
- #define OSAL_US2I(usecs) \
- ((sysinterval_t)((((((uint32_t)(usecs)) * \
- ((uint32_t)OSAL_ST_FREQUENCY)) - 1UL) / 1000000UL) + 1UL))
- /** @} */
- /**
- * @name Time conversion utilities for the realtime counter
- * @{
- */
- /**
- * @brief Seconds to realtime counter.
- * @details Converts from seconds to realtime counter cycles.
- * @note The macro assumes that @p freq >= @p 1.
- *
- * @param[in] freq clock frequency, in Hz, of the realtime counter
- * @param[in] sec number of seconds
- * @return The number of cycles.
- *
- * @api
- */
- #define OSAL_S2RTC(freq, sec) ((freq) * (sec))
- /**
- * @brief Milliseconds to realtime counter.
- * @details Converts from milliseconds to realtime counter cycles.
- * @note The result is rounded upward to the next millisecond boundary.
- * @note The macro assumes that @p freq >= @p 1000.
- *
- * @param[in] freq clock frequency, in Hz, of the realtime counter
- * @param[in] msec number of milliseconds
- * @return The number of cycles.
- *
- * @api
- */
- #define OSAL_MS2RTC(freq, msec) (rtcnt_t)((((freq) + 999UL) / 1000UL) * (msec))
- /**
- * @brief Microseconds to realtime counter.
- * @details Converts from microseconds to realtime counter cycles.
- * @note The result is rounded upward to the next microsecond boundary.
- * @note The macro assumes that @p freq >= @p 1000000.
- *
- * @param[in] freq clock frequency, in Hz, of the realtime counter
- * @param[in] usec number of microseconds
- * @return The number of cycles.
- *
- * @api
- */
- #define OSAL_US2RTC(freq, usec) (rtcnt_t)((((freq) + 999999UL) / 1000000UL) * (usec))
- /** @} */
- /**
- * @name Sleep macros using absolute time
- * @{
- */
- /**
- * @brief Delays the invoking thread for the specified number of seconds.
- * @note The specified time is rounded up to a value allowed by the real
- * system tick clock.
- * @note The maximum specifiable value is implementation dependent.
- *
- * @param[in] secs time in seconds, must be different from zero
- *
- * @api
- */
- #define osalThreadSleepSeconds(secs) osalThreadSleep(OSAL_S2I(secs))
- /**
- * @brief Delays the invoking thread for the specified number of
- * milliseconds.
- * @note The specified time is rounded up to a value allowed by the real
- * system tick clock.
- * @note The maximum specifiable value is implementation dependent.
- *
- * @param[in] msecs time in milliseconds, must be different from zero
- *
- * @api
- */
- #define osalThreadSleepMilliseconds(msecs) osalThreadSleep(OSAL_MS2I(msecs))
- /**
- * @brief Delays the invoking thread for the specified number of
- * microseconds.
- * @note The specified time is rounded up to a value allowed by the real
- * system tick clock.
- * @note The maximum specifiable value is implementation dependent.
- *
- * @param[in] usecs time in microseconds, must be different from zero
- *
- * @api
- */
- #define osalThreadSleepMicroseconds(usecs) osalThreadSleep(OSAL_US2I(usecs))
- /** @} */
- /*===========================================================================*/
- /* External declarations. */
- /*===========================================================================*/
- extern const char *osal_halt_msg;
- #ifdef __cplusplus
- extern "C" {
- #endif
- void osalInit(void);
- void osalSysHalt(const char *reason);
- void osalSysPolledDelayX(rtcnt_t cycles);
- void osalOsTimerHandlerI(void);
- void osalOsRescheduleS(void);
- systime_t osalOsGetSystemTimeX(void);
- void osalThreadSleepS(sysinterval_t time);
- void osalThreadSleep(sysinterval_t time);
- msg_t osalThreadSuspendS(thread_reference_t *trp);
- msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, sysinterval_t timeout);
- void osalThreadResumeI(thread_reference_t *trp, msg_t msg);
- void osalThreadResumeS(thread_reference_t *trp, msg_t msg);
- msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout);
- void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg);
- void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg);
- void osalEventBroadcastFlagsI(event_source_t *esp, eventflags_t flags);
- void osalEventBroadcastFlags(event_source_t *esp, eventflags_t flags);
- void osalEventSetCallback(event_source_t *esp,
- eventcallback_t cb,
- void *param);
- void osalMutexLock(mutex_t *mp);
- void osalMutexUnlock(mutex_t *mp);
- #ifdef __cplusplus
- }
- #endif
- /*===========================================================================*/
- /* Module inline functions. */
- /*===========================================================================*/
- /**
- * @brief Disables interrupts globally.
- *
- * @special
- */
- static inline void osalSysDisable(void) {
- }
- /**
- * @brief Enables interrupts globally.
- *
- * @special
- */
- static inline void osalSysEnable(void) {
- }
- /**
- * @brief Enters a critical zone from thread context.
- * @note This function cannot be used for reentrant critical zones.
- *
- * @special
- */
- static inline void osalSysLock(void) {
- }
- /**
- * @brief Leaves a critical zone from thread context.
- * @note This function cannot be used for reentrant critical zones.
- *
- * @special
- */
- static inline void osalSysUnlock(void) {
- }
- /**
- * @brief Enters a critical zone from ISR context.
- * @note This function cannot be used for reentrant critical zones.
- *
- * @special
- */
- static inline void osalSysLockFromISR(void) {
- }
- /**
- * @brief Leaves a critical zone from ISR context.
- * @note This function cannot be used for reentrant critical zones.
- *
- * @special
- */
- static inline void osalSysUnlockFromISR(void) {
- }
- /**
- * @brief Returns the execution status and enters a critical zone.
- * @details This functions enters into a critical zone and can be called
- * from any context. Because its flexibility it is less efficient
- * than @p chSysLock() which is preferable when the calling context
- * is known.
- * @post The system is in a critical zone.
- *
- * @return The previous system status, the encoding of this
- * status word is architecture-dependent and opaque.
- *
- * @xclass
- */
- static inline syssts_t osalSysGetStatusAndLockX(void) {
- return (syssts_t)0;
- }
- /**
- * @brief Restores the specified execution status and leaves a critical zone.
- * @note A call to @p chSchRescheduleS() is automatically performed
- * if exiting the critical zone and if not in ISR context.
- *
- * @param[in] sts the system status to be restored.
- *
- * @xclass
- */
- static inline void osalSysRestoreStatusX(syssts_t sts) {
- (void)sts;
- }
- /**
- * @brief Adds an interval to a system time returning a system time.
- *
- * @param[in] systime base system time
- * @param[in] interval interval to be added
- * @return The new system time.
- *
- * @xclass
- */
- static inline systime_t osalTimeAddX(systime_t systime,
- sysinterval_t interval) {
- return systime + (systime_t)interval;
- }
- /**
- * @brief Subtracts two system times returning an interval.
- *
- * @param[in] start first system time
- * @param[in] end second system time
- * @return The interval representing the time difference.
- *
- * @xclass
- */
- static inline sysinterval_t osalTimeDiffX(systime_t start, systime_t end) {
- return (sysinterval_t)((systime_t)(end - start));
- }
- /**
- * @brief Checks if the specified time is within the specified time window.
- * @note When start==end then the function returns always true because the
- * whole time range is specified.
- * @note This function can be called from any context.
- *
- * @param[in] time the time to be verified
- * @param[in] start the start of the time window (inclusive)
- * @param[in] end the end of the time window (non inclusive)
- * @retval true current time within the specified time window.
- * @retval false current time not within the specified time window.
- *
- * @xclass
- */
- static inline bool osalTimeIsInRangeX(systime_t time,
- systime_t start,
- systime_t end) {
- return (bool)((time - start) < (end - start));
- }
- /**
- * @brief Initializes a threads queue object.
- *
- * @param[out] tqp pointer to the threads queue object
- *
- * @init
- */
- static inline void osalThreadQueueObjectInit(threads_queue_t *tqp) {
- osalDbgCheck(tqp != NULL);
- }
- /**
- * @brief Initializes an event source object.
- *
- * @param[out] esp pointer to the event source object
- *
- * @init
- */
- static inline void osalEventObjectInit(event_source_t *esp) {
- osalDbgCheck(esp != NULL);
- esp->flags = (eventflags_t)0;
- esp->cb = NULL;
- esp->param = NULL;
- }
- /**
- * @brief Initializes s @p mutex_t object.
- *
- * @param[out] mp pointer to the @p mutex_t object
- *
- * @init
- */
- static inline void osalMutexObjectInit(mutex_t *mp) {
- osalDbgCheck(mp != NULL);
- *mp = 0;
- }
- #endif /* OSAL_H */
- /** @} */
|