|
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.
|
|
More details are explained in the [Serial Communication](Serial-Communication) page.
|
|
|
|
|
|
## Introduction
|
|
## 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).
|
|
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`.
|
|
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:
|
|
In order to read the _TRACE()_ output you can use a serial to USB adapter and the connections you need are:
|
|
|
|
| STM32F407VG | Adapter |
|
|
| STM32F407VG | Adapter |
|
|
|-------------|---------|
|
|
|--------------|---------|
|
|
| PB10 (TX) | RX |
|
|
| PB10 (TX) | RX |
|
|
| PB11 (RX) | TX |
|
|
| PB11 (RX) | TX |
|
|
| PB13 | CTS |
|
|
| PB13 | CTS |
|
|
| PB14 | RST |
|
|
| PB14 | RST |
|
|
| GND | GND |
|
|
| GND | GND |
|
|
|
|
|
|
Make sure you connect at least `RX`, `TX` and `GND` pins.\
|
|
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).
|
|
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.
|
|
Consider that you should set your serial adapter to work with 3.3 volts, since the microcontroller GPIOs work with this voltage level.
|
... | @@ -25,13 +24,12 @@ Consider that you should set your serial adapter to work with 3.3 volts, since t |
... | @@ -25,13 +24,12 @@ Consider that you should set your serial adapter to work with 3.3 volts, since t |
|
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.
|
|
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
|
|
#### 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`).
|
|
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
|
|
## 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:
|
|
In `src/entrypoints` add a file called `hello-skyward.cpp`.
|
|
|
|
Inside it you can put your code:
|
|
|
|
|
|
|
|
```cpp
|
|
```cpp
|
|
/* Copyright (c) <YEAR_HERE> Skyward Experimental Rocketry
|
|
/* Copyright (c) <YEAR_HERE> Skyward Experimental Rocketry
|
... | @@ -56,7 +54,8 @@ Inside it you can put your code: |
... | @@ -56,7 +54,8 @@ Inside it you can put your code: |
|
* THE SOFTWARE.
|
|
* THE SOFTWARE.
|
|
*/
|
|
*/
|
|
|
|
|
|
#include <Common.h>
|
|
#include <miosix.h>
|
|
|
|
#include <utils/Debug.h>
|
|
|
|
|
|
using namespace miosix;
|
|
using namespace miosix;
|
|
using namespace Boardcore;
|
|
using namespace Boardcore;
|
... | @@ -76,7 +75,8 @@ int main() |
... | @@ -76,7 +75,8 @@ int main() |
|
```
|
|
```
|
|
|
|
|
|
#### Note
|
|
#### Note
|
|
You can also send data from the PC to the microcontroller.
|
|
|
|
|
|
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
|
|
```cpp
|
... | @@ -124,6 +124,7 @@ int main() |
... | @@ -124,6 +124,7 @@ int main() |
|
```
|
|
```
|
|
|
|
|
|
## Compile with SBS
|
|
## Compile with SBS
|
|
|
|
|
|
You should also add the entrypoint configuration to `CMakeLists.txt`:
|
|
You should also add the entrypoint configuration to `CMakeLists.txt`:
|
|
|
|
|
|
```cmake
|
|
```cmake
|
... | @@ -135,6 +136,6 @@ sbs_target(hello-skyward stm32f407vg_stm32f4discovery) |
... | @@ -135,6 +136,6 @@ sbs_target(hello-skyward stm32f407vg_stm32f4discovery) |
|
|
|
|
|
In order for the _TRACE()_ function to act as a normal _printf()_, we need to compile the entrypoint with debug enabled. To enable debug you can add the option `-d` when you build with the SBS script:
|
|
In order for the _TRACE()_ function to act as a normal _printf()_, we need to compile the entrypoint with debug enabled. To enable debug you can add the option `-d` when you build with the SBS script:
|
|
|
|
|
|
```sh
|
|
```shell
|
|
./sbs -d -f test-led-driver
|
|
./sbs -d -f hello-skyward
|
|
``` |
|
``` |
|
|
|
\ No newline at end of file |