1

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

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

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

2 Answers2

0

I would advice you to use STMCubeMX as a starting point, it does all the initialisation for you.

Debug step by step and check if you have something valid in USBD_Device. In my case, I had to manually increase the size of the heap in the startup.s file to make the VCP work.

Another common issue with USB is the clock initialisation. You need to have exactly 48MHz on what they call the 48MHz clock for the USB to work. With a 8MHz crystal, PLLM = 25, PLLN = 336 and PLLQ = 7 as you have now, you don't have 48MHz. Once again, with STMCubMX, the clock init is straightforward.

If you use an external power supply for powering the MCU, make sure it is more than 3.0V.

Guillaume Michel
  • 1,189
  • 8
  • 14
  • I tried to use the HAL example for the communication via VCP and it works; but when i try to implement also the CAN the code doesn't work. I tried to debug but i'm not able to fix the issue (the program stop to work when the function HAL_init() start. Are CAN and USB compatible? I need to have 48MHz clock for VCP, and for CAN? Can i use the same clock? At the end, which value of PLLM, PLLN and PLLQ must i initialize to obtain a 48MHz clock? Thank you so much for the answer. – Simone Sindaco Aug 02 '16 at 13:53
  • M=5, N=210, P=2 and Q=7 will set the 48MHz clock to 48MHz and the system clock to 168MHz. USB and CAN could potentially share the same pins. USB has fixed pins, so you need to make sure that CAN does not use the same as USB. The 48MHz clock is only needed for USB. Edit your post and share the CAN init code, it might help. – Guillaume Michel Aug 02 '16 at 14:26
  • i have added the CAN init code. How can i set the correct pins for CAN? Thanks for answer and i'm sorry for my stupid questions but i'm a newbie. – Simone Sindaco Aug 02 '16 at 15:12
  • Have you implemented `HAL_CAN_MspInit`. `HAL_CAN_Init` calls it. Once again with STMCube, the init is done for you and you don't have to worry about it. If you don't want to use STMCube, that's fine. But use it first to make it work and then base your code on the one created by STMCube. – Guillaume Michel Aug 02 '16 at 16:08
  • 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? – Simone Sindaco Aug 03 '16 at 13:31
  • Which pins are the CAN connected to? and USB? How do you access CAN physically? – Guillaume Michel Aug 03 '16 at 15:57
  • I use CAN only in loopback mode, so i haven't set any pins for CAN and i don't know how i can do it. For USB i used the USB cable directly. – Simone Sindaco Aug 05 '16 at 13:06
-1

I had the same issue and problem was with assigning same PINs for both CAN and USB. I didn't know which PINs my USB were on, but changed the PINs for CAN and it worked just fine after that.

Kaveh
  • 11
  • 1