Internal Flash Read Write and Erase for STM32F767ZI Storing and Reading Configuration Info STM32

main.cpp

Committer:
shivanandgowdakr
Date:
2019-08-19
Revision:
0:7c7d15e2f39e

File content as of revision 0:7c7d15e2f39e:

/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "stats_report.h"

DigitalOut led1(LED1);



#define FLASH_USER_START_ADDR                0x080C0000
#define FLASH_USER_END_ADDR                  0x080FFFFF
#define DATA_32                              ((uint32_t)0x12345678)

uint32_t Address = 0, SectorError = 0;
__IO uint32_t data32 = 0 , MemoryProgramStatus = 0;

int main()
{
   
    static FLASH_EraseInitTypeDef EraseInitStruct;
    HAL_FLASH_Unlock();

  /* Erase the user Flash area
    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

  /* Fill EraseInit structure*/
    EraseInitStruct.TypeErase   = FLASH_TYPEERASE_SECTORS;
    EraseInitStruct.Banks = FLASH_BANK_1;
    EraseInitStruct.Sector = FLASH_SECTOR_7;
    EraseInitStruct.NbSectors = 1;
    EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;

  if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK)
  {
    /*
      Error occurred while page erase.
      User can add here some code to deal with this error.
      PAGEError will contain the faulty page and then to know the code error on this page,
      user can call function 'HAL_FLASH_GetError()'
    */
    /* Infinite loop */
    while (1)
    {
      /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
    }
  }

  /* Program the user Flash area word by word
    (area defined by FLASH_USER_START_ADDR =  0x080C 0000 and FLASH_USER_END_ADDR) ***********/
    
   
  Address =  FLASH_USER_START_ADDR;

  while (Address < FLASH_USER_END_ADDR)
  {
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32) == HAL_OK)
    {
      Address = Address + 4;
    }
    else
    {
      /* Error occurred while writing data in Flash memory.
         User can add here some code to deal with this error */
      while (1)
      {
       
      }
    }
  }

  /* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation) *********/
  HAL_FLASH_Lock();

  /* Check if the programmed data is OK
      MemoryProgramStatus = 0: data programmed correctly
      MemoryProgramStatus != 0: number of words not programmed correctly ******/
  Address = FLASH_USER_START_ADDR;
  MemoryProgramStatus = 0x0;

  while (Address < FLASH_USER_END_ADDR)
  {
    data32 = *(__IO uint32_t *)Address;

    if (data32 != DATA_32)
    {
      MemoryProgramStatus++;
    }
    Address = Address + 4;
  }

  /*Check if there is an issue to program data*/
  if (MemoryProgramStatus == 0)
  {
    
  }
  else
  {
    /* Error detected. LED2 will blink with 1s period */
    while (1)
    {
     
    }
  }
  
  
  HAL_FLASH_Unlock();

    
}