Phillip Stanley-Marbell
Foundations of
Embedded Systems
Physical Constraints, Sensor Uncertainty, Error Propagation,
Low-Level C on RISC-V, and Open-Source FPGA Tools
Draft Version of Michaelmas 2020
9
Sensor and Actuator I/O Interfaces
Figure 9.1: The data from
sensors is eventually
consumed by computation.
Sensors may have either
analog or digital interfaces,
but digital interfaces are
increasingly more common.
The success of embedded
systems lies partly in the
existence of standards
specifying the electrical
and data protocol access
to sensors with digital
interfaces.
Write a spec for your system. This is super-unpopular among
programmers . . . At least try writing down the state of your
system . . . For example, the state of a filesystem is a mapping from
path names to variable-length arrays of bytes.
Butler Lampson Heidelberg Laureate Forum, September 2013.
Table 9.1: Concepts.
Concept Section
Sensor examples § 9.2
I2C § 9.3
SPI § 9.4
JTAG § 9.5.1
SWD § 9.5.1
SDIO § 9.5.2
I2S § 9.5.3
I3C § 9.5.4
MIPI CSI § 9.5.5
MIPI DSI § 9.5.6
FTDI ICs § 9.5.7
Johnson-Nyquist noise § 8.3.1
“Flicker or 1/ f noise § 8.3.2
Shot noise § 8.3.3
Random telegraph noise § 8.3.4
Reset noise § 8.3.5
Sensors and actuators are the means by which computation inter-
faces with the physical world. Sensors in common use today include temper-
ature sensors, humidity sensors (hygrometers), atmospheric pressure sensors
(barometers, altimeters), magnetometers (magnetic flux density sensors, elec-
tronic compasses), accelerometers, gyroscopes, and a range of electrochemi-
cal sensors for specific compounds such as carbon monoxide or for families
of volatile organic compounds. Each of these sensor types comprises some
mechanism for transducing a physical phenomenon such as temperature, lin-
ear acceleration, or angular rotation, into a voltage or current that is then typ-
ically digitized for subsequent analysis in some signal processing algorithm.
In this course, we will focus not on the method of transduction from physical
phenomenon to voltage or current, but rather on the methods for retrieving
the transduced voltage or current from a sensor. The availability of standard
2 phillip stanley-marbell
electrical and data access protocol interfaces for retrieving data from sensors
is an important contributor to the widespread availability and use of sensors
in embedded systems.
This chapter will provide examples of sensors in use in embedded systems
and will detail the common sensor interface standards in use today.
9.1 Intended Learning Outcomes
At the end of this topic, you should be able to:
1. Enumerate the types of commonly-used sensor interfaces.
2. Enumerate the differences between the common embedded wired com-
munication interfaces.
3. Describe the operation of the I2C and SPI interfaces.
4. Read the datasheet for a sensor (or other device such as a display) with an
SPI or I2C interface and determine the sequence of digital signals needed
to obtain a given behavior from the SPI or I2C device.
5. Select and substantiate your choice for an appropriate interface to a sensor
(or other device) for a given engineering design problem.
6. Select the electrical interface parameters for a given I/O interface imple-
mentation to satisfy design criteria of speed or power efficiency.
9.1.1 Learning outcomes pre-assessment
Complete the following
quiz to evaluate your prior knowledge of the material
for this chapter.
9.1.2 Things to Look Out For
Things people sometimes have trouble digesting in this topic include:
1. The different conventions for notating unknown, undefined, or don’t-care
values in timing diagrams.
2. The role of the SDA signal in I2C.
3. The difference between I2C slave addresses for different slaves on an I2C
bus versus I2C register addresses within a given I2C slave.
4. How slave addresses are defined in I2C.
5. How register addresses within a given slave are defined when using I2C.
foundations of embedded systems 3
6. Whether I2C addresses are assigned to slaves by software or whether they
are defined by the manufacturer of the I2C device
1
.
1
They are assigned by
manufacturers, with some
design-time configurability
as we will see in the topic.
This leads to the possibility
of address conflicts. We
will discuss how to resolve
these conflicts.
7. How an I2C slave at a given address knows what its address is.
8. The maximum speed achievable with SPI.
9. Whether the MISO signal in SPI could be used by the slave to acknowledge
transactions.
10. Whether I2C is preferred over SPI in practice, or vice versa.
11. What an I2C ACK and NAK look like in a timing diagram.
12. Why the SPI chip select often has a / written before it (e.g, /CS).
13. Whether data in SPI is sent first, before sending the register address the
data are relevant to.
14. What open drain means and how open-drain drivers are implemented at
the circuit level.
15. Whether it is possible to daisy-chain SPI
2
.
2
In some specific im-
plementations, yes. One
example is the ISL23415 IC
used in the Warp research
platform.
16. How voltages that are transduced from an underlying phenomenon in
a sensor are converted into the bits and bytes that are transmitted over
interfaces such as I2C and SPI.
9.1.3 The muddiest point
As you go through the material in this chapter, think about the following two
questions and note your responses for yourself or using the annotation tools
of the online version of the chapter. You will have the opportunity to submit
your responses to these questions at the end of the chapter:
1. What is least clear to you in this chapter? (You can simply list the section
numbers or write a few words.)
2. What is most clear to you in this chapter? (You can simply list the section
numbers or write a few words.)
9.2 Sensor Interfaces
Figure
9.2 shows examples of the kinds of miniaturized sensors we will study
in this chapter, along with examples of the miniaturized microcontrollers
and FPGAs that we will use to interface to them in this course. Chapter
8.2
introduced examples of sensors for several physical phenomena, focusing on
the precision, accuracy, and noise in their measurement interfaces. In this
4 phillip stanley-marbell
Figure 9.2: Examples of
state-of-the-art (circa
2018) miniaturized mi-
crocontrollers, FPGAs,
and sensors. Today, most
sensors have digital in-
terfaces. Internally, they
typically comprise a sensor
(such as a MEMS, piezore-
sistive, or functionalized
electrochemical sensor) and
an additional die imple-
menting analog to digital
conversion, digital I/O
interfaces, and in some
cases even implementing
complete programmable
processor cores such as an
ARM Cortex-M0. All the
devices shown in the figure
are between 2 × 2 mm and
5 × 5 mm.
chapter, we will focus on the interfaces for obtaining values from sensors,
rather than on the properties of the measurements themselves.
Sensors may have either analog or digital interfaces. When sensors have
analog interfaces, they will typically have one or more interface pins whose
voltages will vary in proportion to the phenomenon being sensed. Histori-
cally, most sensors had analog interfaces and would be interfaced with via
either a dedicated analog to digital converter (ADC), or using a built-in ADC
within a microcontroller or DSP. Even today, however, there are still high-
precision sensors which provide analog interfaces, such as the Analog De-
vices ADXL356.
3
Figure
9.3 shows the block diagram of the ADXL356. The
3
Analog Devices 2017.
ADXL356 has three output voltages, X
OUT
, Y
OUT
, and Z
OUT
, which take on
voltages in proportion to the x-, y-, and z-axis acceleration being sensed.
Figure 9.3: The Analog
Devices ADXL356, a
state-of-the-art 3-axis
accelerometer with an
analog (voltage) output.
Diagram source: Analog
Devices.
Unlike the ADXL356, most low-cost sensors used in embedded systems
today have digital interfaces. These digital interfaces define the method by
which a microcontroller, DSP, or other system component can retrieve digi-
tized samples from the sensor. The interfaces define how many pins on the
sensor are used for digital communications. They define how to initiate and
terminate digital communication with a sensor, such as what sequence of
digital logic levels should be driven on a given I/O pin to initiate acquisition
of a sensor measurement. And they define how the sensor will indicate com-
pletion of the measurement, and so on. Figure
9.4 shows the block diagram
of the ADXL362,
4
a 3-axis accelerometer similar to the ADXL356 but with an
4
Analog Devices
2017.
SPI digital interface rather than analog output.
foundations of embedded systems 5
Figure 9.4: The Analog
Devices ADXL362, a
state-of-the-art 3-axis
accelerometer with a digital
serial peripheral interface
(SPI) standard output and
configuration interface.
Diagram source: Analog
Devices.
Because of the desire to continually miniaturize sensors, digital interfaces
are most often serial (i.e., 1-bit-at-a-time) interfaces, allowing sensors to be im-
plemented in integrated circuit packages with fewer pins and hence smaller
size. Unlike analog interfaces where the means of sampling the analog volt-
ages at the output of a sensor is conceptually straightforward, even a simple
1-bit digital interface could have a large range of possible methods for re-
trieving samples. The different access methods could include different speci-
fications for the boundaries of samples in the stream of bits coming out of a
sensor, different conventions for the duration of bits, whether or not there is
a separate clock interface for indicating the boundaries of bits, and so on. As
a result of this range of possibilities, sensor manufacturers and sensor system
designers have developed a number of serial interface standards which are now
in common use.
The common serial sensor interface standards in use today include I2C,
SPI, and OneWire for sensors such as accelerometers and gyros, I2S for sen-
sors such as microphones which require higher data throughput, MIPI CSI
for image sensors (cameras), CAN for automotive and industrial automation
applications, and the new I3C interface which is intended to be the next-
generation replacement for the popular I2C interface.
9.3 I2C
The inter-integrated-circuit (I2C or IIC or I
2
C)
5
serial bus is the most common
5
NXP Semiconductors
2014.
digital interface to sensors. I2C is a communication bus because multiple
communicating devices are connected to the electrical interface and any one
of these could be the target or destination for communications.
9.3.1 I2C electrical interface
The electrical interface of I2C consists of two signals
6
: the SDA (serial data)
6
Because signals are
ultimately carried over
wires or printed circuit
board traces, we will
sometimes also refer to
signals in this chapter as
“wires” or “lines”.
6 phillip stanley-marbell
and the SCL (serial clock) lines. Each component such as a microcontroller,
DSP, or sensor that is connected to an I2C bus connects to both the SDA and
SCL lines.
The electrical signaling on I2C is what is called open collector or open drain
(as opposed to push-pull): each participant connected to the SDA and SCL
lines by default leaves the connected pin floating, i.e., neither driving a logic 0
nor driving a logic 1. The SDA and SCL lines in I2C are therefore pulled up to
the I/O supply voltage
7
with pull-up resistors, preventing the SDA and SCL
7
We will also refer to
supply voltages in this
chapter and elsewhere in
this course as “rails”.
from floating by default. These pull-up resistors are typically of resistance
values between 4.7 k and 10 k. The smaller the pull-up resistance value,
the more quickly signal values on the SDA and SCL lines can be toggled
(and hence the higher the data rates possible), but the more power will be
dissipated whenever a logic 0 is driven on the lines
8
. Typical maximum clock
8
Test your understanding:
Can you see why this is the
case?
speeds for the clock signal (i.e., SCL) in I2C is 400 kHz. Figure
9.5 shows an
example I2C bus comprising a microcontroller (processor) and two sensors.
Pull-up Resistors
SDA
SCL
System
Power
Supply
10kΩ 10kΩ
I/O
Power
Supply
Processor
I2C
Interface
SensorSensor
Figure 9.5: An example I2C
serial bus configuration.
The SDA and SCL lines
are pulled-up to the I/O
supply voltage by pull-
up resistors which are
typically set to be between
4.7 k and 10 k. The
smaller the pull-up, the
faster it is possible to drive
signals on the lines, but
the greater the power
dissipation incurred in
communications.
9.3.2 I2C transactions
Communications on the I2C bus consist of transactions. A transaction on the
I2C bus is initiated by the bus master and is targeted at one other member of
the bus referred to as the slave.
I2C Slave Addresses: To distinguish between the multiple slaves on an
I2C bus, each slave on the bus is allocated a pre-defined 7-bit slave address by
its manufacturer. Because of the limited range of possible slave addresses,
there is a real possibility of having two different I2C slaves (e.g., two differ-
ent sensors) in a particular I2C bus with the same address. To address this
problem, most I2C slaves can be configured to take on one or more alternative
addresses from their default. Configuring an I2C device with an alternative
slave address is typically achieved by tying one or more specially-designated
configuration pins to either ground or the I/O voltage, as prescribed by the
manufacturer in the device’s datasheet.
foundations of embedded systems 7
Transactions on the I2C bus are defined in terms of the SDA and SCL
lines and their values relative to each other, with the device initiating the
communication (the master) driving the SCL line with the I2C clock (at some
speed typically less than 400 kHz as mentioned above).
To initiate an I2C bus transaction, the master first drives an address on the
bus and the addressed slave responds by driving an acknowledge transaction
(ACK) on the bus. To drive the slave address, the master drives SDA low
during the half cycle when SCL is high. This is referred to as the start bit. The
master then begins to drive a sequence of bits on the SDA line, setting the bit
on SDA in the half cycle while SCL is low, driving SCL high at which point
the value on SDA is read by the slave, then driving SCL low again in order
to set the next bit on SDA.
A slave generates an ACK during this sequence by driving a 0 on the SDA
line during the half cycle when the SCL is low and generates a NAK during
this sequence by driving a 1 (i.e., doing nothing) on the SDA line. To indicate
the completion of a sequence of bits, the master drives SDA from low to high
while SCL is high. This is referred to as the stop bit. Throughout these steps,
the clock (SCL) is driven by the master. We will refer to the data transmitted
between the last address bit and the stop bit as the payload.
9.3.3 I2C addresses in transactions
Each slave on the bus is allocated a pre-defined 7-bit address, but the ad-
dresses transmitted in I2C transactions are 8-bit. The I2C bus master ap-
pends an additional bit to each 7-bit address: A 1 when the transaction is
a read transaction, and 0 when the transaction is a write transaction. The
master and slave use this additional bit to distinguish between whether the
transaction is a read transaction or a write transaction.
For example, the Freescale MMA8451Q accelerometer
9
is an I2C slave with
9
Freescale Semiconductor
2014.
a 7-bit slave address of 0x1C. The MMA8451Q has a pin, SA0 which you
can use to change the default slave address the MMA8451Q will respond to:
When the SA0 pin is tied to ground, the MMA8451Q responds to I2C slave
address 0x1C; when SA0 is tied to VDD, the MMA8451Q responds to slave
address 0x1D. Figure
9.6 shows the block diagram of the MMA8451Q with SA0
tied to VDD. During a read transaction, the 8-bit address transmitted by the
master is 0x3B and during a write transaction, the 8-bit address transmitted
by the master is 0x3A.
In addition to the slave address which is the first part of an I2C transaction,
the payload of an I2C transaction typically also contains an additional register
address. Unlike the slave address which distinguish between different devices
connected to an I2C bus, register addresses are addresses of internal registers
in the I2C slave device and represent pieces of internal functionality in the I2C
8 phillip stanley-marbell
Figure 9.6: Block dia-
gram of the Freescale
MMA8451Q accelerome-
ter. Source: MMA8451Q
datasheet.
slave device. The MMA8451Q in the example above has 50 internal register
addresses corresponding to various pieces of information and functionality
such as the device’s unique identifier, a register holding the lower 8 bits of
the most recent x-axis acceleration measurement, and so on.
Figure 9.7: A subset of the
on-sensor register defi-
nitions for the Freescale
MMA8451Q accelerome-
ter. Source: MMA8451Q
datasheet.
Figure
9.7 shows the first 16 registers as defined in the datasheet for the
MMA8451Q. The figure shows, for example, that the internal register at ad-
dress 0x05 is the register to read to obtain the most-significant byte of z-axis
acceleration. Similarly, the register 0x0D will always return the value 0x1A,
the Device ID. Table
9.2 shows an example of a sequence of reads from the
MMA8451Q.
foundations of embedded systems 9
Time (s) Sequence Number I2C Address Value R/W ACK/NAK
0.000167 0 0x3A 6 Write ACK
0.0002915 1 0x3B 152 Read NAK
0.00042 2 0x3A 5 Write ACK
0.000544 3 0x3B 55 Read NAK
0.000673 4 0x3A 6 Write ACK
0.000797 5 0x3B 152 Read NAK
0.000926 6 0x3A 5 Write ACK
0.00105 7 0x3B 65 Read NAK
0.0011785 8 0x3A 6 Write ACK
Table 9.2: A sequence of
I2C transactions for the
MMA8451Q accelerometer.
9.3.4 Exercise
1. Given the timestamps shown in Table
9.2, at approximately what fre-
quency is the SCL clock running?
2. Can you figure out what the data format is for the acceleration values read
from the OUT
_
Z
_
LSB and OUT
_
Z
_
MSB registers (least-significant byte and
most-significant byte of the current z-axis acceleration reading)? Where
would you look to figure this out?
3. The transaction trace in Table
9.2 shows repeated read transactions to ob-
tain a sequence of samples. Do you think there is a more efficient way to
obtain a sequence of samples such as that shown in Table 9.2?
9.3.5 Example: Reading the I2C register on the MMA8451Q using the
KSDK library helper routines on the FRDMKL03 and FRDMKL05
boards
The following shows a basic C program to read from the WHO
_
AM
_
I on-sensor
register of the MMA8451Q using I2C, using the Freescale KSDK tools (Ap-
pendix
C).
1 #include <string.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include "board.h"
5 #include "fsl
_
i2c
_
master
_
driver.h"
6
7 int
8 main(void)
9 {
10 uint8
_
t commandBuffer[1] = {0xFF};
11 uint8
_
t payloadBuffer[1] = {0xFF};
12 uint8
_
t receiveBuff[1] = {0xFF};
13
14 i2c
_
master
_
state
_
t master;
15 i2c
_
status
_
t returnValue;
16
10 phillip stanley-marbell
17 i2c
_
device
_
t slave =
18 {
19 .address = 0x1D,
20 .baudRate
_
kbps = 100
21 };
22
23 hardware
_
init();
24 dbg
_
uart
_
init();
25
26 configure
_
i2c
_
pins(BOARD
_
I2C
_
COMM
_
INSTANCE);
27
28 OSA
_
Init();
29 GPIO
_
DRV
_
Init(0, ledPins);
30 I2C
_
DRV
_
MasterInit(BOARD
_
I2C
_
COMM
_
INSTANCE, &master);
31
32 commandBuffer[0] = 0x0D;
33 payloadBuffer[0] = 0x00;
34
35 returnValue = I2C
_
DRV
_
MasterSendDataBlocking(
36 BOARD
_
I2C
_
COMM
_
INSTANCE,
37 &slave,
38 commandBuffer,
39 1,
40 payloadBuffer,
41 sizeof(payloadBuffer),
42 500);
43 if (returnValue != kStatus
_
I2C
_
Success)
44 {
45 printf("\r\nI2C communication failed, error code: %d", returnValue);
46 }
47 }
9.4 SPI
The serial peripheral interface (SPI)
10
is another popular serial interface used
10
Serial Peripheral Interface
(SPI)
.
in embedded systems. The basic SPI interface consists of four signals: the
master out slave in (MOSI), on which the master sends data to the slave; the
master in slave out (MISO), on which the slave sends data to the master; the
slave/serial clock (SCK), which the master drives to send a clock to the slave;
the chip select (/CS) (typically active low), which the master drives low to
activate the slave. Unlike the I2C serial bus described in Section
9.3, SPI is
essentially a point-to-point link although a single master can drive multiple
slaves by having a dedicated chip select for each slave.
All communication in SPI is bi-directional. Whenever the master is driving
the clock (SCK), the connected slave with /CS driven low
11
shifts in bits from
11
There should be only one
slave with /CS asserted.
the MOSI and at the same time shifts out bits on the MISO, even if it has no
real data to send. Figure
9.8 shows a typical SPI configuration.
foundations of embedded systems 11
MISO
SCK
System
Power
Supply
MOSI
Processor
SPI
Interface
(SPI Master)
Sensor
I/O Power
Supply
(SPI Slave)
/CS
Figure 9.8: An example
SPI configuration. All
communication in SPI is
bi-directional: Whenever
the master is driving the
clock, the slave shifts in
bits from the MOSI and
shifts out bits on the MISO,
even if it has no real data to
send.
9.4.1 SPI electrical interface
Unlike I2C in which the SDA and SCL drivers are open-drain, SPI interface
drivers are push-pull, actively driving the MISO, MOSI, SCK, and CS to either
a 0 or a 1. There is therefore no mandatory need for pull-ups to the I/O
supply rail. SPI interfaces can reach interface speeds of up to 10 Mb/s.
9.4.2 SPI transactions
Because SPI is a point-to-point interface, SPI slaves do not have addresses like
I2C slaves do. In some sense, the dedicated /CS acts as a sort of addressing
signal. An SPI transaction starts with the master driving the /CS from high
to low, followed by the master shifting out the sequence of bits corresponding
to the payload on the MOSI. Any data which the slave device shifts out on
the MISO is shifted into a receive FIFO on the master which can then be read
by software. In contrast to I2C, there is no special transaction like the ACK or
NAK at the end of an SPI transaction, so the only way to determine whether
an SPI transaction succeeded or failed is based on the data received on the
MISO.
Figure 9.9: A subset of
the on-sensor register
definitions for the Ana-
log Devices ADXL362
accelerometer. Source:
ADXL362 datasheet.
12 phillip stanley-marbell
Figure
9.9 shows the first 18 registers as defined in the datasheet for the
ADXL362, a 3-axis accelerometer with an SPI interface. The figure shows, for
example, that the internal register at address 0x13 is the register to read to
obtain the most-significant byte of z-axis acceleration. Similarly, the register
0x00 contains a fixed identifier for the ADXL362 SPI slave and will always
return the value 0xAD.
9.4.3 Example: Driving an SPI peripheral from the FRDMKL03 evaluation
board
The following shows a C program to read register 0x0 from the device con-
nected to the SPI interface on the KL03 / KL05 microcontroller, using the
Freescale KSDK tools (Appendix
C).
1 #include <string.h>
2 #include <stdio.h>
3 #include "board.h"
4 #include "fsl
_
spi
_
master
_
driver.h"
5 #include "fsl
_
clock
_
manager.h"
6
7 volatile spi
_
master
_
state
_
t spiMasterState;
8 volatile spi
_
master
_
user
_
config
_
t spiUserConfig;
9 volatile WarpSPIDeviceState deviceADXL362State;
10
11
12 typedef enum
13 {
14 kWarpStatusOK = 0,
15
16 /
*
17
*
... Several items trimmed for this example ...
18
*
/
19 kWarpStatusMax
20 } WarpStatus;
21
22 typedef struct
23 {
24 /
*
25
*
For holding ksdk-based error codes
26
*
/
27 spi
_
status
_
t ksdk
_
spi
_
status;
28
29 /
*
30
*
... Several items trimmed for this example ...
31
*
/
32 uint8
_
t spiSourceBuffer[3];
33 uint8
_
t spiSinkBuffer[3];
34 WarpStatus deviceStatus;
35 } WarpSPIDeviceState;
36
37
38 void
39 enableSPIpins(void)
40 {
foundations of embedded systems 13
41 CLOCK
_
SYS
_
EnableSpiClock(0);
42
43 /
*
KL03
_
SPI
_
MISO --> PTA6 (ALT3)
*
/
44 PORT
_
HAL
_
SetMuxMode(PORTA
_
BASE, 6, kPortMuxAlt3);
45
46 /
*
KL03
_
SPI
_
MOSI --> PTA8 (ALT3)
*
/
47 PORT
_
HAL
_
SetMuxMode(PORTA
_
BASE, 8, kPortMuxAlt3);
48
49 /
*
KL03
_
SPI
_
SCK --> PTA9 (ALT3)
*
/
50 PORT
_
HAL
_
SetMuxMode(PORTA
_
BASE, 9, kPortMuxAlt3);
51
52 /
*
53
*
Initialize SPI master. See KSDK13APIRM.pdf Section 70.4
54
*
/
55 uint32
_
t calculatedBaudRate;
56 spiUserConfig.polarity = kSpiClockPolarity
_
ActiveHigh;
57 spiUserConfig.phase = kSpiClockPhase
_
FirstEdge;
58 spiUserConfig.direction = kSpiMsbFirst;
59 spiUserConfig.bitsPerSec = 50000;
60 SPI
_
DRV
_
MasterInit(0 /
*
SPI master instance
*
/, (spi
_
master
_
state
_
t
*
)&spiMasterState);
61 SPI
_
DRV
_
MasterConfigureBus(0 /
*
SPI master instance
*
/, (spi
_
master
_
user
_
config
_
t
*
)&spiUserConfig, &
calculatedBaudRate);
62 printf("Calculated baud rate is %ld\n", calculatedBaudRate);
63 }
64
65 WarpStatus
66 writeSensorRegisterADXL362(uint8
_
t command, uint8
_
t deviceRegister, uint8
_
t writeValue)
67 {
68 /
*
69
*
Populate the shift-out register with the read-register command,
70
*
followed by the register to be read, followed by a zero byte.
71
*
/
72 deviceADXL362State.spiSourceBuffer[0] = command;
73 deviceADXL362State.spiSourceBuffer[1] = deviceRegister;
74 deviceADXL362State.spiSourceBuffer[2] = writeValue;
75
76 deviceADXL362State.spiSinkBuffer[0] = 0x00;
77 deviceADXL362State.spiSinkBuffer[1] = 0x00;
78 deviceADXL362State.spiSinkBuffer[2] = 0x00;
79
80 /
*
81
*
First, create a falling edge
on chip-select.
82
*
83
*
/
84 GPIO
_
DRV
_
SetPinOutput(kWarpPinADXL362
_
CS);
85 OSA
_
TimeDelay(50);
86 GPIO
_
DRV
_
ClearPinOutput(kWarpPinADXL362
_
CS);
87
88 /
*
89
*
The result of the SPI transaction will be stored in deviceADXL362State.spiSinkBuffer.
90
*
91
*
Providing a device structure here is optional since it
92
*
is already provided when we did SPI
_
DRV
_
MasterConfigureBus(),
93
*
so we pass in NULL.
94
*
95
*
The
"master instance" is always 0 for the KL03 since
96
*
there is only one SPI peripheral.
97
*
/
14 phillip stanley-marbell
98 deviceADXL362State.ksdk
_
spi
_
status = SPI
_
DRV
_
MasterTransferBlocking(0 /
*
master instance
*
/,
99 NULL /
*
spi
_
master
_
user
_
config
_
t
*
/,
100 (const uint8
_
t
*
restrict)deviceADXL362State.spiSourceBuffer,
101 (uint8
_
t
*
restrict)deviceADXL362State.spiSinkBuffer,
102 3 /
*
transfer size
*
/,
103 1000 /
*
timeout in microseconds (unlike I2C which is ms)
*
/);
104
105 /
*
106
*
Disengage the ADXL362
107
*
/
108 GPIO
_
DRV
_
SetPinOutput(kWarpPinADXL362
_
CS);
109
110 return kWarpStatusOK;
111 }
112
113 WarpStatus
114 readSensorRegisterADXL362(uint8
_
t deviceRegister)
115 {
116 /
*
117
*
Analog Devices ADXL362.
118
*
119
*
From device manual, Rev. B, Page 19 of 44:
120
*
121
*
"
122
*
The SPI port uses a multibyte structure
123
*
wherein the first byte is a command. The
124
*
ADXL362 command set is:
125
*
126
*
- 0x0A: write register
127
*
- 0x0B: read register
128
*
- 0x0D: read FIFO
129
*
"
130
*
/
131 return writeSensorRegisterADXL362(0x0B /
*
command == read register
*
/, deviceRegister, 0x00 /
*
writeValue
*
/);
132 }
133
134 int
135 main(void)
136 {
137 hardware
_
init();
138 OSA
_
Init();
139 enableSPIpins();
140
141 /
*
142
*
Read register 0x0 of the device sitting
on the SPI interface.
143
*
/
144 readSensorRegisterADXL362(0x0 /
*
deviceRegister
*
/);
145 }
9.5 Other I/O Interfaces
In addition to SPI and I2C, there are several other I/O interfaces commonly
used in embedded systems. These include JTAG and SWD which are used for
debug interfaces to integrated circuits, SDIO, which is used for the interface
foundations of embedded systems 15
to SD cards, I2S and its variants which are used for the interfaces to audio
devices, and MIPI CSI and MIPI DSI which are used to interface to cameras
and displays, respectively.
9.5.1 JTAG and SWD
JTAG
12
(IEEE 1149.1) and SWD
13
(serial-wire debug) are two common serial
12
IEEE Std. 1149.1-1990.
13
Serial Wire Debug (SWD).
communication interfaces used primarily for debugging. The JTAG standard
(link) describes JTAG in detail, while documentation from ARM (link) de-
scribes SWD in detail.
9.5.2 SDIO
The SDIO interface is logically very similar to SPI and SPI interfaces can be
co-opted to drive SDIO.
9.5.3 I2S
The inter-integrated-circuit sound (I2S) bus interface is used primarily for
high-speed data interfaces, such as interfacing to analog to digital converters
and to audio CODECs.
Documentation from TI (link) describes I2S in detail.
9.5.4 I3C
I3C is intended to be the next-generation replacement for I2C, with higher
speeds, backwards compatibility with I2C, and with the addition of new fea-
tures such as in-band interrupts. A document (link) from the MIPI Alliance
provides an overview of I3C.
9.5.5 MIPI CSI
MIPI CSI is a serial interface intended for use for interfacing microprocessors
with image sensors (cameras). You are unlikely to find a MIPI CSI interface
on a microcontroller, but likely to find one on a mobile applications processor
used in a device such as a mobile phone.
9.5.6 MIPI DSI
MIPI DSI is a serial interface intended for use for interfacing microprocessors
with high-resolution displays. You are unlikely to find a MIPI DSI interface
on a microcontroller, but likely to find one on a mobile applications processor
used in a device such as a mobile phone.
9.5.7 The FTDI USB to Serial Interface ICs
The company FTDI makes a line of very popular USB to serial interface in-
tegrated circuits. These ICs provide an easy way to add USB support to a
16 phillip stanley-marbell
microcontroller that only has a UART, I2C, or SPI interface. They also pro-
vide a convenient way to communicate with an SPI, I2C, SWD, SDIO, or
JTAG interface from a PC, by using a breakout board that can be plugged into
a host PC’s USB interface with the corresponding serial interface wired up to
an embedded system.
9.6 Exercises
Complete the following
exercise to evaluate your understanding of the mate-
rial in this chapter.
9.7 Summary
Data from sensors is eventually consumed by computation. Sensors may
have either analog or digital interfaces, but digital interfaces are increasingly
more common. The success of embedded systems lies partly in the existence
of standards for interchange of data from sensors with digital interfaces. Ex-
amples of these interfaces include I2C, SPI, I2S, and I3C.
9.8 Optional Reading
The book The Art of Electronics
14
provides more details on other serial inter-
14
Horowitz and Hill
2015.
faces not covered here, include the OneWire interface, which uses just one
wire for power and data.
9.9 Relevant Books Available in the Engineering Library
1. The Circuit Designers Companion, 3rd Edition, ISBN: 978-0080971384.
2. Embedded Systems Dictionary, ISBN: 978-1578201204.
3. The Art of Designing Embedded Systems, S econd Edition, ISBN: 978-0750686440.
4. The Art of Electronics, ISBN: 978-0521809269.
foundations of embedded systems 17
9.10 The Muddiest Point
Think about the following two questions and submit your responses through
this
link.
1. What was least clear to you in this chapter? (You can simply list the section
numbers or write a few words.)
2. What was most clear to you in this chapter? (You can simply list the
section numbers or write a few words.)
18 phillip stanley-marbell
9.11 Coursework Item #1 Survey
Think about the following two questions and submit your responses through
this
link.
1. How much time did you spend on Coursework #1?
2. What was least clear to you in Coursework #1?
3. What was most clear to you in Coursework #1?
Bibliography
1. Analog Devices (2017). ADXL356 Low Noise, Low Drift, Low Power, 3-Axis
MEMS Accelerometer with Analog Output, Data Sheet (cit. on p.
4).
2. ARM Holdings. Serial Wire Debug (SWD) (cit. on p. 15).
3. Freescale Semiconductor (2014). MMA8451Q 3-Axis, 14-bit/8-bit Digital Ac-
celerometer, Data Sheet (cit. on p.
7).
4. Horowitz, Paul and Winfield Hill (2015). The art of electronics. Cambridge
Univ. Press (cit. on p.
16).
5. Joint Test Action Group. IEEE Std. 1149.1-1990 (cit. on p. 15).
6. Motorola. Serial Peripheral Interface (SPI) (cit. on p. 10).
7. NXP Semiconductors (2014). UM10204, I2C-bus specification and user man-
ual (cit. on p.
5).
Index
IEEE Std. 1149.1-1990,
15
Serial Peripheral Interface (SPI), 10
Serial Wire Debug (SWD), 15
UM10204, I2C-bus specification and
user manual,
5
ADXL356 Low Noise, Low Drift, Low
Power, 3-Axis MEMS Accelerom-
eter with Analog Output, Data
Sheet,
4
MMA8451Q 3-Axis, 14-bit/8-bit Digi-
tal Accelerometer, Data Sheet,
7
ADC, 4
Analog Devices, 4
ARM Holdings, 15
bus, 5
CAN, 5
Freescale Semiconductor, 7
Hill, Winfield, 16
Horowitz, Paul, 16
I
2
C, 5
I2C, 5
I2C, alternative slave address, 6
I2C, bus master, 6
I2C, bus slave, 6
I2C, payload, 7
I2C, register address, 2, 7
I2C, SCL, 6
I2C, SDA, 5
I2C, slave address, 2, 6
I2C, start bit, 7
I2C, stop bit, 7
I2C, transactions, 6
I2C, ACK, 7
I2C, NAK, 7
I2S, 5, 15
I3C, 5
IIC, 5
inter-integrated-circuit, 5
Joint Test Action Group, 15
MIPI CSI, 5
Motorola, 10
NXP Semiconductors, 5
OneWire, 5
open collector, 6
open drain, 3, 6
open-drain, 11
pull-up, 6
push-pull, 11
sensor, accelerometer, 1
sensor, altimeter, 1
sensor, barometer, 1
sensor, CO, 1
sensor, electrochemical, 1
sensor, gyroscope, 1
sensor, humidity, 1
sensor, interface, analog, 4
sensor, interface, digital, 4
sensor, interface, serial, 5
sensor, magnetometer, 1
sensor, pressure, 1
sensor, temperature, 1
sensor, transduction, 1
sensor, VOC, 1
serial interface standards, 5
serial peripheral interface, 10
SPI, 5, 10
SPI, CS, 10
SPI, MISO, 10
SPI, MOSI, 10
SPI, SCK, 10
The art of electronics, 16