Page 1 of 1

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

Posted: Sat Dec 10, 2011 9:41 am
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? :(

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

Posted: Sat Dec 10, 2011 11:10 am
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... ;)

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

Posted: Sat Dec 10, 2011 11:46 am
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

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

Posted: Sat Dec 10, 2011 12:31 pm
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 )

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

Posted: Sat Dec 10, 2011 6:05 pm
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.

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

Posted: Sat Dec 10, 2011 7:31 pm
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

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

Posted: Sat Dec 10, 2011 8:55 pm
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

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

Posted: Sat Dec 10, 2011 9:01 pm
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 ;)

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

Posted: Sat Dec 10, 2011 9:22 pm
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

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

Posted: Sat Dec 10, 2011 9:45 pm
by barthess
Better advise books/articles to read to understand how all that stuff works.