#include "bsp_spi_dac8760_driver.h"
#include <stdio.h>
#include "myLogger.h"
DAC_Ctrl_u dac_ctrl;
DAC_Config_u dac_config;
static int8_t dac8760_spi_init(void)
{
return 0;
}
static int8_t dac8760_cs_init(void)
{
return 0;
}
static void Dac8760Select(void)
{
HAL_GPIO_WritePin(DAC8760_CS_GPIO_PORT, DAC8760_CS_GPIO_PIN, GPIO_PIN_RESET);
}
static void Dac8760Deselect(void)
{
HAL_GPIO_WritePin(DAC8760_CS_GPIO_PORT, DAC8760_CS_GPIO_PIN, GPIO_PIN_SET);
}
/**
* @brief Dac8760TxRx 使用SPI发送/接收数据
* @param pTxData - 要发送的数据
* Size - 数据长度
* Timeout - 超时时间(单位ms)
* pRxData - 接收缓冲区
* @retval 0 - 成功, (-1)-失败
* @attention
* (注意这个函数没有设置片选信号)
*/
static int Dac8760TxRx(uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
{
if (HAL_OK == HAL_SPI_TransmitReceive(&HSPI_DAC8760, pTxData, pRxData, Size, Timeout))
return 0;
else
return -1;
}
/**
* @brief Dac8760Tx 使用SPI发送数据
* @param pTxData - 要发送的数据
* Size - 数据长度
* Timeout - 超时时间(单位ms)
* @retval 0 - 成功, (-1)-失败
* @attention
* (注意这个函数没有设置片选信号)
*/
static int Dac8760Tx(uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
{
if (HAL_OK == HAL_SPI_Transmit(&HSPI_DAC8760, pTxData, Size, Timeout))
return 0;
else
return -1;
}
/**
* @brief Dac8760Rx 使用SPI读取数据
* @param pRxData - 接收缓冲区
* Size - 数据长度
* Timeout - 超时时间(单位ms)
* @retval 0 - 成功, (-1)-失败
* @attention
* (注意这个函数没有设置片选信号)
*/
static int Dac8760Rx(uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
{
if (HAL_OK == HAL_SPI_Receive(&HSPI_DAC8760, pRxData, Size, Timeout))
return 0;
else
return -1;
}
/**
* @brief Dac8760Write 写函数
* @param addr - 写哪个地址, 写操作
* data - 写数据
* @retval 0 - 成功, 非0 - 失败
* @attention
*
*/
int Dac8760Write(uint8_t addr, uint16_t data)
{
uint8_t tmpbuf[3];
int ret;
tmpbuf[0] = addr;
tmpbuf[1] = (data >> 8) & 0xff;
tmpbuf[2] = (data >> 0) & 0xff;
Dac8760Select();
ret = Dac8760Tx(tmpbuf, 3, DAC8760_TIMEOUT);
Dac8760Deselect();
return ret;
}
/**
* @brief Dac8760Read 读函数
* @param addr_r - 读哪个地址
* data - 用来保存读到的数据
* @retval 0 - 成功, 非0 - 失败
* @attention
*
*/
int Dac8760Read(uint8_t addr_r, uint16_t *data)
{
uint8_t tmpbuf[3], rcvbuf[3];
int ret;
tmpbuf[0] = W_ADDR_READ;
tmpbuf[1] = 0x00;
tmpbuf[2] = addr_r;
Dac8760Select();
/* 发送写命令 */
ret = Dac8760Tx(tmpbuf, 3, DAC8760_TIMEOUT);
if (ret)
{
Dac8760Deselect();
return ret;
}
Dac8760Deselect();
HAL_Delay(1);
Dac8760Select();
ret = Dac8760Rx(rcvbuf, 3, DAC8760_TIMEOUT);
Dac8760Deselect();
*data = rcvbuf[1];
*data <<= 8;
*data |= rcvbuf[2];
return ret;
}
int Dac8760_WriteCtrl(DAC_Ctrl_u *dac_data)
{
uint8_t tmpbuf[3];
int ret;
tmpbuf[0] = W_ADDR_CTRL;
tmpbuf[1] = (dac_data->all >> 8) & 0xff;
tmpbuf[2] = (dac_data->all >> 0) & 0xff;
Dac8760Select();
ret = HAL_SPI_Transmit(&HSPI_DAC8760, tmpbuf, 3, DAC8760_TIMEOUT);
Dac8760Deselect();
return ret;
}
int Dac8760_WriteConfig(DAC_Config_u *dac_data)
{
uint8_t tmpbuf[3];
int ret;
tmpbuf[0] = R_ADDR_CONFIG;
tmpbuf[1] = (dac_data->all >> 8) & 0xff;
tmpbuf[2] = (dac_data->all >> 0) & 0xff;
Dac8760Select();
ret = Dac8760Tx(tmpbuf, 3, DAC8760_TIMEOUT);
Dac8760Deselect();
return ret;
}
/**
* @brief Dac8760Read 读函数
* @param addr_r - 读哪个地址
* @retval data - 读到的数据
* @attention
*/
uint16_t Dac8760_Read(uint8_t addr_r)
{
uint8_t tmpbuf[3], rcvbuf[3];
uint16_t data;
tmpbuf[0] = W_ADDR_READ;
tmpbuf[1] = 0x00;
tmpbuf[2] = addr_r;
Dac8760Select();
Dac8760Tx(tmpbuf, 3, DAC8760_TIMEOUT);
Dac8760Deselect();
HAL_Delay(1);
Dac8760Select();
Dac8760Rx(rcvbuf, 3, DAC8760_TIMEOUT);
Dac8760Deselect();
data = rcvbuf[1] << 8;
data |= rcvbuf[2];
return data;
}
void Dac8760_Reset(void)
{
Dac8760Write(W_ADDR_RESET,0x0001); // 0x0001执行软件复位
}
void DAC8760_SetRange(uint8_t Range)
{
dac_ctrl.bit.outen = 1; // 由range确定输出范围
dac_ctrl.bit.range = Range; // 设置电压/电流的输出范围
Dac8760_WriteCtrl(&dac_ctrl);
}
int DAC8760_Output(double percent)
{
uint16_t value = 65535*percent;
return Dac8760Write(W_ADDR_DATA,value);
}
void DAC8760_Init(void)
{
/* 1 软件复位*/
Dac8760_Reset();
/* 2 片选信号引脚配置,已在gpio.c中被配置为输出引脚*/
dac8760_cs_init();
/* 3 SPI配置,SPI已在spi.c中配置好 */
dac8760_spi_init();
/* 4 配置 */
dac_config.bit.dual_outen = 0; // 双输出失能
dac_config.bit.apd = 0; // 备用电源关闭
Dac8760_WriteConfig(&dac_config);
DAC8760_SetRange(V_0_P10);
}
void Dac8760Test(void)
{
int ret;
// uint16_t temp;
dac_ctrl.all = Dac8760_Read(R_ADDR_CTRL);
LOG("dac_ctrl = %d\n", dac_ctrl.all);
dac_config.all = Dac8760_Read(R_ADDR_CONFIG);
LOG("dac_config = %d\n", dac_config.all);
// ret = Dac8760Read(R_ADDR_CTRL, &temp);
// LOG("ret = %d\t temp = %d\n", ret, temp);
ret = DAC8760_Output(0);
LOG("ret = %d\t value = 0\n", ret);
HAL_Delay(5000);
ret = DAC8760_Output(0.5);
LOG("ret = %d\t value = 5V\n", ret);
HAL_Delay(5000);
ret = DAC8760_Output(1);
LOG("ret = %d\t value = 10V\n", ret);
HAL_Delay(5000);
}