/** * @brief Toggle LED2 thread * @param argument not used * @retval None */ static void LED_Thread2(void const *argument) { uint32_t count; (void) argument; for(;;) { count = osKernelSysTick() + 10000; /* Toggle LED2 every 500 ms for 10 s */ while (count >= osKernelSysTick()) { BSP_LED_Toggle(LED2); osDelay(500); } /* Turn off LED2 */ BSP_LED_Off(LED2); /* Resume Thread 1 */ osThreadResume(LEDThread1Handle); /* Suspend Thread 2 */ osThreadSuspend(NULL); } }
/** * @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); } } } }
/** * @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); } } } }
/** * @brief Toggle LED3 thread * @param argument not used * @retval None */ static void LED_Thread2(void const *argument) { uint32_t count; (void) argument; for (;;) { count = osKernelSysTick() + 10000; while (count >= osKernelSysTick()) { BSP_LED_On(LED3); osDelay(100); BSP_LED_Off(LED3); osDelay(100); BSP_LED_On(LED3); osDelay(100); BSP_LED_Off(LED3); HAL_Delay(1000); } /* Turn off LED3 */ BSP_LED_Off(LED3); /* Resume Thread 1 */ osThreadResume(LEDThread1Handle); /* Suspend Thread 2 */ osThreadSuspend(NULL); } }
/** * @brief Toggle LED1 thread * @param Thread not used * @retval None */ static void LED_Thread1(void const *argument) { (void) argument; uint32_t PreviousWakeTime = osKernelSysTick(); for(;;) { /* osDelayUntil function differs from osDelay() in one important aspect: osDelay () will * cause a thread to block for the specified time in ms from the time osDelay () is * called. It is therefore difficult to use osDelay () by itself to generate a fixed * execution frequency as the time between a thread starting to execute and that thread * calling osDelay () may not be fixed [the thread may take a different path though the * code between calls, or may get interrupted or preempted a different number of times * each time it executes]. * * Whereas osDelay () specifies a wake time relative to the time at which the function * is called, osDelayUntil () specifies the absolute (exact) time at which it wishes to * unblock. * PreviousWakeTime must be initialised with the current time prior to its first use * (PreviousWakeTime = osKernelSysTick() ) */ osDelayUntil (&PreviousWakeTime, 200); BSP_LED_Toggle(LED1); } }
/** * @brief Toggle LED3 and LED4 thread * @param thread not used * @retval None */ static void LED_Thread1(void const *argument) { uint32_t count = 0; (void) argument; for (;;) { count = osKernelSysTick() + 5000; /* Toggle LED3 every 200 ms for 5 s */ while (count >= osKernelSysTick()) { BSP_LED_Toggle(LED3); osDelay(200); } /* Turn off LED3 */ BSP_LED_Off(LED3); /* Suspend Thread 1 */ osThreadSuspend(NULL); count = osKernelSysTick() + 5000; /* Toggle LED3 every 400 ms for 5 s */ while (count >= osKernelSysTick()) { BSP_LED_Toggle(LED3); osDelay(400); } /* Resume Thread 2*/ osThreadResume(LEDThread2Handle); } }
static void _prog_loop(void const * argument) { ticks = osKernelSysTick(); while (1) { update_common_values(); io_execute_in(); program_execute(); io_execute_out(); osDelayUntil(&ticks, PROG_LOOP_PERIOD_MS); } }
/********************************** 功能说明 *********************************** * 恒温箱温度控制 *******************************************************************************/ __task void _task_HCBox( void const * p_arg ) { // HCBoxFan_Circle_PortInit(); SaveTick = osKernelSysTick(); // get start value of the Kernel system tick HCBox_Wait(); // dummy read, skip 0x0550 HCBox_Wait(); // 强制停留在等待状态一段时间 for(;;) { set_HCBoxTemp( Configure.HCBox_SetTemp * 0.1f, Configure.HCBox_SetMode ); Set_HCBox_Temp( Configure.HCBox_SetTemp * 0.1f, Configure.HCBox_SetMode ); switch ( HCBox.SetMode ) { case MD_Auto: if (( HCBox.SetTemp - HCBox.RunTemp ) < -1.0f ) { HCBox_Cool(); } else if (( HCBox.SetTemp - HCBox.RunTemp ) > +1.0f ) { HCBox_Heat(); } else { // 设置自动模式即无法确定实际工作模式可暂时进入等待状态 HCBox_Wait(); } break; case MD_Cool: HCBox_Cool(); break; case MD_Heat: HCBox_Heat(); break; default: case MD_Shut: HCBox_Wait(); break; } } }
systime_t osGetSystemTime(void) { systime_t time; #if defined(osCMSIS_RTX) //Forward function declaration extern uint32_t rt_time_get(void); //Get current tick count time = rt_time_get(); #else //Get current tick count time = osKernelSysTick(); #endif //Convert system ticks to milliseconds return OS_SYSTICKS_TO_MS(time); }
/* StartMainTask function */ void StartMainTask(void const * argument) { /* USER CODE BEGIN 5 */ SYS_DEBUG_TRACK(); #ifdef DEBUG_ON uint32_t l_time; uint32_t c_time; l_time = c_time = 0; #endif /* Infinite loop */ for(;;) { SYS_DEBUG("one loop!\n"); // PrintStackRest(); /* 1.查询模式开关状态,如果是自动,则切换为指定的自动模式 */ ModeControl_CheckAndSetAutoMode( CONTROL_MODE_AUTO_PITCH_ROLL ); /* 2.监视系统处理函数的执行时间,如果长时间没有收到数据或者没有处理,则释放飞行器 */ DataProcess_CheackProcessTime( ); #ifdef DEBUG_ON /* 用于调试 */ c_time = osKernelSysTick(); if( (c_time - l_time)*portTICK_PERIOD_MS > 2000 ) { SensorData_t SensorData; SensorData.time_boot_ms = 10000; SensorData.x = 0100; SensorData.y = 0200; SensorData.z = 0200; SensorData.convariance = 1.2; DataParse_PutData( &SensorData ); } l_time = c_time; #endif osDelay(200); } /* USER CODE END 5 */ }
void detect_task(void const *argu) { uint32_t detect_wake_time = osKernelSysTick(); while(1) { detect_time_ms = HAL_GetTick() - detect_time_last; detect_time_last = HAL_GetTick(); /* module offline detect */ module_offline_detect(); if (glb_err_exit == 1) { if (pc_glb_cnt++ > 50) pc_glb_cnt = 0; if (pc_glb_cnt < 15) g_err.beep_ctrl = g_err.beep_tune/2; else g_err.beep_ctrl = 0; } else { if (g_err.err_now != NULL) { //LED_G_OFF; module_offline_callback(); } else { g_err.beep_ctrl = 0; //LED_G_ON; } } beep_ctrl(g_err.beep_tune, g_err.beep_ctrl); detect_stack_surplus = uxTaskGetStackHighWaterMark(NULL); osDelayUntil(&detect_wake_time, DETECT_TASK_PERIOD); } }
void HCBox_Output( FP32 OutValue ) { // 更新输出状态 HCBox.OutValue = OutValue; if ( OutValue < 0.0f ) { // 关闭加热 HCBoxHeat_OutCmd( FALSE ); // 开启制冷 if ( OutValue < -0.990f ) { HCBoxCool_OutCmd( TRUE ); // delay( 1000u ); } else if ( OutValue > -0.010f ) { HCBoxCool_OutCmd( FALSE ); // delay( 1000u ); } else { uint16_t delayCount = (((( -1000 * OutValue ) + 5 ) / 10 ) * 10 ); HCBoxCool_OutCmd( TRUE ); delay( delayCount ); HCBoxCool_OutCmd( FALSE ); // delay( 1000u - delayCount ); } } else if ( OutValue > 0.0f ) { // 关闭制冷 HCBoxCool_OutCmd( FALSE ); // 开启加热 if ( OutValue > +0.990f ) { HCBoxHeat_OutCmd( TRUE ); // delay( 1000u ); } else if ( OutValue < +0.010f ) { HCBoxHeat_OutCmd( FALSE ); // delay( 1000u ); } else { uint16_t delayCount = (((( +1000 * OutValue ) + 5 ) / 10 ) * 10 ); HCBoxHeat_OutCmd( TRUE ); delay( delayCount ); // 加热延时 HCBoxHeat_OutCmd( FALSE ); // delay( 1000u - delayCount ); // 加热暂停 } } else { // 关闭加热 HCBoxHeat_OutCmd( FALSE ); // 关闭制冷 HCBoxCool_OutCmd( FALSE ); // delay( 1000u ); } // 定间隔延时 { // 使用SysTick同步方法,要点在于delay的单位是固定的1毫秒,osKernelSysTickMicroSec( 1000 )即1毫秒。 const uint32_t Tick1ms = osKernelSysTickMicroSec( 1000u ); uint16_t delayCount = 1000u - (( osKernelSysTick() - SaveTick ) / Tick1ms ); if ( delayCount < 1000u ) { delay( delayCount ); } SaveTick += ( Tick1ms * 1000u ); } // 延时后立即执行 HCBoxFan_Update(); // 测量风扇转速 }