cancel
Showing results for 
Search instead for 
Did you mean: 

I use HAL_FLASH_Program to program my internal flash, but it takes too much time, I program about 90kbytes, it will take about 90 seconds. Is there any faster way to program stm32l072.

ZXiao.1
Associate II

my pagesize is 256 bytes, and 90k is about 351 pages

  for(i = 0; i < pages; i++)

  {

    W25qxx_ReadPage((uint8_t *)u32_databuf, EXT_FLASH_ADDR_APP_START + i,0, w25qxx.PageSize);

    ProgramInternalFlash(APP_BASE_ADDR + i * w25qxx.PageSize, u32_databuf,w25qxx.PageSize);

  }

void ProgramInternalFlash(uint32_t u32_intFlashstart, uStruct_u32 *pData ,uint32_t data_len)

{

 HAL_FLASH_Unlock();

 /* Fill EraseInit structure*/

 EraseInitStruct.TypeErase  = FLASH_TYPEERASE_PAGES;

 EraseInitStruct.PageAddress = u32_intFlashstart;

 EraseInitStruct.NbPages   = data_len / FLASH_PAGE_SIZE;

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

 {

   Error_Handler();

 }

  

 Address = u32_intFlashstart;

 uint16_t i = 0;

 while (Address < u32_intFlashstart + data_len)

 {

    

  if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address,pData[i].u32_buf) == HAL_OK)

  {

   Address = Address + 4;

   i += 1;

  }

  else

  {

    Error_Handler();

  }

 }

 HAL_FLASH_Lock();

   

}

6 REPLIES 6

Perhaps it is your W25Q routines which are slowing. See ​how fast it runs without those for a test/baseline.

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

No, I have comment the code W25qxx_ReadPage, the result is same.

ZXiao.1
Associate II

is there any DMA code to move memory to internal flash code example or use interrupt for flash code?

TDK
Guru

It's possible erasing all pages at once would be faster than one at a time.

There is HAL_FLASH_Program_IT, but unlikely to help if the bottleneck is the flash erase/write speed.

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

i use the code below follow the code examples on page938 of RM0376 Reference manual , it will reduce the time to 9 seconds.The note was under the code Note: This function must be loaded in RAM. I'm not sure can this code run in flash? and why only program half page instead of 1 page?

/**

* This function programs a half page. It is executed from the RAM.

* The Programming bit (PROG) and half-page programming bit (FPRG)

* is set at the beginning and reset at the end of the function,

* in case of successive programming, these two operations

* could be performed outside the function.

* This function waits the end of programming, clears the appropriate

* bit in the Status register and eventually reports an error.

* Param flash_addr is the first address of the half-page to be programmed

* data is the 32-bit word array to program

* Retval None

*/

__RAM_FUNC void FlashHalfPageProg(uint32_t flash_addr, uint32_t *data)

{

  uint8_t i;

  /* (1) Set the PROG and FPRG bits in the FLASH_PECR register

  to enable a half page programming */

  /* (2) Perform the data write (half-word) at the desired address */

  /* (3) Wait until the BSY bit is reset in the FLASH_SR register */

  /* (4) Check the EOP flag in the FLASH_SR register */

  /* (5) clear it by software by writing it at 1 */

  /* (6) Reset the PROG and FPRG bits to disable programming */

  FLASH->PECR |= FLASH_PECR_PROG | FLASH_PECR_FPRG; /* (1) */

  for (i = 0; i < ((FLASH_PAGE_SIZE/2) / 4); i++)

  {

    *(__IO uint32_t*)(flash_addr) = *data++; /* (2) */

  }

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

  {

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

  }

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

  {

    FLASH->SR = FLASH_SR_EOP; /* (5) */

  }

  else

  {

    /* Manage the error cases */

  }

  FLASH->PECR &= ~(FLASH_PECR_PROG | FLASH_PECR_FPRG); /* (6) */

}

Thank you for your advice. i have moved all erase code before write code.

firstly i EraseInternalFlash(APP_BASE_ADDR,APP_LENGTH ); erase all my app code.