STM32F446 Flash programming issue - Page 1
Forum section: electronics / general

author: cmcraeslo
author_rank: Regular Contributor
timestamp: June 26, 2024, 12:56:46 pm
content: 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: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 everytimeHAL_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

author: cmcraeslo
author_rank: Regular Contributor
timestamp: June 27, 2024, 06:33:39 am
content: 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.

author: pcprogrammer
author_rank: Super Contributor
timestamp: June 27, 2024, 09:22:43 am
content: 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.

author: cmcraeslo
author_rank: Regular Contributor
timestamp: June 27, 2024, 09:47:56 am
content: 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)

author: cmcraeslo
author_rank: Regular Contributor
timestamp: June 27, 2024, 10:56:50 am
content: 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 stuffHAL_UART_DMAResume(&huart1);Go figure.