Global struct/array not initalized to zero

Discussions and support about ChibiOS/RT, the free embedded RTOS.
akshaim
Posts: 20
Joined: Tue Sep 21, 2021 9:47 am
Has thanked: 9 times

Global struct/array not initalized to zero

Postby akshaim » Fri Apr 01, 2022 9:45 pm

Hi Forum,

I have a strange issue. I am using STM32F767ZIT6 micro controller for my project. Since I ran out of space in ram3 , I tried to use `ram0` using the

Code: Select all

__attribute__((section("DATA_RAM")))
. `DATA_RAM` alias is defined in `ARMCMx/compilers/GCC/ld/STM32F76xxI.ld` .

I then declared a "global" struct as follows

Code: Select all

struct Temp_Struct struct_d[NUM_VAL][NUM_VAL_1] __attribute__((section("DATA_RAM"))) = {0};


As per the C standard, all members in the struct should be initialised to zero using the syntax, right? However, I see that a few members are not initialized to zero. When I remove `__attribute__((section("DATA_RAM")))` the members are initialized to zero. Has anyone faced a similar issue, if so did you manage to solve it ?

I did some debugging and noticed the following.
I printed memory usage using
--print-memory-usage
in os/common/startup/ARMCMx/compilers/GCC/mk/rules.mk by editing the following line

Code: Select all

LDFLAGS   = $(MCFLAGS) $(OPT)  -nostartfiles $(LLIBDIR) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,--library-path=$(STARTUPLD),--script=$(LDSCRIPT)$(LDOPT),--print-memory-usage


This would however print ram0 as full. This is not true since I have rarely used DATA_RAM (ram0). Does this mean that the linker script is what that's causing the trouble ? The linker could also be wrong since ram1 and ram2 are marked as 0.00% being used, ram0 is the sum of ram1 and ram2.

Code: Select all

Linking build/ch.elf
Memory region         Used Size  Region Size  %age Used
          flash0:      297624 B         2 MB     14.19%
          flash1:      134672 B         2 MB      6.42%
          flash2:          0 GB         0 GB
          flash3:          0 GB         0 GB
          flash4:          0 GB         0 GB
          flash5:          0 GB         0 GB
          flash6:          0 GB         0 GB
          flash7:          0 GB         0 GB
            ram0:        384 KB       384 KB    100.00%
            ram1:          0 GB       368 KB      0.00%
            ram2:          0 GB        16 KB      0.00%
            ram3:      110824 B       128 KB     84.55%
            ram4:          0 GB        16 KB      0.00%
            ram5:          0 GB         4 KB      0.00%
            ram6:          0 GB         0 GB
            ram7:          0 GB         0 GB


Thanks and Regards,

Akshai M

psyco
Posts: 21
Joined: Fri May 22, 2020 1:40 am
Been thanked: 11 times

Re: Global struct/array not initalized to zero

Postby psyco » Sat Apr 02, 2022 3:20 am

Akshai,

ChibiOS only zeroes the ".bss*" and ".ramX_clear*" sections on startup. Try putting the array in the ".ram0_clear" section. See rules_memory.ld next to your linker script, and crt1.c a few levels up for how that works.

Ram0 is full due to the rest of the free memory being stolen for the heap (last rule in rules_memory.ld). I recall changing CH_CFG_MEMCORE_SIZE allowed you to see the true used size, but it isn't doing that for me right now (optimization / discard fail?).

The ram0 vs ram1+ram2 thing is due to the linker printing the size of things it put exactly in that section. It doesn't double count the used size for overlapping regions.

Patrick

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Global struct/array not initalized to zero

Postby Giovanni » Sat Apr 02, 2022 4:21 am

Hi,

Psyco is correct, all the RAM not used by the application is allocated to the default heap, allocators use that area:

- Core allocator.
- Heap allocator.
- C runtime allocator (malloc, free).

Also the suggestion about the clear regions is on spot.

Giovanni

akshaim
Posts: 20
Joined: Tue Sep 21, 2021 9:47 am
Has thanked: 9 times

Re: Global struct/array not initalized to zero

Postby akshaim » Mon Apr 04, 2022 1:36 pm

Thanks, Patrick and Giovanni.

Change from `DATA_RAM` to `.ram0_clear` did the trick :)

Also, I tried changing the `CH_CFG_MEMCORE_SIZE`, but still, as Patrick already pointed out the `ramo` utilization stays at 100%. Any suggestion on how to make it print the right amount being used?

Regards,

Akshai M

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Global struct/array not initalized to zero

Postby Giovanni » Mon Apr 04, 2022 3:47 pm

Hi,

The best way is to look into the .map file, all real values are in there.

Giovanni

akshaim
Posts: 20
Joined: Tue Sep 21, 2021 9:47 am
Has thanked: 9 times

Re: Global struct/array not initalized to zero

Postby akshaim » Mon Apr 04, 2022 4:30 pm

Thanks, Giovanni :). I shall do that.

Also, do you think it's okay to set up the LD Script to zero sections in `ram0` as well? Does it add any overhead? I expect the startup timing could be stretched a bit. Any particular reason why you did not zero it?

Regards,

Akshai M

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Global struct/array not initalized to zero

Postby Giovanni » Mon Apr 04, 2022 4:48 pm

Hi,

You may want RAM portions to not be cleared at startup (startup time, storing cross-reset data), this is why in only clears/inits specific parts.

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 6 guests