384 lines
13 KiB
C
384 lines
13 KiB
C
#include "user_spi.h"
|
|
#include "stm32f4xx_hal.h"
|
|
#include "stm32f4xx_hal_gpio_ex.h" // GPIO复用功能定义
|
|
#include "stm32f4xx_hal_spi.h" // SPI外设定义
|
|
#include "stm32f4xx_hal_rcc.h" // RCC时钟定义
|
|
#include "stm32f4xx_hal_def.h" // 基本类型定义
|
|
#include "stm32f4xx_hal_rcc_ex.h" // RCC扩展定义
|
|
#include "stm32f4xx_hal_gpio.h" // GPIO定义
|
|
#include "stm32f4xx_hal_conf.h" // HAL配置
|
|
#include "main.h"
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include "DAC8568.h"
|
|
#include "spi.h"
|
|
|
|
// 定义基本类型
|
|
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中定义
|
|
extern SPI_HandleTypeDef hspi3; // 在main.c中定义
|
|
|
|
// 定义GPIO端口
|
|
// #define GPIOC_BASE (AHB1PERIPH_BASE + 0x0800)
|
|
// #define GPIOB_BASE (AHB1PERIPH_BASE + 0x0400)
|
|
// #define GPIOD_BASE (AHB1PERIPH_BASE + 0x0C00)
|
|
// #define GPIOC ((GPIO_TypeDef *) GPIOC_BASE)
|
|
// #define GPIOB ((GPIO_TypeDef *) GPIOB_BASE)
|
|
// #define GPIOD ((GPIO_TypeDef *) GPIOD_BASE)
|
|
|
|
// // 定义SPI外设
|
|
// #define SPI2_BASE (APB1PERIPH_BASE + 0x3800)
|
|
// #define SPI2 ((SPI_TypeDef *) SPI2_BASE)
|
|
|
|
// // 定义SPI寄存器位
|
|
// #define SPI_CR1_MSTR_Pos 2
|
|
// #define SPI_CR1_CPOL_Pos 1
|
|
// #define SPI_CR1_CPHA_Pos 0
|
|
// #define SPI_CR1_SSI_Pos 8
|
|
// #define SPI_CR1_SSM_Pos 9
|
|
// #define SPI_CR1_BR_Pos 3
|
|
// #define SPI_CR1_MSTR (1 << SPI_CR1_MSTR_Pos)
|
|
// #define SPI_CR1_CPOL (1 << SPI_CR1_CPOL_Pos)
|
|
// #define SPI_CR1_CPHA (1 << SPI_CR1_CPHA_Pos)
|
|
// #define SPI_CR1_SSI (1 << SPI_CR1_SSI_Pos)
|
|
// #define SPI_CR1_SSM (1 << SPI_CR1_SSM_Pos)
|
|
// #define SPI_CR1_BR_2 (1 << (SPI_CR1_BR_Pos + 2))
|
|
|
|
/******************************************************************************/
|
|
/***************************** #defines ***************************************/
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
/************************ Variable Declarations *******************************/
|
|
/******************************************************************************/
|
|
|
|
// SPI handle declaration
|
|
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)
|
|
{
|
|
if (HAL_SPI_TransmitReceive(hspi, data_write, data_read, bytes_number, 1000) != HAL_OK)
|
|
{
|
|
return FAILURE;
|
|
}
|
|
return SUCCESS;
|
|
}
|
|
|
|
void ad7124_spi_init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
// Enable GPIO and SPI clocks
|
|
__HAL_RCC_SPI2_CLK_ENABLE();
|
|
__HAL_RCC_GPIOC_CLK_ENABLE(); // For MISO and MOSI
|
|
__HAL_RCC_GPIOB_CLK_ENABLE(); // For SCK
|
|
__HAL_RCC_GPIOD_CLK_ENABLE(); // For CS
|
|
|
|
// Configure SPI2 pins
|
|
// MISO (PC2) and MOSI (PC3)
|
|
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; // SPI2 alternate function
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
// SCK (PB10)
|
|
GPIO_InitStruct.Pin = GPIO_PIN_10;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; // SPI2 alternate function
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
// CS (PD10) - Configure as output
|
|
GPIO_InitStruct.Pin = GPIO_PIN_10;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP; // Add pull-up to ensure high when not driven
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
// CS (PD11) - Configure as output
|
|
GPIO_InitStruct.Pin = GPIO_PIN_11;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP; // Add pull-up to ensure high when not driven
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
// Configure SPI2
|
|
hspi2.Instance = SPI2;
|
|
hspi2.Init.Mode = SPI_MODE_MASTER;
|
|
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
|
|
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH; // CPOL = 1
|
|
hspi2.Init.CLKPhase = SPI_PHASE_2EDGE; // CPHA = 1
|
|
hspi2.Init.NSS = SPI_NSS_SOFT;
|
|
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
|
|
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
|
|
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
|
hspi2.Init.CRCPolynomial = 10;
|
|
|
|
if (HAL_SPI_Init(&hspi2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
// Double check CS is high after initialization
|
|
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET);//关闭AD7124_CS1
|
|
HAL_GPIO_WritePin(SPI2_CS2_GPIO_Port, SPI2_CS2_Pin, GPIO_PIN_SET);//关闭AD7124_CS2
|
|
HAL_GPIO_WritePin(AD7124_SYNC_GPIO_Port, AD7124_SYNC_Pin, GPIO_PIN_SET);//关闭AD7124_SYNC
|
|
HAL_Delay(1);
|
|
}
|
|
|
|
void dac8568_spi_init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
// 使能SPI3和GPIO时钟
|
|
__HAL_RCC_SPI3_CLK_ENABLE();
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
|
|
// 配置SPI3引脚 (PC10-SCK, PC12-MOSI)
|
|
GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_12;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
// 配置CS引脚 (PD15)
|
|
GPIO_InitStruct.Pin = DAC8568_CS_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP; // 添加上拉
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
// 配置LDAC引脚 (PD14)
|
|
GPIO_InitStruct.Pin = DAC8568_LD_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP; // 添加上拉
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
// 配置SPI3
|
|
hspi3.Instance = SPI3;
|
|
hspi3.Init.Mode = SPI_MODE_MASTER;
|
|
hspi3.Init.Direction = SPI_DIRECTION_2LINES;
|
|
hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
hspi3.Init.CLKPolarity = SPI_POLARITY_HIGH; // CPOL = 0
|
|
hspi3.Init.CLKPhase = SPI_PHASE_1EDGE; // CPHA = 0
|
|
hspi3.Init.NSS = SPI_NSS_SOFT;
|
|
hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; // 提高SPI速度
|
|
hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
|
|
hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
|
hspi3.Init.CRCPolynomial = 10;
|
|
|
|
if (HAL_SPI_Init(&hspi3) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
// 初始化CS和LDAC引脚状态
|
|
HAL_GPIO_WritePin(DAC8568_CS_GPIO_Port, DAC8568_CS_Pin, GPIO_PIN_SET); // CS初始为高电平
|
|
HAL_GPIO_WritePin(DAC8568_LD_GPIO_Port, DAC8568_LD_Pin, GPIO_PIN_SET); // LDAC初始为高电平
|
|
|
|
// 等待一段时间确保DAC8568稳定
|
|
HAL_Delay(10);
|
|
}
|
|
|
|
void dac161s997_spi_init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
// Enable clocks
|
|
__HAL_RCC_SPI1_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
|
|
// Configure SPI1 pins for DAC161S997
|
|
// SCK (PB3), MISO (PB4), MOSI (PB5)
|
|
GPIO_InitStruct.Pin = DAC1_SCK_Pin | DAC1_MISO_Pin | DAC1_MOSI_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; // SPI1 alternate function
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
// CS (PB6) - Configure as output
|
|
GPIO_InitStruct.Pin = DAC1_CS_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
HAL_GPIO_Init(DAC1_CS_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
// Ensure CS is high initially
|
|
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET);
|
|
|
|
// Configure SPI1 for DAC161S997
|
|
hspi1.Instance = SPI1;
|
|
hspi1.Init.Mode = SPI_MODE_MASTER;
|
|
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
|
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; // CPOL = 0
|
|
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; // CPHA = 0
|
|
hspi1.Init.NSS = SPI_NSS_SOFT;
|
|
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; // 适中的时钟频率
|
|
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
|
|
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
|
hspi1.Init.CRCPolynomial = 10;
|
|
|
|
if (HAL_SPI_Init(&hspi1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
void ad7124_cs_on(uint8_t device_num)
|
|
{
|
|
if (device_num == 1) {
|
|
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_RESET); // 使能AD7124_CS1
|
|
HAL_GPIO_WritePin(SPI2_CS2_GPIO_Port, SPI2_CS2_Pin, GPIO_PIN_SET); // 关闭AD7124_CS2
|
|
} else {
|
|
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET); // 关闭AD7124_CS1
|
|
HAL_GPIO_WritePin(SPI2_CS2_GPIO_Port, SPI2_CS2_Pin, GPIO_PIN_RESET); // 使能AD7124_CS2
|
|
}
|
|
HAL_Delay(1);//延时1ms
|
|
}
|
|
|
|
void ad7124_cs_off(uint8_t device_num)
|
|
{
|
|
if (device_num == 1) {
|
|
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET); // 关闭AD7124_CS1
|
|
} else {
|
|
HAL_GPIO_WritePin(SPI2_CS2_GPIO_Port, SPI2_CS2_Pin, GPIO_PIN_SET); // 关闭AD7124_CS2
|
|
}
|
|
HAL_Delay(1);
|
|
}
|
|
|
|
void dac161s997_cs_on(chip_type_e dac_num)
|
|
{
|
|
switch (dac_num)
|
|
{
|
|
case DAC161S997:
|
|
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_RESET);
|
|
break;
|
|
case DAC8568:
|
|
//HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_RESET);
|
|
break;
|
|
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
void dac161s997_cs_off(uint8_t dac_num)
|
|
{
|
|
switch (dac_num)
|
|
{
|
|
case DAC161S997:
|
|
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET);
|
|
break;
|
|
case DAC8568:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
void board_spi_init(chip_type_e chip_type)
|
|
{
|
|
switch(chip_type) {
|
|
|
|
case AD7124_1:
|
|
ad7124_spi_init();
|
|
break;
|
|
case AD7124_2:
|
|
ad7124_spi_init();
|
|
break;
|
|
case DAC161S997:
|
|
dac161s997_spi_init();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void board_spi_cs_on(chip_type_e chip_type)
|
|
{
|
|
switch(chip_type) {
|
|
case AD7124_1:
|
|
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_RESET); // AD7124_CS1打开
|
|
HAL_Delay(1);
|
|
break;
|
|
case DAC161S997:
|
|
dac161s997_cs_on(DAC161S997);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void board_spi_cs_off(chip_type_e chip_type)
|
|
{
|
|
switch(chip_type) {
|
|
case AD7124_1:
|
|
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET); // AD7124_CS1关闭
|
|
HAL_Delay(1);
|
|
break;
|
|
case DAC161S997:
|
|
dac161s997_cs_off(DAC161S997);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Add helper function for AD7124 SPI communication
|
|
int32_t ad7124_spi_transmit_receive(uint8_t *data, uint8_t bytes, uint8_t device_num)
|
|
{
|
|
ad7124_cs_on(device_num);
|
|
int32_t result = spi_transmit_receive(&hspi2, data, spi_rx_buffer, bytes);
|
|
ad7124_cs_off(device_num);
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|