question about static and volatile

natanel wrote on Sunday, 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

rtel wrote on Monday, 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.

natanel wrote on Monday, 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

sashiono wrote on Tuesday, 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.

natanel wrote on Wednesday, 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:

natanel wrote on Wednesday, 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