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 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.
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 |
---|---|
PB10 (TX) | RX |
PB11 (RX) | TX |
PB13 | CTS |
PB14 | RST |
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.
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. In src/entrypoints
add a file called hello-skyward.cpp
. Inside it you can put your code:
/* Copyright (c) <YEAR_HERE> Skyward Experimental Rocketry
* Author: <YOUR_NAME_HERE>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <miosix.h>
#include <utils/Debug.h>
using namespace miosix;
using namespace Boardcore;
int main()
{
int i = 0;
while (true)
{
TRACE("%d : Hello Skyward! \n", i);
Thread::sleep(1000);
i++;
}
return 0;
}
Note
You can also send data from the PC to the microcontroller.
This can be done through the scanf() function:
/* Copyright (c) <YEAR_HERE> Skyward Experimental Rocketry
* Author: <YOUR_NAME_HERE>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <Common.h>
using namespace miosix;
using namespace Boardcore;
int main()
{
int i;
while (true)
{
scanf("%d", &i);
if (i > 10)
{
TRACE("Hello Skyward! \n");
}
}
return 0;
}
Compile with SBS
You should also add the entrypoint configuration to CMakeLists.txt
:
add_executable(hello-skyward src/entrypoints/hello-skyward.cpp)
sbs_target(hello-skyward stm32f407vg_stm32f4discovery)
Run it!
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:
./sbs -d -f hello-skyward