First some resources from the web,
The colin walls blog (http://blogs.mentor.com/colinwalls/blog/2009/08/31/volatile/),
There is one feature of C/C++, that is often not well understood, but is a godsend to embedded programmers: the keyword volatile …
When you declare a variable/object volatile, you are telling the compiler not to optimize access to the data. When your code writes a value to the variable, it should be written straight away and not saved in a register for use later. Likewise, when the code reads the value, it should not use a copy that was obtained earlier.
Broadly speaking, it must be assumed that a volatile variable can change at any time, independently of the current code. This implies two uses: variables that are shared between execution threads [between tasks in an RTOS or between the mainline code and an interrupt service routine]; I/O device registers.
Although volatile is a useful language facility, it is not without difficulties. Firstly, some research has shown that many compilers do not implement the keyword correctly in all circumstances. So care and attention to generated code is required. There are also the usual troubles with C syntax; consider these:
volatile int* pi1 = 0; // pointer to volatile int
int* volatile pi2 = 0; // volatile pointer to int
volatile int* volatile pi3 = 0; // volatile pointer to volatile int
Plenty of room for errors there! [The same syntactic issues arise with const.]
However, just declaring a variable volatile is not enough if you are sharing it between execution threads [even if there are texts that suggest that this is the case]. To illustrate the problem, consider this code:
volatile x = 1;
...
x++;
Syntactically this is fine, but what about the generated code? If the CPU instruction set allows a memory location to be incremented directly with a single, non-interruptible instruction, there is no problem [so long as the compiler uses those instructions!]. But many devices would require the data to be read, incremented and written back. This is all fine according to the language definition, but what if an interrupt occurs during this sequence of instructions?
You need to protect access to it so that the memory location always contains a valid value. You could do this by disabling and re-enabling interrupts or, with an RTOS, maybe a semaphore would be the answer.
I had to re-read this multiple times to understand what exactly it means. So when we define a variable volatile,
-> We are telling compiler to do always read that variable from memory, it should not do any optimization like storing in a register or optimize the code assuming it being a deadcode.
-> volatile is not storage specifier, i.e. it doesn't specify where the variable would be stored.
-> volatile can not control the way the caching is done in the system. Caching is controlled by the way we define in the pages in page table during linking stage.
Thank you for the info. It sounds pretty user friendly. I guess I’ll pick one up for fun.
ReplyDeleteEmbedded Systems Training
This is a very great and informational posting about embedded course and training. Thanks for sharing it with us. Keep it on…
ReplyDeleteEmbedded Engineering Course
This is a nice information about languages and courses are also nice. Its a great help for learners.
ReplyDeleteReally helpful in my fieldwork.I liked the way you explain this volatile variable.
ReplyDeleteNice article,thanks for sharing this information.Looking forward for more posts like this.
ReplyDeleteEmbedded Systems development
We offers Industrial Automation Solution, Jobs for Electrical Electronics Fresher, Major/Minor Project Electrical and Electronics, Internship program for Electronics Engineer, Internship program for Electrical Engineer, Jobs for Electronics student in Jaipur, Training for Electronics student in Jaipur, summer training in Jaipur.Industrial Automation Solution | Jobs for Electrical Electronics Fresher | Major/Minor Project Electrical and Electronics
ReplyDeleteJoin Our new batch for Embedded System Training In Jaipur
ReplyDeleteGet Details on http://issjaipur.com/Training/PHP_training.aspx
Here are the best interview questions for embedded systems jobs. to view them click here.
ReplyDeleteOur Embedded Training Institute Provides 100 % Training & Placements. Embedded (8051, PIC, AVR, RTOS).Projects also Provides in Best & affordable fees Structure.Call @ +91-9940426826 / 044-26209369.
ReplyDeleteBest Embedded Systems Training Institute
I am very interested for this blog.Its very useful for me.
ReplyDeleteadvanced embedded systems training
This comment has been removed by the author.
ReplyDeleteYour design of the blog is really eye-catching. More over the content is also very productive. Information you have provided is really very beneficial.AVR Training
ReplyDeleteI appreciate the amazing job you had done, it saved the time of searching for each of the videos separately embedded systems training institute | embedded systems training in bangalore
ReplyDeleteVERY INFORMATIVE AND USEFUL ....VERY NICE.Mainframes Training In Hyderabad
ReplyDeleteThe content on the recent technology is very useful. Software Testing is indispensable to provide a quality product without any bugs or issues.
ReplyDeleteSoftware testing selenium training in Chennai
Nice Post and very useful and helpful your posting
ReplyDeleteFind Embedded system diploma in Egypt. Maklabacademy.com provides a comprehensive introduction to the embedded system courses. Call now: 01061633336
Embedded System Diploma in Egypt
Interesting and informative content is discussed here.
ReplyDeleteLinux Training in Chennai
thanks For Sharing. Phoenix Binary System Is Thanks for sharing great post. I really like this post.
ReplyDeleteWeb Design Company in Anand
Web designer company Surat This company is provided a Best IT Services In Gujarat India.
Thanks so much for such a nice and proper explanation
ReplyDeleteSharma Academy consists of the best minds from academics. Thhis is top quality coaching institutes of Central India. The institute has achieved status of distinction following the path envisioned by its founder. The institute has been very successful in making potential aspirants realize their dreams which is evident from the success stories of the previous years. It is Perfect Coaching for MPPSC in Indore. Providing Best MPPSC Coaching in Indore, MPPSC Notes, MPPSC Syllabus, MPPSC Online Coaching Available.
ReplyDeletetütün sarma makinesi
ReplyDeletesite kurma
sms onay
binance hesap açma
1ZKAMG