Proper way to access shared data from IRQ

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Tinker
Posts: 14
Joined: Tue Feb 28, 2012 6:55 pm

Proper way to access shared data from IRQ

Post by Tinker »

Hi,

If I want to write to shared data from a normal IRQ (non-"fast") what is the correct way to access it safely without using chSysLockFromIsr? I want to use a semaphore but my code keeps crashing, so maybe I am using the wrong calls. (chBSemWait & chBSemSignal use chSysLock internally instead of chSysLockFromIsr, for example). The reason I want to use a semaphore instead of chSysLock is that I am copying a chunk of data and I don't want to affect latency in other IRQs. With a semaphore the latency hit should be minimal.

To summarize: what are the correct semaphore calls ( *I vs *S vs neither) ? Should I do it differently?

Thanks,
~t
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Proper way to access shared data from IRQ

Post by Giovanni »

The correct way is to use chSysLockFromIsr/chSysUnlockFromIsr from the ISR side and chSysLock/chSysUnlock from the thread size. You cannot use a semaphore nor a mutex because you cannot "wait" into an ISR.

Alternatively you may consider a mailbox where to insert the data to be passed to the thread without having to wait (until there is room of course).

Another approach is to use a worker thread doing the copying, this is done in the STM32 OTG driver but it is VERY complex to implement.

Giovanni
Post Reply