GD32F303,实用模拟i2c模版,驱动4路模拟i2c
public_i2c.c
#include "public_i2c.h"
#define NOP(); __NOP();
static void Delay_us(uint32_t ulTime)
{
uint32_t i;
while (ulTime--) {
for (i = 0; i < 10; i++)
{
NOP();NOP();NOP();
}
}
}
static void Delay_ms(uint32_t ulTime)
{
while (ulTime--) {
MCU_Delay_us(1000);
}
}
/*********************************************************************************************************
** Function name: public_Start_IIC
** Descriptions:
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
static void public_Start_IIC(int cnt)
{
switch(cnt){
case 0:
Sda_A_out;
Sda_A_HIGH;
Clk_A_HIGH ;
Delay_us(12);
Sda_A_LOW;
Delay_us(6);
Clk_A_LOW;
Delay_us(6);
break;
case 1:
Sda_B_out;
Sda_B_HIGH;
Clk_B_HIGH ;
Delay_us(12);
Sda_B_LOW;
Delay_us(6);
Clk_B_LOW;
Delay_us(6);
break;
case 2:
Sda_C_out;
Sda_C_HIGH;
Clk_C_HIGH ;
Delay_us(12);
Sda_C_LOW;
Delay_us(6);
Clk_C_LOW;
Delay_us(6);
break;
case 3:
Sda_D_out;
Sda_D_HIGH;
Clk_D_HIGH ;
Delay_us(12);
Sda_D_LOW;
Delay_us(6);
Clk_D_LOW;
Delay_us(6);
break;
default:
;break;
}
}
/*********************************************************************************************************
** Function name: public_Start_IIC
** Descriptions:
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
static void public_Stop_IIC(int cnt)
{
switch(cnt){
case 0:
Sda_A_out;
Delay_us(6);
Clk_A_LOW;
Sda_A_LOW;
Delay_us(6);
Clk_A_HIGH;
Delay_us(6);
Sda_A_HIGH;
Delay_us(6);
break;
case 1:
Sda_B_out;
Delay_us(6);
Clk_B_LOW;
Sda_B_LOW;
Delay_us(6);
Clk_B_HIGH;
Delay_us(6);
Sda_B_HIGH;
Delay_us(6);
break;
case 2:
Sda_C_out;
Delay_us(6);
Clk_C_LOW;
Sda_C_LOW;
Delay_us(6);
Clk_C_HIGH;
Delay_us(6);
Sda_C_HIGH;
Delay_us(6);
break;
case 3:
Sda_D_out;
Delay_us(6);
Clk_D_LOW;
Sda_D_LOW;
Delay_us(6);
Clk_D_HIGH;
Delay_us(6);
Sda_D_HIGH;
Delay_us(6);
break;
default:
;break;
}
}
/*********************************************************************************************************
** Function name: public_Start_IIC
** Descriptions:
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
void public_Wr_Bit_IIC(int cnt, const unsigned char bitData)
{
switch(cnt){
case 0:
Clk_A_LOW;
Delay_us(6);
if(bitData == 0)
{
Sda_A_LOW;
}
else
{
Sda_A_HIGH;
}
Clk_A_HIGH ;
Delay_us(6);
Clk_A_LOW;
break;
case 1:
Clk_B_LOW;
Delay_us(6);
if(bitData == 0)
{
Sda_B_LOW;
}
else
{
Sda_B_HIGH;
}
Clk_B_HIGH ;
Delay_us(6);
Clk_B_LOW;
break;
case 2:
Clk_C_LOW;
Delay_us(6);
if(bitData == 0)
{
Sda_C_LOW;
}
else
{
Sda_C_HIGH;
}
Clk_C_HIGH ;
Delay_us(6);
Clk_C_LOW;
break;
case 3:
Clk_D_LOW;
Delay_us(6);
if(bitData == 0)
{
Sda_D_LOW;
}
else
{
Sda_D_HIGH;
}
Clk_D_HIGH ;
Delay_us(6);
Clk_D_LOW;
break;
default:
;break;
}
}
/*********************************************************************************************************
** Function name: public_Start_IIC
** Descriptions:
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
static void public_Wr_Byte_IIC(int cnt, const unsigned char wData8)
{
unsigned char dTemp;
unsigned char i;
dTemp = wData8;
switch(cnt){
case 0:
Sda_A_out;
for(i=0; i<8; i++)
{
if((dTemp&0x80) == 0)
{
public_Wr_Bit_IIC(cnt, 0);
}
else
{
public_Wr_Bit_IIC(cnt, 1);
}
dTemp = dTemp<<1;
}
break;
case 1:
Sda_B_out;
for(i=0; i<8; i++)
{
if((dTemp&0x80) == 0)
{
public_Wr_Bit_IIC(cnt, 0);
}
else
{
public_Wr_Bit_IIC(cnt, 1);
}
dTemp = dTemp<<1;
}
break;
case 2:
Sda_C_out;
for(i=0; i<8; i++)
{
if((dTemp&0x80) == 0)
{
public_Wr_Bit_IIC(cnt, 0);
}
else
{
public_Wr_Bit_IIC(cnt, 1);
}
dTemp = dTemp<<1;
}
break;
case 3:
Sda_D_out;
for(i=0; i<8; i++)
{
if((dTemp&0x80) == 0)
{
public_Wr_Bit_IIC(cnt, 0);
}
else
{
public_Wr_Bit_IIC(cnt, 1);
}
dTemp = dTemp<<1;
}
break;
default:
;break;
}
}
/*********************************************************************************************************
** Function name: public_Start_IIC
** Descriptions:
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
static unsigned char public_Rd_Byte_IIC(int cnt)
{
unsigned char rData = 0;
unsigned char i;
switch(cnt){
case 0:
Sda_A_in;
for(i=0; i<8; i++)
{
Clk_A_LOW;
Delay_us(12);
Clk_A_HIGH ;
Delay_us(6);
rData = rData<<1;
if(Read_A_sda != 0)
{
rData |= 0x01;
}
else
{
rData &= 0xfe;
}
Delay_us(6);
}
Sda_A_out;
break;
case 1:
Sda_B_in;
for(i=0; i<8; i++)
{
Clk_B_LOW;
Delay_us(12);
Clk_B_HIGH ;
Delay_us(6);
rData = rData<<1;
if(Read_B_sda != 0)
{
rData |= 0x01;
}
else
{
rData &= 0xfe;
}
Delay_us(6);
}
Sda_B_out;
break;
case 2:
Sda_C_in;
for(i=0; i<8; i++)
{
Clk_C_LOW;
Delay_us(12);
Clk_C_HIGH ;
Delay_us(6);
rData = rData<<1;
if(Read_C_sda != 0)
{
rData |= 0x01;
}
else
{
rData &= 0xfe;
}
Delay_us(6);
}
Sda_C_out;
break;
case 3:
Sda_D_in;
for(i=0; i<8; i++)
{
Clk_D_LOW;
Delay_us(12);
Clk_D_HIGH ;
Delay_us(6);
rData = rData<<1;
if(Read_D_sda != 0)
{
rData |= 0x01;
}
else
{
rData &= 0xfe;
}
Delay_us(6);
}
Sda_D_out;
break;
default:
;break;
}
return rData;
}
/*********************************************************************************************************
** Function name: public_Start_IIC
** Descriptions:
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
static unsigned char public_Check_SlaveAck_IIC(int cnt)
{
switch(cnt){
case 0:
Sda_A_in;
Clk_A_LOW;
Delay_us(10);
Clk_A_HIGH ;
Delay_us(2);
if(Read_A_sda != 0)
{
return 0;
}
Delay_us(3);
Sda_A_out;
Clk_A_LOW;
Delay_us(4);
break;
case 1:
Sda_B_in;
Clk_B_LOW;
Delay_us(10);
Clk_B_HIGH ;
Delay_us(2);
if(Read_B_sda != 0)
{
return 0;
}
Delay_us(3);
Sda_B_out;
Clk_B_LOW;
Delay_us(4);
break;
case 2:
Sda_C_in;
Clk_C_LOW;
Delay_us(10);
Clk_C_HIGH ;
Delay_us(2);
if(Read_C_sda != 0)
{
return 0;
}
Delay_us(3);
Sda_C_out;
Clk_C_LOW;
Delay_us(4);
break;
case 3:
Sda_D_in;
Clk_D_LOW;
Delay_us(10);
Clk_D_HIGH ;
Delay_us(2);
if(Read_D_sda != 0)
{
return 0;
}
Delay_us(3);
Sda_D_out;
Clk_D_LOW;
Delay_us(4);
break;
default:
;break;
}
return 1;
}
/*********************************************************************************************************
** Function name: public_Start_IIC
** Descriptions:
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
static void public_Master_Ack_IIC(int cnt)
{
public_Wr_Bit_IIC(cnt, 0);
}
/*********************************************************************************************************
** Function name: public_Start_IIC
** Descriptions:
** input parameters:
** Output parameters:
** Returned value:
*********************************************************************************************************/
static void public_Master_NAck_IIC(int cnt)
{
public_Wr_Bit_IIC(cnt, 1);
}
uint8_t Public_I2C_ReadNByte (int cnt, uint8_t chipaddr, PublicRegWidth RegWidth, uint32_t addr, uint8_t *s, uint32_t num)
{
uint8_t rAck;
uint8_t sAddrTmp;
sAddrTmp = chipaddr & (~0x01);
public_Start_IIC(cnt);
public_Wr_Byte_IIC(cnt, public_IIC_WR_CMD | sAddrTmp);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
if(RegWidth == REG_24Bit){
public_Wr_Byte_IIC(cnt, (addr>>16)&0x0000FF);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
}
if((RegWidth == REG_24Bit) || (RegWidth == REG_16Bit)){
public_Wr_Byte_IIC(cnt, addr>>8);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
}
public_Wr_Byte_IIC(cnt, addr&0xFF);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
public_Start_IIC(cnt);
public_Wr_Byte_IIC(cnt, public_IIC_RD_CMD | sAddrTmp);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
while(--num){
*s++ = public_Rd_Byte_IIC(cnt);
public_Master_Ack_IIC(cnt);
}
*s = public_Rd_Byte_IIC(cnt);
public_Master_NAck_IIC(cnt);
public_Stop_IIC(cnt);
return 1;
}
uint8_t Public_I2C_WriteNByte(int cnt,uint8_t chipaddr, PublicRegWidth RegWidth, uint32_t addr, uint8_t *s, uint32_t num)
{
uint8_t rAck;
uint8_t sAddrTmp;
sAddrTmp = chipaddr & (~0x01);
public_Start_IIC(cnt);
public_Wr_Byte_IIC(cnt, public_IIC_WR_CMD | sAddrTmp);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
if(RegWidth == REG_24Bit){
public_Wr_Byte_IIC(cnt, (addr>>16)&0x0000FF);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
}
if((RegWidth == REG_24Bit) || (RegWidth == REG_16Bit)){
public_Wr_Byte_IIC(cnt, addr>>8);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
}
public_Wr_Byte_IIC(cnt, addr&0xff);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
while(num--){
public_Wr_Byte_IIC(cnt, *s++);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 0;
}
//Delay_us(200);
Delay_us(12);
}
public_Stop_IIC(cnt);
return 1;
}
uint8_t public_I2C_Read(int cnt,uint8_t chipaddr, uint16_t addr, uint8_t *data)
{
uint8_t rAck;
uint8_t sAddrTmp;
sAddrTmp = chipaddr & (~0x01);
public_Start_IIC(cnt);
public_Wr_Byte_IIC(cnt, public_IIC_WR_CMD | sAddrTmp);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 251;
}
public_Wr_Byte_IIC(cnt, addr>>8);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 252;
}
public_Wr_Byte_IIC(cnt, addr&0xFF);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 252;
}
public_Start_IIC(cnt);
public_Wr_Byte_IIC(cnt, public_IIC_RD_CMD | sAddrTmp);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 253;
}
*data = public_Rd_Byte_IIC(cnt);
public_Master_NAck_IIC(cnt);
public_Stop_IIC(cnt);
return 1;
}
uint8_t public_I2C_Write(int cnt,uint8_t chipaddr, uint16_t addr, uint8_t data)
{
uint8_t rAck;
uint8_t sAddrTmp;
sAddrTmp = chipaddr & (~0x01);
public_Start_IIC(cnt);
public_Wr_Byte_IIC(cnt, public_IIC_WR_CMD | sAddrTmp);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 251;
}
public_Wr_Byte_IIC(cnt, addr>>8);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 252;
}
public_Wr_Byte_IIC(cnt, addr&0xff);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 252;
}
public_Wr_Byte_IIC(cnt, data);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 253;
}
public_Stop_IIC(cnt);
Delay_ms(1);
return 1;
}
uint8_t public_WR_aByte_IIC(int cnt, const uint8_t sAddr, const uint8_t wAddr, const uint8_t wData)
{
uint8_t rAck;
uint8_t sAddrTmp;
sAddrTmp = sAddr & (~0x01);
public_Start_IIC(cnt);
public_Wr_Byte_IIC(cnt, public_IIC_WR_CMD | sAddrTmp);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 251;
}
public_Wr_Byte_IIC(cnt,wAddr);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 252;
}
public_Wr_Byte_IIC(cnt, wData);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 253;
}
public_Stop_IIC(cnt);
Delay_ms(1);
//Delay_ms();
//Delay_ms();
return 1;
}
uint8_t public_RD_aByte_IIC(int cnt, const uint8_t sAddr, const uint8_t rAddr, uint8_t *outData)
{
uint8_t rAck;
uint8_t sAddrTmp;
sAddrTmp = sAddr & (~0x01);
public_Start_IIC(cnt);
public_Wr_Byte_IIC(cnt, public_IIC_WR_CMD | sAddrTmp);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 251;
}
public_Wr_Byte_IIC(cnt,rAddr);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 252;
}
public_Start_IIC(cnt);
public_Wr_Byte_IIC(cnt, public_IIC_RD_CMD | sAddrTmp);
rAck = public_Check_SlaveAck_IIC(cnt);
if(rAck == 0)
{
public_Stop_IIC(cnt);
return 253;
}
*outData = public_Rd_Byte_IIC(cnt);
public_Master_NAck_IIC(cnt);
public_Stop_IIC(cnt);
return 1;
}
public_i2c.h
#ifndef _PUBLIC_I2C_H_
#define _PUBLIC_I2C_H_
#include "gd32f30x.h"
#define public_IIC_WR_CMD 0x00
#define public_IIC_RD_CMD 0x01
#define Sda_A_in gpio_init(GPIOA, GPIO_MODE_IPD, GPIO_OSPEED_50MHZ, GPIO_PIN_10)
#define Sda_A_out gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10)
#define Sda_A_HIGH gpio_bit_set(GPIOA, GPIO_PIN_10)
#define Sda_A_LOW gpio_bit_reset(GPIOA, GPIO_PIN_10)
#define Read_A_sda gpio_input_bit_get(GPIOA, GPIO_PIN_10)
#define Clk_A_out gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9)
#define Clk_A_HIGH gpio_bit_set(GPIOA, GPIO_PIN_9)
#define Clk_A_LOW gpio_bit_reset(GPIOA, GPIO_PIN_9)
#define Sda_B_in gpio_init(GPIOA, GPIO_MODE_IPD, GPIO_OSPEED_50MHZ, GPIO_PIN_1)
#define Sda_B_out gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1)
#define Sda_B_HIGH gpio_bit_set(GPIOA, GPIO_PIN_1)
#define Sda_B_LOW gpio_bit_reset(GPIOA, GPIO_PIN_1)
#define Read_B_sda gpio_input_bit_get(GPIOA, GPIO_PIN_1)
#define Clk_B_out gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0)
#define Clk_B_HIGH gpio_bit_set(GPIOA,GPIO_PIN_0)
#define Clk_B_LOW gpio_bit_reset(GPIOA,GPIO_PIN_0)
#define Sda_C_in gpio_init(GPIOE, GPIO_MODE_IPD, GPIO_OSPEED_50MHZ, GPIO_PIN_0)
#define Sda_C_out gpio_init(GPIOE, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0)
#define Sda_C_HIGH gpio_bit_set(GPIOE, GPIO_PIN_0)
#define Sda_C_LOW gpio_bit_reset(GPIOE, GPIO_PIN_0)
#define Read_C_sda gpio_input_bit_get(GPIOE, GPIO_PIN_0)
#define Clk_C_out gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9)
#define Clk_C_HIGH gpio_bit_set(GPIOB, GPIO_PIN_9)
#define Clk_C_LOW gpio_bit_reset(GPIOB, GPIO_PIN_9)
#define Sda_D_in gpio_init(GPIOE, GPIO_MODE_IPD, GPIO_OSPEED_50MHZ, GPIO_PIN_4)
#define Sda_D_out gpio_init(GPIOE, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4)
#define Sda_D_HIGH gpio_bit_set(GPIOE,GPIO_PIN_4)
#define Sda_D_LOW gpio_bit_reset(GPIOE,GPIO_PIN_4)
#define Read_D_sda gpio_input_bit_get(GPIOE,GPIO_PIN_4)
#define Clk_D_out gpio_init(GPIOE, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3)
#define Clk_D_HIGH gpio_bit_set(GPIOE,GPIO_PIN_3)
#define Clk_D_LOW gpio_bit_reset(GPIOE,GPIO_PIN_3)
typedef enum{
REG_8Bit,
REG_16Bit,
REG_24Bit
}PublicRegWidth;
uint8_t public_WR_aByte_IIC(int cnt, const uint8_t sAddr, const uint8_t wAddr, const uint8_t wData);
uint8_t public_RD_aByte_IIC(int cnt, const uint8_t sAddr, const uint8_t rAddr, uint8_t *outData);
uint8_t public_I2C_Read(int cnt, uint8_t chipaddr, uint16_t addr, uint8_t *data);
uint8_t public_I2C_Write(int cnt, uint8_t chipaddr, uint16_t addr, uint8_t data);
uint8_t Public_I2C_ReadNByte (int cnt, uint8_t chipaddr, PublicRegWidth RegWidth, uint32_t addr, uint8_t *s, uint32_t num);
uint8_t Public_I2C_WriteNByte(int cnt, uint8_t chipaddr, PublicRegWidth RegWidth, uint32_t addr, uint8_t *s, uint32_t num);
#endif //_PUBLIC_I2C_H_