/*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; } }
void UART1_PrintCh (uint8_t ch) { #if 0 OS_ERR err; CPU_TS ts; OSMutexPend((OS_MUTEX *)&PC_UART_DEVICE, (OS_TICK )0, (OS_OPT )OS_OPT_PEND_BLOCKING, (CPU_TS *)&ts, (OS_ERR *)&err); /* Loop until USARTy DR register is empty */ while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); /* Send one byte from USARTy to USARTz */ USART_SendData(USART1, (uint8_t)ch); OSMutexPost((OS_MUTEX *)&PC_UART_DEVICE, (OS_OPT )OS_OPT_POST_NONE, (OS_ERR *)&err); #else /* Loop until USARTy DR register is empty */ while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); /* Send one byte from USARTy to USARTz */ USART_SendData(USART1, (uint8_t)ch); #endif }
static void appTaskCanReceive(void *pdata) { uint8_t error; canMessage_t msg; /* Install the CAN interrupt handler and start the OS ticker * (must be done in the highest priority task) */ canRxInterrupt(canHandler); osStartTick(); /* * Now execute the main task loop for this task */ while ( true ) { OSSemPend(can1RxSem, 0, &error); msg = can1RxBuf; interfaceLedToggle(D1_LED); OSMutexPend(displayMutex, 0, &error); lcdSetTextPos(2,1); lcdWrite("ID : %08x", msg.id); lcdSetTextPos(2,2); lcdWrite("LEN : %08x", msg.len); lcdSetTextPos(2,3); lcdWrite("DATA_A : %08x", msg.dataA); lcdSetTextPos(2,4); lcdWrite("DATA_B : %08x", msg.dataB); error = OSMutexPost(displayMutex); } }
/****************************************************** TimeGet- sets the value of TimeofDay to ltime to be displyed on the LCD Passes in: nothing Passes out : TimeOfDay *******************************************************/ void TimeGet(TIME *ltime) { INT8U err; OSMutexPend(ClockMutexKey,0,&err); *ltime = TimeOfDay ; OSMutexPost(ClockMutexKey); }
/* ********************************************************************************************************* * Input PWM ********************************************************************************************************* * ********************************************************************************************************* */ void InputTask(void *p_arg) { CPU_TS ts; OS_ERR err; TimerConfig(); SetUpTimer1(); SetUpTimer3(); while (1) { OSTimeDly(15,OS_OPT_TIME_DLY,&err); //Read every 150ms if(TIM3->SR & (1 << 1)) //if status register is true go fetch the Period { Period = TIM3->CCR1; } if(TIM3->SR & (1 << 2)) //if status register is true go fetch the Duty Cycle { DutyCycle = TIM3->CCR2; } //Save la valeur de Commande dans la variable golbale OSMutexPend(&mutTMR3,0,OS_OPT_PEND_BLOCKING,&ts,&err); Command = DutyCycle; OSMutexPost(&mutTMR3,OS_OPT_POST_NONE,&err); } }
void motion_SetDefaultSpeed(float speed) { int i; BOOL found = FALSE; OSMutexPend(mtxDefaultSpeeds, 0, &OSLastError); LOG_TEST_OS_ERROR(OSLastError); defSpeed = speed; //find appropriate speed and set mouse resolution for(i=0; i<CALIB_SPEED_NB; i++) { if(speed == calibSpeed[i]) { encoder_SetResolution(calibMouse[i][0], calibMouse[i][1]); found = TRUE; break; } } motors_ConfigAllIMax(SLIPPAGE_RATIO * MAX(speed, 0.3f)); if(!found) { encoder_SetResolution(DEFAULT_LEFT_TICKS_PER_M, DEFAULT_RIGHT_TICKS_PER_M); } OSMutexPost(mtxDefaultSpeeds); }
void GUI_X_Lock(void) { CPU_TS ts; OS_ERR os_err; OSMutexPend(&xQueueMutex, 0, OS_OPT_PEND_BLOCKING, &ts, &os_err); }
int8_t avgAllArrays(){ INT8U err = OS_NO_ERR; int16_t avgTempData[9]; int i,j; for (i = 0; i < 9; i++) { int16_t sum = 0; for (j = 0; j < VALUE_NUM; j++) { sum += (arrs[i])[j]; } avgTempData[i] = sum/VALUE_NUM; } //get Semaphore for the avg Data OSMutexPend(sensorDataMutex, 0, &err); for(i = 0; i<9 ;i++){ avgData[i] = avgTempData[i]; } //release Semaphore for the avg Data err = OSMutexPost(sensorDataMutex); return err; }
void osAcquireMutex(OsMutex *mutex) { OS_ERR err; //Obtain ownership of the mutex object OSMutexPend(mutex, 0, OS_OPT_PEND_BLOCKING, NULL, &err); }
void task2(task* pdata) { int start = 0; int end; int delay; INT8U err; int arrival = 3; OSTCBCur->Period = pdata->period; OSTCBCur->Exec = pdata->exec; OSTCBCur->ExecTime = pdata->exec; OSTCBCur->Start = 0; OSTCBCur->Deadline = pdata->period + arrival; OSTCBCur->Org_Deadline = pdata->period + arrival; OSTimeDly(arrival); while (1) { printf("\t%d\tTask_2\n",OSTimeGet()); mywait(OSTCBCur->Exec); // CPU time printf("\t%d\tTask_2 get R2\t",OSTimeGet()); OSMutexPend(R2, 0, &err); mywait(3); // R2 printf("\t%d\tTask_2 release R2",OSTimeGet()); OSMutexPost(R2); end = OSTimeGet(); delay = OSTCBCur->Deadline-end; // let it delay 1 timetick to avoid running the while loop OSTCBCur->Resp = end-start; start += pdata->period; OSTCBCur->Deadline += OSTCBCur->Period; OSTCBCur->Org_Deadline = OSTCBCur->Deadline; OSTimeDly(delay); } }
/****************************************************** TimeSet - takes the programmed time of the clock, and * sets it to the TimeofDay Passes in: ltime Passes out: nothing *******************************************************/ void TimeSet(TIME *ltime) { INT8U err; OSMutexPend(ClockMutexKey,0,&err); TimeOfDay = *ltime; OSMutexPost(ClockMutexKey); }
void motion_SetDefaultDecel(float decel) { OSMutexPend(mtxDefaultSpeeds, 0, &OSLastError); LOG_TEST_OS_ERROR(OSLastError); defDecel = decel; OSMutexPost(mtxDefaultSpeeds); }
/*! * @brief Display data on the LCD, safely. * @param[in] Position and data. */ void protected_lcd_clear(void) { OS_ERR err; OSMutexPend(&lcd_mutex, 0, OS_OPT_PEND_BLOCKING, 0, &err); assert(OS_ERR_NONE == err); ClearLCD(); OSMutexPost(&lcd_mutex, OS_OPT_POST_NONE, &err); assert(OS_ERR_NONE == err); }
/*! * @brief Display data on the LCD, safely. * @param[in] Position and data. */ void protected_lcd_display(uint8_t position, const uint8_t *data) { OS_ERR err; OSMutexPend(&lcd_mutex, 0, OS_OPT_PEND_BLOCKING, 0, &err); assert(OS_ERR_NONE == err); DisplayLCD(position, data); OSMutexPost(&lcd_mutex, OS_OPT_POST_NONE, &err); assert(OS_ERR_NONE == err); }
static void AppTaskObj1 (void *p_arg) { OS_ERR os_err; OS_MSG_SIZE msg_size; (void)p_arg; while (DEF_TRUE) { #if (OS_CFG_SEM_EN > 0u) OSSemPend(&AppTaskObjSem, 0, OS_OPT_PEND_BLOCKING, 0, &os_err); #endif #if (OS_CFG_MUTEX_EN > 0u) OSMutexPend(&AppTaskObjMutex, 0, OS_OPT_PEND_BLOCKING, 0, &os_err); OSMutexPost(&AppTaskObjMutex, OS_OPT_POST_NONE, &os_err); #endif #if (OS_CFG_Q_EN > 0u) OSQPend(&AppTaskObjQ, 0, OS_OPT_PEND_BLOCKING, &msg_size, 0, &os_err); #endif #if (OS_CFG_FLAG_EN > 0u) OSFlagPend(&AppTaskObjFlag, DEF_BIT_00, 0, OS_OPT_PEND_FLAG_SET_ALL + OS_OPT_PEND_FLAG_CONSUME + OS_OPT_PEND_BLOCKING, 0, &os_err); #endif OSTimeDlyHMSM( 0u, 0u, 0u, 10u, OS_OPT_TIME_HMSM_STRICT, &os_err); APP_TRACE_INFO(("Object test task 1 running ....\n")); } }
static void appTaskCanMonitor(void *pdata) { uint8_t error; while (true) { OSMutexPend(displayMutex, 0, &error); lcdSetTextPos(2,5); lcdWrite("CAN1GSR: %08x", canStatus(CAN_PORT_1)); error = OSMutexPost(displayMutex); OSTimeDly(20); } }
int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */ _SYNC_t sobj /* Sync object to wait */ ) { int ret; OS_ERR err; OSMutexPend(&sobj, _FS_TIMEOUT, OS_OPT_PEND_BLOCKING, (CPU_TS *)0, &err); /* uC/OS-III */ ret = (int)(err == OS_ERR_NONE); return ret; }
float motion_GetDefaultDecel() { float decel; OSMutexPend(mtxDefaultSpeeds, 0, &OSLastError); LOG_TEST_OS_ERROR(OSLastError); decel = defDecel; OSMutexPost(mtxDefaultSpeeds); return decel; }
float motion_GetDefaultSpeed() { float speed; OSMutexPend(mtxDefaultSpeeds, 0, &OSLastError); LOG_TEST_OS_ERROR(OSLastError); speed = defSpeed; OSMutexPost(mtxDefaultSpeeds); return speed; }
/*! * @brief Initialize the reentrant LCD driver. */ void protected_lcd_init(void) { OS_ERR err; OSMutexCreate(&lcd_mutex, "LCD Mutex", &err); assert(OS_ERR_NONE == err); OSMutexPend(&lcd_mutex, 0, OS_OPT_PEND_BLOCKING, 0, &err); assert(OS_ERR_NONE == err); InitialiseLCD(); OSMutexPost(&lcd_mutex, OS_OPT_POST_NONE, &err); assert(OS_ERR_NONE == err); }
void BSP_OS_RIIC0_MutexPend (void) { OS_ERR err; CPU_TS ts; OSMutexPend(&RIIC0_Mutex, 0u, /* no timeout */ OS_OPT_PEND_BLOCKING, /* block if not available */ &ts, /* timestamp */ &err); }
void Solenoid::lock() { INT8U err = OS_NO_ERR; OSMutexPend(solenoidMutex, 0, &err); if(err != OS_NO_ERR){ printf("Error pending on solenoid mutex\n"); return; } IOWR_ALTERA_AVALON_PIO_DATA(SOLENOID_CONTROLLER_BASE, LOCKED); err = OSMutexPost(solenoidMutex); if(err != OS_NO_ERR){ printf("Error posting to solenoid mutex\n"); } }
/* ********************************************************************************************************* * App_Task_KEY() * * Description : This is an example of an application task. * * Argument(s) : p_arg is the argument passed to 'App_Task_KEY()' by 'OSTaskCreate()'. * * Return(s) : none. * * Caller(s) : This is a task. * * Notes : (1) The first line of code is used to prevent a compiler warning because 'p_arg' is not * used. The compiler should not generate any code for this statement. ********************************************************************************************************* */ static void App_Task_KEY (void *p_arg) { OS_ERR os_err; CPU_TS ts; (void)p_arg; while (DEF_TRUE) { OSTimeDly ((OS_TICK )1, (OS_OPT )OS_OPT_TIME_DLY, (OS_ERR *)&os_err); while (BSP_SW_Read (BSP_SW_1) == DEF_ON) { OSTimeDly(1, OS_OPT_TIME_HMSM_STRICT, &os_err); } //OS_CRITICAL_ENTER(); OSQPost((OS_Q *)&AppQ, /* sends a message to another task using the message queue AppQ. */ (void *)1, /* sends a fixed message of value "1". */ (OS_MSG_SIZE)sizeof(void *), (OS_OPT )OS_OPT_POST_FIFO, (OS_ERR *)&os_err); OSMutexPend((OS_MUTEX *)&AppMutex, /* waits on the mutual exclusion semaphore. */ (OS_TICK )0, (OS_OPT )OS_OPT_PEND_BLOCKING, (CPU_TS *)&ts, (OS_ERR *)&os_err); /* Access shared resource */ OSMutexPost((OS_MUTEX *)&AppMutex, /* must call OSMutexPost() to release the mutex. */ (OS_OPT )OS_OPT_POST_NONE, (OS_ERR *)&os_err); //BSP_LED_Toggle(BSP_LED_RED); //OSTimeDlyResume(&App_Task_LED_TCB, &os_err); while (BSP_SW_Read (BSP_SW_1) == DEF_OFF) { OSTimeDly(1, OS_OPT_TIME_HMSM_STRICT, &os_err); } OSTimeDlyHMSM(0, 0, 0, 50, OS_OPT_TIME_HMSM_STRICT, &os_err); /* Suspend the task execution for 50 milliseconds. */ } }
static void AppTaskObj0 (void *p_arg) { OS_ERR os_err; (void)p_arg; while (DEF_TRUE) { #if (OS_CFG_SEM_EN > 0u) OSSemPost(&AppTaskObjSem, OS_OPT_POST_1, &os_err); #endif #if (OS_CFG_MUTEX_EN > 0u) OSMutexPend(&AppTaskObjMutex, 0, OS_OPT_PEND_BLOCKING, 0, &os_err); OSTimeDlyHMSM( 0u, 0u, 0u, 100u, OS_OPT_TIME_HMSM_STRICT, &os_err); OSMutexPost(&AppTaskObjMutex, OS_OPT_POST_NONE, &os_err); #endif #if (OS_CFG_Q_EN > 0u) OSQPost( &AppTaskObjQ, (void *) 1u, 1u, OS_OPT_POST_FIFO, &os_err); #endif #if (OS_CFG_FLAG_EN > 0u) OSFlagPost(&AppTaskObjFlag, DEF_BIT_00, OS_OPT_POST_FLAG_SET, &os_err); #endif OSTimeDlyHMSM( 0u, 0u, 0u, 10u, OS_OPT_TIME_HMSM_STRICT, &os_err); APP_TRACE_INFO(("Object test task 0 running ....\n")); } }
static void test_task1(void* p_arg) { INT8U err; p_arg = p_arg; while(1) { OSMutexPend(p_mutex, 0, &err); printk("consume "); OSTimeDly(5); } }
/* ********************************************************************************************************* * ReportAdcSample ********************************************************************************************************* * Arguments: * uChannel: ADC channel sepecification. * pstrADControlBlock: pointer to the channel control block. ********************************************************************************************************* */ static void ReportAdcSample(CPU_INT08U uChannel, ADC_CONTROL_BLOCK_t* pstrADControlBlock) { OS_ERR err; CPU_TS ts; OSMutexPend(&mutADC,0,OS_OPT_PEND_BLOCKING,&ts,&err); strSemADC.uValue = ADC1->JDR1; OSMutexPost(&mutADC,OS_OPT_POST_NONE,&err); OSSemPost(&semADC_Complete,OS_OPT_POST_NONE,&err); }
/*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; } }
void Task_B(void *p_arg) { OS_ERR err; CPU_TS ts; while(DEF_TRUE) { OSMutexPend(&SerialMutex, 0, OS_OPT_PEND_BLOCKING, &ts, &err); BSP_LCD_SetTextColor(LCD_COLOR_RED); BSP_LCD_DisplayStringAtLine(5, (uint8_t *)"Task A is waiting"); BSP_LCD_DisplayStringAtLine(6, (uint8_t *)"Task B is runing "); OSTimeDly(400, OS_OPT_TIME_DLY, &err); OSMutexPost(&SerialMutex, OS_OPT_POST_NONE, &err); } }
INT8U getSensorData(int16_t* avgSensorData, uint32_t* deltaTime){ INT8U err = OS_NO_ERR; int i = 0; OSMutexPend(sensorDataMutex, 0, &err);//Acquire Mutex for the avg Data for(i = 0;i <9 ;i++){ avgSensorData[i] = avgData[i]; } *deltaTime = averagedDataDeltaT; SDM_NEW_DATA_AVAILABLE = 0; err = OSMutexPost(sensorDataMutex);//release Semaphore for the avg Data return err; }
//浮点测试任务 void float_task(void *p_arg) { OS_ERR err; //CPU_SR_ALLOC(); while(1) { OSTimeDlyHMSM(0,0,0,400,OS_OPT_TIME_HMSM_STRICT,&err); //延时400ms printf("float task wait Mutex Sem . \n\r"); // 请求信号量 OSMutexPend(&Mutex_Sem,0,OS_OPT_PEND_BLOCKING,0,&err); //OSSemPend(&SYNC_SEM,0,OS_OPT_PEND_BLOCKING,0,&err); //请求信号量 printf("float task get Mutex Sem \n\r"); OSMutexPost(&Mutex_Sem,OS_OPT_POST_NO_SCHED,&err); OSTimeDlyHMSM(0,0,0,100,OS_OPT_TIME_HMSM_STRICT,&err); //延时100ms } }