Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Keywords: Uninitialized, Compilation Environment

Table of Contents

1. Introduction

2. IAR Method for Uninitialized Variables

3. Keil Method for Uninitialized Variables

4. CubeIDE Method for Uninitialized Variables

01

Introduction

Sometimes in our applications, we require variables to maintain continuity or to retain state, such as during a Bootloader jump. For various reasons, during a reset process, some critical variables must not be initialized. Different compilation environments have different settings. This article summarizes this operation and introduces the methods used in Keil, IAR, and CubeIDE. The microcontroller used in this article is STM32G431RBT6.

02

IAR Method for Uninitialized Variables

The implementation in IAR is relatively simple; you can directly use the keyword “__no_init” to modify the variable:

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

To verify successful execution, you can periodically reset the system and observe the variable changes. For example, the following sample program resets the system periodically, and you will find that each time the Test_NoInit data increases by 10 based on the previous data, rather than being initialized and then increasing by 10.

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

03

Keil Method for Uninitialized Variables

In Keil, there is no equivalent keyword like in IAR, and there may be version differences. Below are the introductions:

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Figure 1. Different compilation versions in Keil

To prevent uninitialized variables from being initialized to 0, they must be placed in a special section that meets the ZI data section (.bss), which has the UNINIT attribute in its execution region.

3.1. ArmĀ® Compiler 5 Operation

Modify the project’s linker file, *.sct file

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Figure 2. Modify the default linker file

Here, RAM is divided into two regions, where RW_IRAM2 is the region for uninitialized variables, with the attribute set to UNINIT, defining a region name NO_INIT.

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Define variables in this section; AC5 requires the use of the zero_init modifier.

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

3.2. ArmĀ® Compiler 6 Operation

In AC6, you need to add the .bss ZI definition, as shown in the following sct file modification:

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Define variables in the section; AC5 and AC6 also have differences, and the zero_init modifier is no longer supported, as shown in the definition below:

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

The specific differences between versions AC5 and AC6 can be referenced in the Keil help file:

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Figure 3. Keil help file regarding zero initialization

04

CubeIDE Method for Uninitialized Variables

The implementation in CubeIDE is similar to that in Keil; the linker file *.ld needs to be modified. First, divide the RAM to delineate the uninitialized RAM area:

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Figure 4. Dividing RAM regions

Add region descriptions and include region names:

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Figure 5. Region description and name definition

Define variables in this uninitialized region:

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Additionally, it is important to note that some STM32 series have specific option configuration bits for whether certain RAM regions will be initialized after reset. For example, in the STM32L4 series, to prevent SRAM2 variables from being initialized, you need to configure the SRAM2_RST bit in the option byte as shown below:

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

Practical Experience: Variable Initialization in Keil, IAR, CubeIDE

For the complete content, please click “Read Original” to download the original document.

Leave a Comment