Disable PWM on SysHalt?

Discussions and support about ChibiOS/RT, the free embedded RTOS.
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Location: Luleå, Sweden
Has thanked: 5 times
Been thanked: 6 times
Contact:

Disable PWM on SysHalt?

Postby Korken » Fri May 20, 2016 2:16 pm

Hi all!

Is there a way to add code that should disable PWM on a SysHalt?
I have the problem that when Chibi does a SysHalt and I am flying a quadrotor on it, the motor throttle PWM gets stuck and horror follows.
So I want to disable the PWM on SysHalt, but the problem I have is when I add a pwmStop (and include the headers for hal) I get an army of errors as:

Code: Select all

Compiling crt0_v7m.s
Compiling chcoreasm_v7m.s
Compiling crt1.c
Compiling vectors.c
Compiling chsys.c
In file included from ./ChibiOS/os/hal/include/hal.h:28:0,
                 from ./chconf.h:488,
                 from ./ChibiOS/os/rt/include/ch.h:71,
                 from ./ChibiOS/os/rt/src/chsys.c:36:
./ChibiOS/os/hal/osal/rt/osal.h: In function 'DisableMotors':
./ChibiOS/os/hal/osal/rt/osal.h:449:20: error: invalid storage class for function 'osalInit'
 static inline void osalInit(void) {
                    ^
./ChibiOS/os/hal/osal/rt/osal.h:460:20: error: invalid storage class for function 'osalSysHalt'
 static inline void osalSysHalt(const char *reason) {
                    ^
./ChibiOS/os/hal/osal/rt/osal.h: In function 'osalSysHalt':
./ChibiOS/os/hal/osal/rt/osal.h:462:3: warning: implicit declaration of function 'chSysHalt' [-Wimplicit-function-declaration]
   chSysHalt(reason);
   ^
./ChibiOS/os/hal/osal/rt/osal.h: In function 'DisableMotors':
./ChibiOS/os/hal/osal/rt/osal.h:470:20: error: invalid storage class for function 'osalSysDisable'
 static inline void osalSysDisable(void) {
                    ^
./ChibiOS/os/hal/osal/rt/osal.h: In function 'osalSysDisable':
./ChibiOS/os/hal/osal/rt/osal.h:472:3: warning: implicit declaration of function 'chSysDisable' [-Wimplicit-function-declaration]
   chSysDisable();
   ^
./ChibiOS/os/hal/osal/rt/osal.h: In function 'DisableMotors':
./ChibiOS/os/hal/osal/rt/osal.h:480:20: error: invalid storage class for function 'osalSysEnable'
 static inline void osalSysEnable(void) {
                    ^
./ChibiOS/os/hal/osal/rt/osal.h: In function 'osalSysEnable':
./ChibiOS/os/hal/osal/rt/osal.h:482:3: warning: implicit declaration of function 'chSysEnable' [-Wimplicit-function-declaration]
   chSysEnable();
   ^
./ChibiOS/os/hal/osal/rt/osal.h: In function 'DisableMotors':
./ChibiOS/os/hal/osal/rt/osal.h:491:20: error: invalid storage class for function 'osalSysLock'
 static inline void osalSysLock(voi


Code for disable motors:

Code: Select all

void RCOutputDisable(void)
{
    if (rcoutput_cfg != NULL)
    {
        pwmDisableChannel(rcoutput_cfg->pwmp_lowbank, 0, 0);
        pwmDisableChannel(rcoutput_cfg->pwmp_lowbank, 1, 0);
        pwmDisableChannel(rcoutput_cfg->pwmp_lowbank, 2, 0);
        pwmDisableChannel(rcoutput_cfg->pwmp_lowbank, 3, 0);

        pwmDisableChannel(rcoutput_cfg->pwmp_highbank, 0, 0);
        pwmDisableChannel(rcoutput_cfg->pwmp_highbank, 1, 0);
        pwmDisableChannel(rcoutput_cfg->pwmp_highbank, 2, 0);
        pwmDisableChannel(rcoutput_cfg->pwmp_highbank, 3, 0);

        pwmStop(rcoutput_cfg->pwmp_lowbank, rcoutput_cfg->pwmcfg);
        pwmStop(rcoutput_cfg->pwmp_highbank, rcoutput_cfg->pwmcfg);
    }
}


Hook definition:

Code: Select all

#if !defined(_FROM_ASM_)

static inline void DisableMotors(void)
{
  #include "ch.h"
  #include "hal.h"
  #include "rc_output.h"
  RCOutputDisable();
}

#endif
/**
 * @brief   System halt hook.
 * @details This hook is invoked in case to a system halting error before
 *          the system is halted.
 */
#define CH_CFG_SYSTEM_HALT_HOOK(reason)  DisableMotors()


Any idea on how to fix this?

Best
Emil

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Disable PWM on SysHalt?

Postby Giovanni » Fri May 20, 2016 4:44 pm

Hi,

You should use the hook CH_CFG_SYSTEM_HALT_HOOK, I recomment something like this:

Code: Select all

#define CH_CFG_SYSTEM_HALT_HOOK(reason) {                                   \
  /* System halt code here.*/                                               \
  extern void my_crash_code(void);                                          \
  chSysUnconditionalLock();                                                 \
  my_crash_code();                                                          \
}


You can call only I-Class functions from there so pwmStop() is not possible, you can disable PWM channels individually using pwmDisableChannelI().

Giovanni

User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Location: Luleå, Sweden
Has thanked: 5 times
Been thanked: 6 times
Contact:

Re: Disable PWM on SysHalt?

Postby Korken » Sat May 21, 2016 3:42 pm

Thanks Giovanni!
Simple and works a treat!


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 11 guests