Search code examples
cembeddedstm32flash-memoryjtag

Writing in STM32L4x1 flash memory in C


I am trying to write in STM32L476's flash memory using a JTAG ST-Link/V2 on Windows 7. No software has to be uploaded, I only need to write data in a non-volatile place where it can be read and deleted.

As a newbie regarding hardware and being efficient only when programming non-embedded regular C, I am afraid I might harm or modify irrevocably the flash memory. Also, I am not really sure regarding what I can or cannot do.

I have figured out reading the manual that writing in 0x08000000 memory place seemed to be a good idea. Using C code to make calls to ST-Link_Utility :

const char CMD_ACCESS_ST_UTILITY[] = "cd C:/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility&&ST-LINK_CLI.exe ";

bool STLINKWriteSystemCalls(void)
{
    char cmd[200] = "";


    strcpy(cmd, CMD_ACCESS_ST_UTILITY); // cmd to access utility
    strcat(cmd, "-c"); // Then connect with ST-Link
    if (system(cmd) != 0)
        return false; // If failed exit


    strcpy(cmd, CMD_ACCESS_ST_UTILITY);
    strcat(cmd, "-r8 0x08000000 0x100"); // Read to check if there is data already

    // I haven't managed yet how I'll compare the result of read
    // To FFFF but that's not the main issue

    if (system(cmd) != 0)
        return false; // If failed exit


    strcpy(cmd, CMD_ACCESS_ST_UTILITY);
    strcat(cmd, "-w8 0x08000000 0x00"); // Write data into 0x080000000
    if (system(cmd) != 0)
        return false; // If failed exit

    return true;
}

Is there a better way to do this regarding where to write and how to write (errors checking, ressource used etc) ?


Solution

  • Main things to know about flash memories:

    1. Flash memories are page erasable, in your case page size if 2K. What does it means? That you must be sure that your code does not resides in the 2k range of page.
    2. After erase the state of all bytes in memory is 0xFF.
    3. Write to a flash byte means, at raw level, to modify bits set to 1 to bit set to 0. If you want to modify a bit from 0 to 1 you must erase a whole page.

    ST-Link_Utility does it embedded way, so when you write to flash it erase a whole sector ans then write data. The same if your code is required to ease data after they are used.


    By default your mCU, at startup, uses 0x0000 0000 aliased address of 0x0800 0000.

    First words should contain the reset vector and the default vector table. Reset vector is always the first instruction to be executed.The reset vector in this table will contain a branch to an address which will contain the reset code.

    So, in other words, At the time of power up, the processor jumps at fixed location 0x0, which means it jumps at 0x0800 0000. Obviously the address you selected is not correct ;)