Skip to main content
richm
Associate II
September 8, 2015
Question

Developing custom boot-loader for STM32F103

  • September 8, 2015
  • 2 replies
  • 1224 views
Posted on September 08, 2015 at 22:27

I am developing a custom bootloader for the STM32F103.  The general plan is that there are two Micro-vision projects - one for the application (0x08040000) and one for the boot-loader (0x08000000).  In general this concept works when there is a valid application in flash.  My problem is when there is no application in flash...

My idea was to use the hard-fault exception as a clue that there is no code loaded to the application space.  On boot, the bootloader simply jumps to the application start.  If there is a hard-fault, simply jump back to the bootloader...eg

; Reset handler

Reset_Handler   PROC

                EXPORT  Reset_Handler             [WEAK]

                IMPORT  __main

                IMPORT  SystemInit

                ;rrm test of branching to application code vs bootloader code

                LDR     R0, =0x2000FFFC  ;load R0 with the address at shared memory

                LDR     R1, =0x00000001  ;key to run bootloader

                LDR     R2, [R0, #0]     ;load R2 with the contents of the address in R0

                STR     R0, [R0, #0]     ;Invalidate

                CMP     R2, R1

                BEQ     Boot_Loader      ;Application signals to run Bootloader

                B       Application      ;Normal operation - just jump to app

                

Boot_Loader     LDR     R0, =0x08000000

                LDR     SP,[R0, #0] 

                LDR     R0,[R0, #4]

                LDR     R0, =SystemInit

                BLX     R0               

                LDR     R0, =__main

                BX      R0

                ENDP

Application     PROC                

                ;jump to application

                LDR     R0, =0x08040000 ; Application Base

                LDR     SP,[R0, #0]

                LDR     R0,[R0, #4]

                BX      R0

                ENDP

;rrm end test

; Dummy Exception Handlers (infinite loops which can be modified)

NMI_Handler     PROC

                EXPORT  NMI_Handler                [WEAK]

                B       .

                ENDP

HardFault_Handler\

                PROC

                EXPORT  HardFault_Handler          [WEAK]

                B       Boot_Loader

                ENDP

The problem I have run into is that when the hard-fault occurs, the bootloader runs but interrupts do not get executed.  The vector table offset is correct and I can see with the debugger that they are enabled and pending but don't execute...???

Clearly there is something that the hard-fault is doing to the system and I need to reset something to get back to where I was...I have verified that if I simply go directly to the bootloader without attempting an application jump, the bootloader runs correctly (interrupts work)

Any ideas?

Thanks

rich
    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    September 8, 2015
    Posted on September 08, 2015 at 22:55

    Perhaps it's because the processor is running in a different context? It's faulted and you need to unravel that, not just jump to some other code. If you just want it to Reset,  using NVIC_SystemReset() would seem like a more viable path.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    richm
    richmAuthor
    Associate II
    September 9, 2015
    Posted on September 09, 2015 at 16:36

    Thanks clive1

    Sometimes I am amazed at how a simple response triggers the solution to a problem...All I needed to do was set my shared RAM region to indicate Bootloader ops and then NVIC_Reset within the hardfault handler...problem solved!

    rich