Learning diary - MCU internal flash read and write (2020.2.12)

FLASH Basics

• stm32 of flash address starts at 0x0800 0000, the end of the address is 0x0800 0000 plus actual flash chip size, different flash chip size.
• Flash content is generally used to store code and data definitions const, the power is not lost.
• Internal Flash configuration:
• the STM32 internal FLASH includes a main memory, system memory, the OTP region and an option byte area.

FLASH internal configuration

• STM32 internal FLASH includes a main memory, system memory, the OTP region and an option byte area.
• Main memory: In general we say when the internal STM32 FLASH, refer to the main memory area which is space to store user applications, 1M FLASH chip model description of, 2M FLASH refer to the size of the region. Like other FLASH, before writing data, first press sector erase.
• System memory: System memory is an area user can not access it when the device is shipped with the boot code has been cured, it is responsible for implementing serial, USB, and CAN and other burning ISP functions.
• OTP area: OTP (One Time Program), refers to the write-once storage area of 512 bytes, the write data can not be changed, commonly used in the OTP encryption key storage applications.
• Byte options: Option byte configuration for the FLASH write protection, the BOR-level power management, software / hardware watchdog function, which part of a total of 32 bytes. Can be modified by modifying FLASH option control register.

Pin Configuration

Configuration LED, KEY1, KEY2 and serial
Here Insert Picture Description

Configure the system clock

Using the internal clock (default); the system clock up to 80MHZ
Here Insert Picture Description

Pin detailed configuration parameters

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

FLASH production engineering

1、自定义工程名称。
2、选择英文路径,否则会丢 失启动文件而无法编译通过, 需要手动添加启动文件: startup_stm32l431xx.s
3、选择MDK-ARM V5开发软件, 即KEIL5软件

代码编写说明

1、按键KEY1按下时,把数据写入MCU的flash
2、按键KEY2按下时,把写入MCU的flash数据并读取出来,打印数据。
调用函数
1、HAL_FLASH_Unlock(void);//解锁
2、FLASH_EraseInitTypeDef FLASH_EraseInitSturcture;//配置需要擦除的参数
3、HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef pEraseInit, uint32_tPageError);//擦除
4、HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);//写数据到flash
5、HAL_FLASH_Lock(void);//锁住
6、HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);//检测按键
7、void HAL_Delay(uint32_t Delay);//延时
8、HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);//点亮和熄灭LED
9、uint8_t OneWord=(__IO uint32_t)addr;//读取地址数据
10、printf();//打印数据到串口函数 需要重新定义函数fputc();才能正常使用printf();
int fputc(int ch,FILE*f)
{
uint8_t temp[1]={ch};
HAL_UART_Transmit(&huart1,temp,1,2);
return HAL_OK;
}

代码编写说明

• FLASH读写流程
1、对FLASH写入数据流程:
1)解锁FLASH:调用HAL_FLASH_Unlock(void);
2)擦除FLASH:调用HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef pEraseInit, uint32_t PageError);
3)写入数据到FLASH:调用HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
4)锁住FLASH:调用HAL_FLASH_Lock(void);
2、FLASH读取数据流程 直接读取相应的FLASH地址即可:通过
(__IO uint32_t
)ADDR读取该地址数据

代码编写实现

#define strLen sizeof (strWriteToFlash1) / sizeof (uint8_t) // write data length
#define ADDR 0x0803f800 // write address
to write data FLASH :

void writeflash(int addr, uint32_t* writeFlashData) 
{ 
	uint32_t PageError = 0; 
	int i=0; HAL_FLASH_Unlock();
	FLASH_EraseInitTypeDef f; 
	f.Banks=1; 
	f.NbPages=1; 
	f.Page=255; 
	f.TypeErase=0;
	HAL_FLASHEx_Erase(&f,&PageError);
	HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,addr,writeFlashData[i]); 
	HAL_FLASH_Lock(); 
}

Coding to achieve

while (1) within a program cycle :

if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==GPIO_PIN_RESET)
 { 
 	HAL_Delay(100); 
 	if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==GPIO_PIN_RESET)
 	 {
 	 printf("KEY1 Press\r\n");
 	 writeflash(ADDR,(uint32_t*)&writeFlashData);//擦除并写入数据			        HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);//LED电平翻转
 	  } 
 }
 if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==GPIO_PIN_RESET) 
 {
 	HAL_Delay(100);
 	if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==GPIO_PIN_RESET)
 	{
 		printf("KEY2 Press\r\n"); ReadFlashData(ADDR,sizeof(writeFlashData),R_OldFlash);//读取写入的数据 
 		HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin); //LED电平翻转 }}
 		```
 **读取FLASH数据**: 
 `void ReadFlashData(int addr,uint32_t strSize,uint8_t* ReadData) 
 {
 	uint8_t OneWord; for(int i=0;i<strSize;i++)
 	 {
 	 	OneWord=*(__IO uint32_t*)addr; 
 	 	ReadData[i]=OneWord; 
 	 	addr++;
 	  }
 	  	printf("Read the flash data:>>%s<<,address:%x\r\n",ReadData,addr-strSize);// 显示该地址的数据 
 }`
 **扩展实验**:将移植flash.c,该文件有实现写数 据到FLASH的多种方式,如写字符串数据到 FLASH等。



 




Released nine original articles · won praise 0 · Views 258

Guess you like

Origin blog.csdn.net/quanqueen/article/details/104331534