2023-10-30 10:35 AM
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.
Solved! Go to Solution.
2023-10-30 11:10 AM
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.
2023-10-30 11:02 AM
Maybe you may simply use BREAK generation for the "low pulse". And "high pulse" is an idle state of UART output.
2023-10-30 11:10 AM
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.
2023-10-30 11:42 AM
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).
2023-10-31 01:52 PM
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 :)
2023-10-31 02:18 PM - edited 2023-10-31 02:56 PM
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