如何让(1 << 9)通过MISRA?

Tho*_*ews 7 c bit-shift misra c-preprocessor

我们正在使用Parasoft Static Analysis并启用了MISRA C 2004检查程序.

该软件是一个嵌入式系统.我们喜欢描述常量如下:

[1]    #define MOTOR_ON (1 << 9)  
Run Code Online (Sandbox Code Playgroud)

这将显示寄存器中的第9位应为1以打开电机.

表达式是MISRA失败,所以我们改变了它:

[2]    #define MOTOR_ON (1U << 9U)
Run Code Online (Sandbox Code Playgroud)

更改转换为无符号整数常量,因为最好使用无符号整数进行移位.

语句2中的表达式仍然失败,因为右侧操作符(9U)需要检查.根据MISRA,如果右手操作符大于左手操作符的底层类型的位宽,则存在问题.

该问题的基础是1U具有基础类型unsigned char或8位.
我们写的寄存器是16位,所以理论上没有问题.

如何更改[2]中的表达式以使其通过MISRA C 2004,更喜欢不使用演员表?

我正在使用IAR Embedded Workbench和8/32位模式的ARM7TDMI处理器.

编辑1:示例代码.

void turn_on_motor(void);
#define MOTOR_ON (1U << 9U)
void turn_on_motor(void)
{
    uint16_t * const p_motor_control = (uint16_t *)(0x01234567U);
    *p_motor_control = MOTOR_ON;
}
Run Code Online (Sandbox Code Playgroud)

错误文本:应限制用作移位运算符的右侧操作数的常量.

来自Parasoft提供的MISRA规则文档:

Rule reports a violation if:

- the right-hand operand is a constant with negative value or with value that
  exceeds the length (in bits) of the left-hand operand

- the right-hand operand is not a constant and is not checked by specific
  pattern
Run Code Online (Sandbox Code Playgroud)

Jen*_*ens 1

您还可以通过使用来简单地规避转移问题

 #define MOTOR_ON ((uint16_t)512U)   /* 1 << 9 = 2^9 */
Run Code Online (Sandbox Code Playgroud)