The noinit section is familiar to most of us developers working with microcontrollers. If this section is used, it will be automatically allocated by the compiler to a region in the chip’s SRAM (note that it is in the RAM area, not Flash). Variables defined in this section will not be re-initialized during any chip reset, except for power-off. Therefore, the significance of this section is considerable, commonly used to store sensitive information, data that cannot be reset in special applications, or reset flag data. Personally, I feel that the usage of this data section is quite broad and very useful.
Next, let’s take the NXP Kinetis series KL26 as an example in the IAR environment to briefly introduce the usage of the noinit section. The method is quite simple, as shown in the figure below. There are two ways to define variables in the noinit section: one is to directly add the “__no_init” keyword in front of the variable type, and the other is to use the pre-compile directive #pragma, which is a commonly used method in IAR to specify sections. I personally prefer the first method, as it is straightforward and clear.
In fact, at this point, we can consider it concluded, as the usage of the noinit section is relatively simple. However, I do not want to end here so soon; I would like to introduce another situation of using noinit (I’m not done yet, haha). Normally, the address range of the SRAM area where the noinit section is located is automatically allocated by the compiler. However, if I were a perfectionist (ahem, I’m not, just hypothetically, haha), or if the user needs to specify the variables in noinit to fixed addresses (some applications require this, such as those with a Bootloader, since the Bootloader code and user code are two separate projects compiled separately), it is necessary to inform the Bootloader of the specific address where the variables defined in the noinit section are located in the user code, so that the Bootloader can directly read from that address. This requires us to “play some tricks” to force the compiler to allocate the noinit variables to our specified addresses. Of course, there are several methods to achieve this in IAR, but I will only provide one reliable method, which is to modify the linker file. In the IAR linker file, allocate the specified size and address range for the noinit section, allowing us to determine the size and address range allocated to the noinit section based on the size of the external variables. The specific method is as follows:
1. Open the linker file for the user project in IAR (I will take the linker file for KL26 128kB flash as an example). As shown in the figure below, when allocating the starting space for RAM, we can reserve 0x30, which is 48 bytes (I defined 12 integer variables in the application, requiring 48 bytes);
2. Specify a fixed region for the noinit section (if not specified, it will be automatically allocated by the compiler). I set the address range for the noinit section to be from (RAMstart-0x30) to RAMstart (this way it will not conflict with other data), occupying 0x30 bytes, and at the same time, the starting address of the noinit section is also determined, which is RAMstart-0x30;
3. This step is to allocate the .noinit section range to the above-defined noinit_region. Essentially, it tells the compiler that variables defined as __no_init need to be forcibly placed in the noinit_region;
4. Then, in the C file, directly define a temp array and declare it in the __no_init section, with 12 elements, occupying a total of 48 bytes. If you define 13 elements (52 bytes), the compiler will return an error indicating that it exceeds the range, so remember to modify the linker file if we need to change the size of the variables;
5. Finally, compile the entire project and open the generated .map file. You can see from the figure below that the temp variable is fixedly allocated at address 0x1ffff410, with a size of 0x30 bytes. How cool is that, haha.
Only now can we say it’s truly finished. I wonder if my fellow bloggers have gained any insights after reading this carefully. If you found it helpful, remember to give a thumbs up below my blog, haha.