Page 1 of 1

Noob to Chibios trying to call pwmEnableChannel via a callback from a library

Posted: Wed Jul 31, 2019 10:45 am
by svenssonjoel
Hi everyone,

I am looking for pointers on how to debug an issue. I am attempting to alternate the PWM duty cycle using pwmEnableChannel and have verified via scope that this works when I call pwmEnableChannel from my main.c. But what I really want to do is call pwmEnableChannel via a callback that I register with a library. This, however, leads to a chSysHalt and the following stack-trace:

Code: Select all

(gdb) backtrace
#0  0x080094f4 in chSysHalt (
    reason=0x801eb4c <__func__.8261> "pwmEnableChannel")
    at ChibiOS_19.1.2/os/rt/src/chsys.c:206
#1  0x080136a4 in pwmEnableChannel.constprop.84 (channel=<optimized out>,
    width=<optimized out>, pwmp=0x200035a0 <PWMD2>)
    at ChibiOS_19.1.2/os/hal/src/hal_pwm.c:171
#2  0x0800f370 in ext_set_duty (args=<optimized out>, argn=<optimized out>)
    at main.c:180
#3  0x080018ea in apply_continuation ()
#4  0x08001aec in run_eval ()
#5  0x08001fc6 in eval_cps_program ()
#6  0x0800f17e in cmd_repl.lto_priv.141 (chp=0x20001af8 <SDU1>,
    argc=<optimized out>, argv=<optimized out>) at main.c:325
#7  0x0800d62c in cmdexec (argv=<optimized out>, argc=<optimized out>,
    name=<optimized out>, chp=<optimized out>, scp=<optimized out>)
    at ChibiOS_19.1.2/os/various/shell/shell.c:106
#8  shellThread (p=0x801dd0c <shell_cfg1>)
    at ChibiOS_19.1.2/os/various/shell/shell.c:401
#9  0x08000352 in _port_thread_start ()
    at ChibiOS_19.1.2/os/common/ports/ARMCMx/compilers/GCC/chcoreasm_v7m.S:122
Backtrace stopped: previous frame identical to this frame (corrupt stack?)



In this trace #5, #4, #3 takes place withing the external library code. at #2 it calls the callback (ext_set_duty) that in turn calls pwmEnableChannel which seems to lead to a "halt".

Any hints, pointers or ideas are very much appreciated. Thanks!

Re: Noob to Chibios trying to call pwmEnableChannel via a callback from a library

Posted: Wed Jul 31, 2019 11:13 am
by Giovanni
Hi,

I suppose you have the "state checker" enabled in chconf.h.

Your problem is that from ISRs and callbacks is only possible to call i-class functions, pwmEnableChannel() is not one of those. There should be an pwmEnableChannelI() function, note the final "I". In addition you need to call it in between a critical zone chSysLockFromISR/chSysUnlockFromISR.

See the "system states" diagram in the manual, section "kernel concepts". ChibiOS is very strict on API usage but once you get used to the convention then it becomes very hard to do things wrong.

Giovanni

Re: Noob to Chibios trying to call pwmEnableChannel via a callback from a library

Posted: Wed Jul 31, 2019 5:15 pm
by svenssonjoel
Thank you Giovanni,

"state checker" is on as far as I understand.

I tried using the chSysLockFromISR/chSysUnlockFromISR wrapped around a call to pwmEnableChannelI.
In this setup if I step through the ext_set_duty (function called from an external library, but defined in my main.c) it seems that the call to chSysLockFromISR takes a path that ends up in chSysHalt:

Code: Select all

(gdb) s
chSysLockFromISR () at ChibiOS_19.1.2/os/rt/include/chsys.h:397
397     _dbg_check_lock_from_isr();
(gdb) s
_dbg_check_lock_from_isr () at ChibiOS_19.1.2/os/rt/src/chdebug.c:176
176     if ((ch.dbg.isr_cnt <= (cnt_t)0) || (ch.dbg.lock_cnt != (cnt_t)0)) {
(gdb) s
177       chSysHalt("SV#6");
(gdb) s


I messed around a bit more (but it is just shooting from the hip) and replaced the chSysLockFromISR with calls to chSysLock and unlock. This version of the code does not end up in chSysHalt but the call to pwmEnableChannelI seems to have no effect at all instead.

I don't quite understand why I would be in any other state than "Normal" though, the ext_set_duty function is not called from an interrupt service routine or something like that. ext_set_duty is simply called from a thread (different from the thread that initialised and set initial pwm settings).

Thanks

Re: Noob to Chibios trying to call pwmEnableChannel via a callback from a library

Posted: Wed Jul 31, 2019 6:36 pm
by Giovanni
In ChibiOS callbacks are called in ISR context so definitely you should use "FromISR" versions.

See http://www.chibios.org/dokuwiki/doku.ph ... ebug_guide

Code: Select all

SV#6. The function chSysLockFromISR() has not been called from an ISR or has been called from within a critical zone. This can happen, for example, if you call chSysLockFromISR() twice.


Giovanni

Re: Noob to Chibios trying to call pwmEnableChannel via a callback from a library

Posted: Wed Jul 31, 2019 7:34 pm
by svenssonjoel
Thank you again.

I am very sorry but I think I may have been abusing the terminology. What I called a callback, the ext_set_duty function, is really just a regular function that I define in the same file as my main function. I then pass a pointer to ext_set_duty to routine defined in a library.

The program uses the "shell" example code. Shell commands call the library routines that in turn call the ext_set_duty function pointer.

So what happens in main is something like this:

- halInit
- chSysInit
- bunch of USB init (sdu)
- configure pwm on PA0 - PA3 (TIM2)
- shellInit
- while loop that spawns shell threads.

Thanks

Re: Noob to Chibios trying to call pwmEnableChannel via a callback from a library

Posted: Wed Jul 31, 2019 8:31 pm
by Giovanni
I suggest disabling optimization in order to debug with full info and see where the function fails exactly, look at the stack trace it should be an assertion, look at the condition.

Giovanni

Re: Noob to Chibios trying to call pwmEnableChannel via a callback from a library

Posted: Wed Jul 31, 2019 9:11 pm
by svenssonjoel
Thanks again.

It seems that changing the THD_WORKING_AREA_SIZE, increasing it, had positive effect.

#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(10*2048)

With that setting the version of ext_set_duty that uses the non-I version of pwmEnableChannel seems to work just fine. I guess my memory usage was pushing right up against the limits here (or more) and a crash was bound to happen at one place or another.

I can tell I have a lot to learn. These initial stumbling steps where fun though ;). Will read up some before wading deeper.

Thank you and have a great evening.