/*
Analogysemi xutong
2023/02/15
*/
#include "ADX516.h"
#include "cmsis_os2.h"
static void ADX516_GPIO_Init(void);
void ADX516_READ(uint32_t len, uint8_t *wdata, uint8_t *rdata);
void ADX516_WRITE(uint32_t len, uint8_t *wdata);
// 初始化ADX516
void ADX516_Init(void)
{
// ADX516_GPIO_Init();
// START_L;
// XTAL_H;
// ADX516_PWD_L;
osDelay(1);
// ADX516_PWD_H;
ADX516_REST_L;
osDelay(150);
ADX516_REST_H;
osDelay(1);
ADX516_REST_H;
}
static void ADX516_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10 | GPIO_PIN_11, GPIO_PIN_SET);
GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_15 | GPIO_PIN_14;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10 | GPIO_PIN_15 | GPIO_PIN_14, GPIO_PIN_SET);
GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11 | GPIO_PIN_12, GPIO_PIN_SET);
GPIO_InitStruct.Pin = GPIO_PIN_7;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_7, GPIO_PIN_SET);
GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
}
// 获取芯片的ID号
// Adx516 default Value 0x20
uint8_t ADX516_GetChipID(void)
{
uint8_t ID;
ID = ADX516_GetReg(ADX516_STATUS);
return ID;
}
// 获取芯片寄存器值
uint8_t ADX516_GetReg(uint8_t Rreg)
{
uint8_t TxData[3];
uint8_t RxData[3];
// 读取命令,
TxData[0] = ADX516_RREG | Rreg;
// 选择要读取的寄存器
TxData[1] = 0x00;
// 读取单个寄存器
TxData[2] = 0X00;
// 开始读取
ADX516_READ(3, TxData, RxData);
// 返回寄存器值
return RxData[2];
}
// 写单个芯片寄存器值
void ADX516_WriteReg(uint8_t Wreg, uint8_t Data)
{
uint8_t TxData[3];
// 写入命令,
TxData[0] = ADX516_WREG | Wreg;
// 选择要写入的的寄存器
TxData[1] = 0x00;
// 读取写入寄存器
TxData[2] = Data;
ADX516_WRITE(3, TxData);
}
// 写多个芯片寄存器值
// StartReg 起始寄存器地址
// RegNum 要写的寄存器数量
//*Data 寄存器参数传入
void ADX516_WriteMultiReg(uint8_t StartReg, uint8_t RegNum, uint8_t *Data)
{
uint8_t TxData[32];
uint8_t i;
// 写入命令,
TxData[0] = ADX516_WREG | StartReg;
// 选择要写入的的寄存器
TxData[1] = RegNum - 1;
for (i = 0; i < RegNum; i++)
{
// 将指针数据赋值给Txdata
TxData[2 + i] = *Data;
// 指向下个数据地址
Data++;
}
// 最终发送多少个数据
ADX516_WRITE(2 + RegNum, TxData);
}
// 读多个芯片寄存器值
// StartReg 起始寄存器地址
// RegNum 要写的寄存器数量
//*Data 寄存器参数传出
void ADX516_ReadMultiReg(uint8_t StartReg, uint8_t RegNum, uint8_t *Data)
{
// 读取寄存器
uint8_t TxData[3];
// 读取命令,
TxData[0] = ADX516_RREG | StartReg;
// 选择要写入的的寄存器
TxData[1] = RegNum - 1;
// 最终接收多少个数据
ADX516_READ(2 + RegNum, TxData, Data);
}
// 打印所有寄存器地址
void ADX516_GetAllReg(void)
{
uint8_t i, j;
for (i = 0; i < 20; i++)
{
j = ADX516_GetReg(i);
// 打印所有寄存器值
// rt_kprintf("reg addr: %d reg default Value: %x\r\n",i,j);
}
}
// 发送ADX516 数据指令 如SDATAC或者RDATAC
// 详情见头文件command define部分
void ADX516_Command(uint8_t Com)
{
uint8_t TxData[] = {Com};
ADX516_WRITE(1, TxData);
}
// 设定通道并且获取adc数据
// 获取ADC的值
void ADX516_GetADC_Value(uint8_t Ch, uint32_t *Rxdata,uint32_t times)
{
uint8_t TxData[] = {ADX516_RDATA};
ADX516_Command(ADX516_SDATAC);
ADX516_WriteReg(ADX516_MUX, Ch << 4 | 0X0F);
ADX516_Command(ADX516_SYNC);
ADX516_Command(ADX516_WAKEUP);
// rt_thread_mdelay(1);
osDelay(1);
for (uint32_t i =0;i<times;i++){
ADX516_READ(4, TxData, (uint8_t *)&Rxdata[i]);
Rxdata[i] &=0x00FFFFFF;
HAL_Delay(3);
}
}
void ADX516_READ(uint32_t len, uint8_t *wdata, uint8_t *rdata)
{
uint8_t buff;
uint16_t timeout = 10;
uint8_t RES = 0;
ADX516_CS_H;
ADX516_CLK_L;
HAL_Delay(1);
// ADX516_CS_L;
// while(ADX516_DRDY);
ADX516_CS_L;
while (timeout--)
;
for (uint32_t Num = 0; Num < len; Num++)
{
buff = wdata[Num];
RES = 0;
for (int i = 7; i >= 0; i--)
{
ADX516_CLK_H;
if (buff & (0x01 << i))
ADX516_DIN_H;
else
ADX516_DIN_L;
timeout = 2;
while (timeout--)
;
if(ADX516_SDO)
RES |= (0x1 << i);
timeout = 3;
while (timeout--)
;
ADX516_CLK_L;
timeout = 8;
while (timeout--)
;
}
rdata[len-Num-1]=RES;
}
}
void ADX516_WRITE(uint32_t len, uint8_t *wdata)
{
uint8_t buff;
uint16_t timeout = 10;
uint32_t RES = 0;
ADX516_CS_H;
ADX516_CLK_L;
HAL_Delay(1);
ADX516_CS_L;
// while(ADX516_SDO);
// ADX516_CS_L;
while (timeout--)
;
for (uint32_t Num = 0; Num < len; Num++)
{
buff = wdata[Num];
for (int i = 7; i >= 0; i--)
{
if (buff & (0x01 << i))
ADX516_DIN_H;
else
ADX516_DIN_L;
ADX516_CLK_H;
timeout = 5;
while (timeout--)
;
ADX516_CLK_L;
timeout = 5;
while (timeout--)
;
}
}
HAL_Delay(1);
ADX516_CS_H;
}
/*
Analogysemi xutong
2023/02/15
*/
#include "main.h"
#ifndef __ADX516_H
#define __ADX516_H
/*
device 需要先写入SDTAC 启动寄存器写入才可以对寄存器进行操作
在此之前需要检查PWDN以及Reset引脚的状态是否为 高
在操作完寄存器后,需要发送RDATAC 才能连续 读取数据
bsp_lpspi 的接口函数如下:
Lpspi_Write(uint8_t datasize,uint8_t *data);
Lpspi_Read(uint8_t datasize,uint8_t *data,uint8_t *Rxdata);
注意:
Lpspi的时序并不一定满足ADX516的配置,以下配置经过验证
lpspi_master_config_t config;
LPSPI_MasterGetDefaultConfig(&config);
config.whichPcs=kLPSPI_Pcs0;
config.baudRate=50000;
config.betweenTransferDelayInNanoSec = 1000000000 /500;
config.pcsToSckDelayInNanoSec=1000000000 /500;
config.lastSckToPcsDelayInNanoSec=1000000000 /500;
config.cpha=kLPSPI_ClockPhaseSecondEdge;
LPSPI_MasterInit(LPSPI3, &config, LPSPI3_CLOCK_FREQ);
*/
/*command define*/
#define ADX516_REF 2.5
#define ADX516_PGA 1
#define ADX516_LSB (2*ADX516_REF)/(ADX516_PGA*8388607)
#define ADX516_WAKEUP 0x00
#define ADX516_RDATA 0x01
#define ADX516_RDATAC 0x03
#define ADX516_SDATAC 0x0F
#define ADX516_RREG 0x10
#define ADX516_WREG 0X50
#define ADX516_SELFCAL 0XF0
#define ADX516_SELFOCAL 0XF1
#define ADX516_SELFGCAL 0XF2
#define ADX516_SYSOCAL 0XF3
#define ADX516_SYSGCAL 0XF4
#define ADX516_SYNC 0XFC
#define ADX516_STANDBY 0xFD
#define ADX516_RESET 0xFE
//RREG Read n nnnn registers starting at address r rrrr
//First Byte 001r rrrr (2xh)
//Second Byte 000n nnnn
//n nnnn = number of registers to be read or written – 1. For example, to read or write three registers, set n nnnn = 0 (0010). r rrrr = the
//starting register address for read and write commands
//WREG 同上
/*
寄存器定义
*/
#define ADX516_STATUS 0x00
#define ADX516_MUX 0x01
#define ADX516_ADCON 0x02
#define ADX516_DRATE 0x03
#define ADX516_IO 0x04
#define ADX516_OFC0 0x05
#define ADX516_OFC1 0x06
#define ADX516_OFC2 0x07
#define ADX516_FSC0 0x08
#define ADX516_FSC1 0x09
#define ADX516_FSC2 0x0A
#define ADX516_CFG0 0x0B
#define ADX516_CFG1 0x0C
#define ADX516_CFG2 0x0D
#define ADX516_CFG3 0x0E
#define ADX516_CFG4 0x0F
#define ADX516_CFG5 0x10
#define ADX516_DEV_CFG 0x11
#define ADX516_MOD_STAT 0x12
#define ADX516_DEV_STAT 0x13
#define ADX516_CMD_STAT 0x14
#define ADX516_CLK_PORT GPIOB
#define ADX516_CLK_PIN GPIO_PIN_15
#define ADX516_REST_PORT GPIOD
#define ADX516_REST_PIN GPIO_PIN_12
//#define ADX516_SYN_PORT GPIOB
//#define ADX516_SYN_PIN GPIO_PIN_14
#define ADX516_CS_PORT GPIOB
#define ADX516_CS_PIN GPIO_PIN_14
#define ADX516_DIN_PORT GPIOD
#define ADX516_DIN_PIN GPIO_PIN_11
#define ADX516_DRDY_PORT GPIOA
#define ADX516_DRDY_PIN GPIO_PIN_11
#define ADX516_DO_PORT GPIOG
#define ADX516_DO_PIN GPIO_PIN_9
#define ADX516_SDO (ADX516_DO_PORT->IDR & ADX516_DO_PIN)
#define ADX516_DRDY HAL_GPIO_ReadPin(ADX516_DRDY_PORT,ADX516_DRDY_PIN)
#define ADX516_PWD_H HAL_GPIO_WritePin(ADX516_SYN_PORT,ADX516_SYN_PIN,GPIO_PIN_SET)
#define ADX516_PWD_L HAL_GPIO_WritePin(ADX516_SYN_PORT,ADX516_SYN_PIN,GPIO_PIN_SET)
#define ADX516_CS_H HAL_GPIO_WritePin(ADX516_CS_PORT,ADX516_CS_PIN,GPIO_PIN_SET)
#define ADX516_CLK_H HAL_GPIO_WritePin(ADX516_CLK_PORT,ADX516_CLK_PIN,GPIO_PIN_SET)
#define ADX516_SYN_H HAL_GPIO_WritePin(ADX516_SYN_PORT,ADX516_SYN_PIN,GPIO_PIN_SET)
#define ADX516_DIN_H HAL_GPIO_WritePin(ADX516_DIN_PORT,ADX516_DIN_PIN,GPIO_PIN_SET)
#define ADX516_REST_H HAL_GPIO_WritePin(ADX516_REST_PORT,ADX516_REST_PIN,GPIO_PIN_SET)
#define ADX516_CS_L HAL_GPIO_WritePin(ADX516_CS_PORT,ADX516_CS_PIN,GPIO_PIN_RESET)
#define ADX516_CLK_L HAL_GPIO_WritePin(ADX516_CLK_PORT,ADX516_CLK_PIN,GPIO_PIN_RESET)
#define ADX516_SYN_L HAL_GPIO_WritePin(ADX516_SYN_PORT,ADX516_SYN_PIN,GPIO_PIN_RESET)
#define ADX516_DIN_L HAL_GPIO_WritePin(ADX516_DIN_PORT,ADX516_DIN_PIN,GPIO_PIN_RESET)
#define ADX516_REST_L HAL_GPIO_WritePin(ADX516_REST_PORT,ADX516_REST_PIN,GPIO_PIN_RESET)
/*
通道配置
channel config
*/
#define ADX516_ADC_CH0
#pragma anon_unions
typedef union
{
uint32_t data;
struct
{
uint8_t BYTE;
uint8_t B_W_R;
uint8_t ADDR;
uint8_t A_W_R;
};
}ADX516_RCOMMAND_TYPE;
typedef union
{
uint32_t data[2];
struct
{
uint16_t IO_DATA;
uint16_t DATA;
uint8_t BYTE;
uint8_t B_W_R;
uint8_t ADDR;
uint8_t A_W_R;
};
}ADX516_WCOMMAND_TYPE;
//写命令
void ADX516_Command(uint8_t Com);
//获取芯片的ID号
extern uint8_t ADX516_GetChipID(void);
//获取寄存器值
uint8_t ADX516_GetReg(uint8_t Rreg);
//获取多个寄存器值
void ADX516_ReadMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);
//写入寄存器值
extern void ADX516_WriteReg(uint8_t Wreg,uint8_t Data);
//写多个寄存器的值
extern void ADX516_WriteMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);
//获取所有寄存器值
extern void ADX516_GetAllReg(void);
//设定通道并且获取ADC的值
extern void ADX516_GetADC_Value(uint8_t Ch, uint32_t *Rxdata,uint32_t times);
//初始化ADX516
extern void ADX516_Init(void);
#endif
ADX516的驱动,eeworld备份
|