Programming option bytes on STM32F051 with J-Link

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • Programming option bytes on STM32F051 with J-Link

    Hello,

    I am trying to program the option bytes (read protection) on a STM32F051 with the j-Link Commander.
    I read UM8001 and UM08003, in both manuals only J-Flash is mentioned (J-Link only for unsecure).

    Can i set the read protection with j-Link Commander? Does anybody has a example?

    Here my code where i want to add the read protection

    Source Code

    1. device STM32F051K8
    2. r
    3. h
    4. loadbin MyProgram.bin, 0x0
    5. verifybin MyProgram.bin, 0x0
    6. // Enable read protection
    7. g
    8. qc


    Thank you :)
  • Hello,

    The option bytes of the STM32F051 can be set by performing several read/write accesses to different flash registers.
    Please find below a small generic sample sequence which can be adapted into J-Link Commander syntax (e.g. w4 to write to a register).

    #define FLASH_OPTOBL (1 << 13) // Start Option Byte Loader and force system reset
    #define FLASH_OPTWRE (1 << 9)
    #define FLASH_OPTPG (1 << 4)
    #define FLASH_OPTER (1 << 5)

    #define FLASH_KEY1 (0x45670123)
    #define FLASH_KEY2 (0xCDEF89AB)

    #define FLASH_BSY (0x00000001)

    //
    // Unlock flash block
    //
    FlashKEYR = FLASH_KEY1;
    FlashKEYR = FLASH_KEY2;
    FlashCR = 0;
    //
    // Erase option bytes
    //
    FlashOPTKEYR = FLASH_KEY1;
    FlashOPTKEYR = FLASH_KEY2;
    FlashCR = FlashCR | FLASH_OPTWRE | FLASH_OPTER; // Enable "Option byte erase"
    FlashCR = FlashCR | FLASH_OPTWRE | FLASH_OPTER | FLASH_STRT; // Start operation
    while (FlashSR & FLASH_BSY);
    //
    // Program option bytes
    //
    FlashCR = FlashCR | FLASH_OPTWRE | FLASH_OPTER; // Enable "Option byte program"
    0x1FFFF800 = 0x55AA; // Values to set device to "unlocked state"; Must adapted according by the user
    while (FlashSR & FLASH_BSY);
    //
    // Trigger the Option Byte Loader to activate the programmed option bytes
    //
    FlashCR = FLASH_OPTWRE | FLASH_OPTPG | FLASH_OPTOBL;
    while (FlashSR & FLASH_BSY);


    Below a small sample how the code must be adapted into J-Link Commander syntax for the first 3 lines:
    //
    // Unlock flash block
    //
    w4 0x40022004 0x45670123
    w4 0x40022004 0xCDEF89AB
    w4 0x40022010 0


    Best regards
    Erik
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hello Erik,

    thanks for your detailed help.

    I read the procedure to set the option bytes in the STM32F0xx RM but i did not know hat to set them by the j-link commander.

    You wrote i had to set the option bytes with the j-link commander syntax. Is it possible to write it into jlinkSkript (with C-like syntax) and to
    start it by the j-link commander? If i type "?" i see all commands but there are no commands like "if" or "while".

    Can I add some text to the stdout by the commander? After programming and setting up the read protection i would like to post some
    text at the console like "Program, verification and locking successfully)".

    Best Regards
    HawkEye
  • Hi,

    You could pass a command file to the J-Link Commander via command line option.
    J-Link Commander will perform the commands located in the file in batch mode. So you could automate this process.
    For further information, please refer to chapter "3.2.1 Command line options" of the J-Link user manual: UM08001.
    As the command file does not allow do - while loops, you have to enter defined sleeps instead of reading the status register and waiting
    for a ready bit is set. This can be done by passing the "sleep" command. A list of all commands can be outputted by writing "?" in the J-Link Commander.

    You should be able to adapt the sequence by using the commands below:
    w4 <Addr> <Value>
    mem32 <Addr> <Value>
    sleep <TimInMS>

    If you need any further help, please do not hesitate to ask.

    Can I add some text to the stdout by the commander? After programming and setting up the read protection i would like to post some
    text at the console like "Program, verification and locking successfully)".
    Custom user output is currently not supported by J-Link Commander command files. We will discuss internally if we will add such a opportunity to the tool.


    Erik
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.