|
|
Serial communication can be acheived using the Miosix drivers for one of the open USARTs. To communicate with the USARTs from your PC you will need a USB-TTL driver, or you can do it via the Discovery's USB cable if the feature is enabled in your Discovery board.
|
|
|
Serial communication can be achieved using the Miosix drivers for one of the open USARTs. This page describes how the serial drivers work and how to access them. To communicate on a USART from your PC you will need a USB-TTL driver, or you can do it via the Discovery's USB cable if the feature is enabled in your Discovery board.
|
|
|
|
|
|
As a default, in the STM32 Discovery boards Miosix opens 3 USARTs: one *default usart* (`printf()` and `scanf()` are redirected here) and two additional ones.
|
|
|
## Default Serial Port
|
|
|
|
|
|
### Open USARTs
|
|
|
|
|
|
In `arch/common/drivers/serial_stm32.cpp` you can find how the 3 Usarts that Miosix lets you use are configured.
|
|
|
|
|
|
| USART | TX | RX | CTS | RTS |
|
|
|
|--------|-----|-----|------|-------|
|
|
|
| 1 | PA9 | PA10 | PA11 | PA12 |
|
|
|
| 2 | PA2 | PA3 | PA0 | PA1 |
|
|
|
| 3 | PB10 | PB11 | PB13 | PB14 |
|
|
|
|
|
|
The **default USART** for the boards are set to:
|
|
|
|
|
|
| BOARD | DEFAULT | AUXTTY |
|
|
|
| ------- | --------------- | -------- |
|
|
|
|STM32F429zi| 1 | / |
|
|
|
|STM32F407vg| 3 | 2 |
|
|
|
|
|
|
### Default USART Configuration
|
|
|
|
|
|
The **default serial** for a certain board is defined in `miosix/config/arch/<your_arch>/<your_board>/board_settings.h`: here you can find the default baudrate and other related configurations:
|
|
|
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;
|
|
|
const unsigned int defaultSerial=1; //default serial = USART1
|
|
|
const unsigned int defaultSerialSpeed=19200; //baudrate
|
|
|
const bool defaultSerialFlowctrl=false; //use rts/cts
|
|
|
|
|
|
#define SERIAL_1_DMA //use the DMA
|
|
|
```
|
|
|
|
|
|
The default serial port is opened in the *IRQBspInit()* method
|
|
|
inside `miosix/arch/<your arch>/<your board>/interfaces-impl/bsp.cpp`
|
|
|
> :warning: **WARNING: printfs are 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.**
|
|
|
|
|
|
```cpp
|
|
|
DefaultConsole::instance().IRQset(intrusive_ref_ptr<Device>(
|
|
|
new STM32Serial(defaultSerial,defaultSerialSpeed,
|
|
|
defaultSerialFlowctrl ? STM32Serial::RTSCTS : STM32Serial::NOFLOWCTRL)));
|
|
|
```
|
|
|
## Open USARTs
|
|
|
|
|
|
### Auxtty USART Configuration (STM32F407VG only)
|
|
|
For the *f407vg* board, there's also another serial port, called **auxtty**: you need to remove the comment from the `board_settings.h` in order to use it:
|
|
|
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 that Miosix lets you use. Here is the pinout:
|
|
|
|
|
|
```cpp
|
|
|
//#define AUX_SERIAL "auxtty" UNCOMMENT THIS TO OPEN THE SERIAL
|
|
|
const unsigned int auxSerialSpeed=9600;
|
|
|
const bool auxSerialFlowctrl=false;
|
|
|
```
|
|
|
| USART | TX | RX | CTS | RTS | STM32F429zi | STM32F407vg |
|
|
|
|--------|------|-----|------|-------|-------------|-------------|
|
|
|
| 1 | PA9 | PA10 | PA11 | PA12 | default | |
|
|
|
| 2 | PA2 | PA3 | PA0 | PA1 | | |
|
|
|
| 3 | PB10 | PB11 | PB13 | PB14 | | default |
|
|
|
|
|
|
|
|
|
## Other Serial ports
|
|
|
|
|
|
Miosix gives the possibility to access other serial 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:
|
|
|
|
|
|
This port is opened in the *bspInit2()* method
|
|
|
inside `miosix/arch/<your arch>/<your board>/interfaces-impl/bsp.cpp`
|
|
|
```cpp
|
|
|
void bspInit2()
|
|
|
{
|
|
|
#ifdef WITH_FILESYSTEM
|
|
|
#ifdef AUX_SERIAL
|
|
|
intrusive_ref_ptr<DevFs> devFs=basicFilesystemSetup(SDIODriver::instance());
|
|
|
devFs->addDevice(AUX_SERIAL,
|
|
|
intrusive_ref_ptr<Device>(new STM32Serial(2,auxSerialSpeed,
|
|
|
auxSerialFlowctrl ? STM32Serial::RTSCTS : STM32Serial::NOFLOWCTRL)));
|
|
|
#else //AUX_SERIAL
|
|
|
basicFilesystemSetup(SDIODriver::instance());
|
|
|
#endif //AUX_SERIAL
|
|
|
#endif //WITH_FILESYSTEM
|
|
|
#ifdef WITH_FILESYSTEM
|
|
|
/**
|
|
|
* Pointer to file system.
|
|
|
*/
|
|
|
intrusive_ref_ptr<DevFs> devFs = basicFilesystemSetup(SDIODriver::instance())
|
|
|
|
|
|
/**
|
|
|
* Open serial port, accessible as '/dev/gps', on USART2 with baudrate 115200.
|
|
|
*/
|
|
|
devFs->addDevice("gps", intrusive_ref_ptr<Device>(new STM32Serial(2,115200)));
|
|
|
#endif //WITH_FILESYSTEM
|
|
|
}
|
|
|
```
|
|
|
|
|
|
### Usage
|
|
|
|
|
|
**Discovery**
|
|
|
After that, you can easily use the device as follows:
|
|
|
|
|
|
To read or write in the default USART, you can use the standard input and output (`printf()` and `scanf()`).
|
|
|
|
|
|
If other serial ports are open, you can use them like in this example:
|
|
|
```cpp
|
|
|
#include <fcntl.h>
|
|
|
//...
|
|
|
int fd=open("/dev/auxtty",O_RDWR); //open serial port named auxtty
|
|
|
int fd=open("/dev/gps",O_RDWR); //open serial port named gps
|
|
|
if(fd<0) printf("Cannot open /dev/auxtty");
|
|
|
|
|
|
char buffer[11]; //Buffer is 10 chars + '\0'
|
... | ... | @@ -86,7 +61,16 @@ write(fd, buffer, 10); //Write 10 chars |
|
|
```
|
|
|
Pay attention to the baudrate at which the used port is set!
|
|
|
|
|
|
**PC**
|
|
|
## Auxtty USART Configuration (STM32F407VG only)
|
|
|
For the *f407vg* board, there's also another serial port, called **auxtty**: you need to remove the comment from the `board_settings.h` in order to use it:
|
|
|
|
|
|
```cpp
|
|
|
//#define AUX_SERIAL "auxtty" UNCOMMENT THIS TO OPEN THE SERIAL
|
|
|
const unsigned int auxSerialSpeed=9600;
|
|
|
const bool auxSerialFlowctrl=false;
|
|
|
```
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
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).
|
|
|
|
... | ... | |