<?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>LPC MicrocontrollersのトピックRe: Unable to Update register using bitfield structure</title>
    <link>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516529#M1727</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by TheFallGuy on Fri Sep 19 00:17:31 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;So, what doe you mean by 'doesn't work? Have you looked at the code generated by the compiler to see what it is doing? And compared it with the working version?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 15 Jun 2016 17:24:38 GMT</pubDate>
    <dc:creator>lpcware</dc:creator>
    <dc:date>2016-06-15T17:24:38Z</dc:date>
    <item>
      <title>Unable to Update register using bitfield structure</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516526#M1724</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by mathseng on Thu Sep 18 23:31:13 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Can anyone advise me why I do not seem to be able to update registers using bitfield structs.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Specifically, when updating the PCLKSEL1 register, when using a bitfield struct, writing to one bitfield results in other fields being incorrectly updated. My example is updating PCLK_TIMER2, which is 2 bits at 13:12.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have attached a source file which can be used to demonstrate the issue.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Below are snippets &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mathseng&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;code&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;//Using the format to update TIMER2 with &amp;lt;anyPCLK value, eg PCLK1 ( divide by 1 clock)&amp;gt;, and then extract the PCLK value ALWAYS works&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; LPC_SC-&amp;gt;PCLKSEL1 &amp;amp;= ~((0b11 &amp;lt;&amp;lt; 12) | ((0b11 &amp;lt;&amp;lt; 8) | (0b11 &amp;lt;&amp;lt; 24))); // desired bitfield at 13:12 and reserved bitfields at 9:8, 25:24.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; LPC_SC-&amp;gt;PCLKSEL1 &amp;amp;= ~(0b11 &amp;lt;&amp;lt; 12); // desired bitfield at 13:12, ignoring reserved bitfields at 9:8, 25:24.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; LPC_SC-&amp;gt;PCLKSEL1 |= (PCLK1 &amp;lt;&amp;lt; 12);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; PCLK_type pclk = ((LPC_SC-&amp;gt;PCLKSEL1) &amp;amp; (0b11 &amp;lt;&amp;lt; 12)) &amp;gt;&amp;gt; 12;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;//Using a bitfield struct to read the register always works, but updating the register results in other fields being corrupted.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;volatile PCLKSEL1_Type * pClkSel = (PCLKSEL1_Type*)&amp;amp;(LPC_SC-&amp;gt;PCLKSEL1);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;pClkSel-&amp;gt;PCLK_TIMER2 = PCLK1;// FAILS with other register field values being corrupted&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PCLK_type pValu = pClkSel-&amp;gt;PCLK_TIMER2;// Always works&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;//Using the same bitfield struct to operate on a memory variable ALWAYS works correctly:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;volatile uint32_t memVar = 0;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;volatile PCLKSEL1_Type * pMemVar = (PCLKSEL1_Type*)&amp;amp;memVar ;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;pMemVar -&amp;gt;PCLK_TIMER2 = PCLK1;// Always works&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PCLK_type memClk = pMemVar -&amp;gt;PCLK_TIMER2;// Always works&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;/code&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;P&gt;&lt;STRONG&gt;Original Attachment has been moved to: &lt;A _jive_internal="true" href="https://community.nxp.com/docs/DOC-338025"&gt;WriteRegFail.c.zip&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 17:24:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516526#M1724</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T17:24:35Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to Update register using bitfield structure</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516527#M1725</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by TheFallGuy on Fri Sep 19 00:05:14 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Just a guess. Bitfields work by doing a read-modify-write. So reserved bits are going to be read and then written -&amp;nbsp; possibly illegally. This is why most embedded libraries do not use bitfields. Could this explain what you are seeing?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 17:24:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516527#M1725</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T17:24:36Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to Update register using bitfield structure</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516528#M1726</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by mathseng on Fri Sep 19 00:15:43 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;I don't believe so.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;reg |= value&amp;nbsp;&amp;nbsp; is read/modifywrite, and that works.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, looking at the data in the struct for the reserved fields, they are both 0 (zero), so writing 0 should not be an issue.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 17:24:37 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516528#M1726</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T17:24:37Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to Update register using bitfield structure</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516529#M1727</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by TheFallGuy on Fri Sep 19 00:17:31 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;So, what doe you mean by 'doesn't work? Have you looked at the code generated by the compiler to see what it is doing? And compared it with the working version?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 17:24:38 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516529#M1727</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T17:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to Update register using bitfield structure</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516530#M1728</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by mathseng on Fri Sep 19 01:16:35 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;I looked at the size of the code generated, but not explicitly the instructions. The code generated by the bitfield is shorter, and more importantly for me, easier to write/maintain because the code reads in 'user speak', not in magic numbers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;SPAN&gt;I am just now looking at a post on &lt;A href="www.embeddedrelated.com&amp;nbsp;" target="test_blank"&gt;www.embeddedrelated.com&amp;nbsp;&lt;/A&gt; (Using Bit Field structures to access LPC-2000 registers)&amp;nbsp; at &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fwww.embeddedrelated.com%2Fgroups%2Flpc2000%2Fshow%2F40838.php" rel="nofollow" target="_blank"&gt;http://www.embeddedrelated.com/groups/lpc2000/show/40838.php&lt;/A&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This series of posts note that bitfield writing using GCC appears to be bytewide, which is consistent with the pattern of data I am seeing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;With an original register value of 0x00, writing a 0x01 to 13:12 causes replication of the data every byte. I got bogged down by writing data to other registers, and I could not see the pattern.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Writing pattern -1 (all 1's) to the register results in one reserved register being set to 0b00, the other to 0b11. No observed anomalies resulted.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Update:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Code generated by bitfield write is definitely BYTE, hence the reason observed issues.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;code&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;pSelS-&amp;gt;PCLK_TIMER2 = PCLK1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; 504:7843&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ldrbr3, [r0, #1]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; 506:f023 0330 bic.wr3, r3, #48; 0x30&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; 50a:f043 0310 orr.wr3, r3, #16&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; 50e:7043&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; strbr3, [r0, #1]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;/code&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 17:24:38 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516530#M1728</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T17:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to Update register using bitfield structure</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516531#M1729</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by lpcxpresso-support on Fri Sep 19 01:33:33 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;You should not use bitfields on hardware registers, as hardware registers do not behave the same way as RAM when accessed as byte/half-word/word. This is why all LPCOpen and every example uses bit manipulation and not bitfields.&lt;/SPAN&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 17:24:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Unable-to-Update-register-using-bitfield-structure/m-p/516531#M1729</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T17:24:39Z</dc:date>
    </item>
  </channel>
</rss>

