2025-01-26 10:10 PM - last edited on 2025-01-27 12:48 AM by Andrew Neil
I'm trying uart loopback and checking if I'm receiving the transmitted data on the receive buffer, I'm using nucleo-u575zi board , uart2 I'm using , baudrate is 57600 , the tx pin & rx pin are configured as shown below
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
I'm using receive in interrupt mode and in ISR function I'm trying to receive byte by byte, And also, before going to receive callback it goes error callback function where the error-code is 4 and then it goes rxcallback where byte by byte reception happens, but buffer value is zero.
If the tx & rx modes AF_PP it works fine where in buffer I can see the data in buffer Whatever I'm transmitting but in the above-mentioned mode, receive buffer nothing is coming.
I have attached the main.c , msp.c and it.c files below, kindly ignore the commented lines I was trying to work on something else too.
Solved! Go to Solution.
2025-01-27 01:42 AM
Hi
I have checked it using logic analyzer, the transmitted bits show up. I'll attach the ss below. I'm transmitting txdata = [0x1, 0x2, 0x3] .......
As it is loopback the same thing comes in receive but I couldn't see anything in receive buffer.
2025-01-27 02:15 AM
In your code:
void send_dcdi_command(uint8_t* data, uint8_t size){
//HAL_UART_Transmit_IT(&huart2, data, 3);
// HAL_UART_AbortReceive(&huart2);
// HAL_UART_Receive_IT(&huart2, buffer, 1);
//__HAL_UART_CLEAR_FEFLAG(&huart2);
//enable uart receive in interrupt mode
HAL_UART_Transmit(&huart2, data, 3,20);
HAL_UART_Receive_IT(&huart2, buffer, 1);
HAL_UART_Transmit does not return until the transmission is finished;
You only call HAL_UART_Receive_IT after that - when the transmission is finished and gone!
2025-01-27 02:47 AM - edited 2025-01-27 02:47 AM
Sorry, just to understand more clearly so, u meant when transmission is happening only receiving also is happening at same time but I'm calling receive function after transmission so that is the reason no data is coming in receive buffer, is it?
2025-01-27 02:58 AM - edited 2025-01-27 02:59 AM
@Meghana wrote:u meant when transmission is happening only receiving also is happening at same time
Not in the way you've coded it.
Again, HAL_UART_Transmit does not return until the transmission is finished.
@Meghana wrote:I'm calling receive function after transmission so that is the reason no data is coming in receive buffer, is it?
Probably; or, at least, the reason you're not getting an interrupt.
Try this instead:
// Prepare to receive data
HAL_UART_Receive_IT(&huart2, buffer, 1);
// Transmit some data
HAL_UART_Transmit(&huart2, data, 3,20);
There seems to be a lot of superfluous stuff in the code you posted.
I would strongly suggest that you start with a simple project which just does this - nothing else.
You can add the extras after you have the basics working.
2025-01-27 03:12 AM
Hi @Imen.D,
I have explored the examples which u have sent. Thank you for the response and resources. But I'm trying to figure out the working of Tx & Rx in different modes which are open drain mode and input mode respectively, when using the AF_PP mode I'm getting data correctly but not in open drain mode. I need help to figure out the open-drain mode.
In loop back I don't think baud rate might be an issue for frame error, other uart settings are there in the main.c file which I've attached.
Thanks,
Meghana
2025-01-27 04:58 AM - edited 2025-01-27 06:32 AM
I wrote it already: configure TX as OD AF with pullup enabled. Configure RX as AF with pullup enabled. Do NOT set RX as input - it's not STM32F1 series. For any MCU of any STM32 series other than F1, the peripheral input pin MUST BE CONFIGURED as AF, not as input (input == GPIO input). Also, unlike in F1, the pullups are configured independently from other pin characteristics.
I am not sure if internal pullups are enough for 57600; I guess not. They should be ok for up to 9600 maybe. Above that use external pullup resistor of 1.5..4.7k.
2025-01-27 05:27 AM
2025-01-28 12:51 AM
Thank you for the response @gbm . It worked when the RX pin mode is configured as AF_PP. Without the need of external or internal pullup, I was able to receive data in buffer.
2025-01-28 12:54 AM
Hi
Thank you for the response.
Not sure on how to work with oscilloscope it would be of great help if u could give me detailed steps how to check with respect to internal pull up using oscilloscope.
2025-01-28 09:08 AM
Of course there MUST be some pullup if TX is configured as OD and it's driven by OD output. Otherwise the operation of your circuit will be random - it may even work, sometimes; usually it won't.
You seem to think of your MCU in terms of F1. It's NOT F1 series. Read about port config options - see MODER, AFR, OTYPER, OSPEEDR registers.
Push-pull/OD is an output configuration which may apply to GPIO out or AF out. For AF input there is no such option, regardless of what unreasonable constant names you use in your GPIO config routines. RX is just an input which may or may not be pulled up (or down).