Skip to main content
Mehdi98Vosoughi
Associate
July 20, 2023
Question

How to toggle GPIO in the PWM Generation No Output mode

  • July 20, 2023
  • 3 replies
  • 3712 views

Hello
I want to use timer 17 of STM32G030K6T6 microcontroller in pwm generation no output mode and toggle one of the GPIO pins.
I just started using HAL functions. I am looking for the interrupt function related to the duty setting registry to toggle one of the GPIO pins, but I could not find it.
Thank you for your help

This topic has been closed for replies.

3 replies

Stassen.C
ST Employee
July 20, 2023

Hello Mehdi98Vosoughi, 

First could you please tell me if you used STM32CubeMx for your code generation?

If yes, did you enable the timer 17 global interrupt in NVIC settings?

Regards,

Stassen

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.
Mehdi98Vosoughi
Associate
July 20, 2023

Yes, I am using STM32CubeMx and I have activated timer 17 global interrupt in NVIC settings

Stassen.C
ST Employee
July 20, 2023

Ok then your HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1); could be in either :

your callback

 

 

void TIM17_IRQHandler(void)
{
 /* USER CODE BEGIN TIM17_IRQn 0 */

 /* USER CODE END TIM17_IRQn 0 */
 HAL_TIM_IRQHandler(&htim17);
 /* USER CODE BEGIN TIM17_IRQn 1 */
 HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);

 /* USER CODE END TIM17_IRQn 1 */
}

 

 

which is found in your stm32g0xx_it.c file.

OR

Add :

 

 

void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim){
 HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
}

 

 

in your main.c

You can find more information on this in RM0454 if needed.

Don't forget to start your timer in It mode in your main.c

 

 

 HAL_TIM_PWM_Start_IT(&htim17, TIM_CHANNEL_1); //--> Start PWM with IT

 

 

It should run fine.

Regards,

Stassen 

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.
waclawek.jan
Super User
July 20, 2023

> I am looking for the interrupt function related to the duty setting registry

Where do you look for it?

waclawekjan_0-1689855896569.png

waclawekjan_1-1689855965024.png

waclawekjan_2-1689856122552.png

It's screenshot of the startup code, clicking on it it will bring you to the highlighted line, which is the item in vector table - you are supposed to write an ISR with that name.

JW

 

Stassen.C
ST Employee
July 20, 2023

Hello JW, 

If like Mehdi, you did generate your code through STM32CubeMx, your callback should be written automatically in stm32g0xx_it.c file.

And it should look like this:

void TIM17_IRQHandler(void)
{
 /* USER CODE BEGIN TIM17_IRQn 0 */

 /* USER CODE END TIM17_IRQn 0 */
 HAL_TIM_IRQHandler(&htim17);
 /* USER CODE BEGIN TIM17_IRQn 1 */
 HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);

 /* USER CODE END TIM17_IRQn 1 */
}

No need to write it yourself. You can incorporate your application here. For instance toggling a led in my example.

But please be aware that you need to start your timer in your main:

 HAL_TIM_PWM_Start_IT(&htim17, TIM_CHANNEL_1); //--> Start PWM with IT

Otherwise it won't work.

 

Regards,

Stassen 

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.
waclawek.jan
Super User
July 20, 2023

Hi @Stassen.C ,

Thank you, I don't use Cube/CubeMX.

I guess the autogenerated ISR does not contain the pin toggle, i.e. it's

void TIM17_IRQHandler(void)
{
 /* USER CODE BEGIN TIM17_IRQn 0 */

 /* USER CODE END TIM17_IRQn 0 */
 HAL_TIM_IRQHandler(&htim17);
 /* USER CODE BEGIN TIM17_IRQn 1 */

 /* USER CODE END TIM17_IRQn 1 */
}

By " I am looking for the interrupt function related to the duty setting registry" IMO Mehdi meant "where in CubeMX is a tickbox generating such a function, or if it's generated automatically, how is it called". As there is no such simple mechanism, IMO it's nearly impossible for a CubeMX user to "discover" the steps you've just described, on their own, just by using CubeMX and/or reading the associated documentation.

In other words, this is probably the simplest example where Cube as a concept stops being useful and starts to get into way.

JW