Question

I am using MISRA C 2004 standards in Code Composer Studio. I am always getting errors with respect to bitwise operations during initialization of the peripherals.

In the compiler .h file, the definition is like this.

#define SFR_8BIT(addr)  extern volatile unsigned char addr

SFR_8BIT(REG1);

#define REG2  REG1

Now in my code, if I use

REG2 |= 0x01;

The MISRA C is popping out these errors:

  1. Bitwise operators shall not be applied to operands whose underlying type is signed - #1393-D (MISRA-C:2004 10.1/R)

  2. The value of an expression of integer type shall not be implicitly converted to a different underlying type if it is not a conversion to a integer type of the same signedness.

I don't want to change the compiler .h file, and I want to eradicate these warnings.

Was it helpful?

Solution

At a guess, your char is 8 bits and int is (at least) 16. That means all values of unsigned char can be represented as (signed) ints. That, in turn, means in your expression REG2 |= 0x01;, your unsigned char will be promoted to int, then the OR operation carried out, then the result of that cast back to unsigned char.

If I'm not mistaken, changing your constant to an unsigned char should prevent that:

REG2 |= (unsigned char)0x01;

or:

unsigned char one = (unsigned char)0x01;

REG2 |= one;

OTHER TIPS

Have you tried:

REG2 |= 0x01U;

?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top