another need for advice on high priority interrupt

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

another need for advice on high priority interrupt

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: another need for advice on high priority interrupt

Post 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
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: another need for advice on high priority interrupt

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: another need for advice on high priority interrupt

Post 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
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: another need for advice on high priority interrupt

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: another need for advice on high priority interrupt

Post by Giovanni »

Interesting, I am curious about what GCC generates while compiling that.

Giovanni
Post Reply