Erasing my Flash on STM32L1

2k views Asked by At

I'm facing a problem with my erasing function. I try to erase 15 sectors of my Flash in order to put a new binary file. I don't understand why but my function freeze and i cannot erase all memory i need.

Here is my code if you want to give a try

/*
 * bootloader.c
 *
 *  Created on: 9 juin 2015
 *      Author: tgloaguen
 */
#include "usart.h"
#include "stm32l1xx_flash.h"
#define WRITE_START_ADDR 0x08000000
#define WRITE_END_ADDR 0x0800FFFF
#define FLASH_PAGE_SIZE    ((uint16_t)0x100) //If a page is 256 bits
#define MY_BL_FUNCTIONS __attribute__((section(".bootsection")))

void BootLoader(void) MY_BL_FUNCTIONS;
FLASH_Status Flash_Write ( uint32_t StartAddress, uint8_t *p, uint32_t Size ) MY_BL_FUNCTIONS;
uint8_t Flash_Erase() MY_BL_FUNCTIONS;
void Receive_Data(char * buffer,int size)MY_BL_FUNCTIONS;
void Receive_Size(char * buffer, int *sizeData)MY_BL_FUNCTIONS;

void BootLoader(void) {

    //clear all ITs
    USART_ITConfig( USART1, USART_IT_RXNE, DISABLE );
    //SendString("HELLO",USART2);
    uint8_t status,i;
    char buffer[33];
    //en dur
    uint16_t *adr = WRITE_START_ADDR;
    uint16_t sizeBin = 51400,k = 0,k_hexa = 0x20;
    SendString("BOOTLOADER",USART2);


    Flash_Erase();
    SendString("ERASEOK",USART2);
    //if sizeBin ok
}

and the erase function

uint8_t Flash_Erase() {

    uint32_t EraseCounter = 0x00, Address = 0x00;//Erase count, write address
    uint32_t NbrOfPage = 0x00;//Recording to erase the pages
    volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;/*FLASH complete erasure marks*/
      /*Unlock FLASH*/

    FLASH_Unlock();
      /*Calculate the number of FLASH pages need to erase */
    NbrOfPage = (WRITE_END_ADDR - WRITE_START_ADDR) / FLASH_PAGE_SIZE;
      /* Remove all hang flags */
    FLASH_ClearFlag ( FLASH_FLAG_EOP     |
                            FLASH_FLAG_WRPERR  |
                            FLASH_FLAG_PGAERR  |
                            FLASH_FLAG_SIZERR  |
                            FLASH_FLAG_OPTVERR );
      /* Erase the FLASH page*/
    for(EraseCounter = 0; (EraseCounter <NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
      {
        SendString("ok |",USART2);
        FLASHStatus = FLASH_ErasePage(WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter));

      }
      FLASH_Lock ( );

    return (uint8_t)FLASHStatus;
}
1

There are 1 answers

6
LPs On

In your code you are tring to erase the whole flash, but you are executing your bootloader from flash from final sectors.

As reported by th REF man

During a write/erase operation to the NVM (except Half Page programming or Double-word erase/write), any attempt to read the same bank of NVM stalls the bus.

Then you have to preserve the space of bootloader changing WRITE_END_ADDR define according to your memory map.

Example: if your bootloader is 4K long and belongs to the last sector (0x0801 F000 - 0x0801 FFFF) then WRITE_END_ADDR must be 0x0801 EFFF.

EDIT As @Olaf wrote take care about your ISP (Initial Stack Pointer) and IPC (Initial Program Counter) that belong to first 8 bytes in your flash at address 0.