Août 26

Robotics 101 : Serial Driver, the basics



Well, we are now able to do so cool stuff with GPIOs and PWM, but we still lack something really important : communication. As a matter of fact you cannot communicate at all right now, and that’s what we will work on with the serial driver.
But first we will discover UART is.

What is UART :

Wikipedia tells us (read it, it’s interesting) :

A Universal Asynchronous Receiver/Transmitter, abbreviated UART /ˈjuːɑrt/, is a piece of computer hardware that translates data between parallel and serial forms. UARTs are commonly used in conjunction with communication standards such as EIA, RS-232, RS-422 or RS-485. The universal designation indicates that the data format and transmission speeds are configurable. The electric signaling levels and methods (such as differential signaling etc.) are handled by a driver circuit external to the UART.
A UART is usually an individual (or part of an) integrated circuit used for serial communications over a computer or peripheral device serial port. UARTs are now commonly included in microcontrollers. A dual UART, or DUART, combines two UARTs into a single chip. Many modern ICs now come with a UART that can also communicate synchronously; these devices are called USARTs (universal synchronous/asynchronous receiver/transmitter).

In this course we are only going to see two types of UART :

  • Full-duplex
  • Half-duplex

The main difference is in the number of wires : the full-duplex uses 3 (tx, rx, gnd) and the half duplex uses only 2 (data and gnd). In full-duplex the two connected devices send information on separated wires (from tx of device 1 to rx of device to, the same applies to tx of the second device), this way there are no risks of information collision. Whereas in half-duplex the two devices send information on the same shared wire (data) so you have to be careful about when to send data (just like a in phone-call to a faraway location).

When using UART, the devices must use the same frequency (baud rate) to send the data bytes, so that the receiver samples the signal at the same frequency it was transmitted. Sharing the same baud rate is really important, a problem in this usually ends up in all the data being unreadable.

What is chibiOS’s serial driver :

ChibiOS’s Serial Driver uses the UART peripheral, it is a very simple API to use and allows to quickly communicate through UART. Giovani (chibiOS’s creator) presented it as follows:

The serial driver is an interrupt-driven driver implementing circular buffers for input and output, the access is through classic read/write/get/put functions.

What matters to us is that it is an easy way to use UART quickly. It is also a good way to learn how to use a new driver of chibiOS.

SerialConfig :

Just as the PWMDriver, the Serial as to be configured. There are many options, but in order to have a working serial driver only the first one is really needed.
This single parameter is the baud rate : it is written in bits per second (actual it is in symbol per seoncd, but we only send one symbol at a time).
Sample code :
static SerialConfig serialCfg =
9600 // bit rate
This should give you a compilation warning as some parameters are missing. Putting 0,0,0 as last parameter makes them disappear, even though i have honestly no idea what they are…
Nevertheless you have to be careful about the chosen baud rate as it mus be a divisor of the UART clock (8MHz for the stm32f407, you can find it in the board.h file), most of all the two devices must be able to produce this baud rate (usually if a 15$ board can make it, your computer can, but you still have to look for it while troubleshooting).

Minicom :

First, you will need a usb-serial adapter (or cable) in order to link your computer to the board and will usually be recognized as ttyUSBX (where X is a number). That is to say that the data sent by the board will be written in /dev/ttyUSBX.
As the X might be different ecery time, the simplest way to know which one it is for sure is to unplug everything in usb then replug everything starting by this cable (X is the number in which it was plugged) this way it will be named ttyUSB0. You can also check which devices are counted as serial ones by typing :
ls /dev/
If the only ttyUSBX is ttyUSB0 then your cable can only be ttyUSB0.
Minicom is a serial communication program. It allows you to communicate with your board : it handles both receiving and transmitting messages.
Before the first use you will also have to configure it. Type in a shell :
minicom -s
you should see the following thing :
Untitled drawing
As i did not manage to get my hands on an english version i will have to give you approximate translations.
First choose the third configuration : something like serial port configuration.
Type A in and confirm to select the default serial port listened to, and type in /dev/ttyUSB0.
You can also configure the baud rate with the E option, if you want to be able to communicate with the other serial device, make sure to select the proper baud rate.
Eventually go back to the main config menu (type validate without any parameters) and chose « save config under dsl ».

Warning: minicom will only read ascii encoded messages which is enough for us right now, but must be known (to see the bytes, use od -t x1 /dev/ttyUSB0 instead of minicom, it uses the same baud rate as the one defined in minicom)

That’s about it for minicom.