... | ... | @@ -2,7 +2,7 @@ This page describes how the serial drivers work and how to access them. |
|
|
|
|
|
## Using `stdio.h` (*printf()*, *scanf()*, ...)
|
|
|
|
|
|
When you use functions like *printf()*, *scanf()*, *puts()*, *gets()* etc... inside your code, MIOSIX redirects them to one of the microcontroller's serial ports, called the **default** serial port. This is defined in `miosix/config/arch/<your_arch>/<your_board>/board_settings.h`, along with the default baudrate and other related configurations. The file looks like this:
|
|
|
When you use functions like *printf()*, *scanf()*, *puts()*, *gets()* etc... inside your code, Miosix redirects them to one of the microcontroller's serial ports, called the **default** serial port. This is defined in `miosix/config/arch/<your_arch>/<your_board>/board_settings.h`, along with the default baudrate and other related configurations. The file looks like this:
|
|
|
|
|
|
```cpp
|
|
|
const unsigned int defaultSerial=1; //default serial = USART1
|
... | ... | @@ -14,18 +14,20 @@ const bool defaultSerialFlowctrl=false; //use rts/cts |
|
|
|
|
|
> :warning: **WARNING: *printf()* is very heavy and can significantly modify the timings if used inside drivers. For this reason, we suggest instead the use of *TRACE()* throughout the code. *TRACE()* works as a normal *printf()* if *DEBUG* is defined (see below example), otherwise it does not perform any operation.**
|
|
|
|
|
|
You can find an example in the [Hello Skyward tutorial](Hello-Skyward) page.
|
|
|
|
|
|
## Using `fnctl.h` (other serial ports)
|
|
|
|
|
|
A part from the default serial port, Miosix offers access to up to 3 USARTs in STM32 microcontrollers. In `arch/common/drivers/serial_stm32.cpp` you can find the 3 USARTs enabled by default. Here is the pinout:
|
|
|
|
|
|
| USART | TX | RX | CTS | RTS | STM32F429zi | STM32F407vg |
|
|
|
| USART | TX | RX | CTS | RTS | STM32F429ZI | STM32F407VG |
|
|
|
|--------|------|-----|------|-------|-------------|-------------|
|
|
|
| 1 | PA9 | PA10 | PA11 | PA12 | default | |
|
|
|
| 2 | PA2 | PA3 | PA0 | PA1 | | |
|
|
|
| 3 | PB10 | PB11 | PB13 | PB14 | | default |
|
|
|
|
|
|
Miosix gives the possibility to access these USART ports through Unix-like [device files](https://en.wikipedia.org/wiki/Device_file). In particular, to access a particular USART of your microcontroller (e.g. USART2) you should modify the *bspInit2()* method
|
|
|
inside `miosix/arch/<your arch>/<your board>/interfaces-impl/bsp.cpp` as follows:
|
|
|
inside the [Board Support Package](BSP) as follows:
|
|
|
|
|
|
```cpp
|
|
|
void bspInit2()
|
... | ... | @@ -65,7 +67,6 @@ To communicate with a board from your PC you will need a USB-TTL driver, or you |
|
|
Once your USB-TTL dongle is attached to the PC, you will need to connect the RX and TX of the dongle to the TX and RX of the decided serial port in your Discovery (**ATTENTION:** RX discovery<-->TX usb dongle and vice-versa).
|
|
|
|
|
|
Then you can open a communication using:
|
|
|
|
|
|
- **Putty** in Windows: Select the *Session* tab on the left panel and select *Serial* as "Connection type". You will need to know the COM port to which the Discovery is attached (you can find it out in *Control Pane > Device Manager*).
|
|
|
- **GTKTerm** on Linux: *Configuration* > *Port* > `/dev/ttyUSB0` and select baudrate (19200 for the default serial). You can also use `screen` or `minicom`.
|
|
|
|
... | ... | @@ -80,40 +81,4 @@ For the *f407vg* board, there's also another serial port, called **auxtty**: you |
|
|
//#define AUX_SERIAL "auxtty" UNCOMMENT THIS TO OPEN THE SERIAL
|
|
|
const unsigned int auxSerialSpeed=9600;
|
|
|
const bool auxSerialFlowctrl=false;
|
|
|
```
|
|
|
|
|
|
### Example
|
|
|
Usage of *TRACE()* in order to send data to the PC.
|
|
|
|
|
|
#### test-serial.cpp
|
|
|
```cpp
|
|
|
#include <Common.h>
|
|
|
|
|
|
using namespace miosix;
|
|
|
|
|
|
int main()
|
|
|
{
|
|
|
int i = 0;
|
|
|
while (true)
|
|
|
{
|
|
|
TRACE("%d : Serial is working!\n", i);
|
|
|
Thread::sleep(1000);
|
|
|
i++;
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
#### sbs.conf
|
|
|
You should also add the following configuration to `sbs.conf`.
|
|
|
```config
|
|
|
[test-serial]
|
|
|
Type: test
|
|
|
BoardId: *your-board-id* # e.g. stm32f407vg_stm32f4discovery
|
|
|
BinName: test-serial
|
|
|
Include: %shared
|
|
|
Defines: -DDEBUG
|
|
|
Main: test-serial
|
|
|
```
|
|
|
The *-DDEBUG* define is necessary in order for the *TRACE()* function to act as a normal *printf()*.
|
|
|
Moreover the *shared* include is needed in order to compile also `Debug.cpp`, on which the *TRACE()* macro depends. |
|
|
\ No newline at end of file |
|
|
``` |
|
|
\ No newline at end of file |