I2C: add timeout to send start condition
sometimes the internal state f the I2C peripheral is confused, and sending a start condition is not possible and the corresponding registers will not be set. see the errata for more details. as workaround a timer is used, resetting the I2C is case it times out
This commit is contained in:
parent
5e13d19bda
commit
396c7852e7
|
@ -15,7 +15,7 @@
|
|||
/** library to communicate using I2C as master (code)
|
||||
* @file i2c_master.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017-2018
|
||||
* @date 2017-2019
|
||||
* @note peripherals used: I2C
|
||||
*/
|
||||
|
||||
|
@ -24,6 +24,7 @@
|
|||
#include <stdlib.h> // general utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencm3/cm3/systick.h> // SysTick library
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/i2c.h> // I2C library
|
||||
|
@ -288,24 +289,50 @@ enum i2c_master_rc i2c_master_start(uint32_t i2c)
|
|||
while (true);
|
||||
}
|
||||
|
||||
bool retry = true; // retry after reset if first try failed
|
||||
enum i2c_master_rc to_return; // return code
|
||||
try:
|
||||
to_return = I2C_MASTER_RC_NONE; // return code
|
||||
// send (re-)start condition
|
||||
if (I2C_CR1(i2c) & (I2C_CR1_START|I2C_CR1_STOP)) { // ensure start or stop operations are not in progress
|
||||
return I2C_MASTER_RC_START_STOP_IN_PROGESS;
|
||||
}
|
||||
// prepare timer in case the peripheral hangs on sending stop condition (see errata 2.14.4 Wrong behavior of I2C peripheral in master mode after a misplaced Stop)
|
||||
systick_counter_disable(); // disable SysTick to reconfigure it
|
||||
systick_set_frequency(500, rcc_ahb_frequency); // set timer to 2 ms (that should be long enough to send a start condition)
|
||||
systick_clear(); // reset SysTick (set to 0)
|
||||
systick_interrupt_disable(); // disable interrupt to prevent ISR to read the flag
|
||||
systick_get_countflag(); // reset flag (set when counter is going for 1 to 0)
|
||||
i2c_send_start(i2c); // send start condition to start transaction
|
||||
while ((I2C_CR1(i2c) & I2C_CR1_START) && !(I2C_SR1(i2c) & (I2C_SR1_BERR|I2C_SR1_ARLO))); // wait until start condition has been accepted and cleared
|
||||
if (I2C_SR1(i2c) & (I2C_SR1_BERR|I2C_SR1_ARLO)) {
|
||||
return I2C_MASTER_RC_BUS_ERROR;
|
||||
bool timeout = false; // remember if the timeout has been reached
|
||||
systick_counter_enable(); // start timer
|
||||
while ((I2C_CR1(i2c) & I2C_CR1_START) && !(I2C_SR1(i2c) & (I2C_SR1_BERR|I2C_SR1_ARLO)) && !timeout) { // wait until start condition has been accepted and cleared
|
||||
timeout |= systick_get_countflag(); // verify if timeout has been reached
|
||||
}
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_SB) && !(I2C_SR1(i2c) & (I2C_SR1_BERR|I2C_SR1_ARLO))); // wait until start condition is transmitted
|
||||
if (I2C_SR1(i2c) & (I2C_SR1_BERR|I2C_SR1_ARLO)) {
|
||||
return I2C_MASTER_RC_BUS_ERROR;
|
||||
to_return = I2C_MASTER_RC_BUS_ERROR;
|
||||
}
|
||||
if (!(I2C_SR2(i2c) & I2C_SR2_MSL)) { // verify if in master mode
|
||||
return I2C_MASTER_RC_NOT_MASTER;
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_SB) && !(I2C_SR1(i2c) & (I2C_SR1_BERR|I2C_SR1_ARLO)) && !timeout && I2C_MASTER_RC_NONE == to_return) { // wait until start condition is transmitted
|
||||
timeout |= systick_get_countflag(); // verify if timeout has been reached
|
||||
}
|
||||
if (I2C_SR1(i2c) & (I2C_SR1_BERR|I2C_SR1_ARLO)) {
|
||||
to_return = I2C_MASTER_RC_BUS_ERROR;
|
||||
} else if (!(I2C_SR1(i2c) & I2C_SR1_SB)) { // the start bit has not been set although we the peripheral is not busy anymore
|
||||
to_return = I2C_MASTER_RC_BUS_ERROR;
|
||||
} else if (!(I2C_SR2(i2c) & I2C_SR2_MSL)) { // verify if in master mode
|
||||
to_return = I2C_MASTER_RC_NOT_MASTER;
|
||||
} else if (timeout) { // timeout has been reached, i.e. the peripheral hangs
|
||||
to_return = I2C_MASTER_RC_NOT_MASTER;
|
||||
}
|
||||
|
||||
return I2C_MASTER_RC_NONE;
|
||||
if (I2C_MASTER_RC_NOT_MASTER == to_return && retry) { // error happened
|
||||
retry = false; // don't retry a second time
|
||||
I2C_CR1(i2c) |= I2C_CR1_SWRST; // assert peripheral reset
|
||||
I2C_CR1(i2c) &= ~I2C_CR1_SWRST; // release peripheral reset
|
||||
goto try;
|
||||
}
|
||||
systick_counter_disable(); // we don't need to timer anymore
|
||||
return to_return;
|
||||
}
|
||||
|
||||
enum i2c_master_rc i2c_master_select_slave(uint32_t i2c, uint16_t slave, bool address_10bit, bool write)
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
/** library to communicate using I2C as master (API)
|
||||
* @file i2c_master.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017-2018
|
||||
* @date 2017-2019
|
||||
* @note peripherals used: I2C
|
||||
*/
|
||||
#pragma once
|
||||
|
|
Loading…
Reference in New Issue