_unhandled_exception when running a chThdSleep (running chibi from a bootloader)

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:

_unhandled_exception when running a chThdSleep (running chibi from a bootloader)

Postby Korken » Sun Jul 29, 2018 5:24 pm

Hi all,

I have been testing to write a bootloader, and all seems fine.
ChibiOS starts up after the bootloader calls the binary, initialization works, HAL init goes fine and LEDs go on.

However, whenever I call a chThdSleep, I get an _unhandled_exception and it seems to happen on the return to _idle_thread.

Has anyone run into a similar issue before? The interesting part is that this only happens when going through the bootloader.
So my question is, is there something that ChibiOS does not reset at startup? Something in the NVIC perhaps?

User avatar
wurstnase
Posts: 68
Joined: Tue Oct 17, 2017 2:24 pm
Has thanked: 26 times
Been thanked: 9 times
Contact:

Re: _unhandled_exception when running a chThdSleep (running chibi from a bootloader)

Postby wurstnase » Sun Jul 29, 2018 6:08 pm

I had some issues when jumping from os-less chibi bootloader to a none chibios application.

Finally I reset also CONTROL and PRIMASK register and going an extra round with a NVIC_SystemReset().
I write a magic code into a backup register (e.g. BKP->DR42 = 0x4242). If this key exist after bootup, I don't start any xxxInit() and jump directly to my application.

In total something like:

Code: Select all

if (BKP->DR42 == 0x4242) {
  BKP->DR42 = 0x0;

  typedef void (*pFunction)(void);
  pFunction Jump_To_Application; uint32_t JumpAddress;

  JumpAddress = *(__IO uint32_t *)(APP_ADDRESS + 4);
  Jump_To_Application = (pFunction)JumpAddress;

  /* Clear pending interrupts just to be on the safe side */
  SCB->ICSR = SCB_ICSR_PENDSVCLR_Msk;

  /* Disable the SysTick */
  SysTick->CTRL = 0;
  SysTick->LOAD = 0;
  SysTick->VAL  = 0;
  /* Disable all interrupts */
  for(uint8_t i=0; i<8; i++)
    NVIC->ICER[i] = NVIC->IABR[i];

  SCB->VTOR = APP_ADDRESS;
  __set_CONTROL(0);
  __set_PRIMASK(0);
  __DSB();

  __set_MSP(*(__IO uint32_t *)APP_ADDRESS);
  Jump_To_Application();
}
\o/ Nico

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: _unhandled_exception when running a chThdSleep (running chibi from a bootloader)

Postby Korken » Mon Jul 30, 2018 1:34 pm

Hi, thanks for the tips!

I'm doing exactly the same, inside the bootloader I am also using a "magic value" (in RAM though) to read after a soft reset, only to set addresses and VTOR and then jump to my ChibiOS code.
Just to be safe I checked with the debugger that all registers are correctly set and at their reset values.

It is very strange issue for me, especially that it only happens during chThdSleepXXX functions.

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: _unhandled_exception when running a chThdSleep (running chibi from a bootloader)

Postby Korken » Mon Jul 30, 2018 2:08 pm

Finally figured it out :D

I read the ARM docs wrong about the alignment of the vector table (round up to power of 2 of your table size).
I read it as it always need to be 64 words.

With the alignment fixed, everything is working, thanks for the tips!

User avatar
wurstnase
Posts: 68
Joined: Tue Oct 17, 2017 2:24 pm
Has thanked: 26 times
Been thanked: 9 times
Contact:

Re: _unhandled_exception when running a chThdSleep (running chibi from a bootloader)

Postby wurstnase » Wed Aug 01, 2018 4:08 pm

Can you please explain what you've done before and what you changed afterwards?
\o/ Nico

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: _unhandled_exception when running a chThdSleep (running chibi from a bootloader)

Postby Korken » Tue Aug 07, 2018 6:40 pm

Sure!
Before I had 128 byte alignment of the vector table, after I have 1024 byte alignment.
Nothing more was needed :)


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 1 guest