3/31/2020 Stm32 Serial Communication Protocols
Jul 06, 2018 The STM32’s UART is designed to implement many serial protocols: for example, it implements two different kinds of binary encoding which are the Unipolar NRZL and the Manchester Code. In the first case a ‘1’ is represented by a VDD and a ‘0’ by a GND, in the second case, a data signal is encoded in the raising and falling edges. The USART is a very flexible serial interface that supports:. Asynchronous UART communication,. SPI (serial peripheral interface) master mode, and. LIN (local interconnect network) mode. It can also interface with ISO/IEC 7816 smartcards and IrDA devices.
![]()
In the last post about communication we discussed what it took to build up the ESP tool chain as well as the STM32 Toolchain. We loaded the esp-link interface on to the esp and a simple serial communication program onto the STM32 and had the two micro-controllers communicate. It was amazing but for the Sunleaf team sending data between micro-controllers just wasn't enough. We are building a next generation internet of things device. This means it has to work with a cloud system. This also means we need to be able to do basic REST commands. If you need to know more about REST commands I added a link here.
The esp-link supports micro-controller REST Commands but only though a Serial Line Internet Protocol SLIP interface. This was a surprise. Initially we were planning on using simple AT commands to 'GET' and 'POST' data. But there are a few issues with this. The basic serial communication protocol is error prone. Even with some nice libraries that do error checking we still experienced a few errors in the communication. It is also slower. We really though the right way to build this up is to add a SLIP interface to the STM32 so we can communicate with the esp-link and the rest of the internet reliably and quickly. In this log we first discuss what SLIP is. Then we'll cover the port we are using for the STM and give a working example.
When I first realized that I needed a SLIP interface I was hopeful. The STM32F446RE is a fairly common chip. There had to be someone working on an open source version that I could use. I couldn't find one. There are a lot of closed source versions available, if you have between $10K-$30K to drop on a license... But if that's you then you probably aren't reading this now. This definitely isn't us. So we decided we needed to build our own. To do this we needed to learn a little more about SLIP, why it exists and why is it used?
The SLIP protocol consists of commands with binary arguments sent from the attached the STM32 to the esp8266. The ESP8266 then performs the command and responds back. The responses back use a callback address in the attached STM-Client code. For example the command sent by the STM32 contains a callback address and the response from the esp8266 starts with that callback address. This enables asynchronous communication where esp-link can notify the STM32 when requests complete or when other actions happen, such as wifi connectivity status changes or RSSI Values...
However, there is no true internet standard for Serial Line IP. There is a good deal of work done on this still. It is intended to work as a point-to-point serial connection for TCP/IP connections.
SLIP is commonly used on dedicated serial links but has been replaced on many CPUs with PPP. For embedded systems like the STM32F+ESP8266 system it works well. It is also nice because I don't have to post my SSID and wifi password wit this configuration. This is because SLIP allows mixes of hosts and routes to communicate with one another (host-host, host-router and router- router are all common SLIP network configurations).
Basically it is a simple two layer protocol providing basic IP framing. Here is a diagram of how the protocol is supposed to work:
Here is a summary of SLIP:
Kinda nifty? Well, not really. It is very old but for an embedded system to post reliable REST requests it is top-notch. Next we talk about how it is implemented in the STM32F446RE Board.
As we discussed we've loaded a sun leaf version of the esp-link software onto the ESP. Here is where the source is kept: https://github.com/jeelabs/esp-link. Now, versions of esp-link use the SLIP protocol over the serial link to support simple outbound HTTP REST requests and an MQTT client. There are examples of these libraries for REST and MQTT libraries as well as demo sketches in the el-client repository. These only work on Arduino boards and I think a few NXP boards. If you want to use a real micro-controller you'll have to port some of this to C. Which is what I did...so I guess you don't have to any more...
The homepage for the mbed repository: https://developer.mbed.org/users/ShaneKirkbride/code/STM-Client/
Again keeping in mind that is is largely ported from the EL-Client repository here are some highlights about how this port works. First there are two serial objects that the client requires: Serial and DebugSerial. This is extreamly helpful because you are essentially blind to what is going on inside the STM32 once the program kicks off unless you use the debugSerial object. In the El-Client repo there is no true main.cpp for the rest example. This is because the program is intended for the Arduino. So the first thing we did was make our own. It was a very similiar example. The main difference is that the STM32 (or maybe the mbed API...) dosen't support asynchronous communication. There are a lot of stream objects in the Arduino code that don't port correctly to C.
This is fairly easy to overcome by Arduino the serial objects in main and changing the stream* datatype classes to serial* datatypes. This was really the biggest catch. Once I figured this out the only other trick was to reset the ESP8266. A lot of research went into figuring out how to do this correctly. I almost ported a LWIP library but found the STM-Client to work better.
Here is a screen shot of the system sending a 'get' request to www.timeapi.org.
I used putty...but you can use any serial output. This text is all through the serial debug port.
There is also a 'post' example that is commented out in the code for posttestserver.com. This also works but I'm working on this to get it talking to vivaplanet right now so it has limited functionality.
There are a few more firmware goals we are going to address in the next two months. The first will be posting to a cloud based server: vivaplanet. Posting to something like an Azure cloud service is non-trival for a platform but I'm confident it can be done. The second is OTA programming the STM32. This is also non-trival and may require a hardware change to the rev 2 board. Lastly, I need to get the Kalman filter engine working. If we're lucky we'll get to all of these topics before October. We also need a rev 2 board and a rugged mechanical housing but that will also be coming soon to a log entry near you.
If we remember the old computer parts like printer, mouse, the keyboard is associated with the help of connectors. The communication process between the computer and these parts could be done using the UART. Universal Serial Bus (USB) has changed all kinds of communication principles on computers. But, UART is still used in the above-declared applications. Approximately all types of microcontroller architectures have built-in UART hardware due to serial communication and use only two cables for communication. This article discusses what UART, How UART Works, the difference between serial and parallel communication, UART block diagram, UART communication, UART interfacing, Applications, Advantages, and Disadvantages.
What is UART?
The UART full form is “Universal Asynchronous Receiver/Transmitter”, and it is an inbuilt IC within a microcontroller but not like a communication protocol (I2C & SPI). The main function of UART is to serial data communication. In UART, the communication between two devices can be done in two ways namely serial data communication and parallel data communication.
Serial and Parallel Communication
In serial data communication, the data can be transferred through a single cable or line in a bit- by- bit form and it requires just two cables. Serial data communication is not expensive when we compared with parallel communication. It requires very less circuitry as well as wires. Thus, this communication is very useful in compound circuits compared with parallel communication.
In parallel data communication, the data can be transferred through multiple cables at once. Parallel data communication is expensive as well as very fast, as its requires additional hardware and cables. The best examples for this communication are old printers, PCI, RAM, etc.
UART Block Diagram![]()
The UART block diagram consists of two components namely the transmitter & receiver that is shown below. The transmitter section includes three blocks namely transmit hold register, shift register and also control logic. Likewise, the receiver section includes a receive hold register, shift register, and control logic. These two sections are commonly provided by a baud-rate-generator. This generator is used for generating the speed when the transmitter section & receiver section has to transmit or receive the data.
The hold register in the transmitter comprises the data-byte to be transmitted. The shift registers in transmitter and receiver move the bits to the right or left till a byte of data is transmitted or received. A read (or) write control logic is used for telling when to read or write.
The baud-rate-generator among the transmitter and the receiver generates the speed that ranges from 110 bps to 230400 bps. Typically, the baud rates of microcontrollers are 9600 to 115200.
UART Communication
In this communication, there are two types of UARTs available namely transmitting UART and receiving UART, and the communication between these two can be done directly by each other. For this, simply two cables are required to communicate between two UARTs. The flow of data will be from both the transmitting (Tx) & receiving (Rx) pins of the UARTs. In UART, the data transmission from Tx UART to Rx UART can be done asynchronously (there is no CLK signal for synchronizing the o/p bits).
The data transmission of a UART can be done by using a data bus in the form of parallel by other devices like a microcontroller, memory, CPU, etc. After receiving the parallel data from the bus, it forms a data packet by adding three bits like start, stop and parity. It reads the data packet bit by bit and converts the received data into the parallel form to eliminate the three bits of the data packet. In conclusion, the data packet received by the UART transfers in parallel toward the data bus at the receiving end.
Start Bit
Start-bit is also known as a synchronization bit that is placed before the actual data. Generally, an inactive data transmission line is controlled at a high-voltage level. In order to begin the data transmission, the UART transmission drags the data-line from a high voltage level (1) to a low voltage level (0). The obtaining UART notices this transform from the high level to low level over the data line as well as starts understanding the real data. Generally, there is just a single start bit.
Stop Bit
The Stop Bit is placed at the ending of the data packet. Usually, this bit is 2-bits lengthy but frequently on bit only utilized. In order to stop the broadcast, the UART keeps the data-line on high voltage.
Parity Bit
Parity bit lets the receiver to ensure whether the collected data is right or not. It is a low-level fault checking system & parity bit is available in two ranges such as Even Parity as well as Odd Parity. Actually, this bit is not widely used so it is not compulsory.
Data Bits or Data Frame
The data bits include the real data being conveyed from the sender to receiver. The data frame length could be between 5 & 8. If the parity bit is not used when the data frame length could be 9-bit long. Generally, the LSB of the data to be transmitted first then it is very useful for transmitting.
UART Interfacing
The following figure shows UART interfacing with a microcontroller. The UART communication can be done using three signals like TXD, RXD, and GND.
By using this, we can exhibit a text in personal computer from 8051 microcontroller board as well as the UART module. In 8051 board, there are two serial interfaces such as UART0 and UART1. Here, UART0 interfacing is used. The Tx pin transmits the information to PC & Rx pin receives the information from PC. Baud rate can be used to denote the speeds of both the microcontroller and PC. The data transmission and reception can be done properly when the baud rates of both microcontroller & PC are similar.
Applications of UART
UART is normally used in microcontrollers for exact requirements, and these are also available in various communication devices like wireless communication, GPS units, Bluetooth module, and many other applications.
The communication standards like RS422 & TIA are used in UART except for RS232. Usually, a UART is a separate IC used in UART serial communications.
Advantages and Disadvantages of UART
The pros and cons of UART include the following
Thus, this is all about an overview of Universal Asynchronous Receiver Transmitter (UART) is one of the fundamental interfaces which gives a simple, cost-effective & consistent communication among microcontroller as well as PC. Here is a question for you what are UART pins?
![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |