Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

question about static and volatile

Posted by natanel on August 12, 2007
Hi

I just started using freeRTOS and I have few questions

1) if I want to keep the value of an array between task switch
that the array will have the same values after task return

what is the best way to do it
I am using "static" right now, is it ok?

2) I am almost sure that those values are not pushed to the stack (array can be very big)
so were exactly they are kept between calls?

3) reading examples I found that parameters sent to task use memory allocation
when does it is exactly needed? way?
for example telling task to ran with delay of N msec.
do it need to be used by parameters sending with memory allocation?

do I need to use parameters with memory allocation for solving question number 1 ?

4) finally "volatile". on which variables it should be used
does the array from 1 should be declared as volatile to keep its data between task switch?
does all the variables in a task should be volatile?

thanks

RE: question about static and volatile

Posted by Richard on August 13, 2007
> 1) if I want to keep the value of an array between task switch
> that the array will have the same values after task return
>
> what is the best way to do it
> I am using "static" right now, is it ok?


If the array is declared on the stack then it will automatically have the same value between context switches - but be aware that the stack must be dimensioned to be large enough to hold the array.

If you declare the array static then it will not be on the stack, but will retain its value because it is just a block of memory. Be aware doint this though that the code will not be reentrant. It will only hold its value if no other tasks write to the array.

> 2) I am almost sure that those values are not pushed to the
> stack (array can
> be very big)
> so were exactly they are kept between calls?


Wherever your linker places the array in your RAM. Look at the map generated by the build if you need to know where this is.


> 3) reading examples I found that parameters sent to task use
> memory allocation

Some examples do this, it is not mandatory.


> when does it is exactly needed? way?
> for example telling task to ran with delay of N msec.
> do it need to be used by parameters sending with memory allocation?

You do not need to pass anything in if you don't need to.


> do I need to use parameters with memory allocation for
> solving question number
> 1 ?

No.

>
> 4) finally "volatile". on which variables it should be used
> does the array from 1 should be declared as volatile to keep
> its data between
> task switch?
> does all the variables in a task should be volatile?

The array need not be declared volatile unless values in it are going to be changed in a way that is unexpected by the sequential flow of your program - for example, by another task or by an interrupt. This is just the normal use of the volatile qualifier.

Regards.

RE: question about static and volatile

Posted by natanel on August 13, 2007
Hi

1) may I be more specific

I am using two task
one filling an array (with high priority) from the ADC
and the other read from the array (lower priority)

the writing task must be accurate and put new data every msec
the reading task should read data every msec (if there is a new one)

what will be the right way to handle it in the freeRTOS

right now the array is declared as
Static in the first task
and
extern in the second one

is it ok?

2) I want to learn when the "volatile" is uses
is there any link for examples explain when it should be used and when not.

3) can you advice for a link which give concept of good programming at RTOS
(already read the one in the freeRTOS)

Thankas

RE: question about static and volatile

Posted by sashiono on August 14, 2007
I would use a queue to pass data between tasks, this prevents any data corruption. in addition the read task can block while data is not ready.

look at how the serial code works, you should try to do it in a similar style.

RE: question about static and volatile

Posted by natanel on August 15, 2007
so if I want the writer to keep in the queue only the last updated data
not considering if the reader read the data

how to do it

writer:
check queue is empty
yes -> write data
no -> read to empty queue -> write new data

reader:


RE: question about static and volatile

Posted by natanel on August 15, 2007
sorry

so if I want the writer to keep in the queue only the last updated data
not considering if the reader read the data

how do I do it?

writer:
check if queue is empty
yes -> write data
no -> read in order to empty queue -> write new data

reader:
read. if queue us empty? wait N tick

thanks


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner