... | ... | @@ -40,7 +40,7 @@ For best results, do not yet connect the UART pins. |
|
|
In the main loop, add some code (and fill in the missing details like includes):
|
|
|
```c
|
|
|
/* Infinite loop */
|
|
|
/* USER CODE BEGIN WHILE */(&huart2, (void*)rx_data, sizeof(rx_data));
|
|
|
/* USER CODE BEGIN WHILE */
|
|
|
while (1)
|
|
|
{
|
|
|
char tx_data[MAX_LEN];
|
... | ... | @@ -52,7 +52,7 @@ In the main loop, add some code (and fill in the missing details like includes): |
|
|
}
|
|
|
tx_data[tx_len] = '\0';
|
|
|
|
|
|
printf("%s -> ", tx_data);
|
|
|
printf("%10s -> ", tx_data);
|
|
|
fflush(stdout); // print immediately, don't buffer output until end of line ('\n')
|
|
|
|
|
|
HAL_UART_Transmit(&huart1, (void*)tx_data, tx_len, 100);
|
... | ... | @@ -66,3 +66,75 @@ In the main loop, add some code (and fill in the missing details like includes): |
|
|
`tx_len` will be initialized by a random int value between 1 and 10.
|
|
|
The array `tx_data` will be filled with ASCII chars for the decimal digits 0 to 9.
|
|
|
The `printf` and `fflush` function will do the logging. To receive the logging output on the ST-LINK host, [redirect stdout](redirect stdout) to UART2 output.
|
|
|
The logging output should look like (keep in mind it is randomized, ymmv)
|
|
|
```
|
|
|
012345 -> 678901 -> 23 -> 456789 -> 01234567 -> 890123 ->
|
|
|
```
|
|
|
A logic analyzer attached to UART1_TX and UART2_TX (logging output) should show a pattern like this:
|
|
|

|
|
|
The leading spaces ' ' of the logging output are not fully visible.
|
|
|
|
|
|
Receiving Data
|
|
|
===
|
|
|
For receiving in interrupt mode, the USART2 interrupt must be enabled in STM32CubeMX.
|
|
|

|
|
|
|
|
|
Define the receiving callback and the error callback in main.c:
|
|
|
```c
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
|
|
#define MAX_LEN 20
|
|
|
|
|
|
char rx_data[MAX_LEN];
|
|
|
|
|
|
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t len)
|
|
|
{
|
|
|
HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
|
|
|
rx_data[len] = '\0';
|
|
|
printf("%s\n", rx_data);
|
|
|
HAL_UARTEx_ReceiveToIdle_IT(&huart2, (void*)rx_data, sizeof(rx_data));
|
|
|
}
|
|
|
|
|
|
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
|
|
{
|
|
|
volatile unsigned error = HAL_UART_GetError(huart);
|
|
|
printf("HAL_UART_ErrorCallback: %d\n", error);
|
|
|
Error_Handler();
|
|
|
}
|
|
|
|
|
|
/* USER CODE END 0 */
|
|
|
```
|
|
|
|
|
|
Trigger the very first receive right above the main while loop:
|
|
|
```c
|
|
|
/* Infinite loop */
|
|
|
/* USER CODE BEGIN WHILE */
|
|
|
HAL_UARTEx_ReceiveToIdle_IT(&huart2, (void*)rx_data, sizeof(rx_data));
|
|
|
while (1)
|
|
|
{
|
|
|
```
|
|
|
|
|
|
Connect the UART2_RX pin to the UART1_TX pin and restart the code.
|
|
|
The LD3 LED should start blinking.
|
|
|
It toggles with each reception.
|
|
|
The logging output should look like:
|
|
|
```
|
|
|
12 -> 12
|
|
|
3 -> 3
|
|
|
456 -> 456
|
|
|
789012 -> 789012
|
|
|
34567 -> 34567
|
|
|
8901 -> 8901
|
|
|
2345678 -> 2345678
|
|
|
901 -> 901
|
|
|
23456 -> 23456
|
|
|
78 -> 78
|
|
|
```
|
|
|
where the right half is printed at the receiver side.
|
|
|
And the logic analyzer:
|
|
|

|
|
|
From left to right:
|
|
|
* printf at the transmitter side
|
|
|
* transmission log "on the wire"
|
|
|
* printf at the receiver side |
|
|
\ No newline at end of file |