Skip to main content
OMarc.1
Associate II
February 7, 2022
Solved

How to change GPIO parameters from OUTPUT to PWM, and vice versa?

  • February 7, 2022
  • 4 replies
  • 2683 views

Hi, everyone, I have the next problem, I am using the same pins like a simple GPIO output, and like a PWM output.

To change the output from PWM to GPIO i using this code:

   GPIO_InitTypeDef gpio;

   gpio.Pin = GPIO_PIN_0|GPIO_PIN_1;

   gpio.Mode = GPIO_MODE_OUTPUT_PP;

   HAL_GPIO_Init(GPIOB, &gpio);

But, how to return PWM initialization?

This topic has been closed for replies.
Best answer by Tesla DeLorean

You miss which AF mux value​ it should associate with the pin(s)

P​SC and ARR are N-1 comparators.

4 replies

waclawek.jan
Super User
February 7, 2022

I don't use Cube/HAL, but probably by using GPIO_MODE_AF_PP?

In reality you only need to change the bitfield which belongs to given pin in GPIO_MODERx register from 0b01 to 0b10.

JW

OMarc.1
OMarc.1Author
Associate II
February 7, 2022

Hi, Jan.

It doesn't work.

But thank you for your answer.

Kind regards

waclawek.jan
Super User
February 7, 2022

What doesn't work? What did you do, and what are the results?

If PWM does not work, read out the TIM and relevant GPIO registers content and check/post.

Also, which STM32?

JW

OMarc.1
OMarc.1Author
Associate II
February 7, 2022

Maybe i do something wrong, but i use the next code:

  GPIO_InitTypeDef gpio;

  gpio.Pin = GPIO_PIN_0|GPIO_PIN_1;

  gpio.Mode = GPIO_MODE_AF_PP;

  HAL_GPIO_Init(GPIOB, &gpio);

//MX_TIM8_Init();

 TIM8->ARR=1023;

 TIM8->PSC=500;

 TIM8->CCR2=x;

Maybe i need to re-write some parameters?

Because, when i use this code in one of the int:

  GPIO_InitTypeDef gpio;

  gpio.Pin = GPIO_PIN_0|GPIO_PIN_1;

  gpio.Mode = GPIO_MODE_OUTPUT_PP;

  HAL_GPIO_Init(GPIOB, &gpio);

Need to reset the board, to enable pwm output.

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
February 7, 2022

You miss which AF mux value​ it should associate with the pin(s)

P​SC and ARR are N-1 comparators.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
February 7, 2022

> GPIO_InitTypeDef gpio;

>   gpio.Pin = GPIO_PIN_0|GPIO_PIN_1;

>   gpio.Mode = GPIO_MODE_AF_PP;

>   HAL_GPIO_Init(GPIOB, &gpio);

Fill in *all* fields of the gpio struct, not just .Pin and .Mode.

If this won't help, read out and check/post content of *all* GPIOB and TIM8 registers.

JW

S.Ma
Principal
February 8, 2022

Configure the mode and af gpio field, under interrupt temporary disabled to avoid side effect. Then change dynamically. Remember to save the primask, change bit fields, restore. So the code also works under interrupts.