cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4xx USB_CDC Class Configuration on SensorTile: Why Timer is needed?

TNguy.10
Associate II

Dear,

From the SensorTile Sample Code (STSW-STLKT01). The USB CDC device class is set up with some function of Timer3. I do not understand why Timer3 is used and how useful to do so. Will someone please help me to understand this?

Please refer to the code below for the setup of Timer3 on USB_CDC_Class:

static int8_t CDC_Itf_Init(void)

{

 /*##-2- Enable TIM peripherals Clock #######################################*/

 TIMx_CLK_ENABLE();

  

  /*##-3- Configure the NVIC for TIMx ########################################*/

 /* Set Interrupt Group Priority */

 HAL_NVIC_SetPriority(TIMx_IRQn, 0x6, 0);

  

 /* Enable the TIMx global Interrupt */

 HAL_NVIC_EnableIRQ(TIMx_IRQn);

  

 /*##-3- Configure the TIM Base generation #################################*/

 TIM_Config();

  

 /*##-4- Start the TIM Base generation in interrupt mode ####################*/

 /* Start Channel1 */

 if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)

 {

  /* Starting Error */

  Error_Handler();

 }

  

 /*##-5- Set Application Buffers ############################################*/

 USBD_CDC_SetTxBuffer(&USBD_Device, UserTxBuffer, 0);

 USBD_CDC_SetRxBuffer(&USBD_Device, UserRxBuffer);

  

 return (USBD_OK);

}

static void TIM_Config(void)

{  

 /* Set TIMx instance */

 TimHandle.Instance = TIMx;

  

 /* Initialize TIM3 peripheral as follow:

    + Period = 10000 - 1

    + Prescaler = ((SystemCoreClock/2)/10000) - 1

    + ClockDivision = 0

    + Counter direction = Up

 */

 TimHandle.Init.Period = (CDC_POLLING_INTERVAL*1000) - 1;

 TimHandle.Init.Prescaler = 80-1;

 TimHandle.Init.ClockDivision = 0;

 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;

 if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK)

 {

  /* Initialization Error */

  Error_Handler();

 }

}

Best,

Tony Nguyen

2 REPLIES 2
Pavel A.
Evangelist III

> I do not understand why Timer3 is used and how useful to do so.

Are you asking why TIM3 is used and not other timer? Or why they use a timer at all?

If the latter... Can you just find the timer interrupt service routine and see what it does?

Maybe it triggers reading some sensor data?

-- pa

Hi Pavel,

I am asking why Timer is used at all? is that for the USB interrupt? how is that useful compared to not use it at all? Thank you!

best,

Tony Nguyen