I2C具体内容参考资料
https://blog.csdn.net/sternlycore/article/details/85600668 https://blog.csdn.net/qq_43460068/article/details/122409361
以下是在Atmel 300MHz下调试处的400KHz模拟I2C,延时函数需要根据实际内核进行设置
#define I2C_SDA_OUTPUT_DIR(pin) ioport_set_pin_dir(pin, IOPORT_DIR_OUTPUT);
#define I2C_SDA_INPUT_DIR(pin) ioport_set_pin_dir(pin, IOPORT_DIR_INPUT);
#define I2C_SDA_SET(pin) ioport_set_pin_level(pin, IOPORT_PIN_LEVEL_HIGH);
#define I2C_SDA_CLR(pin) ioport_set_pin_level(pin, IOPORT_PIN_LEVEL_LOW);
#define I2C_PIN_SDA(pin) ioport_get_pin_level(pin)
#define I2C_SCL_SET(pin) ioport_set_pin_level(pin, IOPORT_PIN_LEVEL_HIGH);
#define I2C_SCL_CLR(pin) ioport_set_pin_level(pin, IOPORT_PIN_LEVEL_LOW);
#define BOARD_LOOPSPERMSEC 1300
#define CONFIG_BOARD_LOOPSPER100USEC ((BOARD_LOOPSPERMSEC+5)/10)
#define CONFIG_BOARD_LOOPSPER10USEC ((BOARD_LOOPSPERMSEC+50)/100)
#define CONFIG_BOARD_LOOPSPERUSEC ((BOARD_LOOPSPERMSEC+500)/1000)
static void i2c_delay(uint32_t us)
{
volatile int i;
while (us > 1000) {
for (i = 0; i < BOARD_LOOPSPERMSEC; i++);
us -= 1000;
}
while (us > 100) {
for (i = 0; i < CONFIG_BOARD_LOOPSPER100USEC; i++);
us -= 100;
}
while (us > 10) {
for (i = 0; i < CONFIG_BOARD_LOOPSPER10USEC; i++);
us -= 10;
}
while (us > 0) {
for (i = 0; i < CONFIG_BOARD_LOOPSPERUSEC; i++);
us--;
}
}
static void i2c_start(I2C_Object *object)
{
I2C_SDA_OUTPUT_DIR(object->sda);
I2C_SDA_SET(object->sda);
I2C_SCL_SET(object->scl);
i2c_delay(50);
I2C_SDA_CLR(object->sda);
i2c_delay(15);
}
static void i2c_stop(I2C_Object *object)
{
I2C_SDA_OUTPUT_DIR(object->sda);
I2C_SDA_CLR(object->sda);
i2c_delay(15);
I2C_SCL_SET(object->scl);
i2c_delay(25);
I2C_SDA_SET(object->sda);
i2c_delay(25);
}
static uint8_t i2c_wait_ack(I2C_Object *object)
{
uint8_t ret = 1;
I2C_SDA_SET(object->sda);
I2C_SDA_INPUT_DIR(object->sda);
i2c_delay(10);
I2C_SCL_SET(object->scl);
i2c_delay(13);
if(I2C_PIN_SDA(object->sda)){
ret = 0;
}
I2C_SCL_CLR(object->scl);
return ret;
}
static void i2c_send_ack(I2C_Object *object)
{
I2C_SDA_OUTPUT_DIR(object->sda);
I2C_SDA_CLR(object->sda);
i2c_delay(13);
I2C_SCL_SET(object->scl);
i2c_delay(13);
I2C_SCL_CLR(object->scl);
}
static void i2c_send_noack(I2C_Object *object)
{
I2C_SDA_OUTPUT_DIR(object->sda);
I2C_SDA_SET(object->sda);
i2c_delay(13);
I2C_SCL_SET(object->scl);
i2c_delay(13);
I2C_SCL_CLR(object->scl);
}
static void i2c_send_data(I2C_Object *object, uint8_t data)
{
uint8_t i = 8;
I2C_SDA_OUTPUT_DIR(object->sda);
while(i--)
{
I2C_SCL_CLR(object->scl);
i2c_delay(15);
if(data & 0x80)
{
I2C_SDA_SET(object->sda);
}
else
{
I2C_SDA_CLR(object->sda);
}
i2c_delay(5);
data <<= 1;
I2C_SCL_SET(object->scl);
i2c_delay(20);
}
I2C_SCL_CLR(object->scl);
i2c_delay(5);
}
static uint8_t i2c_receive_data(I2C_Object *object)
{
uint8_t i = 8;
uint8_t data = 0;
I2C_SDA_SET(object->sda);
I2C_SDA_INPUT_DIR(object->sda);
while(i--)
{
data <<= 1;
I2C_SCL_CLR(object->scl);
i2c_delay(13);
I2C_SCL_SET(object->scl);
i2c_delay(12);
if(I2C_PIN_SDA(object->sda))
data |= 0x01;
}
I2C_SCL_CLR(object->scl);
return(data);
}
uint8_t read_datas(I2C_Object *object, I2C_Transaction *transaction)
{
uint8_t i;
uint8_t *pdata = (uint8_t*)transaction->readBuf;
i2c_start(object);
i2c_send_data(object, transaction->slaveAddress<<1);
if(i2c_wait_ack(object) == 0)
{
i2c_stop(object);
return 0;
}
i2c_send_data(object, ((uint8_t *)transaction->writeBuf)[0]);
if(i2c_wait_ack(object) == 0)
{
i2c_stop(object);
return 0;
}
i2c_start(object);
i2c_send_data(object, (transaction->slaveAddress<<1)|0x01);
if(i2c_wait_ack(object) == 0)
{
i2c_stop(object);
return 0;
}
for(i = 0; i < transaction->readCount; i++)
{
*pdata++ = i2c_receive_data(object);
if(i < (transaction->readCount - 1))
i2c_send_ack(object);
}
i2c_send_noack(object);
i2c_stop(object);
return 1;
}
static uint8_t i2c_write_one_byte(I2C_Object *object, I2C_Transaction *transaction)
{
uint8_t *write_buf = (uint8_t*)transaction->writeBuf;
i2c_start(object);
i2c_send_data(object, transaction->slaveAddress<<1);
if(i2c_wait_ack(object) == 0)
{
i2c_stop(object);
return 0;
}
i2c_send_data(object, write_buf[0]);
if(i2c_wait_ack(object) == 0)
{
i2c_stop(object);
return 0;
}
i2c_send_data(object, write_buf[1]);
if(i2c_wait_ack(object) == 0)
{
i2c_stop(object);
return 0;
}
i2c_stop(object);
return 1;
}
|