Partially volatile variable?

DarthRubik

Suppose I have a micro controller with a main loop and 1 millisecond interrupt (if you don't know what that is it is just a task that interrupts the main loop's execution, while it does something.....and it is a 1 millisecond interrupt, because it happens every millisecond).

I have a variable that I use to communicate between the main loop and the millisecond interrupt:

volatile status_t Status;

Right now I have a section of code in the main loop that updates the Status variable, which does a ton of transformations on it:

cli();    // This temporarily turns off interrupts, so we don't 
          // modify the variable unsafely
Status.UpdateStuff();
Status.UpdateOtherStuff();
//etc.

sei();    // Turn interrupts back on

The problem is that each of these function calls to Status rewrites Status......the compiler can not cache the values of Status in local memory.

One possible solution to this problem is this:

cli();
status_t* localStatus = (status_t*)&Status;
localStatus->UpdateStuff();
localStatus->UpdateOtherStuff();
//etc.

Status = *localStatus;
sei();

The real question here is this:

Is this going to do what I hope it will do or is there a better way to get around the issue of constant refreshing the variable, instead of allowing the optimizer to cache the variable?

Barmar

Your second version might still write to the microcontroller multiple times, because the compiler may not realize it can cache the value across method calls (it could probably only determine this if the methods are inline). So I suggest making an explicit local copy, not just a local pointer.

cli();
status_t localStatus = Status;
localStatus.UpdateStuff();
localStatus.UpdateOtherStuff();
...
Status = localStatus;
sei();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

java - synchronization and volatile variable

From Dev

Volatile variable explanation in Java docs

From Dev

Is there anyway to make a variable "partially protected"?

From Dev

Order of const and volatile for a variable

From Dev

Java Volatile Variable

From Dev

Variable freshness guarantee in .NET (volatile vs. volatile read)

From Dev

How can an auto variable be volatile?

From Dev

Can a reordering happen in the presence of a volatile variable?

From Dev

Access an Array whose name is partially a variable

From Dev

Understanding volatile variable by example

From Dev

Combination of Singleton class and volatile variable

From Dev

Incrementing a volatile variable in C

From Dev

Volatile variable and non volatile reordering / visibility

From Dev

Force read of volatile variable

From Dev

Java volatile variable multithreading behavior

From Dev

When not to use volatile variable in Java

From Dev

Casting volatile variable in c

From Dev

volatile variable is not giving expected output

From Dev

Volatile variable explanation in Java docs

From Dev

Is there anyway to make a variable "partially protected"?

From Dev

How to escape partially a template variable in Django

From Dev

Access an Array whose name is partially a variable

From Dev

Partially update a session variable in meteor?

From Dev

Why is my environment variable is partially set?

From Dev

Volatile variable and non volatile reordering / visibility

From Dev

When not to use volatile variable in Java

From Dev

Partially volatile variable?

From Dev

arduino thread update volatile variable

From Dev

Should a pointer to stack variable be volatile?

Related Related

HotTag

Archive