cancel
Showing results for 
Search instead for 
Did you mean: 

How to toggle GPIO in the PWM Generation No Output mode

Mehdi98Vosoughi
Associate

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

7 REPLIES 7
Stassen.C
ST Employee

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

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

> 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

 

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.

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.

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

Trying to answer your question, 

You are right for the time being we don't have any article on it. I will bring this up to my colleagues. 

Trying to explain it though:

In CubeMx it looks like this:

StassenC_2-1689863564262.png

 

You choose your timer in question on the left and you activate it.

In the NVIC Settings tab, the global interrupt should be activated:

StassenC_1-1689862746551.png

This checkbox will create your callback in the stm32g0xx_it.c file.

Nevertheless you have access to STM32G0 cube package here

This package contains all the examples available for the G0 series and is sorted by board and applications, you can always start with one example and work your way from there. 

I really hope it helps.

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.