cancel
Showing results for 
Search instead for 
Did you mean: 

I'm using STM32F030K6T6TR and i'm trying to use the flash memory. It worked to erase the pages that i need but when i write i get a hard fault error

AMaha.1
Associate

#define FLASH_USER_START_ADDR  0x0800FC00UL

#define FLASH_USER_END_ADDR  0x08010000UL

 uint32_t Address = 0, PageError = 0;

 __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;

 /*Variable used for Erase procedure*/

 static FLASH_EraseInitTypeDef EraseInitStruct;

 HAL_FLASH_Unlock();

 EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;

 EraseInitStruct.PageAddress = FLASH_USER_START_ADDR;

 EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;

 if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)

 {

  /*

   Error occurred while page erase.

   User can add here some code to deal with this error.

   PageError will contain the faulty page and then to know the code error on this page,

   user can call function 'HAL_FLASH_GetError()'

  */

  /* Infinite loop */

   HAL_FLASH_GetError();

  while (1)

  {

  }

 }

 HAL_FLASH_Lock() ;

 __disable_irq();

 HAL_FLASH_Unlock();

 if (HAL_FLASH_Unlock() == HAL_ERROR) {

 while(1) {}

 }

 Address = 0x0800FC00UL;

//  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_BSY | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);

 uint32_t varToWrite[10] = {12};

 uint32_t *pVarToWrite = varToWrite;

   FLASH->CR |= FLASH_CR_PER; /* (1) */

   FLASH->AR = Address; /* (2) */

   FLASH->CR |= FLASH_CR_STRT; /* (3) */

   while ((FLASH->SR & FLASH_SR_BSY) != 0) /* (4) */

   {/* For robust implementation, add here time-out management */}

   if ((FLASH->SR & FLASH_SR_EOP) != 0) /* (5) */

   {FLASH->SR |= FLASH_SR_EOP; /* (6)*/}

   else

   {/* Manage the error cases */}

   FLASH->CR &= ~FLASH_CR_PER; /* (7) */

 while (Address < FLASH_USER_END_ADDR)

 {

//  HAL_Delay(1000);

  if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, *(uint64_t*)pVarToWrite) == HAL_OK)

  {

   Address = Address + 4; // 4 tis integer

   pVarToWrite++;

  }

  else

  {

   /* Error occurred while writing data in Flash memory.

     User can add here some code to deal with this error */

   while (1)

   {

   }

  }

 }

 HAL_FLASH_Lock();

 __enable_irq();

Can some one please tell me why i get the error at HAL_FLASH_Program()

2 REPLIES 2

Double word in this context is 8 bytes wide, not 4

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Yes i wanted to try the FLASH_TYPEPROGRAM_DOUBLEWORD but i forgot to put the FLASH_TYPEPROGRAM_WORD back. It's still not working with the other options