Place the bootloader program at the bottom of the FLASH

I'm trying to create a custom bootloader for the STM32L1 serial card, and I need to put my bootloader code at the bottom of my flash memory. Then I can change the memory correctly. I know that this can be specified in the linker script, but I do not know how to do this. I declared my bootloader section as follows:

  .bootsection :
  {
    . = ALIGN(4);
    KEEP(*(.bootsection)) /* Bootloader code */
    . = ALIGN(4);
  } >FLASH

and my memory is set as follows:

MEMORY
{
  FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 512K
  RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 80K
  MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
}

Does anyone have an idea on a team?

+4
source share
1 answer

Here is my script linker, I tried something with a new section of memory

 MEMORY
    {
      FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 510K
      MEM_BOOT (rx)   : ORIGIN = 0x0807CFFE, LENGTH = 2K
      RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 80K
      MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
    }

    /* Define output sections */
    SECTIONS
    {
      /* The startup code goes first into FLASH */
      .isr_vector :
      {
        . = ALIGN(4);
        KEEP(*(.isr_vector)) /* Startup code */
        . = ALIGN(4);
      } >FLASH


      .bootsection :
      {
        . = ALIGN(4);
        KEEP(*(.bootsection)) /* Bootloader code */
        . = ALIGN(4);
      } >MEM_BOOT

here is my boot code, i wroted flash program, but the erase program has not been written yet.

#define MY_BL_FUNCTIONS __attribute__((section(".bootsection")))

void BootLoader(void) MY_BL_FUNCTIONS;
uint8_t Flash_Write ( uint32_t StartAddress, uint8_t *p, uint32_t Size ) MY_BL_FUNCTIONS;

void BootLoader(void) {
    char buffer[1000];
    int i = 0;
    /*test if we enter the bootloader , toggle the led and send a string to the usart */
    GPIO_ToggleBits(GPIOA, GPIO_Pin_5);
    do {
        buffer[i] = Uart2ReadChar();
        i++;
    } while (buffer[i] != '\0');

    SendString(buffer,USART2);
}

uint8_t Flash_Write ( uint32_t StartAddress, uint8_t *p, uint32_t Size )
{
      uint32_t idx;
      uint32_t Address;
      __IO FLASH_Status status = FLASH_COMPLETE;

      Address = StartAddress;

      /* Unlock the FLASH Program memory */
      FLASH_Unlock ( );

      /* Clear all pending flags */
      FLASH_ClearFlag ( FLASH_FLAG_EOP     |
                        FLASH_FLAG_WRPERR  |
                        FLASH_FLAG_PGAERR  |
                        FLASH_FLAG_SIZERR  |
                        FLASH_FLAG_OPTVERR );

      while  ( Address < StartAddress + Size )
      {
            status = FLASH_FastProgramWord ( Address,  *(uint32_t *)p );
            Address = Address + 4;
            p = p + 4;
            if ( status != FLASH_COMPLETE ) return status;
      }

      /* Lock the FLASH Program memory */
      FLASH_Lock ( );

      return (uint8_t)status;
}
+2
source

All Articles