I thought to create a simple test to send and receive a buffer via UART. I succeeded in sending bytes from my PC to the nucleo bord and receiving them in blocking and non-blocking mode. But for some reason I can't succeed to send data from UART3 and receive the bytes on UART2.
I have a nucleo-f103rb and have the following pins setup:
- UART2: tx=PA2;rx=PA3
- UART3: tx=PB10;rx=PB11
And then the following code:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (HAL_UART_Transmit(&huart3, send, 2, 1000) == HAL_OK)
{
if (HAL_UART_Receive(&huart2, receive, 2, 1000) == HAL_OK)
{
HAL_GPIO_TogglePin(GPIOA, LD2_Pin);
}
}
}
All the boiler plate is generated using cube mx. So the UART 2 and 3 are all default, same for the button pins.
- I connected the transmit pin of uart3 to the receiving pin of uart2
- I connected the receive pin of uart3 to the transmitting pin of uart2 (while not used in this trivial example though)
What am I missing here?
Update
As comments and answers suggested, I shouldn't be using the blocking mode approach. So I rewrote it slightly to rely more on interrupts.
I attached a callback to the nucleo blue button
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == B1_Pin)
{
HAL_UART_Transmit_IT(&huart3, (uint8_t *)send, 2);
}
}
And I added a callback for receiving stuff over uart
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
/* NOTE : This function should not be modified, when the callback is needed,
the HAL_UART_RxCpltCallback can be implemented in the user file
*/
// Toggle the pin to see some proof that I received something
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
// Resubscribe uart2
HAL_UART_Receive_IT(&huart2, (uint8_t *)receive, 2);
}
In the main
function I call the interrupt so that the callback is active.
while (HAL_UART_Receive_IT(&huart2, receive, 2) != HAL_OK);
__NOP();
while (1)
{
}
Again, when debugging through I do end up in the button released callback, but the callback on the uart receive isn't triggered. When I send stuff from the PC, it does. So it seems that sending stuff via uart3 isn't working. So I guess that must be the wiring.
But I really can't see what I am missing here, the pins are clearly defined in the pinout here: https://os.mbed.com/platforms/ST-Nucleo-F103RB/.
I hope the photo is clear enough, but this is how I connected them.
No comments:
Post a Comment