{"id":400,"date":"2009-08-31T12:14:05","date_gmt":"2009-08-31T11:14:05","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=400"},"modified":"2009-08-31T12:14:05","modified_gmt":"2009-08-31T11:14:05","slug":"volatile","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2009\/08\/31\/volatile\/","title":{"rendered":"volatile"},"content":{"rendered":"<p>I have often talked and written about my thoughts on programming languages. In particular the fact that there really is no language that was designed with embedded programming specifically in mind. Historically, there was assembly language, but that is rarely used for large scale coding nowadays. Intel promoted PL\/M many years ago, but that has fallen by the wayside.<\/p>\n<p>C and C++ are the key languages today. Neither was designed for embedded, but they do the job quite well. Ironically, I think that one of the precursors to C, BCPL, would have been well suited to embedded work, but that language has been consigned to the history books.<\/p>\n<p>There is one feature of C\/C++, that is often not well understood, but is a godsend to embedded programmers: the keyword <strong>volatile<\/strong> &#8230;<\/p>\n<p><!--more-->When you declare a variable\/object <strong>volatile<\/strong>, 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.<br \/>\nBroadly speaking, it must be assumed that a <strong>volatile<\/strong> 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.<\/p>\n<p>Although <strong>volatile<\/strong> 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:<\/p>\n<pre style=\"padding-left: 30px\"><strong>volatile int* pi1 = 0;          \/\/ pointer to volatile int\nint* volatile pi2 = 0;          \/\/ volatile pointer to int\nvolatile int* volatile pi3 = 0; \/\/ volatile pointer to volatile int<\/strong><\/pre>\n<p>Plenty of room for errors there! [The same syntactic issues arise with <strong>const<\/strong>.]<\/p>\n<p>However, just declaring a variable <strong>volatile<\/strong> 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:<\/p>\n<pre style=\"padding-left: 30px\"><strong>volatile x = 1;\n...\nx++;<\/strong><\/pre>\n<p>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?<\/p>\n<p>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.<\/p>\n<p>[With acknowledgement to my friend Meador Inge, upon whom I can always rely when I get puzzled about compilers, languages etc.]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have often talked and written about my thoughts on programming languages. In particular the fact that there really is&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,300,309],"industry":[],"product":[],"coauthors":[],"class_list":["post-400","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-embedded-software","tag-rtos"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=400"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/400\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=400"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=400"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=400"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}