在STM32L1上擦除我的Flash

时间:2015-06-17 11:03:41

标签: c embedded flash-memory

我的擦除功能遇到了问题。 我尝试擦除我的Flash的15个扇区,以便放置一个新的二进制文件。 我不明白为什么,但我的功能冻结,我无法抹去我需要的所有记忆。

如果您想尝试一下,这是我的代码

/*
 * 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
}

和擦除功能

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 个答案:

答案 0 :(得分:0)

在您的代码中,您要删除整个闪存,但是您正在从最终扇区的闪存执行引导加载程序。

据REF男子报道

  

对NVM执行写入/擦除操作期间(半页编程或双字除外)   擦除/写入),任何读取同一组NVM的尝试都会使总线停止。

然后你必须保留引导加载程序的空间,根据你的内存映射改变.jqtree-tree .jqtree-title { color: bisque !important; } define。

示例:如果您的引导加载程序长度为4K且属于最后一个扇区WRITE_END_ADDR,则(0x0801 F000 - 0x0801 FFFF)必须为WRITE_END_ADDR

修改 正如@Olaf所写,请注意您的ISP(初始堆栈指针)和IPC(初始程序计数器),它们属于闪存中地址0的前8个字节。