/** \brief Test case: TC_MutexTimeout \details - Create and initialize a mutex object - Create a thread that acquires a mutex but never release it - Wait for mutex release until timeout */ void TC_MutexTimeout (void) { osThreadId ctrl_id, lock_id; osEvent evt; /* Get control thread id */ ctrl_id = osThreadGetId (); ASSERT_TRUE (ctrl_id != NULL); if (ctrl_id != NULL) { /* - Create and initialize a mutex object */ G_MutexId = osMutexCreate (osMutex (MutexTout)); ASSERT_TRUE (G_MutexId != NULL); if (G_MutexId != NULL) { /* - Create a thread that acquires a mutex but never release it */ lock_id = osThreadCreate (osThread (Th_MutexLock), &ctrl_id); ASSERT_TRUE (lock_id != NULL); if (lock_id != NULL) { /* - Wait for mutex release until timeout */ ASSERT_TRUE (osMutexWait (G_MutexId, 10) == osErrorTimeoutResource); /* - Release a mutex */ osSignalSet (lock_id, 0x01); evt = osSignalWait (0x01, 100); ASSERT_TRUE (evt.status == osEventSignal); /* - Terminate locking thread */ ASSERT_TRUE (osThreadTerminate (lock_id) == osOK); } /* Delete mutex object */ ASSERT_TRUE (osMutexDelete (G_MutexId) == osOK); } } }
/** \brief Test case: TC_MutexOwnership \details - Create a mutex object - Create a child thread and wait until acquires mutex - Create a child thread that trys to release a mutex - Only thread that obtains a mutex can release it. */ void TC_MutexOwnership (void) { osThreadId ctrl_id, id[2]; /* Ensure that priority of the control thread is set to normal */ ctrl_id = osThreadGetId(); ASSERT_TRUE (osThreadSetPriority (ctrl_id, osPriorityNormal) == osOK); /* - Create a mutex object */ G_MutexId = osMutexCreate (osMutex (Mutex_Ownership)); ASSERT_TRUE (G_MutexId != NULL); if (G_MutexId != NULL) { /* - Create a child thread and wait until acquires mutex */ id[0] = osThreadCreate (osThread (Th_MutexAcqLow), NULL); ASSERT_TRUE (id[0] != NULL); if (id[0] != NULL) { osDelay(10); /* - Create a child thread that trys to release a mutex */ id[1] = osThreadCreate (osThread (Th_MutexRelHigh), NULL); ASSERT_TRUE (id[1] != NULL); /* Terminate both threads */ if (id[1] != NULL) { ASSERT_TRUE (osThreadTerminate (id[1]) == osOK); } ASSERT_TRUE (osThreadTerminate (id[0]) == osOK); } } /* - Delete a mutex object */ ASSERT_TRUE (osMutexDelete (G_MutexId) == osOK); }
void *__user_perthread_libspace (void) { osThreadId_t id; uint32_t n; if (!os_kernel_is_active()) { return (void *)&os_libspace[OS_THREAD_LIBSPACE_NUM][0]; } id = osThreadGetId(); for (n = 0U; n < OS_THREAD_LIBSPACE_NUM; n++) { if (os_libspace_id[n] == NULL) { os_libspace_id[n] = id; return (void *)&os_libspace[n][0]; } if (os_libspace_id[n] == id) { return (void *)&os_libspace[n][0]; } } if (n == OS_THREAD_LIBSPACE_NUM) { osRtxErrorNotify(osRtxErrorClibSpace, id); } return (void *)&os_libspace[n][0]; }
// Provide libspace for current thread void *__user_perthread_libspace(void) { osThreadId_t id; uint32_t n; if (os_kernel_is_active() != 0U) { id = osThreadGetId(); for (n = 0U; n < (uint32_t)OS_THREAD_LIBSPACE_NUM; n++) { if (os_libspace_id[n] == NULL) { os_libspace_id[n] = id; } if (os_libspace_id[n] == id) { break; } } if (n == (uint32_t)OS_THREAD_LIBSPACE_NUM) { (void)osRtxErrorNotify(osRtxErrorClibSpace, id); } } else { n = OS_THREAD_LIBSPACE_NUM; } //lint -e{9087} "cast between pointers to different object types" return (void *)&os_libspace[n][0]; }
/*---------------------------------------------------------------------------- * Main: Initialise and start RTX Kernel *---------------------------------------------------------------------------*/ int main(void) { // Get main thread ID mainFunctionId = osThreadGetId(); // Create thread threadFunctionId = osThreadCreate(osThread(threadFunction), NULL); if (threadFunctionId == NULL) { __asm__("bkpt"); } // Need suitable features for the stdio & leds // printf("Main thread ID = %d\n", (int)mainFunctionId); // printf("Thread ID = %d\n", (int)threadFunctionId); // led_initialise(); for (;;) { // Wait for signal from thread osSignalWait(SIGNAL_MASK, osWaitForever); // printf("Thread signaled\n"); // greenLedToggle(); } }
/// Get current priority of an active thread. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId. /// \return current priority value of the thread function. /// \note MUST REMAIN UNCHANGED: \b osThreadGetPriority shall be consistent in every CMSIS-RTOS. osPriority osThreadGetPriority (osThreadId thread_id) { if (thread_id == 0) thread_id = osThreadGetId(); return (osPriority)((int)thread_id->prio + (int)osPriorityIdle); }
int main(void) { uint32_t currentSecond, lastSecond; currentSecond = lastSecond = 0; /* Configure the HW */ boardInit(); #if defined CFG_CMSIS_RTOS tid_blinkthread = osThreadCreate(osThread(blink_thread), NULL); tid_mainthread = osThreadGetId(); for (;;) { osDelay(1000); boardLED((currentSecond++) & 1); } #endif while (1) { currentSecond = delayGetSecondsActive(); if (currentSecond != lastSecond) { lastSecond = currentSecond; /* Blinky */ boardLED(lastSecond % 2); } /* Poll for CLI input if CFG_INTERFACE is enabled */ #ifdef CFG_INTERFACE cliPoll(); #endif } }
/*********** Invoke Forground Command *********************/ static void command_invoke(void const *args) { void (*func)(void const * ) ; int i,iteration ; func = (void(*)(void const *))((func_args *)args)->argv[0] ; #if defined(HAVE_KEIL_RTX) wc_LockMutex((wolfSSL_Mutex *)&command_mutex) ; #endif iteration = for_iteration ; for(i=0; i< iteration; i++) { if(iteration > 1) printf("--- Start for %d ---->\n", i) ; #if defined(HAVE_KEIL_RTX) && !defined(WOLFSSL_CMSIS_RTOS) stack_fill(command_stack, COMMAND_STACK_SIZE) ; #endif func(args) ; /* invoke command */ #if defined(HAVE_KEIL_RTX)&& !defined(WOLFSSL_CMSIS_RTOS) stack_check(command_stack, COMMAND_STACK_SIZE) ; #endif } if(iteration > 1) for_iteration = 1 ; osDelay(20000) ; #ifdef HAVE_KEIL_RTX wc_UnLockMutex((wolfSSL_Mutex *)&command_mutex) ; #ifdef WOLFSSL_CMSIS_RTOS osThreadTerminate(osThreadGetId()) ; #else os_tsk_delete_self() ; #endif #endif }
void eventOS_scheduler_mutex_wait(void) { osMutexAcquire(event_mutex_id, osWaitForever); if (0 == owner_count) { event_mutex_owner_id = osThreadGetId(); } owner_count++; }
void exit_command(void) { printf("Command Aborted\n") ; #ifdef WOLFSSL_CMSIS_RTOS osThreadTerminate(osThreadGetId()) ; #else os_tsk_delete_self() ; #endif }
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == GPIO_PIN_0 && osThreadGetId() != button_task_handle) { // let the button_thread to determine if this is a long press or not xSemaphoreGiveFromISR(raw_button_sem, NULL); } }
int os_usart1_gets(char *pBuffer){ osMutexWait(usart1_rx_mutex_id, osWaitForever); usart1_rx_owner = osThreadGetId(); osSignalClear(usart1_rx_owner, USART_SIGNAL); usart1_async_gets(pBuffer, _os_usart1_rx_callback); osSignalWait(USART_SIGNAL, osWaitForever); osMutexRelease(usart1_rx_mutex_id); return usart1_rx_nBytesRead; }
/// Terminate execution of a thread and remove it from Active Threads. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId. /// \return status code that indicates the execution status of the function. /// \note MUST REMAIN UNCHANGED: \b osThreadTerminate shall be consistent in every CMSIS-RTOS. osStatus osThreadTerminate (osThreadId thread_id) { if (port_isr_inside()) return osErrorISR; if (thread_id == 0) thread_id = osThreadGetId(); tsk_kill(thread_id); return osOK; }
static void antenna_thread(void const *params) { dbg_add_thread(osThreadGetId(), "antenna"); dbg_printf("Thread started"); NICHROME_On(NICHROME_Burner_1); osDelay(5000); NICHROME_Off(NICHROME_Burner_1); //NICHROME_On(NICHROME_Burner_2); //osDelay(5000); //NICHROME_Off(NICHROME_Burner_2); }
int32_t osSignalClear (osThreadId thread_id, int32_t signals) { uint32_t flags; if (thread_id != osThreadGetId()) { return ((int32_t)0x80000000U); } flags = osThreadFlagsClear((uint32_t)signals); if ((flags & 0x80000000U) != 0U) { return ((int32_t)0x80000000U); } return ((int32_t)flags); }
/*! @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; } } }
/** * @brief Thread termination. * @note The thread is not really terminated but asked to terminate which * is not compliant. */ osStatus osThreadTerminate(osThreadId thread_id) { if (thread_id == osThreadGetId()) { /* Note, no memory will be recovered unless a cleaner thread is implemented using the registry.*/ chThdExit(0); } chThdTerminate(thread_id); chThdWait((thread_t *)thread_id); return osOK; }
/*---------------------------------------------------------------------------- * Main Thread *---------------------------------------------------------------------------*/ int main (void) { /* program execution starts here */ /* Set higher priority of main thread to preempt job2 */ osThreadSetPriority(osThreadGetId(), osPriorityAboveNormal); thread1_id = osThreadCreate(osThread(job1),NULL); /* create thread1 */ thread2_id = osThreadCreate(osThread(job2),NULL); /* create thread2 */ thread3_id = osThreadCreate(osThread(job3),NULL); /* create thread3 */ while (1) { /* endless loop */ counter++; /* increment counter */ osDelay(5); /* wait for timeout: 5ms */ } }
/* * Main Thread */ int main (void) { SER_Init(); // Initialize Serial Interface printf("Example: CppSamp - Operator overloading / friend functions.\r\n" "===========================================================\r\n\r\n"); /* Get main thread ID */ main_id = osThreadGetId(); /* Create thread X */ threadX_id = osThreadCreate(osThread(threadX), NULL); /* Indicate to thread X completion of do-this */ osSignalSet(threadX_id, 0x0001); /* Wait for completion of do-that */ osSignalWait(0x0001, osWaitForever); Complex c1(1,2); Complex c2(2); Complex c3; c1.SetKey(1000); printf("Complex number c1: "); c1.Print(); printf("Complex number c2: "); c2.Print(); printf("Calculate c1 + c2: "); c3 = c1 + c2; c3.Print(); printf("Calculate c2 - c1: "); c3 = c2 - c1; c3.Print(); printf("Negate previous : "); c3 = -c3; c3.Print(); c3.Print(10); printf("\nEnd of Program.\r\n"); while (1); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* This project template calls firstly two functions in order to configure MPU feature and to enable the CPU Cache, respectively MPU_Config() and CPU_CACHE_Enable(). These functions are provided as template implementation that User may integrate in his application, to enhance the performance in case of use of AXI interface with several masters. */ /* Configure the MPU attributes as Write Through */ MPU_Config(); /* Enable the CPU Cache */ CPU_CACHE_Enable(); #ifdef RTE_CMSIS_RTOS // when using CMSIS RTOS osKernelInitialize(); // initialize CMSIS-RTOS #endif /* STM32F7xx HAL library initialization: - Configure the Flash ART accelerator on ITCM interface - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the System clock to have a frequency of 216 MHz */ SystemClock_Config(); /* Add your application code here */ BSP_SDRAM_Init(); Touch_Initialize(); #ifdef RTE_CMSIS_RTOS // when using CMSIS RTOS Init_Thread1(); Init_Thread2(); osKernelStart(); // start thread execution #endif Analyzer_Init(); GUI_Init(); osMutexWait(mid_Thread_Mutex,osWaitForever); Hello_MSG(); osMutexRelease(mid_Thread_Mutex); osThreadTerminate(osThreadGetId()); }
void DispatcherPrivate::run() { // we need the stack space, this is the only way to get it _dispid = osThreadGetId(); _ticker.attach_us(ticker_handler, _clock_multiplier); _wall.start(); TRACE(("Dispatcher starting all threads.\r\n")); for (int i = 0; i < _cnt_proc; ++i) PROCESS_LIST[i]->thread->thread.start(); TRACE(("Dispatcher starting.\r\n")); for (;;) { run_steps(); } }
/// Change priority of an active thread. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId. /// \param[in] priority new priority value for the thread function. /// \return status code that indicates the execution status of the function. /// \note MUST REMAIN UNCHANGED: \b osThreadSetPriority shall be consistent in every CMSIS-RTOS. osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority) { if (port_isr_inside()) return osErrorISR; if (thread_id == 0) thread_id = osThreadGetId(); sys_lock(); priority -= osPriorityIdle; thread_id->basic = priority; core_tsk_prio(thread_id, priority); sys_unlock(); return osOK; }
/********************************** 功能说明 *********************************** * 套接调用采样定时程序 *******************************************************************************/ static __task void funSocket( void const * p_arg ) { // typedef void ( * FunHandler )( void ); // static const FunHandler HandlerList[SamplerNum_Max] = static void ( * const HandlerList[SamplerNum_Max])( void ) = { _task_Sample_TSP, _task_Sample_R24, _task_Sample_SHI, _task_Sample_AIR, }; uint32_t SamplerID = (uint32_t)p_arg; (HandlerList[SamplerID])(); // 调用采样控制程序,然后结束任务 osThreadTerminate( osThreadGetId()); }
void net_task (void const *arg) { uint8_t count; bool ntpSuccess; netTaskErr resultGetTime; netTaskId = osThreadGetId(); while (1) { osSignalWait(FLAG_NTP_REQ_START, osWaitForever); ntpSuccess = false; ledSetState(TASK_LED, LedOn); for (count = 0; count < DHCP_WAIT_TRIES; count++) { if ((localm[NETIF_ETH].IpAddr[0] != 0) || (localm[NETIF_ETH].IpAddr[1] != 0) || (localm[NETIF_ETH].IpAddr[2] != 0) || (localm[NETIF_ETH].IpAddr[3] != 0)) { resultGetTime = getEpochTime(); if (resultGetTime == errOK) { updateRtcTime(); ntpSuccess = true; break; } } else osDelay(DHCP_WAIT_DELAY_MS); } if (ntpSuccess == true) ledSetState(TASK_LED, LedOff); else { ledSetState(TASK_LED, LedBlink); //rerequest NTP time after delay scheldueNTPRequest(); } } }
// Main program int main (void) { DAP_Setup(); // DAP Setup USBD_Initialize(0U); // USB Device Initialization USBD_Connect(0U); // USB Device Connect while (!USBD_Configured(0U)); // Wait for USB Device to configure LED_CONNECTED_OUT(1U); // Turn on Debugger Connected LED LED_RUNNING_OUT(1U); // Turn on Target Running LED Delayms(500U); // Wait for 500ms LED_RUNNING_OUT(0U); // Turn off Target Running LED LED_CONNECTED_OUT(0U); // Turn off Debugger Connected LED // Create HID Thread HID0_ThreadId = osThreadCreate(osThread(HID0_Thread), NULL); osThreadSetPriority(osThreadGetId(), osPriorityIdle); for (;;); // Endless Loop }
/** * @brief Main function. Executes all initialization and terminate its thread. * @param None * @retval None */ int main(void) { /////////////////////////////////////////////////////////////////////////////////////////// /* Configure the MPU attributes as Write Through */ MPU_Config(); /* Enable the CPU Cache */ CPU_CACHE_Enable(); // initialize CMSIS-RTOS osKernelInitialize(); /////////////////////////////////////////////////////////////////////////////////////////// // Hardware initialize if( HAL_Init() != HAL_OK) Error_Handler(); if( ConfigureDMA(&g_DmaHandle, &g_AdcHandle) != HAL_OK) Error_Handler(); if( ADC_INIT(&g_AdcHandle) != HAL_OK) Error_Handler(); BSP_SDRAM_Init(); Touch_Initialize(); /////////////////////////////////////////////////////////////////////////////////////////// /* Configure the System clock to have a frequency of 216 MHz */ SystemClock_Config(); // Thread initialization Init_TH_GUI(); Init_TH_Touch(); // RTOS Start Kernel osKernelStart(); // start thread execution // Get Main Thread ID Main_thID = osThreadGetId(); /////////////////////////////////////////////////////////////////////////////////////////// // Start data acquire HAL_ADC_Start_DMA(&g_AdcHandle, values, ADC_BUFFER_LENGTH); /////////////////////////////////////////////////////////////////////////////////////////// // Terminate main thread osThreadTerminate(Main_thID); /* Infinite loop */ while (1) { } }
void temperature_detection_thread (void const *argument) { /*Set configuration to ADC for acquiring Vadc*/ adcSetup(); setupTemperatureSensor(); Temperature_Tim_Interrupt(); temperatureGetId = osThreadGetId(); while(1){ // wait for the signal data ready sent by the ISR osSignalWait(SIGNAL_TEMPERATURE_READY, osWaitForever); temp = runTemperatureSensorMoving(); Interrupt_Timer_Temperature_read=0; //printf ("Temp:%f\n", temp); //--> FOR DEBUG PURPOSE //DisplayTemperature(temp); } }
/*! * Main function - forms main thread * */ int main(void) { // Get main thread ID mainFunctionId = osThreadGetId(); // Create thread threadFunctionId = osThreadCreate(osThread(threadFunction), NULL); if (threadFunctionId == NULL) { __asm("bkpt"); } printf("Main thread ID = %d\n", (int)mainFunctionId); printf("Thread ID = %d\n", (int)threadFunctionId); for (;;) { // Wait for signal from thread osSignalWait(SIGNAL_MASK, osWaitForever); printf("Thread signaled\n"); } }
/* 如果在中断中,则返回0 */ u32 icc_taskid_get(void) { #ifdef __KERNEL__ if (in_interrupt()) /*lint !e737 */ { return 0; } else { icc_print_debug("%s\n", current->comm); return (u32)(current->pid); } #elif defined(__VXWORKS__) if (intCnt) { return 0; } else { icc_print_debug("%s\n", taskName(taskIdSelf())); return (u32)(taskIdSelf()); } #elif defined(__CMSIS_RTOS) if (in_interrupt()) { return 0; } else { osThreadId task = osThreadGetId(); return (u32)(task->task_id); } #endif }
/*..........................................................................*/ void QF_init(void) { /* CMSIS-RTX must be initialized and started from the starupt code. */ Q_REQUIRE_ID(100, osKernelRunning()); /* Special consideration for Cortex-M systems with the hardware FPU... */ #ifdef __TARGET_FPU_VFP /* Enable access to Floating-point coprocessor in NVIC_CPACR */ (*((uint32_t volatile *)0xE000ED88U)) |= ((3U << 10*2) | (3U << 11*2)); /* Explictily Disable the automatic FPU state preservation and * the FPU lazy stacking in SCB_FPCCR */ (*((uint32_t volatile *)0xE000EF34U)) &= ~((1U << 31) | (1U << 30)); #endif /* __TARGET_FPU_VFP */ /* The main thread will be used as the ticker thread; save its ID */ l_tickerThreadId = osThreadGetId(); /* set the default period of the ticker thread... * NOTE: can be changed later in QF_setRtxTicker() */ l_tickerPeriod = 100000U; /* [microseconds] */ }