/*FUNCTION********************************************************************** * * Function Name : OSA_SemaWait * Description : This function checks the semaphore's counting value, if it is * positive, decreases it and returns kStatus_OSA_Success, otherwise, timeout * will be used for wait. The parameter timeout indicates how long should wait * in milliseconds. Pass OSA_WAIT_FOREVER to wait indefinitely, pass 0 will * return kStatus_OSA_Timeout immediately if semaphore is not positive. * This function returns kStatus_OSA_Success if the semaphore is received, returns * kStatus_OSA_Timeout if the semaphore is not received within the specified * 'timeout', returns kStatus_OSA_Error if any errors occur during waiting. * *END**************************************************************************/ osa_status_t OSA_SemaWait(semaphore_t *pSem, uint32_t timeout) { INT8U err; /* If timeout is 0, try to get the semaphore. */ if (!timeout) { if (OSSemAccept(*pSem) > 0) { return kStatus_OSA_Success; } else { return kStatus_OSA_Timeout; } } /* If timeout is not 0, convert it to tickes. */ timeout = wait_timeout_msec_to_tick(timeout); OSSemPend(*pSem, timeout, &err); if (OS_ERR_NONE == err) { return kStatus_OSA_Success; } else if (OS_ERR_TIMEOUT == err) { return kStatus_OSA_Timeout; } else { return kStatus_OSA_Error; } }
/*FUNCTION********************************************************************** * * Function Name : OSA_MutexLock * Description : This function checks the mutex's status, if it is unlocked, * lock it and returns kStatus_OSA_Success, otherwise, timeout will be used for * wait. The parameter timeout indicates how long should wait in milliseconds. * Pass OSA_WAIT_FOREVER to wait indefinitely, pass 0 will return the value * kStatus_OSA_Timeout immediately if mutex is locked. * This function returns kStatus_OSA_Success if the mutex is obtained, returns * kStatus_OSA_Timeout if the mutex is not obtained within the specified * 'timeout', returns kStatus_OSA_Error if any errors occur during waiting. * *END**************************************************************************/ osa_status_t OSA_MutexLock(mutex_t *pMutex, uint32_t timeout) { OS_ERR err; /* If timeout is 0, try to lock the mutex. */ if (0 == timeout) { OSMutexPend(pMutex, 0u, OS_OPT_PEND_NON_BLOCKING, (CPU_TS*)0, &err); } else { timeout = wait_timeout_msec_to_tick(timeout); OSMutexPend(pMutex, timeout, OS_OPT_PEND_BLOCKING, (CPU_TS*)0, &err); } if (OS_ERR_NONE == err) { return kStatus_OSA_Success; } else if ((OS_ERR_TIMEOUT == err) || (OS_ERR_PEND_WOULD_BLOCK == err)) { return kStatus_OSA_Timeout; } else { return kStatus_OSA_Error; } }
/*FUNCTION********************************************************************** * * Function Name : OSA_SemaWait * Description : This function checks the semaphore's counting value, if it is * positive, decreases it and returns kStatus_OSA_Success, otherwise, timeout * will be used for wait. The parameter timeout indicates how long should wait * in milliseconds. Pass OSA_WAIT_FOREVER to wait indefinitely, pass 0 will * return kStatus_OSA_Timeout immediately if semaphore is not positive. * This function returns kStatus_OSA_Success if the semaphore is received, returns * kStatus_OSA_Timeout if the semaphore is not received within the specified * 'timeout', returns kStatus_OSA_Error if any errors occur during waiting. * *END**************************************************************************/ osa_status_t OSA_SemaWait(semaphore_t *pSem, uint32_t timeout) { OS_ERR err; /* If timeout is 0, try to get semaphore. */ if (0 == timeout) { OSSemPend(pSem, 0u, OS_OPT_PEND_NON_BLOCKING, (CPU_TS*)0, &err); } else { timeout = wait_timeout_msec_to_tick(timeout); OSSemPend(pSem, timeout, OS_OPT_PEND_BLOCKING, (CPU_TS*)0, &err); } if (OS_ERR_NONE == err) { return kStatus_OSA_Success; } else if ((OS_ERR_TIMEOUT == err) || (OS_ERR_PEND_WOULD_BLOCK == err)) { return kStatus_OSA_Timeout; } else { return kStatus_OSA_Error; } }
/*FUNCTION********************************************************************** * * Function Name : OSA_EventWait * Description : This function checks the event's status, if it meets the wait * condition, return kStatus_OSA_Success, otherwise, timeout will be used for * wait. The parameter timeout indicates how long should wait in milliseconds. * Pass OSA_WAIT_FOREVER to wait indefinitely, pass 0 will return the value * kStatus_OSA_Timeout immediately if wait condition is not met. The event flags * will be cleared if the event is auto clear mode. Flags that wakeup waiting * task could be obtained from the parameter setFlags. * This function returns kStatus_OSA_Success if wait condition is met, returns * kStatus_OSA_Timeout if wait condition is not met within the specified * 'timeout', returns kStatus_OSA_Error if any errors occur during waiting. * *END**************************************************************************/ osa_status_t OSA_EventWait(event_t *pEvent, event_flags_t flagsToWait, bool waitAll, uint32_t timeout, event_flags_t *setFlags) { OS_ERR err; OS_OPT opt; /* Prepare wait options base on wait type and clear type. */ if (waitAll) { opt = OS_OPT_PEND_FLAG_SET_ALL; } else { opt = OS_OPT_PEND_FLAG_SET_ANY; } if (kEventAutoClear == pEvent->clearMode) { opt |= OS_OPT_PEND_FLAG_CONSUME; } if (0U == timeout) { opt |= OS_OPT_PEND_NON_BLOCKING; } else { timeout = wait_timeout_msec_to_tick(timeout); opt |= OS_OPT_PEND_BLOCKING; } *setFlags = OSFlagPend(&(pEvent->group), flagsToWait, timeout, opt, (CPU_TS*)0, &err); if (OS_ERR_NONE == err) { return kStatus_OSA_Success; } else if ((OS_ERR_TIMEOUT == err) || (OS_ERR_PEND_WOULD_BLOCK == err)) { return kStatus_OSA_Timeout; } else { return kStatus_OSA_Error; } }
/*FUNCTION********************************************************************** * * Function Name : OSA_EventWait * Description : This function checks the event's status, if it meets the wait * condition, return kStatus_OSA_Success, otherwise, timeout will be used for * wait. The parameter timeout indicates how long should wait in milliseconds. * Pass OSA_WAIT_FOREVER to wait indefinitely, pass 0 will return the value * kStatus_OSA_Timeout immediately if wait condition is not met. The event flags * will be cleared if the event is auto clear mode. Flags that wakeup waiting * task could be obtained from the parameter setFlags. * This function returns kStatus_OSA_Success if wait condition is met, returns * kStatus_OSA_Timeout if wait condition is not met within the specified * 'timeout', returns kStatus_OSA_Error if any errors occur during waiting. * *END**************************************************************************/ osa_status_t OSA_EventWait(event_t *pEvent, event_flags_t flagsToWait, bool waitAll, uint32_t timeout, event_flags_t *setFlags) { INT8U err, opt; /* Prepare wait options base on wait type and clear type. */ if (waitAll) { opt = OS_FLAG_WAIT_SET_ALL; } else { opt = OS_FLAG_WAIT_SET_ANY; } if (kEventAutoClear == pEvent->clearMode) { opt |= OS_FLAG_CONSUME; } /* If timeout is 0, try to wait. */ if (0U == timeout) { *setFlags = OSFlagAccept (pEvent->pGroup, flagsToWait, opt, &err); } else { /* If timeout is not 0, convert it to tickes. */ timeout = wait_timeout_msec_to_tick(timeout); *setFlags = OSFlagPend(pEvent->pGroup, flagsToWait, opt, timeout, &err); } if (OS_ERR_NONE == err) { return kStatus_OSA_Success; } else if ((OS_ERR_TIMEOUT == err) || (OS_ERR_FLAG_NOT_RDY == err)) { return kStatus_OSA_Timeout; } else { return kStatus_OSA_Error; } }
/*FUNCTION********************************************************************** * * Function Name : OSA_MutexLock * Description : This function checks the mutex's status, if it is unlocked, * lock it and returns kStatus_OSA_Success, otherwise, timeout will be used for * wait. The parameter timeout indicates how long should wait in milliseconds. * Pass OSA_WAIT_FOREVER to wait indefinitely, pass 0 will return the value * kStatus_OSA_Timeout immediately if mutex is locked. * This function returns kStatus_OSA_Success if the mutex is obtained, returns * kStatus_OSA_Timeout if the mutex is not obtained within the specified * 'timeout', returns kStatus_OSA_Error if any errors occur during waiting. * *END**************************************************************************/ osa_status_t OSA_MutexLock(mutex_t *pMutex, uint32_t timeout) { INT8U err; /* Avoid to get the mutex current task has already got. */ if ((*pMutex)->OSEventPtr == (void *)OSTCBCur) { return kStatus_OSA_Error; } if (!timeout) /* If timeout is 0, try to lock the mutex. */ { BOOLEAN pollResult = OSMutexAccept(*pMutex, &err); if (OS_ERR_NONE == err) { if (OS_TRUE == pollResult) { return kStatus_OSA_Success; } else { return kStatus_OSA_Timeout; } } else { return kStatus_OSA_Error; } } /* If timeout is not 0, convert it to tickes. */ timeout = wait_timeout_msec_to_tick(timeout); OSMutexPend(*pMutex, timeout, &err); if (OS_ERR_NONE == err) { return kStatus_OSA_Success; } else if (OS_ERR_TIMEOUT == err) { return kStatus_OSA_Timeout; } else { return kStatus_OSA_Error; } }
/*FUNCTION********************************************************************** * * Function Name : OSA_MsgQGet * Description : This function checks the queue's status, if it is not empty, * get message from it and return kStatus_OSA_Success, otherwise, timeout will * be used for wait. The parameter timeout indicates how long should wait in * milliseconds. Pass OSA_WAIT_FOREVER to wait indefinitely, pass 0 will return * kStatus_OSA_Timeout immediately if queue is empty. * This function returns kStatus_OSA_Success if message is got successfully, * returns kStatus_OSA_Timeout if message queue is empty within the specified * 'timeout', returns kStatus_OSA_Error if any errors occur during waiting. * *END**************************************************************************/ osa_status_t OSA_MsgQGet(msg_queue_handler_t handler, void *pMessage, uint32_t timeout) { OS_ERR err; OS_OPT opt; OS_MSG_SIZE dummy; void* localMsg; int32_t *from_ptr, *to_ptr; uint32_t msg_size = handler->size; if (0u == timeout) { opt = OS_OPT_PEND_NON_BLOCKING; } else { opt = OS_OPT_PEND_BLOCKING; timeout = wait_timeout_msec_to_tick(timeout); } localMsg = OSQPend(&(handler->queue), timeout, opt, &dummy, (CPU_TS*)0, &err); if (OS_ERR_NONE == err) { /* Copy localMsg to msg */ to_ptr = (int32_t*)pMessage; from_ptr = (int32_t*)localMsg; while (msg_size--) { *to_ptr++ = *from_ptr++; } OSMemPut(&(handler->mem), localMsg, &err); return kStatus_OSA_Success; } else if ((OS_ERR_PEND_WOULD_BLOCK == err) || (OS_ERR_TIMEOUT == err)) { return kStatus_OSA_Timeout; } else { return kStatus_OSA_Error; } }
/*FUNCTION********************************************************************** * * Function Name : OSA_MsgQGet * Description : This function checks the queue's status, if it is not empty, * get message from it and return kStatus_OSA_Success, otherwise, timeout will * be used for wait. The parameter timeout indicates how long should wait in * milliseconds. Pass OSA_WAIT_FOREVER to wait indefinitely, pass 0 will return * kStatus_OSA_Timeout immediately if queue is empty. * This function returns kStatus_OSA_Success if message is got successfully, * returns kStatus_OSA_Timeout if message queue is empty within the specified * 'timeout', returns kStatus_OSA_Error if any errors occur during waiting. * *END**************************************************************************/ osa_status_t OSA_MsgQGet(msg_queue_handler_t handler, void *pMessage, uint32_t timeout) { INT8U err; void* localMsg; int32_t *from_ptr, *to_ptr; uint16_t msg_size = handler->size; if (0U != timeout) { /* If timeout is not 0, convert it to tickes. */ timeout = wait_timeout_msec_to_tick(timeout); localMsg = OSQPend(handler->pQueue, timeout, &err); } else { localMsg = OSQAccept(handler->pQueue, &err); } if (OS_ERR_NONE == err) { /* Copy localMsg to msg */ to_ptr = (int32_t*)(pMessage); from_ptr = (int32_t*)localMsg; while (msg_size--) { *to_ptr++ = *from_ptr++; } OSMemPut(handler->pMem, localMsg); return kStatus_OSA_Success; } else if ((OS_ERR_Q_EMPTY == err) || (OS_ERR_TIMEOUT == err)) { return kStatus_OSA_Timeout; } else { return kStatus_OSA_Error; } }