# Global variable and freetos

**URL:** <https://forums.freertos.org/t/global-variable-and-freetos/17221>\
**Category:** Kernel\
**Tags:** debug, monitoring\
**Created:** [April 14, 2023, 11:19am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221 "2023-04-14T11:19:20Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aline](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Aline](https://forums.freertos.org/u/Aline)\
**Post date:** [April 14, 2023, 11:19am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/1 "2023-04-14T11:19:21Z")

</div>

Hello everyone,  
I am new to freetos development. By the way, I have in my program several cyclic tasks that run and that all use the same global variables. Now I would like to create in addition another task independent of the others, that is, a task which uses its own global variables: A task which could not have the global variables of the other tasks and the other tasks could not have access to its own .  
I would like to know at the level of the memory how it is organized?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [April 14, 2023, 11:31am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/2 "2023-04-14T11:31:14Z")

</div>

Hey @Aline,

I have couple of questions here:  
What do you mean by cyclic tasks here? Do you mean periodic tasks that run at a specific interval?  
Is the shared global variables accessed in critical sections?  
It would be great if you could elaborate more on what you are trying to achieve here.

Global variables can be accessed across the application, unless they are static global variables which have there linkage limited to the files (translation units) in which they are defined. They are not scoped/linked to any particular tasks.

---

<div class="post-metadata">

**Author:** ![Aline](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Aline](https://forums.freertos.org/u/Aline)\
**Post date:** [April 19, 2023, 8:11am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/3 "2023-04-19T08:11:48Z")

</div>

> [@tony-josi-aws](#):
>
> I have couple of questions here:  
> What do you mean by cyclic tasks here? Do you mean periodic tasks that run at a specific interval?  
> Is the shared global variables accessed in critical sections?  
> It would be great if you could elaborate more on what you are trying to achieve here.
> 
> Global variables can be accessed across the application, unless they are static global variables which have there linkage limited to the files (translation units) in which they are defined. They are not scoped/linked to any particular tasks.

Thank your for your replay /

I have couple of questions here:  
What do you mean by cyclic tasks here? Do you mean periodic tasks that run at a specific interval?  
Oui je veux dire les fonctions crées par  
response = xTask\_A\_CreateRestricted(XX, XX);  
Basically what I want to do is:

```auto
////////////////////////////////////////////////
//Variable Globale

int BEDPosition;// Variable for Task A
Int TTLPosition;//Variable for Task B

static const Task_A_Seeting_t repetifTask_A_Seeting = {
	  repetifTask_A,
	  repetifTask_A_Name,
      K_repetifTask_A_StackSize,
      NULL,
      K_repetifTask_A_Priority,
      NULL,
      // Regions
      {
        { (UINT32) &_RAM_soft_Start_Address__,
          (UINT32) &_RAM_soft_End_Address__,
          portMPU_REGION_R_EXECUTE
        },
        { 0,
          0,
          0
        },
        { (UINT32) &_ROM_ConfigurationStartAdr__,
          (UINT32) &_ROM_ConfigurationEndAdr__,
          portMPU_REGION_READ_WRITE
        },
      }
};

________________________________
//Création de l task A
response = xTask_A_CreateRestricted(&repetifTask_A_Seeting, &repetifTask_A_Hdl);
//Création de la task B
response = xTask_B_CreateRestricted(&repetifTask_B_Seeting, &repetifTask_B_Hdl);
______________________

```

Now I want task A to be unable to access the global variable TTLPosition only access BEDPosition and task B also to be unable to access the global variable BEDPosition only TTLPosition.  
It’s for cyber reasons I want to create an independent task that could not communicate with other tasks, but since it shares the variables with the others, I wanted to protect the variables or do such so that the others cannot see his global variables and he could not see those of others

thank you in advance for your help

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [April 19, 2023, 10:28am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/4 "2023-04-19T10:28:25Z")

</div>

I’m assuming the target device you are using supports MPU as the target was not specified in the question.

If that’s the case you can specify the memory regions (start address [`pvBaseAddress`] and length [`ulLengthInBytes`]) of any non-stack variables (`TTLPosition` and `BEDPosition` in your example) you need restricted access in the `xRegions` field of each task’s with privilege levels (`ulParameters`) as per each task’s requirement.

You can refer to the MPU demos ([example - CORTEX\_MPU\_STM32L4\_Discovery\_GCC\_IAR\_Keil](https://github.com/FreeRTOS/FreeRTOS/blob/main/FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil/Demo/mpu_demo.c)) in the [FreeRTOS/FreeRTOS/Demo](https://github.com/FreeRTOS/FreeRTOS/tree/main/FreeRTOS/Demo) folder for more information and detailed examples.

More details: [FreeRTOS-MPU](https://www.freertos.org/MPU_Chapter.pdf)

---

<div class="post-metadata">

**Author:** ![Aline](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Aline](https://forums.freertos.org/u/Aline)\
**Post date:** [April 19, 2023, 12:05pm UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/5 "2023-04-19T12:05:36Z")

</div>

Thank you tony-josi-aws for your answer, I understand better, but I have another question.  
So by saying that I can restrict memory access for each task, now I want to know where the global variables (beginning address and end address) are stored in the memory so I could divide this area into several areas with the xTaskCreateRestricted() function.  
Any idea where they might be stored in memory?  
The cyber used is the Renesas RX66 microcontroller?

thanks again  
Cordially,

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [April 19, 2023, 1:30pm UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/6 "2023-04-19T13:30:49Z")

</div>

You will need to use compiler specific attributes on the variables to force them into specific blocks of memory, and setup the linker to place those at appropriate addresses.

For gcc, it is typically something like adding an

```auto
__attribute__ ((section(“sectname”)))

```

Which will place the variable that attribute is attached to into linker section “sectname”, which you will need to define in the linker scripts to be a MPU page aligned block of memory.

---

<div class="post-metadata">

**Author:** ![Aline](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Aline](https://forums.freertos.org/u/Aline)\
**Post date:** [April 19, 2023, 2:34pm UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/7 "2023-04-19T14:34:16Z")

</div>

OK,  
Thank you richard-damon

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [April 19, 2023, 4:29pm UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/8 "2023-04-19T16:29:07Z")

</div>

The only other thing that I want to add here is that you need to ensure that every memory region that you are granting a task access to satisfies hardware requirements such as size and alignment. For example, Cortex-M33 requires that minimum MPU region size is 32 bytes and every region is 32 bytes aligned. As a result, you cannot grant access to only one integer on a Cortex-M33. See [this example](https://github.com/FreeRTOS/FreeRTOS/blob/main/FreeRTOS/Demo/Common/ARMv8M/mpu_demo/mpu_demo.c#L63) how we allocate 32 bytes even though we use only one so that we control access to this region.

---

<div class="post-metadata">

**Author:** ![Aline](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Aline](https://forums.freertos.org/u/Aline)\
**Post date:** [April 26, 2023, 12:53pm UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/9 "2023-04-26T12:53:27Z")

</div>

Hello, I allow myself to come back to you, because after having understood and created the task with its isolated global variables, I would also like to know if technically with freetos it is possible to create a queue for task A and another queue for task B and do so that task A cannot write to or read from queue A and task B either cannot write to or read from queue A.

That is to say if Task A wants to communicate with Task B, it has to go through a function that I am going to create otherwise it could not.  
here is my idea,

Thanks in advance

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [April 26, 2023, 1:49pm UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/10 "2023-04-26T13:49:19Z")

</div>

You can put the handle for the queues a file-local scope so only the functions in the file with the select functions you want to be able to do the communication can access the queue.

This can NOT be done by the MPU, as the function, as far as the MPU is concerned, is part of the task, so can’t see the difference between them. This is true unless you implement the function like FreeRTOS implements the actual Queue code with a security gate trampoline between an unprotected call into a privileged function from restricted addresses.

---

<div class="post-metadata">

**Author:** ![Aline](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Aline](https://forums.freertos.org/u/Aline)\
**Post date:** [May 16, 2023, 8:04am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/12 "2023-05-16T08:04:42Z")

</div>

Hello everyone here is the code I developed to isolate tasks  
Here is my code :  
_In my link editor configuration file (.icf)_  
**Step 1 :**  
I defined my memory areas c :

```auto
//Sal1 memory
define exported symbol __RAM_Sal2_Start_Address__ = 0x0003FFF0;
define exported symbol __RAM_Sal2_End_Address__ = 0x0003FFFB;
//Sal2
define exported symbol __RAM_Sal1_Start_Address__ = 0x0003FFFC;
define exported symbol __RAM_Sal1_End_Address__ = 0x0003FFFF;

```

**Step 2**  
I added in my linker configuration file (.icf) the following lines to place all data in the memory address range specified in the :MY\_DATASal1 and :MY\_DATASal2 sections

```auto
//Place in :MY_DATASal1
define region DATA_region = mem:[from __RAM_Sal1_Start_Address__ to __RAM_Sal1_End_Address__];
place in DATA_region { readwrite section MY_DATASal1 };
//Place in :MY_DATASal2
define region DATA_region = mem:[from __RAM_Sal2_Start_Address__ to __RAM_Sal2_End_Address__];
place in DATA_region { readwrite section MY_DATASal2 };

```

**Step 3**  
_- In my Tasksal1.c file, I added_

```auto
#pragma default_variable_attributes = @ "MY_DATASAL1
int Data_Sal1;
#pragma default_variable_attributes =

```

_- In my Tasksal2.c file_

```auto
#pragma default_variable_attributes = @ "MY_DATASal1"
 int data1Sal2;
 int data2Sal2;
#pragma default_variable_attributes =

```

**Step 4 :**  
In my tasksal2.c I created a task :

```auto
static const TaskParameters_t TaskSal2Parameters = {
                                                      (TaskFunction_t) TaskSal2,
                                                      TaskSal2Name,
                                                      K_TaskSal2StackSize,
                                                      NULL,
                                                      K_TaskSal2Priority,
                                                      NULL,
                                                      // xRegions - Allocate up to three separate memory regions for access by
                                                      // the task, with appropriate access permissions. Different processors have
                                                      // different memory alignment requirements - refer to the FreeRTOS documentation
                                                      // for full information.
                                                      {
                                                        // access to the Ram code for the Task
                                                        {
                                                          (UINT32) &_RAM_Sal1_Start_Address__,
                                                          (UINT32) &_RAM_Sal1_End_Address__,
                                                          portMPU_REGION_NO_ACCESS },
                                                        // access to the FCU registers for the Task

```

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [May 16, 2023, 8:53am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/13 "2023-05-16T08:53:13Z")

</div>

Some observations -

- Both the memory regions in ICF file are called `DATA_region` - you should give them different names.
- You are using `portMPU_REGION_NO_ACCESS` - by default an unprivileged task does not have access to any memory region other than its own stack, so you do not need to explicitly define no access.

Which hardware are you using? As I mentioned in my previous response, you need to ensure that MPU regions satisfy the hardware requirements. Currently your MPU regions are too small (12 bytes and 4 bytes respectively) and most likely do not meet the hardware requirements.

---

<div class="post-metadata">

**Author:** ![Aline](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Aline](https://forums.freertos.org/u/Aline)\
**Post date:** [May 16, 2023, 9:47am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/14 "2023-05-16T09:47:13Z")

</div>

> [@aggarg](#):
>
> Vous utilisez - par défaut, une tâche non privilégiée n’a accès à aucune région mémoire autre que sa propre pile, vous n’avez donc pas besoin de définir explicitement aucun accès.`portMPU_REGION_NO_ACCESS`

Hello,  
Thank you for your answer,  
By hardware used Do you mean microcontroller ?:  
I am using a Ranesas RX63 microcontroller.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [May 16, 2023, 9:48am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/15 "2023-05-16T09:48:54Z")

</div>

> [@Aline](#):
>
> By hardware used Do you mean microcontroller ?:  
> I am using a Ranesas RX63 microcontroller.

Yes, I mean microcontroller. Please check the documentation that it allows creation of MPU regions of that size. Other than that, your solution looks good.

---

<div class="post-metadata">

**Author:** ![Aline](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Aline](https://forums.freertos.org/u/Aline)\
**Post date:** [May 16, 2023, 9:52am UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/16 "2023-05-16T09:52:21Z")

</div>

Ok Gaurav aggarwal, It works, I’ll look at the documentation. Thank you very much for your precious help

---

<div class="post-metadata">

**Author:** ![Aline](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@Aline](https://forums.freertos.org/u/Aline)\
**Post date:** [May 16, 2023, 12:38pm UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/17 "2023-05-16T12:38:20Z")

</div>

I read the documatation, I made the changes it doesn’t work, I have the impression that the variables are not in the right section  
I used:  
#pragma default\_variable\_attributes = @ "MY\_DATASAL1  
int Data\_Sal1;  
but I have also seen other uses:  
#pragma section = “MY\_DATASAL1”  
int Data\_Sal1;  
I tell you that I don’t understand the difference and which one to use

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [May 16, 2023, 2:53pm UTC](https://forums.freertos.org/t/global-variable-and-freetos/17221/18 "2023-05-16T14:53:52Z")

</div>

Try the following:

```auto
 int Data_Sal1 __attribute__ ( ( section( "MY_DATASAL1" ) ) );

```

And you can look at the map file to see which variable is going where.
