STM32L0开发笔记4: 解决使用--CPP11编译USB驱动报错问题
程序员文章站
2022-06-30 17:31:21
...
项目希望使用C++语言进行程序设计,在Keil中使用C++语言的方法,我已经在在Keil中使用C++进行程序设计进行介绍过,不过对于STM32CubeMX生成的STM32L0XX相关文件进行编译时,提示如下图所示的错误。
仔细分析可以看出,所有的错误都集中在usbd_conf.c文件,定位错误的具体语句,如下所示。
USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev)
{
HAL_StatusTypeDef hal_status = HAL_OK;
USBD_StatusTypeDef usb_status = USBD_OK;
hal_status = HAL_PCD_DeInit(pdev->pData);
switch (hal_status) {
case HAL_OK :
usb_status = USBD_OK;
break;
case HAL_ERROR :
usb_status = USBD_FAIL;
break;
case HAL_BUSY :
usb_status = USBD_BUSY;
break;
case HAL_TIMEOUT :
usb_status = USBD_FAIL;
break;
default :
usb_status = USBD_FAIL;
break;
}
return usb_status;
}
错误出现在hal_status = HAL_PCD_DeInit(pdev->pData);语句上。根据错误的具体提示,可分析出是传递参数的类型不匹配,先查看HAL_PCD_DeInit函数,如下所示,函数希望传递的参数类型是PCD_HandleTypeDef。
HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
{
/* Check the PCD handle allocation */
if(hpcd == NULL)
{
return HAL_ERROR;
}
hpcd->State = HAL_PCD_STATE_BUSY;
/* Stop Device */
HAL_PCD_Stop(hpcd);
/* DeInit the low level hardware */
HAL_PCD_MspDeInit(hpcd);
hpcd->State = HAL_PCD_STATE_RESET;
return HAL_OK;
}
再查看pdev->pData变量,如下所示,pData的类型是void,这就是编译时报错的原因。具体分析可认为C99允许对void隐式的改变数据类型,但CPP11则不可以。
typedef struct _USBD_HandleTypeDef
{
uint8_t id;
uint32_t dev_config;
uint32_t dev_default_config;
uint32_t dev_config_status;
USBD_SpeedTypeDef dev_speed;
USBD_EndpointTypeDef ep_in[15];
USBD_EndpointTypeDef ep_out[15];
uint32_t ep0_state;
uint32_t ep0_data_len;
uint8_t dev_state;
uint8_t dev_old_state;
uint8_t dev_address;
uint8_t dev_connection_status;
uint8_t dev_test_mode;
uint32_t dev_remote_wakeup;
USBD_SetupReqTypedef request;
USBD_DescriptorsTypeDef *pDesc;
USBD_ClassTypeDef *pClass;
void *pClassData;
void *pUserData;
void *pData;
} USBD_HandleTypeDef;
将*pData的数据类型直接修改为PCD_HandleTypeDef,如下所示,再编译时,问题解决。typedef struct _USBD_HandleTypeDef
{
uint8_t id;
uint32_t dev_config;
uint32_t dev_default_config;
uint32_t dev_config_status;
USBD_SpeedTypeDef dev_speed;
USBD_EndpointTypeDef ep_in[15];
USBD_EndpointTypeDef ep_out[15];
uint32_t ep0_state;
uint32_t ep0_data_len;
uint8_t dev_state;
uint8_t dev_old_state;
uint8_t dev_address;
uint8_t dev_connection_status;
uint8_t dev_test_mode;
uint32_t dev_remote_wakeup;
USBD_SetupReqTypedef request;
USBD_DescriptorsTypeDef *pDesc;
USBD_ClassTypeDef *pClass;
void *pClassData;
void *pUserData;
PCD_HandleTypeDef *pData;
} USBD_HandleTypeDef;
原创性文章,转载请注明出处
CSDN:http://blog.csdn.net/qingwufeiyang12346。