<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments for Barr Code</title>
	<atom:link href="http://embeddedgurus.com/barr-code/comments/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/barr-code</link>
	<description>A Blog by Michael Barr</description>
	<lastBuildDate>Tue, 24 Jan 2012 21:58:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>Comment on Coding Standard Rule #4: Use volatile Whenever Possible by Luke Teyssier</title>
		<link>http://embeddedgurus.com/barr-code/2009/03/coding-standard-rule-4-use-volatile-whenever-possible/comment-page-1/#comment-67530</link>
		<dc:creator>Luke Teyssier</dc:creator>
		<pubDate>Tue, 24 Jan 2012 21:58:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/27/coding-standard-rule-4-use-volatile-whenever-possible/#comment-67530</guid>
		<description>I agree wholeheartedly with &quot;The volatile keyword shall be used whenever appropriate&quot;. I don&#039;t agree with &quot;Use volatile Whenever Possible&quot;. Volatile comes with a price. In many compilers it will defeat any attempt at optimization, pipelining, or out of order execution around that variable, and possibly for the entire block or function. It&#039;s important to know _exactly_ when volatile is required and use it _every_ time. If volatile is essential to the function of your program, you also need to spot-check the generated assembly at every optimization setting you are using to ensure that your compiler gets it right. Compiler errors here are more common than anyone would like to admit.

If you suspect a &quot;volatile&quot; problem, many compilers support an option like -fvolatile, which will treat all variables in a module as volatile. This will kill your performance, but at least it can help you track down the problem quickly.

Further, if you are working on a memory-mapped device, you _must_ ensure cache coherency. This is a major cause of race conditions in device drivers.

Thanks for the article.</description>
		<content:encoded><![CDATA[<p>I agree wholeheartedly with &#8220;The volatile keyword shall be used whenever appropriate&#8221;. I don&#8217;t agree with &#8220;Use volatile Whenever Possible&#8221;. Volatile comes with a price. In many compilers it will defeat any attempt at optimization, pipelining, or out of order execution around that variable, and possibly for the entire block or function. It&#8217;s important to know _exactly_ when volatile is required and use it _every_ time. If volatile is essential to the function of your program, you also need to spot-check the generated assembly at every optimization setting you are using to ensure that your compiler gets it right. Compiler errors here are more common than anyone would like to admit.</p>
<p>If you suspect a &#8220;volatile&#8221; problem, many compilers support an option like -fvolatile, which will treat all variables in a module as volatile. This will kill your performance, but at least it can help you track down the problem quickly.</p>
<p>Further, if you are working on a memory-mapped device, you _must_ ensure cache coherency. This is a major cause of race conditions in device drivers.</p>
<p>Thanks for the article.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Combining C&#8217;s volatile and const Keywords by Tim Wescott</title>
		<link>http://embeddedgurus.com/barr-code/2012/01/combining-cs-volatile-and-const-keywords/comment-page-1/#comment-67498</link>
		<dc:creator>Tim Wescott</dc:creator>
		<pubDate>Tue, 24 Jan 2012 18:01:31 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/barr-code/?p=695#comment-67498</guid>
		<description>There are various ways to achieve what Dan does with IAR&#039;s extensions to C, with varying degrees of elegance and portability.

My preferred method is to declare registers as &#039;extern&#039; in the C (or C++) code, then actually define their locations as absolutely-located entities in a linker file or an assembly file.  This method is also tool-dependent, but since there&#039;s almost always a way to achieve this goal it&#039;s fairly safe.  It can be cumbersome if you try to define registers one-by-one, which is why I generally make up a &#039;struct&#039; type for each peripheral (or peripheral type, if the microprocessor designer was kind enough to make duplicate peripherals identical but for offset), then declare entire structures in the C code and in the linker script or assembly.

Another method is to make up header files with appropriate defines:

#define p_led_reg  ((uint8_t *) 0x00080000)

etc.  This gets more complicated with C++ (the compiler will complain about old-style casts, or outright throw an error), and even more complicated yet if your code is a mix of C and C++.  But it&#039;s a method that will work with just about every tool chain that I&#039;ve encountered.

Probably the best way to do this of all is to make sure that you choose a form for addressing your registers that is tool-independent outside of the actual register definitions (header files and possibly linker scripts &amp;c.) and make sure that those register definitions are well encapsulated and easily identifiable so that if you do have to do some mad thing like changing tool chains in mid stream, the surgery will be limited to the pertinent header files, in stead of being splattered over your whole code base.</description>
		<content:encoded><![CDATA[<p>There are various ways to achieve what Dan does with IAR&#8217;s extensions to C, with varying degrees of elegance and portability.</p>
<p>My preferred method is to declare registers as &#8216;extern&#8217; in the C (or C++) code, then actually define their locations as absolutely-located entities in a linker file or an assembly file.  This method is also tool-dependent, but since there&#8217;s almost always a way to achieve this goal it&#8217;s fairly safe.  It can be cumbersome if you try to define registers one-by-one, which is why I generally make up a &#8216;struct&#8217; type for each peripheral (or peripheral type, if the microprocessor designer was kind enough to make duplicate peripherals identical but for offset), then declare entire structures in the C code and in the linker script or assembly.</p>
<p>Another method is to make up header files with appropriate defines:</p>
<p>#define p_led_reg  ((uint8_t *) 0&#215;00080000)</p>
<p>etc.  This gets more complicated with C++ (the compiler will complain about old-style casts, or outright throw an error), and even more complicated yet if your code is a mix of C and C++.  But it&#8217;s a method that will work with just about every tool chain that I&#8217;ve encountered.</p>
<p>Probably the best way to do this of all is to make sure that you choose a form for addressing your registers that is tool-independent outside of the actual register definitions (header files and possibly linker scripts &amp;c.) and make sure that those register definitions are well encapsulated and easily identifiable so that if you do have to do some mad thing like changing tool chains in mid stream, the surgery will be limited to the pertinent header files, in stead of being splattered over your whole code base.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Coding Standard Rule #2: Use const Wherever Possible by Dan Szabo</title>
		<link>http://embeddedgurus.com/barr-code/2009/03/coding-standard-rule-2-use-const-wherever-possible/comment-page-1/#comment-67496</link>
		<dc:creator>Dan Szabo</dc:creator>
		<pubDate>Tue, 24 Jan 2012 17:16:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/24/coding-standard-rule-2-use-const-wherever-possible/#comment-67496</guid>
		<description>I always use a #define over const when I have a single peice of constant data, for example: when defining array lengths or maximum/minimum values for data.  I&#039;ve found that the compiler can often put the value directly into the assembly instruction for the projects I&#039;ve worked on.  I&#039;m sure that this is going to be dependent on both the compiler and processor being used, but it&#039;s in our coding standard to do it that way, so thats how I do it.

I always use const when I have data that is tightly coupled, such data in a structure or array that is all constant.  This is in addition to using const as a &#039;do not overwrite this data&#039; declaration for read only hardware registers.</description>
		<content:encoded><![CDATA[<p>I always use a #define over const when I have a single peice of constant data, for example: when defining array lengths or maximum/minimum values for data.  I&#8217;ve found that the compiler can often put the value directly into the assembly instruction for the projects I&#8217;ve worked on.  I&#8217;m sure that this is going to be dependent on both the compiler and processor being used, but it&#8217;s in our coding standard to do it that way, so thats how I do it.</p>
<p>I always use const when I have data that is tightly coupled, such data in a structure or array that is all constant.  This is in addition to using const as a &#8216;do not overwrite this data&#8217; declaration for read only hardware registers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Combining C&#8217;s volatile and const Keywords by Dan Szabo</title>
		<link>http://embeddedgurus.com/barr-code/2012/01/combining-cs-volatile-and-const-keywords/comment-page-1/#comment-67493</link>
		<dc:creator>Dan Szabo</dc:creator>
		<pubDate>Tue, 24 Jan 2012 17:05:38 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/barr-code/?p=695#comment-67493</guid>
		<description>I&#039;ve become accustomed to using the extended keywords in IAR&#039;s EW IDE to define hardware registers at specific addresses.  All hardware registers are defined as volatile, and read only registers are also defined as const.  This was to both provide our engineers (myself included) with registers that look like variables instead of pointers to variables, and also to provide more efficient code, as additional variables wouldn&#039;t be required in RAM to access hardware registers.  The tradeoff being that the code won&#039;t easily port to other IDEs.  That&#039;s always been fair in our projects, as we rarely move between IDEs when developing for a particular part, and the hardware registers need to get redefined anyway (and most likely the usage of them) if you are switching to a new processor.

Always good to refresh the old memory on volatile and const usage though.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve become accustomed to using the extended keywords in IAR&#8217;s EW IDE to define hardware registers at specific addresses.  All hardware registers are defined as volatile, and read only registers are also defined as const.  This was to both provide our engineers (myself included) with registers that look like variables instead of pointers to variables, and also to provide more efficient code, as additional variables wouldn&#8217;t be required in RAM to access hardware registers.  The tradeoff being that the code won&#8217;t easily port to other IDEs.  That&#8217;s always been fair in our projects, as we rarely move between IDEs when developing for a particular part, and the hardware registers need to get redefined anyway (and most likely the usage of them) if you are switching to a new processor.</p>
<p>Always good to refresh the old memory on volatile and const usage though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Combining C&#8217;s volatile and const Keywords by Mike Acton</title>
		<link>http://embeddedgurus.com/barr-code/2012/01/combining-cs-volatile-and-const-keywords/comment-page-1/#comment-67492</link>
		<dc:creator>Mike Acton</dc:creator>
		<pubDate>Tue, 24 Jan 2012 17:03:38 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/barr-code/?p=695#comment-67492</guid>
		<description>&quot;C’s only other decorator keyword is const.&quot; Nope. See: &#039;restrict&#039;</description>
		<content:encoded><![CDATA[<p>&#8220;C’s only other decorator keyword is const.&#8221; Nope. See: &#8216;restrict&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Coding Standard Rule #2: Use const Wherever Possible by Combining C&#8217;s volatile and const Keywords &#171; Barr Code</title>
		<link>http://embeddedgurus.com/barr-code/2009/03/coding-standard-rule-2-use-const-wherever-possible/comment-page-1/#comment-67472</link>
		<dc:creator>Combining C&#8217;s volatile and const Keywords &#171; Barr Code</dc:creator>
		<pubDate>Tue, 24 Jan 2012 13:44:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/24/coding-standard-rule-2-use-const-wherever-possible/#comment-67472</guid>
		<description>[...] Coding Standard Rule #2: Use const Whenever Possible for more on the use of const by [...]</description>
		<content:encoded><![CDATA[<p>[...] Coding Standard Rule #2: Use const Whenever Possible for more on the use of const by [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Coding Standard Rule #4: Use volatile Whenever Possible by Combining C&#8217;s volatile and const Keywords &#171; Barr Code</title>
		<link>http://embeddedgurus.com/barr-code/2009/03/coding-standard-rule-4-use-volatile-whenever-possible/comment-page-1/#comment-67452</link>
		<dc:creator>Combining C&#8217;s volatile and const Keywords &#171; Barr Code</dc:creator>
		<pubDate>Tue, 24 Jan 2012 11:29:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/27/coding-standard-rule-4-use-volatile-whenever-possible/#comment-67452</guid>
		<description>[...] Coding Standard Rule #4: Use volatile Whenever Possible for more on the use of volatile by [...]</description>
		<content:encoded><![CDATA[<p>[...] Coding Standard Rule #4: Use volatile Whenever Possible for more on the use of volatile by [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Coding Standard Rule #4: Use volatile Whenever Possible by Combining C&#8217;s volatile and const Keywords &#171; Barr Code</title>
		<link>http://embeddedgurus.com/barr-code/2009/03/coding-standard-rule-4-use-volatile-whenever-possible/comment-page-1/#comment-67451</link>
		<dc:creator>Combining C&#8217;s volatile and const Keywords &#171; Barr Code</dc:creator>
		<pubDate>Tue, 24 Jan 2012 11:29:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/27/coding-standard-rule-4-use-volatile-whenever-possible/#comment-67451</guid>
		<description>[...] Coding Standard Rule #4: Use volatile Whenever Possible for more on the use of volatile by [...]</description>
		<content:encoded><![CDATA[<p>[...] Coding Standard Rule #4: Use volatile Whenever Possible for more on the use of volatile by [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on How to Enforce Coding Standards Using PC-Lint by Bill</title>
		<link>http://embeddedgurus.com/barr-code/2011/06/how-to-enforce-coding-standards-using-pc-lint/comment-page-1/#comment-66784</link>
		<dc:creator>Bill</dc:creator>
		<pubDate>Wed, 18 Jan 2012 18:03:39 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/barr-code/?p=652#comment-66784</guid>
		<description>I followed the latest IAR Technote (TN-21805) on my Win7 setup and found two discrepancies:  1) Once you generate the predefined macros (Step 2B of the TN) you should mark that file &quot;Read Only&quot; so that it isn&#039;t removed when you do a Project-&gt;Clean; 2) The path to the libraries in the &quot;iar-co&#039;arm-v6.lnt&quot; file is incorrect (at least as it was installed on my system.  The .lnt file comes with &quot;C:\Program...\Embedded Workbench ARM 6.30.1\arm\...&quot; but should be &quot;C:\Program...\Embedded Workbench 6.0\arm\...&quot;</description>
		<content:encoded><![CDATA[<p>I followed the latest IAR Technote (TN-21805) on my Win7 setup and found two discrepancies:  1) Once you generate the predefined macros (Step 2B of the TN) you should mark that file &#8220;Read Only&#8221; so that it isn&#8217;t removed when you do a Project-&gt;Clean; 2) The path to the libraries in the &#8220;iar-co&#8217;arm-v6.lnt&#8221; file is incorrect (at least as it was installed on my system.  The .lnt file comes with &#8220;C:\Program&#8230;\Embedded Workbench ARM 6.30.1\arm\&#8230;&#8221; but should be &#8220;C:\Program&#8230;\Embedded Workbench 6.0\arm\&#8230;&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on What Belongs in a C .h Header File? by Garry</title>
		<link>http://embeddedgurus.com/barr-code/2010/11/what-belongs-in-a-c-h-header-file/comment-page-1/#comment-66406</link>
		<dc:creator>Garry</dc:creator>
		<pubDate>Mon, 16 Jan 2012 16:16:45 +0000</pubDate>
		<guid isPermaLink="false">http://embeddedgurus.com/barr-code/?p=400#comment-66406</guid>
		<description>It is clear that volume1 and volume2 are NOT equivalent. 

The bulk of the objection to volume2&#039;s using access functions seems to be &quot;less convenient, less clear, and leads to bigger and slower code&quot;. Is that a fair summary?

If the syntax of updating &#039;volume&#039; were were identical to using volume1 in the source code, would the &#039;less convenient, less clear&#039; objections go away?
If the speed and size difference could be eliminated by correct use of compiler flags, would those objections go away?

Put another way, are all of those objections potentially soluble by careful use of a programming language like C++ or C#?

Some of the issues comparing the two approaches are discussed, but some are not. If all of those objections go away, then IMHO we should consider the other side of the argument.

The code to actually *update* volume in volume1 is in every part of the program that updates it, but the code to actually *update* volume in volume2 can be forced to exist at only one place. Depending on the development system, it may be possible to choose to distribute the update code to all the update sites using volum2 by changing a few compiler flags and/or a single piece of source code. So, not only are the two approaches functionally different, the scope to change the functional behaviour of the program during developmnt is different.

This wasn&#039;t discussed as a difference, and I think it can be important.

A contrived example: If the debugging system does not support &#039;data watchpoints&#039; but only a limited number of breakpoints, the difference may become extremely significant. An intermittent problem might be hard to find, and access functions may be better than direct data access. One approach might be to augment the code around the setting of volume to log a little bit of extra information to help diagnose the error. For example, the return address of the code attempting to update the variable is available (maybe in a link register, or on a stack). None of the other code would need to be changed, so the &#039;Heisenberg effect&#039; of making code changes is likely more limited in the case of accessors than global data.

I am putting the case that writing code which makes it easy to take a defensive approach, in the absence of other issues, is inherently worth considering as a &#039;good thing&#039;.

I don&#039;t accept the &quot;you should just get yourself a better debugger and development system&quot; response. That seems equivalent to saying, &quot;make the world work in a different way, and you would have a solution to that development problem&quot;. IMHO, it is a smaller problem to write robust code than make the world work in such a way that all development problems can be sidestepped. 

I have not seen any solid, evidence based, research that supports the view that access functions are inherently less clear than assignment. Maybe someone can direct me to it? I tend to think both sides of the debate are &#039;religious war&#039; stuff in the absence of evidence.

I am willing to suggest that the fact that there *is* an explicit difference between local variables and &#039;globals&#039;, where locals can be assigned directly, and all &#039;global&#039; data must be &#039;accessed&#039;, might actually help.  
Of course, some programming languages allow that syntactic difference to be hidden, so that may be a mute point.

I especially accept a concern over &#039;lack of syntactic clarity&#039; is important, but that may depend on the language used. If  &#039;lack of syntactic clarity&#039; is the main objection, then it may also be an argument for some languages and against others.

I am simply aiming to ensure a more complete set of issues are considered, and clarify the previous objections.</description>
		<content:encoded><![CDATA[<p>It is clear that volume1 and volume2 are NOT equivalent. </p>
<p>The bulk of the objection to volume2&#8242;s using access functions seems to be &#8220;less convenient, less clear, and leads to bigger and slower code&#8221;. Is that a fair summary?</p>
<p>If the syntax of updating &#8216;volume&#8217; were were identical to using volume1 in the source code, would the &#8216;less convenient, less clear&#8217; objections go away?<br />
If the speed and size difference could be eliminated by correct use of compiler flags, would those objections go away?</p>
<p>Put another way, are all of those objections potentially soluble by careful use of a programming language like C++ or C#?</p>
<p>Some of the issues comparing the two approaches are discussed, but some are not. If all of those objections go away, then IMHO we should consider the other side of the argument.</p>
<p>The code to actually *update* volume in volume1 is in every part of the program that updates it, but the code to actually *update* volume in volume2 can be forced to exist at only one place. Depending on the development system, it may be possible to choose to distribute the update code to all the update sites using volum2 by changing a few compiler flags and/or a single piece of source code. So, not only are the two approaches functionally different, the scope to change the functional behaviour of the program during developmnt is different.</p>
<p>This wasn&#8217;t discussed as a difference, and I think it can be important.</p>
<p>A contrived example: If the debugging system does not support &#8216;data watchpoints&#8217; but only a limited number of breakpoints, the difference may become extremely significant. An intermittent problem might be hard to find, and access functions may be better than direct data access. One approach might be to augment the code around the setting of volume to log a little bit of extra information to help diagnose the error. For example, the return address of the code attempting to update the variable is available (maybe in a link register, or on a stack). None of the other code would need to be changed, so the &#8216;Heisenberg effect&#8217; of making code changes is likely more limited in the case of accessors than global data.</p>
<p>I am putting the case that writing code which makes it easy to take a defensive approach, in the absence of other issues, is inherently worth considering as a &#8216;good thing&#8217;.</p>
<p>I don&#8217;t accept the &#8220;you should just get yourself a better debugger and development system&#8221; response. That seems equivalent to saying, &#8220;make the world work in a different way, and you would have a solution to that development problem&#8221;. IMHO, it is a smaller problem to write robust code than make the world work in such a way that all development problems can be sidestepped. </p>
<p>I have not seen any solid, evidence based, research that supports the view that access functions are inherently less clear than assignment. Maybe someone can direct me to it? I tend to think both sides of the debate are &#8216;religious war&#8217; stuff in the absence of evidence.</p>
<p>I am willing to suggest that the fact that there *is* an explicit difference between local variables and &#8216;globals&#8217;, where locals can be assigned directly, and all &#8216;global&#8217; data must be &#8216;accessed&#8217;, might actually help.<br />
Of course, some programming languages allow that syntactic difference to be hidden, so that may be a mute point.</p>
<p>I especially accept a concern over &#8216;lack of syntactic clarity&#8217; is important, but that may depend on the language used. If  &#8216;lack of syntactic clarity&#8217; is the main objection, then it may also be an argument for some languages and against others.</p>
<p>I am simply aiming to ensure a more complete set of issues are considered, and clarify the previous objections.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

