In many cases, we want to display values for counters, sensors, and
other debugging information. Although there is an LCD on the discovery kit, it can only display six characters, simultaneously.
Fortunately, we have another nice option that uses the ST-Link V2.1 interface.
This option does not require any extra hardware. You can see there is only one USB cable connected here.
We are not using the other USB cable.
The main function of ST-Link is to flash and debug the ARM processor through the
mini-USB interface. In addition, ST-Link V2.1 also provides a USB virtual com port.
This virtual-com port, also called UART port, allows PC and the discovery kit to exchange information via a terminal emulator.
Let's take a look at the schematics.
This is ST-Link V2.1.
In fact, it is a Cortex-M0 processor.
Let's pay attention to pin PA.2, PA.3.
PA.3, ST-Link RX
There are two possible connections.
UART TX and MFX UART3 TX
PA.2, ST-Link TX,
There are two possible connections. MFX UART3 RX, UART RX.
And those connections are controlled by four solder bridges. SB 13, 16, 15, 17
By default, the solder bridge 13 and 16 are soldered. Therefore, PA.2 is connected to UART RX,
PA. 3 is connected to UART TX.
Let's take a look at UART RX and UART TX.
If we go to the next page of the schematics, you can see here. UART TX, UART RX,
They are connected to pin PD.5 and PD.6, respectively.
Let's examine what alternative function PD.5 and PD.6 should be.
If you go to the textbook website,
go to the example labs
Look at the STM Discovery Kit with STM32L4,
If we take a look at the pin functions. You can either look at the excel file or pdf.
Let's take a look at the pdf.
The first row shows the alternative function number from AF0 to AF15.
The first column shows the pin name, PA0, PA1, ...,
Let's take a look at PD.5 and PD.6.
Here are PD.5 and PD.6.
They should be set to UART 2 TX, UART 2 RX.
Let's go all the way up to look at what alternative function number it should be.
It should be alternative function number 7.
Let's take a look at the example code.
Here is the demo code.
In this demo code, I initialize the system clock,
initialize LED, and initialize UART 2.
Let's take a look at UART.
The UART is PD.5, PD6, as UART 2.
That is the transmitter and receiver.
Set as alternative function 7.
And the UART initialization
I initialize the UART 2 as asynchronous mode, 9600 baud rate, no parity, 8 data bits, 1 stop bit, no hardware flow control.
Now let's go back to the main program.
In the main program,
I use sprintf to print an integer a into a string buffer,
and then a floating-point number b into the string buffer.
and then use UART_Write function to write the data to the UART port.
then update a and b, for loop delay, toggle a LED
Let's what you see here.
The board is running. The LED is flashing, and the data is printed here.
Let's see how we set up the terminal emulator.
I am using Tera Term as an example. Let me close it and restart.
Let me download the code to stop the program.
close the terminal,
When you plug your discovery kit to your computer,
It will have a com port.
On my computer, it shows as ST-Link virtual com 3.
It might be a different com number on your computer.
So let me open the terminal.
and I select serial
I will select com 3, ST-Link
Click OK
And let me change the font size. Make it a little bit larger.
Before I start,
make sure that you have the right setting.
For example, here, if you go to the set up
serial port
It has com 3, 9600, 8 bit, none parity, 1 stop bit, no hardware flow control
So if I click
If I download the program
Let me download the program
and click the reset button
You see, the information is printed out on the terminal.
This example show you a nice way for you to print debug information on the terminal.
Next, I am going to show you how we can
transfer information from the virtual terminal to the board.
So let me comment out this code.
and uncomment this part of code.
save it
compile it
at the same time, I am going to clear the buffer.
Let me download the program.
clear buffer
Let me reset the processor.
and look at the code here
This one first uses UART_Write() to print a string.
Basically, require user's input
provide a hint for user to type, and
to turn off the LED,
or "Y" to turn on the LED
After we write this string to the terminal,
we are going to use USART_Read() to receive a byte from the terminal.
If we type "N", it will turn off the LED.
confirm LED has been turned off by using USART_Write()
If it is "Y", then turn on the LED
confirm that LED has been turned on.
Let's me show you the demo.
Let me highlight this window.
I am going to type "Y",
You see here, LED is on.
I am going to type "N", LED is off.
So, look at here, the LED.
"Y", on; "N", off
This example shows that we can also send information from the terminal to the discovery kit.
Let's go back and look at the schematic file.
By default, the ST-Link UART TX and RX are connected to UART RX and UART TX.
There is another possible connection.
We can connect them to MFX UART3 RX and MFX UART3 TX, respectively.
So what are they?
MFX stands for multiple function units.
Let's look at the board.
And in fact this is MFX.
It is another Cortex-M processor.
It is put there to measure the power consumption of STM32L4.
We can also let ST-Link to talk to this processor by changing the solder bridges.
in stead of talking to this one
But one thing we need to pay attention is
in this example, I use polling method to send data and receive data.
If you look at here,
In USART_Read(), I wait until the reciever buffer is not empty.
When I send the data, I will wait until the transmission buffer is empty.
Polling method is very inefficient.
We cannot this method too often in a busy program,
because this will impact your application performance.
To improve the efficiency
We can rewrite the code by using UART interrupts or DMA.
And if needed, we might even need a real-time operating systems to reduce the impact of UART communication.