AD105怎么读PCAP01 CDC 芯片 GD32F105RB驱动

新闻资讯2026-04-20 22:36:01

#include "pcap01.h"

/* firmware buffer */
uint8_t firmware[928] = {0};

/* timmet variable */
#define IIC_TIMEOUT ((uint32_t)0x1000)
uint32_t timeout = IIC_TIMEOUT;

/* BSY bit clear wait time out */
#define WAIT_BSY_CLR() timeout = IIC_TIMEOUT;
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY))
if(0 == (timeout--))
return BSY_ERR
/* SBSEND bit set wait time out */
#define WAIT_SBSEND_SET() timeout = IIC_TIMEOUT;
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND))
if(0 == (timeout--))
return SBSEND_ERR
/* ADDSEND bit set wait time out */
#define WAIT_ADDSEND_SET() timeout = IIC_TIMEOUT;
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND))
if(0 == (timeout--))
return ADDSEND_ERR
/* RBNE bit set wait time out */
#define WAIT_RBNE_SET() timeout = IIC_TIMEOUT;
while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE))
if(0 == (timeout--))
return RBNE_ERR
/* TBE bit set wait time out */
#define WAIT_TBE_SET() timeout = IIC_TIMEOUT;
while(!i2c_flag_get(I2C0, I2C_FLAG_TBE))
if(0 == (timeout--))
return TBE_ERR
/* TBC bit set wait time out */
#define WAIT_BTC_SET() timeout = IIC_TIMEOUT;
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC))
if(0 == (timeout--))
return BTC_ERR
/* STOP bit clear wait time out */
#define WAIT_STOP_CLR() timeout = IIC_TIMEOUT;
while(I2C_CTL0(I2C0) & 0x0200)
if(0 == (timeout--))
return STOP_ERR

/* PCAP01 slave address define */
#define PCAP01ADDR 0xA0

/**
* @brief Print the error infomation of I2C communication
* @param err: error type
* @retval NONE
*/
void PCAP01_Error(I2CERR err)
{
switch(err)
{
case NONE_ERR:
// printf("NONE ERR.
");
break;

case BSY_ERR:
printf("BSY err!
");
break;

case SBSEND_ERR:
printf("SBSEND err!
");
break;

case ADDSEND_ERR:
printf("ADDRSEND err!
");
break;

case RBNE_ERR:
printf("RBNE err!
");
break;

case TBE_ERR:
printf("TBE err!
");
break;

case STOP_ERR:
printf("STOP err!
");
break;
}
}

/*!
brief enable the peripheral clock
param[in] none
param[out] none

etval none
*/
void rcu_config()
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable alternate function clock */
rcu_periph_clock_enable(RCU_AF);
/* enable I2C0 clock */
rcu_periph_clock_enable(RCU_I2C0);
}

/*!
brief cofigure the GPIO ports
param[in] none
param[out] none

etval none
*/
void gpio_config()
{
/* I2C0 GPIO ports */
/* connect PB6 to I2C0_SCL */
gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
/* connect PB7 to I2C0_SDA */
gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
}

/*!
brief cofigure the I2C0 interfaces
param[in] none
param[out] none

etval none
*/
void i2c_config()
{
/* I2C clock configure */
i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
/* I2C address configure */
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, PCAP01ADDR);
/* enable I2C0 */
i2c_enable(I2C0);
/* enable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
}

/**
* @brief Initializes the PCAP01_I2C.
* @param None
* @retval None
*/
void PCAP01_I2CInit()
{
/* RCU config */
rcu_config();
/* GPIO config */
gpio_config();
/* I2C config */
i2c_config();
}

/**
* @brief Master send 8-bit OP code to PCAP01 through I2C
* @param op: 8bit cp code
* 0x88 reset
* 0x8A initial
* 0x8c start C measure once
* 0x84 stop write-to-OTP
* 0x8E start R measure once
* @retval I2C error
*/
I2CERR PCAP01_8bitOPCodeWrite(uint8_t op)

/**
* @brief Master send 24-bit OP code to PCAP01 through I2C
* @param op: 8bit op code
* 0x90 write SRAM
* 0xA0 write OTP
* @param addr: address of SRAM/OTP to write
* @param val: data to write
* @retval I2C error
*/
I2CERR PCAP01_24bitOPCodeWrite(uint8_t op, uint16_t addr, uint8_t val)

/**
* @brief Master get specific address register value by 24-bit OP code
* @param op: 8bit op code
* 0x01 read SRAM
* 0x02 read OTP
* @param addr: address of SRAM/OTP to read
* @param val: pointer to read buffer
* @retval I2C error
*/
I2CERR PCAP01_24bitOPCodeRead(uint8_t op, uint16_t addr, uint8_t* val)
{
uint16_t temp = ((uint16_t)op << 8) | addr;
/* Please refer to DB_PCap01Ax_0301_en.pdf Table 4-2 */
uint8_t byte2 = (uint8_t)((temp & 0xff00) >> 8);
uint8_t byte1 = (uint8_t)(temp & 0x00ff);
uint8_t byte0;

/* wait until I2C bus is idle */
WAIT_BSY_CLR();

/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
WAIT_SBSEND_SET();

/* send slave address to I2C bus */
i2c_master_addressing(I2C0, PCAP01ADDR, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
WAIT_ADDSEND_SET();
/* clear ADDSEND bit */
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);

/* wait until the transmit data buffer is