Page 1 of 1

another need for advice on high priority interrupt

Posted: Wed May 08, 2013 3:06 pm
by alex31
Hello.

Still have some problem with high priority interrupt.

When my ISR has an priority < 2, program hangs after couple of minutes
with ISR >= 2, it run onvernight.

I thing i have not understood well how to deal with that.

i have high priority interrupt which fill a ring buffer
when condition is meet, i trig a pendSV interrupt,
and in the pendSV handler i do that :

Code: Select all

CH_IRQ_HANDLER(PendSVVector) {
 
  CH_IRQ_PROLOGUE();

  chSysLockFromIsr();
    chMBPostI (&xQueue, transEncodeIndexAsMsgt (&rtTransactionBuffer));
    chSysUnlockFromIsr();
  CH_IRQ_EPILOGUE();
}

static inline msg_t transEncodeIndexAsMsgt (volatile RtTransactionBuffer *rb)
{
  const msg_t retVal = rb->transStart_forPendSV | (rb->transEnd_forPendSV << 16);
  return retVal;
}

CH_FAST_IRQ_HANDLER(STM32_TIM1_UP_HANDLER) {
    tim1_isrHandler(); // fill circular buffer, does not call any chibi api, trig pendSV ISR when buffer is full enough
}

CH_FAST_IRQ_HANDLER(STM32_TIM1_CC_HANDLER) {
    tim1_isrHandler(); // fill circular buffer, does not call any chibi api, trig pendSV ISR when buffer is full enough
}



The problem is : if an high speed interrupt occurs during the execution of PendSVVector, it hangs.
In fact, if i remove chMBPostI from the pendsv ISR, of course nothing works since i don't fectch any value in the regular thread,
but at least chibios continue to live.

So, my question is, is there any thing to to to cope with high priority interrupt interrupting PendSV ISR during execution of chMBPostI ?

Alexandre

Re: another need for advice on high priority interrupt

Posted: Wed May 08, 2013 6:16 pm
by Giovanni
Hi,

You don't have to cope with fast interrupts at all, fast interrupts do not call the kernel and the kernel is not affected by them.

You have one thing in common between the PendSV and the fast interrupt, your fast queue, I would verify if you have a race condition there because your "queue put" function will be able to preempt the "queue get" called from PendSV. How do you implement mutual exclusion within the queue?

Giovanni

Re: another need for advice on high priority interrupt

Posted: Thu May 09, 2013 9:17 am
by alex31
Hi,

How do you implement mutual exclusion within the queue?


Well, i use a circular buffer with startChunck, endChunck, index.

Fast interrupt do
buffer[endChunck++] = value;

when a complete frame is received fast interrupt do
stop interrupt source for complete frame received
save startChunck and endChunck in startChunck_S and endChunck_S
let startChunck = endChunck
tigger pendSV

pendSV read saved version startChunck_S and endChunck_S, and send them via mailbox.
then reenable interrupt source for complete frame received.

I have made some test, and you are right, problem occurs when i get startChunck_S and endChunck_S,
if i post a constant value, it's works.

So i need a better mutual exclusion scheme, since i cannot use chibios sync object for that,
is there a safe way to do atomic test_and_set to achieve mutual exclusion ?

Alexandre

Re: another need for advice on high priority interrupt

Posted: Thu May 09, 2013 9:52 am
by Giovanni
chSysDisable(), chSysEnable()

those disable all interrupts and can be used for synchronization between fast interrupts and other code but by using them you add latency and jitter to the fast interrupts.

The alternative would be to use the Cortex special instructions for synchronization, basically you would implement a spinlock in the "slow" code making sure no "fast" code preempted you but I don't have an example at hand.

Giovanni

Re: another need for advice on high priority interrupt

Posted: Mon May 13, 2013 7:42 am
by alex31
Hi,

found the problem !

I didnt' set the pendsv priority, which is positionned at the highest priority in chcore v7m.c

I have gift lower priority to pendsv isr and now all is fine

Code: Select all

      
nvicSetSystemHandlerPriority(HANDLER_PENDSV,
                            CORTEX_PRIORITY_MASK(2));


To made more than 32 bits affection atomic, the simplest way i have found to implement spinlock is to use gcc intrinsics :

Code: Select all


#define LOCK_BUSY 1

typedef struct {
  bool_t flag;
} spinlock_t;

static inline void spin_lock (volatile spinlock_t *lock)
{
  while (__sync_lock_test_and_set(&lock->flag, LOCK_BUSY));
}

static inline void spin_unlock (volatile spinlock_t *lock)
{
  __sync_lock_release(&lock->flag);
}

static inline bool_t try_lock (volatile spinlock_t *lock)
{
  return (__sync_lock_test_and_set(&lock->flag, LOCK_BUSY));
}




Alexandre

Re: another need for advice on high priority interrupt

Posted: Mon May 13, 2013 8:08 am
by Giovanni
Interesting, I am curious about what GCC generates while compiling that.

Giovanni