refactor(io): 重构 TCA6416 控制逻辑
写IO成功 - 优化 TCA6416 芯片的初始化和控制逻辑 - 添加 TCA6416_WritePort_buff 缓冲区用于存储输出数据 - 修改主任务中 TCA6416 相关的代码,提高稳定性和可靠性 - 调整 TCA6416 地址定义,适应硬件改动
This commit is contained in:
parent
e4f4929e01
commit
0e8af8fa30
|
@ -171,6 +171,7 @@ DMA_HandleTypeDef hdma_uart6_tx; // UART6发送DMA句柄
|
||||||
/* Private function prototypes -----------------------------------------------*/
|
/* Private function prototypes -----------------------------------------------*/
|
||||||
/* USER CODE BEGIN FunctionPrototypes */
|
/* USER CODE BEGIN FunctionPrototypes */
|
||||||
extern float current_buff[2];
|
extern float current_buff[2];
|
||||||
|
extern uint8_t TCA6416_WritePort_buff[2];
|
||||||
extern struct netif gnetif;
|
extern struct netif gnetif;
|
||||||
extern ETH_HandleTypeDef heth;
|
extern ETH_HandleTypeDef heth;
|
||||||
|
|
||||||
|
@ -262,7 +263,7 @@ void MX_FREERTOS_Init(void)
|
||||||
osThreadDef(io_control_task, start_io_control_task, osPriorityNormal, 0, 256);
|
osThreadDef(io_control_task, start_io_control_task, osPriorityNormal, 0, 256);
|
||||||
io_control_taskHandle = osThreadCreate(osThread(io_control_task), NULL);
|
io_control_taskHandle = osThreadCreate(osThread(io_control_task), NULL);
|
||||||
|
|
||||||
/* DAC控制任务 - 合并DAC161S997和DAC8568控制 */
|
/* DAC控制任务 DAC8568控制 */
|
||||||
osThreadDef(dac_control_task, start_dac_control_task, osPriorityBelowNormal, 0, 256);
|
osThreadDef(dac_control_task, start_dac_control_task, osPriorityBelowNormal, 0, 256);
|
||||||
dac_control_taskHandle = osThreadCreate(osThread(dac_control_task), NULL);
|
dac_control_taskHandle = osThreadCreate(osThread(dac_control_task), NULL);
|
||||||
|
|
||||||
|
@ -474,10 +475,16 @@ void start_io_control_task(void const *argument)
|
||||||
tca6416_init2_result = TCA6416_Init2();
|
tca6416_init2_result = TCA6416_Init2();
|
||||||
tca6416_init3_result = TCA6416_Init3();
|
tca6416_init3_result = TCA6416_Init3();
|
||||||
|
|
||||||
// 配置TCA6416端口方向
|
// 配置第一个TCA6416芯片的端口方向和输出状态
|
||||||
TCA6416_SetPortDirection(0, 0x00); // Port0输出
|
if(tca6416_init_result == 0) // 确保初始化成功
|
||||||
TCA6416_SetPortDirection(1, 0x00); // Port1输出
|
{
|
||||||
|
// 设置端口为输出模式
|
||||||
|
TCA6416_SetPortDirection(0, 0x00); // Port0输出
|
||||||
|
TCA6416_SetPortDirection(1, 0x00); // Port1输出
|
||||||
|
osDelay(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 配置其他两个芯片的端口方向
|
||||||
if(tca6416_init2_result == 0)
|
if(tca6416_init2_result == 0)
|
||||||
{
|
{
|
||||||
TCA6416_SetPortDirection2(0, 0xFF); // Port0输入
|
TCA6416_SetPortDirection2(0, 0xFF); // Port0输入
|
||||||
|
@ -496,20 +503,12 @@ void start_io_control_task(void const *argument)
|
||||||
// 获取信号量
|
// 获取信号量
|
||||||
if(osSemaphoreWait(io_semaphoreHandle, 100) == osOK)
|
if(osSemaphoreWait(io_semaphoreHandle, 100) == osOK)
|
||||||
{
|
{
|
||||||
// 处理GPIO输入输出
|
TCA6416_WritePort(0, TCA6416_WritePort_buff[1]);
|
||||||
user_gpio_trigger();
|
osDelay(5);
|
||||||
|
TCA6416_WritePort(1, TCA6416_WritePort_buff[0]);
|
||||||
// 读取TCA6416状态
|
osDelay(5);
|
||||||
if(TCA6416_ReadPort(0, &tca6416_read_data) == 0)
|
|
||||||
{
|
// 读取其他芯片状态
|
||||||
tca6416_port0_status = tca6416_read_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(TCA6416_ReadPort(1, &tca6416_read_data) == 0)
|
|
||||||
{
|
|
||||||
tca6416_port1_status = tca6416_read_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(tca6416_init2_result == 0)
|
if(tca6416_init2_result == 0)
|
||||||
{
|
{
|
||||||
if(TCA6416_ReadPort2(0, &tca6416_read_data) == 0)
|
if(TCA6416_ReadPort2(0, &tca6416_read_data) == 0)
|
||||||
|
@ -539,6 +538,7 @@ void start_io_control_task(void const *argument)
|
||||||
// 释放信号量
|
// 释放信号量
|
||||||
osSemaphoreRelease(io_semaphoreHandle);
|
osSemaphoreRelease(io_semaphoreHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 200ms扫描周期
|
// 200ms扫描周期
|
||||||
osDelay(200);
|
osDelay(200);
|
||||||
|
@ -734,10 +734,7 @@ void start_usart6_test_task(void const *argument)
|
||||||
}
|
}
|
||||||
/* USER CODE END start_usart6_test_task */
|
/* USER CODE END start_usart6_test_task */
|
||||||
}
|
}
|
||||||
void start_usart6_dma_test_task(void const *argument)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_tca6416_task(void const *argument)
|
void test_tca6416_task(void const *argument)
|
||||||
{
|
{
|
||||||
|
|
|
@ -75,6 +75,7 @@ uart_t hart2_uart2 = {0};
|
||||||
uart_t hart1_uart5 = {0};
|
uart_t hart1_uart5 = {0};
|
||||||
uart_t usb_uart1 = {0};
|
uart_t usb_uart1 = {0};
|
||||||
float current_buff[2] = {12.0f, 0};//设置初始电流为12ma
|
float current_buff[2] = {12.0f, 0};//设置初始电流为12ma
|
||||||
|
uint8_t TCA6416_WritePort_buff[2] = {0, 0};
|
||||||
|
|
||||||
uint8_t tcp_echo_flags_hart1 = 0;
|
uint8_t tcp_echo_flags_hart1 = 0;
|
||||||
uint8_t tcp_echo_flags_hart2 = 0;
|
uint8_t tcp_echo_flags_hart2 = 0;
|
||||||
|
|
|
@ -250,6 +250,21 @@
|
||||||
<WinNumber>1</WinNumber>
|
<WinNumber>1</WinNumber>
|
||||||
<ItemText>dac161s997_status_reg</ItemText>
|
<ItemText>dac161s997_status_reg</ItemText>
|
||||||
</Ww>
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>20</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>tca6416_error_count</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>21</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>TCA6416_WritePort_buff[0]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>22</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>TCA6416_WritePort_buff[1]</ItemText>
|
||||||
|
</Ww>
|
||||||
</WatchWindow1>
|
</WatchWindow1>
|
||||||
<Tracepoint>
|
<Tracepoint>
|
||||||
<THDelay>0</THDelay>
|
<THDelay>0</THDelay>
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
#define TCA6416_SDA_PORT2 GPIOE
|
#define TCA6416_SDA_PORT2 GPIOE
|
||||||
|
|
||||||
/* TCA6416 I2C地址 */
|
/* TCA6416 I2C地址 */
|
||||||
#define TCA6416_ADDR 0x20 // 第一个芯片地址 (A0=A1=A2=GND)单独总线,SDA PE3 SCL PE4
|
#define TCA6416_ADDR 0x20 // 第一个芯片地址 (A0=A1=A2=GND)单独总线,SDA PE3 SCL PE2
|
||||||
#define TCA6416_ADDR2 0x21 // 第二个芯片地址 (A0=1, A1=A2=GND)与TCA6416_ADDR3共用总线,SDA PE4 SCL PE5
|
#define TCA6416_ADDR2 0x21 // 第二个芯片地址 (A0=1, A1=A2=GND)与TCA6416_ADDR3共用总线,SDA PE4 SCL PE5
|
||||||
#define TCA6416_ADDR3 0x20 // 第三个芯片地址 (与第二个芯片共用总线,地址0x20)
|
#define TCA6416_ADDR3 0x20 // 第三个芯片地址 (与第二个芯片共用总线,地址0x20)
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,9 @@
|
||||||
#include "task.h"
|
#include "task.h"
|
||||||
#include "cmsis_os.h"
|
#include "cmsis_os.h"
|
||||||
|
|
||||||
|
// 添加外部信号量声明
|
||||||
|
extern osSemaphoreId io_semaphoreHandle; // 添加IO控制信号量外部声明
|
||||||
|
|
||||||
// 添加test_adc_read_data变量定义
|
// 添加test_adc_read_data变量定义
|
||||||
uint8_t test_adc_read_data[74] = {0}; // 74字节的测试数据缓冲区
|
uint8_t test_adc_read_data[74] = {0}; // 74字节的测试数据缓冲区
|
||||||
|
|
||||||
|
@ -51,6 +54,7 @@ extern uint8_t tcp_echo_flags_ble2;
|
||||||
extern uint8_t tcp_echo_flags_control;
|
extern uint8_t tcp_echo_flags_control;
|
||||||
extern uint8_t send_data_flag_cmd;
|
extern uint8_t send_data_flag_cmd;
|
||||||
extern float current_buff[2]; // 只声明,不初始化
|
extern float current_buff[2]; // 只声明,不初始化
|
||||||
|
extern uint8_t TCA6416_WritePort_buff[2];
|
||||||
|
|
||||||
extern uint8_t uart_echo_flags_hart1;
|
extern uint8_t uart_echo_flags_hart1;
|
||||||
extern uint8_t uart_echo_flags_hart2;
|
extern uint8_t uart_echo_flags_hart2;
|
||||||
|
@ -805,14 +809,44 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *
|
||||||
current_buff [0] = (float)dac161s_value / 1000.0f;
|
current_buff [0] = (float)dac161s_value / 1000.0f;
|
||||||
|
|
||||||
|
|
||||||
|
//中间4个字节是比例阀
|
||||||
// 数字量输出刷新第一个芯片的输出状态
|
// 数字量输出刷新第一个芯片的输出状态
|
||||||
if(tcp_rx_data[27] != 0xFF || tcp_rx_data[28] != 0xFF) { // 只在值变化时更新
|
TCA6416_WritePort_buff[0] = tcp_rx_data[31];
|
||||||
TCA6416_WritePort(0, tcp_rx_data[27]); // 直接写入Port0的值8-15
|
TCA6416_WritePort_buff[1] = tcp_rx_data[32];
|
||||||
osDelay(5);
|
// if(tcp_rx_data[27] != 0xFF || tcp_rx_data[28] != 0xFF)
|
||||||
TCA6416_WritePort(1, tcp_rx_data[28]); // 写入Port1的值0-7
|
// { // 只在值变化时更新
|
||||||
osDelay(5);
|
// // 获取信号量保护
|
||||||
}
|
// if(osSemaphoreWait(io_semaphoreHandle, 100) == osOK)
|
||||||
|
// {
|
||||||
|
// // 设置输出前先确认端口方向
|
||||||
|
// TCA6416_SetPortDirection(0, 0x00); // 确保Port0为输出
|
||||||
|
// osDelay(5);
|
||||||
|
// TCA6416_SetPortDirection(1, 0x00); // 确保Port1为输出
|
||||||
|
// osDelay(5);
|
||||||
|
|
||||||
|
// // 写入输出值
|
||||||
|
// TCA6416_WritePort(0, tcp_rx_data[27]); // Port0的值8-15
|
||||||
|
// osDelay(5);
|
||||||
|
// TCA6416_WritePort(1, tcp_rx_data[28]); // Port1的值0-7
|
||||||
|
// osDelay(5);
|
||||||
|
|
||||||
|
// // 验证写入是否成功
|
||||||
|
// uint8_t verify0, verify1;
|
||||||
|
// if(TCA6416_ReadPort(0, &verify0) == 0 && TCA6416_ReadPort(1, &verify1) == 0)
|
||||||
|
// {
|
||||||
|
// if(verify0 != tcp_rx_data[27] || verify1 != tcp_rx_data[28])
|
||||||
|
// {
|
||||||
|
// // 如果验证失败,重试一次
|
||||||
|
// TCA6416_WritePort(0, tcp_rx_data[27]);
|
||||||
|
// osDelay(5);
|
||||||
|
// TCA6416_WritePort(1, tcp_rx_data[28]);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 释放信号量
|
||||||
|
// osSemaphoreRelease(io_semaphoreHandle);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// 保存设置的数据到adc_set_data数组
|
// 保存设置的数据到adc_set_data数组
|
||||||
memcpy(adc_set_data, tcp_rx_data + 9, body_len);
|
memcpy(adc_set_data, tcp_rx_data + 9, body_len);
|
||||||
|
|
Loading…
Reference in New Issue