On-chip Flash

This tutorial demonstrates how to control the internal Flash of the development board to read and write data through keys , and print the data through the serial port (USART1)

1、Software-Hardware

2、Brief principle

2.1、Hardware schematic diagram

image-20231018170623917

2.2、Physical connection diagram

image-20231020093159203

2.3、Principle of control

Use the keys to control the data written and read in flash, and print the data out through the serial port.

By defining different functions for the key, the data read and write operation in flash is realized.

KEY(Schematic name)Control pinFunction
KEY1PG3Add 1 to the count
KEY2PG4The count is cleared to zero
KEY3PG5Reading count values

STM32F103ZET6 Flash memory capacity is 512K bytes, a total of 256 2K byte pages;

Flash memory is mainly used to store program code and constant data in order to retain these data when restarted after power failure.

Programming and erasing flash memoryregisterFunction
The FPEC key registerFLASH_KEYRUsed to erase and program Flash memory unlocking and locking
Select the byte key registerFLASH_OPTKEYROption bytes used to unlock and lock the Flash memory
Flash memory control registersFLASH_CRUsed to control Flash memory programming and erase operations
Flash memory status registerFLASH_SRUsed to report Flash memory status information
Flash address registerFLASH_ARUsed to specify the address of Flash memory to be programmed or erased
Select the byte registerFLASH_OBRThe value used to store the option bytes
Write the protected registerFLASH_WRPRUsed to specify that Flash memory sectors are write-protected to prevent accidental modification.

Unlock → erase → write data → lock

operationfunction
UnlockHAL_FLASH_Unlock
eraseHAL_FLASHEx_Erase
write dataHAL_FLASH_Program
lockHAL_FLASH_Lock

3、Engineering configuration

Project Configuration: Prompts for configuration options in the STM32CubeIDE project configuration process

3.1、Notes

Omitted project configuration: New project, chip selection, project configuration, SYS for pin configuration, RCC configuration, clock configuration, and project configuration content

The project configuration part, which is not omitted, is the key point to configure in this tutorial.

3.2、Pin configuration

You can directly select the corresponding pin number in the pin view, and the corresponding option will appear when the mouse is left clicked

5f368c0f80beabc0089a5fb5abf8b35b

image-20231020122426785

image-20231020122526760

image-20231020122549648

image-20231020122655542

image-20231030192315753

image-20231020123054441

4、Main function

This section mainly introduces the functional code written by users. Detailed code can be opened by yourself in the project file we provide, and enter the Bsp folder to view the source code.

4.1、User function

function:STMFLASH_ReadHalfWord

Function prototypesu16 STMFLASH_ReadHalfWord(u32 faddr)
Functional DescriptionRead a half-word (16-bit data) at the specified address.
Input parametersfaddr:address
Return valueData for the address

function:STMFLASH_Write_NoCheck

Function prototypesvoid STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
Functional DescriptionWrites data of the specified length from the specified address without checking
Input parameters1WriteAddr:Start address
Input parameters2pBuffer:Data pointer
Input parameters3NumToWrite:Half-word (16 bit) number
Return valueNone

function:STMFLASH_Write

Function prototypesvoid STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
Functional DescriptionWrites data of the specified length starting at the specified address
Input parameters1WriteAddr:Start address
Input parameters2pBuffer:Data pointer
Input parameters3NumToWrite:Half-word (16 bit) number
Return valueNone

function:STMFLASH_Read

Function prototypesvoid STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
Functional DescriptionRead data of the specified length starting at the specified address
Input parameters1ReadAddr:Start address
Input parameters2pBuffer:Data pointer
Input parameters3NumToRead:Half-word (16 bit) number
Return valueNone

4.2、HAL library function parsing

The HAL library functions that were covered in the previous tutorial will not be covered

function:HAL_FLASH_Unlock

Function prototypesHAL_StatusTypeDef HAL_FLASH_Unlock(void)
Functional DescriptionUnlock access to the flash control register
Input parametersNone
Return valueHAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT

function:HAL_FLASH_Lock

Function prototypesHAL_StatusTypeDef HAL_FLASH_Lock(void)
Functional DescriptionLock flash control register access
Input parametersNone
Return valueHAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT

function:HAL_FLASH_Program

Function prototypesHAL_StatusTypeDef HAL_FLASH_Program
(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
Functional DescriptionUsed for writing data to Flash
Input parameters1TypeProgram:Type of data written: half-word, byte, or double-word
Input parameters2Address:The address where Flash writes data
Input parameters3Data:To write data
Return valueHAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT

function:HAL_FLASHEx_Erase

Function prototypesHAL_StatusTypeDef HAL_FLASHEx_Erase
(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
Functional DescriptionA pointer to a FLASH_EraseInitTypeDef structure used to erase a sector in Flash memory
Input parameters1pEraseInit:Configure the parameters for the erase operation
Input parameters2PageError:Used to get the sector number of the error in the erase operation
Return valueHAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT

5、Experimental phenomenon

After downloading the program successfully, press the RESET button of the development board to open the serial debugging assistant to observe the phenomenon

phenomenon:

Press KEY1: The serial debugging assistant will output the prompt of adding 1 to the count;

Press KEY2: The serial debugging assistant will output the prompt of zero count;

Press KEY3: Serial debugging assistant will read the current count value;

After the development board power off, you can also read the previous count value.

image-20231102193914514