Read-Modify-Write Flash

From SEGGER Wiki
Jump to: navigation, search

This article describes how to use the Read-Modify-Write (RMW) function in the J-Link software. This article assumes that there is already a basic knowledge about flash programming in general. By default the Read-Modify-Write threshold is set to 1 KiB in order to allow read-modify-write of single bytes in flash (e.g. when editing bytes in memory windows of IDEs). To increase or decrease the threshold J-Link offers the following command string: SetFlashDLNoRMWThreshold = <value>

Note:

RMW will only be executed if the memory to be written is smaller than the RMW value. This also means:

  • Setting the RMW to 0x0, will disable read modify write entirely
  • Setting it to 0xFFFFFFFF will result in RMW to be always used

Use Case 1

This example assumes that a bootloader is already present in the sector in which the sample application is to be downloaded. The memory of bootloader and application is not overlapping so the application can be added to the same sector as the bootloader, without overwriting the bootloader, assuming the correct RMW value is set.

Assumptions

Description Location Size
First sector 0x0000_0000 to 0x0000_FFFF 0x10000 bytes
Bootloader location 0x0000_0000 to 0x0000_3FFF 0x4000 bytes (already in flash)
Application location 0x0000_4000 to 0x0000_FFFF 0xC000 bytes (to be downloaded)

Determining the correct value for RMW

To make sure RMW will be used when setting SetFlashDLNoRMWThreshold = <value>, <value> has to be ApplicationSize + 1 or higher.

To cover the use case above <value> must be between 0xC001 and 0xFFFFFFFF. E.g.:

"SetFlashDLNoRMWThreshold = 0xC001"

or

"SetFlashDLNoRMWThreshold = 0xFFFFFFFF"

will both result in read modify write to be executed, and thus the bootloader and application will both be present in flash after downloading.

Use Case 2

Assumptions:

  • RMW value is set to default (0x400)
  • Flash has 2 * 1 KiB sectors
  • Debugger writes 512 bytes

J-Link will perform a read-modify-write on the first sector, preserving contents of 512 - 1023 bytes. The Second sector is left untouched.

Use Case 3

  • RMW value is set to default (0x400)
  • Flash has 2 * 1 KiB sectors
  • Debugger writes 1280 bytes

J-Link will erase + program 1 KiB of first sector.
J-Link will erase + program 256 bytes of second sector.
Previous 768 bytes from second sector are lost.
The default makes sense for flash programming where old contents in remaining space of affected sectors are usually not needed anymore. Writes of < 1 KiB usually mean that the user is performing flash manipulation from within a memory window in a debugger to manipulate the application behavior during runtime (e.g. by writing some constant data used by the application). In such cases, it is important to preserve the remaining data in the sector to allow the application to further work correctly.

RMW using J-Link Commander

How to execute a J-Link command string using the J-Link Commander is described in detail here.

Example

exec SetFlashDLNoRMWThreshold = 0xFFFFFFFF // Always use read modify write.
r
loadfile App.hex

RMW using J-Link Script file

How to use J-Link script files is described in detail here.

int HandleBeforeFlashProg(void) {
 JLINK_ExecCommand("SetFlashDLNoRMWThreshold = 0xFFFFFFFF"); // Always use read modify write.
 return 0;
}

Sample project

In this section the RMW is explained with a couple of practical examples.

XMC4xxx

The following project was designed for an Infineon XMC4500 MCU, but the procedure can be applied to any other MCU, too. In case of the XMC4xxx MCUs, the physical memory sectors are split into smaller logical ones. The .bat and .jlink file can be used as a reference for your own RMW flash projects.

How to

For the example project the SetFlashDLNoRMWThreshold value is set to 0xFFFF (64 KiB RMW threshold). This means that the J-Link DLL will perform a read-modify-write of flash memory regions <= 64 KiB. Make sure to adjust this value depending on the device and sector you are trying to modify.

To use the example project follow these steps:

  • Unzip the contents of the Wiki_J-LinkCommander_RMW_Example.zip to a example folder
  • execute StartTest.bat
  • The J-Link commander will open and you should see the following output
Flash Success.PNG
  • If your output matches the screenshot, the RMW was successful (this applies only to the example project, the output on your own project might vary)