<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/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>Bitband: Macro to get bit number from bit mask???</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/28904/bitband-macro-to-get-bit-number-from-bit-mask</link><description> 
Hi, 
another crazy wish: 

 
Anybody possibly has an idea how to get the bit number from the
predefined bitmask? 

 
STM32F4xx.h has very nicely predefined all bitmasks for the
peripheral bits, e. g. for the interrupt status flags in the TIM_SR
registers</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Bitband: Macro to get bit number from bit mask???</title><link>https://community.arm.com/thread/127586?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2012 22:56:37 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:23a9c026-5741-4bac-8338-3faa86bd2fc2</guid><dc:creator>nice day</dc:creator><description>&lt;p&gt;&lt;p&gt;
Are you sure - this sounds strange - maybe in optimisation level
zero?&lt;/p&gt;

&lt;p&gt;
(I always use optimization level 1 - USB lib of ST e. g. does not
work in optimisation level zero).&lt;/p&gt;

&lt;p&gt;
I checked my macro thoroughly, and the compiler produces exactly 2
assembler commands:&lt;br /&gt;
An LDR to load the address in a register (which is omitted if some
nearby base address is already available).&lt;br /&gt;
An STR to save the 0 or 1 to this register.&lt;/p&gt;

&lt;p&gt;
So very optimum - I did not find any troubles.&lt;/p&gt;

&lt;p&gt;
The advantage of my solution is, that you can work with the
bitmask values which are defined in the STM32F4xx.h directly - you do
not need to define your own BitNr values.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bitband: Macro to get bit number from bit mask???</title><link>https://community.arm.com/thread/119735?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2012 12:14:27 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e70fd15c-3bf4-41d8-9970-3480044afad1</guid><dc:creator>Mike Kleshov</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Can you post your version of CLEAR_BIT_BB?&lt;/i&gt;&lt;/p&gt;

&lt;pre&gt;
#define REG32(addr)     (*(uint32_t volatile*)(addr))
#define PERIPH_BASE     0x40000000
#define REGBIT(reg,bit) REG32(PERIPH_BASE+0x2000000+((uint32_t)&amp;amp;(reg)-PERIPH_BASE)*32+(bit)*4)

...
/* usage example */
REGBIT(RCC_AHBENR, 3) = 1;
REGBIT(RCC_AHBENR, 3) = 0;
&lt;/pre&gt;

&lt;p&gt;
What I meant is when all numbers are known at compile time, the
compiler should be able to just use the resulting address. I was very
surprised to see that the RealView compiler would not do that. It
appears that it will not use the result of
&lt;b&gt;(uint32_t)&amp;amp;(reg)&lt;/b&gt; for compile time calculations. Odd, isn&amp;#39;t
it?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bitband: Macro to get bit number from bit mask???</title><link>https://community.arm.com/thread/106127?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2012 09:04:07 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b9b81d1c-b74a-4827-9177-4527d0ac1c83</guid><dc:creator>nice day</dc:creator><description>&lt;p&gt;&lt;p&gt;
Yes, of course my version calculates all the addresses at compile
time (otherwise I would not use it - I need it to save time and to
keep atomic :) ).&lt;/p&gt;

&lt;p&gt;
Can you post your version of CLEAR_BIT_BB?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bitband: Macro to get bit number from bit mask???</title><link>https://community.arm.com/thread/91605?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2012 08:38:15 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:bc116171-5720-4856-8cc9-ad4af7d8442d</guid><dc:creator>Mike Kleshov</dc:creator><description>&lt;p&gt;&lt;p&gt;
By the way, I seem to remember that my version of the
CLEAR_BIT_BB() macro would do address calculation at run time with
the RealView compiler. Can you verify that with your setup?&lt;br /&gt;
IAR compiler, on the other hand, would calculate the address at
compile time.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bitband: Macro to get bit number from bit mask???</title><link>https://community.arm.com/thread/60865?ContentTypeID=1</link><pubDate>Thu, 28 Jun 2012 07:36:10 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:924a1090-7e80-42dc-bcbf-0417bb85f357</guid><dc:creator>nice day</dc:creator><description>&lt;p&gt;&lt;p&gt;
Now I made my own &amp;quot;log2&amp;quot;-macro:&lt;/p&gt;

&lt;pre&gt;
#define BITNR(x) (x&amp;amp;0x1?0:x&amp;amp;0x2?1:x&amp;amp;0x4?2:x&amp;amp;0x8?3: \ 
                 x&amp;amp;0x10?4:x&amp;amp;0x20?5:x&amp;amp;0x40?6:x&amp;amp;0x80?7: \ 
                 x&amp;amp;0x100?8:x&amp;amp;0x200?9:x&amp;amp;0x400?10:x&amp;amp;0x800?11: \ 
                 x&amp;amp;0x1000?12:x&amp;amp;0x2000?13:x&amp;amp;0x4000?14:x&amp;amp;0x8000?15: \ 
                 x&amp;amp;0x10000?16:x&amp;amp;0x20000?17:x&amp;amp;0x40000?18:x&amp;amp;0x80000?19: \ 
                 x&amp;amp;0x100000?20:x&amp;amp;0x200000?21:x&amp;amp;0x400000?22:x&amp;amp;0x800000?23: \ 
                 x&amp;amp;0x1000000?24:x&amp;amp;0x2000000?25:x&amp;amp;0x4000000?26:x&amp;amp;0x8000000?27: \ 
                 x&amp;amp;0x10000000?28:x&amp;amp;0x20000000?29:x&amp;amp;0x40000000?30:x&amp;amp;0x80000000?31:32)

#define CLEAR_BIT_BB( Var, BitMask)    \ 
          (*(__IO uint32_t *) ( ( ((uint32_t)&amp;amp;(Var)) &amp;amp; BB_SRAMMASK) | BB_OFFSET | (( ((uint32_t)&amp;amp;(Var)) &amp;amp; (BB_OFFSET-1)) &amp;lt;&amp;lt; 5) | (BITNR(BitMask) &amp;lt;&amp;lt; 2)) = 0)
#define SET_BIT_BB( Var, BitMask)    \ 
          (*(__IO uint32_t *) ( ( ((uint32_t)&amp;amp;(Var)) &amp;amp; BB_SRAMMASK) | BB_OFFSET | (( ((uint32_t)&amp;amp;(Var)) &amp;amp; (BB_OFFSET-1)) &amp;lt;&amp;lt; 5) | (BITNR(BitMask) &amp;lt;&amp;lt; 2)) = 1)
#define READ_BIT_BB(Var, BitMask)       \ 
          (*(__IO uint32_t *) ( ( ((uint32_t)&amp;amp;(Var)) &amp;amp; BB_SRAMMASK) | BB_OFFSET | (( ((uint32_t)&amp;amp;(Var)) &amp;amp; (BB_OFFSET-1)) &amp;lt;&amp;lt; 5) | (BITNR(BitMask) &amp;lt;&amp;lt; 2)) )
&lt;/pre&gt;

&lt;p&gt;
With this it works nicely, I can write such things like:&lt;/p&gt;

&lt;pre&gt;
CLEAR_BIT_BB(ADC1-&amp;gt;SR,ADC_SR_JEOC);
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
using the BitMASK definition for ADC_SR_JEOC.&lt;/p&gt;

&lt;p&gt;
If anybody knows some simpler replacement for this long macro
BITNR defined above, I anyway would be very grateful.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>