<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Avoid compiler issue when copying port input  to output in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Avoid-compiler-issue-when-copying-port-input-to-output/m-p/169997#M5160</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, CW6.3 doesn't make CPU writing anything back&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 13:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PTAD_PTAD0=PTAD_PTAD0; // Write current value to output latch&lt;BR /&gt;&amp;nbsp; 0000 b600&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDA&amp;nbsp;&amp;nbsp; _PTAD&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since CPU overwrites all PTAD bits when one tries to update just one bit (PTAD_PTADn=xx), I would try to update whole PTAD register.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 23:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PTAD = PTAD;&lt;BR /&gt;&amp;nbsp; 000a 4e0000&amp;nbsp;&amp;nbsp; [5]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MOV&amp;nbsp;&amp;nbsp; _PTAD,_PTAD&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But since the task is to avoid glitch on dedicated output pin, I think you don't need to sample input and make pin outputing sampled&amp;nbsp;value. I think you should just latch to PTAD what you need to be outputed, then set data direction to output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="message-edit-history"&gt;&lt;SPAN class="edit-author"&gt;Message Edited by kef on&lt;/SPAN&gt; &lt;SPAN class="local-date"&gt;2010-01-26&lt;/SPAN&gt; &lt;SPAN class="local-time"&gt;09:28 AM&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 26 Jan 2010 15:25:55 GMT</pubDate>
    <dc:creator>kef</dc:creator>
    <dc:date>2010-01-26T15:25:55Z</dc:date>
    <item>
      <title>Avoid compiler issue when copying port input  to output</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Avoid-compiler-issue-when-copying-port-input-to-output/m-p/169995#M5158</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it's probably covered somewhere, but I just cant seem to find the right search phrases.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to switch a port from input to output (repeatedly) without generating any spikes. So my plan is to read the current value fromt the input and write it to the output latch while the port is still in input mode, then switch to output.&lt;/P&gt;&lt;P&gt;The code for this obviously looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// Switching to output mode&lt;/P&gt;&lt;P&gt;PTAD_PTAD0=PTAD_PTAD0; // Write current value to output latch&lt;/P&gt;&lt;P&gt;PTADD_PTADD0 = 1; // Switch port direction to output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The compiler will generate a warning "Result not used", which is ok, the first expression does of course seem pretty useless.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did not check the generated assembly instructions yet, so I do not know if the compiler really wipes out the whole instruction (I guess it should not, since the registers should be declared volatile?)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, my question is, what is best practice here?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Am I simply supposed to ignore the warning?&lt;/P&gt;&lt;P&gt;Is CW supposed to not generate a warning since it's a volatile declared thing?&lt;/P&gt;&lt;P&gt;Should I read the input value to a temporary variable before writing it back to the port?&lt;/P&gt;&lt;P&gt;Is there any suggested way to do it "right"?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sven&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Jan 2010 09:20:31 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Avoid-compiler-issue-when-copying-port-input-to-output/m-p/169995#M5158</guid>
      <dc:creator>Kopone</dc:creator>
      <dc:date>2010-01-26T09:20:31Z</dc:date>
    </item>
    <item>
      <title>Re: Avoid compiler issue when copying port input  to output</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Avoid-compiler-issue-when-copying-port-input-to-output/m-p/169996#M5159</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;indeed, volatile is a big and complicated area.&amp;nbsp; &lt;A href="http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf" target="test_blank"&gt;http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf&lt;/A&gt; is a good paper on possible traps and pitfalls.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To your problem: You did not indicate which compiler you are using, but I assume that the compiler will do a read from the port, and does not write the value back. At least this is what the warning is indicating, and that's why the warning makes sense here. In any case you should look at the generated code in your case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It may depend on your hardware if writing to an input port will latch the value (you need to check this). Anyway, if you want to enforce a read-write cycle you could use&lt;/P&gt;&lt;P&gt;if (PTAD_PTAD0) {&lt;BR /&gt;&amp;nbsp; PTAD_PTAD0=1;&lt;BR /&gt;} else {&lt;BR /&gt;&amp;nbsp; PTAD_PTAD0=0;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BK&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Jan 2010 13:17:57 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Avoid-compiler-issue-when-copying-port-input-to-output/m-p/169996#M5159</guid>
      <dc:creator>BlackNight</dc:creator>
      <dc:date>2010-01-26T13:17:57Z</dc:date>
    </item>
    <item>
      <title>Re: Avoid compiler issue when copying port input  to output</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Avoid-compiler-issue-when-copying-port-input-to-output/m-p/169997#M5160</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, CW6.3 doesn't make CPU writing anything back&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 13:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PTAD_PTAD0=PTAD_PTAD0; // Write current value to output latch&lt;BR /&gt;&amp;nbsp; 0000 b600&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDA&amp;nbsp;&amp;nbsp; _PTAD&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since CPU overwrites all PTAD bits when one tries to update just one bit (PTAD_PTADn=xx), I would try to update whole PTAD register.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 23:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PTAD = PTAD;&lt;BR /&gt;&amp;nbsp; 000a 4e0000&amp;nbsp;&amp;nbsp; [5]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MOV&amp;nbsp;&amp;nbsp; _PTAD,_PTAD&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But since the task is to avoid glitch on dedicated output pin, I think you don't need to sample input and make pin outputing sampled&amp;nbsp;value. I think you should just latch to PTAD what you need to be outputed, then set data direction to output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="message-edit-history"&gt;&lt;SPAN class="edit-author"&gt;Message Edited by kef on&lt;/SPAN&gt; &lt;SPAN class="local-date"&gt;2010-01-26&lt;/SPAN&gt; &lt;SPAN class="local-time"&gt;09:28 AM&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Jan 2010 15:25:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Avoid-compiler-issue-when-copying-port-input-to-output/m-p/169997#M5160</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2010-01-26T15:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: Avoid compiler issue when copying port input  to output</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Avoid-compiler-issue-when-copying-port-input-to-output/m-p/169998#M5161</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ooh, stupid me, totally forgot about the tech details, SORRY:&lt;/P&gt;&lt;P&gt;I am using CW 6.2 on a 9S08QE8 here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;So meanwhile, I did check the assembly, and yes, it only reads the port data, so the warning is totally valid.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BlackKnight, I changed the code to your suggested&amp;nbsp; if/else pattern and this produces correct code. I'll check out that paper you ref'd to, thanks a bunch.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kef, thank you very much for your suggestions. Absolutely valid points and the PTAD=PTAD approach seems even more elegant (though it might have a tad of a smell of asking for sideeffects, but that might be just me :smileywink: ).&lt;/P&gt;&lt;P&gt;It's just in my certain case here that the present code and type of encapsulation would mean quite some reprogramming to have the code know what port and what direction register belongs to the used bit. But I dont want to bore you with further details :smileywink:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you guys for your time and help,&lt;/P&gt;&lt;P&gt;&amp;nbsp;Sven&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Jan 2010 21:10:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Avoid-compiler-issue-when-copying-port-input-to-output/m-p/169998#M5161</guid>
      <dc:creator>Kopone</dc:creator>
      <dc:date>2010-01-26T21:10:40Z</dc:date>
    </item>
  </channel>
</rss>

