问题描述
我刚刚在我的嵌入式项目中下载了带有 c++20 的 GCC 10。
然而,在嵌入式应用程序中,将 volatile 用于寄存器结构映射器是很常见的。
由于编译器不知道寄存器值是否被 DMA 从外部更改,因此“强制”重新加载该寄存器是有意义的。
为了提高性能,其中一些 volatiles 被放置在 C 头文件中。 当我在 cpp 文件中包含这些头文件时,我会遇到很多不稳定的弃用错误。
有什么办法可以禁用这些错误吗?
@Edit 根据要求提供一些示例代码。
/*!
* @brief Enable the clock for specific IP.
*
* @param name Which clock to enable,see \ref clock_ip_name_t.
*/
static inline void CLOCK_EnableClock(clock_ip_name_t name)
{
uint32_t regAddr = SIM_BASE + CLK_GATE_ABSTRACT_REG_OFFSET((uint32_t)name);
(*(volatile uint32_t *)regAddr) |= (1UL << CLK_GATE_ABSTRACT_BITS_SHIFT((uint32_t)name));
}
C:/xxx/kinetisSDK/2.7.0/devices/MK64F12/drivers/fsl_clock.h:671:37: error: compound assignment with 'volatile'-qualified left operand is deprecated [- Werror=volatile]
671 | (*(volatile uint32_t *)regAddr) |= (1UL << CLK_GATE_ABSTRACT_BITS_SHIFT((uint32_t)name)); |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
由于这些定义遍布恩智浦 SDK,我尝试仅提取必要的部分。
不过,Godbolt 似乎还不错。也许是因为我在 arm eabi 上使用了 gcc 10。
解决方法
您不能再使用 |=
,但您可以使用 =
,因此更改此:
(*(volatile uint32_t *)regAddr) |= (1UL << CLK_GATE_ABSTRACT_BITS_SHIFT((uint32_t)name));
为此:
*(volatile uint32_t *)regAddr = *(volatile uint32_t *)regAddr | (1UL << CLK_GATE_ABSTRACT_BITS_SHIFT((uint32_t)name));