How to switch from output or input Mode in Stm MIctrocontroller for exemple after using GpioA_Pin0 as digital output and in the same programm how to use it to send data via Usart2 so my problem is how to switch between output and alternative function mode
How to switch from output or input Mode in Stm MIctrocontroller for exemple after using GpioA_Pin0 as digital output and in the same programm how to use it to send data via Usart2 so my problem is how to switch between output and alternative function mode
In general this is a more advanced usage the IO pins but not impossible. Stm uses the hal abstraction. But your both use cases are organized in different files.
You can use Stm32CubeMx to help to generate the init files.
The key would be to switch in the GPIO matrix. Your application needs to handle to GPIO matrix switches.
Port Init for Output:
/* Configure Port */
GPIO_InitStructure.Pin = GPIO_PIN_12;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_NO_ALT;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
Port Init for UART:
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
So your solution should be to switch between
(GPIO_MODE_OUTPUT_PP
) vs (GPIO_MODE_AF_PP
+ GPIO_AF7_USART2
)
If helpful please vote me up.