我该怎么做才能避免MISRA为下面的代码提供此错误?我尝试使用(unit16_t)进行投射.但后来它不允许显式转换.
在复杂表达式中从基础MISRA类型"unsigned char"到"unsigned int"的非法隐式转换(MISRA C 2004规则10.1)
uint8_t rate = 3U; uint8_t percentage = 130U; uint16_t basic_units = rate * percentage;
我想存储一个寄存器值并执行bitwise NOT operator如下操作
typedef union TEST_REG {
uint32_t u32Register;
uint8_t Byte[4];
uint16_t hword[2];
} tst_reg;
#define TEST_REGISTER ((volatile tst_reg*) 0x7023100CUL) // Register
Run Code Online (Sandbox Code Playgroud)
下面是存储值的示例代码
#define CHECK_FLAG (0x00000004)
TEST_REGISTER->u32Register &= (uint32_t)(~(CHECK_FLAG));
Run Code Online (Sandbox Code Playgroud)
但是当我执行TEST_REGISTER->u32Register &= (uint32_t)(~(CHECK_FLAG));代码时,QAC会抛出类似的警告
Constant: Negative value cast to an unsigned type
Run Code Online (Sandbox Code Playgroud)
我知道如果我们这样做,NOT (~) operator十六进制值将更改为负数。
所以我想知道如何正确地将负值转换为无符号类型?有什么建议 ?
我想在我的文件中使用以下MACRO
#define DO_WAKEUP_SYNCHRONISIEREN proc.id = process_WAKEUP;proc.priority.level = PRIO_AKTIV_1;proc.priority.param=0u;(void)prio_mapF_put_in_matrix(proc)
Run Code Online (Sandbox Code Playgroud)
我知道这违反了MISRA-C:2004规则19.4 - 但我需要使用它并解决违规行为.
有什么方法可以使用MACRO并解决MISRA-C违规问题吗?
MISRA-C:2004规则19.4是
C宏只能扩展为支撑的初始化,常量,字符串文字,带括号的表达式,类型限定符,存储类说明符或do-while-zero结构.