add timeout to I2C to prevent blocking calls
This commit is contained in:
parent
5991b48a39
commit
bbce44cb8f
158
lib/i2c.c
158
lib/i2c.c
|
@ -28,6 +28,7 @@
|
|||
#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
|
||||
#include <libopencm3/stm32/timer.h> // timer utilities
|
||||
|
||||
#include "global.h" // global utilities
|
||||
#include "i2c.h" // I2C header and definitions
|
||||
|
@ -38,13 +39,22 @@
|
|||
#define I2C_I2C 2 /**< I2C peripheral */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup i2c_timer timer peripheral used for timeouts
|
||||
* @{
|
||||
*/
|
||||
#define I2C_TIMER 4 /**< timer peripheral */
|
||||
#define I2C_TIMEOUT 4 /**< timeout factor (compared to expected time) */
|
||||
/** @} */
|
||||
|
||||
|
||||
void i2c_master(bool fast)
|
||||
{
|
||||
// configure I2C peripheral
|
||||
rcc_periph_clock_enable(RCC_I2C_SCL_PORT(I2C_I2C)); // enable clock for I2C I/O peripheral
|
||||
gpio_set(I2C_SCL_PORT(I2C_I2C), I2C_SCL_PIN(I2C_I2C)); // already put signal high to avoid small pulse
|
||||
gpio_set_mode(I2C_SCL_PORT(I2C_I2C), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C_SCL_PIN(I2C_I2C)); // setup I2C I/O pins
|
||||
rcc_periph_clock_enable(RCC_I2C_SCL_PORT(I2C_I2C)); // enable clock for I2C I/O peripheral
|
||||
gpio_set(I2C_SDA_PORT(I2C_I2C), I2C_SDA_PIN(I2C_I2C)); // already put signal high to avoid small pulse
|
||||
gpio_set_mode(I2C_SDA_PORT(I2C_I2C), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C_SDA_PIN(I2C_I2C)); // setup I2C I/O pins
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function
|
||||
rcc_periph_clock_enable(RCC_I2C(I2C_I2C)); // enable clock for I2C peripheral
|
||||
|
@ -61,6 +71,28 @@ void i2c_master(bool fast)
|
|||
i2c_set_trise(I2C(I2C_I2C), (1000/(1000/(rcc_apb1_frequency/1000000)))+1); // max rise time for 100 kHz is 1000 ns (~1 MHz)
|
||||
}
|
||||
i2c_peripheral_enable(I2C(I2C_I2C)); // enable I2C after configuration completed
|
||||
|
||||
// configure time for timeouts
|
||||
rcc_periph_clock_enable(RCC_TIM(I2C_TIMER)); // enable clock for timer block
|
||||
timer_reset(TIM(I2C_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(I2C_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
|
||||
timer_one_shot_mode(TIM(I2C_TIMER)); // stop counter after update event (we only need to one timeout and reset before next operation)
|
||||
if (fast) {
|
||||
timer_set_prescaler(TIM(I2C_TIMER), rcc_ahb_frequency/400000-1); // set the prescaler so one tick is also one I2C bit (used I2C frequency)
|
||||
} else {
|
||||
timer_set_prescaler(TIM(I2C_TIMER), rcc_ahb_frequency/100000-1); // set the prescaler so one tick is also one I2C bit (used I2C frequency)
|
||||
}
|
||||
timer_set_period(TIM(I2C_TIMER), I2C_TIMEOUT*9); // use factor to wait for all 9 bits to be transmitted
|
||||
timer_update_on_overflow(TIM(I2C_TIMER)); // only use counter overflow as UEV source (use overflow as timeout)
|
||||
//timer_enable_irq(TIM(I2C_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
|
||||
|
||||
// wait one transaction for the signal to be stable (some slave have issues when an I2C transaction immediately follows)
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while ( !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF));
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
}
|
||||
|
||||
bool i2c_read(uint8_t slave, const uint8_t* address, size_t address_size, uint8_t* data, size_t data_size)
|
||||
|
@ -72,16 +104,34 @@ bool i2c_read(uint8_t slave, const uint8_t* address, size_t address_size, uint8_
|
|||
|
||||
bool to_return = false; // return if read succeeded
|
||||
|
||||
|
||||
// send start condition
|
||||
i2c_send_start(I2C(I2C_I2C)); // send start condition to start transaction
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_SB)); // wait until start condition is transmitted
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_SB) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until start condition is transmitted
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
//led_toggle();
|
||||
goto error;
|
||||
}
|
||||
if (!(I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL)) { // verify if in master mode
|
||||
goto error;
|
||||
}
|
||||
|
||||
// select slave
|
||||
i2c_send_7bit_address(I2C(I2C_I2C), slave, I2C_WRITE); // select slave
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_ADDR)); // wait until address is transmitted
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_ADDR) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until address is transmitted
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
goto error;
|
||||
}
|
||||
if (!((I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
|
||||
goto error;
|
||||
}
|
||||
|
@ -89,14 +139,39 @@ bool i2c_read(uint8_t slave, const uint8_t* address, size_t address_size, uint8_
|
|||
// send address
|
||||
for (size_t i=0; i<address_size; i++) {
|
||||
i2c_send_data(I2C(I2C_I2C), address[i]); // send memory address we want to read
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_TxE)); // wait until byte has been transmitted
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_TxE) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until byte has been transmitted
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
// switch to read mode
|
||||
i2c_send_start(I2C(I2C_I2C)); // send restart condition to switch from write to read mode
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_SB)); // wait until start condition is transmitted
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_SB) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until start condition is transmitted
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
goto error;
|
||||
}
|
||||
|
||||
i2c_send_7bit_address(I2C(I2C_I2C), slave, I2C_READ); // select slave
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_ADDR)); // wait until address is transmitted
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_ADDR) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until address is transmitted
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
goto error;
|
||||
}
|
||||
if ((I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_TRA)) { // verify we are in read mode (and read SR2 to clear ADDR)
|
||||
goto error;
|
||||
}
|
||||
|
@ -109,16 +184,32 @@ bool i2c_read(uint8_t slave, const uint8_t* address, size_t address_size, uint8_
|
|||
} else {
|
||||
i2c_enable_ack(I2C(I2C_I2C)); // ACK received byte to continue slave transmission
|
||||
}
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_RxNE)); // wait until byte has been received
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_RxNE) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until byte has been received
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
goto error;
|
||||
}
|
||||
data[i] = i2c_get_data(I2C(I2C_I2C)); // read received byte
|
||||
}
|
||||
to_return = true;
|
||||
|
||||
error:
|
||||
if (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_BUSY) { // release bus if busy
|
||||
i2c_send_stop(I2C(I2C_I2C)); // send stop to release bus
|
||||
// i2c_send_stop(I2C(I2C_I2C)); // send stop to release bus
|
||||
}
|
||||
i2c_send_stop(I2C(I2C_I2C)); // send stop to release bus
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while ((I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until bus released (non master mode)
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
}
|
||||
while (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL); // wait until bus released (non master mode)
|
||||
return to_return;
|
||||
}
|
||||
|
||||
|
@ -133,14 +224,32 @@ bool i2c_write(uint8_t slave, const uint8_t* address, size_t address_size, const
|
|||
|
||||
// send start condition
|
||||
i2c_send_start(I2C(I2C_I2C)); // send start condition to start transaction
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_SB)); // wait until start condition is transmitted
|
||||
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_SB) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until start condition is transmitted
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!(I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL)) { // verify if in master mode
|
||||
goto error;
|
||||
}
|
||||
|
||||
// select slave
|
||||
i2c_send_7bit_address(I2C(I2C_I2C), slave, I2C_WRITE); // select slave
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_ADDR)); // wait until address is transmitted
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_ADDR) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until address is transmitted
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
goto error;
|
||||
}
|
||||
if (!((I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_TRA))) { // verify we are in transmit mode (and read SR2 to clear ADDR)
|
||||
goto error;
|
||||
}
|
||||
|
@ -148,13 +257,29 @@ bool i2c_write(uint8_t slave, const uint8_t* address, size_t address_size, const
|
|||
// send address
|
||||
for (size_t i=0; i<address_size; i++) {
|
||||
i2c_send_data(I2C(I2C_I2C), address[i]); // send memory address we want to read
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_TxE)); // wait until byte has been transmitted
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_TxE) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until byte has been transmitted
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
// write data
|
||||
for (size_t i=0; i<data_size; i++) { // write bytes
|
||||
i2c_send_data(I2C(I2C_I2C), data[i]); // send byte to be written in memory
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_TxE)); // wait until byte has been transmitted
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while (!(I2C_SR1(I2C(I2C_I2C)) & I2C_SR1_TxE) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until byte has been transmitted
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
to_return = true;
|
||||
|
||||
|
@ -162,6 +287,13 @@ error:
|
|||
if (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_BUSY) { // release bus if busy
|
||||
i2c_send_stop(I2C(I2C_I2C)); // send stop to release bus
|
||||
}
|
||||
while (I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL); // wait until bus released (non master mode)
|
||||
timer_set_counter(TIM(I2C_TIMER),0); // restart timer
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_counter(TIM(I2C_TIMER)); // enable timer for timeouts
|
||||
while ((I2C_SR2(I2C(I2C_I2C)) & I2C_SR2_MSL) && !timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)); // wait until bus released (non master mode)
|
||||
timer_disable_counter(TIM(I2C_TIMER)); // disable timer for timeouts
|
||||
if (timer_get_flag(TIM(I2C_TIMER), TIM_SR_UIF)) { // timeout occurred
|
||||
timer_clear_flag(TIM(I2C_TIMER), TIM_SR_UIF); // clear flag
|
||||
}
|
||||
return to_return;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue