Page 1 of 1

SV#4 by calling chBSemSignal() in ADC callback function

Posted: Thu Dec 12, 2013 4:32 pm
by jnz
I get a "SV#4" chDbgPanic when I call chBSemSignal() on a binary semaphore in an ADC callback. I wonder why?
(MCU: STM32F407 [Discovery Board]), ChibiOS 2.6.1

Code: Select all

void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n)
{
    (void) buffer; (void) n;
    /* Note, only in the ADC_COMPLETE state because the ADC driver fires an
     intermediate callback when the buffer is half full.*/
    if (adcp->state == ADC_COMPLETE) {
        chBSemSignal(&AdcSema);
    }
}


Here is the GDB stacktrace:

Code: Select all

#0  port_halt () at ../../os/ports/GCC/ARMCMx/chcore.c:50
#1  0x08000a76 in chDbgPanic (msg=0x80049c8 "SV#4") at ../../os/kernel/src/chdebug.c:274
#2  0x080007aa in dbg_check_lock () at ../../os/kernel/src/chdebug.c:117
#3  0x080043fe in adccb (adcp=0x20000dfc, buffer=0x20000ef0, n=1) at main.c:79
#4  0x08002f5a in adc_lld_serve_rx_interrupt (adcp=0x20000dfc, flags=48)
    at ../../os/hal/platforms/STM32F4xx/adc_lld.c:94
#5  0x0800278e in Vector130 () at ../../os/hal/platforms/STM32F4xx/stm32_dma.c:352
#6  <signal handler called>
#7  0x55555554 in ?? ()
#8  0x55555554 in ?? ()


The problem seems to be triggered by "dbg_isr_cnt".

Code: Select all

stm32_dma.c:352
    CH_IRQ_PROLOGUE Makro
           chdebug.c:162 dbg_check_enter_isr()
                    chdebug.c:167  dbg_isr_cnt++;
    adc_lld.c:76 adc_lld_serve_rx_interrupt()
        adc_lld.c:94 _adc_isr_full_code()
            main.c:73 adccb()
                main.c:79 chBSemSignal()
                    chSysLock() Makro
                        chsys.h:144 dbg_check_lock()
                            chdebug.c:114 dbg_check_lock()
                                chdebug.c:116 dbg_isr_cnt != 0 -> SV#4

Re: SV#4 by calling chBSemSignal() in ADC callback function

Posted: Thu Dec 12, 2013 5:06 pm
by Giovanni
Hi,

It is a call protocol violation, from callbacks and ISRs you can only call I-Class functions and only within a critical zone. See "kernel concepts" in the documentation about function classes and the system state machine.

Also see this thread: viewtopic.php?f=16&t=1574

Giovanni

Re: SV#4 by calling chBSemSignal() in ADC callback function

Posted: Fri Dec 13, 2013 9:53 am
by jnz
Thank you for your quick and helpful answer, Giovanni.

This works for me now:

Code: Select all

    if (adcp->state == ADC_COMPLETE) {
        chSysLockFromIsr();
        chBSemSignalI(&AdcSema);
        chSysUnlockFromIsr();
    }