Shivanand Gowda / Mbed OS Internal_Flash_Write_STM32
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005 
00006 #include "mbed.h"
00007 #include "stats_report.h"
00008 
00009 DigitalOut led1(LED1);
00010 
00011 
00012 
00013 #define FLASH_USER_START_ADDR                0x080C0000
00014 #define FLASH_USER_END_ADDR                  0x080FFFFF
00015 #define DATA_32                              ((uint32_t)0x12345678)
00016 
00017 uint32_t Address = 0, SectorError = 0;
00018 __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
00019 
00020 int main()
00021 {
00022    
00023     static FLASH_EraseInitTypeDef EraseInitStruct;
00024     HAL_FLASH_Unlock();
00025 
00026   /* Erase the user Flash area
00027     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
00028 
00029   /* Fill EraseInit structure*/
00030     EraseInitStruct.TypeErase   = FLASH_TYPEERASE_SECTORS;
00031     EraseInitStruct.Banks = FLASH_BANK_1;
00032     EraseInitStruct.Sector = FLASH_SECTOR_7;
00033     EraseInitStruct.NbSectors = 1;
00034     EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
00035 
00036   if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK)
00037   {
00038     /*
00039       Error occurred while page erase.
00040       User can add here some code to deal with this error.
00041       PAGEError will contain the faulty page and then to know the code error on this page,
00042       user can call function 'HAL_FLASH_GetError()'
00043     */
00044     /* Infinite loop */
00045     while (1)
00046     {
00047       /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
00048     }
00049   }
00050 
00051   /* Program the user Flash area word by word
00052     (area defined by FLASH_USER_START_ADDR =  0x080C 0000 and FLASH_USER_END_ADDR) ***********/
00053     
00054    
00055   Address =  FLASH_USER_START_ADDR;
00056 
00057   while (Address < FLASH_USER_END_ADDR)
00058   {
00059     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32) == HAL_OK)
00060     {
00061       Address = Address + 4;
00062     }
00063     else
00064     {
00065       /* Error occurred while writing data in Flash memory.
00066          User can add here some code to deal with this error */
00067       while (1)
00068       {
00069        
00070       }
00071     }
00072   }
00073 
00074   /* Lock the Flash to disable the flash control register access (recommended
00075      to protect the FLASH memory against possible unwanted operation) *********/
00076   HAL_FLASH_Lock();
00077 
00078   /* Check if the programmed data is OK
00079       MemoryProgramStatus = 0: data programmed correctly
00080       MemoryProgramStatus != 0: number of words not programmed correctly ******/
00081   Address = FLASH_USER_START_ADDR;
00082   MemoryProgramStatus = 0x0;
00083 
00084   while (Address < FLASH_USER_END_ADDR)
00085   {
00086     data32 = *(__IO uint32_t *)Address;
00087 
00088     if (data32 != DATA_32)
00089     {
00090       MemoryProgramStatus++;
00091     }
00092     Address = Address + 4;
00093   }
00094 
00095   /*Check if there is an issue to program data*/
00096   if (MemoryProgramStatus == 0)
00097   {
00098     
00099   }
00100   else
00101   {
00102     /* Error detected. LED2 will blink with 1s period */
00103     while (1)
00104     {
00105      
00106     }
00107   }
00108   
00109   
00110   HAL_FLASH_Unlock();
00111 
00112     
00113 }