|
|
Ingredients
|
|
|
===
|
|
|
Board: Nucelo-STM32L432KC
|
|
|
UART1 is used for transmission, sending data.
|
|
|
UART2 is used for receiving the data with an interrupt handler.
|
|
|
Both interfaces could be used on the same board or on two different boards.
|
|
|
When two boards are used, both GND lines must be connected!
|
|
|
|
|
|
UART2 is already connected on the board to the VCP (USB) UART of the ST-LINK.
|
|
|
We use UART2_TX for logging output and route UART2_RX to another available pin:
|
|
|
```
|
|
|
PA9 ------> USART1_TX
|
|
|
PA10 ------> USART1_RX
|
|
|
PA2 ------> USART2_TX
|
|
|
PA3 ------> USART2_RX
|
|
|
```
|
|
|
|
|
|
Create a new STM32CubeMX project
|
|
|
===
|
|
|
Create a new C project with all default settings.
|
|
|
|
|
|
Build and run (debug) the project on the board
|
|
|
===
|
|
|
Using your favorite IDE to check your software and hardware setup.
|
|
|
After successfully testing, it is a good fime to create (init) a git repository for the source code!
|
|
|
|
|
|
STM32CubeMX Connectivity: activate UART1
|
|
|
===
|
|
|
The most common 115200 8N1 settings are used. The UART is configured in full-duplex mode.
|
|
|

|
|
|
|
|
|
STM32CubeMX Connectivity: activate UART2
|
|
|
===
|
|
|
The UART2 config should match the UART1 config for proper communication. The UART2 config must also match to what the VCPUART expects for logging. So we stick to 115200 8N1 here too.
|
|
|

|
|
|
|
|
|
Sending Data
|
|
|
===
|
|
|
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));
|
|
|
while (1)
|
|
|
{
|
|
|
char tx_data[MAX_LEN];
|
|
|
int tx_len = 1 + random() % 9;
|
|
|
static int k;
|
|
|
for(int i=0; i<tx_len; ++i) {
|
|
|
tx_data[i] = '0' + k;
|
|
|
k = (k + 1) % 10;
|
|
|
}
|
|
|
tx_data[tx_len] = '\0';
|
|
|
|
|
|
printf("%s -> ", 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);
|
|
|
HAL_Delay(100);
|
|
|
/* USER CODE END WHILE */
|
|
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
|
}
|
|
|
/* USER CODE END 3 */
|
|
|
```
|
|
|
`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. |
|
|
\ No newline at end of file |