|
|
|
|
|
This is the third tutorial and here you will see how to establish serial communication between the microcontroller and your PC.
|
|
|
|
|
|
More details are explained in the [Serial Communication](Serial-Communication) page.
|
|
|
|
|
|
## Introduction
|
|
|
If you want to visualize the data you read from the sensor on your PC, you can use the *TRACE()* function: it is basically a *printf()* during the debugging, but it performs no action when the production code is released. You can use it as normal *printf()* (it has the same signature). In fact the *printf()* usage is almost prohibited by our [coding guidelines](Coding-Guidelines).
|
|
|
Everytime you call *TRACE()* in your code, the output data is redirected to the default serial port of the microcontroller. The default serial port for the STM32F407VG microcontroller is the `USART3` (default value set by Miosix), which corresponds to the GPIOs `PB10` (TX) and `PB11` (RX). The baud rate is by default set to `19200`.
|
|
|
|
|
|
In order to read the *TRACE()* output you can use a serial to USB adapter and the connections you need are:
|
|
|
- PB10 (TX) -> adapter RX
|
|
|
- PB11 (RX) -> adapter TX
|
|
|
- PB13 -> adapter CTS
|
|
|
- PB14 -> adapter RST
|
|
|
- GND -> adapter GND
|
|
|
|
|
|
Make sure you connect at least RX, TX and GND pins.
|
|
|
If you want to only receive data you can connect only the RX pin of the adapter (or TX if you only want to send data).
|
|
|
|
|
|
Consider that you should set your serial adapter to work with 3.3 volts, since the microcontroller GPIOs work with this voltage level.
|
|
|
|
|
|
You also need a program that is able to read those data from the USB port of your PC. You can use for example *GTKTerm* on Linux or *Putty* on Windows. You have to select the correct serial port (for example `/dev/ttyUSB0`) and set the correct baud rate.
|
|
|
|
|
|
#### Note
|
|
|
Consider that if you are using a different board, it may be possible that its USB connector is also connected to a microcontroller's serial port (i.e. you don't need to connect an external adapter). Just check which serial port is connected to the USB connector (e.g. `USARTX`).
|
|
|
|
|
|
## Writing an entrypoint
|
|
|
|
|
|
Here we will write an entrypoint that uses the *TRACE()* function in order to send data to the PC.
|
|
|
|
|
|
#### hello-skyward.cpp
|
|
|
```cpp
|
|
|
#include <Common.h>
|
|
|
|
|
|
using namespace miosix;
|
|
|
|
|
|
int main()
|
|
|
{
|
|
|
int i = 0;
|
|
|
while (true)
|
|
|
{
|
|
|
TRACE("%d : Hello Skyward! \n", i);
|
|
|
Thread::sleep(1000);
|
|
|
i++;
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
#### sbs.conf
|
|
|
You should also add the entrypoint configuration to `sbs.conf`.
|
|
|
|
|
|
First of all we need an `srcfiles` section in which we add the `Debug.cpp` file, on which the *TRACE()* function depends:
|
|
|
```config
|
|
|
[trace-include]
|
|
|
Type: srcfiles
|
|
|
Files: src/shared/Debug.cpp
|
|
|
```
|
|
|
|
|
|
Also, in order for the *TRACE()* function to act as a normal *printf()*, `DEBUG` has to be defined. We can define it in `sbs.conf` by inserting `-DDEBUG` in the defines section of our entrypoint configuration.
|
|
|
Notice that all the defines specified in `sbs.conf` start with `-D` followed by the string corresponding to the required define: if we want to define `MYDEF`, we will specify `-DMYDEF` in the SBS configuration file.
|
|
|
|
|
|
Moreover the *trace_include* include is needed in order to compile also `Debug.cpp`, on which the *TRACE()* macro depends.
|
|
|
|
|
|
```config
|
|
|
[hello-skyward]
|
|
|
Type: test
|
|
|
BoardId: stm32f407vg_stm32f4discovery
|
|
|
BinName: hello-skyward
|
|
|
Include: %trace-include
|
|
|
Defines: -DDEBUG
|
|
|
Main: hello-skyward
|
|
|
```
|
|
|
|
|
|
## Run it!
|
|
|
Same as before: build the entrypoint with `python sbs -b test-led-driver` and [flash the binary on the board](Flashing-on-a-Target-Board). |
|
|
\ No newline at end of file |