2011-06-13 06:25 AM
I'm trying to write a half word in the flash memory. Every time the system starts my code write a half word in flash, but it works
only the first time. In the others times the data is not store(PGERR error), and still the first data recorded until I re-program the chip. I'm using stm32f103C4, 16K flash. My code to write in flash: &sharpdefine FLASH_PAGE_SIZE ((uint16_t)0x400) &sharpdefine BANK1_WRITE_START_ADDR ((uint32_t)0x08003C00) &sharpdefine BANK1_WRITE_END_ADDR ((uint32_t)0x08003FFF) typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; uint32_t EraseCounter, Address; __IO uint8_t NbrOfPage; volatile FLASH_Status FLASHStatus; volatile TestStatus MemoryProgramStatus; void Grava_data(unsigned int Data){ FLASHStatus = FLASH_COMPLETE; MemoryProgramStatus = PASSED; FLASH_UnlockBank1(); /* Define the number of page to be erased */ NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR); FLASHStatus = FLASH_EraseOptionBytes(); /* Erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); } /* Program Flash Bank1 */ Address = BANK1_WRITE_START_ADDR; /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR); FLASHStatus = FLASH_ProgramHalfWord(Address, Data); } To read the data from flash: unsigned int Le_data(void){ Address = BANK1_WRITE_START_ADDR; return (*(__IO uint16_t*) Address); } #basic-math-error #write-flash-memory-pgerr-error2011-06-13 06:55 AM
/* Define the number of page to be erased */
NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; The way you've coded your addresses, NbrOfPage = ZERO, nothing is erased.
2011-06-13 07:24 AM
Thanks,
You Solve the problem, now it's working. :)