Skip to content

Commit

Permalink
add timeout to blocking i2c
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkscheper committed May 8, 2018
1 parent 02a35b8 commit b1c2dd1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
34 changes: 22 additions & 12 deletions sw/airborne/mcu_periph/i2c.c
Expand Up @@ -26,6 +26,7 @@
*/

#include "mcu_periph/i2c.h"
#include "mcu_periph/sys_time.h"

#if PERIODIC_TELEMETRY
#include "subsystems/datalink/telemetry.h"
Expand Down Expand Up @@ -287,55 +288,64 @@ bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t,
}

bool i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t,
uint8_t s_addr, uint8_t len)
uint8_t s_addr, uint8_t len)
{
t->type = I2CTransTx;
t->slave_addr = s_addr;
t->len_w = len;
t->len_r = 0;
if(!i2c_submit(p, t)){
if (!i2c_submit(p, t)) {
return false;
}

// Wait for transaction to complete
while(t->status == I2CTransPending || t->status == I2CTransRunning) {
;
float start_t = get_sys_time_float();
while (t->status == I2CTransPending || t->status == I2CTransRunning) {
if (get_sys_time_float() - start_t > 1.f) {
break; // timeout after 1 second
}
}
return true;
}

bool i2c_blocking_receive(struct i2c_periph *p, struct i2c_transaction *t,
uint8_t s_addr, uint16_t len)
uint8_t s_addr, uint16_t len)
{
t->type = I2CTransRx;
t->slave_addr = s_addr;
t->len_w = 0;
t->len_r = len;
if(!i2c_submit(p, t)){
if (!i2c_submit(p, t)) {
return false;
}

// Wait for transaction to complete
while(t->status == I2CTransPending || t->status == I2CTransRunning) {
;
float start_t = get_sys_time_float();
while (t->status == I2CTransPending || t->status == I2CTransRunning) {
if (get_sys_time_float() - start_t > 1.f) {
break; // timeout after 1 second
}
}
return true;
}

bool i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t,
uint8_t s_addr, uint8_t len_w, uint16_t len_r)
uint8_t s_addr, uint8_t len_w, uint16_t len_r)
{
t->type = I2CTransTxRx;
t->slave_addr = s_addr;
t->len_w = len_w;
t->len_r = len_r;
if(!i2c_submit(p, t)){
if (!i2c_submit(p, t)) {
return false;
}

// Wait for transaction to complete
while(t->status == I2CTransPending || t->status == I2CTransRunning) {
;
float start_t = get_sys_time_float();
while (t->status == I2CTransPending || t->status == I2CTransRunning) {
if (get_sys_time_float() - start_t > 1.f) {
break; // timeout after 1 second
}
}
return true;
}
12 changes: 6 additions & 6 deletions sw/airborne/mcu_periph/i2c.h
Expand Up @@ -256,7 +256,7 @@ extern void i2c_event(void);
* @return TRUE if insertion to the transaction queue succeeded
*/
extern bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t,
uint8_t s_addr, uint8_t len);
uint8_t s_addr, uint8_t len);

/** Submit a read only transaction.
* Convenience function which is usually preferred over i2c_submit,
Expand All @@ -268,7 +268,7 @@ extern bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t,
* @return TRUE if insertion to the transaction queue succeeded
*/
extern bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t,
uint8_t s_addr, uint16_t len);
uint8_t s_addr, uint16_t len);

/** Submit a write/read transaction.
* Convenience function which is usually preferred over i2c_submit,
Expand All @@ -281,7 +281,7 @@ extern bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t,
* @return TRUE if insertion to the transaction queue succeeded
*/
extern bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t,
uint8_t s_addr, uint8_t len_w, uint16_t len_r);
uint8_t s_addr, uint8_t len_w, uint16_t len_r);

/** Submit a write only transaction and wait for it to complete.
* Convenience function which is usually preferred over i2c_submit,
Expand All @@ -293,7 +293,7 @@ extern bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t,
* @return TRUE if insertion to the transaction queue succeeded
*/
bool i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t,
uint8_t s_addr, uint8_t len);
uint8_t s_addr, uint8_t len);

/** Submit a read only transaction and wait for it to complete.
* Convenience function which is usually preferred over i2c_submit,
Expand All @@ -305,7 +305,7 @@ bool i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t,
* @return TRUE if insertion to the transaction queue succeeded
*/
bool i2c_blocking_receive(struct i2c_periph *p, struct i2c_transaction *t,
uint8_t s_addr, uint16_t len);
uint8_t s_addr, uint16_t len);

/** Submit a write/read transaction and wait for it to complete.
* Convenience function which is usually preferred over i2c_submit,
Expand All @@ -318,7 +318,7 @@ bool i2c_blocking_receive(struct i2c_periph *p, struct i2c_transaction *t,
* @return TRUE if insertion to the transaction queue succeeded
*/
bool i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t,
uint8_t s_addr, uint8_t len_w, uint16_t len_r);
uint8_t s_addr, uint8_t len_w, uint16_t len_r);
/** @}*/
/** @}*/

Expand Down

0 comments on commit b1c2dd1

Please sign in to comment.