22 #define I2C_DEVICE (I2CDEV_2) 89 #define I2C_DEFAULT_TIMEOUT 30000 92 static volatile bool error =
false;
95 static volatile uint8_t
addr;
96 static volatile uint8_t
reg;
131 if (!(I2Cx->
CR1 & 0x0100)) {
132 while (I2Cx->
CR1 & 0x0200 && --timeout > 0) {
143 while (
busy && --timeout > 0) {
152 bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data)
157 bool i2cRead(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t *buf)
175 if (!(I2Cx->
CR1 & 0x0100)) {
176 while (I2Cx->
CR1 & 0x0200 && --timeout > 0) {
187 while (
busy && --timeout > 0) {
196 bool i2cReadAsync(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t *buf,
volatile uint8_t* status_,
void (*CB)(uint8_t))
216 if (!(I2Cx->
CR1 & 0x0100)) {
217 while (I2Cx->
CR1 & 0x0200 && --timeout > 0) {
229 bool i2cWriteAsync(uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *
buf_,
volatile uint8_t* status_,
void (*CB)(uint8_t))
249 if (!(I2Cx->
CR1 & 0x0100)) {
250 while (I2Cx->
CR1 & 0x0200 && --timeout > 0) {
265 volatile uint32_t SR1Register = I2Cx->
SR1;
267 if (SR1Register & 0x0F00)
274 if (SR1Register & 0x0700) {
277 if (!(SR1Register & 0x0200) && !(I2Cx->
CR1 & 0x0200)) {
278 if (I2Cx->
CR1 & 0x0100) {
280 while (I2Cx->
CR1 & 0x0100 && --timeout > 0) {
285 while (I2Cx->
CR1 & 0x0200 && --timeout > 0) {
295 I2Cx->
SR1 &= ~0x0F00;
306 static uint8_t subaddress_sent, final_stop;
308 uint8_t SReg_1 = I2Cx->
SR1;
310 if (SReg_1 & 0x0001) {
311 I2Cx->
CR1 &= ~0x0800;
314 if (
reading && (subaddress_sent || 0xFF ==
reg)) {
325 }
else if (SReg_1 & 0x0002) {
347 }
else if (SReg_1 & 0x004) {
349 if (
reading && subaddress_sent) {
352 read_p[index++] = (uint8_t)I2Cx->
DR;
355 read_p[index++] = (uint8_t)I2Cx->
DR;
363 read_p[index++] = (uint8_t)I2Cx->
DR;
364 read_p[index++] = (uint8_t)I2Cx->
DR;
368 if (subaddress_sent || (
writing)) {
381 while (I2Cx->
CR1 & 0x0100 && --timeout > 0) {
385 }
else if (SReg_1 & 0x0040) {
386 read_p[index++] = (uint8_t)I2Cx->
DR;
387 if (
bytes == (index + 3))
391 }
else if (SReg_1 & 0x0080) {
404 if (index ==
bytes + 1) {
429 I2Cx = i2cHardwareMap[index].
dev;
495 for (i = 0; i < 8; i++) {
579 void i2c_queue_job(
i2cJobType_t type, uint8_t addr_, uint8_t reg_, uint8_t *data, uint8_t length,
volatile uint8_t* status_,
void(*CB)(uint8_t))
uint16_t i2cGetErrorCounter(void)
static bool i2c_buffer_lock
static void i2c_job_handler(void)
void NVIC_Init(NVIC_InitTypeDef *NVIC_InitStruct)
Initializes the NVIC peripheral according to the specified parameters in the NVIC_InitStruct.
bool i2cRead(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t *buf)
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Enables or disables the Low Speed APB (APB1) peripheral clock.
void i2cInit(I2CDevice index)
static i2cJob_t i2c_buffer[I2C_BUFFER_SIZE+10]
static volatile bool error
static bool i2cHandleHardwareFailure(void)
#define RCC_APB1Periph_I2C1
static volatile uint8_t writing
bool i2cWriteAsync(uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *buf_, volatile uint8_t *status_, void(*CB)(uint8_t))
uint16_t I2C_AcknowledgedAddress
static void(* complete_CB)(uint8_t)
void I2C_AcknowledgeConfig(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C acknowledge feature.
void I2C_GenerateSTART(I2C_TypeDef *I2Cx, FunctionalState NewState)
Generates I2Cx communication START condition.
void delayMicroseconds(uint32_t us)
void I2C_Send7bitAddress(I2C_TypeDef *I2Cx, uint8_t Address, uint8_t I2C_Direction)
Transmits the address byte to select the slave device.
static I2CDevice I2Cx_index
static volatile uint8_t i2c_buffer_head
static volatile uint8_t * write_p
uint8_t NVIC_IRQChannelSubPriority
void I2C_GenerateSTOP(I2C_TypeDef *I2Cx, FunctionalState NewState)
Generates I2Cx communication STOP condition.
void I2C1_ER_IRQHandler(void)
static I2C_TypeDef * I2Cx
NVIC Init Structure definition.
void gpioInit(GPIO_TypeDef *gpio, gpio_config_t *config)
static volatile bool busy
static volatile uint8_t * status
#define I2C_Direction_Transmitter
bool i2cReadAsync(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t *buf, volatile uint8_t *status_, void(*CB)(uint8_t))
#define I2C_AcknowledgedAddress_7bit
static const i2cDevice_t i2cHardwareMap[]
void I2C2_ER_IRQHandler(void)
static volatile uint16_t i2cErrorCount
volatile uint8_t * status
void I2C_DeInit(I2C_TypeDef *I2Cx)
Deinitializes the I2Cx peripheral registers to their default reset values.
static void i2c_init_buffer(void)
void I2C2_EV_IRQHandler(void)
bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data)
void I2C_StructInit(I2C_InitTypeDef *I2C_InitStruct)
Fills each I2C_InitStruct member with its default value.
static volatile uint8_t bytes
static volatile uint8_t addr
void I2C_Cmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C peripheral.
static volatile uint8_t reg
static volatile uint8_t i2c_buffer_tail
static volatile uint8_t * read_p
void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitTypeDef *I2C_InitStruct)
Initializes the I2Cx peripheral according to the specified parameters in the I2C_InitStruct.
void i2c_queue_job(i2cJobType_t type, uint8_t addr_, uint8_t reg_, uint8_t *data, uint8_t length, volatile uint8_t *status_, void(*CB)(uint8_t))
static void i2c_er_handler(void)
FunctionalState NVIC_IRQChannelCmd
static void i2cUnstick(void)
uint8_t NVIC_IRQChannelPreemptionPriority
I2C Init structure definition.
static volatile uint8_t i2c_buffer_count
static void i2c_ev_handler(void)
Inter-integrated Circuit Interface.
bool i2cWriteBuffer(uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data)
#define I2C_Direction_Receiver
struct i2cDevice_t i2cDevice_t
#define I2C_DEFAULT_TIMEOUT
void I2C_ITConfig(I2C_TypeDef *I2Cx, uint16_t I2C_IT, FunctionalState NewState)
Enables or disables the specified I2C interrupts.
#define RCC_APB1Periph_I2C2
static volatile uint8_t reading
void I2C1_EV_IRQHandler(void)