|
|
|
@ -72,7 +72,7 @@ void IIC_Stop(const IIC_SOFT_TypeDef *pSoftIIC) |
|
|
|
|
//等待应答信号到来
|
|
|
|
|
//返回值:1,接收应答失败
|
|
|
|
|
// 0,接收应答成功
|
|
|
|
|
uint8_t IIC_Wait_Ack(const IIC_SOFT_TypeDef *pSoftIIC) |
|
|
|
|
int IIC_Wait_Ack(const IIC_SOFT_TypeDef *pSoftIIC) |
|
|
|
|
{ |
|
|
|
|
uint8_t ucErrTime=0; |
|
|
|
|
// SDA_IN(); //SDA设置为输入
|
|
|
|
@ -125,7 +125,7 @@ void IIC_NAck(const IIC_SOFT_TypeDef *pSoftIIC) |
|
|
|
|
//返回从机有无应答
|
|
|
|
|
//1,有应答
|
|
|
|
|
//0,无应答
|
|
|
|
|
void IIC_Send_Byte(const IIC_SOFT_TypeDef *pSoftIIC,uint8_t txd) |
|
|
|
|
int IIC_Send_Byte(const IIC_SOFT_TypeDef *pSoftIIC, uint8_t txd) |
|
|
|
|
{
|
|
|
|
|
uint8_t t;
|
|
|
|
|
// SDA_OUT();//sda线输出
|
|
|
|
@ -138,15 +138,16 @@ void IIC_Send_Byte(const IIC_SOFT_TypeDef *pSoftIIC,uint8_t txd) |
|
|
|
|
// IIC_SDA( (txd&0x80)>>7);
|
|
|
|
|
GPIO_WRITE_PIN(pSoftIIC->sda_port, pSoftIIC->sda_pin, (txd&0x80)>>7 ); |
|
|
|
|
txd<<=1;
|
|
|
|
|
delay_us(2); //对TEA5767这三个延时都是必须的
|
|
|
|
|
delay_us(2);
|
|
|
|
|
GPIO_WRITE_PIN(pSoftIIC->scl_port, pSoftIIC->scl_pin, 1); |
|
|
|
|
delay_us(2);
|
|
|
|
|
GPIO_WRITE_PIN(pSoftIIC->scl_port, pSoftIIC->scl_pin, 0);
|
|
|
|
|
delay_us(2); |
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t IIC_Read_Byte( const IIC_SOFT_TypeDef *pSoftIIC) |
|
|
|
|
int IIC_Read_Byte( const IIC_SOFT_TypeDef *pSoftIIC) |
|
|
|
|
{ |
|
|
|
|
unsigned char i,receive=0; |
|
|
|
|
|
|
|
|
@ -168,17 +169,85 @@ uint8_t IIC_Read_Byte( const IIC_SOFT_TypeDef *pSoftIIC) |
|
|
|
|
return receive; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint8_t IIC_Write_Test(const IIC_SOFT_TypeDef *pSoftIIC , uint8_t reg_addr, uint8_t *buf, uint8_t len) |
|
|
|
|
// 发送Slave_Addr 带应答, 兼容7bit 10bit
|
|
|
|
|
int IIC_Send_Slave_Addr(const IIC_SOFT_TypeDef *pSoftIIC,uint8_t r_w) |
|
|
|
|
{ |
|
|
|
|
if( pSoftIIC->slave_address_width == I2C_SW_Slave_Address_7Bit ) |
|
|
|
|
{ |
|
|
|
|
log_i("salve adrr %02X %02X",pSoftIIC->addr, (pSoftIIC->addr << 1)|r_w); |
|
|
|
|
IIC_Send_Byte(pSoftIIC, (pSoftIIC->addr << 1) | r_w); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
} |
|
|
|
|
else if( pSoftIIC->slave_address_width == I2C_SW_Slave_Address_10Bit ) |
|
|
|
|
{ |
|
|
|
|
uint8_t h_addr = 0x0F00 | ((pSoftIIC->addr >> 8 & 0x03) << 2) | r_w; |
|
|
|
|
IIC_Send_Byte(pSoftIIC, h_addr); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
|
|
|
|
|
uint8_t l_addr = pSoftIIC->addr & 0xff; |
|
|
|
|
IIC_Send_Byte(pSoftIIC, l_addr); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 发送reg_addr 带应答, 兼容8bit 16bit 32bit
|
|
|
|
|
int IIC_Send_Reg_Addr( const IIC_SOFT_TypeDef *pSoftIIC, uint32_t reg_addr ) |
|
|
|
|
{ |
|
|
|
|
for(int j = 0; j < pSoftIIC->slave_reg_addr_width; j++) |
|
|
|
|
{ |
|
|
|
|
uint8_t offset = (pSoftIIC->slave_reg_addr_width - 1 - j) * 8; |
|
|
|
|
uint8_t temp_reg_addr = (reg_addr >> offset) & 0xff; |
|
|
|
|
|
|
|
|
|
IIC_Send_Byte(pSoftIIC, temp_reg_addr); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int IIC_Send_One_Reg_Value( const IIC_SOFT_TypeDef *pSoftIIC, uint32_t reg_val ) |
|
|
|
|
{ |
|
|
|
|
for(int k = 0; k < pSoftIIC->slave_reg_value_width; k++) |
|
|
|
|
{ |
|
|
|
|
uint8_t offset = (pSoftIIC->slave_reg_value_width - 1 - k) * 8; |
|
|
|
|
|
|
|
|
|
i2c_send_byte(pSoftIIC, (reg_val >> offset) & 0xff); |
|
|
|
|
if(!i2c_wait_ack(pSoftIIC)) |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int IIC_Send_Receive_One_Reg_Value( const IIC_SOFT_TypeDef *pSoftIIC, uint32_t* reg_val, uint8_t ack ) |
|
|
|
|
{ |
|
|
|
|
for(int k = 0; k < pSoftIIC->slave_reg_value_width; k++) |
|
|
|
|
{ |
|
|
|
|
uint8_t offset = (pSoftIIC->slave_reg_value_width - 1 - k) * 8; |
|
|
|
|
*reg_val |= (IIC_Read_Byte(pSoftIIC) << offset); |
|
|
|
|
|
|
|
|
|
if(ack) |
|
|
|
|
i2c_ack(pSoftIIC); |
|
|
|
|
else |
|
|
|
|
i2c_no_ack(pSoftIIC); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int IIC_Write_Buf(const IIC_SOFT_TypeDef *pSoftIIC , uint32_t reg_addr, uint8_t *buf, uint8_t len) |
|
|
|
|
{ |
|
|
|
|
IIC_Start(pSoftIIC);
|
|
|
|
|
IIC_Send_Byte(pSoftIIC,pSoftIIC->addr & 0xFE); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
|
|
|
|
|
IIC_Send_Byte(pSoftIIC,reg_addr); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
|
|
|
|
|
IIC_Send_Slave_Addr( pSoftIIC,I2C_WRITE ); |
|
|
|
|
IIC_Send_Reg_Addr( pSoftIIC, reg_addr ); |
|
|
|
|
|
|
|
|
|
IIC_Send_Byte(pSoftIIC,*buf); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
for (size_t i = 0; i < len; i++) |
|
|
|
|
{ |
|
|
|
|
IIC_Send_Byte(pSoftIIC,*buf); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
IIC_Stop(pSoftIIC);//产生一个停止条件
|
|
|
|
|
// HAL_Delay( 200 );
|
|
|
|
@ -187,21 +256,16 @@ uint8_t IIC_Read_Byte( const IIC_SOFT_TypeDef *pSoftIIC) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 寄存器地址 8位置
|
|
|
|
|
uint8_t IIC_Read_Test(const IIC_SOFT_TypeDef *pSoftIIC, uint8_t reg_addr, uint8_t *buf, uint8_t len) |
|
|
|
|
int IIC_Read_Buf(const IIC_SOFT_TypeDef *pSoftIIC, uint32_t reg_addr, uint8_t *buf, uint8_t len) |
|
|
|
|
{ |
|
|
|
|
uint8_t temp=0;
|
|
|
|
|
IIC_Start(pSoftIIC);
|
|
|
|
|
|
|
|
|
|
IIC_Send_Byte(pSoftIIC,pSoftIIC->addr & 0xFE); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
uint8_t temp=0;
|
|
|
|
|
|
|
|
|
|
IIC_Send_Byte(pSoftIIC,reg_addr); |
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
IIC_Start(pSoftIIC);
|
|
|
|
|
IIC_Send_Slave_Addr( pSoftIIC,I2C_WRITE ); |
|
|
|
|
IIC_Send_Reg_Addr( pSoftIIC, reg_addr ); |
|
|
|
|
|
|
|
|
|
IIC_Start(pSoftIIC);
|
|
|
|
|
IIC_Send_Byte(pSoftIIC,pSoftIIC->addr |0x01); //进入接收模式
|
|
|
|
|
if(IIC_Wait_Ack(pSoftIIC)) { IIC_Stop(pSoftIIC); return -1;};
|
|
|
|
|
|
|
|
|
|
IIC_Send_Slave_Addr( pSoftIIC,I2C_READ ); |
|
|
|
|
for(int i=0; i<len-1; i++) |
|
|
|
|
{ |
|
|
|
|
*buf=IIC_Read_Byte(pSoftIIC); |
|
|
|
@ -216,3 +280,15 @@ uint8_t IIC_Read_Test(const IIC_SOFT_TypeDef *pSoftIIC, uint8_t reg_addr, uint8_ |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int IIC_Device_Check(const IIC_SOFT_TypeDef *pSoftIIC) |
|
|
|
|
{ |
|
|
|
|
uint8_t ret = 0; |
|
|
|
|
IIC_Start(pSoftIIC);
|
|
|
|
|
|
|
|
|
|
ret = IIC_Send_Slave_Addr( pSoftIIC,I2C_WRITE ); |
|
|
|
|
|
|
|
|
|
IIC_Stop(pSoftIIC); |
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
|
} |