STM32F1 Flash擦除未擦除

时间:2018-07-13 08:49:51

标签: c erase flash-memory stm32f1

大家好,

我正在尝试用STM32F1的FLASH编写程序,但是由于某种原因它无法正常工作……每当我编写某些程序时,它都会返回错误:HAL_FLASH_ERROR_PROG,这是编程错误。当我在闪存状态寄存器中查看它时,它的意思是:当要编程的地址包含一个与编程前的“ 0xFFFF”不同的值时,由硬件进行设置。

然后当我解析到STM32F1的FLASH存储器中时,我看到从0x801EC00到0x801F17F的内存范围为0x0000。因此,编程错误。

我不明白的是为什么没有删除存储器的这一部分。这是我的代码:

/* Specify the memory areas */
MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 20K
FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 128K - 5K
Erases (RW)          : ORIGIN = 0x801EC00, LENGTH = 4
Frames (RW)          : ORIGIN = 0x801EC04, LENGTH = 4
FRAMESTx (RW)     : ORIGIN = 0x801EC08, LENGTH = 1400
FRAMESRx (RW)     : ORIGIN = 0x801F180, LENGTH = 1800
FRAMESADC(RW)     : ORIGIN = 0x801F888, LENGTH = 800
FRAMESOP (RW)     : ORIGIN = 0x801FBA8, LENGTH = 800
}


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

 .nb_Erases : 
 {
    . = ALIGN(4);

      *(.nb_Erases)

    . = ALIGN(4);
    } > Erases

 .nb_Frames : 
 {
    . = ALIGN(4);

      *(.nb_Frames)

    . = ALIGN(4);
    } > Frames

 .user_TramesTx : 
 {
    . = ALIGN(4);

      *(.user_FramesTx)

    . = ALIGN(4);
    } > FRAMESTx

 .user_TramesRx : 
 {
    . = ALIGN(4);

      *(.user_FramesRx)

    . = ALIGN(4);
    } > FRAMESRx

 .user_TramesADC : 
 {
    . = ALIGN(4);

      *(.user_FramesADC)

    . = ALIGN(4);
    } > FRAMESADC

 .user_TramesOp : 
 {
    . = ALIGN(4);

      *(.user_FramesOp)

    . = ALIGN(4);
    } > FRAMESOP

链接描述文件

#include "includes.h"

__attribute__((__section__(".nb_Erases"))) const uint32_t Flash_nb_Erases;
__attribute__((__section__(".nb_Frames"))) const uint32_t Flash_nb_Frames;
__attribute__((__section__(".user_TramesTx"))) const CanTxMsgTypeDef CAN_Tab_TxMessages_Flash[50];
__attribute__((__section__(".user_TramesRx"))) const CanRxMsgTypeDef CAN_Tab_RxMessages_Flash[50];
__attribute__((__section__(".user_TramesADC"))) const ADC_TxTypeDef CAN_ADC_Tab_TxMessages_Flash[50];
__attribute__((__section__(".user_TramesOp"))) const CAN_operation Operation_Flash[50];



int Flash_writepreparation(void)
/* Resumé : Function preparing for Flash writing. Unlock flash, clear flags and erase flash.
 *
 * Arguments : none
 *
 * Return : -1 if everything is ok, else : faulty page number
 *
 */
{
     FLASH_EraseInitTypeDef *Erase_Page = NULL;
     uint32_t *Erasing_Error = NULL;


     if (HAL_FLASH_Unlock() != HAL_OK)                                                                                     // We unlock the FLASH to write in it
     {
          return(0);
     }
     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR | FLASH_FLAG_BSY | FLASH_FLAG_OPTVERR );     // Clear of every FLASH flag
     Erase_Page = malloc(sizeof(Erase_Page));                                                                           // Configuration of the Erase_Page structure
     Erasing_Error = malloc(sizeof(Erasing_Error));
     if(Erase_Page == NULL || Erasing_Error == NULL)
     {
          Error_Handler();
     }
     Erase_Page ->TypeErase = FLASH_TYPEERASE_PAGES;
     Erase_Page ->PageAddress =0x801EC00; //ADDRESSE DU DEBUT DE DATA SAUVEGARDE
     Erase_Page ->NbPages = 5;
     HAL_FLASHEx_Erase(Erase_Page , Erasing_Error);                                                                 // We need to erase FLASH to be able to write on it
     if(*Erasing_Error!= 0xFFFFFFFF)
     {
          return(*Erasing_Error);                                                                                          // If there is an error during the erase, Erasing_Error takes the faulty page value
     }
     else
     {
          return(-1);                                                                                                              // -1 = everything is ok
     }

}




int Flash_CANTx_write(CanTxMsgTypeDef *CAN_Tab_TxMessages)
/* Résumé : Function writing CANTx frames in FLASH memory
 *
 * Arguments : CAN_Tab_TxMessages
 *
 * Retourne : 1 if everything is ok, 0 otherwise
 *
 */
{
     uint32_t nb_Erases = 0;
     uint32_t nb_frames_Tx = 0;


     int count_Frames;

     nb_Erases = Flash_nb_Erases + 1;
     nb_frames_Tx = CAN_nb_frames_Tx_cc; //CAN_nb_frames_Tx_cc defined in includes.h

     for (count_Frames= 0 ; count_Frames< nb_frames_Tx ; count_Frames++)
     {
          if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (intptr_t)&CAN_Tab_TxMessages_Flash[count_Frames].DLC, CAN_Tab_TxMessages[count_Frames].DLC) != HAL_OK)     
          {                                                       
               return 0;
          }
          if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (intptr_t)&CAN_Tab_TxMessages_Flash[count_Frames].ExtId, CAN_Tab_TxMessages[count_Frames].ExtId) != HAL_OK)
          {
               return 0;
          }
          if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (intptr_t)&CAN_Tab_TxMessages_Flash[count_Frames].IDE, CAN_Tab_TxMessages[count_Frames].IDE) != HAL_OK)
          {
               return 0;
          }
          if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (intptr_t)&CAN_Tab_TxMessages_Flash[count_Frames].RTR, CAN_Tab_TxMessages[count_Frames].RTR) != HAL_OK)
          {
               return 0;
          }
          if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (intptr_t)&CAN_Tab_TxMessages_Flash[count_Frames].StdId, CAN_Tab_TxMessages[count_Frames].StdId) != HAL_OK)
          {
               return 0;
          }
          if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (intptr_t)&CAN_Tab_TxMessages_Flash[count_Frames].Data[0], CAN_Tab_TxMessages[count_Frames].Data[0]) != HAL_OK)
          {
               return 0;
          }
          if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (intptr_t)&CAN_Tab_TxMessages_Flash[count_Frames].Data[4], CAN_Tab_TxMessages[count_Frames].Data[4]) != HAL_OK)
          {
               return 0;
          }
     }

     if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (intptr_t)&Flash_nb_Erases , nb_Erases ) != HAL_OK)                                                       
     {
          return 0;
     }

     return 1;





}



int Saving_Flash(CanTxMsgTypeDef *CAN_Tab_TxMessages)
/* Résumé : Function saving in FLASH
 *
 * Arguments : CAN_Tab_TxMessages
 *
 * Retourne : 1 if everything is ok, 0 otherwise
 *
 */
{
     int test = 0;

     test = Flash_writepreparation();                                                  

     if (test != -1)
     {
          Error_Handler();
     }

     if(Flash_CANTx_write(CAN_Tab_TxMessages) != 1)                         
     {
          Error_Handler();
     }

     if(HAL_FLASH_Lock() != HAL_OK)                                                       //We need to lock the FLASH at the end
     {
          Error_Handler();
     }

     return 1;



}

函数“ Saving_Flash”是我在main.c中调用的函数,用于将CAN_Tab_TxMessages保存在FLASH中。 Flash_writepreparation()应该准备好要写入的Flash(解锁,擦除5页,清除标志)。最后,Flash_CANTx_write将数组的每一帧写入FLASH。

如果您对为什么HAL_FLASHEx_Erase()无法正常工作有任何想法...

只需添加:构建代码时就没有特殊的警告或错误。

提前谢谢!

EDIT:我试图更改FLASH_CANTx_Write中的HAL_FLASH_Program的地址,以将&CAN_Tab_RxMessages_Flash放入内存缓冲区中,从0x801F180到0x801F888等于00000000。因此,根据我要写入的位置,地址无法正确删除...但是我不知道该如何处理..

0 个答案:

没有答案