cancel
Showing results for 
Search instead for 
Did you mean: 

SPI clock use as a timer

pshin.2
Associate II

Hii,

I am using STM32h745IIK6. I want to use my SPI clock as a timer to generate delay in microsecond how can i to that. Please help with tutorial or your guidance.  Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
FBL
ST Employee

Hello @pshin.2

 

You can synchronize the SPI clock and Timer input pin. Here is an example to get started with.

 

  /*##- Configure the SPI peripheral with the desired frequency #######################################
...

  /*##- Configure the TIM peripheral #######################################
   *
  *-The external signal is connected to TIM4_CH2 pin (PB.07),
  * and a rising edge on this input is used to trigger the Timer.
  *
  *-The One Pulse signal is output on TIM4_CH1 (PB.06).
  *
  * The delay value is fixed to:
  * - Delay =  CCR1/TIM4 counter clock
  *          = 16383 / 20000000 [sec]
  *
  * The pulse value is fixed to :
  * - Pulse value = (TIM_Period - TIM_Pulse)/TIM4 counter clock
  *               = (65535 - 16383) / 20000000 [sec]
  *
  * ------------------------------------------------------------------------ */
 /* Enable the TIMx clock */
 TIMx_CLK_ENABLE();

 /* Enable GPIO Port Channel1 & channel2 Clock */
 TIMx_CHANNEL_GPIO_PORT();

 /* Configure PB.06 and PB.07 */
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF_TIMx;

 GPIO_InitStruct.Pull = GPIO_PUPD_CHANNEL1;
 GPIO_InitStruct.Pin = GPIO_PIN_CHANNEL1;
 HAL_GPIO_Init(TIMx_GPIO_PORT, &GPIO_InitStruct);

 GPIO_InitStruct.Pull = GPIO_PUPD_CHANNEL2;
 GPIO_InitStruct.Pin = GPIO_PIN_CHANNEL2;
 HAL_GPIO_Init(TIMx_GPIO_PORT, &GPIO_InitStruct);



TimHandle.Instance = TIMx;

 TimHandle.Init.Period            = 0xFFFF;
 TimHandle.Init.Prescaler         = uwPrescalerValue;
 TimHandle.Init.ClockDivision     = 0;
 TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
 TimHandle.Init.RepetitionCounter = 0;

 if (HAL_TIM_OnePulse_Init(&TimHandle, TIM_OPMODE_SINGLE) != HAL_OK)
 {
   /* Initialization Error */
   Error_Handler();
 }

 /*##-2- Configure the Channel 1 in One Pulse mode ##########################*/
 sConfig.OCMode       = TIM_OCMODE_PWM2;
 sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
 sConfig.Pulse        = 16383;
 sConfig.ICPolarity   = TIM_ICPOLARITY_RISING;
 sConfig.ICSelection  = TIM_ICSELECTION_DIRECTTI;
 sConfig.ICFilter     = 0;
 sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
 sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;
 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;

 if (HAL_TIM_OnePulse_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1, TIM_CHANNEL_2) != HAL_OK)
 {
   /* Configuration Error */
   Error_Handler();
 }

  if (HAL_TIM_OnePulse_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
 {
   /* Starting Error */
   Error_Handler();
 }

You can configure SPI peripheral with the desired clock frequency. You will also need to adjust the timer prescaler and period values to generate interrupts at the desired frequency.

 

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.

View solution in original post

1 REPLY 1
FBL
ST Employee

Hello @pshin.2

 

You can synchronize the SPI clock and Timer input pin. Here is an example to get started with.

 

  /*##- Configure the SPI peripheral with the desired frequency #######################################
...

  /*##- Configure the TIM peripheral #######################################
   *
  *-The external signal is connected to TIM4_CH2 pin (PB.07),
  * and a rising edge on this input is used to trigger the Timer.
  *
  *-The One Pulse signal is output on TIM4_CH1 (PB.06).
  *
  * The delay value is fixed to:
  * - Delay =  CCR1/TIM4 counter clock
  *          = 16383 / 20000000 [sec]
  *
  * The pulse value is fixed to :
  * - Pulse value = (TIM_Period - TIM_Pulse)/TIM4 counter clock
  *               = (65535 - 16383) / 20000000 [sec]
  *
  * ------------------------------------------------------------------------ */
 /* Enable the TIMx clock */
 TIMx_CLK_ENABLE();

 /* Enable GPIO Port Channel1 & channel2 Clock */
 TIMx_CHANNEL_GPIO_PORT();

 /* Configure PB.06 and PB.07 */
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF_TIMx;

 GPIO_InitStruct.Pull = GPIO_PUPD_CHANNEL1;
 GPIO_InitStruct.Pin = GPIO_PIN_CHANNEL1;
 HAL_GPIO_Init(TIMx_GPIO_PORT, &GPIO_InitStruct);

 GPIO_InitStruct.Pull = GPIO_PUPD_CHANNEL2;
 GPIO_InitStruct.Pin = GPIO_PIN_CHANNEL2;
 HAL_GPIO_Init(TIMx_GPIO_PORT, &GPIO_InitStruct);



TimHandle.Instance = TIMx;

 TimHandle.Init.Period            = 0xFFFF;
 TimHandle.Init.Prescaler         = uwPrescalerValue;
 TimHandle.Init.ClockDivision     = 0;
 TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
 TimHandle.Init.RepetitionCounter = 0;

 if (HAL_TIM_OnePulse_Init(&TimHandle, TIM_OPMODE_SINGLE) != HAL_OK)
 {
   /* Initialization Error */
   Error_Handler();
 }

 /*##-2- Configure the Channel 1 in One Pulse mode ##########################*/
 sConfig.OCMode       = TIM_OCMODE_PWM2;
 sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
 sConfig.Pulse        = 16383;
 sConfig.ICPolarity   = TIM_ICPOLARITY_RISING;
 sConfig.ICSelection  = TIM_ICSELECTION_DIRECTTI;
 sConfig.ICFilter     = 0;
 sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
 sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;
 sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;

 if (HAL_TIM_OnePulse_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1, TIM_CHANNEL_2) != HAL_OK)
 {
   /* Configuration Error */
   Error_Handler();
 }

  if (HAL_TIM_OnePulse_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
 {
   /* Starting Error */
   Error_Handler();
 }

You can configure SPI peripheral with the desired clock frequency. You will also need to adjust the timer prescaler and period values to generate interrupts at the desired frequency.

 

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.