<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>embedded C++: add cheap bitwise conversion to a register structure</title><link>/technologies/code_exchange/b/blog/posts/embedded-c-add-cheap-bitwise-conversion-to-a-register-structure</link><description>This blog is based on real world work. I&amp;#39;m controlling a TI DRV8711 stepper motor controller via SPI.
I need to modify 16-bit registers of that IC. TI provided a header file with register definitions, and code to convert that struct to a 16 bit ...</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: embedded C++: add cheap bitwise conversion to a register structure</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/embedded-c-add-cheap-bitwise-conversion-to-a-register-structure</link><pubDate>Thu, 17 Apr 2025 22:17:43 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:4677fef4-8aed-43e7-97b1-0a780bb57be6</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I reused this for a stepper motor design that I&amp;#39;m developing. But I used the constructor this time, to do the conversion.&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:86887152-f39d-4180-a51f-7f898ef4cc9d:type=c_cpp&amp;text=%2F%2A%20%20Stepper%20motor%20command%20wrapper%0D%0A%20%20%20%20lightweight%20%0D%0A%2A%2F%0D%0Aclass%20command%20%7B%0D%0Apublic%3A%0D%0A%20%20%20%20inline%20command%28uint32_t%20steps%2C%20bool%20reverse%29%20%3A%20cmd_%28steps%20%3C%3C%201%20%7C%20%28reverse%20%3F%200%20%3A%201%29%29%20%7B%7D%0D%0A%20%20%20%20inline%20operator%20uint32_t%28%29%20const%20%7B%20return%20cmd_%3B%20%7D%0D%0Aprivate%3A%0D%0A%20%20%20%20uint32_t%20cmd_%3B%0D%0A%7D%3B]&lt;/p&gt;
&lt;p&gt;My stepper motor driver&amp;nbsp;uses 32 bit instructions.&amp;nbsp;&lt;br /&gt;Bit 0 is the direction (left/right)&lt;br /&gt;Bit 31 - 1 are the number of steps.&lt;/p&gt;
&lt;p&gt;During construction, the two parameters are immediately converted to a single uint32_t, ready for the stepper driver.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;How much space does this object take?&lt;/p&gt;
&lt;p&gt;data size (each object): same as an &lt;strong&gt;uint32_t&lt;br /&gt;&lt;/strong&gt;code size (one time) - and also the cost of a single conversion: same as a call to&amp;nbsp;&lt;strong&gt;steps &amp;lt;&amp;lt; 1 | (reverse ? 0 : 1);&lt;br /&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;example:&lt;/p&gt;
&lt;pre&gt;command cmd(&lt;em&gt;steps&lt;/em&gt;,&amp;nbsp;&lt;em&gt;reverse&lt;/em&gt;);&lt;/pre&gt;
&lt;p&gt;At runtime (firmware), this uses&amp;nbsp;exactly the&amp;nbsp;same resources as when you&amp;#39;d&amp;nbsp;use a uint32_t variable, and fill it by calling:&lt;/p&gt;
&lt;pre&gt;uint32_t cmd =&amp;nbsp;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;em&gt;steps&lt;/em&gt; &amp;lt;&amp;lt; 1 | (&lt;em&gt;reverse&lt;/em&gt; ? 0 : 1)&lt;/pre&gt;
&lt;p&gt;The original post uses late conversion. Data is&amp;nbsp;converted when the operator is called. In that design,&amp;nbsp;The operator uint32_t() has the conversion logic.&lt;br /&gt;It makes sense. because the developer may want to manipulate individual register&amp;nbsp;parts, before writing.. And we&amp;#39;re not expecting arrays of registers.&lt;/p&gt;
&lt;p&gt;In the example that I use in this comment, we don&amp;#39;t expect manipulation on the data&amp;nbsp;once it&amp;#39;s created. You collect steps and&amp;nbsp; directions, then send it to the stepper motor.&lt;br /&gt;In that case, it makes sense to convert to the smallest data size immediately, in the constructor, taking the conversion hit at declaration. &lt;br /&gt;When the operator uint32_t() is called, it just passes the data.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=28808&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: embedded C++: add cheap bitwise conversion to a register structure</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/embedded-c-add-cheap-bitwise-conversion-to-a-register-structure</link><pubDate>Sat, 05 Apr 2025 20:45:07 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:4677fef4-8aed-43e7-97b1-0a780bb57be6</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;The approach allowed me to restrict the code in my firmware to just this:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " height="196" src="/resized-image/__size/844x392/__key/commentfiles/f7d226abd59f475c9d224a79e3f0ec07-4677fef4-8aed-43e7-97b1-0a780bb57be6/pastedimage1743885782138v1.png" width="422" /&gt;&lt;/p&gt;
&lt;p&gt;At the same time, I used way less clock ticks and other resources (init time, memory) than TI&amp;#39;s example. Without adding any costs to their (good, I think, but aged) code.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=28808&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: embedded C++: add cheap bitwise conversion to a register structure</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/embedded-c-add-cheap-bitwise-conversion-to-a-register-structure</link><pubDate>Mon, 31 Mar 2025 10:12:18 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:4677fef4-8aed-43e7-97b1-0a780bb57be6</guid><dc:creator>michaelkellett</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;What happens if ENBL = 2u&amp;nbsp; ?&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;what does the generated assembler code look like ?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;why not use bit fields, although in C the assignment of an out of range value to a bit field is implementation dependent (don&amp;#39;t know about C++).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;MK&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=28808&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: embedded C++: add cheap bitwise conversion to a register structure</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/embedded-c-add-cheap-bitwise-conversion-to-a-register-structure</link><pubDate>Mon, 31 Mar 2025 04:24:17 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:4677fef4-8aed-43e7-97b1-0a780bb57be6</guid><dc:creator>shabaz</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Nice work! Very clever how all this can be encapsulated when using C++, and&amp;nbsp;structs, not just classes. Sometimes easy to forget. I understand why the const (I won&amp;#39;t put it here in case others want to have a go).&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=28808&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>