【发布时间】:2017-06-24 15:56:42
【问题描述】:
我正在研究 GTM 模块,并且很难理解 MCAL 代码, 这是代码示例:
/* Here the definition of the register */
/** \brief CMU Global Clock Control Numerator Register */
typedef struct _Ifx_GTM_CMU_GCLK_NUM_Bits
{
Ifx_Strict_32Bit GCLK_NUM:24; /**< \brief [23:0] Numerator for global clock divider (rw) */
Ifx_Strict_32Bit reserved_24:8; /**< \brief \internal Reserved */
} Ifx_GTM_CMU_GCLK_NUM_Bits;
/**
\brief CMU Global Clock Control Numerator Register */
typedef union
{
unsigned int U; /**< \brief Unsigned access */
signed int I; /**< \brief Signed access */
Ifx_GTM_CMU_GCLK_NUM_Bits B; /**< \brief Bitfield access */
} Ifx_GTM_CMU_GCLK_NUM;
/*CMU Global Clock Control Numerator Register */
#define GTM_CMU_GCLK_NUM /*lint --e(923)*/ (*(volatile fx_GTM_CMU_GCLK_NUM*)0xF0100304u)
/* Here we are using the register */
/* The content of Numerator and Denominator are temporarily taken in local
variables and used in the if statement below for Misra reasons. */
RegTemp3 = GTM_CMU_GCLK_NUM.U;
那么为什么联合中有字段 U 和 I 以及为什么使用 U 而不是位域 Ifx_GTM_CMU_GCLK_NUM_Bits B
执行此代码时遇到问题,因为我在执行时遇到算术溢出异常:
RegTemp3 = GTM_CMU_GCLK_NUM.U;
【问题讨论】:
-
RegTemp3 是如何定义的?联合用于以不同的方式表示同一事物。因此,他们想将联合的“未签名”版本分配给 reg。它将防止在 Reg 中进行符号扩展
-
看起来像带有少量 XML 的 C。删除了 C++ 标签。
-
uint32 RegTemp3; /* 存储寄存器值的临时变量 */
-
您的编译器的 int 位大小是多少?在任何情况下,为确保类型兼容性,RegTemp3 应声明为
unsigned int。
标签: c struct unions cpu-registers