Skip to main content
Visitor II
March 7, 2022
Question

bootloader for stm32G031k8t6

  • March 7, 2022
  • 6 replies
  • 1393 views

hello,

I try to write a bootloader function fo the STM32g03 MCU

this is the code:

void JumpToBootLoader(void)

{

UART_HandleTypeDef huart2;

// System boot function pointer:

void (*StartBootLoader)(void);

// System address for the F76xxx family (see ST AN2606):

volatile uint32_t rom_addr = 0x1FFF0000;

// RAM address for the F76xxx family (see ST AN2606):

volatile uint32_t ram_addr = 0x20001000;

// System bootloader location:

StartBootLoader = (void (*)(void)) (*((uint32_t *)(rom_addr + 4)));

// The code below emulates reset conditions:

HAL_RCC_DeInit();

HAL_DeInit();

// Stop & disable SysTick:

SysTick->CTRL = 0;

SysTick->LOAD = 0;

SysTick->VAL = 0;

__set_PRIMASK(1); // Disable interrupts

__set_MSP(*(uint32_t*) ram_addr); // Stack pointer

huart2.Instance = USART2;

huart2.Init.BaudRate = 115200;

huart2.Init.WordLength = UART_WORDLENGTH_8B;

huart2.Init.StopBits = UART_STOPBITS_1;

huart2.Init.Parity = UART_PARITY_EVEN;

huart2.Init.Mode = UART_MODE_TX_RX;

huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart2.Init.OverSampling = UART_OVERSAMPLING_16;

huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;

huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if (HAL_UART_Init(&huart2) != HAL_OK)

{

 Error_Handler();

}

StartBootLoader(); // Jump to the bootloader address

}

as you can see before the end of the function I added the definition of the UART2

I use the cube programmer application and choose UART and CONNECT.

when pressing the CONNECT button I get this error:

Error: Activating device: KO. Please, verify the boot mode configuration and check the serial port configuration. Reset your device then try again

I use a com convertor with 2 LEDS and I can see the LEDS blinking when I press the CONNECT button.

but keep getting the error.

what can I check?

thanks in advanced

    This topic has been closed for replies.

    6 replies

    Super User
    March 7, 2022

    Don't enable UART. The bootloader will initialize its own pins.

    In general, interrupts should not be disabled globally, although for the UART bootloader it should work.

    Ensure no other devices are talking at the same time and that you're connected to the supported bootloader pins per AN2606.

    Yben .1Author
    Visitor II
    March 7, 2022

    ok so you mean that I should remove the following code:

    huart2.Instance = USART2;

    huart2.Init.BaudRate = 115200;

    huart2.Init.WordLength = UART_WORDLENGTH_8B;

    huart2.Init.StopBits = UART_STOPBITS_1;

    huart2.Init.Parity = UART_PARITY_EVEN;

    huart2.Init.Mode = UART_MODE_TX_RX;

    huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

    huart2.Init.OverSampling = UART_OVERSAMPLING_16;

    huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

    huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;

    huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

    if (HAL_UART_Init(&huart2) != HAL_OK)

    {

     Error_Handler();

    }

    correct?

    and what about the rest of the code is it good?

    this is the first time I do a bootloader and its a code I found and change the RAM and ROM addresses according to the AN2606.

    when it didnt work I tried to initiate the USART...

    Graduate II
    March 7, 2022

    You can check the code/tables present at the prescribed addresses. Use the debugger, inspect the memory.

    Typically you'd probably want to remap the zero address space to the ROM to be consistent with the BOOT0=HIGH state the ROM loader enters with.

    The ROM loader assumes reset conditions, it's not going to be using your UART configuration, and it doesn't want interrupts completely disabled on the processor, nor other clocks and plls set up.

    Review and step the code in the debugger, walk it into the ROM checking that it goes there as expected, and doesn't fail/fault immediately. Inspect the code generated by the compiler.

    Expectations here are that you understand the functional mechanics of the Cortex-M0(+) MCU, and the code your compiler generates doesn't break the stack or references to local/auto variables.

    Yben .1Author
    Visitor II
    March 8, 2022

    I dont know how to inspect the memory...

    I dont know what to expect to see there...

    I assume that the bootloader is waiting for the cube programmer app to send the bin/hex file via UART.

    I use this 2 lines:

    volatile uint32_t rom_addr = 0x1FFF0000;

    volatile uint32_t ram_addr = 0x20001000;

    because I found the addresses in the AN2606 at page 397. it says there those addresses.

    I connect using UART2 at ports PA3 and PA2...

    I checked and they are connencted ok.

    but in the original software those ports are used at GPIO output. could this be a problem?

    you said it doesnt want interrupts completely disabled globally.

    ao I canceled this line:

    __set_PRIMASK(1); // Disable interrupts

    but I keep getting this error when I press the CONNECT button:

    Error: Activating device: KO. Please, verify the boot mode configuration and check the serial port configuration. Reset your device then try again

    Yben .1Author
    Visitor II
    March 14, 2022

    ok so I tried to debug the code:

    this is the code I debug:

    ==========================================

    void JumpToBootLoader(void)

    {

    UART_HandleTypeDef huart2;

    // System boot function pointer:

    void (*StartBootLoader)(void);

    // System address for the F76xxx family (see ST AN2606):

    volatile uint32_t rom_addr = 0x1FFF0000;

    // RAM address for the F76xxx family (see ST AN2606):

    volatile uint32_t ram_addr = 0x20001000;

    // System bootloader location:

    StartBootLoader = (void (*)(void)) (*((uint32_t *)(rom_addr + 4)));

    // The code below emulates reset conditions:

    HAL_RCC_DeInit();

    HAL_DeInit();

    // Stop & disable SysTick:

    SysTick->CTRL = 0;

    SysTick->LOAD = 0;

    SysTick->VAL = 0;

    __set_PRIMASK(1); // Disable interrupts

    __set_MSP(*(uint32_t*) ram_addr); // Stack pointer

    StartBootLoader(); // Jump to the bootloader address

    }

    =============================================================

    I call this function immidately after the call to HAL_Init() in the main function.

    I put a break point in the line of

    HAL_RCC_DeInit();

    and start debugging it step by step.

    when arriving to this line:

    StartBootLoader(); // Jump to the bootloader address

    it goes to a function called

    HardFault_Handler

    what can be the problem here?

    any help will be appreciated.

    thanks

    Yben .1Author
    Visitor II
    March 14, 2022

    there is a mistake in the comment above the RAM addresses it should be

    // System address for the stm32g03 family (see ST AN2606):

    and not F76xxx as it is there but its only a comment