cancel
Showing results for 
Search instead for 
Did you mean: 

change pins from gpio to uart

matias_efe
Associate

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5
gbm
Lead III

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

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
Up vote any posts that you find helpful, it shows what's working..
Hagi
Associate II

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).

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 :)

gbm
Lead III

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