cancel
Showing results for 
Search instead for 
Did you mean: 

Writing to array to flash memory which is uint16_t. I am using a STM32L053 nucleo. My program gets a hard fault error.

AV.9
Associate II

This is the how I'm trying to write to flash

Basically I pass my uint16_t array to a function called FLASH_WriteA which accepts the array, the destination array or the location and the length

void FLASH_WriteA(uint16_t * src, uint16_t * dest, uint16_t length_16)

{

   uint32_t firstAddress= FLASH_GetFirstAddress((uint32_t)dest, PAGE_SIZE_BYTES); // gets the first address of that page

   uint32_t offset = ((uint32_t)dest - seg) / 2; // offset is in words

   HAL_FLASH_Unlock();

   Flash_Erase_Page((uint16_t*)seg); // calls the page erase function

   FLASH_Write_HAL((uint32_t *)&flashBuffer, (uint16_t*)seg, FLASH_TABLE_SIZE_BYTES); //FLASH_TABLE_SIZE_BYTES = 256 = size of array

   HAL_FLASH_Lock();

}

HAL_StatusTypeDef Flash_Erase_Page(uint16_t* seg){//, uint16_t length){

//uint16_t pages = (length/page_size) + (int)((length % page_size) != 0);

   uint32_t pages = 2; // page size for STM32L053 = 128 bytes

   FLASH_EraseInitTypeDef EraseInitStruct;

   EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;

   EraseInitStruct.PageAddress = seg;

   EraseInitStruct.NbPages = pages;

   uint32_t PageError;

   if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)      //Erase the Page Before a Write Operation

   {

         return HAL_ERROR;

   }

}

void FLASH_Write_HAL(uint32_t *p_source, uint16_t* seg, uint16_t length)

{

   uint32_t varToPass;

   uint16_t wordcount=0;

   int i;

   for (i=0; i<length; i++){

      varToPass = p_source[i] ;

      HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,seg+wordcount,varToPass);

      wordcount += 4;

//      if (wordcount>=256){

//         break;

//      }

   }

}

2 REPLIES 2
TDK
Guru

seg isn't defined as a parameter in the function that you use it in, while src is never used.

You don't show the values you're passing to FLASH_WriteA, so who knows.

Perhaps you're trying to erase pages the program resides in.

If you feel a post has answered your question, please click "Accept as Solution".
AV.9
Associate II

seg basically gets the starting address of the fourth last page. I have debugged this.

The error during debugging actually occurs during the HAL_FLASH_PROGRAM