0

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;

//      }

   }

}
kaylum
  • 13,833
  • 2
  • 22
  • 31
  • Is there a question? – Tom V Feb 02 '22 at 22:17
  • There's not enough information. Although you vaguely describe how you're calling `FLASH_WriteA` you need to show the code that calls it and how those parameters are all defined. Also, where and how is `seg` defined (for `FLASH_WriteA`)? Where and how is `flashBuffer` defined and initialized? These all seem to affect what's going on. – lurker Feb 02 '22 at 22:53
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 12 '22 at 13:44

0 Answers0