s32 bsp_close(u32 fd) { s32 ret; struct bsp_rfile_close_req stReq; osSemaphoreWait(g_stRfileMain.semAll, osWaitForever); stReq.opType = EN_RFILE_OP_CLOSE; stReq.pstlist = NULL; stReq.fd = fd; ret = bsp_icc_send(ICC_CPU_APP, RFILE_MCORE_ICC_WR_CHAN, &stReq, sizeof(struct bsp_rfile_close_req)); if (sizeof(struct bsp_rfile_close_req) != ret) { bsp_trace(BSP_LOG_LEVEL_DEBUG, BSP_MODU_RFILE, "%d.\n", __LINE__); g_stRfileMain.ret = -1; } else { osSemaphoreWait(g_stRfileMain.semReq, osWaitForever); } osSemaphoreRelease(g_stRfileMain.semAll); return g_stRfileMain.ret; }
bool_t osWaitForEvent(OsEvent *event, systime_t timeout) { int32_t ret; //Wait until the specified event is in the signaled //state or the timeout interval elapses if(timeout == INFINITE_DELAY) { //Infinite timeout period ret = osSemaphoreWait(event->id, osWaitForever); } else { #if defined(osCMSIS_RTX) systime_t n; //Loop until the assigned time period has elapsed do { //Limit the timeout value n = MIN(timeout, 10000); //Wait for the specified time interval ret = osSemaphoreWait(event->id, n); //Decrement timeout value timeout -= n; //Check timeout value } while(ret == 0 && timeout > 0); #else //Wait for the specified time interval ret = osSemaphoreWait(event->id, timeout); #endif } #if defined(osCMSIS_RTX) //Check return value if(ret > 0) { //Force the event back to the nonsignaled state while(osSemaphoreWait(event->id, 0) > 0); //The specified event is in the signaled state return TRUE; } else { //The timeout interval elapsed return FALSE; } #else //Check return value if(ret == osOK) return TRUE; else return FALSE; #endif }
void Start_input_buttonTask(void const * argument) { GPIO_InitTypeDef GPIO_InitStruct; fsm_event_f button_fsm_event; /* Take both semaphores for the first time */ osSemaphoreWait(sem_input_button_short_pressHandle, osWaitForever); osSemaphoreWait(sem_input_button_long_pressHandle, osWaitForever); /* Infinite loop */ for(;;) { /* If I/O button is pressed */ if (osSemaphoreWait(sem_input_button_short_pressHandle, osWaitForever) == osOK) { /* Make falling edge sensitive */ GPIO_InitStruct.Pin = WAKEUP_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(WAKEUP_GPIO_Port, &GPIO_InitStruct); // TEST osSemaphoreRelease(sem_ecg_keygenHandle); /* Wait for falling edge */ if (osSemaphoreWait(sem_input_button_long_pressHandle, 2000) == osErrorOS) { /* If falling edge is NOT detected before timeout * we have a long press */ button_fsm_event = fsm_button_long; while(osMailPut(queue_fsm_eventsHandle, (void *) &button_fsm_event) != osOK) { osDelay(1); } } else { /* Make rising edge sensitive again */ GPIO_InitStruct.Pin = WAKEUP_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(WAKEUP_GPIO_Port, &GPIO_InitStruct); /* If falling edge is detected before timeout * we have a short press */ button_fsm_event = fsm_button_short; while(osMailPut(queue_fsm_eventsHandle, (void *) &button_fsm_event) != osOK) { osDelay(1); } } } } }
void osResetEvent(OsEvent *event) { #if defined(osCMSIS_RTX) //Force the specified event to the nonsignaled state while(osSemaphoreWait(event->id, 0) > 0); #else //Force the specified event to the nonsignaled state osSemaphoreWait(event->id, 0); #endif }
s32 bsp_open(const s8 *path, s32 flags, s32 mode) { s32 ret; u32 ulNameLen, ulSize; struct bsp_rfile_open_req *pstReq; osSemaphoreWait(g_stRfileMain.semAll, osWaitForever); if(!path) { return BSP_ERROR; } ulNameLen = strlen(path) + 1; ulSize = sizeof(struct bsp_rfile_open_req) + ulNameLen; if(ulSize > RFILE_LEN_MAX) { return BSP_ERROR; } pstReq = Rfile_Malloc(ulSize); if(!pstReq) { return BSP_ERROR; } pstReq->opType = EN_RFILE_OP_OPEN; pstReq->pstlist = NULL; pstReq->nameLen = ulNameLen; pstReq->mode = mode; pstReq->flags = flags; memcpy(pstReq->aucData, path, ulNameLen); ret = bsp_icc_send(ICC_CPU_APP, RFILE_MCORE_ICC_WR_CHAN, pstReq, ulSize); if (ulSize != ret) { bsp_trace(BSP_LOG_LEVEL_DEBUG, BSP_MODU_RFILE, "%d.\n", __LINE__); g_stRfileMain.ret = -1; } else { osSemaphoreWait(g_stRfileMain.semReq, osWaitForever); } Rfile_Free(pstReq); osSemaphoreRelease(g_stRfileMain.semAll); return g_stRfileMain.ret; }
/*! @brief Program entry point */ int main (void) { Mode currentMode; mainThread_ID = osThreadGetId(); // start threads. Low priority ensures main will finish osThreadDef (tM_run, osPriorityBelowNormal, 1, 0); tempThread_ID = osThreadCreate( osThread (tM_run), NULL); osThreadDef (aM_run, osPriorityBelowNormal, 1, 0); accThread_ID = osThreadCreate( osThread (aM_run), NULL); LED_init(); buttonInit(); tM_init(); aM_init(); // lock both mode semaphores, they are both in "used" mode osSemaphoreWait( tempSema, osWaitForever); osSemaphoreWait( accSema, osWaitForever); // start in TEMP_MODE currentMode = TEMP_MODE; osSemaphoreRelease( tempSema); TIM3_Init(ACC_FREQ); while(1) { //will start and wait for a tap before anything happends //it will run the threads though osSignalWait(0x1, osWaitForever); // once we're past here, it means a tap has been detected! switch( currentMode) { case TEMP_MODE: // we wait until the semaphore becomes available osSemaphoreWait( tempSema, osWaitForever); osDelay (500); acc_clearLatch(); osSemaphoreRelease(accSema); currentMode = ACC_MODE; break; case ACC_MODE: osSemaphoreWait( accSema, osWaitForever); osDelay (500); acc_clearLatch(); osSemaphoreRelease(tempSema); currentMode = TEMP_MODE; break; } } }
s32 bsp_write(u32 fd, const s8 *ptr, u32 size) { s32 ret = -1; u32 ulCur, ulLen; osSemaphoreWait(g_stRfileMain.semAll, osWaitForever); if(!ptr) { return BSP_ERROR; } /* 如果长度超过ICC最大长度限制,需要拆分传送 */ ulCur = 0; while(ulCur < size) { ulLen = ((size - ulCur) > RFILE_WR_LEN_MAX) ? RFILE_WR_LEN_MAX : (size - ulCur); ret = rfile_write(fd, ((u8*)ptr+ulCur), ulLen); ulCur += ret; if(ret != ulLen) { break; } } osSemaphoreRelease(g_stRfileMain.semAll); return ulCur; }
/** * @brief Semaphore Thread 2 function * @param argument: shared semaphore * @retval None */ static void SemaphoreThread2 (void const *argument) { uint32_t count = 0; osSemaphoreId semaphore = (osSemaphoreId) argument; for(;;) { if (semaphore != NULL) { /* Try to obtain the semaphore. */ if(osSemaphoreWait(semaphore , 0) == osOK) { /* Resume Thread 1 (higher priority)*/ osThreadResume(SemThread1Handle); count = osKernelSysTick() + 5000; /* Toggle LED3 every 200 ms for 5 seconds*/ while (count >= osKernelSysTick()) { BSP_LED_Toggle(LED3); osDelay(200); } /* Turn off LED3 */ BSP_LED_Off(LED3); /* Release the semaphore to unblock Thread 1 (higher priority) */ osSemaphoreRelease(semaphore); } } } }
bool_t osCreateEvent(OsEvent *event) { osSemaphoreDef_t semaphoreDef; #if defined(osCMSIS_RTX) semaphoreDef.semaphore = event->cb; #else semaphoreDef.dummy = 0; #endif //Create a binary semaphore object event->id = osSemaphoreCreate(&semaphoreDef, 1); //Check whether the returned semaphore ID is valid if(event->id != NULL) { //Force the specified event to the nonsignaled state osSemaphoreWait(event->id, 0); //Event successfully created return TRUE; } else { //Failed to create event object return FALSE; } }
/** * @brief Semaphore Thread 1 function * @param argument: shared semaphore * @retval None */ static void SemaphoreThread1 (void const *argument) { uint32_t count = 0; osSemaphoreId semaphore = (osSemaphoreId) argument; for(;;) { if (semaphore != NULL) { /* Try to obtain the semaphore. */ if(osSemaphoreWait(semaphore , 100) == osOK) { count = osKernelSysTick() + 5000; while (count >= osKernelSysTick()) { /* Toggle LED1 */ BSP_LED_Toggle(LED1); /* Delay 200 ms */ osDelay(200); } /* Turn off LED1 */ BSP_LED_Off(LED1); /* Release the semaphore */ osSemaphoreRelease(semaphore); /* Suspend ourseleves to execute thread 2 (lower priority) */ osThreadSuspend(NULL); } } } }
int main(void) { /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Initialize all configured peripherals */ MX_GPIO_Init(); /* Create the threads and semaphore */ osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128); defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL); osThreadDef(blinkTask, BlinkTask, osPriorityNormal, 0, 128); blinkTaskHandle = osThreadCreate(osThread(blinkTask), NULL); osSemaphoreDef(sem); semHandle = osSemaphoreCreate(osSemaphore(sem), 1); osSemaphoreWait(semHandle, osWaitForever); /* Start scheduler */ osKernelStart(); /* We should never get here as control is now taken by the scheduler */ /* Infinite loop */ while (1); }
void getWirelessAngles(int8_t* angles) { osSemaphoreWait(wirelessAccId, osWaitForever); angles[0] = wirelessAngles[0]; angles[1] = wirelessAngles[1]; osSemaphoreRelease(wirelessAccId); }
CAN_ERROR CAN_hw_tx_empty (U32 ctrl) { HAL_CAN_StateTypeDef state; CAN_HandleTypeDef *hCAN; int av = osSemaphoreWait(wr_sem[ctrl-1], 0); if (av >0) { switch (ctrl) { #if RTE_CAN1 == 1 case __CTRL1: hCAN = &hCAN1; break; #endif #if RTE_CAN2 == 1 case __CTRL2: hCAN= &hCan2; break; #endif default: return CAN_UNEXIST_CTRL_ERROR; } state = HAL_CAN_GetState(hCAN); if((state == HAL_CAN_STATE_READY) || (state == HAL_CAN_STATE_BUSY_RX)) return CAN_OK; else osSemaphoreRelease(wr_sem[ctrl-1]); /* Return a token back to semaphore */ } return CAN_TX_BUSY_ERROR; }
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout) { int32_t ret; //Wait until the semaphore is available or the timeout interval elapses if(timeout == INFINITE_DELAY) { //Infinite timeout period ret = osSemaphoreWait(semaphore->id, osWaitForever); } else { #if defined(osCMSIS_RTX) systime_t n; //Loop until the assigned time period has elapsed do { //Limit the timeout value n = MIN(timeout, 10000); //Wait for the specified time interval ret = osSemaphoreWait(semaphore->id, n); //Decrement timeout value timeout -= n; //Check timeout value } while(ret == 0 && timeout > 0); #else //Wait for the specified time interval ret = osSemaphoreWait(semaphore->id, timeout); #endif } #if defined(osCMSIS_RTX) //Check return value if(ret > 0) return TRUE; else return FALSE; #else //Check return value if(ret == osOK) return TRUE; else return FALSE; #endif }
void test_thread(void const *name) { while (true) { osSemaphoreWait(two_slots, osWaitForever); printf("%s\n\r", (const char*)name); osDelay(1000); osSemaphoreRelease(two_slots); } }
/*---------------------------------------------------------------------------* * Routine: sys_arch_sem_wait *---------------------------------------------------------------------------* * Description: * Blocks the thread while waiting for the semaphore to be * signaled. If the "timeout" argument is non-zero, the thread should * only be blocked for the specified time (measured in * milliseconds). * * If the timeout argument is non-zero, the return value is the number of * milliseconds spent waiting for the semaphore to be signaled. If the * semaphore wasn't signaled within the specified time, the return value is * SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore * (i.e., it was already signaled), the function may return zero. * * Notice that lwIP implements a function with a similar name, * sys_sem_wait(), that uses the sys_arch_sem_wait() function. * Inputs: * sys_sem_t sem -- Semaphore to wait on * u32_t timeout -- Number of milliseconds until timeout * Outputs: * u32_t -- Time elapsed or SYS_ARCH_TIMEOUT. *---------------------------------------------------------------------------*/ u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) { u32_t start = us_ticker_read(); if (osSemaphoreWait(sem->id, (timeout != 0)?(timeout):(osWaitForever)) < 1) return SYS_ARCH_TIMEOUT; return (us_ticker_read() - start) / 1000; }
/*----------------------------------------------------------------------------- Routine to Move the Balls *----------------------------------------------------------------------------*/ void S_MoveBall (bool newBall){ uint8_t i; uint8_t j; if (newBall & (actualBallNumber < maxNumberBalls)){ ballArray[actualBallNumber][0] = plane_x+9; ballArray[actualBallNumber][1] = plane_y+5; actualBallNumber++; } for (i = 0; i < actualBallNumber; i++){ osSemaphoreWait (glcd_semaph_id, osWaitForever); GLCD_SetForegroundColor (GLCD_COLOR_BLACK); GLCD_DrawPixel (ballArray[i][0], ballArray[i][1]); osSemaphoreRelease(glcd_semaph_id); ballArray[i][0]+=2; // If the new position exceed the limits, it will be erased from the array and it wont be drawn if(ballArray[i][0] > GLCD_WIDTH ){ actualBallNumber--; ballArray[i][0] = ballArray[actualBallNumber][0]; ballArray[i][1] = ballArray[actualBallNumber][1]; } else { osSemaphoreWait (glcd_semaph_id, osWaitForever); GLCD_SetForegroundColor (GLCD_COLOR_RED); GLCD_DrawPixel (ballArray[i][0]+2, ballArray[i][1]); osSemaphoreRelease(glcd_semaph_id); } // Object is deleted if hit by a ball osMutexWait(objectVar_mutex_id, osWaitForever); for (j = 0; j < objectNumb; j++){ if ( (ballArray[i][0] > objectArray[j][0]) & (ballArray[i][0] < objectArray[j][0]+9) & (ballArray[i][1] > objectArray[j][1]) & (ballArray[i][1] < objectArray[j][1]+9) ){ osSemaphoreWait (glcd_semaph_id, osWaitForever); GLCD_SetForegroundColor (GLCD_COLOR_BLACK); S_DrawStar (objectArray[j][0], objectArray[j][1]); S_IncPlayerScore(); osSemaphoreRelease(glcd_semaph_id); objectNumb--; objectArray[j][0] = objectArray[objectNumb][0]; objectArray[j][1] = objectArray[objectNumb][1]; } } osMutexRelease(objectVar_mutex_id); } }
void BlinkTask(void const *argument) { if(osSemaphoreWait(semHandle, osWaitForever) == osOK) { while(1) { HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); osDelay(500); } } }
/** *@brief A function that gets the information recieved over wireless to be processed *@param[inout] rxBuffer is the buffer to which the data is copied to *@param[in] bufferSize The size of the passed in buffer *@warning bufferSize must be of the same size as WIRELESS_BUFFER_SIZE, see wireless.h for details *@retval None */ void getRecieved(uint8_t* rxBuffer, uint8_t bufferSize){ uint8_t i = 0; for( i =0; i< bufferSize; i++){ osSemaphoreWait(rxId, osWaitForever); rxBuffer[i] = rxWireless[i]; //Critical access portion osSemaphoreRelease(rxId); } }
static void demo_task1(void *arg) { int count = 0; while (1) { osSemaphoreWait(pSem, 0xffffffff); printf("demo_task1 get sem %d\n", count++); }; }
CAN001_MessageHandleType* myCAN1_Get() { CAN001_MessageHandleType* pmsg; if(can1rx.SendPos==can1rx.SendPos) osSemaphoreWait(can1rx_semaphore_id,osWaitForever); pmsg=&can1rx.buf[can1rx.SendPos]; can1rx.SendPos++; can1rx.SendPos&=(MAXLINE-1); return pmsg; }
/** *@brief A function that safely access's the other board's accelerometer readings *@param[inout] accValues A pointer to the new location in memory that data is copied to *@retval None */ void getWirelessACCValues(float* accValues) { int i = 0; for(i = 0; i < 3; i++){ osSemaphoreWait(wirelessAccId, osWaitForever); accValues[i] = wirelessAccValues[i]; //Critical access portion osSemaphoreRelease(wirelessAccId); } }
/*---------------------------------------------------------------------------- * Thread 2 - Normal Priority - looks for a free semaphore and uses * the resource whenever it is available *---------------------------------------------------------------------------*/ void thread2 (void const *argument) { while (1) { /* Wait indefinetly for a free semaphore */ osSemaphoreWait (semaphore, osWaitForever); /* OK, the serial interface is free now, use it. */ printf ("Thread 2\n"); /* Return a token back to a semaphore. */ osSemaphoreRelease (semaphore); } }
/** *@brief A function that safely access's the corrected values of the accelerometer *@param[inout] accValues A pointer to the new location in memory that data is copied to *@retval None */ void getACCValues(float* accValues) { int i = 0; for(i = 0; i < 3; i++){ osSemaphoreWait(accId, osWaitForever); accValues[i] = accCorrectedValues[i]; //Critical access portion osSemaphoreRelease(accId); } }
/*! @brief Thread to display temperature @param argument Unused */ void temperature_DispFlag_thread(void const * argument) { // Extracts the semaphore object from the argument. // This semaphore object was created in the main osSemaphoreId* semaphore; semaphore = (osSemaphoreId*)argument; uint8_t ResourceLocked = 1; // Flag gets turned on if semaphore_lock achieved uint32_t tokentemp; // Return of semwait // Wait for the semaphore with 1ms timeout osEvent event = osSignalWait(SIGNAL_DISP_TEMPERATURE, 1); while(1) { tokentemp=osSemaphoreWait (*semaphore, osWaitForever); ResourceLocked=1; // turn flag on if resource_locked while (ResourceLocked) { // wait for a signal with 1ms timeout event = osSignalWait(SIGNAL_DISP_TEMPERATURE, 1); if (event.status == osEventTimeout) { // in case of timeout display LCD_DisplayTemperature(temp); } else { // signal received which is not timeout // clear the signal flag for the tilt display thread osSignalClear (tilt_TurnDispFlag_thread, SIGNAL_DISP_TILT); //clear the screen LCD_clear_display(); // turn the resource_locked flag off ResourceLocked = 0; tokentemp=0; // turn the flag to display the string "temperature" once GLB_Temperature_display_flag=1; // release the semaphore osSemaphoreRelease(*semaphore); //wait 5ms for the other thread to grab the semaphore osDelay(5); } } } }
void S_Thread_MovePlane (void const *arg){ uint16_t plane_x_last = 1; uint16_t plane_y_last = GLCD_HEIGHT/2; uint16_t plane_x_i; uint16_t plane_y_i; uint8_t i; while(!stopThreads){ osMutexWait(planeVar_mutex_id, osWaitForever); plane_x_i = plane_x; plane_y_i = plane_y; osMutexRelease(planeVar_mutex_id); if( (plane_x_last != plane_x_i) || (plane_y_last != plane_y_i) ){ osSemaphoreWait (glcd_semaph_id, osWaitForever); S_DrawPlane(plane_x_last, plane_y_last, true); // Delete last position plane S_DrawPlane(plane_x_i, plane_y_i, false); // Draw new position plane osSemaphoreRelease(glcd_semaph_id); plane_x_last = plane_x_i; plane_y_last = plane_y_i; } osMutexWait(objectVar_mutex_id, osWaitForever); for (i = 0; i < objectNumb; i++){ // If the square around the plane intersects with the square around the object: the plane is destroyed if ( ( (objectArray[i][0]>(plane_x_i-9)) & (objectArray[i][0]<(plane_x_i+9)) ) & ( (objectArray[i][1]>(plane_y_i-9)) & (objectArray[i][1]<(plane_y_i+9)) ) ){ osSemaphoreWait (glcd_semaph_id, osWaitForever); GLCD_SetForegroundColor (GLCD_COLOR_YELLOW); S_DrawExplosion(plane_x_i, plane_y_i); S_IncMachineScore(); osSemaphoreRelease(glcd_semaph_id); } } osMutexRelease(objectVar_mutex_id); osDelay(50); } }
/** * @brief Thread function of LCD * @param Not used * @retval None */ void lcd_thread(void const * argument) { while (1) { osSemaphoreWait (lcd_semaphore, osWaitForever); if (getThreadToRun() == MODE_1) { //Accelerometer mode lcd_set_display_angles(getFilteredRollAngle(),getFilteredPitchAngle()); lcd_display_string(line1, line2); } } }
int ff_req_grant ( /* TRUE:Got a grant to access the volume, FALSE:Could not get a grant */ _SYNC_t sobj /* Sync object to wait */ ) { int ret = 0; if(osSemaphoreWait(sobj, _FS_TIMEOUT) == osOK) { ret = 1; } return ret; }
/** * @brief Semaphore Test. * @param argument: Not used * @retval None */ static void SemaphoreTest(void const *argument) { for(;;) { if (osSemaphore != NULL) { /* Try to obtain the semaphore */ if(osSemaphoreWait(osSemaphore , 0) == osOK) { BSP_LED_Toggle(LED1); } } } }
void SPI_DMA_xfer(uint8_t* write_buff, uint8_t* read_buff, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, int numByte){ // sema will be released in interrupt osSemaphoreWait(dmaSema_ID, osWaitForever); //set chip select low GPIO_ResetBits(GPIOx, GPIO_Pin); // wait for slave //while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) != Bit_RESET); DMA2_Stream0->M0AR = (uint32_t)(&read_buff[0]); DMA2_Stream3->M0AR = (uint32_t)(&write_buff[0]); DMA2_Stream0->NDTR = numByte; DMA2_Stream3->NDTR = numByte; osDelay(10); DMA2_Stream0->CR |= DMA_SxCR_EN; DMA2_Stream3->CR |= DMA_SxCR_EN; osSemaphoreWait(dmaComplete_ID, osWaitForever); GPIO_SetBits(GPIOx, GPIO_Pin); osSemaphoreRelease(dmaSema_ID); }