From ff9a13534403d6af55e7518ef39546b60cb12da4 Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Sun, 6 Dec 2020 17:39:36 +0100 Subject: [PATCH] Added i2c timeout - i2c timeout to avoid getting stuck in while loop --- Src/i2c_it.c | 2 ++ Src/setup.c | 8 ++++---- Src/util.c | 16 +++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Src/i2c_it.c b/Src/i2c_it.c index dbfa270..c25016a 100644 --- a/Src/i2c_it.c +++ b/Src/i2c_it.c @@ -198,6 +198,7 @@ void I2C0_ErrorIRQ_Handler(void) if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_PECERR)){ i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_PECERR); } + /* disable the error interrupt */ 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)){ i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_PECERR); } + /* disable the error interrupt */ i2c_interrupt_disable(I2C1,I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV); } diff --git a/Src/setup.c b/Src/setup.c index f758c85..fd3b0a9 100644 --- a/Src/setup.c +++ b/Src/setup.c @@ -342,12 +342,12 @@ void i2c_nvic_config(void) /* configure the NVIC peripheral */ nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3); - nvic_irq_enable(I2C0_EV_IRQn, 0, 3); - nvic_irq_enable(I2C0_ER_IRQn, 0, 2); + nvic_irq_enable(I2C0_EV_IRQn, 0, 0); + nvic_irq_enable(I2C0_ER_IRQn, 0, 0); #ifdef AUX45_USE_I2C - nvic_irq_enable(I2C1_EV_IRQn, 0, 4); - nvic_irq_enable(I2C1_ER_IRQn, 0, 1); + nvic_irq_enable(I2C1_EV_IRQn, 0, 2); + nvic_irq_enable(I2C1_ER_IRQn, 0, 2); #endif } diff --git a/Src/util.c b/Src/util.c index 594f34c..dafc64a 100644 --- a/Src/util.c +++ b/Src/util.c @@ -570,17 +570,20 @@ int8_t i2c_writeBytes(uint8_t slaveAddr, uint8_t regAddr, uint8_t length, uint8_ i2c_nDABytes = length; i2c_nRABytes = 1; + uint16_t i2c_timeout = 0; + // enable the I2C0 interrupt i2c_interrupt_enable(MPU_I2C, I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV); // 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 i2c_start_on_bus(MPU_I2C); - + // 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; @@ -627,6 +630,8 @@ int8_t i2c_readBytes(uint8_t slaveAddr, uint8_t regAddr, uint8_t length, uint8_t i2c_nDABytes = length; i2c_nRABytes = 1; + uint16_t i2c_timeout = 0; + // enable the I2C0 interrupt 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 - 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 i2c_start_on_bus(MPU_I2C); // 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 i2c_status;