单片机型号:STM32F091RCT6
使用C语音在PlatformIO中进行STM32Cube的工程设计,一切正常,希望使用C++语音进行程序开发,顾将main.c文件改为main.cpp文件,再次编译报如下警告:
In file included from C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_rcc.h:45:0,
from C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_conf.h:185,
from C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal.h:46,
from include/main.h:31,
from src\5_LogicLayer\main.cpp:22:
src\5_LogicLayer\main.cpp: In function 'void MX_GPIO_Init()':
C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_rcc.h:664:48: warning: conversion to void will not access object of type 'volatile uint32_t {aka volatile long unsigned int}'
UNUSED(tmpreg); \
C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_def.h:87:27: note: in definition of macro 'UNUSED'
#define UNUSED(x) ((void)(x))
^
src\5_LogicLayer\main.cpp:166:3: note: in expansion of macro '__HAL_RCC_GPIOF_CLK_ENABLE'
__HAL_RCC_GPIOF_CLK_ENABLE();
^~~~~~~~~~~~~~~~~~~~~~~~~~
C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_rcc.h:650:48: warning: conversion to void will not access object of type 'volatile uint32_t {aka volatile long unsigned int}'
UNUSED(tmpreg); \
C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_def.h:87:27: note: in definition of macro 'UNUSED'
#define UNUSED(x) ((void)(x))
^
src\5_LogicLayer\main.cpp:167:3: note: in expansion of macro '__HAL_RCC_GPIOB_CLK_ENABLE'
__HAL_RCC_GPIOB_CLK_ENABLE();
^~~~~~~~~~~~~~~~~~~~~~~~~~
C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_rcc.h:643:48: warning: conversion to void will not access object of type 'volatile uint32_t {aka volatile long unsigned int}'
UNUSED(tmpreg); \
C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_def.h:87:27: note: in definition of macro 'UNUSED'
#define UNUSED(x) ((void)(x))
^
src\5_LogicLayer\main.cpp:168:3: note: in expansion of macro '__HAL_RCC_GPIOA_CLK_ENABLE'
__HAL_RCC_GPIOA_CLK_ENABLE();
警告的内容较多,可以一级级分析,最后可将警告语句定位在下列文件:
C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_rcc.h:664:48
代码如下:
#define __HAL_RCC_GPIOA_CLK_ENABLE() do { \
__IO uint32_t tmpreg; \
SET_BIT(RCC->AHBENR, RCC_AHBENR_GPIOAEN);\
/* Delay after an RCC peripheral clock enabling */\
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_GPIOAEN);\
UNUSED(tmpreg); \
} while(0U)
#define __HAL_RCC_GPIOB_CLK_ENABLE() do { \
__IO uint32_t tmpreg; \
SET_BIT(RCC->AHBENR, RCC_AHBENR_GPIOBEN);\
/* Delay after an RCC peripheral clock enabling */\
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_GPIOBEN);\
UNUSED(tmpreg); \
} while(0U)
#define __HAL_RCC_GPIOC_CLK_ENABLE() do { \
__IO uint32_t tmpreg; \
SET_BIT(RCC->AHBENR, RCC_AHBENR_GPIOCEN);\
/* Delay after an RCC peripheral clock enabling */\
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_GPIOCEN);\
UNUSED(tmpreg); \
} while(0U)
#define __HAL_RCC_GPIOF_CLK_ENABLE() do { \
__IO uint32_t tmpreg; \
SET_BIT(RCC->AHBENR, RCC_AHBENR_GPIOFEN);\
/* Delay after an RCC peripheral clock enabling */\
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_GPIOFEN);\
UNUSED(tmpreg); \
} while(0U)
#define __HAL_RCC_CRC_CLK_ENABLE() do { \
__IO uint32_t tmpreg; \
SET_BIT(RCC->AHBENR, RCC_AHBENR_CRCEN);\
/* Delay after an RCC peripheral clock enabling */\
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_CRCEN);\
UNUSED(tmpreg); \
} while(0U)
#define __HAL_RCC_DMA1_CLK_ENABLE() do { \
__IO uint32_t tmpreg; \
SET_BIT(RCC->AHBENR, RCC_AHBENR_DMA1EN);\
/* Delay after an RCC peripheral clock enabling */\
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_DMA1EN);\
UNUSED(tmpreg); \
} while(0U)
#define __HAL_RCC_SRAM_CLK_ENABLE() do { \
__IO uint32_t tmpreg; \
SET_BIT(RCC->AHBENR, RCC_AHBENR_SRAMEN);\
/* Delay after an RCC peripheral clock enabling */\
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_SRAMEN);\
UNUSED(tmpreg); \
} while(0U)
#define __HAL_RCC_FLITF_CLK_ENABLE() do { \
__IO uint32_t tmpreg; \
SET_BIT(RCC->AHBENR, RCC_AHBENR_FLITFEN);\
/* Delay after an RCC peripheral clock enabling */\
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FLITFEN);\
UNUSED(tmpreg); \
} while(0U)
具体语句为:
UNUSED(tmpreg)
仔细分析以上语句可以看出程序中定义了tmpreg变量,并给tmpreg进行了赋值操作,但是并没有使用tmpreg变量。如果不使用UNUSED(tmpreg)语句,则会报“变量已赋值,但没有使用”的警告。所以UNUSED(tmpreg)是为了避免警告发生。
再看UNUSED宏的实现代码,在下列文件中:
C:\users\snmplink\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Inc/stm32f0xx_hal_def.h:87:27:
具体程序如下:
#define UNUSED(x) ((void)(x))
可看到GCC认为不应将volatile uint32_t变量强制转换为void类型。
由于这条警告没有提示类型,我在网上查找很长时间,均没有找到屏蔽的方法,故考虑直接解决,实际上避免“变量已赋值,但没有使用”的警告,还有另外一种方法,就是使用如下宏:
#define UNUSED(x) ((x) = (x))
将UNUSED宏修改后,问题解决。