-
Notifications
You must be signed in to change notification settings - Fork 983
Open
Labels
Description
In my program I read the sensor each 10 sec. After some time the MCU stop working always on the same line. I print output before calling the sensor and also inside the sensor driver to catch this issue. I have already optimized the driver code to prevent all heap allocations and for the first time it seems to improve the behavior, but after some repeated tests it shows that nothing has really changed.
// this is called each 10 sec and calls the driver implementation
// the caller implements an println(err.Error()) in case of an error is returned
func (as *AirSensor) RefreshMeasurement() error {
// TODO: this call seems to hang after some time if the sensor is not present
if !as.driver.Connected() {
println("ss0E")
return errors.New("BME280 not connected")
}
...
// Connected returns whether a BME280 has been found.
// It does a "who am I" request and checks the response.
// dataBuf [8]byte, regBuf [2]byte
func (d *Device) Connected() bool {
println("b0")
//data := []byte{0}
d.dataBuf[0] = 0
data := d.dataBuf[:1]
println("b1")
//legacy.ReadRegister(d.bus, uint8(d.Address), WHO_AM_I, data)
//d.bus.Tx(uint16(d.Address), []byte{WHO_AM_I}, data)
d.regBuf[0] = WHO_AM_I
d.bus.Tx(uint16(d.Address), d.regBuf[:1], data)
println("b2")
return data[0] == CHIP_ID
}
If working like expected, the output looks like this:
b0
b1
b2
ss0E
BME280 not connected
In case of stuck it looks always like this;
b0
b1
So I had a look to the TX implementation of tinygo. May it be possible, that in case of error there is no return path of the loop and this causes the issue?:
for i2c.Bus.EVENTS_STOPPED.Get() == 0 {
// Allow scheduler to run
gosched()
// Handle errors by ensuring STOP sent on bus
if i2c.Bus.EVENTS_ERROR.Get() != 0 {
if i2c.Bus.EVENTS_STOPPED.Get() == 0 {
// STOP cannot be sent during SUSPEND
i2c.Bus.TASKS_RESUME.Set(1)
i2c.Bus.TASKS_STOP.Set(1)
}
err = twiCError(i2c.Bus.ERRORSRC.Get())
}
}