/********************************************************************** * Copyright: (C)2024 LingYun IoT System Studio * Author: GuoWenxue * * Description: The purpose of this code is to provide a simple C library, * which providing software bit-bang of the I2C protocol on * any GPIO pins for ISKBoard. * * ChangeLog: * Version Date Author Description * V1.0.0 2024.08.29 GuoWenxue Release initial version * ***********************************************************************/ #ifndef __I2C_BITBANG_H_ #define __I2C_BITBANG_H_ #include "stm32l4xx_hal.h" #define I2C_CLK_STRETCH_TIMEOUT 6000 /* *+---------------------------------+ *| I2C GPIO port Level functions | *+---------------------------------+ */ /* GPIO port for I2C typedef */ typedef struct i2c_gpio_s { GPIO_TypeDef *group; /* GPIO group */ uint16_t pin; /* GPIO pin */ } i2c_gpio_t; /* I2C bus typedef */ typedef struct i2c_bus_s { uint8_t addr; /* 7-bits slave address */ i2c_gpio_t scl; /* SCL GPIO pin */ i2c_gpio_t sda; /* SDA GPIO pin */ } i2c_bus_t; /* *+--------------------------------+ *| I2C High Level API functions | *+--------------------------------+ */ /* I2C bus error number */ enum { ERROR_NONE = 0x00, // no error ERROR_PARM = 0x01, // parameter out of range error ERROR_ACK = 0x02, // no acknowledgment error ERROR_TIMEOUT = 0x03, // timeout error ERROR_CHECKSUM = 0x04, // checksum mismatch error }; /* Set I2C slave device 7-bits address in the bus to lock it */ extern int i2c_lock(uint8_t addr); /* Clear I2C slave device 7-bits address in the bus to free it */ extern void i2c_free(void); /* write data by to slave device */ extern int i2c_write(uint8_t *data, int bytes); /* read data from slave device */ extern int i2c_read(uint8_t *buf, int size); /* *+--------------------------------+ *| I2C Low Level API functions | *+--------------------------------+ */ /* Send I2C start condition */ extern void I2C_StartCondition(void); /* Send I2C stop condition */ extern void I2C_StopCondition(void); /* Send I2C slave device 8-bits address */ #define W_ADDR(x) ((x)<<1 ) /* I2C address bit[0]=0 means write */ #define R_ADDR(x) ((x)<<1 | 1) /* I2C address bit[0]=1 means read */ enum { I2C_WR, /* Direction for write */ I2C_RD, /* Direction for read */ }; extern int I2C_SendAddress(uint8_t dir); /* Write a byte data to I2C slave device */ extern int I2C_WriteByte(uint8_t txByte); /* Read a byte data from I2C slave device */ enum { ACK_NONE, /* ACK None */ ACK, /* ACK */ NAK, /* NAK */ }; extern int I2C_ReadByte(uint8_t *rxByte, uint8_t ack, uint32_t timeout); #endif /* __I2C_BITBANG_H_ */