Memory

This document describes the memory system of RTL87x2G and introduces how to use it. RTL87x2G memory consists of ROM, RAM, Flash, and eFuse, as shown in Memory Layout. This flexible memory configuration mechanism allows RTL87x2G to support a wide range of application programs.

Memory Layout

Memory Type

Region

Start Addr

End Addr

Size(bytes)

ROM

0x00000000

0x0007FFFF

512K

RAM

ITCM1

0x00100000

0x0012FFFF

192K

DTCM0

0x00130000

0x0013FFFF

64K

DTCM1

0x00140000

0x0014FFFF

64K

Data SRAM

0x00200000

0x00203FFF

16K

Buffer SRAM

0x00280000

0x0028BFFF

48K

Cache

I-Cache

32K

D-Cache

16K

Flash

SPI Flash

0x04000000

0x07FFFFFF

64M*

eFuse

eFuse

1K

Note

* means the maximum mapping space is 64 MB, but the actual size depends on the used Flash memory size.

ROM

The ROM code is located at [0x0, 0x7FFFF), with a total size of 512KB, in which Bootloader, Flash Driver, Power Manager, Bluetooth Controller, and other platform modules are built in. RTL87x2G ROM opens some modules such as Flash Driver for applications to use. RTL87x2G SDK contains the header files of these ROM modules, which enable users to access built-in ROM functions. This reduces the application code size and RAM size.

RAM

Memory Space Layout

RAM is divided into three blocks, namely TCM, Data RAM, and Buffer RAM. Namely TCM with an address space of 0x00100000 ~ 0x0012FFFF with a total size of 192KB, DTCM0 with an address space of 0x00130000 ~ 0x0013FFFF with a total size of 64KB, and DTCM1 with an address space of 0x00140000 ~ 0x0014FFFF with a total size of 64KB. The three space addresses are consecutive.

  • TCM: A specially designed type of memory that focuses on providing low latency, high bandwidth, and deterministic access characteristics, containing ITCM and DTCM:

    • ITCM: A type of TCM specifically used for storing program instructions, allowing the CPU to quickly access and execute the code.

    • DTCM: A type of TCM specifically designed for storing data, enhancing the access speed to this data.

The RAM Space of RTL87x2G is divided into 5 parts. Five parts of RAM space can be used to store data and execute code.

RAM Space

Region

Starting Address

Address Range

Total Size

ITCM1

0x00100000

0x00100000~0x0012FFFF

192KB

DTCM0

0x00130000

0x00130000~0x0013FFFF

64KB

DTCM1

0x00140000

0x00140000~0x0014FFFF

64KB

Data SRAM

0x00200000

0x00200000~0x00203FFF

16KB

Buffer SRAM

0x00280000

0x00280000~0x0028BFFF

48KB

The memory space layout will be different for TrustZone enable and TrustZone disable, as follows:

TrustZone Disable Memory Space Layout

../../../../_images/TrustZone_disable_RAM_layout.png

TrustZone disable RAM Layout

Note

When TrustZone is disabled, there is no distinction between secure and non-secure.

Each part has a fixed arrangement sequence and a specific purpose, as shown in TrustZone disable RAM Usage.

TrustZone disable RAM Usage

Memory Type

Memory Usage

Memory Variable

S ROM data

Store global variables and static variables in Secure ROM Code

No

Main Stack

Used in startup and interrupt service routines

No

Boot Patch RAM

Store Boot Patch global variables and static variables and code executed on RAM

No

System Patch RAM

Store System Patch global variables, static variables, and code executed on RAM

No

Stack Patch RAM

Store Stack Patch global variables, static variables, and code executed on RAM

No

NS ROM data

Store global variables and static variables in Non-Secure ROM Code

No

DTCM + ITCM Heap

For the dynamic application space of Non-Secure ROM code, System Patch code, Non-Secure Upperstack code, and APP code

Yes

APP RAM

Store global variables, static variables, and code executed on RAM in APP

Yes

Bluetooth Host RAM

Store global variables, static variables, and code executed on RAM in Bluetooth Host

Yes

TrustZone Enable Memory Space Layout

../../../../_images/TrustZone_enable_RAM_layout.png

TrustZone enable RAM Layout

TrustZone enable RAM Usage

Memory Type

Memory Usage

Memory Variable

S ROM data

Store global variables and static variables in Secure ROM Code

No

Secure Main Stack

The stack is used in secure startup and interrupt service routines

No

Boot Patch RAM

Store Boot Patch global variables and static variables and code executed on RAM

No

System Patch RAM

Store System Patch global variables and static variables and code executed on RAM

No

Stack Patch RAM

Store Stack Patch global variables and static variables and code executed on RAM

No

NS ROM data

Store global variables and static variables in Non-Secure ROM Code

No

NS Main Stack

The stack is used in non-secure startup and interrupt service routines

No

Secure Heap

For the dynamic application space of Secure ROM code and SecureAPP code

Yes

Secure APP RAM

Store Secure APP global variables and static variables and code executed on RAM

Yes

NS DTCM + ITCM Heap

For the dynamic application space of Non-Secure ROM code, System Patch code, Bluetooth Host code and APP code

Yes

NS APP RAM

Store global variables and static variables in NS APP and code executed on RAM

Yes

Bluetooth Host RAM

Store global variables and static variables in Bluetooth Hostand code executed on RAM

Yes

The Adjustable RAM space is uniformly modified through different macro definitions in mem_config.h. NS_RAM_UPPERSTACK_SIZE must be set accordingly according to the version of the BT Host image used. Different BT Host images occupy different sizes of DTCM. Please pay attention to the BT Host release document and its description. Adjust the size of NS_RAM_UPPERSTACK_SIZE according to application needs.

Adjustable RAM space

Memory Type

Macro

Upperstack RAM

NS_RAM_UPPERSTACK_SIZE

APP RAM

NS_RAM_APP_SIZE

DTCM + ITCM Heap

NS_RAM_APP_RESERVED_SIZE - NS_RAM_APP_SIZE - NS_RAM_UPPERSTACK_SIZE

APIs

By calling the API: os_mem_alloc, memory can be dynamically applied for from DTCM Heap or Buffer SRAM Heap. The type of application is specified by the parameter ram_type, which corresponds to RAM_TYPE_DATA_ON and RAM_TYPE_BUFFER_ON. The specific code is as follows:

typedef enum
{
    RAM_TYPE_DATA_ON,        //DTCM Heap
    RAM_TYPE_DATA_OFF = RAM_TYPE_DATA_ON,
    RAM_TYPE_BUFFER_ON,
    RAM_TYPE_BUFFER_OFF = RAM_TYPE_BUFFER_ON,
    RAM_TYPE_EXT_DATA_SRAM,  //Ext DATA SRAM heap
    RAM_TYPE_NUM
} RAM_TYPE;

/**
* @brief  Allocate memory dynamically from Data RAM Heap or Buffer RAM heap
* @param  ram_type: specify which heap to allocate memory from
* @param  size: memory size in bytes to be allocated
* @retval pointer to the allocated memory
*/
#define os_mem_alloc(ram_type, size)
        os_mem_alloc_intern(ram_type, size, __func__, __LINE__)

Other APIs for dynamically applying for memory are as follows: (For details, refer to os_mem.h)

Dynamic Memory Allocation APIs

API

Description

os_mem_zalloc

allocated memory will be initialized to 0

os_mem_aligned_alloc

allocated memory will be aligned to the specified alignment

os_mem_free

free a memory block that had been allocated

os_mem_aligned_free

free an aligned memory block that had been allocated

os_mem_peek

get the unused memory size of the specified RAM type

Memory Usage Calculation

According to the way the APP uses RAM, it is divided into static area and dynamic area.

Statistics of Static Zone Size on APP RAM

Find the app.map file generated by Build to find the first address allocated in APP RAM, the end address, and the used size. By searching the map of the loading area ER_IRAM_NS Map, it is possible to determine the static usage of APP RAM.

../../../../_images/ER_IRAM_NS_Map.png

ER_IRAM_NS Map

Statistics of Remaining Heap Size

The remaining heap size of the specified RAM type can be obtained through the os_mem_peek function.

Adjust the RAM Available to APP

To further increase the total size of RAM available for the app, you can use the following two methods:

  • Modify some of the stack settings in the Stack interface of the MP Tool Config Setting, such as the number of Bluetooth links, AE, and PSD functions.

../../../../_images/MPTool_Stack_Config_Setting.png

Stack interface in MP Tool Config Setting

  • Disable log: Modify the Log interface in the MP Tool Config Setting to change the log to ‘log disable’.

Cache

RTL87x2G supports 32KB I-Cache and 16KB D-Cache.

Cache default policy:

  • Flash is cacheable by default.

  • Data SRAM and buffer SRAM are non-cacheable by default.

Flash

Refer to Flash.

PSRAM

Refer to PSRAM.

eFuse

eFuse is a block of one-time programming memory that is used to store important and fixed information, such as UUID, security key, and other one-time programming configurations. A single bit of eFuse cannot be changed from 0 to 1, and there is no erase operation for eFuse, so be careful when updating eFuse. Realtek offers MPTool to update certain eFuse sections.