Ch10-Global Supply Chain - Cadena de Suministro.pdf
Spi research paper
1. 1
Serial Parallel Interface Via STM32 and CANbed
Research Paper
By Quyen Vu
California State Polytechnic University, Pomona
ETE4000
Advisor: Dr. Scott Boskovich
4. 4
Background
I would like to introduce a short background of the class materials incorporated in this project.
The 4 courses that helped me tremendously in this project are the C++ Language (ETE1151),
Microcontroller and PIC18F (ETE3441), Data Communication and Networking (ETE4351). The
C++ course helps me to better understand coding language and to program STM32 and Arduino
Leonardo Board. The ETE344 course helped me understand the input/output of the controller
and how to navigate through the pin mapping of the Nucleo Board. The course ETE4351 helps
us understand how serial data transfer from one device to another and communication protocols.
Figure 1: 4 Books to reference for this research paper.
5. 5
Abstract
Serial Peripheral Interface (SPI) is an interface bus commonly used to send data between
microcontrollers and small peripherals such as shift registers, sensors, and Arduino. It uses
separate clock and data lines, along with a select line to choose the device you wish to talk to.
This type of communication is considered Full-Duplex at a very high-speed rate. This interface
will be implemented into another project for Water/Methanol Injection Controller with CAN-bus
capability. The speed of the CAN-bus in an automotive vehicle runs at a speed of 500kbps.
Serial-Parallel Interfaces for rapidly and efficiently transferring data from a CANbed
(ATmega32U4) to STM32 Nucleo Board. This interface design will perform the best out of all
the interface protocols and provide reliability and productivity.
Introduction
SPI, which is short for Serial Peripheral Interface, is one of the frequently used communication
protocols for transfer of data between a Microcontroller and a wide range of peripheral devices.
SPI is a synchronous, full duplex master-slave-based interface. The data from the master or the
slave is synchronized on the rising or falling clock edge. Both master and slave can transmit data
at the same time. The SPI interface can be either 3-wire or 4-wire. For this research, I will be
using the CANbed Board and STM32 to implement the SPI interface for transferring data. The
reason for this is a side project separate from this research. My partner and I are both developing
and designing a Water/Methanol Injection Controller for MINI Cooper S 2006 car. We need to
retrieve serial data from the vehicle CAN-Bus line and transfer it to a STM32 board for
processing. In order, to achieve fast transferring data and reliability, we choose the SPI interface
to incorporate in the design. This research will provide us data and analysis if using SPI interface
over UART and I^C interface is a logical decision.
6. 6
Water/Methanol Injection Controller
A summary of the side project I am working on with my partner with SPI implementation. Our
senior project is designing a progressive injection controller for vehicles that are forced
induction. Forced induction vehicles tend to run hotter than conventional naturally aspirated
engines. Due to the extra components of supercharger and turbocharger creating more thermal
heat in the engine bay. Automotive manufactures use heat exchangers like the air-to-air
intercooler and water to air intercooler for the cooling effect of the intake temperature going into
the engine. However, that alone is not enough to cool the intake temperature low enough to
prevent knocks on a hot summer day. The reason why we use methanol in our project is because
methanol is a fuel additive. It raises the octane of the gasoline and burns cooler than regular
gasoline. As a result, the fuel burns more efficiently and creates less heat in the combustion
chamber which lowers the exhaust gas temperature (EGT). This is a very useful additive because
it will prevent the engine from knocking and the catalytic converter from overheating. The
advantage of spraying a mixture of 50/50 concentration of water/methanol into the intake
manifold pre-throttle body will cool the air mixture entering the engine and making the air
denser. In addition, we want to implement the CAN-bus interface to our system to capture the
temperature and boost pressure of the car using the CAN-bus network in the MINI Cooper. As
well as including the SPI interface to transfer data from our CANbed board to a STM32 for data
processing and controlling the Injection.
Objective
7. 7
Different techniques have been employed to transfer data from one device to another. The
common technique is just traditional wiring from one component to another with no interference
and speed in mind. However, for this research I want to analyze the Pros and Cons of SPI
interface versus UART and I2C. Also, to understand how data is transferring through the SPI
interface and the robustness of the design. In addition, I want to implement the interface to work
with the two microcontrollers and apply the technique to the Water/Methanol Injection
Controller project.
Purpose
The purpose of this paper is to research and to grasp a better understanding of SPI and data
transfer from different devices. The understanding and design idea will help towards the
completion of the CANbed communicating with the STM32 side project. Also, the purpose of
this research is to compare different types of communication interface. In addition, I want to
learn the usage of SPI in the industry and how companies utilize the techniques for their products
and implementation.
Brief History of SPI
The Serial Peripheral Interface (SPI) is a synchronous serial communication interface
specification used for short-distance communication, primarily in embedded systems. The
interface was developed by Motorola in the mid-1980s.Microcomputer systems developed in the
last quarter of the 20th century, employ wide, parallel busses in order to accelerate processing.
While this many-wire, multiple device complexity may be important to achieve high speed in a
computer system, it is generally not necessary in a modern embedded IoT device. Using a serial
bus (like SPI or I2C) can reduce the 30+ wires of our memory example to just a few. SPI rapidly
8. 8
became a de-facto standard. Because of its benefits and utility, it has been widely adopted by
industry.
Development Boards
Figure 2: Shows the two development boards that were used in this project; the CANbed and the
STM32 Nucleo-144. The CANbed contains an onboard processor: ATmega32U4 microcontroller
while the Nucleo-144 uses an onboard processor: Arm Cortex-M4. It has a clock speed of 80Mhz
and supports Arduino connectivity.
The CANbed task is to read the CAN-Bus protocol serial data outputted by the car OBD-2 port.
In addition, the CANbed job is to filter out the undesired serial data that I don’t need for the
Water/Methanol Injection Controller. The data that I want to capture from the board is the rpm,
intake temperature, and manifold boost pressure. This data will then be sent out to the STM32
board as input for the PID for progressive controlling.
The STM32 task is to receive the data sent out by the CANbed board to be processed and input
into the PID for controlling the water/methanol pump.
9. 9
CAN Bus System
What is a CAN bus?
The Controller Area Network (CAN bus) is the nervous system, enabling communication.
In turn, 'nodes' or 'electronic control units' (ECUs) are like parts of the body, interconnected via
the CAN bus. Information sensed by one part can be shared with another. CAN bus is a robust
vehicle bus standard designed to allow microcontrollers and devices to communicate with each
other's applications without a host computer.
Figure 3. The interconnection of the CAN bus network in an automotive vehicle
CAN uses a differential signal with two logic states, called recessive and dominant. Recessive
indicates that the differential voltage is less than a minimum threshold voltage. Dominant
indicates that the differential voltage is greater than this minimum threshold. Interestingly, the
dominant state is achieved by driving a logic '0' onto the bus, while the recessive state is
achieved by a logic '1'. This is inverted from the traditional high and low used in most systems.
Using our knowledge from the course ETE4351 we learned that the CAN is a CSMA/CD
protocol, meaning each node on the bus can detect collisions and back off for a certain amount of
time before trying to retransmit. This collision detection is achieved through a priority arbitration
based on the message identifiers. In order, to send data from one device to another we need to
understand the data frame being sent from the OBD-2 port of the MINI Cooper.
CAN Bus Data
10. 10
Figure 4: Data frame of the CAN-bus signal
The standard CAN message frame consists of number of bit fields. The frame begins with the
start of the frame and the 11 bits ID for priority control, which establishes the priority of the
CAN message. The lower the bit size the higher the priority of the message. The cyclic
redundancy check (CRC) is a 16-bit checksum for detecting errors in the transmitted data. If the
message is properly received, the receiving node overwrites the recessive acknowledge bit
(ACK) with a dominant bit. The ACK also contains a delimiter bit to keep things synchronized.
From the data network course, we learned the importance of the CRC32 function in a
communication network. The CRC is an error-detecting code commonly used in digital networks
and storage devices to detect accidental changes to raw data. Blocks of data entering these
systems get a short check value attached, based on the remainder of a polynomial division of
their contents. In order, to capture data from the CAN bus network from the car I need a system
that could read the CAN bus protocol and filter the serial data signals to utilize for the project.
This frame format is important to understand how the CAN-bus operates and to decode the serial
data into value to send out to the SPI.
MCP2515 Chip
Microchip Technology’s MCP2515 is a stand-alone Controller Area Network (CAN) controller
that implements the CAN specification, Version 2.0B. It is capable of transmitting and receiving
both standard and extended data and remote frames. This chip also includes the SPI protocol
block to Writing to, and reading from, all registers is accomplished using standard SPI read and
write commands, in addition to specialized SPI commands. This chip is included in the CANbed
board which also has the ATmega32U4 chip that runs on Arduino Leonardo.
11. 11
Figure 5: MCP2515 Chip
MCP2551
The MCP2551 is a high-speed CAN, fault-tolerant device that serves as the interface between a
CAN protocol controller and the physical bus. The MCP2551 provides differential transmit and
receive capability for the CAN protocol controller. This unit will receive the serial data frame
from the OBD-2 port which connects to the CAN protocol. It then transmits the serial data that
output from port to the MCP2515 for controlling and processing.
Figure 6: MCP2551 Chip
12. 12
Figure 7: MCP2515 Block Diagram
The MCP2515 chip is very user friendly; it includes the SPI interface on the same chip to be able
to send out the CAN serial data without going through Arduino IDE to figure out the output of
the signal. This means instead of doing the first order filtering for the CAN signal I can now
retrieve the CAN-bus signal using the SPI interface port to the STM32 board. The CANbed
board comes with the Arduino Leonardo interface for light coding and processing. However, I
want to be able to process the data sent out from the CAN bus with speed and efficiency.
SPI Protocol
There are 4 different SPI bus standards that all have to do with the SCK signal. The 4 modes are
broken down into two parameters, CPOL and CPHA. CPOL stands for Clock Polarity and
designates the default value (high/low) of the SCK signal when the bus is idle. CPHA stands for
Clock Phase and determines which edge of the clock data is sampled (rising/falling). The data
sheet for any device will specify these parameters so you can adjust accordingly. The most
common settings are CPOL=0 (idle low) and CPHA=0 (sample rising edge).
13. 13
Example:
Figure 8: Example of a SPI Transmission from Master to Slave
The SPI transmission is controlled solely by the master. The master generates the clock and
controls the slave select signal. This means that the slave has no way of sending data to the
master on its own. Each SPI transfer is full duplex, meaning that data is sent from the master to
the slave and from the slave to the master at the same time. There is no way for a slave to opt-out
of sending data when the master makes a transfer, however, devices will send dummy bytes
(usually all 1's or all 0's) when communication should be one way. If the master is reading data
in for a slave, the slave will know to ignore the data being sent by the master.
MOSI- Master Output; Slave Input
MISO- Master Input; Slave Output
This SPI interface is very good at transmitting data because it supports a hardware cyclic
redundancy check (CRC) feature for reliable communication: the CRC value can be transmitted
as last bytes in transmit mode and an automatic CRC error check is done on the last received
bytes. The CRC method can detect both single-bit errors and burst errors. For example, CRC-16
can detect all burst errors of 16 bits or less.
14. 14
Figure 9: CRC-16 Schematic
The figure 7 shows the schematic of CRC and how it uses the XOR gates to detect the error from
each register.
UART vs I2C vs SPI
UART
It stands for Universal Asynchronous Receiver/Transmitter. It's not a communication protocol
like SPI and I2C, but a physical circuit in a microcontroller, its use two wires to send data called
Tx and Rx. UARTs transmit data asynchronously, which means there is no clock signal to
synchronize the output of bits from the transmitting UART to the sampling of bits by the
receiving UART. Instead of a clock signal, the transmitting UART adds start and stop bits to the
data packet being transferred. These bits define the beginning and end of the data packet so the
receiving UART knows when to start reading the bits.
Figure 10: UART Frame Format
The UART frame does not have a CRC or Acknowledgement bit to distinguish if the frame is
corrupt or received by the destination address.
I2C
15. 15
The Inter-Integrated Circuit (I2C) Protocol is a protocol intended to allow multiple peripheral
likes: shift register, sensor, and device to communicate with one or more controllers. I2C is a
stable communication protocol for embedded applications that are well suited for the embedded
world. Similar to the UART the I2C only requires 2 wires to operate. The I2C interface is a half
duplex which mean only one get to talk at a time.
SDA (Serial Data) – The line for the master and slave to send and receive data.
SCL (Serial Clock) – The line that carries the clock signal.
Figure 11: I2C wiring
Figure 12: I2C Frame format
Like SPI, I2C is synchronous, so the output of bits is synchronized to the sampling of bits by a
clock signal shared between the master and the slave. The clock signal is always controlled by
the master. Similar to the SPI, each frame in a message is followed by an acknowledge/no-
acknowledge bit. If an address frame or data frame was successfully received, an ACK bit is
returned to the sender from the receiving device. This is very helpful to detect loss of frame
during the transfer.
SPI
16. 16
Serial to Peripheral Interface is a very-low-power four-wire serial communication interface. It is
designed so that IC controllers and peripherals can communicate with each other. The SPI bus is
a full-duplex bus, which allows communication to flow to and from the primary device
simultaneously at rates of up to 10 Mbps. This is sufficient speed to operate the CAN-bus
500kbps data rate. Also, the SPI requires low power to operate the interface which is a big
advantage for implementation for automotive.
Advantage and Disadvantages
SPI
Advantages:
● No start and stop bits, so the data can be streamed continuously without
interruption
● No complicated slave addressing system like I2C
● Higher data transfer rate than I2C (almost twice as fast)
● Separate MISO and MOSI lines, so data can be sent and received at the same time
Disadvantages:
● Uses four wires (I2C and UARTs use two)
● No acknowledgement that the data has been successfully received (I2C has this)
● No form of error checking like the parity bit in UART
● Only allows for a single master
I2C
Advantages:
● Only uses two wires
● Supports multiple masters and multiple slaves
● ACK/NACK bit gives confirmation that each frame is transferred successfully
● Hardware is less complicated than with UARTs
● Well known and widely used protocol
Disadvantages:
17. 17
● Slower data transfer rate than SPI
● The size of the data frame is limited to 8 bits
● More complicated hardware needed to implement than SPI
UART
Advantages:
● Only uses two wires
● No clock signal is necessary
● Has a parity bit to allow for error checking
● The structure of the data packet can be changed as long as both sides are set up for it
● Well documented and widely used method
Disadvantages:
● The size of the data frame is limited to a maximum of 9 bits
● Doesn’t support multiple slave or multiple master systems
● The baud rates of each UART must be within 10% of each other
Analysis
Based on the 3 protocols that we cover in this research. I believe that the best one suited for my
project is the SPI. The SPI shows countless advantages and outperforms the other two interfaces.
The Full-duplex, master, and multiple slaves capabilities put the SPI on top of transferring serial
data from one device to another. It allows both the master and the slave to talk to each other
without any interruptions. This is very important for my side project because I need the system to
18. 18
be as efficient and fast as possible. The speed rate that the SPI offers is more than enough for the
500kbps from the CAN-bus network. Although, the other two protocols did show some
advantages over the SPI like less wiring and multiple masters and acknowledgement bits.
Although the SPI can support CRC or error detection it doesn't inherently have the crc built into
its frame format. That leads to the I2C being better for data transferring because it has
ACK/NACK bits to show if the frame is transferred successfully.
Issues Identified
The challenge that I faced was pin mapping the Nucleo STM32 board to gain connectivity with
the CANbed board. The CANbed board has an on-board processor ATmega32U4. The board is
controlled and processed by the Arduino Leonardo. The issue is that the PIN mapping for the SPI
of the STM32 Board is not all the same. The Arduino IDE only has the pin mapping of a certain
STM32 board that is not the same one I have for this project. Also, the Arduino IDE cloud does
not allow me to edit or modify the SPI library because I would need permission from the original
owner of the SPI library to be able to edit it. Due to the issue faced I was unable to simulate the
SPI on STM32 and Arduino. Although the STM32 pin mapping did not work, I was able to code
the Arduino as a slave to receive or transfer data to a master.
Testing
Implementing SPI on STM32 Nucleo Board
Instead of using the STM32 IDE to code our Nucleo Board. We decided to write the C code in
Arduino IDE for the Nucleo board because of the robustness of the Arduino libraries. The library
includes CAN_Bus and SPI libraries that we can use for our design. Using the same software for
the two boards makes the process of establishing slave and master for the SPI communication
less complicated.
19. 19
The Arduino IDE to code the Nucleo board is simple and trouble-free because of the resources
available online. However, the issue that we faced was mapping out the STM32 Nucleo Board to
the existing board that is available for the Arduino IDE. The library manager in Arduino IDE
cloud does not allow users to modify the source code without the original owner permission. We
tried a different method of mapping the I/O of the Nucleo STM32 144 pins to work with the
Arduino IDE. However, the Arduino IDE does not support our specific board. We would need to
buy a different STM32 board that is less capable for our project to run SPI on the Nucleo Board.
Figure 13: The picture shows the connection between the CANbed board and Nucleo
successfully wired up.
Figure 14: SPI code for Slave (Arduino)
20. 20
Figure 15: SPI code for Master (Nucleo STM32)
The CANbed board and Nucleo board both come with the SPI, UART, and I^2C ports for
transferring data from one device to another. It is easier for us to wire up the SPI port because it
only requires 6 ports for transport data and synchronization.
Conclusion
Overall, the research demonstrated that the SPI protocol is ideal for most cases of data
communication. The SPI is more superior than I2C and UART because of its performance and
robustness. This paper gives me an opportunity to research and get a better understanding of
Serial Parallel Interface protocol and how to implement it into the system. Due to the time
constraint and the pin mapping, I wasn’t able to complete the overall simulation design of the
SPI for STM32. I learned that using two different boards with its own IDE interfaces is not a
good idea. It was difficult to set up the Arduino IDE to work with the STM32 Nucleo Board.
For future improvement, I want to apply this same technique to another Arduino for simplicity
and efficiency.
Work Cited
Basics of UART Communication. Circuit Basics. (2017, April 11).
https://www.circuitbasics.com/basics-uart-communication/.
Basics of the I2C Communication Protocol. Circuit Basics. (2017, April 11).
https://www.circuitbasics.com/basics-of-the-i2c-communication-protocol/.
Basics of the SPI Communication Protocol. Circuit Basics. (2018, May 23).
https://www.circuitbasics.com/basics-of-the-spi-communication-protocol/.
21. 21
McCreary, D. (2020, January 21). SPI - What it is, how it works, and what it means for you.
NetBurner. https://www.netburner.com/learn/spi-what-it-is-how-it-works/.