You can do a very basic timeout like this:
I2c& I2c::operator<< (operation_t operation)
{
int count = 0;
const int TIMEOUT = 4242;
switch (operation) {
case Start:
i2c_send_start(_reg);
while (!checkEvent(SR2(I2C_SR2_BUSY | I2C_SR2_MSL) | I2C_SR1_SB)
&& count != TIMEOUT) {
count++;
}
break;
case Stop:
i2c_send_stop(_reg);
break;
case Wait_End:
while (!checkEvent(I2C_SR1_TxE) && count != TIMEOUT) {
count++;
}
break;
}
if (count == TIMEOUT) {
//error handling
return -1;
}
return *this;
}
Of course if you need accurate timeout and don't want to waste processing time increasing the count
variable you should use hardware timers.