Page 1 of 1

Proper way to access shared data from IRQ

Posted: Thu Feb 13, 2014 9:28 pm
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

Re: Proper way to access shared data from IRQ

Posted: Thu Feb 13, 2014 9:35 pm
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