Difference between revisions of "Read-Modify-Write Flash"

From SEGGER Wiki
Jump to: navigation, search
(RMW in J-Link Commander)
(Assumptions)
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
__TOC__
 
__TOC__
   
== Theoretical example 1 ==
+
== Use Case 1 ==
 
This example assumes that a bootloader is already present in the sector in which the sample application is to be downloaded.
 
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.
 
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.
Line 15: Line 15:
 
! Description || Location || Size
 
! Description || Location || Size
 
|-
 
|-
| First sector || 0x0000_0000 || 0x10000 bytes
+
| First sector || 0x0000_0000 to 0x0000_FFFF || 0x10000 bytes
 
|-
 
|-
 
| Bootloader location || 0x0000_0000 to 0x0000_3FFF || 0x4000 bytes (already in flash)
 
| Bootloader location || 0x0000_0000 to 0x0000_3FFF || 0x4000 bytes (already in flash)
Line 24: Line 24:
   
 
=== Determining the correct value for RMW ===
 
=== Determining the correct value for RMW ===
To make sure RMW will be used, the value for <tt>SetFlashDLNoRMWThreshold</tt> has to be ApplicationSize + 1 '''or higher'''.
+
To make sure RMW will be used when setting <tt>SetFlashDLNoRMWThreshold = <value></tt>, <value> has to be ApplicationSize + 1 '''or higher'''.
   
  +
To cover the use case above <value> must be between <tt>0xC001</tt> and <tt>0xFFFFFFFF</tt>. E.g.:
For our example, this means, everything between
 
 
"SetFlashDLNoRMWThreshold = 0xC001"
 
"SetFlashDLNoRMWThreshold = 0xC001"
  +
or
and
 
 
"SetFlashDLNoRMWThreshold = 0xFFFFFFFF"
 
"SetFlashDLNoRMWThreshold = 0xFFFFFFFF"
will result in read modify write to be executed, and thus the bootloader and application will both be present in flash after downloading.
+
will both result in read modify write to be executed, and thus the bootloader and application will both be present in flash after downloading.
   
=== RMW in J-Link Commander ===
+
== Use Case 2 ==
For this example, we assume that
 
* the name of the data file with the application is "App.hex".
 
* the bootloader is already present in the device flash.
 
* the J-Link commander is already connected to the device
 
r
 
exec SetFlashDLNoRMWThreshold = 0xFFFFFFFF // Always do read modify write.
 
loadfile App.hex
 
 
== Theoretical example 2 ==
 
 
Assumptions:
 
Assumptions:
 
* RMW value is set to default (0x400)
 
* RMW value is set to default (0x400)
Line 49: Line 40:
 
The Second sector is left untouched.
 
The Second sector is left untouched.
   
== Theoretical example 3 ==
+
== Use Case 3 ==
 
* RMW value is set to default (0x400)
 
* RMW value is set to default (0x400)
 
* Flash has 2 * 1 KiB sectors
 
* Flash has 2 * 1 KiB sectors
Line 61: Line 52:
 
In such cases, it is important to preserve the remaining data in the sector to allow the application to further work correctly.<br>
 
In such cases, it is important to preserve the remaining data in the sector to allow the application to further work correctly.<br>
   
  +
== RMW using J-Link Commander ==
== Sample projects ==
 
  +
How to execute a J-Link command string using the J-Link Commander is described in detail [[J-Link_Commander#Using_J-Link_Command_Strings | 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 [[J-Link_script_files | here]].
  +
<source lang="c">
  +
int HandleBeforeFlashProg(void) {
  +
JLINK_ExecCommand("SetFlashDLNoRMWThreshold = 0xFFFFFFFF"); // Always use read modify write.
  +
return 0;
  +
}
  +
</source>
  +
  +
== Sample project ==
 
In this section the RMW is explained with a couple of practical examples.
 
In this section the RMW is explained with a couple of practical examples.
   
Line 67: Line 74:
 
The following project was designed for an Infineon XMC4500 MCU, but the procedure can be applied to any other MCU, too.
 
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.
 
In case of the XMC4xxx MCUs, the physical memory sectors are split into smaller logical ones.
The J-Link owever J-Link will always flash the phy
 
 
 
The sample project is designed to be used with J-Link and a Infineon XMC4500 MCU. It will we automatically executed when starting the .bat file.
 
 
 
The .bat and .jlink file can be used as a reference for your own RMW flash projects.
 
The .bat and .jlink file can be used as a reference for your own RMW flash projects.
  +
*[[Media:Wiki_J-LinkCommander_RMW_Example.zip | Wiki_J-LinkCommander_RMW_Example.zip]]
   
  +
=== How to ===
[[Media:Wiki_J-LinkCommander_RMW_Example.zip | Wiki_J-LinkCommander_RMW_Example.zip]]
 
  +
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.
== How to ==
 
  +
Make sure to adjust this value depending on the device and sector you are trying to modify.
 
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 you adjust this value depending on the device and sector you are trying to modify.
 
   
 
To use the example project follow these steps:
 
To use the example project follow these steps:

Latest revision as of 08:23, 7 October 2022

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)