STM32 Flash 擦除 读写 成功

来源:互联网 发布:户外轨迹软件 编辑:程序博客网 时间:2022/05/28 05:22

参考:http://download.csdn.net/detail/my_friend_ship/4166970


要点: 将数据写入flash之前,每次都要擦除,否则会写入不成功,出现的错误状态为:FLASH_Status=FLASH_ERROR_PG

写入数据成功后可以在Memory 中查看刚刚写入的数据,具体在Memory 窗口中输入:0x08010000,既可以看到此处的数据为 :Data = 0x15041979;//要写入的地址


擦除的起始地址与终止地址

#define StartAddr  ((u32)0x08010000)
#define EndAddr    ((u32)0x0807ffff)


//擦除

char IAP_Init()//初始化loaded 的flash

{
  FLASHStatus = FLASH_COMPLETE;
  //MemoryProgramStatus = PASSED;
  
  /* Unlock the Flash Program Erase controller */
  FLASH_Unlock();


  /* Define the number of page to be erased */
  NbrOfPage = (EndAddr - StartAddr) / FLASH_PAGE_SIZE;


  /* Clear All pending flags */
  FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);


  /* Erase the FLASH pages */
  for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
  {
    FLASHStatus = FLASH_ErasePage(StartAddr + (FLASH_PAGE_SIZE * EraseCounter));
  }
 return  FLASHStatus;

}


int main(void)

  u8 t;
  u8 key;
  u16 oldcount=0; //老的串口接收数据值
  u16 applenth=0; //接收到的app代码长度
  u8 clearflag=0;
  u32 FlashDestination;
  FLASH_Unlock();
  RCC_Configuration(); 
  GPIO_Configuration();
  NVIC_Configuration();
  SysTick_Config();
  UART_init(9600);//串口初始化为9600  5648
  IAP_Init();

  Data = 0x15041979;//要写入的地址
  u32    Address;

while(1)//循环擦除写入


  {  
  IAP_Init();
 int j,x;
  u32 RamSource; 
  FLASHStatus = FLASH_COMPLETE;
    /* Program the data received into STM32F10x Flash */
      FLASHStatus=FLASH_ProgramWord(StartAddr, Data);
     if( FLASHStatus == FLASH_COMPLETE )
     {
      Address = StartAddr;
      if (*(u32*)Address != Data ) 
      {
      /* End session */
        RFID_SendString("IAP  XXXXX !"); //写入失败
      }
      else       
       RFID_SendString("IAP   WRITE !");//写入成功
    }

}


原创粉丝点击