STM32: Flash erase and read and write operations

Publisher:亚瑟摩根Latest update time:2018-05-01 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  • Application platform: STM32F030F4P6

  • ST official library: STM32Cube_FW_F0_V1.9.0

background knowledge

  • For most MCUs and microcontrollers (ARM, x86), the address space is in bytes, which means that one address is one byte.

  • Flash memory has a characteristic that it can only write 0, not 1. So if there is data at the original address, it means that some bits are 0, and these bits are equivalent to invalid. So you must make sure they are all 1 before writing, and you can only erase them. In addition, each time you erase, you must erase a 4K sector, which is determined by the characteristics of flash.

  • The internal oscillator must be turned on before operating the Flash. 


Introduction to Flash storage of STM32F030F4P6

STM32F030F4P6 hardware configuration: FLASH (16KB) RAM (4KB) 
(contains 4 sectors, 1 sector contains 4 pages, each page has 1Kbyte space) 
Users can program and erase Flash.

Main Flash memory programming 
The main Flash memory can be programmed 16 bits at a time. 
Flash memory erase 
The Flash memory can be erased page by page or completely (Mass Erase). 
Write the picture description here

STM32F030F4P6 Flash read and write reference code (HAL library)

/* Base address of the Flash sectors */

#define ADDR_FLASH_PAGE_0     ((uint32_t)0x08000000) /* Base @ of Page 0, 1 Kbyte */

#define ADDR_FLASH_PAGE_1     ((uint32_t)0x08000400) /* Base @ of Page 1, 1 Kbyte */

#define ADDR_FLASH_PAGE_2     ((uint32_t)0x08000800) /* Base @ of Page 2, 1 Kbyte */

#define ADDR_FLASH_PAGE_3     ((uint32_t)0x08000C00) /* Base @ of Page 3, 1 Kbyte */

#define ADDR_FLASH_PAGE_4     ((uint32_t)0x08001000) /* Base @ of Page 4, 1 Kbyte */

#define ADDR_FLASH_PAGE_5     ((uint32_t)0x08001400) /* Base @ of Page 5, 1 Kbyte */

#define ADDR_FLASH_PAGE_6     ((uint32_t)0x08001800) /* Base @ of Page 6, 1 Kbyte */

#define ADDR_FLASH_PAGE_7     ((uint32_t)0x08001C00) /* Base @ of Page 7, 1 Kbyte */

#define ADDR_FLASH_PAGE_8     ((uint32_t)0x08002000) /* Base @ of Page 8, 1 Kbyte */

#define ADDR_FLASH_PAGE_9     ((uint32_t)0x08002400) /* Base @ of Page 9, 1 Kbyte */

#define ADDR_FLASH_PAGE_10    ((uint32_t)0x08002800) /* Base @ of Page 10, 1 Kbyte */

#define ADDR_FLASH_PAGE_11    ((uint32_t)0x08002C00) /* Base @ of Page 11, 1 Kbyte */

#define ADDR_FLASH_PAGE_12    ((uint32_t)0x08003000) /* Base @ of Page 12, 1 Kbyte */

#define ADDR_FLASH_PAGE_13    ((uint32_t)0x08003400) /* Base @ of Page 13, 1 Kbyte */

#define ADDR_FLASH_PAGE_14    ((uint32_t)0x08003800) /* Base @ of Page 14, 1 Kbyte */

#define ADDR_FLASH_PAGE_15    ((uint32_t)0x08003C00) /* Base @ of Page 15, 1 Kbyte */


/* Private define ------------------------------------------------------------*/

#define FLASH_USER_START_ADDR   ADDR_FLASH_PAGE_15          /* Start @ of user Flash area */

#define FLASH_USER_END_ADDR     ADDR_FLASH_PAGE_15 + FLASH_PAGE_SIZE   /* End @ of user Flash area */


#define DATA_32                 ((uint32_t)0x12345678)


/*Variable used for Erase procedure*/

static FLASH_EraseInitTypeDef EraseInitStruct;

uint32_t Address = 0;


/**

  * @brief  Main program

  * @param  None

  * @retval None

  */

int main(void)

{

    /* STM32F0xx HAL library initialization:

         - Configure the Flash prefetch

         - Systick timer is configured by default as source of time base, but user

           can eventually implement his proper time base source (a general purpose

           timer for example or other time source), keeping in mind that Time base

           duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and

           handled in milliseconds basis.

         - Low Level Initialization

       */

    HAL_Init();


    /* Configure the system clock to 48 MHz */

    SystemClock_Config();


    /* Unlock the Flash to enable the flash control register access *************/

    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_PAGES;

    EraseInitStruct.PageAddress = FLASH_USER_START_ADDR;

    EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;


    if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != 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)

        {

            /* User doing something here */

        }

    }


    /* Program the user Flash area word by word

      (area defined by FLASH_USER_START_ADDR 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)

            {

                /* User doing something here */

            }

        }

    }


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

    {

        /* User doing something here */

    }

    else

    {

        while (1)

        {

            /* User doing something here */

        }

    }


    /* Infinite loop */

    while (1)

    {

    }

}


Keywords:STM32 Reference address:STM32: Flash erase and read and write operations

Previous article:Use the STM32cubeMX library to read and write FLASH data
Next article:Brief analysis of flash reading and writing (taking stm32f107vct6 as an example)

Recommended ReadingLatest update time:2024-11-16 13:47

STM32-1-LED on and off
led.c file #include "led.h" void LED_Init(void){     //¶¨ÒåÒ»¸öGPIO_InitTypeDefÀàÐ͵ĽṹÌå   GPIO_InitTypeDef GPIO_InitStructure;      //¿ªÆôPA¿ÚʱÖÓ   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);      //Ñ¡ÔñÒª¿ØÖƵÄPA¿Ú   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;   //Eugene   GPIO_I
[Microcontroller]
Focusing on AI and IoT, the STM32 Summit is coming soon
With the theme of "Gathering Wisdom, Creating the Future", the 2019 STM32 Summit focuses on three major topics: artificial intelligence and computing, industry and security, and cloud technology and connectivity. During the two-day summit, STMicroelectronics and 45 partners will jointly exhibit more than 180 prototype
[Internet of Things]
Design of AC permanent magnet synchronous motor driver based on STM32
Introduction In recent years, with the rapid development of microelectronics technology, power electronics technology, modern control technology, material technology and the gradual improvement of motor manufacturing process level, AC permanent magnet synchronous motors have been widely used in industry and agricultu
[Microcontroller]
Design of AC permanent magnet synchronous motor driver based on STM32
Design of remote temperature control system based on STM32
Temperature control is one of the main objects of industrial control. The commonly used mathematical model for temperature control is first-order inertia plus a pure lag link, but it varies greatly with the heating object and environmental conditions. Because the temperature control object has the common
[Microcontroller]
Design of remote temperature control system based on STM32
Parsing of STM32 clock code and pointer jump of startup function
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //HSE means using external clock, HSI means using internal clock   RCC_OscInitStruct.HSEState = RCC_HSE_ON; // Turn on the external clock   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; //Clock source s
[Microcontroller]
stm32systick knowledge
Systick, also known as tick timer, is a 24-bit simple timer of the STM32 core, often used for delay and system heartbeat functions. Systick has a total of 4 registers CTRL, LOAD, VAL, CALIB   The definitions of each bit of SysTick- CTRL are shown in Figure 5.1.2.1:    The definition of SysTick- LOAD is shown in Figure
[Microcontroller]
stm32systick knowledge
STM32 interrupt transmission characteristics and RS485 direction control
STM32 has two interrupt flags for data transmission, one is the transmission data register empty flag, and the other is the transmission completion flag. Both flags can cause interrupts. To send a data packet in interrupt mode, the process is as follows: 1. Set the direction of RS485 to send, enable the send regis
[Microcontroller]
STM32 xPSR affected conditional instructions
I'm studying ARM Cortex-M3 recently, and I found a book called "An Definitive Guide to The ARM Cortex-M3" which is considered a classic. This series of study notes is actually the reading notes I made while studying this book. Unconditional jump instruction Jump instructions are divided into two categories: unconditi
[Microcontroller]
STM32 xPSR affected conditional instructions
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号