提问人:Mahmoud Kotb 提问时间:11/10/2023 更新时间:11/10/2023 访问量:23
STM23L412闪存擦除和编程(EEPROM仿真)问题
STM23L412 Flash Erase and Programming (EEPROM Emulation) Issue
问:
我正在尝试编写一个代码来存储一些值STM32L412我成功擦除该页面,但我没有向其写入任何内容,该代码基于参考手册(RM0394)https://www.st.com/resource/en/reference_manual/rm0394-stm32l41xxx42xxx43xxx44xxx45xxx46xxx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf 第83页和第84页
uint32_t *address_to_write = (uint32_t *)0x0801F800; // Address to write the data page 63
uint64_t data_to_write = 0x1122334455667788; //Data to be written to the flash
uint32_t data_been_read; //read the data which been written
void clearFlash(uint8_t pagesCount, uint8_t firstPage){
HAL_FLASH_Unlock(); //Unlock the flash
FLASH->SR &= ~(FLASH_FLAG_PGSERR | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR |FLASH_FLAG_OPERR | FLASH_FLAG_EOP); // Clear all error programming flags
for (int i = 0; i<pagesCount; i++){ //Looping for clearing enough space.
while(FLASH->SR & FLASH_SR_BSY); // Wait until the BSY bit is cleared
while((FLASH->SR & FLASH_SR_PGSERR)); //PGSERR should not be set
FLASH->CR |= FLASH_CR_PER; //Set erase process
FLASH->CR &= ~FLASH_CR_PNB_Msk; //Clear address mask
FLASH->CR |= ((firstPage+i)<<FLASH_CR_PNB_Pos); //place in address
FLASH->CR |= FLASH_CR_STRT; //Start clearing the flash
}
HAL_FLASH_Lock(); //lock the flash
}
void flash_program_double_word(uint32_t *address, uint64_t data) {
HAL_FLASH_Unlock(); //Unlock the flash
while (FLASH->SR & FLASH_SR_BSY);//Check that no Flash main memory operation is ongoing
FLASH->SR = FLASH_SR_PGSERR | FLASH_SR_PGAERR | FLASH_SR_SIZERR | FLASH_SR_PROGERR; //Clear all error programming flags
FLASH->CR |= FLASH_CR_PG; //Set the PG bit in the Flash control register
//Perform the data write operation
*(__IO uint32_t*)address = (uint32_t)(data & 0xFFFFFFFF); // Write the first word
address += 4; // Move to the next aligned double word address
*(__IO uint32_t*)address = (uint32_t)(data >> 32); // Write the second word
while (FLASH->SR & FLASH_SR_BSY); // Wait until the BSY bit is cleared
FLASH->SR = FLASH_SR_EOP; // Check and clear EOP flag
FLASH->CR &= ~FLASH_CR_PG; // Clear the PG bit in the Flash control register
HAL_FLASH_Lock(); //lock the flash
}
uint32_t flashReadData(uint32_t *address){
__IO uint32_t read_data = *(__IO uint32_t *)address;
return (uint32_t)read_data;
}
这是我的测试代码
clearFlash(1,63);
flash_program_double_word(address_to_write, data_to_write);
data_been_read = flashReadData(address_to_write);
所以从STM32CubeIDE的内存中,我可以看到页面中的所有地址都被擦除了0xFFFFFFFF但那里什么也没写,谁能帮忙吗?
谢谢。
我还尝试了很多其他方法,但都失败了,例如 使用此库 https://github.com/nimaltd/ee 以及使用基于本应用笔记的 HAL 函数 https://www.st.com/resource/en/application_note/an3969-eeprom-emulation-in-stm32f40xstm32f41x-microcontrollers-stmicroelectronics.pdf
答: 暂无答案
评论
address += 4;
如果你有,看起来不对。uint32_t *address