AnsweredAssumed Answered

Write in Flash problem error

Question asked by vasconcelos.vinicius on Jun 13, 2011
Latest reply on Jun 13, 2011 by vasconcelos.vinicius
  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:

 #define FLASH_PAGE_SIZE    ((uint16_t)0x400)
#define BANK1_WRITE_START_ADDR  ((uint32_t)0x08003C00)
#define 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);
}

Outcomes