Access to I2C from ISR handler in HAL without RTOS

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
DrTon
Posts: 6
Joined: Sat Oct 22, 2016 9:30 pm

Access to I2C from ISR handler in HAL without RTOS

Postby DrTon » Thu Oct 18, 2018 10:53 pm

Hello!
I have device on STM32L051 (battery monitor) that uses I2C, UART and GPIO, now implemented using STM32 HAL.
Most of time CPU is in STOP mode, wakes up by EXTI 4 times per second, makes I2C transmission and sleeps again.
On STM32 HAL I work with I2C from EXTI handler with no problems.

Now I want to move to ChibiOS 18 HAL. STOP mode looks a bit complicated when using RTOS and I actually don't need scheduler and other RTOS features on this small application, so I'm trying to run HAL without RTOS, using OSAL from hal/osal/os-less/ARMCMx. Everything works until I try to use I2C from EXTI callback:

Code: Select all

void alert_cb(void *arg) {
    (void) arg;

    osalSysLockFromISR();
    do_some_i2c_transmissions();
    osalSysUnlockFromISR();
}


Then it spins forever in this loop inside osalThreadSuspendS called from i2cMasterTransmitTimeout:
hal/osal/os-less/ARMCMx/osal.c:209

Code: Select all

  while (self.message == MSG_WAIT) {
    osalSysUnlock();
    /* A state-changing interrupt could occur here and cause the loop to
       terminate, an hook macro is executed while waiting.*/
    OSAL_IDLE_HOOK();
    osalSysLock();
  }


As I understand I can't work with I2C from ISR handler? With NIL RTOS I can use semaphores to pass event from ISR to thread and it works (tried already), but how to solve it without RTOS?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Access to I2C from ISR handler in HAL without RTOS

Postby Giovanni » Fri Oct 19, 2018 8:59 pm

Hi,

The I2C driver has a synchronous API, you cannot use any synchronous function from ISR context, you cannot suspend or wait in an ISR.

You need to use I2C from application context, you may trigger OSAL events in the ISR and listen to those in your application, in response of events you can perform I2C operations.

If I may ask, what is the rationale for not using an RTOS? it is pretty convenient for this kind of things.

Giovanni

DrTon
Posts: 6
Joined: Sat Oct 22, 2016 9:30 pm

Re: Access to I2C from ISR handler in HAL without RTOS

Postby DrTon » Sun Oct 21, 2018 8:21 pm

Thank you! Will try with events.

First I wanted to use NIL kernel, and 3 threads (GPIO, UART, I2C), but then how to go to STOP mode? As I understand I need to synchronize all threads before going to STOP. Also I make decision to STOP or not according to state of variables in all threads. To do things correctly I will need a lot of condvars, mutexes etc. Probably solution is in the middle: use NIL but run all tasks in one thread to simplify things, need to try.


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 5 guests