Monday, July 26, 2010

About 'Const Volatile' variables in C


Since last few days, i was collecting some information about 'const volatile' variables. Because not every C programmer use this. Only few of low level Embedded Software Developers use these 'const volatile' veriables in their proffession.

Even though am a Embedded Software developer, still i didnt get chance to use 'volatile', 'const volatile' type of variables. Because, normally people who write low level programs like device drivers, firmwares, use these variables. If our programs are going to communicate directly with the hardware then we use these type of variables.

In our Embedded domain, people work in application level, middleware layer and lower layers like Firmware/Driver. But only few of us will get opportunity to use unique features of C language. When we have have constraints like memory, CPU cycles, code size, then efficiency and optimization come into picture and we will get chance to learn more in depth (not breadth). Thats what my blog's title says.

Regarding 'const volatile' variable:

This is possible and mostly used in embedded system.The example is Interrupt Status Register. As it is a status register, in the program we should not modify this variable. So it should be a constant. But this variable can be changed by the processor or hardware based on the interrupt condition. So when in the program, we want to read the value of this variable, it should read the actual value with out any optimisation. For this reason ,the variable can be declared as volatile too.

volatile int * const port_add = 0x400;

Yes we can have constant volatile variable in cases where you are using a variable value of which wont changed by your program but may change asynchronously because of some external event.

Const volatile variable shall be used in addressing the fixed memory where the address is constant and the value changes due to external events. For ex: when you want to read the data of any hardware port the address is constant and the value keeps on changing based on the external events on the port. In case of reading a hardware status register, const prevents the value from being stomped on, while volatile tells the compiler that this value can be changed at any time external to the program.

I have already given some information about the volatile variable in one of mu previous post. Please refer the same for volatile variables. I always pray to god as please give me chance to work on projects where i can learn many features of C and also, with people who worked more than 5 years in C language itself. Then only i can learn more and more.

Till now, i have worked with people who were masters in assembly language and less experience in C language. Hence when working my decisions were the final decisions. When we work with people who technically strong than us, then we can learn more. Our technical competency increases.

Dont forget to drop a mail regarding my mistakes and your suggestions...

Gud luck...