cancel
Showing results for 
Search instead for 
Did you mean: 

Switching a pin initialization between GPIO and Timer Channel. [STM32G0]

xpp07
Senior

In STM32G030, PA6 can be configured as PWM Output of Timer 16. What I want is to initialize the pin in analog mode (High Impedance) and when I want to use the PWM, then switch the configuration to timer channel. I think this could be better done writing my own functions with registers. But I'm trying to see how to do it with HAL.

Using initialization code from CubeMx, and modifying it:

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

void MX_TIM16_Init(void); //Removed static keyword

void PA6_analog ();

void PA6_timer();

Below the functions which I would call whenever I want to use them.

void PA6_analog(){

//clock is already initialized in MX_GPIO_Init for other ports

 GPIO_InitTypeDef GPIO_InitStruct = {0};  

 GPIO_InitStruct.Pin = GPIO_PIN_6;

 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);   

}

void PA6_timer (){

 MX_TIM16_Init();

//start PWM output

//stop the timer when done using it

PA6_analog(); //switch back to high impedance

}

Would this work?

1 REPLY 1
TDK
Guru

Yes, your PA6_analog function will set PA6 to high impedance mode.

HAL may or may not initialize the pin in timer mode depending on your CubeMX settings. It would be done in HAL_TIM_MspInit.

Seems like your scheme will work.

If you feel a post has answered your question, please click "Accept as Solution".