{"id":11176,"date":"2022-09-26T10:35:19","date_gmt":"2022-09-26T05:05:19","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/"},"modified":"2022-09-26T10:35:19","modified_gmt":"2022-09-26T05:05:19","slug":"solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/","title":{"rendered":"[Solved] volatile keyword usage in ISR function in micro-controller programing"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-41504402\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"41504402\" data-parentid=\"41502926\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Of course you do.<br \/><code>volatile<\/code> isn&#8217;t a prerogative of an ISR, it has a specific definition in the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n1256.pdf\">C11 standard<\/a>:<\/p>\n<blockquote>\n<p>An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects.<br \/>\n  Therefore any expression referring<br \/>\n  to such an object shall be evaluated strictly according to the rules of the abstract machine,<br \/>\n  as described in 5.1.2.3. <\/p>\n<p>Furthermore, at every sequence point the value last stored in the<br \/>\n  object shall agree with that prescribed by the abstract machine, except as modified by the<br \/>\n  unknown factors mentioned previously.<br \/>\n  What constitutes an access to an object that<br \/>\n  has volatile-qualified type is implementation-defined.<\/p>\n<\/blockquote>\n<p>So whenever you control flows in a manner that cannot be deduced from the sources (like when an interrupt occurs), the compiler cannot know that a variable may have changed meanwhile.<br \/>\nYou have to use <code>volatile<\/code> to tell it that such variable is subject to change at any moment.  <\/p>\n<hr>\n<p>If that&#8217;s too abstract,<br \/>\nconsider this toy code for the AVR microcontroller:<\/p>\n<pre><code>unsigned char STATUS;\n\nvoid ISR_SUB()\n{\n  STATUS = 0x80;\n}\n\nvoid ISR ()\n{\n  ISR_SUB();\n}\n\nint main()\n{\n  unsigned char i=1;\n\n  while (STATUS &amp; 0x80)\n  {\n    STATUS |= i;\n  }\n\n  return 0;\n}\n<\/code><\/pre>\n<p>This <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/godbolt.org\/g\/HL4hKE\">gets compiled into<\/a> this assembly code <\/p>\n<pre><code>main:\n        lds r24,STATUS             ;r24 = STATUS \n        sbrs r24,7                 ;Skip next inst if bit7 of r27 is set\n        rjmp .L4                   ;Jump to the end\n.L6:\n        ori r24,lo8(1)             ;OR r24 with 1\n        sbrc r24,7                 ;Do the test again, break loop if bit7 set\n        rjmp .L6                   ;Jump back to the loop\n\n        sts STATUS,r24             ;STATUS = r24\n.L4:\n        ldi r24,lo8(0)\n        ldi r25,hi8(0)\n        ret\n<\/code><\/pre>\n<p>As you can see, the variable <code>STATUS<\/code> is read once and updated in the register <code>r24<\/code>, so the loop will never end!<br \/>\nNow look at <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/godbolt.org\/g\/RIhtJp\">what happens when we use <code>volatile<\/code><\/a><\/p>\n<pre><code>main:\n        rjmp .L8\n.L6:\n        lds r24,STATUS           ;Now status is load in r24 at each iteration ...\n        ori r24,lo8(1)           ;... updated and ...\n        sts STATUS,r24           ;... stored back\n.L8:\n        lds r24,STATUS\n        sbrc r24,7\n        rjmp .L6\n        ldi r24,lo8(0)\n        ldi r25,hi8(0)\n        ret\n<\/code><\/pre>\n<p>This time <code>STATUS<\/code> is read and updated at every iteration, as requested.  <\/p>\n<p><strong>Note on synchronisation<\/strong>  <\/p>\n<p>Many thanks to @Olaf for pointing out the necessity of this section.  <\/p>\n<p>I made no claims above that <code>volatile<\/code> is a <strong>sufficient<\/strong> condition for whatever the OP is trying to implement (there is simply not enough context to made any claim).<br \/>\nThe way this answer is to be interpreted is that <code>volatile<\/code> is a <strong>necessary<\/strong> condition (as the simple counter-example above shows).<\/p>\n<p>The code shown, as said, is a <em>toy example<\/em> meant to show a simple issue that can arise without <code>volatile<\/code>.<br \/>\nIt is not meant to be working code because actually I will <strong>not<\/strong> work.<\/p>\n<p>When dealing with concurrent flows of execution <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Synchronization_(computer_science)\">synchronisation<\/a> is mandatory and for C this can be achieved using the <code>stdatomic.h<\/code> header and functions. <\/p>\n<p>Being this a uC question, <code>stdatomic.h<\/code> may not be present or no synchronisation may be required (this is rare).<br \/>\nJust to avoid any misunderstanding: if you have <code>stdatomic.h<\/code> then <em>use it<\/em> (it will eventually compile to nothing but it made the code portable).<\/p>\n<p>The example above contains a RMW operation (the <code>|=<\/code>) that is not atomic can thus cancel the update made by the ISR.<\/p>\n<hr>\n<p>So yes, you do need (at least) <code>volatile<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">16<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved volatile keyword usage in ISR function in micro-controller programing <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Of course you do.volatile isn&#8217;t a prerogative of an ISR, it has a specific definition in the C11 standard: An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to &#8230; <a title=\"[Solved] volatile keyword usage in ISR function in micro-controller programing\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/\" aria-label=\"More on [Solved] volatile keyword usage in ISR function in micro-controller programing\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,2849,3067],"class_list":["post-11176","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-embedded","tag-microcontroller"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] volatile keyword usage in ISR function in micro-controller programing - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] volatile keyword usage in ISR function in micro-controller programing - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Of course you do.volatile isn&#8217;t a prerogative of an ISR, it has a specific definition in the C11 standard: An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-26T05:05:19+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] volatile keyword usage in ISR function in micro-controller programing\",\"datePublished\":\"2022-09-26T05:05:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/\"},\"wordCount\":423,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"embedded\",\"microcontroller\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/\",\"name\":\"[Solved] volatile keyword usage in ISR function in micro-controller programing - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-26T05:05:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] volatile keyword usage in ISR function in micro-controller programing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] volatile keyword usage in ISR function in micro-controller programing - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] volatile keyword usage in ISR function in micro-controller programing - JassWeb","og_description":"[ad_1] Of course you do.volatile isn&#8217;t a prerogative of an ISR, it has a specific definition in the C11 standard: An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/","og_site_name":"JassWeb","article_published_time":"2022-09-26T05:05:19+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] volatile keyword usage in ISR function in micro-controller programing","datePublished":"2022-09-26T05:05:19+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/"},"wordCount":423,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","embedded","microcontroller"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/","url":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/","name":"[Solved] volatile keyword usage in ISR function in micro-controller programing - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-26T05:05:19+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-volatile-keyword-usage-in-isr-function-in-micro-controller-programing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] volatile keyword usage in ISR function in micro-controller programing"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=11176"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11176\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=11176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=11176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=11176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}