This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Bitband: Macro to get bit number from bit mask???

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:

#define  TIM_SR_UIF                          ((uint16_t)0x0001)            /*!<Update interrupt Flag */
#define  TIM_SR_CC1IF                        ((uint16_t)0x0002)            /*!<Capture/Compare 1 interrupt Flag */
#define  TIM_SR_CC2IF                        ((uint16_t)0x0004)            /*!<Capture/Compare 2 interrupt Flag */
#define  TIM_SR_CC3IF                        ((uint16_t)0x0008)            /*!<Capture/Compare 3 interrupt Flag */
#define  TIM_SR_CC4IF                        ((uint16_t)0x0010)            /*!<Capture/Compare 4 interrupt Flag */
#define  TIM_SR_COMIF                        ((uint16_t)0x0020)            /*!<COM interrupt Flag */
#define  TIM_SR_TIF                          ((uint16_t)0x0040)            /*!<Trigger interrupt Flag */
#define  TIM_SR_BIF                          ((uint16_t)0x0080)            /*!<Break interrupt Flag */
#define  TIM_SR_CC1OF                        ((uint16_t)0x0200)            /*!<Capture/Compare 1 Overcapture Flag */
#define  TIM_SR_CC2OF                        ((uint16_t)0x0400)            /*!<Capture/Compare 2 Overcapture Flag */
#define  TIM_SR_CC3OF                        ((uint16_t)0x0800)            /*!<Capture/Compare 3 Overcapture Flag */
#define  TIM_SR_CC4OF                        ((uint16_t)0x1000)            /*!<Capture/Compare 4 Overcapture Flag */

In the "Timer Update Interrupt" I need to clear the UIF flag in the TIM_SR register. I would prefer to do this atomic.

For this I defined the following macro:

#define BB_OFFSET       0x02000000
#define BB_SRAMMASK 0xF0000000
#define CLEAR_BIT_BB(VarAddr, BitNumber)    \ 
          (*(__IO uint32_t *) ( (VarAddr & BB_SRAMMASK) | BB_OFFSET | ((VarAddr & (BB_OFFSET-1)) << 5) | ((BitNumber) << 2)) = 0)

Now I can nicely use the command

#define BITNR_UIF 0
CLEAR_BIT_BB(TIM8->SR, BITNR_UIF);

This generally is very nice - just I am disturbed that I have to define this BITNR_UIF. It would be very nice to get the BitNumber from the STM32F4xx.h mask definition somehow automatically by some macro. Converting BitNumber to BitMask is easy with the 1<<BitNumber operator, but other direction seems to be impossible. Anybody has some hint? (Or Keil could consider to implement a precompiler instruction log2, so that BitNumber=Log2(BitMask)???