6 years, 11 months ago.

X_NUCLEO_IKS01A1 I2C write timeout in ticker under mbed OS 5

The code below fails to read the HTS221 and LPS25H sensors on the X_NUCLEO_IKS01A1 expansion board, if it is called from a ticker. However, when the same function spins in a while loop, the sensors are read correctly.

#include "rtos.h"
#include "x_nucleo_iks01a1.h"

auto sensors = X_NUCLEO_IKS01A1::Instance(D14, D15);
HumiditySensor* hSensor = sensors->ht_sensor;
TempSensor* tSensor = sensors->ht_sensor;
PressureSensor* pSensor = sensors->pt_sensor;

void isr()
{
	float tmp;
	hSensor->GetHumidity(&tmp);
	printf("%0.2f%%\r\n", tmp);
	tSensor->GetTemperature(&tmp);
	printf("%0.2fC\r\n", tmp);
	pSensor->GetPressure(&tmp);
	printf("%0.2fmbar\r\n", tmp);
}

int main() 
{
	Ticker ticker;
	ticker.attach(isr, 1);

	Thread::wait(osWaitForever);
}

Stepping through the code, when it is called by the ticker, using Visual GDB, eventually leads to line 4 of the block below (found in the i2c_write function in i2c_api.c) being hit. The code does not enter this block when the function spins in a loop.

if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
            DEBUG_PRINTF(" TIMEOUT or error in i2c_write\r\n");
            /* re-init IP to try and get back in a working state */
            i2c_init(obj, obj_s->sda, obj_s->scl);
         } else {
            count = length;
       }

I have a program that uses the HTS221 and LPS25H sensors on the IKS01A1, which calls a function which is almost identical to the one above from a ticker, that runs under mbed OS 2, which does not exhibit the same behaviour.

Is anyone else experiencing similar issues with the IKS01A1 library under mbed 5.4.2? There does not appear to be any info about this issue on the IKS01A1 library page.

I am using Visual GDB and GCC 5.3 with mbed 5.4.2 on an ST Nucleo F401RE.

1 Answer

6 years, 11 months ago.

I2C inside a ticker or ISR doesnt work anymore when using recent mbed libs on STM platforms. The I2C uses interrupts as well and due to interrupt priorities the I2C stalls. See discussion here. I hope it can be fixed because it doesnt look right to me...

Accepted Answer

That is very frustrating, hopefully ST will fix this soon. Thanks for the response.

posted by Robert O'Connor 01 May 2017