HardFault and debug with messagebox

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

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

HardFault and debug with messagebox

Postby omcdr » Mon Feb 13, 2017 3:23 pm

I have simple debug task with messagebox

Code: Select all

#define NUM_BUFFERS 16
#define BUFFERS_SIZE 16

static char buffers[NUM_BUFFERS][BUFFERS_SIZE];
static volatile uint32_t bufIndex;

static msg_t mbDebugQueue[NUM_BUFFERS];
static mailbox_t mbDebug;
uint32_t debugFlags;


void uart3Print(const char *string)
{
   uartStartSend(&UARTD3, strlen(string), string);
}

THD_WORKING_AREA(waThreadDebug, 128);
THD_FUNCTION(threadDebug, arg)
{
   msg_t msg;
   console_t *con;

   con = arg;

   while (1)
   {
      if(chMBFetch(&mbDebug, &msg, TIME_INFINITE) == MSG_OK)
      {
         uart3Print((char *) msg);
      }
      chThdSleepMilliseconds(100);
   }
}

void debugInit(void)
{
   debugFlags = DEBUG_FLAGS_DEFAULT;

   bufIndex = 0;
   chMBObjectInit(&mbDebug, mbDebugQueue, NUM_BUFFERS);
}

void debug(const char *format, ...)
{
   va_list ap;
   va_start(ap, format);

   vsnprintf(buffers[bufIndex % NUM_BUFFERS], BUFFERS_SIZE, format, ap);
   if(chMBPost(&mbDebug, (msg_t) buffers[bufIndex % NUM_BUFFERS], TIME_INFINITE) == MSG_OK)
      bufIndex++;
}

void debugI(const char *string)
{
   memcpy(buffers[bufIndex % NUM_BUFFERS], string, BUFFERS_SIZE);
   if(chMBPostI(&mbDebug, (msg_t) buffers[bufIndex % NUM_BUFFERS]) == MSG_OK)
      bufIndex++;
}


It works correctly with simple task and version debugI from ISR - I got debug messages on console.

Code: Select all

THD_FUNCTION(threadLed, arg)
{
   while (1)
   {
      debug("LED task\n\r");
      LED1_ON;
      chThdSleepMilliseconds(10);
      LED1_OFF;
      chThdSleepMilliseconds(1000);
   }
}



But with threadCanRx2 task I got only one debug message on console and hard fault exception occurs. With commented out debug function, task works.
Do you have idea what can be wrong ?

Code: Select all

THD_WORKING_AREA(waCanRx2, 256);
THD_FUNCTION(threadCanRx2, arg)
{
   event_listener_t evCan2rx, evCan2err;
   CANRxFrame rxmsg;
   eventmask_t mask;

   (void) arg;

   chRegSetThreadName("CAN2 RX");
   chEvtRegister(&CAND2.rxfull_event, &evCan2rx, 0);
   chEvtRegister(&CAND2.error_event, &evCan2err, 1);

   while (1)
   {
      debug("CAN2 task\n\r");

      if ((mask = chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(100))) == 0)
         continue;

      if (mask & EVENT_MASK(0))
      {
         if (canReceive(&CAND2, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE) == MSG_OK)
         {

         }
      }
      if (mask & EVENT_MASK(1))
      {
         errors |= chEvtGetAndClearFlags(&evCan2err);
      }
      chThdSleepMilliseconds(500);
   }
   chEvtUnregister(&CAND2.rxfull_event, &evCan2rx);
   chEvtUnregister(&CAND2.rxfull_event, &evCan2err);
}

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: HardFault and debug with messagebox

Postby Giovanni » Mon Feb 13, 2017 4:49 pm

Hi,

Are debug options in chconf.h enabled? assertions and state checker could give us an hint about the problem.

I suspect that you try to print a message while the previous is still being sent or something like this.

Giovanni

omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: HardFault and debug with messagebox

Postby omcdr » Mon Feb 13, 2017 9:12 pm

I have enabled

Code: Select all

#define CH_DBG_STATISTICS                   TRUE
#define CH_DBG_SYSTEM_STATE_CHECK           TRUE
#define CH_DBG_ENABLE_CHECKS                TRUE
#define CH_DBG_ENABLE_ASSERTS               TRUE
#define CH_DBG_ENABLE_TRACE                 TRUE
#define CH_DBG_ENABLE_STACK_CHECK           TRUE
#define CH_DBG_FILL_THREADS                 TRUE
#define CH_DBG_THREADS_PROFILING            FALSE


I have set breakpoint at CH_CFG_SYSTEM_HALT_HOOK, but can't catch it.

At now only threadCanRx2 has active debug fuction and has chThdSleepMilliseconds(5000) break, but problem still exist.

In my HardFault_Handler

Code: Select all

 
 #define FLT_USAGE 2
 #define FLT_INVSTATE 8
 
   volatile uint32_t hfsr = SCB->HFSR;
   volatile uint32_t cfsr = SCB->CFSR;
   volatile uint8_t fault = 0;
   volatile uint8_t reason = 0;
   
   if((cfsr & SCB_CFSR_USGFAULTSR_Msk) != 0)
   {
       fault = FLT_USAGE;
       (...)
       else if(((cfsr >> 16) & (1 << 1)) != 0)
       {
           reason = FLT_INVSTATE;
       }
   (...)
       


I got
hfsr: 0x40000000
cfsr: 0x20000
fault: 0x2
reason: 0x8


Could you explain how to debug this issue ?

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: HardFault and debug with messagebox

Postby Giovanni » Mon Feb 13, 2017 10:01 pm

Are you compiling using -O0 in Makefile? if not breakpoints can be missed because optimizations.

Try to simplify you application by commenting out parts, this could give an hint about the problem.

Giovanni

omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: HardFault and debug with messagebox

Postby omcdr » Tue Feb 14, 2017 1:35 pm

Yes, I disabled code optimization.

At this moment, here is my task, and problem still exist. I don't have idea what is wrong.

Code: Select all

THD_WORKING_AREA(waCanRx2, 256);
THD_FUNCTION(threadCanRx2, arg)
{
   (void) arg;

   while (1)
   {
      debug("CAN2 task\n\r");
      chThdSleepMilliseconds(5000);
   }
}

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: HardFault and debug with messagebox

Postby Giovanni » Tue Feb 14, 2017 1:43 pm

Hi,

Please give some info about toolchain, compiler etc.

Probably the problem is not where you are looking, just code snippets are not much helpful.

Giovanni

omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: HardFault and debug with messagebox

Postby omcdr » Tue Feb 14, 2017 2:58 pm

This is ChibiStudio 16 with ChibiOS 16.1.2 and gcc-arm-none-eabi-4_9-2015q3.
I will check on another version and prepare sample project.

omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: HardFault and debug with messagebox

Postby omcdr » Thu Feb 16, 2017 9:12 am

I have prepared small version of my project, here is full code

Code: Select all

#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include "ch.h"
#include "hal.h"

//*********************************************************************************************
// UART3
static void txend1(UARTDriver *uartp) {
   (void) uartp;
}

static void txend2(UARTDriver *uartp) {
   (void) uartp;
}

static void rxerr(UARTDriver *uartp, uartflags_t e) {

   (void) uartp;
   (void) e;
}

static void rxchar(UARTDriver *uartp, uint16_t c) {

   (void) uartp;
   (void) c;
}

static void rxend(UARTDriver *uartp) {

   (void) uartp;
}

static UARTConfig uart_cfg_3 = { txend1, txend2, rxend, rxchar, rxerr, 115200,
      0, USART_CR2_LINEN, 0 };

void uart3Print(const char *string) {
   uartStartSend(&UARTD3, strlen(string), string);
}

//*********************************************************************************************
// debug
#define NUM_BUFFERS 16
#define BUFFERS_SIZE 16

static char buffers[NUM_BUFFERS][BUFFERS_SIZE];
static volatile uint32_t bufIndex;

static msg_t mbDebugQueue[NUM_BUFFERS];
static mailbox_t mbDebug;

THD_WORKING_AREA(waThreadDebug, 128);

THD_FUNCTION( threadDebug, arg) {
   (void) arg;
   msg_t msg;

   while (1) {
      if (chMBFetch(&mbDebug, &msg, TIME_INFINITE) == MSG_OK) {
         uart3Print((char *) msg);
      }
//      chThdSleepMilliseconds(100);
   }
}

void debug(const char *format, ...) {
   va_list ap;
   va_start(ap, format);

   vsnprintf(buffers[bufIndex % NUM_BUFFERS], BUFFERS_SIZE, format, ap);
   if (chMBPost(&mbDebug, (msg_t) buffers[bufIndex % NUM_BUFFERS],
         TIME_INFINITE) == MSG_OK)
      bufIndex++;
}

//*********************************************************************************************
// tasks
THD_WORKING_AREA(waThreadLed, 128);

THD_FUNCTION( threadLed, arg) {
   (void) arg;

   while (1) {
      debug("LED task\n\r");
//      LED1_ON;
      chThdSleepMilliseconds(10);
//      LED1_OFF;
      chThdSleepMilliseconds(1000);
   }
}

THD_WORKING_AREA(waCanRx2, 256);
THD_FUNCTION( threadCanRx2, arg) {

   (void) arg;

   while (1) {
      debug("CAN2 task\n\r");

      chThdSleepMilliseconds(5000);
   }
}

//*********************************************************************************************
int main(void) {
   halInit();
   chSysInit();
   bufIndex = 0;

   uartStart(&UARTD3, &uart_cfg_3);
   uart3Print("Uart3 start...\r\n");

   chMBObjectInit(&mbDebug, mbDebugQueue, NUM_BUFFERS);

   chRegSetThreadName("main");
   chThdCreateStatic(waThreadDebug, sizeof(waThreadDebug), LOWPRIO + 20,
         threadDebug, NULL);
   chThdCreateStatic(waThreadLed, sizeof(waThreadLed), NORMALPRIO - 9,
         threadLed, NULL);
   chThdCreateStatic(waCanRx2, sizeof(waCanRx2), NORMALPRIO + 3, threadCanRx2,
         NULL);

   while (TRUE) {
      chThdSleepMilliseconds(100);
   }
}


Linker has error now, I have included $(CHIBIOS)/os/various/syscalls.c in Makefile, but I got this

Code: Select all

Compiling main.c
Linking build/tio.elf
`_sbrk_r' referenced in section `.text._malloc_r' of c:/chibistudio/tools/gnu tools arm embedded/4.7 2014q2/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libg.a(lib_a-mallocr.o): defined in discarded section `.text' of build/obj/syscalls.o (symbol from plugin)
`_sbrk_r' referenced in section `.text._malloc_r' of c:/chibistudio/tools/gnu tools arm embedded/4.7 2014q2/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libg.a(lib_a-mallocr.o): defined in discarded section `.text' of build/obj/syscalls.o (symbol from plugin)
`_sbrk_r' referenced in section `.text._malloc_trim_r' of c:/chibistudio/tools/gnu tools arm embedded/4.7 2014q2/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libg.a(lib_a-freer.o): defined in discarded section `.text' of build/obj/syscalls.o (symbol from plugin)
`_sbrk_r' referenced in section `.text._malloc_trim_r' of c:/chibistudio/tools/gnu tools arm embedded/4.7 2014q2/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libg.a(lib_a-freer.o): defined in discarded section `.text' of build/obj/syscalls.o (symbol from plugin)
`_sbrk_r' referenced in section `.text._malloc_trim_r' of c:/chibistudio/tools/gnu tools arm embedded/4.7 2014q2/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libg.a(lib_a-freer.o): defined in discarded section `.text' of build/obj/syscalls.o (symbol from plugin)
collect2.exe: error: ld returned 1 exit status
make: *** [build/tio.elf] Error 1


Full project is here https://files.fm/u/kpv2k4eq

Do you have any idea what happened ?

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: HardFault and debug with messagebox

Postby Giovanni » Thu Feb 16, 2017 9:17 am

Hi,

Try disabling garbage collection in the Makefile, apparently it is dropping functions from syscall.c. To be fixed.

Giovanni

omcdr
Posts: 89
Joined: Wed Aug 17, 2016 3:25 pm
Has thanked: 7 times
Been thanked: 7 times

Re: HardFault and debug with messagebox

Postby omcdr » Thu Feb 16, 2017 10:18 am

Do you mean option: USE_LINK_GC = no
I have set, but without success.


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 23 guests