diff --git a/demos/nil/NIL-STM32F051-DISCOVERY/Makefile b/demos/nil/NIL-STM32F051-DISCOVERY/Makefile index efa0f7d96..9e0e6cc8c 100644 --- a/demos/nil/NIL-STM32F051-DISCOVERY/Makefile +++ b/demos/nil/NIL-STM32F051-DISCOVERY/Makefile @@ -63,7 +63,7 @@ CHIBIOS = ../../.. #include $(CHIBIOS)/os/hal/boards/ST_STM32F0_DISCOVERY/board.mk #include $(CHIBIOS)/os/hal/ports/STM32F0xx/platform.mk include $(CHIBIOS)/os/nil/nil.mk -#include $(CHIBIOS)/os/rt/osal/osal.mk +include $(CHIBIOS)/os/nil/osal/osal.mk include $(CHIBIOS)/os/nil/ports/ARMCMx/compilers/GCC/mk/port_stm32f0xx.mk #include $(CHIBIOS)/test/test.mk diff --git a/os/nil/include/nil.h b/os/nil/include/nil.h index 898ec6f35..30c8012b0 100644 --- a/os/nil/include/nil.h +++ b/os/nil/include/nil.h @@ -213,7 +213,7 @@ struct nil_thread_cfg { /** * @brief Type of a thread reference. */ -typedef thread_t * thread_ref_t; +typedef thread_t * thread_reference_t; /** * @brief Structure representing a thread. @@ -226,7 +226,7 @@ struct nil_thread { union { msg_t msg; /**< @brief Wake-up message. */ void *p; /**< @brief Generic pointer. */ - thread_ref_t *trp; /**< @brief Pointer to thread reference. */ + thread_reference_t *trp; /**< @brief Pointer to thread reference. */ semaphore_t *semp; /**< @brief Pointer to semaphore. */ } u1; volatile systime_t timeout;/**< @brief Timeout counter, zero @@ -244,13 +244,13 @@ typedef struct { /** * @brief Pointer to the running thread. */ - thread_ref_t current; + thread_reference_t current; /** * @brief Pointer to the next thread to be executed. * @note This pointer must point at the same thread pointed by @p currp * or to an higher priority thread if a switch is required. */ - thread_ref_t next; + thread_reference_t next; #if NIL_CFG_TIMEDELTA == 0 || defined(__DOXYGEN__) /** * @brief System time. @@ -583,7 +583,7 @@ typedef struct { * * @init */ -#define chSemInit(sp, n) ((sp)->cnt = n) +#define chSemObjectInit(sp, n) ((sp)->cnt = n) /** * @brief Performs a wait operation on a semaphore. @@ -613,6 +613,13 @@ typedef struct { */ #define chSemWaitS(sp) chSemWaitTimeoutS(sp, TIME_INFINITE) +/** + * @brief Returns the semaphore counter current value. + * + * @iclass + */ +#define chSemGetCounterI(sp) ((sp)->cnt) + /** * @brief Current system time. * @details Returns the number of system ticks since the @p chSysInit() @@ -689,11 +696,11 @@ extern "C" { void chSysInit(void); void chSysHalt(const char *reason); void chSysTimerHandlerI(void); - thread_ref_t chSchReadyI(thread_ref_t trp, msg_t msg); + thread_reference_t chSchReadyI(thread_reference_t trp, msg_t msg); msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t timeout); void chSchRescheduleS(void); - msg_t chThdSuspendTimeoutS(thread_ref_t *trp, systime_t timeout); - void chThdResumeI(thread_ref_t *trp, msg_t msg); + msg_t chThdSuspendTimeoutS(thread_reference_t *trp, systime_t timeout); + void chThdResumeI(thread_reference_t *trp, msg_t msg); void chThdSleep(systime_t time); void chThdSleepUntil(systime_t time); systime_t chTimeNow(void); diff --git a/os/nil/osal/osal.c b/os/nil/osal/osal.c new file mode 100644 index 000000000..05443d465 --- /dev/null +++ b/os/nil/osal/osal.c @@ -0,0 +1,117 @@ +/* + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011,2012,2013 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT 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/RT 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 osal.c + * @brief OSAL module code. + * + * @addtogroup OSAL + * @{ + */ + +#include "osal.h" + +/*===========================================================================*/ +/* Module local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + +/** + * @brief Dequeues and wakes up one thread from the queue, if any. + * + * @param[in] tqp pointer to the threads queue object + * @param[in] msg the message code + * + * @iclass + */ +void osalQueueWakeupOneI(threads_queue_t *tqp, msg_t msg) { + semaphore_t *sp = &tqp->sem; + + if (chSemGetCounterI(&tqp->sem) < 0) { + thread_reference_t tr = nil.threads; + while (true) { + /* Is this thread waiting on this semaphore?*/ + if (tr->u1.semp == sp) { + + chDbgAssert(NIL_THD_IS_WTSEM(tr), "not waiting"); + + chSchReadyI(tr, msg); + return; + } + tr++; + + chDbgAssert(tr < &nil.threads[NIL_CFG_NUM_THREADS], + "pointer out of range"); + } + } +} + +/** + * @brief Dequeues and wakes up all threads from the queue. + * + * @param[in] tqp pointer to the threads queue object + * @param[in] msg the message code + * + * @iclass + */ +void osalQueueWakeupAllI(threads_queue_t *tqp, msg_t msg) { + semaphore_t *sp = &tqp->sem; + thread_reference_t tr; + cnt_t cnt; + + cnt = sp->cnt; + sp->cnt = 0; + tr = nil.threads; + while (cnt < 0) { + /* Is this thread waiting on this semaphore?*/ + if (tr->u1.semp == sp) { + + chDbgAssert(NIL_THD_IS_WTSEM(tr), "not waiting"); + + cnt++; + chSchReadyI(tr, msg); + } + tr++; + + chDbgAssert(tr < &nil.threads[NIL_CFG_NUM_THREADS], + "pointer out of range"); + } +} + +/** @} */ diff --git a/os/nil/osal/osal.h b/os/nil/osal/osal.h new file mode 100644 index 000000000..3e58154fa --- /dev/null +++ b/os/nil/osal/osal.h @@ -0,0 +1,735 @@ +/* + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011,2012,2013 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT 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/RT 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 osal.h + * @brief OSAL module header. + * + * @addtogroup OSAL + * @{ + */ + +#ifndef _OSAL_H_ +#define _OSAL_H_ + +#include +#include +#include + +#include "nil.h" + +/*===========================================================================*/ +/* Module constants. */ +/*===========================================================================*/ + +/** + * @name Common constants + * @{ + */ +#if !defined(FALSE) || defined(__DOXYGEN__) +#define FALSE 0 +#endif + +#if !defined(TRUE) || defined(__DOXYGEN__) +#define TRUE (!FALSE) +#endif + +#define OSAL_SUCCESS FALSE +#define OSAL_FAILED TRUE +/** @} */ + +#if 0 +/** + * @name Messages + * @{ + */ +#define MSG_OK RDY_OK +#define MSG_RESET RDY_RESET +#define MSG_TIMEOUT RDY_TIMEOUT +/** @} */ +#endif + +#if 0 +/** + * @name Special time constants + * @{ + */ +#define TIME_IMMEDIATE ((systime_t)0) +#define TIME_INFINITE ((systime_t)-1) +/** @} */ +#endif + +/** + * @name Systick modes. + * @{ + */ +#define OSAL_ST_MODE_NONE 0 +#define OSAL_ST_MODE_PERIODIC 1 +#define OSAL_ST_MODE_FREERUNNING 2 +/** @} */ + +/** + * @brief Systick mode required by the underlying OS. + */ +#if (NIL_CFG_TIMEDELTA == 0) || defined(__DOXYGEN__) +#define OSAL_ST_MODE OSAL_ST_MODE_PERIODIC +#else +#define OSAL_ST_MODE OSAL_ST_MODE_FREERUNNING +#endif + +/** + * @brief Required systick frequency or resolution. + */ +#define OSAL_SYSTICK_FREQUENCY NIL_CFG_ST_FREQUENCY + +/*===========================================================================*/ +/* Module pre-compile time settings. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* 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 + +/*===========================================================================*/ +/* Module data structures and types. */ +/*===========================================================================*/ + +#if 0 +/** + * @brief Type of a system status word. + */ +typedef uint32_t syssts_t; +#endif + +#if 0 +/** + * @brief Type of a message. + */ +typedef int32_t msg_t; +#endif + +#if 0 +/** + * @brief Type of system time counter. + */ +typedef uint32_t systime_t; +#endif + +#if 0 +/** + * @brief Type of realtime counter. + */ +typedef uint32_t rtcnt_t; +#endif + +#if 0 +/** + * @brief Type of a thread reference. + */ +typedef thread_t * thread_reference_t; +#endif + +typedef (*eventcallback_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 { + volatile eventflags_t flags; /**< @brief Flags stored into the + object. */ +} eventsource_t; + +/** + * @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 semaphore_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 In this implementation it is implemented as a single reference + * because there are no real threads. + */ +typedef struct { + semaphore_t sem; +} 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 convention for the message is the following:
+ * @(), #@ + * @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) chDbgAssert(c, remark) + +/** + * @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) /*chDbgCheck(c)*/ + +/** + * @brief I-Class state check. + * @note Not implemented in this simplified OSAL. + */ +#define osalDbgCheckClassI() /*chDbgCheckClassI()*/ +/** @} */ + +/** + * @brief S-Class state check. + * @note Not implemented in this simplified OSAL. + */ +#define osalDbgCheckClassS() /*chDbgCheckClassS()*/ + +/** + * @name IRQ service routines wrappers + * @{ + */ +/** + * @brief IRQ prologue code. + * @details This macro must be inserted at the start of all IRQ handlers. + */ +#define OSAL_IRQ_PROLOGUE() CH_IRQ_PROLOGUE() + +/** + * @brief IRQ epilogue code. + * @details This macro must be inserted at the end of all IRQ handlers. + */ +#define OSAL_IRQ_EPILOGUE() CH_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) CH_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 OSAL_S2ST(sec) S2ST(sec) + +/** + * @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 OSAL_MS2ST(msec) MS2ST(msec) +/** + * @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 OSAL_US2ST(usec) US2ST(usec) +/** @} */ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#ifdef __cplusplus +extern "C" { +#endif + void osalQueueWakeupOneI(threads_queue_t *tqp, msg_t msg); + void osalQueueWakeupAllI(threads_queue_t *tqp, msg_t msg); +#ifdef __cplusplus +} +#endif + +/*===========================================================================*/ +/* Module inline functions. */ +/*===========================================================================*/ + +/** + * @brief OSAL module initialization. + * + * @api + */ +static inline void osalInit(void) { + +} + +/** + * @brief System halt with error message. + * + * @param[in] reason the halt message pointer + * + * @api + */ +static inline void osalSysHalt(const char *reason) { + + chSysHalt(reason); +} + +/** + * @brief Enters a critical zone from thread context. + * @note This function cannot be used for reentrant critical zones. + * + * @special + */ +static inline void osalSysLock(void) { + + chSysLock(); +} + +/** + * @brief Leaves a critical zone from thread context. + * @note This function cannot be used for reentrant critical zones. + * + * @special + */ +static inline void osalSysUnlock(void) { + + chSysUnlock(); +} + +/** + * @brief Enters a critical zone from ISR context. + * @note This function cannot be used for reentrant critical zones. + * + * @special + */ +static inline void osalSysLockFromISR(void) { + + chSysLockFromISR(); +} + +/** + * @brief Leaves a critical zone from ISR context. + * @note This function cannot be used for reentrant critical zones. + * + * @special + */ +static inline void osalSysUnlockFromISR(void) { + + chSysUnlockFromISR(); +} + +/** + * @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 chSysGetStatusAndLockX(); +} + +/** + * @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) { + + chSysRestoreStatusX(sts); +} + +/** + * @brief Polled delay. + * @note The real delay is always few cycles in excess of the specified + * value. + * + * @param[in] cycles number of cycles + * + * @xclass + */ +#if PORT_SUPPORTS_RT || defined(__DOXYGEN__) +static inline void osalSysPolledDelayX(rtcnt_t cycles) { + + chSysPolledDelayX(cycles); +} +#endif + +/** + * @brief Systick callback for the underlying OS. + * @note This callback is only defined if the OSAL requires such a + * service from the HAL. + */ +#if (OSAL_ST_MODE != OSAL_ST_MODE_NONE) || defined(__DOXYGEN__) +static inline void osalOsTimerHandlerI(void) { + + chSysTimerHandlerI(); +} +#endif + +/** + * @brief Checks if a reschedule is required and performs it. + * @note I-Class functions invoked from thread context must not reschedule + * by themselves, an explicit reschedule using this function is + * required in this scenario. + * @note Not implemented in this simplified OSAL. + * + * @sclass + */ +static inline void osalOsRescheduleS(void) { + + chSchRescheduleS(); +} + +/** + * @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 + * @systime_t size. + * + * @return The system time in ticks. + * + * @xclass + */ +static inline systime_t osalOsGetSystemTimeX(void) { + + return chVTGetSystemTimeX(); +} + +/** + * @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 osalOsIsTimeWithinX(systime_t time, + systime_t start, + systime_t end) { + + return chVTIsTimeWithinX(time, start, end); +} + +/** + * @brief Suspends the invoking thread for the specified time. + * + * @param[in] time the delay in system ticks, the special values are + * handled as follow: + * - @a TIME_INFINITE is allowed but interpreted as a + * normal time specification. + * - @a TIME_IMMEDIATE this value is not allowed. + * . + * + * @sclass + */ +static inline void osalThreadSleepS(systime_t time) { + + chThdSleepS(time); +} + +/** + * @brief Suspends the invoking thread for the specified time. + * + * @param[in] time the delay in system ticks, the special values are + * handled as follow: + * - @a TIME_INFINITE is allowed but interpreted as a + * normal time specification. + * - @a TIME_IMMEDIATE this value is not allowed. + * . + * + * @api + */ +static inline void osalThreadSleep(systime_t time) { + + chThdSleep(time); +} + +/** + * @brief Sends the current thread sleeping and sets a reference variable. + * @note This function must reschedule, it can only be called from thread + * context. + * + * @param[in] trp a pointer to a thread reference object + * @return The wake up message. + * + * @sclass + */ +static inline msg_t osalThreadSuspendS(thread_reference_t *trp) { + + return chThdSuspendTimeoutS(trp, TIME_INFINITE); +} + +/** + * @brief Sends the current thread sleeping and sets a reference variable. + * @note This function must reschedule, it can only be called from thread + * context. + * + * @param[in] trp a pointer to a thread reference object + * @param[in] timeout the timeout in system ticks, the special values are + * handled as follow: + * - @a TIME_INFINITE the thread enters an infinite sleep + * state. + * - @a TIME_IMMEDIATE the thread is not enqueued and + * the function returns @p MSG_TIMEOUT as if a timeout + * occurred. + * . + * @return The wake up message. + * @retval MSG_TIMEOUT if the operation timed out. + * + * @sclass + */ +static inline msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, + systime_t timeout) { + + return chThdSuspendTimeoutS(trp, timeout); +} + +/** + * @brief Wakes up a thread waiting on a thread reference object. + * @note This function must not reschedule because it can be called from + * ISR context. + * + * @param[in] trp a pointer to a thread reference object + * @param[in] msg the message code + * + * @iclass + */ +static inline void osalThreadResumeI(thread_reference_t *trp, msg_t msg) { + + chThdResumeI(trp, msg); +} + +/** + * @brief Wakes up a thread waiting on a thread reference object. + * @note This function must reschedule, it can only be called from thread + * context. + * + * @param[in] trp a pointer to a thread reference object + * @param[in] msg the message code + * + * @iclass + */ +static inline void osalThreadResumeS(thread_reference_t *trp, msg_t msg) { + + chThdResumeI(trp, msg); + chSchRescheduleS(); +} + +/** + * @brief Initializes a threads queue object. + * + * @param[out] tqp pointer to the threads queue object + * + * @init + */ +static inline void osalQueueObjectInit(threads_queue_t *tqp) { + + chSemObjectInit(&tqp->sem, 0); +} + +/** + * @brief Enqueues the caller thread. + * @details The caller thread is enqueued and put to sleep until it is + * dequeued or the specified timeouts expires. + * + * @param[in] tqp pointer to the threads queue object + * @param[in] time the timeout in system ticks, the special values are + * handled as follow: + * - @a TIME_INFINITE the thread enters an infinite sleep + * state. + * - @a TIME_IMMEDIATE the thread is not enqueued and + * the function returns @p MSG_TIMEOUT as if a timeout + * occurred. + * . + * @return The message from @p osalQueueWakeupOneI() or + * @p osalQueueWakeupAllI() functions. + * @retval RDY_TIMEOUT if the thread has not been dequeued within the + * specified timeout or if the function has been + * invoked with @p TIME_IMMEDIATE as timeout + * specification. + * + * @sclass + */ +static inline msg_t osalQueueGoSleepTimeoutS(threads_queue_t *tqp, + systime_t time) { + + return chSemWaitTimeout(&tqp->sem, time); +} + +/** + * @brief Initializes an event flags object. + * + * @param[out] esp pointer to the event flags object + * + * @init + */ +static inline void osalEventObjectInit(eventsource_t *esp) { + + chEvtObjectInit(esp); +} + +/** + * @brief Add flags to an event source object. + * + * @param[in] esp pointer to the event flags object + * @param[in] flags flags to be ORed to the flags mask + * + * @iclass + */ +static inline void osalEventBroadcastFlagsI(eventsource_t *esp, + eventflags_t flags) { + + chEvtBroadcastFlagsI(esp, flags); +} + +/** + * @brief Add flags to an event source object. + * + * @param[in] esp pointer to the event flags object + * @param[in] flags flags to be ORed to the flags mask + * + * @iclass + */ +static inline void osalEventBroadcastFlags(eventsource_t *esp, + eventflags_t flags) { + + chEvtBroadcastFlags(esp, flags); +} + +/** + * @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) { + + chSemObjectInit((semaphore_t *)mp, 1); +} + +/* + * @brief Locks the specified mutex. + * @post The mutex is locked and inserted in the per-thread stack of owned + * mutexes. + * + * @param[in,out] mp pointer to the @p mutex_t object + * + * @api + */ +static inline void osalMutexLock(mutex_t *mp) { + + chSemWait((semaphore_t *)mp); +} + +/** + * @brief Unlocks the specified mutex. + * @note The HAL guarantees to release mutex in reverse lock order. The + * mutex being unlocked is guaranteed to be the last locked mutex + * by the invoking thread. + * The implementation can rely on this behavior and eventually + * ignore the @p mp parameter which is supplied in order to support + * those OSes not supporting a stack of the owned mutexes. + * + * @param[in,out] mp pointer to the @p mutex_t object + * + * @api + */ +static inline void osalMutexUnlock(mutex_t *mp) { + + chSemSignal((semaphore_t *)mp); +} + +#endif /* _OSAL_H_ */ + +/** @} */ diff --git a/os/nil/osal/osal.mk b/os/nil/osal/osal.mk new file mode 100644 index 000000000..f43560e2d --- /dev/null +++ b/os/nil/osal/osal.mk @@ -0,0 +1,5 @@ +# OSAL files. +OSALSRC += ${CHIBIOS}/os/nil/osal/osal.c + +# Required include directories +OSALINC += ${CHIBIOS}/os/nil/osal diff --git a/os/nil/src/nil.c b/os/nil/src/nil.c index a7677aeeb..d316f01e0 100644 --- a/os/nil/src/nil.c +++ b/os/nil/src/nil.c @@ -66,7 +66,7 @@ nil_system_t nil; * @special */ void chSysInit(void) { - thread_ref_t tr; + thread_reference_t tr; const thread_config_t *tcp; /* Port layer initialization.*/ @@ -136,7 +136,7 @@ void chSysHalt(const char *reason) { void chSysTimerHandlerI(void) { #if NIL_CFG_TIMEDELTA == 0 - thread_ref_t tr = &nil.threads[0]; + thread_reference_t tr = &nil.threads[0]; nil.systime++; do { /* Is the thread in a wait state with timeout?.*/ @@ -162,7 +162,7 @@ void chSysTimerHandlerI(void) { chSysLockFromISR(); } while (tr < &nil.threads[NIL_CFG_NUM_THREADS]); #else - thread_ref_t tr = &nil.threads[0]; + thread_reference_t tr = &nil.threads[0]; systime_t next = 0; chDbgAssert(nil.nexttime == port_timer_get_alarm(), @@ -218,7 +218,7 @@ void chSysTimerHandlerI(void) { * * @return The same reference passed as parameter. */ -thread_ref_t chSchReadyI(thread_ref_t tr, msg_t msg) { +thread_reference_t chSchReadyI(thread_reference_t tr, msg_t msg) { chDbgAssert((tr >= nil.threads) && (tr < &nil.threads[NIL_CFG_NUM_THREADS]), @@ -240,8 +240,8 @@ thread_ref_t chSchReadyI(thread_ref_t tr, msg_t msg) { * @sclass */ void chSchRescheduleS() { - thread_ref_t otr = nil.current; - thread_ref_t ntr = nil.next; + thread_reference_t otr = nil.current; + thread_reference_t ntr = nil.next; if (ntr != otr) { nil.current = ntr; @@ -272,7 +272,7 @@ void chSchRescheduleS() { * @sclass */ msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t timeout) { - thread_ref_t ntr, otr = nil.current; + thread_reference_t ntr, otr = nil.current; chDbgAssert(otr != &nil.threads[NIL_CFG_NUM_THREADS], "idle cannot sleep"); @@ -348,7 +348,7 @@ msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t timeout) { * * @sclass */ -msg_t chThdSuspendTimeoutS(thread_ref_t *trp, systime_t timeout) { +msg_t chThdSuspendTimeoutS(thread_reference_t *trp, systime_t timeout) { chDbgAssert(*trp == NULL, "not NULL"); @@ -367,10 +367,10 @@ msg_t chThdSuspendTimeoutS(thread_ref_t *trp, systime_t timeout) { * * @iclass */ -void chThdResumeI(thread_ref_t *trp, msg_t msg) { +void chThdResumeI(thread_reference_t *trp, msg_t msg) { if (*trp != NULL) { - thread_ref_t tr = *trp; + thread_reference_t tr = *trp; chDbgAssert(NIL_THD_IS_SUSP(tr), "not suspended"); @@ -544,7 +544,7 @@ void chSemSignal(semaphore_t *sp) { void chSemSignalI(semaphore_t *sp) { if (++sp->cnt <= 0) { - thread_ref_t tr = nil.threads; + thread_reference_t tr = nil.threads; while (true) { /* Is this thread waiting on this semaphore?*/ if (tr->u1.semp == sp) { @@ -555,6 +555,9 @@ void chSemSignalI(semaphore_t *sp) { return; } tr++; + + chDbgAssert(tr < &nil.threads[NIL_CFG_NUM_THREADS], + "pointer out of range"); } } } @@ -600,7 +603,7 @@ void chSemReset(semaphore_t *sp, cnt_t n) { * @iclass */ void chSemResetI(semaphore_t *sp, cnt_t n) { - thread_ref_t tr; + thread_reference_t tr; cnt_t cnt; cnt = sp->cnt; @@ -616,6 +619,9 @@ void chSemResetI(semaphore_t *sp, cnt_t n) { chSchReadyI(tr, MSG_RESET); } tr++; + + chDbgAssert(tr < &nil.threads[NIL_CFG_NUM_THREADS], + "pointer out of range"); } } diff --git a/os/rt/osal/osal.h b/os/rt/osal/osal.h index 7ae97f92b..f1d37699f 100644 --- a/os/rt/osal/osal.h +++ b/os/rt/osal/osal.h @@ -437,7 +437,7 @@ static inline void osalSysRestoreStatusX(syssts_t sts) { * * @xclass */ -#if CH_PORT_SUPPORTS_RT || defined(__DOXYGEN__) +#if PORT_SUPPORTS_RT || defined(__DOXYGEN__) static inline void osalSysPolledDelayX(rtcnt_t cycles) { chSysPolledDelayX(cycles); @@ -648,7 +648,8 @@ static inline void osalQueueObjectInit(threads_queue_t *tqp) { * * @sclass */ -static inline msg_t osalQueueGoSleepTimeoutS(threads_queue_t *tqp, systime_t time) { +static inline msg_t osalQueueGoSleepTimeoutS(threads_queue_t *tqp, + systime_t time) { return chQueueGoSleepTimeoutS(tqp, time); }