Communication Bottlenecks in ARM-ATMEL Controller Boards
for Robotic Applications
Submitted as partial fulfillment of the requirements for the award of the degree of
Master of Science in Embedded Systems
By
Selvaraju Venkatesh (G1200959C)
Supervisor:
Mr. Yong Chuen-Tze, Mark
Submitted to the School of Computer Engineering
2013
i
Table of Contents
Acknowledgement ......................................................................................................................... iii
Abstract .......................................................................................................................................... iv
Acronyms ........................................................................................................................................ v
List of Figures ................................................................................................................................ vi
List of Tables ................................................................................................................................ vii
Chapter 1 Introduction .................................................................................................................... 1
1.1 Background and motivation .................................................................................................. 1
1.2 Objectives .............................................................................................................................. 1
1.3 Challenges ............................................................................................................................. 3
1.4 Organization of thesis............................................................................................................ 4
Chapter 2 Communication Protocol................................................................................................ 5
2.1 Communication Protocol: ..................................................................................................... 5
2.2 SPI Communication: ............................................................................................................. 7
Chapter 3: Implementation Platform ............................................................................................ 15
3.1 Raspberry Pi board .............................................................................................................. 15
3.1.1 Communication in Raspberry Pi .................................................................................. 16
3.1.2 Support for SPI Implementation ................................................................................... 16
3.1.3 Enabling SPI Communication ...................................................................................... 16
3.2 Arduino Uno ........................................................................................................................ 17
3.2.1 Arduino IDE ................................................................................................................. 19
3.3 Arduino Due ....................................................................................................................... 21
3.3.1 Arduino IDE: ................................................................................................................ 23
Chapter 4: Experimental Analysis & Implementation Issues ....................................................... 25
4.1 Interface between Arduino UNO and Raspberry pi: ........................................................... 25
ii
4.1.1 Communication Bottlenecks: ....................................................................................... 26
4.1.2 Increasing clock Speed: ................................................................................................ 26
4.1.3 Operating Voltage: ....................................................................................................... 27
4.2 Communication analysis for Arduino Uno ......................................................................... 27
4.3 Components for communication platform .......................................................................... 28
4.4 Communication platform with multiple slaves or nodes .................................................... 31
4.5 Interface between Arduino DUE and Raspberry pi: ........................................................... 30
4.5.1 Communication Bottlenecks: ....................................................................................... 30
4.5.2 Increasing clock Speed: ................................................................................................ 31
4.5.3 Operating Voltage: ....................................................................................................... 31
Chapter 5 ....................................................................................................................................... 32
5.1 Experimental Results and Limitations of Hardware: .......................................................... 32
5.2 Implementation of Error Detection and Correction: ........................................................... 39
5.3 Implementation of Compression: ........................................................................................ 40
5.4 Theoretical Result: .............................................................................................................. 49
5.5 Optimizing Boot time of Raspian Wheezy OS: .................................................................. 50
Chapter 6 Summary ...................................................................................................................... 51
Conclusion:................................................................................................................................ 51
Future works:............................................................................................................................. 52
References: .................................................................................................................................... 52
iii
Acknowledgement
I like to begin my thanks to my parents for supporting me to do a Master degree in their
retired life.
I am grateful to thank my project supervisor Mr. Yong Chuen-Tze, Mark School of
Computer Engineering for guiding me throughout the project to complete my dissertation for
Master's degree. My deepest thanks to his patience for my mistakes in between deadlines. With
your valuable and timing suggestions you made me ease to complete my dissertation. By your
teaching I learned many things technically to find a solution for a problem in a different way of
thinking.
I would like to extend my sincere thanks to Tan Piah Chye, Hardware projects lab for
contributing his support to available resources while working in lab.
I also extend my thanks to each professor during my coursework for clearing my practical
doubts.
At last I extend my sincere thanks to my friend's who supported me to complete my
dissertation.
iv
Abstract
The main scope of the project is to build a high speed communication system for robotic
applications using hybrid controller board for large volume of data. Due to larger number of data
from sensor and actuators, modern applications meets communication bottlenecks to process data
in real time. More number of I/O pins for sensors and actuators requires more power and
memory to handle high volume of data and have latency to handle data. Latency in process can
be avoided by using higher efficient powerful processor and micro-controller with less power
consumption. To avoid overload of a single board to read data from sensor and handle data for
process can be modified to read sensor data in a board and for processing data is passed to a
multi core ARM processor for further communications.
In this project Arduino microcontroller has been used at lower end by interfacing I/O pins
of sensors and Raspberry pi ARM processor board at higher end to handle data for further
communication. By default all low-power communication boards uses serial communication for
reason of ease of implementation and for efficient result. When coming to high bandwidth
communication the speed was limited. The choice of high bandwidth communication protocol
and interface issues has been discussed in further chapters. By implementing high bandwidth
protocol and rectifying issues faced by current robotic controller it is possible to build a better
efficient robot for future enhancement.
v
Acronyms
CAN - Controller Area Network
CPU - Central Processing Unit
DAC - Digital to Analog converter
EEPROM - Electrically Erasable Programmable Memory
EOT - End of Text
IDE - Integrated Development Environment
I2C - Inter Integrated Circuit
KB - Kilo Byte
JTAG - Joint Test Action Group
LAN - Local Area Network
LZW - Lempel-Ziv-Welch
MB - Mega Byte
MISO - Master-In Slave-Out
MOSI - Master-Out Slave-In
RISC - Reduced Instruction Set Computing
SCK - Clock
SOT - Start of Text
SPI - Serial Peripheral Interface
SRAM - Static Random Access Memory
SS - Slave Select
UART - Universal Asynchronous Receiver Transmitter
USB - Universal Serial Bus.
vi
List of Figures
Figure 1.1 Objective of application design .................................................................................... 3
Figure 2.1 SPI Block Diagram ...................................................................................................... 7
Figure 2.2 Modes of SPI communication ...................................................................................... 8
Figure 2.3 SPI timing diagram ....................................................................................................... 9
Figure 2.4 Schematic of SPI data transfer.................................................................................... 10
Figure 2.5 Interrupt Based communication between Master and Slave ...................................... 12
Figure 2.6 Sequence Flow diagram of different booting time processor ..................................... 13
Figure 2.7 Problem with existing boot time asynchronization .................................................... 14
Figure 2.8 Solution for existing boot time asynchronization ....................................................... 14
Figure 3.1 Raspberry pi board...................................................................................................... 15
Figure 3.3 Arduino UNO board specifications ............................................................................ 17
Figure 3.4 Arduino UNO Schematic diagram ............................................................................. 18
Figure 3.6 Arduino Due board specifications .............................................................................. 21
Figure 3.7 Arduino Due Schematic diagram ............................................................................... 22
Figure 3.8 Arduino IDE v1.5.3 for Arduino Due......................................................................... 23
Figure 4.1 Interface between Raspberry pi and Arduino Uno via SPI ........................................ 25
Figure 4.2 Level Shifter for 3.3v to 5v ........................................................................................ 27
Figure 4.3 Interface between Raspberry pi and Arduino Uno via SPI ........................................ 30
Figure 5.1 Graph for receiving data in Raspberry Pi ................................................................... 33
Figure 5.2 Receiving speed of data in Raspberry pi .................................................................... 34
Figure 5.3 Graph for receiving data in Raspberry Pi ................................................................... 36
Figure 5.4 CPU usage while IDLE .............................................................................................. 38
Figure 5.5 CPU usage while running ........................................................................................... 39
Figure 5.6 Flowchart for LZW compression algorithm ............................................................... 42
Figure 5.7 Running compression for random integer and large text. .......................................... 43
Figure 5.8 Slave Mode Functional Block Diagram ..................................................................... 44
vii
List of Tables
Table 3.1 Key specification of Arduino Uno ................................................................................ 17
Table 3.2 Key Specification of Arduino Due ............................................................................... 21
Table 3.3 Cost of Resource ........................................................................................................... 24
Table 5.1 Receiving data speed in Raspberry pi ........................................................................... 33
Table 5.2 Format for Error detection with checksum ................................................................... 40
Table 5.3 Dictionary table for LZW algorithm ............................................................................. 41
Table 5.4 Compression results for various files............................................................................ 43
Table 5.5 Table for configuring BITS in SPI_CSR Register. ....................................................... 46
1
Chapter 1 Introduction
1.1 Background and motivation
In the modern world all embedded applications are handling large amount of data.
Especially for robotic applications it's a new era for modern sensor's. Earlier we used only sound
and light sensor, nowadays various kind of sensor's are used. At present robot applications use
modern sensor which have large amount of data for system. when coming to processing, system
become slow to perform all actions before next command. In high speed data interface systems,
the speed of transmission and data acquisition are creating new challenges for portable systems.
A new trend of ARM+Arduino combinational controller become so popular for robot
applications. In this project we will briefly discuss about implementation high speed
communication between ARM and ATMEL boards.[1]
1.2 Objectives
The objective of the project is to build high speed compact system for robotic
applications with ARM and ATMEL hybrid controller. The task of the robot application to
handle large amount of data between processor and controller. Existing system uses serial
communication protocol for communication between board and sensor's. It is quite slow when
system needs to transfer large amount of data for processing. When using serial communication
we can able to achieve maximum transfer of 14.0625 KB/s.
In my project, I'm building a system which can have large number of sensors Eg. speech
sensor, distance sensor, temperature sensor etc. Speech sensor collect text data, temperature
sensor collect float value, distance sensor collect either integer or byte value. So amount of data
will be in larger value of KB's or even in MB's. With the serial communication we supposed to
loss data bytes. So the communication bandwidth should be higher than serial communication.
Here an example for single sensor. Infrared thermometer-MLX90614 which represents
temperature in float values. It has 11 bit resolution with response time of 1.024ms. For a second
it produces 977 responses, to represent a value in terms of bytes we consider it as 16bit value
which mean it will be calculated as 977*16 bits/sec=15632bits/sec =1954 bytes/sec = 1.91KB/s.
2
If we use 8 similar sensors it exceeds serial communication bandwidth and creates
communication bottleneck for large number of sensors. To avoid communication bottleneck in
between commands we should design system with higher bandwidth communication.
For Example, Arduino Due has 12 analog pins. If we have similar sensors which have
data value for above mentioned sensor. Then we will have 22.92KB of data. If we transfer data
for every second then our required data transfer is exceeds maximum transfer data throughput of
serial communication.
Here Arduino is going to be a data collection node which will transfer collected data at
specific intervals. Two system design are considered as shown in Figure 1.1. In first case
Raspberry Pi and Arduino Uno has been chosen and in second case Arduino Uno is replaced by
Arduino Due. These two cases are used to prove high bandwidth transmission between boards
and communication bottlenecks are clarified to build perfect high-speed hybrid controller[2].
Arduino Uno is chosen here because it is the low-cost arduino ATMEL microcontroller,
as it is the first microcontroller from arduino it has well built libraries for implementation. On the
other hand arduino Due has been chosen, since it has more IO pins and more SRAM memory
compare to other arduino microcontroller. It the latest microcontroller from arduino with
ATMEL SAM3X8E. My final system able to communicate data at higher speed than serial
communication.
3
Figure 1.1 Objective of application design
1.3 Challenges
Basically Arduino micro-controller configured as master node for controlling sensor and
every data has handled in same board. Challenge here is to make Arduino as a sensor node to
collect data and transfer collected data to another board for further communication. When taking
two board its unstable to configure together due to issues of hardware pins and support for one
board to other.
SPI communication doesn't have the support for error correction and detection because
SPI specifies the signaling protocol. The meaning of the bit is not defined by the protocol. We
can write our software to do error correction but it will take additional CPU usage for working it
and may produce delay between incoming bytes.
Compression algorithm implementation where memory source on-board has been limited.
Because when more number of sensor's data are read by microcontroller, if transmission speed is
slow it have to hold the data until all collected data are transmitted. It will skip data to send when
size of data exceeds on-chip memory. So compression can be implemented to avoid memory
4
shortage. Compression quite easier for repeated number of bytes and for worst case it is very
difficult for non-repeated bytes.
1.4 Organization of thesis
The remaining chapters are organized as follows:
Chapter 2-Communication Protocol
The chapter discuss about various communication protocol for board to board
communication. Reason for choosing protocol and discussed about problem in interrupt based
communication.
Chapter 3-Implementation Platform
This chapter describe about each platform of Raspberry pi and Arduino microcontroller.
Elaborated hardware description will give an idea about I/O pins to configure for
communication.
Chapter 4-Experiental analysis & Implementation Issues
This chapter illustrates how to physically design system with wires for communication
pins. And also limitations of hardware platform for certain cases for which communication is
limited. And addressed about latency of hardware and data bandwidth problem.
Chapter 5-Experimental Results
This chapter shows the results of build systems of raspberry pi and Arduino Uno,
Raspberry pi and Arduino Due. Implementation Error correction and detection for SPI protocol
has been discussed. Compression for data where memory resources has limited.
Chapter 6-Conclusion and Future works
This chapter provides summary of high speed communication between ARM and
ATMEL microcontroller and suggestions for future work.
5
Chapter 2 Communication Protocol
2.1 Communication Protocol:
The UART (Universal Asynchronous Receiver Transmitter) a two way communication
protocol Tx and Rx pins since its very basic and popular communication protocol for low level
peripherals. It supports up to 115Kbits per second.
CAN (Controller Area Network) is a multi-master communication. Each master will be
able to send/receive data at a time. It supports up to 1Mbits per second.
I2C (Intel-Integrated Circuit) is a half-duplex, master-slave communication. One master
and maximum of 127 slave devices connection can be possible. There are two pins; one is SDA
(data) and SCL (clock). It can transfer data between two processors but one way transmission
can be possible at a time. Slave select is done by device addressing by software. To be able to
support multiple devices on a same bus, utilizes pull up resistor on the wires. The peripheral can
be pulled low by the device „logic 0‟. And to release the device pulled high by the device „logic
1‟. Latest hardware control capable of transmitting 3.4Mbits per second.
USB2.0 and USB 3.0 can transfer data at 480Mbit/s and 5Gbits/s respectively. For USB
protocol the complexity was high in terms of latency, packet framing, routing issues. Since SPI
and I2C had well support for Atmel controller. Moreover arduino microcontroller doesn't have
physical availability of USB port and driver support for it. Arduino Uno has Type B USB port
which is designed for USB-Serial communication and data speed is limited to 115200bps. Even
if we design USB port, it's very difficult to write our own USB driver support and implement it
in ATMEL controller. With the less amount of SRAM memory we can't frame data to send at all
time. With SPI and I2C protocols it is easier to add additional slave devices without any complex
structure and it needs a single additional wire to add it. Compare to I2C, SPI has higher transfer
bit rate. I observed 60% improve in data transfer rate by using SPI protocol instead of serial
communication in later section.
SPI (Serial Peripheral Interface) is a full-duplex, master-slave communication, basically
3or4 wires protocol. MOSI (Master Out, Slave In), MISO (Master In, Slave Out) and SCLK
(clock from master)[3]. In certain cases multiple slaves are used, at this case 4th
wire SS (Slave
Select) is kept low to use a particular peripheral. Other time it kept high. It‟s a kind of ring
6
communication between two processors. Master shares the clock with slave. Data bit is available
at rising edge of the clock. The data rate often goes 10Mb/s [4].
Whenever one of the devices wants to transmit data to other then it is half duplex. Master
will be identified by the SS pin if it set as low. After the transmission master will set SS pin as
high. It has two libraries namely a circular FIFO and an UART communication library. A
circular FIFO library offers dynamically allocation of circular FIFO, commonly used in
communication applications. The UART communication library supports interrupt based
reception into a circular FIFO; interrupt based transmission from a circular FIFO as well polled
mode byte transmission[5].
Here we are going to briefly discuss about the SPI interface, the reason of choosing this
interface in a system because it‟s a two way communication protocol where the speed was in
Mbps.
7
2.2 SPI Communication:
To begin communication, SPCR (SPI Control Register) register in master and slave
should be configured; master should have clock speed at which going to be run[6]. It should be
less than or equal to slave can able run at the speed. Master generate clock for the whole system.
7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 bit
SPCR SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0
Figure 2.1 SPI Block Diagram
SPR0 and SPR1 the two bits to configure the clock speed of processor.
DORD bit controls the direction of the transmission if it set as low it will transmit MSB
bit first and LSB last in SPDR register else LSB bit first and MSB last. Clock polarity decides
when to send/receive data whether at falling edge or rising edge. If CPOL bit is set as low the
data going to be send/receive at the rising edge of the clock else in falling edge of the clock.
Clock phase decides when to read data whether at falling edge or rising edge. If CPHA bit is set
as low the data going to be read at the rising edge else high for falling edge of the clock.
8
MODE 0
MODE 1
MODE 2
MODE 3
Figure 2.2 Modes of SPI communication
9
Figure 2.3 SPI timing diagram
10
SPE bit enables the SPI interface between master and slave. SPIE bit enables the SPI
interrupt service routine. MSTR bit in the SPCR register tells about whether the processor is
master or slave. If set HIGH act as master else act as slave. SPDR (SPI Data Register) is an 8-bit
register the 7th
bit is the MSB and 0th
bit is LSB of the data.
Master initiates communication by setting CS pin low which indicates the data going to
transfer. After clock is set the MSB bit is first transfer in the 8 bit shift register via MOSI pin. At
the transfer of a bit master can able to read a bit from MISO pin. After the completion of transmit
bytes the CS will be high.
Figure 2.4 Schematic of SPI data transfer
Issues faced by processor for SPI:
Clock speed:
The major issue face by the embedded processor is the clock speed between the processor
going to run. SPI interface communication based on the clock of the master. Slave clock will be
less when compared to master. So we have to run the master at equal to or less than the slave
clock speed. By running master at slave clock speed the synchronization of data between
processors will be correct. What can we do if data‟s are incorrect even running at the same clock
speed. Error detection and error correction technique can be used to resolve the problem. But SPI
doesn‟t have the support of error detection and error correction. Then how to implement it? In
that case the user program can able to resolve this problem. Supported clock speed is based on
clock divider, speed ranges from 500KHz to 4MHz for Arduino Uno.
11
Solution for Clock speed issue-ERROR CORRECTION AND ERROR
DETECTION:
The error correction code is in the form of adding additional bytes of checksum,
length, start byte and end byte along with the data bytes from the transmitter before transmission.
The error detection code is in the form validating bytes received in the receiver side with the
additional bytes. If the bytes received correctly it acknowledges the transmission[7]. Software
written in the transmitter automatically retransmits data until data received correctly by the
receiver which leads to continuously in transmit loop even if receiver is not working. If transmit
was successful transmitter will ready for next transfer but it may leads to continuously consume
power. If transmitter needs acknowledgement for retransmission, then transmitter waits for
acknowledgement from the receiver even receiver fails to work. It will affect the communication
between other slaves. To reduce that wastage of power we can use interrupt-based
communication method.
INTERRUPT-BASED COMMUNICATION METHOD:
In multiple processor communication system, we can implement interrupt based
communication to prevent transmitter to wait for a receiver acknowledgement whether the
receiver is working or not. Also it improves ability of transmitter to communicate to other slave
instead of lock for a slave. ARQ (Automatic Repeat Request) is an acknowledgement from
receiver to transmitter, if it unsuccessful in last transmission of data then negative
acknowledgement otherwise positive acknowledgement[8]. To inform receiver that transmitter
wants to transmit data by sending IQR (Inquire). If receiver ready to receive data then it
acknowledge interrupt to the transmitter, after that transmitter sends data bytes to receiver.
Whether the receiver in processing state of previously data it will send a busy acknowledge or
not working it won‟t acknowledge for IQR from transmitter. Once it completes the receiving of
bytes it will wait for IQR. This way helps the transmitter to avoid unnecessary wait state for
acknowledgement.
In SPI interface, processor which is connected to sensor is continuously reads the
value from it. When the other master sends the IQR (may be command byte) to slave it will
consider as interrupt, after receiving byte it will check whether it is command byte if it so will
12
send data bytes which read from sensor. After sent of data bytes it needs some delay to read next
data from sensor until that it will disable the next interrupt.
Figure 2.5 Interrupt Based communication between Master and Slave
Another issue face by the embedded system is booting time of the processor. Most of the
embedded processor has the different operating clocks. If similar processor used to form a
system then there will be no delay of booting time between them. Else two different processors
in a system, most master takes more time to boot than the slave. If processor finishes the
processing before the boot of other processor have to wait for the other processor to send status
of the processor.
Faster processor has to run time delay in the program after completing processing of data
and before transmit of data to get response from slower processor.
MASTER SLAVE
Booting Time
Booting Time IQR wait
Read data from sensor ACK Wait
Data Processing 1
POWER ON
IQR
ACK IQR Command
DATA
IQR
IQR wait
13
Figure 2.6 Sequence Flow diagram of different booting time processor
Suppose master processor connected to multiple slave devices which have different clock
speed then it will be a complex logic to implement time in software to configure the slaves in the
system to communicate with the master[9]. For different processor different delay time by
coding will make the software worse. Instead of that, we can set unique clock speed at which all
slave can operate, in this case it can work but it may lead to slower in terms of speed of
communication.
Solution for Interrupt based communication- RESET Signal
If Master board has operating system its booting time is longer than microcontroller
slave. Slave may wait for an interrupt will help the system instead of waiting for a command
byte. In another side, slave can take more time to boot the system. At that time interrupt sent by
the master cannot be received by slave. And it won‟t receive any data from slave. But master will
keep on transmitting interrupt. We can use a reset signal after the boot of slave. Once master got
the reset signal from slave, master initiates the communication by sending command byte as
interrupt. Slave will consider command byte as interrupt. At the interrupt function it will check
whether the received byte is command byte for data transfer or some junk data. If the command
byte is received then slave will transmit data to master. Master boot time is greater than slave
then there will be no delay in process because slave communication will establish when master
clock generates signal otherwise it wait for the master clock. Let see how the reset signal is set.
MASTER SLAVE
Booting Time
Booting Time
Time delay (master boot)
Time delay (master process)
Time delay (master process)
Command receive & Data Transfer 1
Command send & Data Receive 1
Data Processing 1
Command receive & Data Transfer 2 Command send & Data Receive 2
Data Processing 2
POWER ON
14
Figure 2.7 Problem with existing boot time asynchronization
Figure 2.8 Solution for existing boot time asynchronization
In the meanwhile, we have to check the capability of processor whether it can act both
master and slave. Especially ARM based system can only act as master. So there should another
microcontroller used in the system to act as slave. The reason behind the issue is by default the
kernel space modules are designed to only act as master.
MASTER SLAVE
Booting Time
Booting
Time
Processing Time
POWER ON
IQR
Reset waiting
MASTER SLAVE
Booting Time
Booting
Time
POWER ON
Reset
IQR waiting
IQR
15
Chapter 3: Implementation Platform
3.1 Raspberry Pi board
Raspberry Pi is a palm size computer with an ARM1176JZF-S ARM processor. Raspian
Wheezy is a Linux based Operating system that can be loaded in a 4GB SD memory card and
booted from it. It is a powerful ARM processor with 512MB of RAM and runs at 700MHz[10].
It can be over-clocked to speed of 1GHz. Nowadays it used widely low-cost, low-power robotic
applications. For developing applications HDMI port is available to connect with HDMI monitor
and writing code can be done with wireless keyboard and mouse.
Figure 3.1 Raspberry pi board
As it runs Linux OS, a developer can able to write code in various programming
languages like C,C++ and python are most popular and execute it. We can write code with or
without GUI. GUI software for coding Geany and Gedit. Geany is integrated IDE with
terminal[11]. For specific applications we can directly hook up sensors with raspberry pi and run.
Operating voltage at 3.3v consumes less power even for continuous run.
16
3.1.1 Communication in Raspberry Pi
Raspberry pi with an ARM11 processor can capable of transfer data via serial,I2C and
SPI protocols with limited wires to other boards. If we are looking for high speed protocols we
can implement Ethernet protocol between boards, but we must ensure that both boards have
physical available of LAN adapter for connection. In our platform we going communicate
between arduino Uno and raspberry pi. Arduino Uno doesn't have a LAN adapter with it. It's
quite expensive to add LAN adapter in arduino. so we try to communicate with SPI. Since SPI
can able to transfer bit rate at a very high range of Mbps. Benefit of SPI over other protocols is
that can capable of Bidirectional communication at a time. Maximum clock speed of SPI in
raspberry is 16MHz. Minimum clock speed supported by SPI is 500KHz.
3.1.2 Support for SPI Implementation
As we know in SPI communication between two boards one should be act as master and
other as slave. Master runs clock for communication with slave. Slave respond back with
synchronization of master clock. Raspberry pi designed as can only work as master so raspberry
pi cannot be slave at any time. Because Raspberry pi cannot work without running clock, as
slave is not possible to configure by muting clock. And also it's not possible to do SPI
communication between two raspberry pi boards[12].
3.1.3 Enabling SPI Communication
By default Broadcom BCM2835 pins for SPI communication has been disabled to avoid
damage of pins. To enable it include "#" at start of lines in the file.
Figure 3.2 Enabling SPI communication[13]
17
3.2 Arduino Uno
An Arduino Uno is an Atmel 328P 8-bit AVR microcontroller with RISC
architecture[14][15]. It has 14 digital IO pins to hook up multiple sensors. It able to handle 6
analog inputs. It supports communication via RS232,I2C,SPI with other boards. Operating
voltage at 5v is common among microcontroller. With compact size of length and width of 2.7
and 2.1 inches acquires less space in a compact system. It is a low cost microcontroller in
arduino family.
Table 3.1 Key specification of Arduino Uno
Input
Voltage
Flash
Memory
SRAM EEPROM Clock Speed SPI Clock
Speed
Software
6-12 V 32 KB
(0.5KB for
boot loader)
2KB 1KB 16MHz 4MHz Arduino
IDE
v1.0.5
Figure 3.3 Arduino UNO board specifications
18
Figure 3.4 Arduino UNO Schematic diagram
With Arduino we can read sensor data, control motor, lights, read switches and control small DC
motor. A beginner can easily able to do code in c program to perform small task. In c program it
can able to compile 300,000 lines of code in a single program. With some extensions we can
access hardware in code.
Flash Memory for source code:
With the 32KB of flash memory we can flash our code. Feature of arduino is no
need to compile code in board. In host pc we can do compiling process and flash the code in the
board memory.
19
Clock Frequency:
The Arduino uno has 16MHz clock speed for operating. For SPI specific it limited
to 4MHz, PIN 13 drives the clock for SPI.
3.2.1 Arduino IDE
To develop code, arduino is provided with IDE which run on host pc to compile code and
is very simple to use. It doesn't have all libraries which normal c compiler has. Latest version of
arduino IDE can be downloaded freely from arduino website. It build with standard
programming language compiler so there is no need to separately install compiler in the host pc.
Arduino IDE can communicate with board through USB cable. And we easily choose to which
arduino board we going to compile. It also support user defined library files to include in our
program to compile.
Figure 3.5 Arduino IDE v1.0.5 for Arduino Uno
Code Downloader:
As all the compiling process done in the host pc, then we have to download code
to board via type B USB cable. Arduino UNO has on board type B USB connector. A single
click of "LOAD" in arduino IDE will download code to arduino Uno board.
20
Power Supply:
As mentioned above it operating voltage at 5v, it can be powered by different forms.
1st by (External power supply pin),
2nd by (type B USB port draws current host pc) and
3rd by (On board pin for power supply from other board).
21
3.3 Arduino Due
An Arduino Due is a Atmel SAM3X8E ARM Cortex-M3 microcontroller[16]. It is a
Thumb-2 Instruction set. It has 54 IO pins to connect digital sensors. It able to handle 12 analog
inputs and additionally it has 4UART for serial communication and 2 DAC(Digital to Analog
Converter). It supports board to board communication like Arduino UNO additionally with CAN
BUS protocol. This board operating at 3.3v it will not tolerate higher voltage like 5v. With
compact size of length and width of 4 and 2.1 inches. Cost is quite high among arduino
microcontroller's.
Table 3.2 Key Specification of Arduino Due
Input
Voltage
Flash
Memory
SRAM DAC Clock Speed SPI Clock
Speed
Software
7-12 V 512 KB
96KB 2 84MHz 4MHz
(default)
Arduino
IDE
v1.5.3
Figure 3.6 Arduino Due board specifications
22
Figure 3.7 Arduino Due Schematic diagram
Flash Memory for source code:
With the 512 KB of flash memory we can flash large user application of our code to the
memory. It has dedicated memory for boot loader.
23
Clock Frequency:
The Arduino due has 84MHz clock speed for operating. For SPI specific it limited to
42MHz, PIN 110 drives the clock for SPI.
3.3.1 Arduino IDE:
As mentioned in 3.2.1 we can understand about arduino IDE. There is a separate version
IDE for Arduino Due. To change type of board we have to select in the board option under tools.
Figure 3.8 Arduino IDE v1.5.3 for Arduino Due
24
Code Downloader:
As all the compiling process done in the host pc, then we have to download code
to board via programming port (micro USB) near to external power supply. A single click of
"LOAD" in arduino IDE will download code to Arduino Due board.
Power Supply:
As mentioned above it operating voltage at 5v, it can be powered by different forms.
1st by (External power supply pin),
2nd by Native usb port (micro USB)and
3rd by (On board pin for power supply).
Resource List:
Table 3.3 Cost of Resource
S.No Items COST in S$
1 Raspberry Pi - Model B 49.9
2 Arduino Uno R3 32.7
3 Arduino Due 71.0
4 Level Shifter 1.9
Source: www.sgbotic.com
25
Chapter 4: Experimental Analysis & Implementation Issues
4.1 Interface between Arduino UNO and Raspberry pi:
Arduino is a very good microcontroller to connect sensors and read values of it. With
availability digital IO pins easy to hook up peripherals. Arduino purpose to read only sensor
values and processing values are done in Raspberry pi[17][18][19] since it has enough RAM. As
mentioned above, SPI protocol is the high speed way of communication between boards. To
communicate arduino and raspberry pi things like clock speed, voltage on pins and available
memory are to be considered.
Figure 4.1 Interface between Raspberry pi and Arduino Uno via SPI
26
In raspberry pi pins 19-MOSI,21-MISO,23-SCLK,6-GND[20] has been connected to
Arduino pins 11-MOSI,12-MISO,13-SCK,GND[21] together for SPI implementation. Wires of
MOSI, MISO, SCK are connected together with level shifter.
4.1.1 Communication Bottlenecks:
To get data byte from arduino uno we should send a byte from raspberry pi(master).
Since arduino uno is 8-bit microcontroller. Only one byte at a time is quite slow process of
communication in one transfer. By doing one byte transfer we will not achieve maximum data
transfer rate of SPI protocol standard. So we going to come up with a solution to eliminate less
data throughput between boards.
4.1.2 Increasing clock Speed:
Clock speed is the major factor to use SPI for high speed data transfer. Master initiates
communication by sending command byte to slave. First bit of command byte transferred when
clock is high. And receives first bit of data byte on low. By default arduino SPI clock speed is
4MHz whereas in Raspberry pi is 16MHz. To synchronize communication with two boards we
must run both boards at same clock speed. Obviously we have to run at 4MHz in both boards to
achieve maximum speed communication. For initial communication and analyze data
throughput, clock speed in raspberry pi is gradually increased from 500KHz to maximum speed.
Results of data received shows that the data transfer rate increased when speed is doubled with
the previous speed it was ran.
Clock speed in Raspberry pi can be increased to its maximum speed but after 4MHz there
is no improvement in the data throughput. At this position increasing speed still works with data
transfer but missing of bytes in a packet is started. This problem is arrives due to improper
synchronization with the arduino packet message. Since arduino uno supports maximum SPI
clock speed of 4MHz. Before it complete full packet transfer next command byte is received in
arduino to send next packet. Because of limited size of RAM it not able to hold data which have
to be send.
By increasing clock speed the percentage of delay between each byte is reduced. As the
percentage of delay is reduced which results increase in data transfer rate. Since raspberry pi has
enough RAM is doesn't disturb data transfer and data processing. SPI master it doesn't know
27
anything about the slave which is connected to it, it's just send and receive byte with the clock
for every transfer. As a serial transfer it send and receive byte by byte between master and slave.
4.1.3 Operating Voltage:
The operating voltage for Raspberry pi GPIO pins is 3.3v and Arduino GPIO is 5v. For
serial communication there will be no issue with voltage whereas incase for doing SPI
communication it should be at same operating voltage because master running clock otherwise it
will affect communication protocol. And also it creates latency issue with transfer of bytes to and
fro. So there is a need of level shifter between these two boards. Level shifter will shift 3.3v pins
from raspberry pi to 5v pins in arduino. MISO,MOSI and CLK are the three pins and one more
pin for GND, the need of common ground connection between two board is protect higher
voltage pins will not affect lower voltage pins.
Figure 4.2 Level Shifter for 3.3v to 5v
4.2 Communication analysis for Arduino Uno
When doing communication with arduino Uno if we send one byte at a time it response
with a byte and if we send two byte at a time it will response for first byte and for second byte it
will not give response. This is due to SPDR register RESET signal is goes high after a transfer of
a byte is completed since it is a 8-bit microcontroller. The second byte will be revert back to
sender. Eg. if we send 100 bytes in a single transfer, the first byte will be responded and
remaining bytes will be revert back to sender.
28
4.3 Components for communication platform
Required system components are listed below. Each component is described briefly for
what purpose it has been used.
Sensor
Level Shifter
Monitor with HDMI port.
Cabling
Power Supply.
Sensor:
Various sensor available for robotic applications like camera, temperature, switch read,
joystick, light, sound, accelerometer, etc. These kind of sensors are available in the market to
hook up with arduino.
Level Shifter:
Since both board operating voltage are different, there is a need of level shifter which has
discussed in section 4.1.3 for arduino Uno. And no need for arduino Due.
Cabling:
As known SPI is a 3 wire communication protocol(MOSI,MISO,SCK) pins to be
connected together in each board (Master & Slave). Additional cable is required if adding
multiple slave to the master. Since both board has different operating voltage, here a level shifter
is used to convert voltage level between them for IO pins. So each pin has been connected from
raspberry pi to level shifter and then to arduino Uno pins of (MOSI - pin 11, MISO - pin 12,
SCK - pin 13 and pin 10 for SS). Without a bread board it's not possible to interconnect board
together.
Monitor with HDMI port:
For evaluating the communication process between arduino and raspberry pi, a monitor
with HDMI port is used to display data received by raspberry pi via HDMI port available in
Raspberry pi.
29
Power Supply:
It has been discussed in section 3.2.1. External power supply require separate wire to the
board. As a compact system it should not require external power supply for each board. so power
is drawn from another board.
Benefits of Arduino DUE over Arduino UNO
More number of digital IO pins and analog pins.
Increased clock speed of microcontroller.
SPI supported clock speed is higher.
Number of SPI channels is more.
Available of 2 DAC.
32bit ARM cortex processor.
4 High speed serial communication ports.
High speed CAN BUS protocol.
More Flash memory for user application.
More SRAM to handle data.
Same operating voltage 3.3v as Raspberry pi.
8-16 bits data length of SPI transfer.
30
4.4 Interface between Arduino DUE and Raspberry pi:
Arduino Due can be best replacement in terms of SRAM, clock speed and number of bits
transfer for arduino Uno.
Figure 4.3 Interface between Raspberry pi and Arduino Uno via SPI
In raspberry pi pins 19-MOSI,21-MISO,23-SCLK,6-GND has been connected to Arduino
Due pins 109-MOSI,108-MISO,110-SCK,GND together for SPI implementation.
4.4.1 Communication Bottlenecks:
In SPI communication 8 bit transfer is a default, here arduino due can be configured to
8-16 bits transfer. To get 16bit data from arduino Due we must transfer 16 bit data transfer from
raspberry pi. By making CS(chip select) low until two byte transfer we can achieve this.
31
4.4.2 Increasing clock Speed:
By default arduino due has SPI clock speed of 4MHz and it can be configured up to
84MHz[22]. Here arduino due is configured as slave so there is no need of setting clock speed.
Compare to Uno it supports higher speed for SPI communication with raspberry pi. It results
increased data transfer rate.
4.4.3 Operating Voltage:
Since both arduino due and raspberry pi has same operating voltage there is no need of
level shifter between them.
4.5 Communication platform with multiple slaves or nodes
Reasons for not testing with multiple slaves or nodes are first when replacing serial
communication with SPI protocol how data throughput is increased?, after analyzing it secondly
our aim is set to test with single node with multiple slaves and with large amount of data to
check how our system able to handle bottleneck, third the current budget was limited with the
available resource, it takes additional time to test analysis part if we have multiple nodes, and the
result of the current analysis is not considered for future work.
What happen if we have two or more data collection node? In near future we may add
multiple data collection nodes to add additional sensors. If we use either SPI or I2C data
throughput depends on clock speed and length of bits for a packet going to be transfer. Once both
terms is set to maximum it will not make change in data throughput when we add multiple slaves
or multiple data collection node into bus protocol. Because data collection node or slave to
collect data is decided by master by selecting slave select line. If slave select is set to HIGH it
will not be active for data collection. Once it is set to LOW master starts to read data from it and
rest of the slave set to HIGH. By coding mistake if we set LOW for two slave at a time data will
collide with each other. Even though we add multiple number of nodes the resultant data
throughput will not increase. Since data throughput increase is based on only clock speed and
length of bits per transfer.
32
Chapter 5 Experimental Results
5.1 Experimental Results and Limitations of Hardware:
Here SPI protocol is chosen, it is the high speed protocol for board to board
communication with limitation of standard up to 4Mbytes/sec and without limitation it can go up
to 10Mbytes/sec. Drawback of SPI protocol is doesn't have ECC for incoming data. In my work,
I have done Error detection and Correction for incoming data, compression of data with offline
and online stream of data added for SPI protocol. I have configured Arduino UNO and Arduino
DUE as slave in two different cases. Since arduino going to be data collection node connected
with sensors and read data from it and pass data to raspberry pi for further data processing. Here
I configured Raspberry pi Type B as Master. Due to limitations with hardware for
communication in Arduino Uno I have extended my work with Arduino Due.
Two cases:
i. Raspberry pi(Master) and Arduino Uno(Slave)
ii. Raspberry pi(Master) and Arduino Due(Slave)
In further section i have explained briefly about using Arduino UNO and Arduino DUE as SPI
slave.
Raspberry pi(Master) and Arduino Uno(Slave)
To begin communication between arduino uno and raspberry pi i have used UART to
transfer data from arduino to raspberry pi via USB cable since raspberry pi configures USB port
as UART. Without any lag i can able to receive data which has been sent from arduino UNO.
Next SPI protocol has been implemented. In arduino Uno i kept array of bytes to test the
communication. As the SPI communication data throughput is based on clock speed, it can be
changed to various speeds up to the maximum supported speed. Here raspberry pi is activated
with various clock speed minimum of 500KHz and i designed my software to measure incoming
byte speed. Then i increased clock speed in raspberry pi gradually in frequencies of 1MHz,
1.5Mhz, 2MHZ, 2.5MHz, 3Mhz, 3.5MHZ, 4MHz. 4MHz is the maximum clock speed supported
by arduino Uno.
33
In below Figure 5.1 Green line indicates the data transfer rate without any missing of
data, after 4MHz the red line indicates data throughput with missing of bytes while transfer.
Table 5.1 Receiving data speed in Raspberry pi
Clock speed in KHz Data Receive speed in Bytes/Sec
500 11.16
1000 14.71
1500 14.99
2000 18.7
2500 18.84
3000 18.68
3500 18.51
4000 24.42
4500 24.4
5000 23.95
5500 25.02
At 4MHz system able to transfer around maximum of ~25.47KBytes/sec.
Figure 5.1 Graph for receiving data in Raspberry Pi
0
5
10
15
20
25
30
500 1000 1500 2000 2500 3000 3500 4000 4500 5000 5500
Dat
a Tr
ansf
er
Rat
e in
KB
yte
s/s
Clock Speed in KHz
Data with valid bytes
Data with corrupted bytes
34
Figure 5.2 Receiving speed of data in Raspberry pi
When using SPI communication we can improve data transfer rate by 60% to
25.42KB/s instead of using serial communication.
35
From the Figure 5.1 i have observed two things. First the data transfer rate is not a
straight line and second scale factor is not 1:1.
For the first, the data throughput doesn't increase linearly with clock speed. Raspberry Pi
only works with the certain clock speeds of 500KHz,1MHz,2MHz,4MHz,8MHz and 16MHz. If
we set clock speed in between these values it will not make effect in change of clock speed.
Secondly the data transfer doesn't have scale factor of 1:1. For every transfer the system
will configure the SPI protocol of clock speed and allocate memory size. In Linux based system
each process has been written to a file and handled by the kernel. i.e. to access hardware SPI
pins. So if we transfer just 1 or 2 bytes per transfer the percentage of latency is high in between
transfer. It means the percentage of configuration time is large compare to less number of data
bytes. So data transfer rate doesn't linearly increase with the clock speed. Here latency is about
the percentage of configuration time. To increase efficiency we should transfer large block of
bytes at a time. If we increase data block size, it will improve the percentage of efficiency of the
data transfer. It means the percentage of configuration time is less compare to large block of
data. So we can able to receive data at higher rate.
To analyze efficiency with large block of data, I started to test with large block of data.
Since Arduino Uno cannot send more than one byte at a time. Because all arduino controller are
designed to transfer 8bit or 16bit at a time. So we cannot send more than two bytes at a time. To
analyze efficiency with large block of data I used Raspberry Pi by simply loopback data to it. To
test it, I connected MOSI to MISO together to receive and measure the data which sent through
MOSI and it is received by MISO in Raspberry pi.
If i sent one byte at a time i can get back data at 11.16KB/s. What happen if we increase
data bytes for each transfer? Then i sent two bytes at a time the data transfer rate is increased to
37.63KB/s at 4MHz. Here I had noticed if we increase data block size the data throughput is
increased but still the data rate doesn't linearly increase with the clock speed. To find a better
efficiency i then increased data bytes to 100bytes in a single transfer which gives higher transfer
of 295.67KB/s. From this analysis i had noticed, if we are trying to send one byte or two
bytes(less number of bytes) at a time the delay between each transfer of byte is high so the data
throughput is slowing down and if we try to send large number of bytes the percentage of
efficiency is increased.
36
Below graph shows data transfer rate for 100bytes transfer at a time.
.
Figure 5.3 Graph for receiving data in Raspberry Pi
From the above graph you can see the data rate is double when double the clock speed.
While going to higher speed the data rate is reduced since the cpu usage at that speed is almost
full so my software can't able to measure accurately. So it is clear here if we try to send large
number of bytes per transfer the data throughput increase linearly with the clock speed.
And also I have measured the data throughput by scope,
bytes
Speed
1 byte
2 byte 100 bytes
(Time taken to transfer byte + delay between transfer)
500KHz 0.016+0.071 0.035+0.066 1.686+0.203
1MHz 0.009+0.065 0.017+0.065 0.850+0.103
2MHz 0.004+0.056 0.009+0.047 0.429+0.092
4MHz 0.002+0.052 0.004+0.042 0.269+0.084
8MHz -- 0.002+0.039 --
16Mhz -- 0.001+0.032 --
0
100
200
300
400
500
600
700
500 1000 2000 4000 8000 16000
Dat
a Tr
ansf
er
Rat
e in
KB
yte
s/s
Clock Speed in KHz
37
For 500KHz time taken to transfer a byte is .016ms and time delay between each transfer is
0.071ms for a transfer it takes 0.87ms.
1second= 1000/0.87
= 11494.25x1(1byte per transfer)
= 11494.25bytes/s
= 11.22KB/s
At same 500KHz clock speed transferred 2bytes at a time it takes 0.035ms and time delay
between each transfer is 0.066ms for a transfer it takes 0.101ms.
1second = 1000/0.101
= 9900.99x2(2bytes per transfer)
= 19801.98bytes/s
= 19.22KB/s
Similarly for 100 bytes per transfer,
1second = 1000/1.889
= 529.38x100(100bytes per transfer)
= 52938.06bytes/s
= 51.69KB/s
Here with the kernel latency we had scale factor of 1:0.66(clock speed : data transfer rate)
when we transferring one byte at a time and for two bytes transfer scale factor is more or less
same around 0.6 continued here. If we transfer data without latency we can achieve scale factor
of 1:1.
What will happen if increase in clock speed? Will communication work? Yes raspberry
pi can able to receive data even up to 16MHz of SPI clock speed. The thing i observed is the data
38
which has been sent over 4MHz clock speed, arduino begins to skip bytes to send in a packet
which i stored in array of bytes. This kind of data missing is not tolerated in an embedded
system.
I also observed that missing of bytes in an array is not frequent, it skips bytes to send
randomly from the array. This is due to un-synchronization of clock speed with what arduino uno
can support. Here communication based on send and receive a byte at a time. There is no
acknowledgement of data and there is no way for request to resend the packet which fails to
receive packet with all bytes. At this stage i thought to retrieve the data which has been lost. By
means retrieving system can able to build the packet message of its own with the missing bytes.
Then i implemented Error detection and correction for incoming bytes. It also named as
ECC(Error Correcting Codes).
CPU Usage:
It is a important factor to measure to describe the capability of system to handle
communication between boards. It has been measured for both idle and working status of system.
Figure 5.4 CPU usage while IDLE
39
Figure 5.5 CPU usage while running
5.2 Implementation of Error Detection and Correction:
Reason to going for error detection and correction is, there is no acknowledgement for
incoming bytes communication in our system. So it is to ensure that we are receiving correct
bytes which has been sent by sender. One of the disadvantage of SPI protocol is, there is no
support for validation of incoming bytes. So we must write our software to include validate of
incoming bytes. Checksum calculation is a good idea for validate received bytes whether it have
any error in it. But it doesn't do error correction for error bytes. CRC32 is a technique to do error
detection and correction of incoming bytes[23][24].
This technique can be resolve from 1bit to burst bits in a packet. As mentioned above
raspberry pi can able to receive and process data at a time as it has enough RAM, so there will be
no slow in transmission. The disadvantage is from arduino, since arduino is a 8 bit
microcontroller it can able to send one byte at a time, so it's not possible to form a packet of 32
bits with 24bits of data and 8 bits of remainder. Otherwise we can do receive 4bytes one by one
and group to 32bits to do CRC32. But incase if a byte miss happens in between transfer it will
take a byte from next packet. This will go wrong frame of packet hence it very complex to
implement CRC32 in raspberry pi. For reconstructing entire packet it will take additional CPU
usage and may hang system for further communication.
I also noticed that CPU usage in raspberry pi is increasing if implementing CRC32
algorithm. To overcome drawback of CRC32 I implemented an efficient algorithm to detect error
in incoming packet.
40
Table 5.2 Format for Error detection with checksum
SOT COMMAND LENGTH DATA BYTES CHECKSUM EOT
In the above packet SOT is defined as value of a byte which is first byte in the packet,
secondly command byte describes what sensor value it is sending, thirdly length of the data bytes
and next byte is checksum XOR of (Command byte, Length, Data bytes) and finally EOT
describes last byte of packet frame. These process done in arduino before transmitting data bytes
from sensor it is framed as describe above. Reversely it is validated in raspberry pi. Hence this
logic overhead with frame bytes.
5.3 Implementation of Compression:
Limited size of RAM in arduino UNO will can only hold less amount of data before it
transferred to other board via SPI . It's better to build a library for arduino to compress data
before it transferred to raspberry pi. By compressing data we can get some memory space in
SRAM memory for additional data to hold. RLE(Run Length Encoding), Huffman code, gzlib
and Lempel-Ziv-Welch are considerable compression algorithms for byte compression. RLE
works if bytes are repeated in a packet message[25]. Huffman code each value is considered as
symbol and then transferred[26]. SPDR data register of SPI is 8 bits it's possible to send only a
byte at a time via SPI. Instead of sending byte by byte we look to build a library for byte
compression to reduce byte transfer in a packet.
Before going to implement compression library in arduino we must look at pre requisite
of header files. Header file for library function should be accepted by compiler in arduino IDE.
RLE doesn't need any pre defined function so it will be helpful only if bytes are repeated in a
packet. Complex issues will be resolved by implementing supportable header file for library
function.
For worst case complexity non repeated bytes will not give efficiency by using RLE
instead of that here i consider LZW which identifies repeated pattern of bytes[27] Eg: "36045792
and 20579214" underlined tells certain numbers are repeated in a way. Similarly it perform for
text also Eg: "Helloworld is simple word most used by beginner programmer. It may be lowor
high"
41
For number's it ranges 0-9, 100 combinations for two digit number
For Text A-Z, 676 combinations for two characters.
Total possible combinations will be kept in an array of table. Predefined combinations needs
more memory to save. But the advantage is very high, because it will take less time to compress
data. But in my system compression takes part in microcontroller. In my software design i kept
512bytes for combination array which means it will have combination only after read each
character until it fills my array table.
Eg: "LEFT EFFECT".
Table 5.3 Dictionary table for LZW algorithm
A-Z 0-25
LE 26
EF 27
FT 28
(EF-27)F
29
In my software maximum range of bits is 16, even for data between 0-15 bits. It will
utilize more number of bits for less digit value, when come to long range values it simplifies into
less number of digits to specify. I have also tested with various size of bits for my array table.
For 16 bit data range gave more compression ratio. Here results were tested in raspberry pi.
Since there is no option to check the compression output in arduino. As the data's are transferred
in bytes for communication here test is done for byte values. The input of speech recognizer
sensor will be taken as text. In testing for compression, the input of text also has been tested
here. For distance sensor the values are in co-ordinates values which has been measured from its
origin. For each position it will give numerical values which changes frequently so a random
number of integers are tested here.
"EF" is already in table so it will
read next character. EFF mapped
as 29
42
Figure 5.6 Flowchart for LZW compression algorithm
YES
NO
YES
NO
YES NO Is
STRING+CHAR
in table?
Input next byte store in CHAR
START
Input first byte store in STRING
Is table size >512?
more bytes to input?
output the code for
STRING
STRING = STRING + CHAR
add entry in table for
STRING+CHAR
STRING=CHAR
output the code for
STRING
END
43
Figure 5.7 Running compression for random integer and large text.
File
Content
Factors
Numbers
Text
Bytes(16bits)
Float values
File size 22.07KB 50.82KB 48.29KB 49.02KB
Compressed File size 12.26KB 39.5KB 33.85KB 20.88KB
% compressed file size 55.55% 77.72% 70.09% 42.6%
Table 5.4 Compression results for various files.
In Arduino Uno it has 2KB of SRAM, algorithm array table acquires 512bytes and rest
will be for reading data's. If we are compressing a text file, it fetches around only 1500bytes of
data from the file. It will be very less for better compression ratio, this is a drawback i have faced
in testing. Even without compression system support to transfer of around 24KBytes per second.
44
Raspberry pi(Master) and Arduino Due(Slave)
As mentioned above arduino Due has more advantage in terms of hardware than arduino
Uno. SPI clock speed and Transmit data register range was high and advantage to communicate
with raspberry pi. In this section i will discuss about configuring arduino Due as slave and
implementing SPI protocol. Both arduino Uno and Due has different microcontroller registers
and configuration are entirely different. Most of the library functions will not support on arduino
Due which was supported by arduino Uno.
Figure 5.8 Slave Mode Functional Block Diagram
To configure arduino Due as slave the following register's have to be coded.
SPI_CR (SPI Control Register).
SPI_MR (SPI Mode Register).
SPI_RDR (SPI Read Data Register).
SPI_TDR (SPI Transfer Data Register).
SPI_SR (SPI Status Register).
SPI_IER (SPI Interrupt Enable Register).
SPI_CSR (SPI Control Status Register).
SPI_WPMR (SPI Write Protection Mode Register).
45
To enable SPI interface in arduino Due SPIEN pin SPI_CR register should be set to 1.
31-25 24 23-8 7 6-2 1 0
- LASTXFER - SWRST - SPIDIS SPIEN
Source code:
REG_SPI0_CR=0x1;
To configure as slave MSTR bit in SPI_MR must set to '0'.
31-24 23-
20
19-
16
15-
8
7 6 5 4 3 2 1 0
DLYBCS - PCS - LLB - WDRBT MODFDIS - PCSDEC PS MSTR
Here data register to transmit and receive is different from Uno. For transmit SPI_TDR,
we should pass data to this register to send bytes and the offset should be 0x0C. For receive
SPI_RDR, this register will receive the incoming bytes and the offset should be 0x08.
SPI_TDR Register:
TD-Transfer Data will be transfer depends upon bits going to be received.
SPI_RDR Register:
31-20 19-16 15-0
- PCS RD
RD-Receive Data will be read depends upon bits transferred.
31-25 24 23-20 19-16 15-0
- LASTXFER - PCS TD
46
To configure how many bits will be transferred is set in SPI_CSR register. LSB 0&1 of
this 32bit register will tell about CPOL(Clock polarity) and NCPHA(Clock Phase) respectively.
SPI_CSR Register:
31-24 23-16 15-8 7-4 3 2 1 0
DLYBCT DLYBS SCBR BITS CSAAT CSNAAT NCPHA CPOL
From bit 4-7 is for transmit data bit range.
Table 5.5 Table for configuring BITS in SPI_CSR Register.
BITS 7-4 Name Description
0000 8_BIT 8 bits for transfer
0001 9_BIT 9 bits for transfer
0010 10_BIT 10 bits for transfer
0011 11_BIT 11 bits for transfer
0100 12_BIT 12 bits for transfer
0101 13_BIT 13 bits for transfer
0110 14BIT 14 bits for transfer
0111 15_BIT 15 bits for transfer
1000 16_BIT 16 bits for transfer
To enable WRITE mode for this register WPEN bit in the SPI_WPMR register should be
cleared. Even if we want run in default mode SPI_CSR register must be written. SPI_WPMR
register will tell about WRITE mode for pins. Status of SPI_WPMR can be checked in
SPI_WPSR(Write Protected Status Register).
SPI_WPMR register:
Above register are configured to act as slave and configuring 16bit transfer per
message[28]. As in general bye transfer is possible in SPI with SPDR register. In arduino Due
31-8 7-1 0
WPKEY - WPEN
47
has extended feature to send 16bits at a time by using parameter SPI_CONTINUE, it indicates
next bytes to be send in the packet. SPI_LAST tells which byte is last to be transfer. If we doesn't
specify SPI_LAST for a transfer, then it will consider as last byte in a packet. This logic helps to
frame 32bit transfer of 24bit data and 8bit remainder for implementing ECC code.
Source code:
short value[] = {0x8308, 0x0233};
SPI.transfer(MISO,value[i],SPI_CONTINUE);
SPI.transfer(MISO,value[i++],SPI_LAST);
Here it will transfer 16 bit data, because its configured to sent 16 bit transfer.
SPI_CONTINUE will transfer 16bit data from value array and SPI_LAST will send next byte. It
will shift MSB first in the data transmit register. So array of value first indicates 16bits of MSB
and in next transfer it will shift remaining 8bit with remainder 8bits. By doing this, clock cycle
between each packet will set Slave Select(SS) pin low up to transfer ends. So it resolve problem
for implementing CRC32 in communication which has been faced in arduino Uno.
To receive a 16bit data we need to configure SPI in raspberry pi to send 2 bytes at a time.
Below code will tells us how to configure 16bit transfer from raspberry pi.
Source code:
uin16_t tx = 0x7689;
transfer(fd,(tx>>8),tx,2);
CS(Chip select) have to be set active low until 16bits are transferred.
Before going to transmit next data we have to ensure that last written data in SPI_TDR
has been transferred. To ensure that we should check SPI_SR status register before each
transmit.
31-
17
16 15-
11
10 9 8 7-
4
3 2 1 0
- SPIENS - UNDES TXEMPTY NSSR - OVRES MODF TDRE RDRF
48
Source code:
int SR_8LSB = (int) ((REG_SPI0_SR & 0x00000002)>>1);
if(SR_8LSB)
{
REG_SPI0_TDR = data[i]; // Put data into data transmit register
}
And also we have to check for interrupt based communication in SPI_IER(Interrupt enable
Register). A bit set in TDRE pin defines whether to enable interrupt or not.
31-11 10 9 8 7-4 3 2 1 0
- UNDES TXEMPTY NSSR - OVESR MODF TDRE RDRF
49
5.4 Theoretical Result:
As of now Arduino DUE SAM3x8E of ATMEL microcontroller Interrupt Service
Routine(ISR) to enable SPI as slave has yet not been coded from the developer side[29][30][31].
Here i did mathematical result which tells the expected data transfer rate. In Arduino UNO able
to receive ~25.42KB/s at 4MHz clock speed with 8 bits transfer at time. In Arduino DUE clock
speed is increased to 16MHz which is four times of Arduino UNO and transfer bit range(size of
data) is increased to 16bits per transfer which is double from Arduino UNO. So, expected result
will be
Arduino Uno Arduino Due
Clock Speed(MHz) 4 16
Number of transfer bit range 8 16
Expected Data rate = 25.42*4*2
= 203.36 KBytes/sec
From Figure 5.1, the data transfer can continue beyond 4MHz in Arduino Uno but with
missing of data while transfer. By that performance we can expect more data throughput with
higher clock speed in Arduino Due.
50
5.5 Optimizing Boot time of Raspian Wheezy OS:
In general, Raspian Wheezy OS takes approximately 2minutes to boot up with full
features of a Linux system[32]. In our application, most of the programs can be muted to reduce
boot time of raspberry pi. The following steps will make raspberry pi to run our application
within 30seconds.
Step 1: Overclock clock speed of system. It can be done by configuring the raspian os.
Command: sudo raspi-config
Step 2: Configure Kernel file of
/boot/cmdline.txt
Replace these lines with the existing
dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootfstype=ext4
rootflags=commit=120,data=writeback elevator=deadline noatime nodiratime data=writeback
rootwait quiet
Step 3: Configure System file of
/etc/sysctl.conf
Add these lines to end of file
vm.dirty_background_ratio = 20
vm.dirty_expire_centisecs = 0
vm.dirty_ratio = 80
vm.dirty_writeback_centisecs = 1200
vm.overcommit_ratio = 2
vm.laptop_mode = 5
vm.swappiness = 10
After applying above changes run command from terminal to made changes effective.
Command: sudo reboot
51
Chapter 6 Summary
Conclusion:
Scope of this project is to build a compact system for robotic application with high speed
data transfer between Atmel microcontroller and raspberry pi. For high speed bandwidth
communication SPI has been chosen. Usual SPI based communication systems try to transfer
data with the clock speed, when exceeds clock speed the data transfer drops from that point. In
my work I have made my software to transfer data by implementing strategies like compression
and error correction and detection. It enables the system to push more with the maximum clock
speed. Usual microcontroller will do SPI communication between sensor's and microcontroller,
here sensor's data are collected and transferred to raspberry pi. In first case Arduino Uno is
configured as SPI slave with master raspberry pi, SPI communication has been established
between them and able to transfer data at a rate of ~25.5KBytes/sec maximum. With compare to
serial communication of 115200 bits/sec (14.06KBytes/sec) nearly data rate is doubled. With the
less amount of clock speed compared with raspberry, Arduino Uno has only 4MHz with 8bit at a
time transfer, the communication result is not achieved as expected. And with 2KB of SRAM is
not a proposed sensor node for future with high end sensor.
In second case Arduino Due is replaced for Arduino Uno, SPI communication has been
established between them and can able to transfer data. With the option of 8-16bits transfer at a
time doubles the data transfer bits for every transfer and with the 16MHz clock speed the data
throughput rate has been multiplied by four times of what Arduino Uno can, theoretically we can
receive around ~203.36KB/s. From the results of arduino Uno, we can find system
communication doesn't drop data transfer when exceeding maximum clock speed but with
drawback of data loss while transfer.
Since SPI doesn't has the support of error correction and detection here CRC32 has been
implemented for validating incoming bytes. At certain cases compression is required for repeated
kind of data from sensors. Instead of transmitting same values of data it is suggested to
implement to compression algorithm for data's. The compression part takes place in arduino
microcontroller and decompression takes place in raspberry pi.
52
Future works:
In my current work, Error detection and correction working fine up to 1bit error. To get
more performance ability of ECC it can be extended to 2bit or burst bit error correction in data. It
helps to reconstruct data when data corruption happen with multiple bit error in data while using
maximum clock speed. In data compression 512bytes of data library with 16bit resolution can be
increased to get increase in percentage of compression for data and it reduces the compression
time. Arduino due has more SRAM than arduino uno so we create more than 2KB of data
library with 32 bit resolution. It will increase the compression ratio. If we increase compression
ratio we can able to send more amount of sensor data. This compression helps a lot when sensors
are replaced with high resolution data bit sensors in future.
References:
1. Daode Zhang, Yourong pan and Xinyu Hu, "Design of High-Speed Parallel Data Interface
Based on ARM & FPGA", Journal of Computers, Vol. 7,No.3, March 2012.
2. John D. Wason and John T. Wen, " Robot Raconteur: A Communication Architecture and
Library for Robotic and Automation Systems", IEEE International Conference on
Automation Science and Engineering Trieste, Italy - August 24-27, 2011.
3. Motorola Inc., “SPI Block Guide V4.01”, July 2004.
4. F. Leens, “An Introduction to I2C and SPI Protocols”, IEEE Instrumentation & Measurement
magazine, pp 12-13, February 2009.
5. Design Note by Zoltan Monar, Using the SPI as High Speed , Bidirectional Data Bus With
Automatic Master/Slave Switch, 2002.
6. SPI Interface http://www.eeherald.com/section/design-guide/esmod12.html
7. Mingji Ban and Sung Ho Cho, “An Efficient Communication Method between Processors in
an Embedded Communication System”, International Conference on New Trends in
Information and Service Science 2009.
8. A.K.Oudjida, M.L. Berrandjia, A.Liachia, R.Tiar, K.Tahraoui & Y.N.Alhoumays, “Design
and Test of General-Purpose SPI Master/Slave IPS on OPB Bus"
9. Dimitrios N Serpanos, “Speeding Up High-Speed Protocol Processors”.
10. Raspberry Pi official website: http://www.raspberrypi.org/quick-start-guide
53
11. Guide for Raspberry Pi beginners http://elinux.org/RPi_Beginners#Beginner_Projects
12. Enabling SPI Interface http://www.hobbytronics.co.uk/raspberry-pi-raspbian-distro
13. Function access for broadcom BCM2835
http://www.airspayce.com/mikem/bcm2835/group__spi.html
14. Arduino UNO official website: http://arduino.cc/en/Main/ArduinoBoardUno
15. ATMEL 328P document: http://www.atmel.com/Images/8161s.pdf
16. Arduino DUE official website: http://arduino.cc/en/main/ArduinoBoardDue
17. Library code for Raspberry Pi to configure as SPI Master
https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPiSPI.c
18. SPI driver file for Raspberry Pi https://github.com/WiringPi/WiringPi/tree/master/wiringPi
19. SPI for Arduino http://www.gammon.com.au/forum/?id=10892
20. Raspberry Pi Model B IO pins http://www.raspberry-projects.com/pi/pi-hardware/model-b-
io-pins
21. Setup and use of SPI in Arduino Uno: http://www.atmel.com/images/doc2585.pdf
22. Arduino Due SPI Clock Divider http://arduino.cc/en/Reference/SPISetClockDivider
23. Wikipedia- Error detection and correction
http://en.wikipedia.org/wiki/Error_detection_and_correction
24. Explanation of Cyclic Redundancy check by Dave Janecek
http://www.csm.ornl.gov/~dunigan/crc.html
25. Wikipedia- Run length encoding http://en.wikipedia.org/wiki/Run-length_encoding
26. Wikipedia-Huffman coding http://en.wikipedia.org/wiki/Huffman_coding
27. Wikipedia- Lempel–Ziv–Welch
http://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
28. Configure SPI for 16 bit data transfer
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=44&t=38830
29. SPI driver implementation file for SAM3X8E
https://github.com/phyrwork/nspi_arm/blob/master/nspi_arm.cpp
30. Arduino issue for Slave mode support https://github.com/arduino/Arduino/issues/965
31. Arduino DUE forum http://forum.arduino.cc/index.php/board,87.0.html
32. Start program before login
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=31&t=31567