Skip to content

Commit

Permalink
Added i2c timeout
Browse files Browse the repository at this point in the history
- i2c timeout to avoid getting stuck in while loop
  • Loading branch information
EFeru committed Dec 6, 2020
1 parent 570e115 commit ff9a135
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Src/i2c_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions Src/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
16 changes: 11 additions & 5 deletions Src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down

0 comments on commit ff9a135

Please sign in to comment.