\u00a0 \u00a0Serial Communication Introduction (Review)\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n <\/p>\n
[<\/strong> Disclaimer: This section has been discussed earlier in Tutorial-18 For UART. If you’ve already read it, just skip to the next one. ]<\/strong><\/p>\nIn Embedded Systems, Telecommunication, and Data Transmission applications, Serial Communication<\/strong> is known to be the process of sending data one bit at a time (bit-by-bit) sequentially, over the serial bus. It takes a complete clock cycle in order to transfer each bit from an end to the other. Conversely, parallel communication is known to be the process of sending several\u00a0bits, even bytes, as a whole only in a single clock cycle. However,\u00a0even if you transfer fewer data per cycle with a serial transmission, you can do it at much higher frequencies which results in higher net transfer rates than of the parallel communication.<\/span><\/p>\n\u00a0The Fundamental Concepts\u00a0<\/span><\/h4>\nThere do exist many serial communication protocols, each of which is working in a different way. However, it turns out to be some similarity that they do share in common. Frankly speaking, serial communication protocols have Shift Registers<\/strong> somehow\/somewhere in their\u00a0hardware implementation as the working-horse (core). Shift registers are used to shift out the data to be transmitted bit-by-bit each clock cycle. So, let’s dig deeper into a shift register in order to build up a complete understanding of what is actually happening at the Register(low)-Level.<\/span><\/p>\nShift registers are basically some D-Flip-Flops serially connected while sharing the same clock line. Here is a graphical animation that demonstrates\u00a0how does a shift register work internally.<\/span><\/p>\n <\/p>\n
<\/p>\n
As you might have noticed, the data input (0’s and 1’s) is being shifted from the input pin to the output end at bit-0. It takes only 1-clock to transfer a single bit, which means it takes 8-clocks for a single byte transfer. For the same of simplicity, we’ll represent the shift register as a single block instead of a series of D-Flip-Flops as shown above.<\/span><\/p>\n <\/p>\n
Here is an animation for an 8-Bit shift register with a serial input & serial output data lines.<\/span><\/p>\n <\/p>\n
<\/p>\n
Well, now you should know how shift registers are actually working. Frankly speaking, serial communication is as simple as connecting a couple of shift registers together! Connecting the data output of a shift register to be the data input of the other shift register enables us of sending digital data serially from an end to another!<\/span><\/p>\n <\/p>\n
In the following animation, I’m connecting a couple of 4-Bit shift registers. One at the transmitter device and the other at the receiver device. The serial bus consists of a couple of wires (data, and clock). Each clock, a bit is sent from the transmitter TX pin and received by the receiver’s RX pin.<\/span><\/p>\n <\/p>\n
As you might have noticed, it takes 4-clocks to send the 4-Bit data from the transmitter to the receiver. This is simply the short answer to the “How Serial Communication Works?”. However, it’s not everything you should know about serial communication or at least to the level of being able to implement a simple serial data transfer process.<\/span><\/p>\nIn fact, there are some other options and configurations for each serial communication protocols. Which includes, various data transmission rates, error detection, and correction mechanisms, and much more that adds to the overall complexity associated with each protocol. We’ll be working with the UART protocol in this tutorial, which has a decent steep learning curve.<\/span><\/p>\n\u00a0Serial VS Parallel Communication\u00a0<\/span><\/h4>\nAs we’ve stated earlier, serial communication is the process of sending data each bit at a time (bit-by-bit). And conversely, parallel communication is the process of sending multiple bits, even bytes, as a whole in a single clock cycle.<\/span><\/p>\nA very basic implementation, that you can create on your own, for the parallel data transfer is shown down below. As I’m connecting a couple of 4-Bit registers, via the 5-wire parallel bus (4-data pins + 1-clock). Here is an animation that shows you how parallel data transfer is done.<\/span><\/p>\n <\/p>\n
As you might have noticed, it takes only 1-clock to transfer the data from a transmitter device to the receiver! Frankly speaking, we can theoretically transfer any number of bits (or bytes) using parallel communication protocols in a single clock cycle at a time.\u00a0<\/span><\/p>\nHere is a brief comparison between serial communication and parallel communication protocols.<\/span><\/p>\n\n\n\nFeature<\/strong><\/span><\/td>\nSerial Communication<\/span><\/strong><\/td>\n Parallel Communication<\/span><\/strong><\/td>\n<\/tr>\n\nRelative Speed<\/strong><\/span><\/td>\nFaster<\/span><\/td>\n Slower<\/span><\/td>\n<\/tr>\n\nDistance Range<\/strong><\/span><\/td>\nMuch Farther<\/span><\/td>\n Shorter<\/span><\/td>\n<\/tr>\n\nTransfer Method<\/strong><\/span><\/td>\nOne bit is transmitted at a time<\/span><\/td>\n Bytes are transmitted in parallel, One byte or more at a time<\/span><\/td>\n<\/tr>\n\nApplications<\/strong><\/span><\/td>\nComputer small peripherals, modules interfacing, and sensors measuring & sending simple data frames<\/span><\/td>\n Short distance High-Speed communication such as Computer printers, etc\u00a0<\/span><\/td>\n<\/tr>\n\nWires Inside<\/strong><\/span><\/td>\nFew wires, all data bits pass only through the same data line<\/span><\/td>\n Multiple, more wires, each bit has a dedicated wire. So as to be transmitted all at once<\/span><\/td>\n<\/tr>\n\nCables Pictures<\/strong><\/span><\/td>\n <\/td>\n <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u00a0Applications Of Serial Communication Protocols\u00a0<\/span><\/h4>\nThere are countless situations in which we do use one or more of the serial communication protocols. Here, I’ll try to categorize the main purposes for which we use serial communication instead of listing down all possible applications that may be serving the same purposes. Hence, any application will, hopefully, possibly fall under the umbrella of the following purposes.<\/span><\/p>\n\nExternal devices\/modules communications<\/span><\/li>\nDownloading \/ Updating The Firmware<\/span><\/li>\nConsole io<\/span><\/li>\nData Transmission<\/span><\/li>\nDebugging interface<\/span><\/li>\n<\/ul>\nSerial communication protocols are of fundamental importance for each embedded systems application. And as embedded systems engineers, we must have a very good experience with almost all of the commonly used serial communication protocols. And in the next sub-section, we’ll see a brief collection of the commonly available serial communication protocols in the embedded market.<\/span><\/p>\n\u00a0Examples Of Serial Communication Protocols\u00a0<\/span><\/h4>\nThere are many serial communication protocols that are existent and being used in various domains and sub-domains of embedded systems. Some of which are more likely to be used in the automotive industry such as LIN & CAN, some of which is more likely to be used for external memory interfacing as I2C, high-speed computer interfacing as USB, and for audio applications such as I2S. Here is a list of the most common serial communication protocols in the industry.<\/span><\/p>\n\n\n\nUSB<\/strong><\/td>\nCAN<\/strong><\/td>\nI2C<\/strong><\/td>\n<\/tr>\n\nI2S<\/strong><\/td>\nLIN<\/strong><\/td>\nSPI<\/strong><\/td>\n<\/tr>\n\nEthernet<\/strong><\/td>\n1-Wire<\/strong><\/td>\nUART\/USART<\/strong><\/td>\n<\/tr>\n\nAnd many others…<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\nWe’ll be focusing only on the SPI serial peripheral interface for the rest of this tutorial. Just to understand the fundamentals & mechanics of SPI communication. Only then, we’ll be able to develop the necessary firmware in order to transmit data from an embedded MCU to another as we’ll be doing the lab at the end of this tutorial. So, let’s get started with SPI.<\/span><\/p>\n \n\u00a0 \u00a0SPI | And How It Works?\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n <\/p>\n
\u00a0What’s SPI? And How it Works?\u00a0<\/span><\/h4>\nSPI is an acronym for (Serial Peripheral Interface) pronounced as “S-P-I” or “Spy”. Which is an interface bus typically used for serial communication between microcomputer systems and other devices, memories, and sensors. Usually used to interface Flash Memories, ADC, DAC, RTC, LCD, SDcards, and much more.<\/span><\/p>\nThe SPI was originally developed by Motorola back in the 80s to provide full-duplex serial communication to be used in embedded systems applications.<\/span><\/p>\nIn typical SPI communication, there should be at least 2 devices attached to the SPI bus. One of them should be the master and the other will essentially be a slave. The master initiates communication by generating a serial clock signal to shift a data frame out, at the same time serial data is being shifted-in to the master. This process is the same whether it’s a read or write operation.<\/span><\/p>\nTypical SPI Connection Diagram<\/figcaption><\/figure>\n\u00a0Pin Conventions & Connections\u00a0<\/span><\/h4>\nThe SPI bus includes 4 logic lines defined as follows:<\/span><\/p>\nMISO<\/strong> -> Master input slave output (Dout From Slave).<\/span><\/p>\nMOSI<\/strong> -> Master output slave input (Dout From Master).<\/span><\/p>\nSCLK<\/strong> -> Serial Clock, generated by the master and goes to the slave.<\/span><\/p>\nSS<\/strong> -> Slave Select. Generated by the master to control which slave to talk to. It’s usually an active-low signal.<\/span><\/p>\nOther naming conventions are followed by different manufacturers across the globe. But in the end, they are referring to the same things. Here are some of the commonly used terms to express the\u00a0above 4 logic lines (SPI Bus).<\/span><\/p>\nMISO (master in slave out) => can also be SIMO, DOUT, DO, SDO, or SO (At The Master End).<\/span><\/p>\nMOSI (master out slave in) => can also be SOMI, DIN, DI, SDI, or SI (At The Master End).<\/span><\/p>\nSS (slave select) => can also be CE, CS, or SSEL.<\/span><\/p>\nSCLK (serial clock) => can also be SCK.<\/span><\/p>\nThe convention we’re going to follow in this tutorial and upcoming topics related to SPI will be the following one<\/span><\/p>\n[ SDI, SDO, SCK, SS]<\/span><\/p>\nThe typical SPI connection between a single master and a single slave is indicated in the diagram below.<\/span><\/p>\nSPI Master-Slave Connection<\/figcaption><\/figure>\n