|
|
|
|
|
This is the third tutorial and here you will see how to establish serial communication between the microcontroller and your PC.
|
|
|
|
|
|
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:
|
|
|
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:
|
|
|
|
|
|
| STM32F407VG | Adapter |
|
|
|
|--------------|---------|
|
... | ... | @@ -18,18 +18,18 @@ In order to read the *TRACE()* output you can use a serial to USB adapter and th |
|
|
| GND | 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.
|
|
|
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`).
|
|
|
|
|
|
## Create your entrypoint
|
|
|
|
|
|
Here we will write an entrypoint that uses the *TRACE()* function in order to send data to the PC.
|
|
|
Here we will write an entrypoint that uses the _TRACE()_ function in order to send data to the PC.
|
|
|
In `src/entrypoints` add a file called `hello-skyward`.cpp`.
|
|
|
Inside it you can put your code:
|
|
|
|
... | ... | @@ -55,7 +55,7 @@ int main() |
|
|
|
|
|
#### Note
|
|
|
You can also send data from the PC to the microcontroller.
|
|
|
This can be done through the *scanf()* function:
|
|
|
This can be done through the _scanf()_ function:
|
|
|
```cpp
|
|
|
#include <Common.h>
|
|
|
|
... | ... | @@ -78,19 +78,19 @@ int main() |
|
|
```
|
|
|
|
|
|
## Compile with SBS
|
|
|
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:
|
|
|
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.
|
|
|
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]
|
... | ... | |