Puzzled: why 3589 crashes if run from another address...

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Puzzled: why 3589 crashes if run from another address...

Post by DrunkenDonkey »

I was using 3454 till now, but the excellent modifications that barthess did to the I2C code pushed me to update ;)
Up till now, I wrote a small bootloader, that essentially just calls the main program that is 0x2000 off:

Code: Select all

#define APPLICATION_OFFSET 0x2000
typedef  void (*pFunction)(void);

void startApp(void)
{
   u32 ApplicationAddress=FLASH_BASE+APPLICATION_OFFSET;
    /* Jump to user application */
    pFunction Jump_To_Application = (pFunction) (*(__IO uint32_t*) (ApplicationAddress + 4));
    /* Initialize user application's Stack Pointer */
    u32 stack=*(__IO uint32_t*) ApplicationAddress;
    __set_MSP(stack);
    Jump_To_Application();
}


Then the main code have custom ld that contains

Code: Select all

MEMORY
{
    flash : org = 0x08002000, len = 124k
    ram : org = 0x20000000, len = 16k
}


and in the main

Code: Select all

void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
{
  /* Check the parameters */
  assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
  assert_param(IS_NVIC_OFFSET(Offset)); 
   
  SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
}

int main(void)
{
   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);


So it was working good on 3454, but not on 3589, SIGTRAP inside chSysInit() (it can't step in chThdCreateStatic, guess some interrupt comes at the time and kills it) with pc=0x08000200, which is weird I think. If I change the program to run without the offset it goes okay, removing the chSysEnable() makes it run further until it freezes in some communication later. I have 3454 saved, so went back, recompiled and it is working again. Giovanni, can you think of anything that have changed recently that forbids me to move the code, like some interrupt handler at specific offset or something? :(
Jacon
Posts: 143
Joined: Wed Dec 08, 2010 7:52 am
Has thanked: 50 times
Been thanked: 5 times

Re: Puzzled: why 3589 crashes if run from another address...

Post by Jacon »

As I understand, your MCU has 128k FLASH.

0x2000 = 8k length.

So your updated ld script should have:

flash : org = 0x08002000, len = 120k

length... ;)
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Puzzled: why 3589 crashes if run from another address...

Post by Giovanni »

Note that the port layer now set the VTOR address, so it changes when you call chSysInit(). The option CORTEX_VTOR_INIT allows to initialize it to an arbitrary address.

Giovanni
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: Puzzled: why 3589 crashes if run from another address...

Post by DrunkenDonkey »

Wooohoo, thanks :D
I don't need to use that NVIC_SetVectorTable anymore too, it all works great (and thanks for the length tip, was 0x1000 before :D )
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Puzzled: why 3589 crashes if run from another address...

Post by barthess »

I am trying to do similar on STM32F407, but "bootloader" write in assembly language:

Code: Select all

.syntax unified
b myflash


build it with following 3 commands:

Code: Select all

arm-none-eabi-gcc -mcpu=cortex-m3 -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -mthumb -DTHUMB -x assembler-with-cpp -c -g -O -Wa,-alh,-L boot.s -o ch.o

arm-none-eabi-ld -Tboot.ld -o ch.elf ch.o

arm-none-eabi-objcopy.exe -O binary ch.elf  ch.bin

where boot.ld:

Code: Select all

_start = 0x8000000;
myflash = 0xE0000;

flash ch.bin at 0x8000000
Than compile my ChibiOS based project with fixed addresses in ld-script:

Code: Select all

MEMORY
{
    flash : org = 0x080E0000, len = 128k
    ram : org = 0x20000000, len = 112k
    ethram : org = 0x2001C000, len = 16k
    ccmram : org = 0x10000000, len = 64k
}

specify in chconf.h

Code: Select all

#define CORTEX_VTOR_INIT       0x000E0000

build project and flash it at 0x80E0000. But gdb can not break at main() and openOCD print following errors:

Code: Select all

Polling target failed, GDB will be halted. Polling again in 100ms
Error: address + size wrapped(0xffffffff, 0x00000004)
Error: JTAG-DP STICKY ERROR
Error: MEM_AP_CSW 0x23000052, MEM_AP_TAR 0xbffef0d8
Error: JTAG-DP STICKY ERROR
Error: MEM_AP_CSW 0x23000052, MEM_AP_TAR 0xbffef0d8
Warn : Block read error address 0xbffef0d4
Error: JTAG-DP STICKY ERROR
Error: MEM_AP_CSW 0x23000052, MEM_AP_TAR 0xbffef0c4
Error: JTAG-DP STICKY ERROR
Error: MEM_AP_CSW 0x23000052, MEM_AP_TAR 0xbffef0c4
Warn : Block read error address 0xbffef0c0

disassembly show at 0x08000000 adress:

Code: Select all

08000000:   b.w 0x80e0000 <_vectors>

at 0x080e0000:

Code: Select all

080e0000:   lsls r0, r0, #16
080e0002:   movs r0, #0
080e0004:   asrs r1, r2, #32
080e0006:   lsrs r6, r1, #32

What I do wrong? My knowing of assembly language is close to zero.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Puzzled: why 3589 crashes if run from another address...

Post by Giovanni »

It looks like your bootloader jumps to 0x80E0000 instead that to the location pointed by the pointer stored in 0x80E0000 (it should point to ResetHandler).

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Puzzled: why 3589 crashes if run from another address...

Post by barthess »

Yes, I want that my "monocelled" bootloader just jump to 0x80E0000 where now moved startup of ChibiOS.
from my ch.map:

Code: Select all

startup         0x080e0000      0x188
 *(vectors)
 vectors        0x080e0000      0x188 build/obj/vectors.o
                0x080e0000                _vectors
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: Puzzled: why 3589 crashes if run from another address...

Post by DrunkenDonkey »

The minimal bootloader, just using normal chibi project is around 2k, that's quite good to not need to resort to assembly, got it to 3k with all the flash programming in ;)
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Puzzled: why 3589 crashes if run from another address...

Post by Giovanni »

Barthess, at that address there is not the entry point, the entry point is ResetHandler which is pointer by the *second* pointer in the vectors table, see vectors.c.

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Puzzled: why 3589 crashes if run from another address...

Post by barthess »

Better advise books/articles to read to understand how all that stuff works.
Post Reply