Author Topic: STM32F446 Flash programming issue  (Read 1255 times)

0 Members and 1 Guest are viewing this topic.

Offline cmcraesloTopic starter

  • Regular Contributor
  • *
  • Posts: 64
  • Country: 00
STM32F446 Flash programming issue
« on: June 26, 2024, 12:56:46 pm »
I'm writing bootloader code for STM32F466 running at 180Mhz and have stumbled across an issue I'm unable to solve.
My bootloader has reserved space from 0x8000000 - 0x8007FFF. At 0x8008000 is where i'm doing the programming and this is where my application starts.

Issue I'm having is that after 7 programming cycles, ecu goes to hard fault.

Simplified code i'm using:
Code: [Select]
fe.TypeErase   = FLASH_TYPEERASE_SECTORS;
fe.Sector = FLASH_SECTOR_2;
fe.VoltageRange = FLASH_VOLTAGE_RANGE_3;
fe.NbSectors     = 1;

HAL_FLASH_Unlock();
HAL_FLASHEx_Erase(&fe, &err);

HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x8008000, 1);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x8008001, 2);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x8008002, 3);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x8008003, 4);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x8008004, 5);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x8008005, 6);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x8008006, 7); // this is where it fails everytime
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x8008007, 8);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, 0x8008008, 9);

I'm stepping through the code:
- flash gets erased successfully
- when I start to write I monitor with debugger and values are stored correctly
- at 7th write, mcu goes to hard fault.

I have tried writing half word and word as well (and increasing the address by +2 and +4) and it's the same. At 7th write, it halts.

I'm sure there's some alignment issue or some other bug in my code, but I just don't see it.

I tried with different bit clearing (EOP, PGPERR etc) before and after the program and erase to no avail, it doesn't make any difference.

Would love to hear any suggestion from someone smarter than me.

p.s. i have bootloaders for stm32f1 and f3 working perfectly, it's just this f4 that's causing issues.

Thanks

« Last Edit: June 26, 2024, 01:05:49 pm by cmcraeslo »
 

Offline cmcraesloTopic starter

  • Regular Contributor
  • *
  • Posts: 64
  • Country: 00
Re: STM32F446 Flash programming issue
« Reply #1 on: June 27, 2024, 06:33:39 am »
One thing I also found today: if I go step by step (into the actual flash program function) instead of step over, it works and doesn't hard fault.
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 5144
  • Country: nl
Re: STM32F446 Flash programming issue
« Reply #2 on: June 27, 2024, 09:22:43 am »
Sounds like a timing issue.

Try adding a delay between every flash program line and increase the delay until the error does not come up anymore.

Offline cmcraesloTopic starter

  • Regular Contributor
  • *
  • Posts: 64
  • Country: 00
Re: STM32F446 Flash programming issue
« Reply #3 on: June 27, 2024, 09:47:56 am »
Tried that. No difference.

Inside the HAL_FLASH_Program there's FLASH_WaitForLastOperation() which I assume waits for flash to be ready again. Also, inside the real program, i'm also checking status of each command and I get HAL_OK everytime (timeout on that wait for example would return != HAL_OK)
 

Offline cmcraesloTopic starter

  • Regular Contributor
  • *
  • Posts: 64
  • Country: 00
Re: STM32F446 Flash programming issue
« Reply #4 on: June 27, 2024, 10:56:50 am »
I think I figured it out. I had to pause UART DMA to make it work. Not sure why this is, but after 3 days of struggling with all sort of things, this looks like a solution.


So basically.. here's the code that works:

HAL_UART_DMAPause(&huart1);
.. do all the flash stuff
HAL_UART_DMAResume(&huart1);

Go figure.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf