98 lines
4.4 KiB
C
98 lines
4.4 KiB
C
#ifndef __DAC161S997_H__
|
||
#define __DAC161S997_H__
|
||
|
||
#include "main.h"
|
||
#include "user_spi.h"
|
||
#include <stdint.h>
|
||
#include "stm32f4xx_hal.h"
|
||
|
||
// 定义返回值常量
|
||
#define SUCCESS 0
|
||
#define FAILURE (-1)
|
||
|
||
// SPI通信状态定义
|
||
#define SPI_STATE_IDLE 0x00
|
||
#define SPI_STATE_WRITE_START 0x01
|
||
#define SPI_STATE_WRITE_END 0x02
|
||
#define SPI_STATE_READ_START 0x03
|
||
#define SPI_STATE_READ_END 0x04
|
||
#define SPI_STATE_ERROR 0xFF
|
||
|
||
// SPI错误代码定义
|
||
#define SPI_ERR_NONE 0x00
|
||
#define SPI_ERR_TIMEOUT 0x01
|
||
#define SPI_ERR_CS 0x02
|
||
#define SPI_ERR_TRANSMIT 0x03
|
||
#define SPI_ERR_RECEIVE 0x04
|
||
#define SPI_ERR_BUSY 0x05
|
||
|
||
/* Defines ********************************************************************/
|
||
/**
|
||
* @defgroup DAC161S997_REGS
|
||
* @{
|
||
*/
|
||
#define DAC161S997_XFR_REG 0x01 /**< Command: transfer data into register (protected write mode) */
|
||
#define DAC161S997_NOP_REG 0x02 /**< Command: does nothing except resetting the timeout timer */
|
||
#define DAC161S997_PROTECT_REG_WR_REG 0x03 /**< 1 means protected write mode in effect */
|
||
#define DAC161S997_DACCODE_REG 0x04 /**< 16 bit DAC code to be written */
|
||
#define DAC161S997_ERR_CONFIG_REG 0x05 /**< Configuration of error states */
|
||
#define DAC161S997_ERR_LOW_REG 0x06 /**< DAC code for the <4mA alarm level */
|
||
#define DAC161S997_ERR_HIGH_REG 0x07 /**< DAC code for the >20mA alarm level */
|
||
#define DAC161S997_RESET_REG 0x08 /**< Command: reset the chip */
|
||
#define DAC161S997_STATUS_REG 0x09 /**< Status of the chip */
|
||
|
||
#define DAC161S997_SPI SPI1
|
||
|
||
// DAC芯片类型定义
|
||
typedef enum {
|
||
DAC_TYPE_161S997 = 0, // DAC161S997芯片
|
||
DAC_TYPE_8568 = 1 // DAC8568芯片
|
||
} dac_type_t;
|
||
|
||
// 定义DAC芯片编号(只有一块DAC161S997)
|
||
#define DAC161S997_CHIP_NUM 1
|
||
|
||
// // DAC161S997寄存器地址定义
|
||
// #define DAC161S997_RANGE_REG 0x01 // 输出范围寄存器
|
||
// #define DAC161S997_OUTPUT_ENABLE_REG 0x02 // 输出使能寄存器
|
||
// #define DAC161S997_ERR_LOW_REG 0x03 // 错误阈值寄存器
|
||
// #define DAC161S997_ERR_CONFIG_REG 0x04 // 错误配置寄存器
|
||
|
||
// 调试变量声明
|
||
extern volatile uint8_t dac161s997_init_status; // 初始化状态 (0=成功, 0xFF=未初始化)
|
||
extern volatile uint8_t dac161s997_error_count; // 错误计数
|
||
extern volatile uint8_t dac161s997_test_step; // 当前测试步骤
|
||
extern volatile uint8_t dac161s997_operation_status; // 操作状态 (0=正常, 1=错误)
|
||
extern volatile uint16_t dac161s997_config_reg; // 配置寄存器值
|
||
extern volatile uint16_t dac161s997_status_reg; // 状态寄存器值
|
||
extern volatile uint16_t dac161s997_data_reg; // 数据寄存器值
|
||
extern volatile float dac161s997_current_value; // 当前输出电流值
|
||
extern volatile uint8_t dac161s997_direction; // 电流变化方向 (0=递减, 1=递增)
|
||
extern volatile float dac161s997_target_current; // 目标电流值
|
||
extern volatile uint16_t dac161s997_dac_code; // DAC代码值
|
||
extern volatile uint8_t dac161s997_cmd_status; // 命令执行状态
|
||
extern volatile uint8_t dac161s997_spi_phase; // SPI通信阶段
|
||
extern volatile uint8_t dac161s997_last_error; // 最后一次错误代码
|
||
extern volatile uint8_t dac161s997_retry_count; // 重试计数
|
||
extern volatile uint8_t dac161s997_verify_status; // 验证状态
|
||
|
||
// 添加SPI通信调试变量声明
|
||
extern volatile uint8_t dac161s997_spi_state; // SPI当前状态
|
||
extern volatile uint8_t dac161s997_spi_error; // SPI错误代码
|
||
extern volatile uint32_t dac161s997_spi_timeout; // SPI超时计数
|
||
extern volatile uint8_t dac161s997_spi_buffer[4]; // SPI数据缓冲区
|
||
extern volatile uint8_t dac161s997_spi_bytes; // SPI传输字节数
|
||
extern volatile uint8_t dac161s997_cs_state; // CS引脚状态
|
||
extern volatile uint32_t dac161s997_last_spi_time; // 上次SPI操作时间
|
||
extern volatile uint32_t dac161s997_spi_retry_count; // SPI重试次数
|
||
|
||
// 函数声明
|
||
void dac161s997_write_reg(dac_type_t dac_type, uint8_t reg, uint16_t data);
|
||
int32_t dac161s997_read_reg(dac_type_t dac_type, uint8_t reg, uint16_t *data);
|
||
int dac161s997_init(dac_type_t dac_type, float current);
|
||
void dac161s997_output(dac_type_t dac_type, float current);
|
||
void dac161s997_spi_init(void);
|
||
int dac161s997_diagnose(dac_type_t dac_type); // 新增诊断函数声明
|
||
|
||
#endif
|