Page 1 of 1

Softwire library blocks in case of hardware failure

Posted: Mon Mar 30, 2020 9:57 am
by Phono
Hi all,
i have interfaced a MPU6050 with an Olimexino STM32 board using the Softwire library. I was performing tests, and I tried to short SCL and SDA to produce an error and check whether it is correctly handled in my code. To my surprise, doing so blocks the execution somewhere in the library, so the called function does not return FALSE, and the error handling cannot take place.
Is anyone aware of this problem? I develop using the Arduino IDE, so no debug is possible.

Re: Softwire library blocks in case of hardware failure

Posted: Mon Mar 30, 2020 1:33 pm
by fpiSTM
There is no softWire library in the STM32 core. I guess you use Roger's core (leaflab)?

Re: Softwire library blocks in case of hardware failure

Posted: Tue Mar 31, 2020 8:12 am
by Phono
Exactly. I found a place where a dead loop can occur. I added a simple timeout mechanism and it helped. The added lines are identified by the comment: JMD 30 March 2020

Code: Select all

void SoftWire::set_scl(bool state) {
	int Timeout = 10000 ;		// JMD 30 March 2020
	
    I2C_DELAY(this->i2c_delay);

	gpio_write_bit(sclDevice,sclBit, state);
//    digitalWrite(this->scl_pin,state);
    //Allow for clock stretching - dangerous currently
    if (state == HIGH) {
        while(digitalRead(this->scl_pin) == 0)		// JMD 30 March 2020
						if ( Timeout-- == 0 )		// JMD 30 March 2020
							return ;		// JMD 30 March 2020
    }
}
If there is a better way, I would appreciate.