我用stm32L562测试程序,存储8字节的数据到内部flash,用的dual bank mode。 在同一地址再次写入数据时,提示了FLASHIF_WRITING_ERROR错误。 #define DESIGNATED_ADDRESS 0x0803F800 uint32_t value = 50; uint64_t addr = DESIGNATED_ADDRESS; void flash() { uint32_t dest = 0; if((FLASH_WRITE(addr, &value,2) != 0)) { print("Error\n"); }
dest = FLASH_READ(addr); print("data = %d", dest); value++; } FLASH_WRITE(uint32_t destination, uint32_t *p_source, uint32_t length) { uint32_t status = FLASHIF_OK; uint32_t i = 0; HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); /* DataLength must be a multiple of 64 bit */ for (i = 0; (i < length / 2) /*&& (destination <= (USER_FLASH_END_ADDRESS - 8))*/; i++) { if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, destination, *((uint64_t *)(p_source + 2*i))) == HAL_OK) { if (*(uint64_t*)destination != *(uint64_t *)(p_source + 2*i)) { /* Flash content doesn't match SRAM content */ status = FLASHIF_WRITINGCTRL_ERROR; break; } /* Increment FLASH destination address */ destination += 8; } else { /* Error occurred while writing data in Flash memory */ status = FLASHIF_WRITING_ERROR; break; } } HAL_FLASH_Lock(); return status; } uint32_t FLASH_READ(uint32_t address) { return *(uint32_t*)address; }
|