STM32 Learning: Internal Flash

Publisher:MysticalGardenLatest update time:2018-05-01 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When we develop applications, we often need to save some program running parameters, such as some correction coefficients. The characteristics of these data are: small in number and do not need to be modified frequently, but they cannot be defined as constants, because each device may be different and may be modified in the future. It is a convenient way to store this type of data in a specified location, directly modify the value of the storage location when it needs to be modified, and directly read it when it needs to be used. Considering that the amount of data is relatively small, it is neither economical nor necessary to use a dedicated storage unit. The internal Flash capacity of STM32F103 is large, and ST's library function also provides basic Flash operation functions, which are also convenient to implement.


Take the large-capacity product STM32F103VE as an example. Its Flash capacity reaches 512K, and part of it can be used for data storage. The following is the organization mode of large-capacity Flash:


According to the above Flash organization model, we can make corresponding definitions according to our own convenience. Because each sector of large capacity is defined as 2K, and each of small capacity and medium capacity is defined as 1K, we make the following macro definition:

#define FLASH_SIZE 512 //FLASH capacity of the selected MCU (in K)


#if FLASH_SIZE<256

  #define SECTOR_SIZE 1024 // bytes

#else 

  #define SECTOR_SIZE 2048 // bytes

#endif


Although ST's library functions are relatively comprehensive, they are all basic operations. For ease of use, we encapsulate them again according to our own needs.


Read operations are relatively simple, and the built-in flash memory module can be directly addressed in the general address space, just like reading a variable.


//Read multiple data from the specified address

void FLASH_ReadMoreData(uint32_t startAddress,uint16_t *readData,uint16_t countToRead)

{

  uint16_t dataIndex;

  for(dataIndex=0;dataIndex

  {

    readData[dataIndex]=FLASH_ReadHalfWord(startAddress+dataIndex*2);

  }

}


//Read the half word (16-bit data) of the specified address

uint16_t FLASH_ReadHalfWord(uint32_t address)

{

  return *(__IO uint16_t*)address; 

}


//Read the full word (32-bit data) of the specified address

uint32_t FLASH_ReadWord(uint32_t address)

{

  uint32_t temp1,temp2;

  temp1=*(__IO uint16_t*)address; 

  temp2=*(__IO uint16_t*)(address+2); 

  return (temp2<<16)+temp1;

}

The write operation is relatively more complicated, including writing and erasing user data. There is also a write protection lock to prevent misoperation. However, these basic operations have been written for us in the ST library functions, and we only need to call them.


After STM32 is reset, the FPEC module is protected. Only after the write protection is released can we operate the related registers. The programming of STM32 flash memory must be written 16 bits at a time. Any operation that is not a half word will cause an error. The following figure shows the Flash writing process:

When programming the STM32 FLASH, the FLASH at the write address must be erased (that is, its value must be 0XFFFF), otherwise it cannot be written. Flash erasure requires that the entire page must be erased, so the entire page must also be written, otherwise data may be lost. The following figure shows the Flash page erase process:

The following is the Flash full erase process

According to the above diagram, we write the data writing function as follows:

//Write multiple data starting from the specified address

void FLASH_WriteMoreData(uint32_t startAddress,uint16_t *writeData,uint16_t countToWrite)

{

  if(startAddress=(FLASH_BASE+1024*FLASH_SIZE)))

  {

    return; //Illegal address

  }

  FLASH_Unlock(); //Unlock write protection

  uint32_t offsetAddress=startAddress-FLASH_BASE; //Calculate the actual offset address after removing 0X08000000

  uint32_t sectorPosition=offsetAddress/SECTOR_SIZE; //Calculate the sector address, which is 0~255 for STM32F103VET6

  

  uint32_t sectorStartAddress=sectorPosition*SECTOR_SIZE+FLASH_BASE; //The first address of the corresponding sector


  FLASH_ErasePage(sectorStartAddress); //Erase this sector

  

  uint16_t dataIndex;

  for(dataIndex=0;dataIndex

  {

    FLASH_ProgramHalfWord(startAddress+dataIndex*2,writeData[dataIndex]);

  }

  

  FLASH_Lock(); //Lock write protection

}

Before erasing, the data on the page should be read out and merged with the data to be written, and then written after erasing. However, the amount of data is large (the large capacity is 2K per sector), so considering that a small amount of data is stored, all the data is written at the same time each time to simplify the operation and reduce the amount of data processing. After testing, the above program writes and reads data correctly, and the internal Flash read and write operations can be realized. For more in-depth understanding, please refer to the "STM32F10xxx Flash Programming Reference Manual".

Keywords:STM32 Reference address:STM32 Learning: Internal Flash

Previous article:STM32F103 series PB4 pin problem
Next article:STM32 Flash read and write; storage content and method corresponding to Flash address

Recommended ReadingLatest update time:2024-11-16 11:37

STM32 serial port register library function configuration method
1. References       "STM32F1 Development Guide - Library Function Version" - 5.3 usart serial port folder introduction                                     - Chapter 9 Serial Port Experiment       "STM32 Chinese Reference Manual V10" - Chapter 25 Universal Synchronous Asynchronous Receiver Transmitter (USART) 2.
[Microcontroller]
STM32 serial port register library function configuration method
STM32 environment construction, project creation and program burning
Development environment construction: The STM32 development environment uses Keil MDK, so how to use MDK to build a project? MDK Download First, you should download the latest version of MDK installation package from the MDK official website: http://www.keil.com/arm/mdk.asp, enter the MDK official website, and c
[Microcontroller]
STM32 environment construction, project creation and program burning
STM32 Series Part 8 - Serial Port Configuration Steps
//Initialize serial port 1 void My_USART1_Init(void) { GPIO_InitTypeDef GPIO_InitStrue; USART_InitTypeDef USART_InitStrue; NVIC_InitTypeDef NVIC_InitStrue; //Serial port clock enable, GPIO clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //GPIO por
[Microcontroller]
Independent key scanning method based on stm32
I have completed a stm32 project in the past two days. When solving the keystroke problem, I wrote a scanning function for an independent keystroke. It should have been done by predecessors. Its essence is a modification of the ordinary scanning method. The advantage is that the key-related parameters are encapsulated
[Microcontroller]
37. Basic principles of STM32ADC
1. What is ADC   2. Characteristics of STM32ADC The number of ADC bits determines the accuracy of ADC, which can be 8-bit, 12-bit, 16-bit, etc. Another very important parameter is the conversion time. STM32f103RBT6 has 2 ADC controllers. 128KFlash, 20KRAM Each ADC controller has multiple channels. For example, A
[Microcontroller]
37. Basic principles of STM32ADC
Deep thinking on the serial port of STM32 microcontroller
In fact, when learning to use a single-chip microcomputer, everyone often thinks it is simple and passes it quickly, but there are actually some things that are worth thinking about. When I wrote programs before, I usually sent data, so I just called the rewritten printf() function. But this project uses NRF full-dupl
[Microcontroller]
STM32 boot[1:0] settings
  (1) When the STM32 is powered on, BOOT is latched to determine where to start      When debugging in RAM, the power has been successfully turned on, and the BOOT status has no effect.      The IDE software automatically downloads the program to RAM and debugs and runs it in RAM.      (2)      It should be use
[Microcontroller]
STM32 DMA Notes
Before doing the experiment, you must first understand what DMA is and where its role lies. DMA, or direct memory access, is used in the transmission of some data, thereby freeing up the CPU and giving it enough time to handle other things. STM32 uses DMA related operations: 1. DMA configuration The configurations i
[Microcontroller]
STM32 DMA Notes
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号