Cortex M0 interrupts question

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

aport
Posts: 36
Joined: Sat May 10, 2014 7:35 pm
Has thanked: 2 times
Been thanked: 4 times

Cortex M0 interrupts question

Post by aport »

The following is my current understanding of how this works, but I'm probably wrong and I'd like to know what's going on.

In the i2c driver, osalSysLock() and osalSysUnlock() surround common i2c operations, like master_transmit. In the Cortex-M0 port these functions disable and enable interrupts, respectively.
Low-level i2c drivers (like kinetis and stm32) suspend the calling thread in master_transmit and wake up the thread from within the i2c ISR. What I don't get is how this works at all, since interrupts were just disabled.

Can someone help explain how this works? Thank you very much!
skute
Posts: 64
Joined: Wed Aug 29, 2012 10:17 pm

Re: Cortex M0 interrupts question

Post by skute »

Interrupts are re-enabled following the context switch to the next runnable task.

In general ChibiOS works like this:

1) Task calls OS wait/pend function
2) OS calls chSysLock()
3) Check if the resource is available (assume it is not)
4) If resource is unavailable, perform context switch to highest runnable task
5) Newly running task calls chSysUnlock()

The cortex-m task switching is harder to follow than the other ports. I followed it through one day and wrote a comment to myself which I'll share here and Giovanni can correct any mistakes. It also assumes the 'simple' interrupt behavior for cortex-m3 (probably the same for cortex-m0).

/*--------------------------------------------------------------------------*
* Post-IRQ switch code.
*
* Exception handlers return here for context switching.
*
* _port_switch_from_isr is called from the task that was interrupted with
* interrupts disabled after exiting the last ISR via artificial exception
* return context nwhen a new thread needs to be swapped in. It will call
* chSchDoReschedule which will begin running the new thread and the previous
* thread will restart from the next instruction after chSchDoReschedule when
* it is resumed. When it is resumed it will trigger the PendSV interrupt
* (highest priority in the system) and re-enable interrupts and the PendSV
* handler will switch back to the task.
*
* The new thread is started with interrupts disabled. If it was sleeping,
* the task was waiting for a mutex for example, interrupts will be re-enabled
* by the task when it resumes (if desired).
*--------------------------------------------------------------------------*/
.global _port_switch_from_isr
.type _port_switch_from_isr, %function
.thumb_func
_port_switch_from_isr:
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Cortex M0 interrupts question

Post by Giovanni »

Correct.

Context switching is always performed between a lock and an unlock. So usually it goes like this:

Thread 1: lock
Thread 1: switch to 2
Thread 2: switched from 1
Thread 2: unlock

In some places the code looks like it disables interrupts and never enables them, in reality interrupts are enabled "on the other side". Critical zones are always very short by design.

Giovanni
aport
Posts: 36
Joined: Sat May 10, 2014 7:35 pm
Has thanked: 2 times
Been thanked: 4 times

Re: Cortex M0 interrupts question

Post by aport »

Ahh... thanks for that breakdown!

I'm trying to use the ChibiOS HAL with FreeRTOS, but my problem is that freertos (and likely other RTOSes) don't have this behaviour of re-enabling interrupts.
This means that mapping osalSysLock() to portENTER_CRITICAL() results in a dead system.

I suppose I could call portEXIT_CRITICAL() before calling any function that reschedules in my osal layer?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Cortex M0 interrupts question

Post by Giovanni »

The OSAL has also been implemented on HW without RTOS, probably you could give a look to that version: /os/hal/osal/os-less/ARMCMx. Probably it is easier to understand.

The OSAL expects the OS to be able to call a "wait" function from within a critical zone on thread side and to perform a "signal" within a critical zone on the ISR side, I am not sure if/how this translates on FreeRTOS. So far OSALs have been created for ChibiOS/RT, NIL and an OSEK variant.

Giovanni
aport
Posts: 36
Joined: Sat May 10, 2014 7:35 pm
Has thanked: 2 times
Been thanked: 4 times

Re: Cortex M0 interrupts question

Post by aport »

Excellent, I think I understand now.

In all S-class functions in osal, if the function reschedules, I added a call to portENABLE_INTERRUPTS() before rescheduling. It's working now but I don't know if this is the best way to do it.

Thanks for the help
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Cortex M0 interrupts question

Post by Giovanni »

Calling S-class functions from within critical zones is done in order to be able to test a volatile condition and go to sleep atomically. The condition is volatile because it can be changed by ISRs, probably enabling interrupts before going to sleep introduces a race condition.

An example is the "state" variable of drivers, the state is checked/changed both at thread level and ISR level.

Giovanni
aport
Posts: 36
Joined: Sat May 10, 2014 7:35 pm
Has thanked: 2 times
Been thanked: 4 times

Re: Cortex M0 interrupts question

Post by aport »

Hmm... the problem is that when FreeRTOS suspends a task and reschedules, it expects interrupts are enabled because it triggers a PendSV.
Therefore I have to enable interrupts immediately before calling a FreeRTOS API function that reschedules. I think that at that point, volatile conditions have been checked already.
I can disable interrupts again after the reschedule function so that the caller of the S-class function expect to be in a critical section when it returns.


Code: Select all

void someFunctionS(void) {
  // test some variables
  // do more work
  // looks like we need to sleep!
  portEXIT_CRITICAL();
  FreeRTOS_Function_That_Reschedules();
  portENTER_CRITICAL();
  // maybe some more stuff here
}

void someFunctionThatCallsSClass(void) {
  osalSysLock();
  someFunctionS();
  osalSysUnlock();
}


Would this be safe?

I understand if this is off-topic and you aren't interested in discussion.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Cortex M0 interrupts question

Post by Giovanni »

Hi,

I understand if this is off-topic and you aren't interested in discussion.


Don't worry, it is interesting.

I think it is not safe, consider this actual code:

Code: Select all

void spiExchange(SPIDriver *spip, size_t n,
                 const void *txbuf, void *rxbuf) {

  osalSysLock();
  spiStartExchangeI(spip, n, txbuf, rxbuf);
  (void) osalThreadSuspendS(&spip->thread);
  osalSysUnlock();
}


The SPI transfer is started and then the thread goes to sleep waiting for completion. If you release the critical zone inside the S function then you could get the SPI interrupt before going into sleep and your thread would sleep there forever.

This assumption that the switch can be synchronously performed is used in many places in the HAL. The code could be rewritten to not use a critical zone using a binary semaphore instead of that osalThreadSuspendS() but that would take 12 bytes (semaphore) vs just 4 (thread pointer) and semaphore handling is usually slower because counter, queue etc. You could try replacing pointers with semaphores but I am not sure that would cover all cases.

Giovanni
aport
Posts: 36
Joined: Sat May 10, 2014 7:35 pm
Has thanked: 2 times
Been thanked: 4 times

Re: Cortex M0 interrupts question

Post by aport »

That is a good demonstration.

I'm not sure if I could implement this without modifying FreeRTOS sources, which is not an appealing solution. I also don't want to modify the ChibiOS HAL.

Perhaps I could minimize modification by adding a hook function in FreeRTOS which is called after a context switch.

Thank you for explaining this. I see why it was implemented this way and it looks to be a good architecture.
Post Reply