简体   繁体   English

GCC 变量映射和MISRA-C

[英]GCC variable mapping and MISRA-C

I know mostly two ways (with many variants, use bifields, data structures per peripheral, etc...) to declare memory mapped registers with GCC:我知道主要有两种方法(有很多变体,使用双域,每个外围设备的数据结构等)来声明 memory 映射寄存器与 GCC:

  • either use a pointer initaliased to the right adress like volatile uint32_t *pMyRegister = (uint32_t *)0xDEADBEEFUL;要么使用指向正确地址的指针,如volatile uint32_t *pMyRegister = (uint32_t *)0xDEADBEEFUL;
  • or define a variable and map it manually like volatile uint32_t MyRegister __attribute((section(".register_section")));或手动定义一个变量和 map 像volatile uint32_t MyRegister __attribute((section(".register_section"))); and then map the section in the LD file.然后是 map LD 文件中的部分。

Unfortunately none of these ways seem compatible with MISRA-C coding rules, the first one because there is an unauthorized (by MISRA) cast from integer to pointer, the second because of the attribute keyword.不幸的是,这些方法似乎都不与 MISRA-C 编码规则兼容,第一个是因为存在未授权(由 MISRA)从 integer 强制转换为指针,第二个是因为属性关键字。

My question is: is there a way (with the second solution) to map a variable in a section without affecting the ANSIness of the code (ie. no compiler specific keyword).我的问题是:有没有办法(使用第二种解决方案)在不影响代码的 ANSIness 的情况下(即没有编译器特定关键字)在一个部分中使用 map 变量。

Thank you谢谢

First of all, neither is correct because of missing volatile .首先,由于缺少volatile ,两者都不正确。 See How to access a hardware register from firmware?请参阅如何从固件访问硬件寄存器? which also briefly addresses MISRA-C.它还简要介绍了 MISRA-C。

To answer the question, this rule MISRA-C:2012 11.4 is advisory and comes with a rationale that says (the note in the end was appended by me):为了回答这个问题,这条规则 MISRA-C:2012 11.4 是建议性的,并附有一个理由说(最后的注释是我附加的):

"...but may be necessary when addressing memory mapped registers or other hardware specific features. If casting between integers and pointers is used, care should be taken..." [to not cause misalignment undefined behavior] “......但在寻址 memory 映射寄存器或其他硬件特定功能时可能是必要的。如果使用整数和指针之间的转换,应注意......”[不要导致未定义的未对齐行为]

So you can safely ignore this rule for your register declarations and if needed refer to the rationale of rule 11.4.因此,您可以安全地为您的寄存器声明忽略此规则,如果需要,请参考规则 11.4 的基本原理。 Since it is advisory, no deviation is required (but preferably you should document that you ignore the rule still).由于它是建议性的,因此不需要偏离(但最好您应该记录您仍然忽略该规则)。

Notably the former of your two versions is preferable from a MISRA-C perspective, since it doesn't contain non-standard extensions that you need to document how they are used, which is otherwise required as per other MISRA rules.值得注意的是,从 MISRA-C 的角度来看,您的两个版本中的前一个更可取,因为它不包含您需要记录它们的使用方式的非标准扩展,否则根据其他 MISRA 规则是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM