Global Variable Overwrite Problem

Learn MoreFAQsTutorials

4 posts / 0 new
Last post
mahmed106
Offline
Last seen:1 month 2 weeks ago
加入:2019-05-03 17:28
Global Variable Overwrite Problem

Hi Dialog

We are working on custom board based on DA14681, running SDK 1.0.14.

Problem is that in my code that is based on PxpReporter, i m doing some specific BLE task at high speed. I perform a high speed data transfer over the BLE. Right after that a global variable in my code, gets a garbage value.

After some research, i found out that using global variables in C++, is not a good practice, as it can be accessed and overwrite from anywhere. Then i converted that global variable from uint8_t to INITIALISED_PRIVILEGED_DATA uint8_t and then it worked fine.

So i have two questions:

1 - INITIALISED_PRIVILEGED_DATA有年代ome kind of protected memory? If yes then should i convert all my global varibles to INITIALISED_PRIVILEGED_DATA?

2 - Is using global variables a good practice or a bad practice? From dialog DA14681's programming point of view?

Device:
PM_Dialog
Offline
Last seen:14 hours 35 min ago
Staff
加入:2018-02-08 11:03
Hi mahmed106,

Hi mahmed106,

Please take a look at the sections.ld from any of SDK examples (for instance the ble_adv example) and search for the RetRAM0 section. The privileged_data_init is stored into the Retention-RAM. Additionally all the stuff here goes to the retained RAM:

RETENTION_INIT0 collects all the code and the (non-zero-initialized) data that should be placed in the retained RAM.

RETENTION_RAM0 collects all the zero-initialized data that should be placed in the retained RAM.

RETENTION_BLE is for the hardcoded data area used by the BLE ROM code. Please don’t put anything else there.

So, in order to stare a variable into the retained section of the RAM you should use one of the following sections

retention_mem_rw - privileged_data_rw - privileged_data_zi - retention_mem_zi

The macros in the portmacro.h file can be used to place the variables in the desired section.

When the INITIALISED_PRIVILEGED_DATA is used, then the variable will be stored into privileged_data_init which is an area of the Ret-RAM.

If you are using any of the sleep modes, when the device wakes up and if the global variable has not been stored into the Ret-RAM, its value will not be retained.

Regarding your second question, is application specific and depends on what you are trying to do.

Thanks, PM_Dialog

mahmed106
Offline
Last seen:1 month 2 weeks ago
加入:2019-05-03 17:28
Yes i understand that ret ram

Yes i understand that ret ram helps when we wakeup from sleep, but my question is that does compiler's optimizations are done on INITIALISED_PRIVILEGED_DATA or not?

Because just by changing a normal global variable to INITIALISED_PRIVILEGED_DATA , its didnt got garbage,,, makes me think that this memory can be protected from overwrite in some sense.

PM_Dialog
Offline
Last seen:14 hours 35 min ago
Staff
加入:2018-02-08 11:03
Hi mahmed106,

Hi mahmed106,

I am not able to understand how the compiler is related to this. The linker scrips set the area into the RAM/FLASH that is retained or not and which variables/code should be retained or nor and what is their value. The start-up file will store the variables/code in that areas.

In general, the “PRIVILEGED” is not the best way of use to store variables into the retention areas as it is when the system has MPU (Memory Protection Unit).

In order to store variables/code is recommended to use the __RETAINED_XXX from the sdk_defs.h header file – please see lines 190 – 218. The variables to be set as __RETAINED_XXX should be global variables – the variable within a task are stored into OS_HEAP which is already retained.

Thanks, PM_Dialog