2013-10-07 09:11 AM
Hi
I'm trying to write to the Flash ROM on my STM32F3 board (256k). Erasing a page works fine, but when I try to write a word (or half word), the MCU crashes. I'm doing what the standard peripheral library suggests:FLASH_Unlock();
FLASH_ErasePage(0x0803f800);
FLASH_ProgramHalfWord(0x0803f800, 0x0001); // crash!
FLASH_Lock();
What am I doing wrong?
Btw, the CPU speed is 72 MHz and the Flash is configured accordingly:
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY_1;
I'm using Keil uVision to install the program on the flash memory (start address is 0x08000000). Vector table offset is 0x0.
2013-10-07 11:22 AM
Working for me in Keil, on a STM32F3-Discovery
printf(''FLASH Size %d KB
'', *((uint16_t *)0x1FFFF7CC));
Address = 0x0803F800;
/* Unlock the Program memory */
FLASH_Unlock();
/* Clear all FLASH flags */
FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR | FLASH_FLAG_BSY);
status = FLASH_ErasePage(Address);
printf(''ErasePage %08
X'', status);
if (status != FLASH_COMPLETE)
puts(''ErasePage Failed'');
printf(''%08X : Before
'', *((uint32_t *)Address));
status = FLASH_ProgramHalfWord(Address, 0x1234);
printf(''ProgramHalfWord %08
X'', status);
if (status != FLASH_COMPLETE)
puts(''ProgramHalfWord Failed'');
printf(''%08X : After
'', *((uint32_t *)Address));
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) */
FLASH_Lock();
FLASH Size 256 KB
ErasePage 00000004
FFFFFFFF : Before
ProgramHalfWord 00000004
FFFF1234 : After
2013-10-07 11:34 AM
I tried your code. Strange, it stops right at this line:
status = FLASH_ProgramHalfWord(Address, 0x1234);
Something is wrong with my setup... any idea?
2013-10-07 11:39 AM
Ok, just found it out:
#ifndef PLL_SOURCE_HSI
RCC_HSICmd(DISABLE); // disable HSI (high-speed internal oscillator)
#endif
I disabled the HSI because I thought I wouldn't need it...
BTW: why is HSI needed to write to the Flash, but not to read from it??
Thanks anyway for your help!
2013-10-07 11:54 AM
I'd have to review the docs. Is this a Discovery board, or a custom one. Your message implies the latter, but you posted to the Discovery forum.
If custom, any major differences to the basic reference design? Mods to system_stm32f3xx.c? Got any other interrupt running? I basically pasted your code into a template project for the Discovery board, from the firmware library.2013-10-07 12:16 PM
It's a basic discovery board. Mods to the system_stm32f30x.c? Only '#define PLL_SOURCE_HSE' instead of HSE_BYPASS, otherwise standard.
I just looked into the reference manual and the 'clock tree' diagram depicts some connection between the HSI clock and the 'Flash programming interface' (FLITFCLK).. strange then why I didn't get any error while erasing a flash page...2013-10-07 06:43 PM
RM0313 also has this in the Flash Controller section
''For program and erase operations on the Flash memory (write/erase), the internal RCoscillator (HSI) must be ON.'' A bit of a subtle trap there. No mention in the example code, or README.TXT (STM32F3-Discovery_FW_V1.1.0\Project\Peripheral_Examples\FLASH_Program\readme.txt) I'll flag this to the moderator.