Operation of reading and writing Flash in STM3210XX
Operation of reading and writing Flash in STM3210XX
Operation of reading and writing Flash in STM3210XX
The programming tool is Keil uVision5.
Some settings need to be saved when the machine is powered off. At this time, the settings should be written to Flash and read out when necessary.
Actual code:
uint32_ t ReadFromFlashOneWord(uint32_t Addr){
//Read a word from Flash (half a word)
uint32_ t HData,LData;// Define high and low data
HData=(__IO uint16_t)Addr;
LData=(__IO uint16_t)(Addr+2);
return (HData<<16)+LData;
}
uint16_ t ReadFromFlashHalfWord(uint32_t Addr){
//Read 32-bit byte data and return 16-bit data
return (__IO uint16_t)Addr;
}
void ReadFromFlashMultipleWord(uint32_t StartAddr,uint16_t ReadData,uint16_t Count){
uint16_ t i;
for(i=0;iReadData[i]=ReadFromFlashHalfWord(StartAddr+i2);
}
}
void WriteToFlashOneWord(uint32_t StartAddr,uint32_t WriteData){
//Write a word to the address specified by Flash
HAL_ FLASH_ Unlock();
//Erase
FLASH_ EraseInitTypeDef EraseInitStrut;
EraseInitStrut.TypeErase=FLASH_ TYPEERASE_ PAGES;
EraseInitStrut.PageAddress=StartAddr;
EraseInitStrut.NbPages=1;
//Set Page Error
uint32_ t PageError=0;
//Erase
HAL_ FLASHEx_ Erase(&EraseInitStrut,&PageError);
//Write Flash
HAL_ FLASH_ Program(TYPEPROGRAM_WORD,StartAddr,WriteData);
//Lock Flash
HAL_ FLASH_ Lock();
}
Read and write test:
uint32_ t TestData=0x12345678;
uint32_ t TestAddr=0x0800F000; // Write Flash
WriteToFlashOneWord(TestAddr,TestData);
HAL_ Delay (100);
//uint16_ t OutData;
uint8_ t HData1,HData2,LData1,LData2;
HData1= (uint8_t)(TestData>>24);
HData2= (uint8_t)(TestData>>16);
LData1= (uint8_t)(TestData>>8);
LData2=(uint8_t)TestData;
//OutData=ReadFromFlashHalfWord(TestAddr);
Send_ Buffer[61]=HData1;
Send_ Buffer[62]=HData2;
Send_ Buffer[63]=LData1;
Send_ Buffer[64]=LData2;
HAL_ UART_ Transmit(&hlpuart1,Send_Buffer,70,10);