222 lines
5.6 KiB
C
222 lines
5.6 KiB
C
/*
|
|
* drv_i2c.c
|
|
*
|
|
* Created on: Mar 31, 2023
|
|
* Author: gxms0
|
|
*/
|
|
|
|
#include "drv_i2c.h"
|
|
|
|
I2C_t i2c1;
|
|
I2C_t i2c2;
|
|
|
|
void i2c_init()
|
|
{
|
|
i2c_config(&i2c1, I2C1, GPIOB, GPIO_Pin_7, GPIOB, GPIO_Pin_6, i2c_Address_8bit, 100000);
|
|
i2c_config(&i2c2, I2C2, GPIOB, GPIO_Pin_11, GPIOB, GPIO_Pin_10, i2c_Address_16bit, 100000);
|
|
|
|
}
|
|
|
|
void i2c_config(I2C_ptr ptr, I2C_TypeDef *I2C, GPIO_TypeDef *GPIO_SDA, uint32_t Pin_SDA, GPIO_TypeDef *GPIO_SCL, uint32_t Pin_SCL, uint8_t addr_len,uint32_t bound)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
|
I2C_InitTypeDef I2C_InitTSturcture = {0};
|
|
//DMA_InitTypeDef DMA_InitStructure = {0};
|
|
//NVIC_InitTypeDef NVIC_InitStructure = {0};
|
|
uint32_t RCC_Periph = RCC_APB1Periph_I2C1;
|
|
|
|
ptr->I2C = I2C;
|
|
ptr->GPIO_SDA = GPIO_SDA;
|
|
ptr->Pin_SDA = Pin_SDA;
|
|
ptr->GPIO_SCL = GPIO_SCL;
|
|
ptr->Pin_SCL = Pin_SCL;
|
|
|
|
ptr->addr_len = addr_len;
|
|
|
|
|
|
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE );
|
|
|
|
|
|
if(ptr->I2C == I2C1)RCC_Periph = RCC_APB1Periph_I2C1;
|
|
else if(ptr->I2C == I2C2)RCC_Periph = RCC_APB1Periph_I2C2;
|
|
RCC_APB1PeriphClockCmd(RCC_Periph, ENABLE);
|
|
|
|
//初始化IO口
|
|
//init sda
|
|
if(ptr->GPIO_SDA == GPIOA)RCC_Periph = RCC_APB2Periph_GPIOA;
|
|
else if(ptr->GPIO_SDA == GPIOB)RCC_Periph = RCC_APB2Periph_GPIOB;
|
|
else if(ptr->GPIO_SDA == GPIOC)RCC_Periph = RCC_APB2Periph_GPIOC;
|
|
else if(ptr->GPIO_SDA == GPIOD)RCC_Periph = RCC_APB2Periph_GPIOD;
|
|
else if(ptr->GPIO_SDA == GPIOE)RCC_Periph = RCC_APB2Periph_GPIOE;
|
|
RCC_APB2PeriphClockCmd(RCC_Periph, ENABLE);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = ptr->Pin_SDA;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(ptr->GPIO_SDA, &GPIO_InitStructure);
|
|
|
|
//init scl
|
|
if(ptr->GPIO_SCL == GPIOA)RCC_Periph = RCC_APB2Periph_GPIOA;
|
|
else if(ptr->GPIO_SCL == GPIOB)RCC_Periph = RCC_APB2Periph_GPIOB;
|
|
else if(ptr->GPIO_SCL == GPIOC)RCC_Periph = RCC_APB2Periph_GPIOC;
|
|
else if(ptr->GPIO_SCL == GPIOD)RCC_Periph = RCC_APB2Periph_GPIOD;
|
|
else if(ptr->GPIO_SCL == GPIOE)RCC_Periph = RCC_APB2Periph_GPIOE;
|
|
RCC_APB2PeriphClockCmd(RCC_Periph, ENABLE);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = ptr->Pin_SCL;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(ptr->GPIO_SCL, &GPIO_InitStructure);
|
|
|
|
//init i2c
|
|
I2C_InitTSturcture.I2C_ClockSpeed = bound;
|
|
I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
|
|
I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_16_9;
|
|
I2C_InitTSturcture.I2C_OwnAddress1 = 0xA0;
|
|
I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;
|
|
I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
|
I2C_Init( ptr->I2C, &I2C_InitTSturcture );
|
|
|
|
//I2C_DMACmd( I2C1, ENABLE );
|
|
|
|
I2C_Cmd( ptr->I2C, ENABLE );
|
|
}
|
|
|
|
uint16_t i2c_read(I2C_ptr ptr, uint16_t address, uint16_t reg,uint8_t *data, uint16_t len)
|
|
{
|
|
uint8_t buff2read[128];
|
|
uint8_t buff2send[5];
|
|
uint8_t buff_size = 0;
|
|
|
|
while( I2C_GetFlagStatus( ptr->I2C, I2C_FLAG_BUSY ) != RESET );
|
|
I2C_GenerateSTART( ptr->I2C, ENABLE );
|
|
|
|
while( !I2C_CheckEvent( ptr->I2C, I2C_EVENT_MASTER_MODE_SELECT ) );
|
|
I2C_Send7bitAddress( ptr->I2C, address, I2C_Direction_Transmitter );
|
|
|
|
while( !I2C_CheckEvent( ptr->I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );
|
|
|
|
if(ptr->addr_len == i2c_Address_8bit)
|
|
{
|
|
buff2send[0] = reg&0x00FF;
|
|
memcpy(buff2send + 1,data,len);
|
|
buff_size += 1;
|
|
}
|
|
else
|
|
{
|
|
buff2send[0] = (reg&0xFF00) >> 8;
|
|
buff2send[1] = reg&0x00FF;
|
|
|
|
memcpy(buff2send + 2,data,len);
|
|
buff_size += 2;
|
|
}
|
|
|
|
for(int i = 0; i < buff_size;i++)
|
|
{
|
|
I2C_SendData( ptr->I2C, buff2send[i]);
|
|
while( !I2C_CheckEvent( ptr->I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
|
|
}
|
|
|
|
I2C_GenerateSTART( ptr->I2C, ENABLE );
|
|
|
|
while( !I2C_CheckEvent( ptr->I2C, I2C_EVENT_MASTER_MODE_SELECT ) );
|
|
I2C_Send7bitAddress( ptr->I2C, address, I2C_Direction_Receiver );
|
|
|
|
while( !I2C_CheckEvent( ptr->I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) );
|
|
while( I2C_GetFlagStatus( ptr->I2C, I2C_FLAG_RXNE ) == RESET )
|
|
I2C_AcknowledgeConfig( ptr->I2C, ENABLE );
|
|
|
|
uint16_t i = 0;
|
|
for(; i < (len - 1);i++)
|
|
{
|
|
buff2read[i] = I2C_ReceiveData( ptr->I2C );
|
|
while( !I2C_CheckEvent( ptr->I2C, I2C_EVENT_MASTER_BYTE_RECEIVED));
|
|
}
|
|
|
|
buff2read[i] = I2C_ReceiveData( ptr->I2C );
|
|
|
|
I2C_GenerateSTOP( ptr->I2C, ENABLE );
|
|
|
|
memcpy(data,buff2read,i);
|
|
|
|
return i;
|
|
}
|
|
|
|
|
|
uint16_t i2c_write(I2C_ptr ptr, uint16_t address, uint16_t reg,uint8_t *data, uint16_t len)
|
|
{
|
|
uint8_t buff2send[128];
|
|
uint8_t buff_size = len;
|
|
|
|
while( I2C_GetFlagStatus( ptr->I2C, I2C_FLAG_BUSY ) != RESET );
|
|
I2C_GenerateSTART( ptr->I2C, ENABLE );
|
|
|
|
while( !I2C_CheckEvent( ptr->I2C, I2C_EVENT_MASTER_MODE_SELECT ));
|
|
I2C_Send7bitAddress( ptr->I2C, address, I2C_Direction_Transmitter);
|
|
|
|
while( !I2C_CheckEvent( ptr->I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ));
|
|
|
|
if(ptr->addr_len == i2c_Address_8bit)
|
|
{
|
|
buff2send[0] = reg&0x00FF;
|
|
memcpy(buff2send + 1,data,len);
|
|
buff_size += 1;
|
|
}
|
|
else
|
|
{
|
|
buff2send[0] = (reg&0xFF00) >> 8;
|
|
buff2send[1] = reg&0x00FF;
|
|
memcpy(buff2send + 2,data,len);
|
|
buff_size += 2;
|
|
}
|
|
|
|
uint16_t i = 0;
|
|
for(; i < buff_size;i++)
|
|
{
|
|
I2C_SendData( ptr->I2C, buff2send[i]);
|
|
while( !I2C_CheckEvent( ptr->I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
|
|
}
|
|
|
|
I2C_GenerateSTOP( ptr->I2C, ENABLE );
|
|
|
|
return i;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|