From f41ec8a70ff0384194d803941cdfcc6b64cdb5a1 Mon Sep 17 00:00:00 2001 From: qiuxin Date: Mon, 16 Jun 2025 10:49:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor(freertos):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E9=85=8D=E7=BD=AE=E5=92=8C=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调整任务优先级和栈大小 - 移除冗余的 DAC161S997 测试任务 - 优化 IO 控制任务的执行逻辑 - 更新 DAC 控制任务,增加 DAC161S997 初始化和输出 - 调整任务执行周期和延时 --- Core/Src/freertos.c | 140 +++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 86 deletions(-) diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 21ac5c8..d6b2388 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -315,7 +315,7 @@ void start_led_toggle_task(void const *argument); void start_adc_task(void const *argument); void start_io_control_task(void const *argument); void start_dac_control_task(void const *argument); -void start_dac161s997_test_task(void const *argument); +void start_dac8568_test_task(void const *argument); extern void MX_LWIP_Init(void); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ @@ -364,29 +364,25 @@ void MX_FREERTOS_Init(void) /* Create the thread(s) */ /* definition and creation of lwip_task */ - osThreadDef(lwip_task, start_tcp_task, osPriorityHigh, 0, 512); + osThreadDef(lwip_task, start_tcp_task, osPriorityRealtime, 0, 512); lwip_taskHandle = osThreadCreate(osThread(lwip_task), NULL); - /* HART测试任务 - 提高优先级和栈大小 */ - osThreadDef(usart6_test_task, start_usart6_test_task, osPriorityAboveNormal, 0, 256); + /* HART测试任务 */ + osThreadDef(usart6_test_task, start_usart6_test_task, osPriorityLow, 0, 256); usart6_test_taskHandle = osThreadCreate(osThread(usart6_test_task), NULL); - /* ADC任务 - 提高优先级和栈大小 */ + /* ADC任务 */ osThreadDef(adc_task, start_adc_task, osPriorityNormal, 0, 256); adc_taskHandle = osThreadCreate(osThread(adc_task), NULL); - /* IO控制任务 - 合并TCA6416和GPIO控制 */ + /* IO监听控制任务 */ osThreadDef(io_control_task, start_io_control_task, osPriorityNormal, 0, 256); io_control_taskHandle = osThreadCreate(osThread(io_control_task), NULL); - /* DAC控制任务 DAC8568控制 */ - osThreadDef(dac_control_task, start_dac_control_task, osPriorityBelowNormal, 0, 256); + /* 合并后的DAC控制任务 - 使用较低优先级 */ + osThreadDef(dac_control_task, start_dac_control_task, osPriorityBelowNormal, 0, 512); // 增加堆栈大小 dac_control_taskHandle = osThreadCreate(osThread(dac_control_task), NULL); - /* DAC161S997测试任务 - 使用较高优先级以确保及时处理 */ - osThreadDef(dac161s997_test_task, start_dac161s997_test_task, osPriorityAboveNormal, 0, 256); - osThreadCreate(osThread(dac161s997_test_task), NULL); - /* USER CODE END RTOS_THREADS */ } @@ -499,7 +495,7 @@ void start_tcp_task(void const *argument) } } - vTaskDelay(1000); + vTaskDelay(5); } /* USER CODE END start_tcp_task */ } @@ -734,9 +730,11 @@ void start_io_control_task(void const *argument) /* USER CODE BEGIN start_io_control_task */ uint8_t ret = 0; uint8_t addr_scan_result[8] = {0}; + static uint32_t last_scan_time = 0; + const uint32_t SCAN_INTERVAL = 20; // 20ms扫描间隔 // 等待系统稳定 - osDelay(100); + osDelay(50); // 初始化TCA6416 tca6416_test_step = 0; @@ -746,7 +744,7 @@ void start_io_control_task(void const *argument) ret = I2C_SendByte(addr << 1); I2C_Stop(); addr_scan_result[addr - 0x20] = ret; - osDelay(10); + osDelay(1); } // 初始化三个TCA6416芯片 @@ -755,73 +753,68 @@ void start_io_control_task(void const *argument) tca6416_init3_result = TCA6416_Init3(); // 配置第一个TCA6416芯片的端口方向和输出状态 - if(tca6416_init_result == 0) // 确保初始化成功 + if(tca6416_init_result == 0) { - // 设置端口为输出模式 - TCA6416_SetPortDirection(0, 0x00); // Port0输出 - TCA6416_SetPortDirection(1, 0x00); // Port1输出 - osDelay(5); + TCA6416_SetPortDirection(0, 0x00); + TCA6416_SetPortDirection(1, 0x00); + osDelay(1); } // 配置其他两个芯片的端口方向 if(tca6416_init2_result == 0) { - TCA6416_SetPortDirection2(0, 0xFF); // Port0输入 - TCA6416_SetPortDirection2(1, 0xFF); // Port1输入 + TCA6416_SetPortDirection2(0, 0xFF); + TCA6416_SetPortDirection2(1, 0xFF); } if(tca6416_init3_result == 0) { - TCA6416_SetPortDirection3(0, 0xFF); // Port0输入 - TCA6416_SetPortDirection3(1, 0xFF); // Port1输入 + TCA6416_SetPortDirection3(0, 0xFF); + TCA6416_SetPortDirection3(1, 0xFF); } /* Infinite loop */ for(;;) { - // 获取信号量 - if(osSemaphoreWait(io_semaphoreHandle, 100) == osOK) + uint32_t current_time = HAL_GetTick(); + + // 使用时间间隔控制扫描频率 + if(current_time - last_scan_time >= SCAN_INTERVAL) { - //写入第一块 - TCA6416_WritePort(0, TCA6416_WritePort_buff[1]); - osDelay(5); - TCA6416_WritePort(1, TCA6416_WritePort_buff[0]); - osDelay(5); - - // 读取其他芯片状态 - if(tca6416_init2_result == 0) + // 获取信号量,但设置较短的超时时间 + if(osSemaphoreWait(io_semaphoreHandle, 5) == osOK) { - if(TCA6416_ReadPort2(0, &tca6416_read_data) == 0) + // 写入第一块 + TCA6416_WritePort(0, TCA6416_WritePort_buff[1]); + TCA6416_WritePort(1, TCA6416_WritePort_buff[0]); + + // 读取其他芯片状态 + if(tca6416_init2_result == 0) { + TCA6416_ReadPort2(0, &tca6416_read_data); tca6416_port2_status = tca6416_read_data; - } - - if(TCA6416_ReadPort2(1, &tca6416_read_data) == 0) - { + + TCA6416_ReadPort2(1, &tca6416_read_data); tca6416_port3_status = tca6416_read_data; } - } - - if(tca6416_init3_result == 0) - { - if(TCA6416_ReadPort3(0, &tca6416_read_data) == 0) - { - tca6416_port4_status = tca6416_read_data; - } - if(TCA6416_ReadPort3(1, &tca6416_read_data) == 0) + if(tca6416_init3_result == 0) { + TCA6416_ReadPort3(0, &tca6416_read_data); + tca6416_port4_status = tca6416_read_data; + + TCA6416_ReadPort3(1, &tca6416_read_data); tca6416_port5_status = tca6416_read_data; } + + osSemaphoreRelease(io_semaphoreHandle); } - // 释放信号量 - osSemaphoreRelease(io_semaphoreHandle); + last_scan_time = current_time; } - - // 200ms扫描周期 - osDelay(50); + // 使用较短的延时,让出CPU时间 + osDelay(100); } /* USER CODE END start_io_control_task */ } @@ -839,7 +832,9 @@ void start_dac_control_task(void const *argument) // 等待系统稳定 osDelay(1000); - + // 初始化DAC161S997 + dac161s997_init(); + // 初始化DAC8568 DAC8568_Init(&hspi3); HAL_Delay(10); @@ -851,9 +846,8 @@ void start_dac_control_task(void const *argument) // 获取信号量 if(osSemaphoreWait(dac_semaphoreHandle, 100) == osOK) { - // 处理DAC161S997输出 - // current_buff[0] = 12.0f; // 示例值 - // dac161s997_output(DAC161S997, current_buff[0]); + // 处理DAC161S997输出 - 必须脉冲输出 + dac161s997_output(DAC161S997, current_buff[0]); // 处理DAC8568输出 // uint16_t dac_value = 32768; // 示例值 @@ -863,8 +857,8 @@ void start_dac_control_task(void const *argument) osSemaphoreRelease(dac_semaphoreHandle); } - // 500ms更新周期 - osDelay(500); + // 使用较短的延时,因为DAC161S997需要脉冲输出 + osDelay(100); // 100ms更新周期,与原来的DAC161S997任务保持一致 } /* USER CODE END start_dac_control_task */ } @@ -1010,7 +1004,7 @@ void start_usart6_test_task(void const *argument) uart6_last_rx_time = 0; } - vTaskDelay(5); // 给其他任务执行的机会 + vTaskDelay(500); // 给其他任务执行的机会 } /* USER CODE END start_usart6_test_task */ } @@ -1133,32 +1127,6 @@ void test_tca6416_task(void const *argument) /* USER CODE END test_tca6416_task */ } -/* USER CODE BEGIN Header_start_dac161s997_test_task */ -/** - * @brief Function implementing the dac161s997_test_task thread. - * @param argument: Not used - * @retval None - */ -/* USER CODE END Header_start_dac161s997_test_task */ -void start_dac161s997_test_task(void const *argument) -{ - - // 等待系统稳定 - osDelay(1000); // 启动延时1秒等待系统稳定 - - // 初始化DAC161S997 - dac161s997_init(); - /* DAC161必须脉冲输出*/ - for (;;) - { - - dac161s997_output(DAC161S997, current_buff[0]); // 输出当前电流值 - - osDelay(100); // 延时100ms - } - /* USER CODE END start_dac161s997_test_task */ -} - /* USER CODE BEGIN Header_start_dac8568_test_task */ /** * @brief Function implementing the dac8568_test_task thread.