我正在开发一个项目,我必须使用USB(虚拟COM端口)和CAN外设,但是当我在STM32F4DISCOVERY上运行代码时,PC无法正确识别VCP(“USB设备无法识别”,错误代码43)。 这是我的主要:
int main(void)
{
/* STM32F4xx HAL library initialization:
- Configure the Flash prefetch, instruction and Data caches
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Set NVIC Group Priority to 4
- Low Level Initialization: global MSP (MCU Support Package) initialization
*/
HAL_Init();
/* Configure the system clock to 180 MHz */
SystemClock_Config();
/* Configure LED1 and LED3 */
BSP_LED_Init(LED4);
BSP_LED_Init(LED5);
/* Init Device Library */
USBD_Init(&USBD_Device, &VCP_Desc, 0);
/* Add Supported Class */
USBD_RegisterClass(&USBD_Device, USBD_CDC_CLASS);
/* Add CDC Interface Class */
USBD_CDC_RegisterInterface(&USBD_Device, &USBD_CDC_fops);
/* Start Device Process */
USBD_Start(&USBD_Device);
if(CAN_Polling() == HAL_OK)
{
/* OK: Turn on LED1 */
BSP_LED_On(LED4);
}
else
{
/* KO: Turn on LED3 */
BSP_LED_On(LED5);
}
/* Infinite loop */
while (1)
{
}
}
这是 SystemClock_Config :
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
HAL_StatusTypeDef ret = HAL_OK;
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* The voltage scaling allows optimizing the power consumption when the device is
clocked below the maximum system frequency, to update the voltage scaling value
regarding system frequency refer to product datasheet. */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
if(ret != HAL_OK)
{
while(1) { ; }
}
/* Activate the OverDrive to reach the 180 MHz Frequency */
/*ret = HAL_PWREx_EnableOverDrive();
if(ret != HAL_OK)
{
while(1) { ; }
}*/
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
if(ret != HAL_OK)
{
while(1) { ; }
}
}
所以我不明白为什么代码无法正常工作。 任何人都可以帮我修复虚拟COM端口吗?
这是 CAN Init :
HAL_StatusTypeDef CAN_Polling(void)
{
CAN_FilterConfTypeDef sFilterConfig;
static CanTxMsgTypeDef TxMessage;
static CanRxMsgTypeDef RxMessage;
/*##-1- Configure the CAN peripheral #######################################*/
CanHandle.Instance = CANx;
CanHandle.pTxMsg = &TxMessage;
CanHandle.pRxMsg = &RxMessage;
CanHandle.Init.TTCM = DISABLE;
CanHandle.Init.ABOM = DISABLE;
CanHandle.Init.AWUM = DISABLE;
CanHandle.Init.NART = DISABLE;
CanHandle.Init.RFLM = DISABLE;
CanHandle.Init.TXFP = DISABLE;
CanHandle.Init.Mode = CAN_MODE_LOOPBACK;
CanHandle.Init.SJW = CAN_SJW_1TQ;
CanHandle.Init.BS1 = CAN_BS1_6TQ;
CanHandle.Init.BS2 = CAN_BS2_8TQ;
CanHandle.Init.Prescaler = 2;
if(HAL_CAN_Init(&CanHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the CAN Filter ###########################################*/
sFilterConfig.FilterNumber = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = 0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if(HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
/*##-3- Start the Transmission process #####################################*/
CanHandle.pTxMsg->StdId = 0x11;
CanHandle.pTxMsg->RTR = CAN_RTR_DATA;
CanHandle.pTxMsg->IDE = CAN_ID_STD;
CanHandle.pTxMsg->DLC = 2;
CanHandle.pTxMsg->Data[0] = 0xCA;
CanHandle.pTxMsg->Data[1] = 0xFE;
if(HAL_CAN_Transmit(&CanHandle, 10) != HAL_OK)
{
/* Transmition Error */
Error_Handler();
}
if(HAL_CAN_GetState(&CanHandle) != HAL_CAN_STATE_READY)
{
return HAL_ERROR;
}
/*##-4- Start the Reception process ########################################*/
if(HAL_CAN_Receive(&CanHandle, CAN_FIFO0,10) != HAL_OK)
{
/* Reception Error */
Error_Handler();
}
if(HAL_CAN_GetState(&CanHandle) != HAL_CAN_STATE_READY)
{
return HAL_ERROR;
}
if(CanHandle.pRxMsg->StdId != 0x11)
{
return HAL_ERROR;
}
if(CanHandle.pRxMsg->IDE != CAN_ID_STD)
{
return HAL_ERROR;
}
if(CanHandle.pRxMsg->DLC != 2)
{
return HAL_ERROR;
}
if((CanHandle.pRxMsg->Data[0]<<8|RxMessage.Data[1]) != 0xCAFE)
{
return HAL_ERROR;
}
return HAL_OK; /* Test Passed */
}
答案 0 :(得分:0)
我建议您使用STMCubeMX作为起点,它会为您完成所有初始化。
逐步调试并检查USBD_Device中是否有效。 在我的情况下,我不得不手动增加startup.s文件中堆的大小,以使VCP工作。
USB的另一个常见问题是时钟初始化。您需要准确的48MHz才能将其称为48MHz时钟,以便USB工作。使用8MHz晶振,PLLM = 25,PLLN = 336,PLLQ = 7,就像现在一样,你没有48MHz。再一次,使用STMCubMX,时钟init非常简单。
如果您使用外部电源为MCU供电,请确保它超过3.0V。
答案 1 :(得分:-1)
我遇到了同样的问题,问题是为CAN和USB分配了相同的PIN。我不知道我的USB启用了哪些PIN,但是更改了CAN的PIN,之后就可以正常工作了。