cancel
Showing results for 
Search instead for 
Did you mean: 

HAL libraries doesn't work correctly with USB and CAN peripheral

simone2399
Associate
Posted on August 03, 2016 at 17:07

i'm working on a project in which i have to use USB (Virtual COM Port) and CAN peripheral but when i run the code on STM32F4DISCOVERY, PC doesn't recognize the VCP correctly (''USB device not recognized'', error code 43). This is my

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)
{
}
}

And this is the

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 = 5;
RCC_OscInitStruct.PLL.PLLN = 210;
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) { ; }
}
}

This is the

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 */
}

So i don't understand why the code doesn't work correctly. Can anyone help me to fix the Virtual COM Port?

I tried to implement first the VCP communication only, and in a new project i've implemented only the CAN. Both projects work separatly, but when i try to combine both project and try to use CAN and VCP it doesn't work. Can you help me to implement in the correct manner HAL INIT?

I tried to implement first the VCP communication only, and in a new project i've implemented only the CAN. Both projects work separatly, but when i try to combine both project and try to use CAN and VCP it doesn't work. Can you help me to implement in the correct manner HAL INIT?

I tried to implement first the VCP communication only, and in a new project i've implemented only the CAN. Both projects work separatly, but when i try to combine both project and try to use CAN and VCP it doesn't work. Can you help me to implement in the correct manner HAL INIT?

#stm32 #can #discovery #cdc #vcp #stm32f4 #usb
1 REPLY 1
Amel NASRI
ST Employee
Posted on August 19, 2016 at 13:37

Hi sindaco.simone,

If you are using CAN1 with pins PA11 and PA12, USB cannot work properly then as same pins are used for DM & DP (see UM1472).

You have to select another configuration for CAN_RX and CAN_TX pins.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.