Page 1 of 1

Global struct/array not initalized to zero

Posted: Fri Apr 01, 2022 9:45 pm
by akshaim
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

Re: Global struct/array not initalized to zero

Posted: Sat Apr 02, 2022 3:20 am
by psyco
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

Re: Global struct/array not initalized to zero

Posted: Sat Apr 02, 2022 4:21 am
by Giovanni
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

Re: Global struct/array not initalized to zero

Posted: Mon Apr 04, 2022 1:36 pm
by akshaim
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

Re: Global struct/array not initalized to zero

Posted: Mon Apr 04, 2022 3:47 pm
by Giovanni
Hi,

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

Giovanni

Re: Global struct/array not initalized to zero

Posted: Mon Apr 04, 2022 4:30 pm
by akshaim
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

Re: Global struct/array not initalized to zero

Posted: Mon Apr 04, 2022 4:48 pm
by Giovanni
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