From 39fa482f9d563010abcd505ea95ec1711edc1cef Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Mon, 22 Feb 2016 16:15:28 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@8924 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/common/ext/nasa-osal/src/osapi.c | 342 +++++++++++++++++++++++++++++++++++- 1 file changed, 339 insertions(+), 3 deletions(-) (limited to 'os') diff --git a/os/common/ext/nasa-osal/src/osapi.c b/os/common/ext/nasa-osal/src/osapi.c index 85237d27c..bb76f312a 100644 --- a/os/common/ext/nasa-osal/src/osapi.c +++ b/os/common/ext/nasa-osal/src/osapi.c @@ -41,6 +41,14 @@ #error "NASA OSAL requires CH_CFG_USE_REGISTRY" #endif +#if CH_CFG_USE_MEMCORE == FALSE +#error "NASA OSAL requires CH_CFG_USE_MEMCORE" +#endif + +#if CH_CFG_USE_MEMPOOLS == FALSE +#error "NASA OSAL requires CH_CFG_USE_MEMPOOLS" +#endif + /*===========================================================================*/ /* Module local definitions. */ /*===========================================================================*/ @@ -61,6 +69,12 @@ */ typedef struct { bool printf_enabled; + memory_pool_t binary_semaphores_pool; + memory_pool_t count_semaphores_pool; + memory_pool_t mutexes_pool; + binary_semaphore_t binary_semaphores[OS_MAX_BIN_SEMAPHORES]; + semaphore_t count_semaphores[OS_MAX_COUNT_SEMAPHORES]; + mutex_t mutexes[OS_MAX_MUTEXES]; } osal_t; /*===========================================================================*/ @@ -93,8 +107,33 @@ int32 OS_API_Init(void) { chSysInit(); + /* OS_printf() initially disabled.*/ osal.printf_enabled = false; + /* Binary Semaphores pool initialization.*/ + chPoolObjectInit(&osal.binary_semaphores_pool, + sizeof (binary_semaphore_t), + NULL); + chPoolLoadArray(&osal.binary_semaphores_pool, + &osal.binary_semaphores, + sizeof (binary_semaphore_t)); + + /* Counter Semaphores pool initialization.*/ + chPoolObjectInit(&osal.count_semaphores_pool, + sizeof (semaphore_t), + NULL); + chPoolLoadArray(&osal.count_semaphores_pool, + &osal.count_semaphores, + sizeof (semaphore_t)); + + /* Mutexes pool initialization.*/ + chPoolObjectInit(&osal.mutexes_pool, + sizeof (mutex_t), + NULL); + chPoolLoadArray(&osal.mutexes_pool, + &osal.mutexes, + sizeof (mutex_t)); + return OS_SUCCESS; } @@ -204,6 +243,276 @@ int32 OS_Milli2Ticks(uint32 milli_seconds) { /*-- Semaphore and Mutex API ------------------------------------------------*/ +/** + * @brief Binary semaphore creation. + * + * @param[out] sem_id pointer to a binary semaphore id variable + * @param[in] sem_name the binary semaphore name + * @param[in] sem_initial_value semaphore intial value + * @param[in] options semaphore options + * @return An error code. + * + * @api + */ +int32 OS_BinSemCreate (uint32 *sem_id, const char *sem_name, + uint32 sem_initial_value, uint32 options) { + binary_semaphore_t *bsp; + + (void)options; + + /* NULL pointer checks.*/ + if ((sem_id == NULL) || (sem_name == NULL)) { + return OS_INVALID_POINTER; + } + + /* Checking semaphore name length.*/ + if (strlen(sem_name) >= OS_MAX_API_NAME) { + return OS_ERR_NAME_TOO_LONG; + } + + /* Semaphore counter check, it is binary so only 0 and 1.*/ + if (sem_initial_value > 1) { + return OS_INVALID_INT_NUM; + } + + bsp = chPoolAlloc(&osal.binary_semaphores_pool); + + if (bsp == 0) { + return OS_SEM_FAILURE; + } + + /* Semaphore is initialized.*/ + chBSemObjectInit(bsp, sem_initial_value == 0 ? false : true); + + *sem_id = (uint32)bsp; + + return OS_SUCCESS; +} + +/** + * @brief Binary semaphore deletion. + * + * @param[in] sem_id binary semaphore id variable + * @return An error code. + * + * @api + */ +int32 OS_BinSemDelete(uint32 sem_id) { + binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; + + if ((bsp < &osal.binary_semaphores[0]) || + (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { + return OS_ERR_INVALID_ID; + } + + chSysLock(); + + /* Resetting the semaphore, no threads in queue.*/ + chBSemResetI(bsp, true); + + /* Flagging it as unused and returning it to the pool.*/ + bsp->sem.queue.prev = NULL; + chPoolFreeI(&osal.binary_semaphores_pool, (void *)bsp); + + /* Required because some thready could have been made ready.*/ + chSchRescheduleS(); + + chSysUnlock(); + + return OS_SUCCESS; +} + +/** + * @brief Binary semaphore flush. + * @note The state of the binary semaphore is not changed. + * + * @param[in] sem_id binary semaphore id variable + * @return An error code. + * + * @api + */ +int32 OS_BinSemFlush(uint32 sem_id) { + binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; + + if ((bsp < &osal.binary_semaphores[0]) || + (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { + return OS_ERR_INVALID_ID; + } + + chSysLock(); + + /* If the semaphore is not in use then error.*/ + if (bsp->sem.queue.prev == NULL) { + chSysUnlock(); + return OS_SEM_FAILURE; + } + + if (bsp->sem.cnt < 0) { + chBSemResetI(bsp, true); + chSchRescheduleS(); + } + + chSysUnlock(); + + return OS_SUCCESS; +} + +/** + * @brief Binary semaphore give. + * + * @param[in] sem_id binary semaphore id variable + * @return An error code. + * + * @api + */ +int32 OS_BinSemGive(uint32 sem_id) { + binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; + + if ((bsp < &osal.binary_semaphores[0]) || + (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { + return OS_ERR_INVALID_ID; + } + + chSysLock(); + + /* If the semaphore is not in use then error.*/ + if (bsp->sem.queue.prev == NULL) { + chSysUnlock(); + return OS_SEM_FAILURE; + } + + chBSemSignalI(bsp); + chSchRescheduleS(); + + chSysUnlock(); + + return OS_SUCCESS; +} + +/** + * @brief Binary semaphore take. + * + * @param[in] sem_id binary semaphore id variable + * @return An error code. + * + * @api + */ +int32 OS_BinSemTake(uint32 sem_id) { + binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; + + if ((bsp < &osal.binary_semaphores[0]) || + (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { + return OS_ERR_INVALID_ID; + } + + chSysLock(); + + /* If the semaphore is not in use then error.*/ + if (bsp->sem.queue.prev == NULL) { + chSysUnlock(); + return OS_SEM_FAILURE; + } + + (void) chBSemWaitS(bsp); + + chSysUnlock(); + + return OS_SUCCESS; +} + +/** + * @brief Binary semaphore take with timeout. + * + * @param[in] sem_id binary semaphore id variable + * @param[in] msecs timeout in milliseconds + * @return An error code. + * + * @api + */ +int32 OS_BinSemTimedWait(uint32 sem_id, uint32 msecs) { + binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; + msg_t msg; + + if ((bsp < &osal.binary_semaphores[0]) || + (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { + return OS_ERR_INVALID_ID; + } + + /* Timeouts of zero not allowed.*/ + if (msecs == 0) { + return OS_INVALID_INT_NUM; + } + + chSysLock(); + + /* If the semaphore is not in use then error.*/ + if (bsp->sem.queue.prev == NULL) { + chSysUnlock(); + return OS_SEM_FAILURE; + } + + msg = chBSemWaitTimeoutS(bsp, MS2ST(msecs)); + + chSysUnlock(); + + return msg == MSG_TIMEOUT ? OS_SEM_TIMEOUT : OS_SUCCESS; +} + +/** + * @brief Retrieves a binary semaphore id by name. + * @note It is not currently implemented. + * + * @param[out] sem_id pointer to a binary semaphore id variable + * @param[in] sem_name the binary semaphore name + * @return An error code. + * + * @api + */ +int32 OS_BinSemGetIdByName(uint32 *sem_id, const char *sem_name) { + + /* NULL pointer checks.*/ + if ((sem_id == NULL) || (sem_name == NULL)) { + return OS_INVALID_POINTER; + } + + return OS_ERR_NOT_IMPLEMENTED; +} + +/** + * @brief Returns binary semaphore information. + * + * @param[in] sem_id binary semaphore id variable + * @param[in] bin_prop binary semaphore properties + * @return An error code. + * + * @api + */ +int32 OS_BinSemGetInfo(uint32 sem_id, OS_bin_sem_prop_t *bin_prop) { + binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; + + /* NULL pointer checks.*/ + if (bin_prop == NULL) { + return OS_INVALID_POINTER; + } + + if ((bsp < &osal.binary_semaphores[0]) || + (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { + return OS_ERR_INVALID_ID; + } + + chSysLock(); + + /* If the semaphore is not in use then error.*/ + if (bsp->sem.queue.prev == NULL) { + chSysUnlock(); + return OS_SEM_FAILURE; + } + + chSysUnlock(); + + return OS_ERR_NOT_IMPLEMENTED; +} + /*-- Task Control API -------------------------------------------------------*/ /** @@ -279,6 +588,7 @@ int32 OS_TaskCreate(uint32 *task_id, /** * @brief Installs a deletion handler. + * @note It is not currently implemented. * * @param[in] function_pointer the handler function * @return An error code. @@ -425,6 +735,12 @@ uint32 OS_TaskGetId(void) { /** * @brief Retrieves a task id by name. + * + * @param[out] task_id pointer to a task id variable + * @param[in] task_name the task name + * @return An error code. + * + * @api */ int32 OS_TaskGetIdByName (uint32 *task_id, const char *task_name) { thread_t *tp; @@ -452,11 +768,31 @@ int32 OS_TaskGetIdByName (uint32 *task_id, const char *task_name) { return OS_ERR_NAME_NOT_FOUND; } -int32 OS_TaskGetInfo (uint32 task_id, OS_task_prop_t *task_prop) { +/** + * @brief Returns task information. + * + * @param[in] sem_id binary semaphore id variable + * @param[in] task_prop task properties + * @return An error code. + * + * @api + */ +int32 OS_TaskGetInfo(uint32 task_id, OS_task_prop_t *task_prop) { + thread_t *tp = (thread_t *)task_id; + size_t wasize = (size_t)tp - (size_t)tp->stklimit + sizeof (thread_t); - (void)task_id; - (void)task_prop; + /* NULL pointer checks.*/ + if (task_prop == NULL) { + return OS_INVALID_POINTER; + } + + strncpy(task_prop->name, tp->name, OS_MAX_API_NAME - 1); + task_prop->creator = (uint32)chSysGetIdleThreadX(); + task_prop->stack_size = (uint32)MEM_ALIGN_NEXT(wasize, PORT_STACK_ALIGN); + task_prop->priority = (uint32)256U - (uint32)tp->realprio; + task_prop->OStask_id = task_id; return OS_ERR_NOT_IMPLEMENTED; } + /** @} */ -- cgit v1.2.3