我正在研究 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;
那么为什么 union 中有文件 U 和 I 以及为什么使用 U 而不是位字段 Ifx_GTM_CMU_GCLK_NUM_Bits B
我在执行此代码时遇到问题,因为在执行时出现算术溢出异常:
RegTemp3 = GTM_CMU_GCLK_NUM.U;
最佳答案
GTM_CMU_GCLK_NUM
是特定于目标系统的硬编码硬件寄存器。
将其作为无符号值读入 RegTemp3
是确保以 32 位对硬件进行单次访问所必需的。
您是否在目标系统上执行此代码?
您确定支持算术溢出异常
吗?
如果您在主机系统上运行代码,很可能会导致段错误。
关于c - 奇怪的寄存器定义、声明和使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44738245/