HAL 库不能与 USB 和 CAN 外围设备一起正常工作
HAL libraries doesn't work correctly with USB and CAN peripheral
我正在做一个项目,我必须使用 USB(虚拟 COM 端口)和 CAN 外围设备,但是当我 运行 STM32F4DISCOVERY 上的代码时,PC 无法正确识别 VCP("USB device not recognized",错误代码 43)。
这是我的 main:
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 */
}
我建议您使用 STMCubeMX 作为起点,它会为您完成所有初始化工作。
逐步调试并检查您是否有在 USBD_Device 中有效的内容。
在我的例子中,我必须手动增加 startup.s 文件中的堆大小以使 VCP 工作。
USB 的另一个常见问题是时钟初始化。你需要在他们所谓的 48MHz 时钟上正好有 48MHz 才能使 USB 工作。对于 8MHz crystal、PLLM = 25、PLLN = 336 和 PLLQ = 7,您现在没有 48MHz。再一次,使用 STMCubMX,时钟初始化很简单。
如果使用外部电源为MCU供电,请确保大于3.0V。
我有同样的问题,问题是为 CAN 和 USB 分配相同的 PIN。我不知道我的 USB 使用了哪些 PIN,但更改了 CAN 的 PIN,之后它工作得很好。
我正在做一个项目,我必须使用 USB(虚拟 COM 端口)和 CAN 外围设备,但是当我 运行 STM32F4DISCOVERY 上的代码时,PC 无法正确识别 VCP("USB device not recognized",错误代码 43)。 这是我的 main:
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 */
}
我建议您使用 STMCubeMX 作为起点,它会为您完成所有初始化工作。
逐步调试并检查您是否有在 USBD_Device 中有效的内容。 在我的例子中,我必须手动增加 startup.s 文件中的堆大小以使 VCP 工作。
USB 的另一个常见问题是时钟初始化。你需要在他们所谓的 48MHz 时钟上正好有 48MHz 才能使 USB 工作。对于 8MHz crystal、PLLM = 25、PLLN = 336 和 PLLQ = 7,您现在没有 48MHz。再一次,使用 STMCubMX,时钟初始化很简单。
如果使用外部电源为MCU供电,请确保大于3.0V。
我有同样的问题,问题是为 CAN 和 USB 分配相同的 PIN。我不知道我的 USB 使用了哪些 PIN,但更改了 CAN 的 PIN,之后它工作得很好。