Added i2c timeout

- i2c timeout to avoid getting stuck in while loop
This commit is contained in:
EmanuelFeru 2020-12-06 17:39:36 +01:00
parent 570e11574e
commit ff9a135344
3 changed files with 17 additions and 9 deletions

View File

@ -198,6 +198,7 @@ void I2C0_ErrorIRQ_Handler(void)
if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_PECERR)){ if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_PECERR)){
i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_PECERR); i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_PECERR);
} }
/* disable the error interrupt */ /* disable the error interrupt */
i2c_interrupt_disable(I2C0,I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV); i2c_interrupt_disable(I2C0,I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV);
} }
@ -365,6 +366,7 @@ void I2C1_ErrorIRQ_Handler(void)
if(i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_PECERR)){ if(i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_PECERR)){
i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_PECERR); i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_PECERR);
} }
/* disable the error interrupt */ /* disable the error interrupt */
i2c_interrupt_disable(I2C1,I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV); i2c_interrupt_disable(I2C1,I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV);
} }

View File

@ -342,12 +342,12 @@ void i2c_nvic_config(void)
/* configure the NVIC peripheral */ /* configure the NVIC peripheral */
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3); nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(I2C0_EV_IRQn, 0, 3); nvic_irq_enable(I2C0_EV_IRQn, 0, 0);
nvic_irq_enable(I2C0_ER_IRQn, 0, 2); nvic_irq_enable(I2C0_ER_IRQn, 0, 0);
#ifdef AUX45_USE_I2C #ifdef AUX45_USE_I2C
nvic_irq_enable(I2C1_EV_IRQn, 0, 4); nvic_irq_enable(I2C1_EV_IRQn, 0, 2);
nvic_irq_enable(I2C1_ER_IRQn, 0, 1); nvic_irq_enable(I2C1_ER_IRQn, 0, 2);
#endif #endif
} }

View File

@ -570,17 +570,20 @@ int8_t i2c_writeBytes(uint8_t slaveAddr, uint8_t regAddr, uint8_t length, uint8_
i2c_nDABytes = length; i2c_nDABytes = length;
i2c_nRABytes = 1; i2c_nRABytes = 1;
uint16_t i2c_timeout = 0;
// enable the I2C0 interrupt // enable the I2C0 interrupt
i2c_interrupt_enable(MPU_I2C, I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV); i2c_interrupt_enable(MPU_I2C, I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV);
// the master waits until the I2C bus is idle // the master waits until the I2C bus is idle
while(i2c_flag_get(MPU_I2C, I2C_FLAG_I2CBSY)); while(i2c_flag_get(MPU_I2C, I2C_FLAG_I2CBSY) && i2c_timeout++ < 20000);
// the master sends a start condition to I2C bus // the master sends a start condition to I2C bus
i2c_start_on_bus(MPU_I2C); i2c_start_on_bus(MPU_I2C);
// Wait until all data bytes are sent/received // Wait until all data bytes are sent/received
while(i2c_nDABytes > 0); i2c_timeout = 0;
while(i2c_nDABytes > 0 && i2c_timeout++ < 20000);
return i2c_status; return i2c_status;
@ -627,6 +630,8 @@ int8_t i2c_readBytes(uint8_t slaveAddr, uint8_t regAddr, uint8_t length, uint8_t
i2c_nDABytes = length; i2c_nDABytes = length;
i2c_nRABytes = 1; i2c_nRABytes = 1;
uint16_t i2c_timeout = 0;
// enable the I2C0 interrupt // enable the I2C0 interrupt
i2c_interrupt_enable(MPU_I2C, I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV); i2c_interrupt_enable(MPU_I2C, I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV);
@ -635,13 +640,14 @@ int8_t i2c_readBytes(uint8_t slaveAddr, uint8_t regAddr, uint8_t length, uint8_t
} }
// the master waits until the I2C bus is idle // the master waits until the I2C bus is idle
while(i2c_flag_get(MPU_I2C, I2C_FLAG_I2CBSY)); while(i2c_flag_get(MPU_I2C, I2C_FLAG_I2CBSY) && i2c_timeout++ < 20000);
// the master sends a start condition to I2C bus // the master sends a start condition to I2C bus
i2c_start_on_bus(MPU_I2C); i2c_start_on_bus(MPU_I2C);
// Wait until all data bytes are sent/received // Wait until all data bytes are sent/received
while(i2c_nDABytes > 0); i2c_timeout = 0;
while(i2c_nDABytes > 0 && i2c_timeout++ < 20000);
// Return status // Return status
return i2c_status; return i2c_status;