Skip to main content
Associate II
October 30, 2023
Solved

change pins from gpio to uart

  • October 30, 2023
  • 5 replies
  • 4753 views

Hi everyone!, in my prototype  i need some low and high pulse before sending uart datas.. How can i do this? I have already tried using HAL_GPIO_Write and HAL_DELAY and it is not worked.I need an advice for it.Thanks.

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

You'd need to switch the MODER off of AF_PP mode and into a GPIO mode.

Or depending on the levels, perhaps find a pattern that matches. Perhaps use microsecond counter for tight resolution. 

5 replies

gbm
Super User
October 30, 2023

Maybe you may simply use BREAK generation for the "low pulse". And "high pulse" is an idle state of UART output.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
October 30, 2023

You'd need to switch the MODER off of AF_PP mode and into a GPIO mode.

Or depending on the levels, perhaps find a pattern that matches. Perhaps use microsecond counter for tight resolution. 

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Associate II
October 31, 2023

Great!!, it works. I share part of the code with the modifications. I was using a stm32F103C8.

USART2->CR1 = 0x00; // Deshabilita la uart

// Configurar el pin 2 del puerto A como salida
GPIOA->CRL &= ~(GPIO_CRL_CNF2 | GPIO_CRL_MODE2); // Borrar la configuración anterior
GPIOA->CRL |= GPIO_CRL_MODE2; // Configurar como salida push-pull de 10 MHz

GPIOA->BSRR |= (1 << 2);// Funcion Write SET pero utilizando registros
HAL_Delay(2000);
GPIOA->BSRR |=(1 << 18);// Funcion Write RESET pero utilizando registros
HAL_Delay(25);
GPIOA->BSRR |=(1 << 2);
HAL_Delay(25);

GPIOA->CRL &= ~(GPIO_CRL_CNF2 | GPIO_CRL_MODE2);//borra la configuracion anterior del pin

USART2->CR1 = 0x01; //habilita la uart
MX_USART2_UART_Init(); // llama a la funcion para configurar la uart
HAL_UART_Receive_IT(&huart2, &rx_buffer, 1); //Inicializacion de la Interrupcion de la uart

Note: HAL_GPIO_Write(); can be used instead of GPIOA->BSRR |= (1 << 2);

Thank you all very much for responding :)

Associate
October 30, 2023

First you'll need to call HAL_GPIO_Init() with appropriate parameters to configure the pin as GPIO Output, then call HAL_GPIO_Write()  and HAL_Delay() as you need. After that, you'll need to return the pin as AF USART.
You'll find examples on HAL_UART_MspInit() and MX_GPIO_Init() functions (I'm assuming that you are auto-generating code with STM32CubeIDE).

gbm
Super User
October 31, 2023

Do not use logic operators on BSRR; use assignments instead. Don't set the TX pin output to 1 - it's 1 when the UART is idle.

You don't need to reconfigure the UART after switching from GPIO to AF. There is no reason to call MX_USART2_UART_Init() after this switch.

Assuming that the GPIO output state is permanently set to 0, your code should probably look like this:

HAL_Delay(2000); // keep TX high while UART is inactive

// Configurar el pin 2 del puerto A como salida
GPIOA->CRL ^= GPIO_CRL_CNF2; // switch from AF to Output; sets TX low
HAL_Delay(25);
GPIOA->CRL ^= GPIO_CRL_CNF2; // switch from Output to AF, sets TX high
HAL_Delay(25);
HAL_UART_Receive_IT(&huart2, &rx_buffer, 1); //Inicializacion de la Interrupcion de la uart
My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice