diff --git a/os/halnew/osal/chibios/osal.c b/os/halnew/osal/chibios/osal.c new file mode 100644 index 000000000..8b0398538 --- /dev/null +++ b/os/halnew/osal/chibios/osal.c @@ -0,0 +1,237 @@ +/* + 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. */ +/*===========================================================================*/ + +/** + * @brief Pointer to a halt error message. + * @note The message is meant to be retrieved by the debugger after the + * system halt caused by an unexpected error. + */ +const char *osal_halt_msg; + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + +/** + * @brief OSAL module initialization. + * + * @api + */ +void osalInit(void) { + +} + +/** + * @brief System halt with error message. + * + * @param[in] reason the halt message pointer + * + * @api + */ +#if !defined(__DOXYGEN__) +__attribute__((weak)) +#endif +void osalSysHalt(const char *reason) { + + osal_halt_msg = reason; + chSysHalt(); +} + +/** + * @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 + */ +msg_t osalThreadSuspendS(thread_reference_t *trp) { + + chDbgAssert(*trp == NULL, "osalThreadSuspendS(), #1", "not NULL"); + + *trp = (thread_reference_t)chThdSelf(); + chSchGoSleepS(CH_STATE_SUSPENDED); + return chThdSelf()->p_msg; +} + +/** + * @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 + */ +void osalThreadResumeI(thread_reference_t *trp, msg_t msg) { + + if (*trp != NULL) { + + chDbgAssert((*trp)->p_state == CH_STATE_SUSPENDED, + "osalThreadResumeI(), #1", "not THD_STATE_SUSPENDED"); + + (*trp)->p_u.rdymsg = msg; + chSchReadyI((thread_t *)*trp); + *trp = NULL; + } +} + +/** + * @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 + */ +void osalThreadResumeS(thread_reference_t *trp, msg_t msg) { + + if (*trp != NULL) { + thread_t *tp = (thread_t *)*trp; + + chDbgAssert(tp->p_state == CH_STATE_SUSPENDED, + "osalThreadResumeS(), #1", "not THD_STATE_SUSPENDED"); + + *trp = NULL; + chSchWakeupS(tp, msg); + } +} + +/** + * @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 + */ +msg_t osalQueueGoSleepTimeoutS(threads_queue_t *tqp, systime_t time) { + + void wakeup(void *p) { + thread_t *tp = (thread_t *)p; + + chSysUnlockFromISR(); + tp->p_u.rdymsg = RDY_TIMEOUT; + chSchReadyI(queue_dequeue(tp)); + chSysUnlockFromISR(); + } + + if (TIME_IMMEDIATE == time) + return MSG_TIMEOUT; + + queue_insert(currp, tqp); + if (TIME_INFINITE == time) + chSchGoSleepS(CH_STATE_SUSPENDED); + else { + virtual_timer_t vt; + + chVTDoSetI(&vt, time, wakeup, currp); + chSchGoSleepS(CH_STATE_SUSPENDED); + if (chVTIsArmedI(&vt)) + chVTDoResetI(&vt); + } + return currp->p_u.rdymsg; +} + +/** + * @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) { + + if (queue_notempty(tqp)) { + thread_t *tp = queue_fifo_remove(tqp); + tp->p_u.rdymsg = msg; + chSchReadyI(tp); + } +} + +/** + * @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) { + + while (queue_notempty(tqp)) { + thread_t *tp = queue_fifo_remove(tqp); + tp->p_u.rdymsg = msg; + chSchReadyI(tp); + } +} + +/** @} */ diff --git a/os/halnew/osal/chibios/osal.h b/os/halnew/osal/chibios/osal.h new file mode 100644 index 000000000..52509e8c8 --- /dev/null +++ b/os/halnew/osal/chibios/osal.h @@ -0,0 +1,454 @@ +/* + 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 "ch.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 +/** @} */ + +/** + * @name Messages + * @{ + */ +#define MSG_OK RDY_OK +#define MSG_RESET RDY_RESET +#define MSG_TIMEOUT RDY_TIMEOUT +/** @} */ + +#if 0 +/** + * @name Special time constants + * @{ + */ +#define TIME_IMMEDIATE ((systime_t)0) +#define TIME_INFINITE ((systime_t)-1) +/** @} */ +#endif + +/*===========================================================================*/ +/* Module pre-compile time settings. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module data structures and types. */ +/*===========================================================================*/ + +#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 + +/** + * @brief Type of a thread reference. + */ +typedef thread_t * thread_reference_t; + +#if 0 +/** + * @brief Type of an event flags mask. + */ +typedef uint32_t eventflags_t; +#endif + +#if 0 +/** + * @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. */ +} event_source_t; +#endif + +/** + * @brief Type of a mutex. + * @note If the OS does not support mutexes or there is no OS then them + * mechanism can be simulated. + */ +#if CH_CFG_USE_MUTEXES || defined(__DOXYGEN__) +#elif CH_CFG_USE_SEMAPHORES +typedef semaphore_t mutex_t; +#else +typedef uint32_t mutex_t; +#endif + +#if 0 +/** + * @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 { + thread_reference_t tr; +} threads_queue_t; +#endif + +/*===========================================================================*/ +/* Module macros. */ +/*===========================================================================*/ + +/** + * @brief Condition assertion. + * @details If the condition check fails then the OSAL panics with the + * specified 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] msg the text message + * @param[in] remark a remark string + * + * @api + */ +#define osalDbgAssert(c, msg, remark) chDbgAssert(c, msg, 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, __FUNCTION__) + +/** + * @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() + +/** + * @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) + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#ifdef __cplusplus +extern "C" { +#endif + void osalInit(void); + void osalSysHalt(const char *reason); + msg_t osalThreadSuspendS(thread_reference_t *trp); + void osalThreadResumeI(thread_reference_t *trp, msg_t msg); + void osalThreadResumeS(thread_reference_t *trp, msg_t msg); + msg_t osalQueueGoSleepTimeoutS(threads_queue_t *tqp, systime_t time); + 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 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 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 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 Initializes an event flags object. + * + * @param[out] esp pointer to the event flags object + * + * @init + */ +static inline void osalEventObjectInit(event_source_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(event_source_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(event_source_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) { + +#if CH_CFG_USE_MUTEXES + chMtxObjectInit(mp); +#elif CH_CFG_USE_SEMAPHORES + chSemObjectInit((semaphore_t *)mp, 1); +#else + *mp = 0; +#endif +} + +/* + * @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) { + +#if CH_CFG_USE_MUTEXES + chMtxLock(mp); +#elif CH_CFG_USE_SEMAPHORES + chSemWait((semaphore_t *)mp); +#else + *mp = 1; +#endif +} + +/** + * @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) { + +#if CH_CFG_USE_MUTEXES + (void)mp; + chMtxUnlock(); +#elif CH_CFG_USE_SEMAPHORES + chSemSignal((semaphore_t *)mp); +#else + *mp = 0; +#endif +} + +/** + * @brief Initializes a threads queue object. + * + * @param[out] tqp pointer to the threads queue object + * + * @init + */ +static inline void osalQueueObjectInit(threads_queue_t *tqp) { + + queue_init(tqp); +} + +#endif /* _OSAL_H_ */ + +/** @} */ diff --git a/os/halnew/osal/chibios/osal.mk b/os/halnew/osal/chibios/osal.mk new file mode 100644 index 000000000..11ecd54b3 --- /dev/null +++ b/os/halnew/osal/chibios/osal.mk @@ -0,0 +1,5 @@ +# OSAL files. +OSALSRC += ${CHIBIOS}/os/halnew/osal/chibios/osal.c + +# Required include directories +OSALINC += ${CHIBIOS}/os/halnew/osal/chibios