From c451e7d75461f7580e1008288f820796390df96f Mon Sep 17 00:00:00 2001 From: qiuxin Date: Fri, 30 May 2025 15:03:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(freertos):=20=E6=9B=B4=E6=96=B0=20TCA6?= =?UTF-8?q?416=20=E4=BB=BB=E5=8A=A1=E5=88=9B=E5=BB=BA=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 TCA6416 任务创建的注释 - 更新任务名称从 start_tca6416_task 到 test_tca6416_task - 删除多余的空行 fix(ad7124): 重构 AD7124 SPI 通信函数 - 移除冗余的接收缓冲区声明 - 新增 AD7124_OUT_spi_transmit_receive 函数用于 AD7124 的 SPI 通信 - 更新函数调用以使用新的 AD7124 通信函数 - 在头文件中添加新函数的声明 --- Core/Src/freertos.c | 6 +++--- User/driver/ad7124.c | 6 +++--- User/system/user_spi.c | 19 +++++++++++++++++-- User/system/user_spi.h | 1 + 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 2fee678..1716548 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -204,9 +204,9 @@ void MX_FREERTOS_Init(void) usart6_test_taskHandle = osThreadCreate(osThread(usart6_test_task), NULL); /* TCA6416任务 */ - //osThreadDef(tca6416_task, start_tca6416_task, osPriorityNormal, 0, 128); - // tca6416_taskHandle = osThreadCreate(osThread(tca6416_task), NULL); - /* TCA6416测试任务 */ + osThreadDef(tca6416_task, test_tca6416_task, osPriorityNormal, 0, 128); + tca6416_taskHandle = osThreadCreate(osThread(tca6416_task), NULL); + /* DAC161S997测试任务 */ diff --git a/User/driver/ad7124.c b/User/driver/ad7124.c index 219d0ad..970374e 100644 --- a/User/driver/ad7124.c +++ b/User/driver/ad7124.c @@ -415,10 +415,10 @@ int32_t ad7124_setup(void) int32_t ad7124_read_write_spi(uint8_t *buff, uint8_t length) { int32_t ret; - uint8_t rx_buff[8] = {0}; // Add receive buffer - board_spi_init(AD7124); // 初始化SPI,因为DAC161的SPI要和AD7124共用,并且时序不一样,所以要先初始化SPI接口。 + board_spi_init(AD7124); board_spi_cs_on(AD7124); - ret = spi_transmit_receive(&hspi1, buff, rx_buff, length); + ret = AD7124_OUT_spi_transmit_receive(buff, length); board_spi_cs_off(AD7124); return ret; } + diff --git a/User/system/user_spi.c b/User/system/user_spi.c index 6577b91..05a0750 100644 --- a/User/system/user_spi.c +++ b/User/system/user_spi.c @@ -20,9 +20,10 @@ typedef volatile uint32_t __IO; #define SUCCESS 0 #define FAILURE -1 +#define SPI_BUFFER_SIZE 255 // 定义SPI缓冲区 static uint8_t spi_rx_buffer[256]; // 接收缓冲区 - +static uint8_t ad7124_spi_rx_buffer[SPI_BUFFER_SIZE] = {0}; // SPI句柄声明 extern SPI_HandleTypeDef hspi1; // 在spi.c中定义 extern SPI_HandleTypeDef hspi2; // 在main.c中定义 @@ -58,7 +59,7 @@ extern SPI_HandleTypeDef hspi3; // 在main.c中定义 /***************************** #defines ***************************************/ /******************************************************************************/ -#define SPI_BUFFER_SIZE 255 + /******************************************************************************/ @@ -71,6 +72,20 @@ extern SPI_HandleTypeDef hspi1; // 在spi.c中定义 /******************************************************************************/ /************************ Functions Definitions *******************************/ /******************************************************************************/ +//AD7124重要通信函数 +int32_t AD7124_OUT_spi_transmit_receive(uint8_t *data_write, uint8_t bytes_number) +{ + // 使用 HAL_SPI_TransmitReceive 函数进行 SPI 通信 + if (HAL_SPI_TransmitReceive(&hspi2, data_write, (uint8_t *)ad7124_spi_rx_buffer, bytes_number, 1000) != HAL_OK) + { + // 如果通信失败,返回 FAILURE + return FAILURE; + } + /* 将 SPI 接收缓冲区复制到提供的数据缓冲区,以便返回给调用者 */ + memcpy(data_write, ad7124_spi_rx_buffer, bytes_number); + // 返回 SUCCESS 表示通信成功 + return SUCCESS; +} int32_t spi_transmit_receive(SPI_HandleTypeDef *hspi, uint8_t *data_write, uint8_t *data_read, uint8_t bytes_number) { diff --git a/User/system/user_spi.h b/User/system/user_spi.h index 597b76d..4982741 100644 --- a/User/system/user_spi.h +++ b/User/system/user_spi.h @@ -13,6 +13,7 @@ typedef enum } chip_type_e; // 芯片类型 extern int32_t spi_transmit_receive(SPI_HandleTypeDef *hspi, uint8_t *data_write, uint8_t *data_read, uint8_t bytes_number); +extern int32_t AD7124_OUT_spi_transmit_receive(uint8_t *data_write, uint8_t bytes_number); extern SPI_HandleTypeDef hspi1; extern SPI_HandleTypeDef hspi2; extern SPI_HandleTypeDef hspi3;