1. Introduction
The STM32WB Zigbee stack is a software component designed to enable Zigbee communication on the STM32WB microcontroller. Like any software, the stack requires memory to operate. There are two main types of memory that are relevant to the stack: RAM and Flash memory.
RAM (Random Access Memory) is a type of volatile memory that is used for temporary storage of data and program code. The stack uses RAM to store data structures, variables, and other runtime objects. The amount of RAM required by the stack will depend on the application and the used features.
Flash memory, on the other hand, is a type of non-volatile memory that is used for long-term storage of program code and data. The stack uses Flash memory to store the code and data that make up the stack itself. The amount of Flash memory required by the stack will depend on..
Memory Types in Microcontrollers |
---|
Another important concept to understand is memory allocation. The stack uses a heap to allocate memory dynamically at runtime. This means that the stack can allocate memory as needed, rather than requiring a fixed amount of memory to be allocated at compile time. However, this also means that the stack requires some amount of overhead to manage the heap.
Developers should carefully consider their application's requirements and consult the stack's documentation to determine the appropriate memory requirements.
2. Memory Layout of C Code
Microcontrollers Flash memory contains different segments of application code such as, interrupt vector table, code segment, initialized data segment, uninitialized data segment, constant data segment etc. The linker script file defines where each of these segments will store in flash memory.
These are the typical sections of a given C binary:
- Text Section: This section contains the machine code of the compiled C program. It is read-only and contains the instructions that the processor executes.
- Data Section: This section contains the initialized global and static variables of the program. It is writable and read-only.
- BSS Section: This section contains the uninitialized global and static variables of the program. It is writable and read-only.
- Heap Section: This section is dynamically allocated by the program during runtime. It is used for dynamic memory allocation. The heap area begins at the end of the BSS segment and grows upwards to higher memory addresses.
- Stack Section: This section is used for storing local variables and function parameters. It grows and shrinks as functions are called and returned. On the standard x86 architecture it grows downwards to lower addresses.
Simple Memory Layout of C Program |
---|
Heap is not automatically managed and should be explicitly allocated using malloc()
for example, and deallocated using free()
. So it is managed by the programmer.
Later, we will focus on heap memory and we will give formula to compute it on STM32WB for a given Zigbee application.
3. Startup File and Linker Script
The startup file and the linker script allow to initialize the microcontroller's hardware and link the various sections of the code together.
The startup file includes functions that set up the stack, initialize the memory, and configure the clock and other peripherals so that the user application can run. The linker script determines how the code and data are organized in memory, it defines the ROM_start, ROM_end, RAM_start and RAM_end for example.
The following figure shows the memory map representation of the On/Off STM32WB55.Nucleo application example of the STM32Cube MCU Package. We have two separate linker files, the first for M4 and the second for M0, which is common and necessary to ensure that the code and data are placed in the correct memory regions for each core
4. STM32WB Zigbee software architecture
As a reminder, the STM32WB MCUs are built with two separate cores, Arm® Cortex®-M4 and Arm® Cortex®-M0+ cores. The architecture is depicted in the figure below. Mainly, the M0 core contains the Zigbee stack, and the M4 core contains clusters and the developer’s application.
STM32WB Zigbee architecture |
---|
STM32WB contains up to 1 Mbyte of flash memory single bank architecture, can be accessed starting from address 0x0000 0000 or 0x0800 0000, internal SRAM1 (up to 192 KB) and internal SRAM2a (32 KB) + SRAM2b (32 KB).
From system point of view, there are 2 heaps:
- one on M4 side
- one on M0 side to be used by Zigbee stack
5. STM32WB stack memory requirements
5.1. STM32WB stack heap size
The stack allocates run-time objects, such as packets, timers, and endpoints, using a heap. The application can configure the size of the heap using the ZbInit struct ZbInitTblSizesT
parameters, specifically heapPtr and heapSz. The heap memory is allocated from the M4 core, allowing any memory, such as packets, passed between the M0 and M4 to be accessed and modified by either core.
M0 heap is included in M4 heap and allocated using ZbMalloc when ZbInit is called.
If ZbInit is called providing a NULL heapPtr, M0 heap will be allocated with default size depending on SW type:
- FFD SW: 32 KB
- RFD SW: 16 KB
5.2. STM32WB stack tables size
The following table describes the impact on memory usage for each additional entry:
Entry Type | Memory Usage Per Entry (bytes) |
---|---|
Network Neighbor Table entry (nwkNeighborTblSz) | 144 |
Network Routing Table entry (nwkRouteTblSz) | 28 |
Network Address Mapping Table (nwkAddrMapTblSz) | 24 |
APS Link Key Table (apsPeerLinkKeyTblSz) | 48 |
Default network topology in FFD SW is:
- ZB_NWK_NEIGHBOR_TABLE_DEFAULT_SIZE=16U
- ZB_NWK_NLME_ADDRMAP_TABLE_DEFAULT_SIZE=16U
- ZB_NWK_NLME_ROUTE_TABLE_DEFAULT_SIZE=16U
- ZB_NWK_NLDE_BTT_TABLE_DEFAULT_SIZE=16U
- ZB_NWK_NLME_RREQ_TABLE_DEFAULT_SIZE=4U
- ZB_APS_DEVICE_KEY_PAIR_DEFAULT_COUNT=16U
With such configuration, overall M4 heap consumption is close to 52 KB with 32 KB allocated for M0 heap + fixed M0 allocation + variable memory size depending on topology.
To compute M4 heap requirement for a given topology, you can use below formula:
M4 heap size= (32+16)*1024 + neighb_table_size*144 + routing_table_size*28 + address_mapping_table_size*24 + aps_link_key_table_size*48
Formula is accurate +/-10%
5.3. Device Configuration And Memory Usage
Description | nwkNeighborTblSz | nwkRouteTblSz | nwkAddrMapTblSz | apsPeerLinkKeyTblSz | Heap kB | Total Memory kB |
---|---|---|---|---|---|---|
Coordinator, 30 Devices, 30 Neighbors / Children | 30 | 30 | 30 | 30 | 32 | 54 |
Coordinator, 100 Devices, 40 Neighbors / Children | 40 | 30 | 100 | 100 | 32 | 60 |
Coordinator, 200 Devices, 40 Neighbors / Children | 40 | 30 | 200 | 200 | 32 | 67 |
Router, 30 Neighbors / Children | 30 | 30 | 4 | 4 | 32 | 52 |
Router, 100 Neighbors / Children | 100 | 30 | 4 | 4 | 32 | 61 |
End-Device, 8 Neighbors | 8 | 0 | 2 | 2 | 8 | 14.5 |
Note: Total Memory includes some items with constant overhead, which are not listed in this table.
6. STM32WBA Zigbee software architecture
As a reminder, the STM32WBA MCUs are built with Arm® Cortex®-M33 core. The architecture is depicted in the figure below.
STM32WBA Zigbee architecture |
---|
STM32WBA5xxG devices contain a 1-Mbyte flash memory from address offset 0x00 0000 to 0x0F FFFF, and SRAM1 64-Kbyte from address offset 0x0 0000 to 0x0 FFFF. (continuous SRAM space with SRAM2)
7. STM32WB stack memory requirements
7.1. STM32WBA stack heap size
The size of memory buffer is provided by heapPtr. If heapSz is set to zero, then a default value of 32 kB for FFD or 16 kB for RFD is chosen, and memory will be allocated through ZbMalloc().
7.2. STM32WBA stack tables size
The following table describes the impact on memory usage for each additional entry:
Entry Type | Memory Usage Per Entry (bytes) |
---|---|
Network Neighbor Table entry (nwkNeighborTblSz) | 136 |
Network Routing Table entry (nwkRouteTblSz) | 32 |
Network Address Mapping Table (nwkAddrMapTblSz) | 24 |
APS Link Key Table (apsPeerLinkKeyTblSz) | 88 |
8. Acronyms and definitions
Term | Definition |
---|---|
APS | Application Support Sublayer |
RAM | Random Access Memory |
FFD | Full Function Device |
RFD | Reduced Function Device |