Skip to main content
keepcoding
Associate III
Oct 7, 2013 4:11 PM UTC
Question

Flash Memory: crash on write access

Posted on October 07, 2013 at 18:11

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.
    This topic has been closed for replies.

    6 replies

    Tesla DeLorean
    Guru
    Oct 7, 2013 6:22 PM UTC
    Posted on October 07, 2013 at 20:22

    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

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    keepcoding
    Associate III
    Oct 7, 2013 6:34 PM UTC
    Posted on October 07, 2013 at 20:34

    I tried your code. Strange, it stops right at this line:

    status = FLASH_ProgramHalfWord(Address, 0x1234);

    Something is wrong with my setup... any idea?
    keepcoding
    Associate III
    Oct 7, 2013 6:39 PM UTC
    Posted on October 07, 2013 at 20:39

    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!
    Tesla DeLorean
    Guru
    Oct 7, 2013 6:54 PM UTC
    Posted on October 07, 2013 at 20:54

    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.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    keepcoding
    Associate III
    Oct 7, 2013 7:16 PM UTC
    Posted on October 07, 2013 at 21:16

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

    Tesla DeLorean
    Guru
    Oct 8, 2013 1:43 AM UTC
    Posted on October 08, 2013 at 03:43

    RM0313 also has this in the Flash Controller section

    ''For program and erase operations on the Flash memory (write/erase), the internal RC

     

    oscillator (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.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..