/* ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio. This file is part of ChibiOS. ChibiOS is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. ChibiOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /** * @file ch.h * @brief Nil RTOS main header file. * @details This header includes all the required kernel headers so it is the * only header you usually need to include in your application. * * @addtogroup NIL_KERNEL * @{ */ #ifndef CH_H #define CH_H #include "chtypes.h" #include "chconf.h" #include "chlicense.h" /*===========================================================================*/ /* Module constants. */ /*===========================================================================*/ /** * @brief ChibiOS/NIL identification macro. */ #define _CHIBIOS_NIL_ /** * @brief Stable release flag. */ #define CH_KERNEL_STABLE 0 /** * @name ChibiOS/NIL version identification * @{ */ /** * @brief Kernel version string. */ #define CH_KERNEL_VERSION "2.0.0" /** * @brief Kernel version major number. */ #define CH_KERNEL_MAJOR 2 /** * @brief Kernel version minor number. */ #define CH_KERNEL_MINOR 0 /** * @brief Kernel version patch number. */ #define CH_KERNEL_PATCH 0 /** @} */ /** * @name Wakeup messages * @{ */ #define MSG_OK (msg_t)0 /**< @brief OK wakeup message. */ #define MSG_TIMEOUT (msg_t)-1 /**< @brief Wake-up caused by a timeout condition. */ #define MSG_RESET (msg_t)-2 /**< @brief Wake-up caused by a reset condition. */ /** @} */ /** * @name Special time constants * @{ */ /** * @brief Zero time specification for some functions with a timeout * specification. * @note Not all functions accept @p TIME_IMMEDIATE as timeout parameter, * see the specific function documentation. */ #define TIME_IMMEDIATE ((systime_t)-1) /** * @brief Infinite time specification for all functions with a timeout * specification. */ #define TIME_INFINITE ((systime_t)0) /** @} */ /** * @name Thread state related macros * @{ */ #define NIL_STATE_READY (tstate_t)0 /**< @brief Thread ready or executing. */ #define NIL_STATE_SLEEPING (tstate_t)1 /**< @brief Thread sleeping. */ #define NIL_STATE_SUSP (tstate_t)2 /**< @brief Thread suspended. */ #define NIL_STATE_WTQUEUE (tstate_t)3 /**< @brief On queue or semaph. */ #define NIL_STATE_WTOREVT (tstate_t)4 /**< @brief Waiting for events. */ #define NIL_THD_IS_READY(tr) ((tr)->state == NIL_STATE_READY) #define NIL_THD_IS_SLEEPING(tr) ((tr)->state == NIL_STATE_SLEEPING) #define NIL_THD_IS_SUSP(tr) ((tr)->state == NIL_STATE_SUSP) #define NIL_THD_IS_WTQUEUE(tr) ((tr)->state == NIL_STATE_WTQUEUE) #define NIL_THD_IS_WTOREVT(tr) ((tr)->state == NIL_STATE_WTOREVT) /** @} */ /** * @name Events related macros * @{ */ /** * @brief All events allowed mask. */ #define ALL_EVENTS ((eventmask_t)-1) /** * @brief Returns an event mask from an event identifier. */ #define EVENT_MASK(eid) ((eventmask_t)(1 << (eid))) /** @} */ /*===========================================================================*/ /* Module pre-compile time settings. */ /*===========================================================================*/ /** * @brief Number of user threads in the application. * @note This number is not inclusive of the idle thread which is * implicitly handled. */ #if !defined(CH_CFG_NUM_THREADS) || defined(__DOXYGEN__) #define CH_CFG_NUM_THREADS 2 #endif /** * @brief System time counter resolution. * @note Allowed values are 16 or 32 bits. */ #if !defined(CH_CFG_ST_RESOLUTION) || defined(__DOXYGEN__) #define CH_CFG_ST_RESOLUTION 32 #endif /** * @brief System tick frequency. * @note This value together with the @p CH_CFG_ST_RESOLUTION * option defines the maximum amount of time allowed for * timeouts. */ #if !defined(CH_CFG_ST_FREQUENCY) || defined(__DOXYGEN__) #define CH_CFG_ST_FREQUENCY 100 #endif /** * @brief Time delta constant for the tick-less mode. * @note If this value is zero then the system uses the classic * periodic tick. This value represents the minimum number * of ticks that is safe to specify in a timeout directive. * The value one is not valid, timeouts are rounded up to * this value. */ #if !defined(CH_CFG_ST_TIMEDELTA) || defined(__DOXYGEN__) #define CH_CFG_ST_TIMEDELTA 0 #endif /** * @brief Semaphores APIs. * @details If enabled then the Semaphores APIs are included in the kernel. * * @note The default is @p TRUE. */ #if !defined(CH_CFG_USE_SEMAPHORES) || defined(__DOXYGEN__) #define CH_CFG_USE_SEMAPHORES TRUE #endif /** * @brief Mutexes APIs. * @details If enabled then the mutexes APIs are included in the kernel. * * @note Feature not currently implemented. * @note The default is @p FALSE. */ #if !defined(CH_CFG_USE_MUTEXES) || defined(__DOXYGEN__) #define CH_CFG_USE_MUTEXES FALSE #endif /** * @brief Events Flags APIs. * @details If enabled then the event flags APIs are included in the kernel. * * @note The default is @p TRUE. */ #if !defined(CH_CFG_USE_EVENTS) || defined(__DOXYGEN__) #define CH_CFG_USE_EVENTS TRUE #endif /** * @brief Mailboxes APIs. * @details If enabled then the asynchronous messages (mailboxes) APIs are * included in the kernel. * * @note The default is @p TRUE. * @note Requires @p CH_CFG_USE_SEMAPHORES. */ #if !defined(CH_CFG_USE_MAILBOXES) || defined(__DOXYGEN__) #define CH_CFG_USE_MAILBOXES TRUE #endif /** * @brief Core Memory Manager APIs. * @details If enabled then the core memory manager APIs are included * in the kernel. * * @note The default is @p TRUE. */ #if !defined(CH_CFG_USE_MEMCORE) || defined(__DOXYGEN__) #define CH_CFG_USE_MEMCORE TRUE #endif /** * @brief Heap Allocator APIs. * @details If enabled then the memory heap allocator APIs are included * in the kernel. * * @note The default is @p TRUE. */ #if !defined(CH_CFG_USE_HEAP) || defined(__DOXYGEN__) #define CH_CFG_USE_HEAP TRUE #endif /** * @brief Memory Pools Allocator APIs. * @details If enabled then the memory pools allocator APIs are included * in the kernel. * * @note The default is @p TRUE. */ #if !defined(CH_CFG_USE_MEMPOOLS) || defined(__DOXYGEN__) #define CH_CFG_USE_MEMPOOLS TRUE #endif /** * @brief Debug option, kernel statistics. * * @note Feature not currently implemented. * @note The default is @p FALSE. */ #if !defined(CH_DBG_STATISTICS) || defined(__DOXYGEN__) #define CH_DBG_STATISTICS FALSE #endif /** * @brief Debug option, system state check. * @note This is a planned feature, not yet implemented. * * @note The default is @p FALSE. */ #if !defined(CH_DBG_SYSTEM_STATE_CHECK) || defined(__DOXYGEN__) #define CH_DBG_SYSTEM_STATE_CHECK FALSE #endif /** * @brief Debug option, parameters checks. * * @note The default is @p FALSE. */ #if !defined(CH_DBG_ENABLE_CHECKS) || defined(__DOXYGEN__) #define CH_DBG_ENABLE_CHECKS FALSE #endif /** * @brief System assertions. * * @note The default is @p FALSE. */ #if !defined(CH_DBG_ENABLE_ASSERTS) || defined(__DOXYGEN__) #define CH_DBG_ENABLE_ASSERTS FALSE #endif /** * @brief Stack check. * * @note The default is @p FALSE. */ #if !defined(CH_DBG_ENABLE_STACK_CHECK) || defined(__DOXYGEN__) #define CH_DBG_ENABLE_STACK_CHECK FALSE #endif /** * @brief System initialization hook. */ #if !defined(CH_CFG_SYSTEM_INIT_HOOK) || defined(__DOXYGEN__) #define CH_CFG_SYSTEM_INIT_HOOK() {} #endif /** * @brief Threads descriptor structure extension. * @details User fields added to the end of the @p thread_t structure. */ #if !defined(CH_CFG_THREAD_EXT_FIELDS) || defined(__DOXYGEN__) #define CH_CFG_THREAD_EXT_FIELDS #endif /** * @brief Threads initialization hook. */ #if !defined(CH_CFG_THREAD_EXT_INIT_HOOK) || defined(__DOXYGEN__) #define CH_CFG_THREAD_EXT_INIT_HOOK(tr) {} #endif /** * @brief Idle thread enter hook. * @note This hook is invoked within a critical zone, no OS functions * should be invoked from here. * @note This macro can be used to activate a power saving mode. */ #if !defined(CH_CFG_IDLE_ENTER_HOOK) || defined(__DOXYGEN__) #define CH_CFG_IDLE_ENTER_HOOK() {} #endif /** * @brief Idle thread leave hook. * @note This hook is invoked within a critical zone, no OS functions * should be invoked from here. * @note This macro can be used to deactivate a power saving mode. */ #if !defined(CH_CFG_IDLE_LEAVE_HOOK) || defined(__DOXYGEN__) #define CH_CFG_IDLE_LEAVE_HOOK() {} #endif /** * @brief System halt hook. */ #if !defined(CH_CFG_SYSTEM_HALT_HOOK) || defined(__DOXYGEN__) #define CH_CFG_SYSTEM_HALT_HOOK(reason) {} #endif /*===========================================================================*/ /* Derived constants and error checks. */ /*===========================================================================*/ #if CH_CUSTOMER_LIC_NIL == FALSE #error "ChibiOS/NIL not licensed" #endif #if (CH_LICENSE_FEATURES != CH_FEATURES_FULL) && \ (CH_LICENSE_FEATURES != CH_FEATURES_INTERMEDIATE) && \ (CH_LICENSE_FEATURES == CH_FEATURES_BASIC) #error "invalid CH_LICENSE_FEATURES setting" #endif /* Restrictions in basic and intermediate modes.*/ #if (CH_LICENSE_FEATURES == CH_FEATURES_INTERMEDIATE) || \ (CH_LICENSE_FEATURES == CH_FEATURES_BASIC) /* System tick limited to 1000hz.*/ #if CH_CFG_ST_FREQUENCY > 1000 #undef CH_CFG_ST_FREQUENCY #define CH_CFG_ST_FREQUENCY 1000 #endif /* Restricted subsystems.*/ #undef CH_CFG_USE_MAILBOXES #define CH_CFG_USE_MAILBOXES FALSE #endif /* (CH_LICENSE_FEATURES == CH_FEATURES_INTERMEDIATE) || (CH_LICENSE_FEATURES == CH_FEATURES_BASIC) */ /* Restrictions in basic mode.*/ #if CH_LICENSE_FEATURES == CH_FEATURES_BASIC /* Tick-Less mode restricted.*/ #undef CH_CFG_ST_TIMEDELTA #define CH_CFG_ST_TIMEDELTA 0 /* Restricted subsystems.*/ #undef CH_CFG_USE_MEMCORE #undef CH_CFG_USE_MEMPOOLS #undef CH_CFG_USE_HEAP #define CH_CFG_USE_MEMCORE FALSE #define CH_CFG_USE_MEMPOOLS FALSE #define CH_CFG_USE_HEAP FALSE #endif /* CH_LICENSE_FEATURES == CH_FEATURES_BASIC */ #if !defined(_CHIBIOS_NIL_CONF_) #error "missing or wrong configuration file" #endif #if CH_CFG_NUM_THREADS < 1 #error "at least one thread must be defined" #endif #if CH_CFG_NUM_THREADS > 16 #error "ChibiOS/NIL is not recommended for thread-intensive applications," \ "consider ChibiOS/RT instead" #endif #if (CH_CFG_ST_RESOLUTION != 16) && (CH_CFG_ST_RESOLUTION != 32) #error "invalid CH_CFG_ST_RESOLUTION specified, must be 16 or 32" #endif #if CH_CFG_ST_FREQUENCY <= 0 #error "invalid CH_CFG_ST_FREQUENCY specified, must be greated than zero" #endif #if (CH_CFG_ST_TIMEDELTA < 0) || (CH_CFG_ST_TIMEDELTA == 1) #error "invalid CH_CFG_ST_TIMEDELTA specified, must " \ "be zero or greater than one" #endif #if CH_CFG_USE_MUTEXES == TRUE #error "mutexes not yet supported" #endif #if CH_DBG_STATISTICS == TRUE #error "statistics not yet supported" #endif #if (CH_DBG_SYSTEM_STATE_CHECK == TRUE) || \ (CH_DBG_ENABLE_CHECKS == TRUE) || \ (CH_DBG_ENABLE_ASSERTS == TRUE) || \ (CH_DBG_ENABLE_STACK_CHECK == TRUE) #define NIL_DBG_ENABLED TRUE #else #define NIL_DBG_ENABLED FALSE #endif /** Boundaries of the idle thread boundaries, only required if stack checking is enabled.*/ #if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || defined(__DOXYGEN__) #define THD_IDLE_BASE (&__main_thread_stack_base__) #define THD_IDLE_END (&__main_thread_stack_end__) #else #define THD_IDLE_BASE NULL #define THD_IDLE_END NULL #endif /*===========================================================================*/ /* Module data structures and types. */ /*===========================================================================*/ /** * @brief Type of system time. */ #if (CH_CFG_ST_RESOLUTION == 32) || defined(__DOXYGEN__) typedef uint32_t systime_t; #else typedef uint16_t systime_t; #endif /** * @brief Type of a structure representing a thread. * @note It is required as an early definition. */ typedef struct nil_thread thread_t; #include "chcore.h" /** * @brief Structure representing a queue of threads. */ struct nil_threads_queue { volatile cnt_t cnt; /**< @brief Threads Queue counter. */ }; /** * @brief Type of a queue of threads. */ typedef struct nil_threads_queue threads_queue_t; #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__) /** * @brief Type of a structure representing a semaphore. * @note Semaphores are implemented on thread queues, the object is the * same, the behavior is slightly different. */ typedef threads_queue_t semaphore_t; #endif /* CH_CFG_USE_SEMAPHORES == TRUE */ /** * @brief Thread function. */ typedef void (*tfunc_t)(void *p); /** * @brief Type of a structure representing a thread static configuration. */ typedef struct nil_thread_cfg thread_config_t; /** * @brief Structure representing a thread static configuration. */ struct nil_thread_cfg { stkalign_t *wbase; /**< @brief Thread working area base. */ stkalign_t *wend; /**< @brief Thread working area end. */ const char *namep; /**< @brief Thread name, for debugging. */ tfunc_t funcp; /**< @brief Thread function. */ void *arg; /**< @brief Thread function argument. */ }; /** * @brief Type of a thread reference. */ typedef thread_t * thread_reference_t; /** * @brief Structure representing a thread. */ struct nil_thread { struct port_context ctx; /**< @brief Processor context. */ tstate_t state; /**< @brief Thread state. */ /* Note, the following union contains a pointer while the thread is in a sleeping state (!NIL_THD_IS_READY()) else contains the wake-up message.*/ union { msg_t msg; /**< @brief Wake-up message. */ void *p; /**< @brief Generic pointer. */ thread_reference_t *trp; /**< @brief Pointer to thread reference.*/ threads_queue_t *tqp; /**< @brief Pointer to thread queue. */ #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__) semaphore_t *semp; /**< @brief Pointer to semaphore. */ #endif #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__) eventmask_t ewmask; /**< @brief Enabled events mask. */ #endif } u1; volatile systime_t timeout; /**< @brief Timeout counter, zero if disabled. */ #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__) eventmask_t epmask; /**< @brief Pending events mask. */ #endif #if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || defined(__DOXYGEN__) stkalign_t *wabase; /**< @brief Thread stack boundary. */ #endif /* Optional extra fields.*/ CH_CFG_THREAD_EXT_FIELDS }; /** * @brief Type of a structure representing the system. */ typedef struct nil_system nil_system_t; /** * @brief System data structure. * @note This structure contain all the data areas used by the OS except * stacks. */ struct nil_system { /** * @brief Pointer to the running thread. */ thread_t *current; /** * @brief Pointer to the next thread to be executed. * @note This pointer must point at the same thread pointed by @p current * or to an higher priority thread if a switch is required. */ thread_t *next; #if (CH_CFG_ST_TIMEDELTA == 0) || defined(__DOXYGEN__) /** * @brief System time. */ volatile systime_t systime; #endif #if (CH_CFG_ST_TIMEDELTA > 0) || defined(__DOXYGEN__) /** * @brief System time of the last tick event. */ systime_t lasttime; /** * @brief Time of the next scheduled tick event. */ systime_t nexttime; #endif #if (CH_DBG_SYSTEM_STATE_CHECK == TRUE) || defined(__DOXYGEN__) /** * @brief ISR nesting level. */ cnt_t isr_cnt; /** * @brief Lock nesting level. */ cnt_t lock_cnt; #endif #if (NIL_DBG_ENABLED == TRUE) || defined(__DOXYGEN__) /** * @brief Panic message. * @note This field is only present if some debug options have been * activated. * @note Accesses to this pointer must never be optimized out so the * field itself is declared volatile. */ const char * volatile dbg_panic_msg; #endif /** * @brief Thread structures for all the defined threads. */ thread_t threads[CH_CFG_NUM_THREADS + 1]; }; /*===========================================================================*/ /* Module macros. */ /*===========================================================================*/ #if CH_DBG_SYSTEM_STATE_CHECK == TRUE #define _dbg_enter_lock() (nil.lock_cnt = (cnt_t)1) #define _dbg_leave_lock() (nil.lock_cnt = (cnt_t)0) #endif /** * @brief Utility to make the parameter a quoted string. */ #define __CH_STRINGIFY(a) #a /** * @name Threads tables definition macros * @{ */ /** * @brief Start of user threads table. */ #define THD_TABLE_BEGIN \ const thread_config_t nil_thd_configs[CH_CFG_NUM_THREADS + 1] = { /** * @brief Entry of user threads table */ #define THD_TABLE_ENTRY(wap, name, funcp, arg) \ {wap, ((stkalign_t *)(wap)) + (sizeof (wap) / sizeof(stkalign_t)), \ name, funcp, arg}, /** * @brief End of user threads table. */ #define THD_TABLE_END \ {THD_IDLE_BASE, THD_IDLE_END, "idle", NULL, NULL} \ }; /** @} */ /** * @name Memory alignment support macros */ /** * @brief Alignment mask constant. * * @param[in] a alignment, must be a power of two */ #define MEM_ALIGN_MASK(a) ((size_t)(a) - 1U) /** * @brief Aligns to the previous aligned memory address. * * @param[in] p variable to be aligned * @param[in] a alignment, must be a power of two */ #define MEM_ALIGN_PREV(p, a) ((size_t)(p) & ~MEM_ALIGN_MASK(a)) /** * @brief Aligns to the new aligned memory address. * * @param[in] p variable to be aligned * @param[in] a alignment, must be a power of two */ #define MEM_ALIGN_NEXT(p, a) MEM_ALIGN_PREV((size_t)(p) + \ MEM_ALIGN_MASK(a), (a)) /** * @brief Returns whatever a pointer or memory size is aligned. * * @param[in] p variable to be aligned * @param[in] a alignment, must be a power of two */ #define MEM_IS_ALIGNED(p, a) (((size_t)(p) & MEM_ALIGN_MASK(a)) == 0U) /** * @brief Returns whatever a constant is a valid alignment. * @details Valid alignments are powers of two. * * @param[in] a alignment to be checked, must be a constant */ #define MEM_IS_VALID_ALIGNMENT(a) \ (((size_t)(a) != 0U) && (((size_t)(a) & ((size_t)(a) - 1U)) == 0U)) /** @} */ /** * @name Working Areas */ /** * @brief Calculates the total Working Area size. * * @param[in] n the stack size to be assigned to the thread * @return The total used memory in bytes. * * @api */ #define THD_WORKING_AREA_SIZE(n) MEM_ALIGN_NEXT(PORT_WA_SIZE(n), \ PORT_STACK_ALIGN) /** * @brief Static working area allocation. * @details This macro is used to allocate a static thread working area * aligned as both position and size. * * @param[in] s the name to be assigned to the stack array * @param[in] n the stack size to be assigned to the thread * * @api */ #define THD_WORKING_AREA(s, n) PORT_WORKING_AREA(s, n) /** @} */ /** * @name Threads abstraction macros */ /** * @brief Thread declaration macro. * @note Thread declarations should be performed using this macro because * the port layer could define optimizations for thread functions. */ #define THD_FUNCTION(tname, arg) PORT_THD_FUNCTION(tname, arg) /** @} */ /** * @name ISRs abstraction macros */ /** * @brief Priority level validation macro. * @details This macro determines if the passed value is a valid priority * level for the underlying architecture. * * @param[in] prio the priority level * @return Priority range result. * @retval false if the priority is invalid or if the architecture * does not support priorities. * @retval true if the priority is valid. */ #if defined(PORT_IRQ_IS_VALID_PRIORITY) || defined(__DOXYGEN__) #define CH_IRQ_IS_VALID_PRIORITY(prio) \ PORT_IRQ_IS_VALID_PRIORITY(prio) #else #define CH_IRQ_IS_VALID_PRIORITY(prio) false #endif /** * @brief Priority level validation macro. * @details This macro determines if the passed value is a valid priority * level that cannot preempt the kernel critical zone. * * @param[in] prio the priority level * @return Priority range result. * @retval false if the priority is invalid or if the architecture * does not support priorities. * @retval true if the priority is valid. */ #if defined(PORT_IRQ_IS_VALID_KERNEL_PRIORITY) || defined(__DOXYGEN__) #define CH_IRQ_IS_VALID_KERNEL_PRIORITY(prio) \ PORT_IRQ_IS_VALID_KERNEL_PRIORITY(prio) #else #define CH_IRQ_IS_VALID_KERNEL_PRIORITY(prio) false #endif /** * @brief IRQ handler enter code. * @note Usually IRQ handlers functions are also declared naked. * @note On some architectures this macro can be empty. * * @special */ #define CH_IRQ_PROLOGUE() \ PORT_IRQ_PROLOGUE(); \ _dbg_check_enter_isr() /** * @brief IRQ handler exit code. * @note Usually IRQ handlers function are also declared naked. * * @special */ #define CH_IRQ_EPILOGUE() \ _dbg_check_leave_isr(); \ PORT_IRQ_EPILOGUE() /** * @brief Standard normal IRQ handler declaration. * @note @p id can be a function name or a vector number depending on the * port implementation. * * @special */ #define CH_IRQ_HANDLER(id) PORT_IRQ_HANDLER(id) /** @} */ /** * @name Fast ISRs abstraction macros */ /** * @brief Standard fast IRQ handler declaration. * @note @p id can be a function name or a vector number depending on the * port implementation. * @note Not all architectures support fast interrupts. * * @special */ #define CH_FAST_IRQ_HANDLER(id) PORT_FAST_IRQ_HANDLER(id) /** @} */ /** * @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] sec number of seconds * @return The number of ticks. * * @api */ #define S2ST(sec) \ ((systime_t)((uint32_t)(sec) * (uint32_t)CH_CFG_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] msec number of milliseconds * @return The number of ticks. * * @api */ #define MS2ST(msec) \ ((systime_t)(((((uint32_t)(msec)) * \ ((uint32_t)CH_CFG_ST_FREQUENCY)) + 999UL) / 1000UL)) /** * @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] usec number of microseconds * @return The number of ticks. * * @api */ #define US2ST(usec) \ ((systime_t)(((((uint32_t)(usec)) * \ ((uint32_t)CH_CFG_ST_FREQUENCY)) + 999999UL) / 1000000UL)) /** @} */ /** * @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 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 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 US2RTC(freq, usec) (rtcnt_t)((((freq) + 999999UL) / 1000000UL) * (usec)) /** @} */ /** * @name Threads queues */ /** * @brief Data part of a static threads queue object initializer. * @details This macro should be used when statically initializing a threads * queue that is part of a bigger structure. * * @param[in] name the name of the threads queue variable */ #define _THREADS_QUEUE_DATA(name) {(cnt_t)0} /** * @brief Static threads queue object initializer. * @details Statically initialized threads queues require no explicit * initialization using @p queue_init(). * * @param[in] name the name of the threads queue variable */ #define _THREADS_QUEUE_DECL(name) \ threads_queue_t name = _THREADS_QUEUE_DATA(name) /** @} */ /** * @name Semaphores macros * @{ */ /** * @brief Data part of a static semaphore initializer. * @details This macro should be used when statically initializing a semaphore * that is part of a bigger structure. * * @param[in] name the name of the semaphore variable * @param[in] n the counter initial value, this value must be * non-negative */ #define _SEMAPHORE_DATA(name, n) {n} /** * @brief Static semaphore initializer. * @details Statically initialized semaphores require no explicit * initialization using @p chSemInit(). * * @param[in] name the name of the semaphore variable * @param[in] n the counter initial value, this value must be * non-negative */ #define SEMAPHORE_DECL(name, n) semaphore_t name = _SEMAPHORE_DATA(name, n) /** @} */ /** * @name Macro Functions * @{ */ /** * @brief Returns the current value of the system real time counter. * @note This function is only available if the port layer supports the * option @p PORT_SUPPORTS_RT. * * @return The value of the system realtime counter of * type rtcnt_t. * * @xclass */ #if (PORT_SUPPORTS_RT == TRUE) || defined(__DOXYGEN__) #define chSysGetRealtimeCounterX() (rtcnt_t)port_rt_get_counter_value() #endif /** * @brief Raises the system interrupt priority mask to the maximum level. * @details All the maskable interrupt sources are disabled regardless their * hardware priority. * @note Do not invoke this API from within a kernel lock. * * @special */ #define chSysDisable() { \ port_disable(); \ _dbg_check_disable(); \ } /** * @brief Raises the system interrupt priority mask to system level. * @details The interrupt sources that should not be able to preempt the kernel * are disabled, interrupt sources with higher priority are still * enabled. * @note Do not invoke this API from within a kernel lock. * @note This API is no replacement for @p chSysLock(), the @p chSysLock() * could do more than just disable the interrupts. * * @special */ #define chSysSuspend() { \ port_suspend(); \ _dbg_check_suspend(); \ } /** * @brief Lowers the system interrupt priority mask to user level. * @details All the interrupt sources are enabled. * @note Do not invoke this API from within a kernel lock. * @note This API is no replacement for @p chSysUnlock(), the * @p chSysUnlock() could do more than just enable the interrupts. * * @special */ #define chSysEnable() { \ _dbg_check_enable(); \ port_enable(); \ } /** * @brief Enters the kernel lock state. * * @special */ #define chSysLock() { \ port_lock(); \ _dbg_check_lock(); \ } /** * @brief Leaves the kernel lock state. * * @special */ #define chSysUnlock() { \ _dbg_check_unlock(); \ port_unlock(); \ } /** * @brief Enters the kernel lock state from within an interrupt handler. * @note This API may do nothing on some architectures, it is required * because on ports that support preemptable interrupt handlers * it is required to raise the interrupt mask to the same level of * the system mutual exclusion zone.
* It is good practice to invoke this API before invoking any I-class * syscall from an interrupt handler. * @note This API must be invoked exclusively from interrupt handlers. * * @special */ #define chSysLockFromISR() { \ port_lock_from_isr(); \ _dbg_check_lock_from_isr(); \ } /** * @brief Leaves the kernel lock state from within an interrupt handler. * * @note This API may do nothing on some architectures, it is required * because on ports that support preemptable interrupt handlers * it is required to raise the interrupt mask to the same level of * the system mutual exclusion zone.
* It is good practice to invoke this API after invoking any I-class * syscall from an interrupt handler. * @note This API must be invoked exclusively from interrupt handlers. * * @special */ #define chSysUnlockFromISR() { \ _dbg_check_unlock_from_isr(); \ port_unlock_from_isr(); \ } /** * @brief Evaluates if a reschedule is required. * * @retval true if there is a thread that must go in running state * immediately. * @retval false if preemption is not required. * * @iclass */ #define chSchIsRescRequiredI() ((bool)(nil.current != nil.next)) /** * @brief Returns a pointer to the current @p thread_t. * * @xclass */ #define chThdGetSelfX() nil.current /** * @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 clock. * @note The maximum specified value is implementation dependent. * * @param[in] sec time in seconds, must be different from zero * * @api */ #define chThdSleepSeconds(sec) chThdSleep(S2ST(sec)) /** * @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 clock. * @note The maximum specified value is implementation dependent. * * @param[in] msec time in milliseconds, must be different from zero * * @api */ #define chThdSleepMilliseconds(msec) chThdSleep(MS2ST(msec)) /** * @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 clock. * @note The maximum specified value is implementation dependent. * * @param[in] usec time in microseconds, must be different from zero * * @api */ #define chThdSleepMicroseconds(usec) chThdSleep(US2ST(usec)) /** * @brief Suspends the invoking thread for the specified time. * * @param[in] timeout the delay in system ticks * * @sclass */ #define chThdSleepS(timeout) (void) chSchGoSleepTimeoutS(NIL_STATE_SLEEPING, timeout) /** * @brief Suspends the invoking thread until the system time arrives to the * specified value. * * @param[in] abstime absolute system time * * @sclass */ #define chThdSleepUntilS(abstime) \ (void) chSchGoSleepTimeoutS(NIL_STATE_SLEEPING, (abstime) - \ chVTGetSystemTimeX()) /** * @brief Initializes a threads queue object. * * @param[out] tqp pointer to the threads queue object * * @init */ #define chThdQueueObjectInit(tqp) ((tqp)->cnt = (cnt_t)0) /** * @brief Evaluates to @p true if the specified queue is empty. * * @param[out] tqp pointer to the threads queue object * @return The queue status. * @retval false if the queue is not empty. * @retval true if the queue is empty. * * @iclass */ #define chThdQueueIsEmptyI(tqp) ((bool)(tqp->cnt >= (cnt_t)0)) #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__) /** * @brief Initializes a semaphore with the specified counter value. * * @param[out] sp pointer to a @p semaphore_t structure * @param[in] n initial value of the semaphore counter. Must be * non-negative. * * @init */ #define chSemObjectInit(sp, n) ((sp)->cnt = n) /** * @brief Performs a wait operation on a semaphore. * * @param[in] sp pointer to a @p semaphore_t structure * @return A message specifying how the invoking thread has been * released from the semaphore. * @retval CH_MSG_OK if the thread has not stopped on the semaphore or the * semaphore has been signaled. * @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset(). * * @api */ #define chSemWait(sp) chSemWaitTimeout(sp, TIME_INFINITE) /** * @brief Performs a wait operation on a semaphore. * * @param[in] sp pointer to a @p semaphore_t structure * @return A message specifying how the invoking thread has been * released from the semaphore. * @retval CH_MSG_OK if the thread has not stopped on the semaphore or the * semaphore has been signaled. * @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset(). * * @sclass */ #define chSemWaitS(sp) chSemWaitTimeoutS(sp, TIME_INFINITE) /** * @brief Decreases the semaphore counter. * @details This macro can be used when the counter is known to be positive. * * @param[in] sp pointer to a @p semaphore_t structure * * @iclass */ #define chSemFastWaitI(sp) ((sp)->cnt--) /** * @brief Increases the semaphore counter. * @details This macro can be used when the counter is known to be not * negative. * * @param[in] sp pointer to a @p semaphore_t structure * * @iclass */ #define chSemFastSignalI(sp) ((sp)->cnt++) /** * @brief Returns the semaphore counter current value. * * @iclass */ #define chSemGetCounterI(sp) ((sp)->cnt) #endif /* CH_CFG_USE_SEMAPHORES == TRUE */ /** * @brief Current system time. * @details Returns the number of system ticks since the @p chSysInit() * invocation. * @note The counter can reach its maximum and then restart from zero. * @note This function can be called from any context but its atomicity * is not guaranteed on architectures whose word size is less than * @p systime_t size. * * @return The system time in ticks. * * @xclass */ #if (CH_CFG_ST_TIMEDELTA == 0) || defined(__DOXYGEN__) #define chVTGetSystemTimeX() (nil.systime) #else #define chVTGetSystemTimeX() port_timer_get_time() #endif /** * @brief Returns the elapsed time since the specified start time. * * @param[in] start start time * @return The elapsed time. * * @xclass */ #define chVTTimeElapsedSinceX(start) \ ((systime_t)(chVTGetSystemTimeX() - (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 */ #define chVTIsTimeWithinX(time, start, end) \ ((bool)((systime_t)((time) - (start)) < (systime_t)((end) - (start)))) /** * @brief Function parameters check. * @details If the condition check fails then the kernel panics and halts. * @note The condition is tested only if the @p CH_DBG_ENABLE_CHECKS switch * is specified in @p chconf.h else the macro does nothing. * * @param[in] c the condition to be verified to be true * * @api */ #if !defined(chDbgCheck) #define chDbgCheck(c) do { \ /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \ if (CH_DBG_ENABLE_CHECKS != FALSE) { \ if (!(c)) { \ /*lint -restore*/ \ chSysHalt(__func__); \ } \ } \ } while (false) #endif /* !defined(chDbgCheck) */ /** * @brief Condition assertion. * @details If the condition check fails then the kernel panics with a * message and halts. * @note The condition is tested only if the @p CH_DBG_ENABLE_ASSERTS * switch is specified in @p chconf.h else the macro does nothing. * @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] r a remark string * * @api */ #if !defined(chDbgAssert) #define chDbgAssert(c, r) do { \ /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \ if (CH_DBG_ENABLE_ASSERTS != FALSE) { \ if (!(c)) { \ /*lint -restore*/ \ chSysHalt(__func__); \ } \ } \ } while (false) #endif /* !defined(chDbgAssert) */ /** @} */ /* Empty macros if the state checker is not enabled.*/ #if CH_DBG_SYSTEM_STATE_CHECK == FALSE #define _dbg_enter_lock() #define _dbg_leave_lock() #define _dbg_check_disable() #define _dbg_check_suspend() #define _dbg_check_enable() #define _dbg_check_lock() #define _dbg_check_unlock() #define _dbg_check_lock_from_isr() #define _dbg_check_unlock_from_isr() #define _dbg_check_enter_isr() #define _dbg_check_leave_isr() #define chDbgCheckClassI() #define chDbgCheckClassS() #endif /*===========================================================================*/ /* External declarations. */ /*===========================================================================*/ #if !defined(__DOXYGEN__) #if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || defined(__DOXYGEN__) extern stkalign_t __main_thread_stack_base__, __main_thread_stack_end__; #endif extern nil_system_t nil; extern const thread_config_t nil_thd_configs[CH_CFG_NUM_THREADS + 1]; #endif #ifdef __cplusplus extern "C" { #endif void chSysInit(void); void chSysHalt(const char *reason); void chSysTimerHandlerI(void); void chSysUnconditionalLock(void); void chSysUnconditionalUnlock(void); syssts_t chSysGetStatusAndLockX(void); bool chSysIsCounterWithinX(rtcnt_t cnt, rtcnt_t start, rtcnt_t end); void chSysPolledDelayX(rtcnt_t cycles); void chSysRestoreStatusX(syssts_t sts); thread_t *chSchReadyI(thread_t *tp, msg_t msg); bool chSchIsPreemptionRequired(void); void chSchDoReschedule(void); void chSchRescheduleS(void); msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t timeout); msg_t chThdSuspendTimeoutS(thread_reference_t *trp, systime_t timeout); void chThdResumeI(thread_reference_t *trp, msg_t msg); void chThdSleep(systime_t timeout); void chThdSleepUntil(systime_t abstime); msg_t chThdEnqueueTimeoutS(threads_queue_t *tqp, systime_t timeout); void chThdDoDequeueNextI(threads_queue_t *tqp, msg_t msg); void chThdDequeueNextI(threads_queue_t *tqp, msg_t msg); void chThdDequeueAllI(threads_queue_t *tqp, msg_t msg); #if CH_CFG_USE_SEMAPHORES == TRUE msg_t chSemWaitTimeout(semaphore_t *sp, systime_t timeout); msg_t chSemWaitTimeoutS(semaphore_t *sp, systime_t timeout); void chSemSignal(semaphore_t *sp); void chSemSignalI(semaphore_t *sp); void chSemReset(semaphore_t *sp, cnt_t n); void chSemResetI(semaphore_t *sp, cnt_t n); #endif /* CH_CFG_USE_SEMAPHORES == TRUE */ #if CH_CFG_USE_EVENTS == TRUE void chEvtSignal(thread_t *tp, eventmask_t mask); void chEvtSignalI(thread_t *tp, eventmask_t mask); eventmask_t chEvtWaitAnyTimeout(eventmask_t mask, systime_t timeout); #endif #if CH_DBG_SYSTEM_STATE_CHECK == TRUE void _dbg_check_disable(void); void _dbg_check_suspend(void); void _dbg_check_enable(void); void _dbg_check_lock(void); void _dbg_check_unlock(void); void _dbg_check_lock_from_isr(void); void _dbg_check_unlock_from_isr(void); void _dbg_check_enter_isr(void); void _dbg_check_leave_isr(void); void chDbgCheckClassI(void); void chDbgCheckClassS(void); #endif #ifdef __cplusplus } #endif /* Optional subsystems.*/ #include "chmboxes.h" #include "chmemcore.h" #include "chmempools.h" #include "chheap.h" #endif /* CH_H */ /** @} */ 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417