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:
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