#ifndef __ADC_H_
#define __ADC_H_
#ifdef __cplusplus
extern "C" {
#endif
#define ADC_VERF 2.45
#define MAX_INT24 8388607
#define SPI_SCL_PORT GPIOC
#define SPI_SCL_PIN LL_GPIO_PIN_2
#define SPI_SDA_PORT GPIOC
#define SPI_SDA_PIN LL_GPIO_PIN_1
#define SPI_CS_PORT GPIOC
#define SPI_CS_PIN LL_GPIO_PIN_0
#define SPI_IO_GRP1_CLK LL_IOP_GRP1_PERIPH_GPIOC
#define SPI_CS_LOW() LL_GPIO_ResetOutputPin(SPI_CS_PORT, SPI_CS_PIN)
#define SPI_CS_HIGH() LL_GPIO_SetOutputPin(SPI_CS_PORT, SPI_CS_PIN)
#define SPI_SCL_LOW() LL_GPIO_ResetOutputPin(SPI_SCL_PORT, SPI_SCL_PIN)
#define SPI_SCL_HIGH() LL_GPIO_SetOutputPin(SPI_SCL_PORT, SPI_SCL_PIN)
#define SPI_SDA_LOW() LL_GPIO_ResetOutputPin(SPI_SDA_PORT, SPI_SDA_PIN)
#define SPI_SDA_HIGH() LL_GPIO_SetOutputPin(SPI_SDA_PORT, SPI_SDA_PIN)
#define CS1259B_SYS 0x0
#define CS1259B_ADC0 0x1
#define CS1259B_ADC1 0x2
#define CS1259B_ADC2 0x3
#define CS1259B_ADC3 0x4
#define CS1259B_ADC4 0x5
#define CS1259B_ADC5 0x6
#define CS1259B_ADO 0x9
#define CS1259B_ADS 0xA
void CS1259B_init(void);
float CS1259B_read(uint8_t ch);
#ifdef __cplusplus
}
#endif
#endif
#include "hardware.h"
static void SPI_IO_Config(void);
static void CS1259B_Read_Ain0(float* voltage);
static void CS1259B_Read_Ain1(float* voltage);
static void CS1259B_Read_Ain2(float* voltage);
static void CS1259B_Read_Ain3(float* voltage);
static void CS1259B_Read_Ain4(float* voltage);
static void SET_DA_INPUT(void);
static void SET_DA_OUTPUT(void);
static void Send_Byte(uint8_t Data);
static uint8_t Read_Byte(void);
static void Read_Reg(uint8_t addr,uint8_t* RegValue,uint8_t len);
static void Write_Reg(uint8_t addr,uint8_t Value);
static void cs1259b_read_ado(uint8_t* buf);
static int32_t adc_24to32(uint8_t *padc);
static void adc_read_voltage(uint8_t gain, float* voltage);
void CS1259B_init(void)
{
SPI_IO_Config();
Write_Reg(CS1259B_ADC1, 0x80);
Write_Reg(CS1259B_ADC3, 0x04);
Write_Reg(CS1259B_ADC4, 0x41);
Write_Reg(CS1259B_ADC5, 0x00);
}
float CS1259B_read(uint8_t ch)
{
float ADC_Value=0.00;
switch(ch)
{
case 0:
CS1259B_Read_Ain0(&ADC_Value);
break;
case 1:
CS1259B_Read_Ain1(&ADC_Value);
break;
case 2:
CS1259B_Read_Ain2(&ADC_Value);
break;
case 3:
CS1259B_Read_Ain3(&ADC_Value);
break;
case 4:
CS1259B_Read_Ain4(&ADC_Value);
break;
default:
break;
}
return ADC_Value;
}
static void SPI_IO_Config(void)
{
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
LL_IOP_GRP1_EnableClock(SPI_IO_GRP1_CLK);
GPIO_InitStruct.Pin = SPI_CS_PIN;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(SPI_CS_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SPI_SDA_PIN;
LL_GPIO_Init(SPI_SDA_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SPI_SCL_PIN;
LL_GPIO_Init(SPI_SCL_PORT, &GPIO_InitStruct);
}
static void SET_DA_INPUT(void)
{
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
LL_IOP_GRP1_EnableClock(SPI_IO_GRP1_CLK);
GPIO_InitStruct.Pin = SPI_SDA_PIN;
GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(SPI_SDA_PORT, &GPIO_InitStruct);
}
static void SET_DA_OUTPUT(void)
{
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
LL_IOP_GRP1_EnableClock(SPI_IO_GRP1_CLK);
GPIO_InitStruct.Pin = SPI_SDA_PIN;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(SPI_SDA_PORT, &GPIO_InitStruct);
}
static void Send_Byte(uint8_t Data)
{
uint8_t i=0;
SET_DA_OUTPUT();
for(i=0;i<8;i++)
{
SPI_SCL_LOW();
if(Data&0x80)
{
SPI_SDA_HIGH();
}
else
{
SPI_SDA_LOW();
}
Data<<=1;
SPI_SCL_HIGH();
}
SPI_SCL_LOW();
}
static uint8_t Read_Byte(void)
{
uint8_t i=0;
uint8_t data=0;
for(i=0;i<8;i++)
{
SPI_SCL_LOW();
SPI_SCL_HIGH();
data<<=1;
if(LL_GPIO_IsInputPinSet(SPI_SDA_PORT, SPI_SDA_PIN))
data++;
}
SPI_SCL_LOW();
return data;
}
static void Read_Reg(uint8_t addr,uint8_t* RegValue,uint8_t len)
{
uint8_t i=0;
SPI_CS_LOW();
SET_DA_OUTPUT();
Send_Byte(addr&0x7F);
SET_DA_INPUT();
for(i=0;i<len;i++)
{
*RegValue=Read_Byte();
RegValue++;
}
SPI_CS_HIGH();
}
static void Write_Reg(uint8_t addr,uint8_t Value)
{
SPI_CS_LOW();
SET_DA_OUTPUT();
Send_Byte(addr|0x80);
Send_Byte(Value);
SPI_CS_HIGH();
}
static void cs1259b_read_ado(uint8_t* buf)
{
uint8_t buffer[1];
Read_Reg(CS1259B_ADO, &buf[0], 3);
Read_Reg(CS1259B_ADS, buffer, 1);
while (!(buffer[0]&0x80))
{
Read_Reg(CS1259B_ADO, &buf[0], 3);
Read_Reg(CS1259B_ADS, buffer, 1);
}
}
static int32_t adc_24to32(uint8_t *padc)
{
uint32_t value = (uint32_t)*(uint32_t *)padc;
value = __rev((value)) >> 8;
if(*(padc) & 0x80) {
value |= 0xFF000000;
}
return value;
}
static void adc_read_voltage(uint8_t gain, float* voltage)
{
uint8_t buf[3];
int32_t adc_mask;
cs1259b_read_ado(buf);
adc_mask = adc_24to32(buf);
if ((adc_mask & 0x800000) == 0x800000) {
adc_mask = -adc_mask;
}
*voltage = ((float)adc_mask/MAX_INT24)* ADC_VERF / gain;
return;
}
static void CS1259B_Read_Ain0(float* voltage)
{
Write_Reg(CS1259B_ADC0, 0x20);
Write_Reg(CS1259B_SYS, 0x16);
LL_mDelay(100);
adc_read_voltage(1, voltage);
Write_Reg(CS1259B_SYS, 0x10);
}
static void CS1259B_Read_Ain1(float* voltage)
{
Write_Reg(CS1259B_ADC0, 0x21);
Write_Reg(CS1259B_SYS, 0x16);
LL_mDelay(100);
adc_read_voltage(1, voltage);
Write_Reg(CS1259B_SYS, 0x10);
}
static void CS1259B_Read_Ain2(float* voltage)
{
Write_Reg(CS1259B_ADC0, 0x22);
Write_Reg(CS1259B_SYS, 0x16);
LL_mDelay(100);
adc_read_voltage(1, voltage);
Write_Reg(CS1259B_SYS, 0x10);
}
static void CS1259B_Read_Ain3(float* voltage)
{
Write_Reg(CS1259B_ADC0, 0x23);
Write_Reg(CS1259B_SYS, 0x16);
LL_mDelay(100);
adc_read_voltage(1, voltage);
Write_Reg(CS1259B_SYS, 0x10);
}
static void CS1259B_Read_Ain4(float* voltage)
{
Write_Reg(CS1259B_ADC0, 0x1F);
Write_Reg(CS1259B_SYS, 0x16);
LL_mDelay(100);
adc_read_voltage(1, voltage);
Write_Reg(CS1259B_SYS, 0x10);
}
void CS1259B_Test(void)
{
float voltage0,voltage1,voltage2,voltage3,voltage4;
SPI_IO_Config();
while(1)
{
CS1259B_Read_Ain0(&voltage0);
debug_print(0, "CS1259B_Read_Ain0:++%lf\r\n",voltage0);
LL_mDelay(500);
CS1259B_Read_Ain1(&voltage1);
debug_print(0, "CS1259B_Read_Ain1:++++%lf\r\n",voltage1);
LL_mDelay(500);
CS1259B_Read_Ain2(&voltage2);
debug_print(0, "CS1259B_Read_Ain2:+++++++%lf\r\n",voltage2);
LL_mDelay(500);
CS1259B_Read_Ain3(&voltage3);
debug_print(0, "CS1259B_Read_Ain3:++++++++++%lf\r\n",voltage3);
LL_mDelay(500);
CS1259B_Read_Ain4(&voltage4);
debug_print(0, "CS1259B_Read_Ain4:++++++++++++++%lf\r\n",voltage4);
LL_mDelay(500);
}
}