ChibiOS crashes when my firmware is not at beginning of flash memory

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: RoccoMarco, barthess

Laurent
Posts: 35
Joined: Mon Mar 25, 2013 9:01 am

ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Laurent » Thu Mar 30, 2017 1:03 pm

Hi

After I moved my firmware to sector 4 of flash memory, ChibiOS stopped working.

I changed this in linker script:

Code: Select all

flash : org = 0x08010000, len = 448k


And added this in chconf.h:

Code: Select all

#define CORTEX_VTOR_INIT 0x00010000


I erased the whole device memory, just in case. Firmware gets correctly written at its new location, and debugger shows that main() is invoked when I start. ChibiOS initialization works fine, but then as soon as a call a function as simple as chThdSleepMilliseconds, I end up in the unhandled_exception handler. More specifically, I can go as far as a call to chSysSwitch in chSchGoSleepS before the exception is triggered.

I've reduced my code to the minimum, to make sure that nothing else was interfering.

Code: Select all

int main()
{
    halInit();
    chSysInit();
    chThdSleepMilliseconds(1);
    return 0;
}


It seems that it is neither a hard fault, a memory fault nor a bus fault (checked with corresponding registers, and also tried to define matching handlers). And I don't know how to debug this kind of issue further.

This happens with both ChibiOS 16.1.4 and 16.1.7. I'm using gcc 4.9 in ChibiStudio (Eclipse) IDE. And most important, this is on a STM32F446.

Thanks.

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: ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Giovanni » Thu Mar 30, 2017 1:08 pm

VTOR is initialized by the startup file in the current trunk code, see CRT0_VTOR_INIT. The address is taken by the ld file. Non-GCC ports have to setup VTOR in initialization code, before chSysInit().

You are using 16.1 so it should work, I would check if VTOR contains what you expect.

Giovanni

Laurent
Posts: 35
Joined: Mon Mar 25, 2013 9:01 am

Re: ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Laurent » Thu Mar 30, 2017 1:37 pm

Thanks for the very quick reply.

SCB->VTOR correctly contains 0x10000 after chSysInit().

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: ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Giovanni » Thu Mar 30, 2017 1:45 pm

Hi,

I imagine you have some kind of bootloader, you should look in settings done by the bootloader and not undone before jumping to the ChibiOS crt0 file. For example, the MSP stack position, is it set as specified in the vectors table?

Giovanni

Laurent
Posts: 35
Joined: Mon Mar 25, 2013 9:01 am

Re: ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Laurent » Thu Mar 30, 2017 1:48 pm

I imagine you have some kind of bootloader

No I don't. I just need the 4 smallest (16KB) flash sectors to store data, and unfortunately those are located at the beginning, that's why I'd like to move my firmware to sector 4 and directly boot there.

So no, no other code is run before my firmware is invoked.

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: ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Giovanni » Thu Mar 30, 2017 1:51 pm

How do you make the STM32 start at that address? the initial vectors table is expected to be at address zero.

Giovanni

Laurent
Posts: 35
Joined: Mon Mar 25, 2013 9:01 am

Re: ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Laurent » Thu Mar 30, 2017 1:55 pm

How do you make the STM32 start at that address?

I don't know :D

All I did was changing the flash start address in linker script and CORTEX_VTOR_INIT, as shown in my first post, and I thought it was enough. Are you saying that the very first thing to run has to be at address 0? In this case, how can my code correctly start and run until that chThdSleepMilliseconds call?

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: ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Giovanni » Thu Mar 30, 2017 1:59 pm

I could get there by chance and with random code executed before.

You need to have a vectors table at address zero containing at least the reset address and the MSP initial value.

Giovanni

Laurent
Posts: 35
Joined: Mon Mar 25, 2013 9:01 am

Re: ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Laurent » Thu Mar 30, 2017 2:07 pm

So flash sector 0 can never contain pure data? That's annoying.

You need to have a vectors table at address zero containing at least the reset address and the MSP initial value.

Sorry but I know very little about vectors table, how would I do that?

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: ChibiOS crashes when my firmware is not at beginning of flash memory

Postby Giovanni » Thu Mar 30, 2017 2:12 pm

You need to read the Cortex-M manual from ARM and understand how it works.

Giovanni


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 26 guests