I2C Async Combinable
Posted: Wed Oct 11, 2017 7:46 pm
I'm implementing Async Combinable, and things seem to be playing nicely. For others in need of a starting point in the future, here is a helper function for any task that needs an 8-bit register read.
Input is appreciated - I'm curious to know if know if I did something wrong, like for example calling i2c.operation_complete():, even-though my stopbit is zero.
Input is appreciated - I'm curious to know if know if I did something wrong, like for example calling i2c.operation_complete():, even-though my stopbit is zero.
Code: Select all
static char i2cReady =1;
uint8_t i2c_read_reg_async(client i2c_master_async_if i2c , uint8_t device_addr, uint8_t reg, i2c_regop_res_t &result)
{
uint8_t a_reg[1] = {reg};
uint8_t data[1] = {0};
size_t n =1;
i2c.write(device_addr, a_reg, 1, 0);
i2cReady = 0;
while(i2cReady == 0)
{
select{
case i2c.operation_complete():
i2c_res_t result;
unsigned num_bytes_sent;
result = i2c.get_write_result(num_bytes_sent);
if (num_bytes_sent != 1)
{
printf("\nError writing register to read from on I2C bus. Result = %d\n", result);
result = I2C_REGOP_DEVICE_NACK;
i2c.send_stop_bit();
i2cReady = 1;
return 0;
}
i2cReady = 1;
break;
}
}
i2c.read(device_addr, n, 1);
i2cReady = 0;
while(i2cReady == 0)
{
select{
case i2c.operation_complete():
i2c_res_t result;
unsigned num_bytes_sent;
result = i2c.get_read_data(data, n);
if (result == I2C_ACK) {
result = I2C_REGOP_SUCCESS;
} else {
result = I2C_REGOP_DEVICE_NACK;
printf("\nError reading on I2C bus. Result = %d\n", result);
}
i2cReady = 1;
break;
}
}
return data[0];
}