EECS 373 Lab 6: Serial Bus Interfacing
Copyright © 2013
Matt
Smith,
1/24/16
Schedule
See posted lab schedule.
Overview
A common way to connect microprocessor system
components with the processor is with a serial bus. Typical examples of
serial buses
are the Universal Asynchronous Receiver/Transmitter (UART), Serial
Peripheral Interface (SPI) and Inter-Integrated Circuit (I2C).
For this lab we will begin by observing example serial
transmissions of each. We will then interface to a few representative
components specifically a SPI digital to analog converter (DAC) and I2C
combination analog to digital converter (DAC) and DAC. We will not
interface to a UART
device. While these are common and you will likely use one for your
project, they are relatively easy to interface to.
Pre-Lab Assignment
There is no Pre-Lab Assignment. You should review
the Serial Bus Lecture slides and consider reading the basic overviews
from such sources as Wikipedia for UART, SPI and I2C.
In-Lab Assignment
In-Lab Part 1: Observing a UART Transmission
1.1) The Serial
Hardware Components
You will use the UART, SPI and I2C devices integrated into the
SmartFusion Microprocessor Subsystem (MSS). There are 2 each in the
subsystem. You have already used UART0 to provide the standard output
for printf. UART1, SPI1 and I2C1 are conveniently ported to pins on the
latest SmartFusion evaluation kit. See below on right side of kit: JP8
UART Header, JP9 I2C Header and J23 SPI Header.
1.2) Configure
MSS Components and Program FPGA
Select UART_1, SPI_1 and I2C_1 with the MSS configurator. It is
not necessary to have the other components
selected.
1.3) SoftConsole
Project
If you open SoftConsole with Libero, a project will be created with the
UART, SPI and I2C drivers provided in the directory. Be sure that SoftConsole v3.3 is
specified in the tool flow as we did in lab 5 before starting your
Libero project.
1.4) Simple
UART Program Excercise
Write a simple UART program that will send the message "Go Blue".
Everything you need is provided in the drivers directory. The header
descriptions in the include files describe the function of the drivers.
Explicit examples are provided. The drivers are in the "project name"_MSS_MSS_CM3_0_hw_platform project directory.
You will not be able to observe UART1 activity with a terminal emulator
such as FUN terminal. Only UART0 is ported to the workstation (via USB
to serial connection). Also, printf is associated with UART0 as the
standard output so it will not work. You will have to use the serial
output functions provided with the device drivers to output on the
UART1 port and the Saleae to observe the transmission.
Set the UART up for the follwoing:
- 57600 baud
- 8 bit data
- no parity
- one stop bit
The following code snipet is a starting point.
#include <stdio.h>
#include <inttypes.h>
#include "drivers/mss_uart/mss_uart.h"
int main()
{
{
uint8_t tx_buff[10] = "Go Blue";
/* initialize the UART */
/* transmit the message Go Blue */
}
}
1.5) Observe UART
Transmission with Saleae
Connect the Saleae (USB based Logic Analyzer from lab 1) to the JP8
header and observe the transmission.
Enable the serial protocol analyzer, configure it to read the "Go Blue"
message. Save a screen shot of the
serial protocol analyzer showing the "Go Blue" in text display. Submit
it with your post lab.
To observe the transmission with the Saleae, add an "Async Serial"
analyzer to the wire connected to the tx channel. Make sure to
configure it based on the values in the provided code. You can trigger
the analzyer on the data line going low.
1.6)
References
SmartFusion
MicroController Subsystem Users Guide (UART Section)
Saleae
Logic User Guide
The mss_uart.h. file in
the device driver directory.
1.7) UART Screen Shot
Screen Shot 1) Submit a screen shot of the UART transaction using the
asynchronous serial protocol analyzer showing the "Go Blue"
message.
In-Lab Part 2: Observing a SPI Transmission
2.1) Hardware
The hardware components and firmware (drivers) for the SPI
devices were provided in the UART part 1 section.
2.2) Example
SPI Program
The following C code sends the code to the SPI serial port J23 SPI Header.
Compile the code and observe it with the serial protocol analyzer in
the Saleae. You should be able to see the information sent in the
transmit frame as a HEX value. This code is taken
directly from mss_spi.h. Look
in this file to get a full description of the functions.
#include <stdio.h>
#include <inttypes.h>
#include "drivers/mss_spi/mss_spi.h"
int main()
{
const uint8_t frame_size = 32;
const uint32_t master_tx_frame = 0x0100A0E1;
MSS_SPI_init( &g_mss_spi1 );
MSS_SPI_configure_master_mode
(
&g_mss_spi1,
MSS_SPI_SLAVE_0,
MSS_SPI_MODE1,
MSS_SPI_PCLK_DIV_256,
frame_size
);
MSS_SPI_set_slave_select( &g_mss_spi1, MSS_SPI_SLAVE_0 );
MSS_SPI_transfer_frame( &g_mss_spi1, master_tx_frame );
MSS_SPI_clear_slave_select( &g_mss_spi1, MSS_SPI_SLAVE_0 );
return(0);
}
}
2.3) Observe SPI
Transmission with Saleae
Connect the Saleae to the JP8 header and observe the transmission.
Enable the serial protocol analyzer, configure it to read the code
message 0x01000A0E1 in HEX. Save a
screen shot of the serial protocol analyzer with the transaction
displayed in HEX. Submit it with your post lab.
To observe, set a breakpoint on return(0) and set the logic
analyzer to trigger on the select line going low.
2.4) References
SmartFusion
MicroController Subsystem Users Guide (SPI Section)
Saleae
Logic User Guide
The mss_spi.h. file in
the device driver directory.
2.5) SPI Screen Shot
Screen Shot 2) Submit a screen shot of the SPI transaction using the
Saleae SPI
serial protocol analyzer showing the message displayed in HEX format.
In-Lab Part 3: Observing an I2C Transmission
3.1) Hardware
The hardware components and firmware (drivers) for the I2C
devices were provided in the UART part 1 section.
3.2) Example
I2C Program
The following C code sends the code to the I2C serial port JP9 I2C Header.
Compile the code and observe it with the serial protocol analyzer in
the Saleae. Look in the mss_i2c.h file for an explanation of the
functions.
#include <stdio.h>
#include <inttypes.h>
#include "drivers/mss_i2c/mss_i2c.h"
/*the target address is a 7 bit value! */
#define TARGET_ADDRESS 0b1111000
int main()
{
uint8_t transmit_buf[] = { 0x01, 0x02 };
uint8_t receive_buf[10];
MSS_I2C_init(&g_mss_i2c1 , TARGET_ADDRESS, MSS_I2C_PCLK_DIV_960 );
MSS_I2C_write
(
&g_mss_i2c1,
TARGET_ADDRESS,
transmit_buf,
sizeof(transmit_buf),
MSS_I2C_RELEASE_BUS
);
MSS_I2C_wait_complete(&g_mss_i2c1, MSS_I2C_NO_TIMEOUT);
return(0);
/* the following code is provided as an example I2C read */
/*
MSS_I2C_read
(
&g_mss_i2c1,
TARGET_ADDRESS,
receive_buf,
sizeof(receive_buf),
MSS_I2C_RELEASE_BUS
);
MSS_I2C_wait_complete(&g_mss_i2c1, MSS_I2C_NO_TIMEOUT); */
}
3.3) Observe I2C
Transmission with Saleae
Connect the Saleae to the JP8 header and observe the transmission.
Enable the serial protocol analyzer, set it up to read the transactions
in HEX. Save a screen shot of the
serial protocol analyzer for your post lab with the transaction
displayed in HEX format. Notice that the transaction is not
complete. Why ? Be prepared to answer this question for your post lab.
3.4)
References
SmartFusion
MicroController Subsystem Users Guide (I2C Section)
Saleae
Logic User Guide
The mss_i2c.h. file in
the device driver directory.
3.5) I2C Screen Shot
Screen Shot 3) Submit a screen shot of the I2C transaction. The view
should be using the Saleae I2C protocol
analyzer.
3.6) Questions
Question 1) Why is the I2C transmission incomplete? That is, why can't you
observe the transmission of 0x1 and 0x2?
Post-Lab Assignment
For the post lab assignment you will interface two external
devices to the SmartFusion kits: the SPI, DAC, Microchip MCP4901
and the I2C, ADC/DAC, Philips PCF8951. You will wire the
devices
to the kits with wire jumpers and protoboard. Suggested layouts
and
connections will be provided. Finally, you will adapt the example code
provided above to work with these devices. It will be necessary to read
the data sheet and make some adjustments to the provided code.
Post-Lab Part 1: Interfacing to the Microchip MCP4901 SPI DAC
1.1) The DAC
The Microchip MCP4901 is an 8 bit digital to analog converter (DAC)
with a typical SPI interface. Keep in mind that SPI interfaces
can vary in number of connections, frame size (number of bits), bit
ordering, clock phasing and max
clock speed. This device is relatively typical
and can be interfaced within the operational capability of the MSS SPI
hardware.
1.2) The Data
Sheet
Part of the work of being a microprocessor systems engineer is sorting
through data sheets for relevant information. Much of this data
sheet describes and specifies the digital to analog behavior and
quantifying this behavior. For the most part, you are just
interested in sections that
will allow you to connect the DAC to the SmartFusion kit and
configure the SPI interface.
Sections of interest in the data sheet are:
Section 1: Electrical Characteristics
Figure 1-1 SPI Timing
Section 3: Pin Functions and Numbers.
Section 5: Serial Interface
SPI details to consider:
- Frame Size
- Command and Data Sequence.
- Clock Phasing (determined by SPI transfer mode)
- Maximum Clock Rate
- Bit Ordering
1.3)
Connecting the DAC to the SmartFusion Kit
You can use the pin function descriptions in Section 3 of the data
sheet to determine what each pin of the DAC should be connected
to. Here are the suggested connections.
Connections Between SmartFusion Kit and MCP4901.
Pin |
Function |
Description |
Connection |
1 |
VDD |
Supply Voltage |
Use 3.3 volt supply from expansion board |
2 |
CSS |
Chips Select (active low) |
Connect to SmartFusion SPI Select
Control |
3 |
SCK |
Serial Clock |
Connect to SmartFusion SPI serial clock |
4 |
SCI |
Serial Data Input |
Connect to SmartFusion SPI output data |
5 |
LDAC |
Sync Output Control (active low) |
Enable all the time by connecting to
ground |
6 |
Vref |
Reference Voltage for DAC |
Connect to VDD |
7 |
VSS |
Ground Reference |
Connect to Expansion Board ground |
8 |
output |
DAC Analog Output |
Put a 2 pin header on this connection
to monitor with scope. |
See the following wiring photos
for suggested wiring. For a quick review on prototyping circuits, see this link. We
will be using the SmartFusion
expansion board to provide power connections. The board provides
access to other IO (input/output) that will be used in the following
labs and the projects.
1.4) Debugging and
Verification
- Use the Saleae and protocol analyzer to verify your SPI settings
with
respect to the device specification.
- Use the scope or logic analyzer to make sure the correct signals
and voltages are being applied to device.
- Check to see the analog output corresponds to the digital value
applied.
- Start simple. Begin by just trying to produce a single analog
voltage.
1.5) SPI Interfacing
Post
Lab Deliverables
Demonstration
Write a software application that will produce a triangle wave
at
a frequency
and Vpp
(voltage peak to peak) of your
choosing. Demonstarte to the lab staff.
Post Lab SPI Screen Shot
Screen Shot 4) Submit a screen shot of the traingle wave.
Screen Shot 5)
Submit a screen shot of a SPI transaction showing a write of a
mid value DAC value. The view should be using the SPI protocol
analyzer.
Questions
Question 2) List the SPI configuration changes you had to make from the
example code for the DAC interface.
Question 3) MSS_SPI_set_slave_select
sets the SPI select line. The evaluation kit provides just one select
line on the SPI header. If we wanted to add another DAC, what is one
way you could provide another select line? Consider that you can write
your own select function.
Question 4) What SPI transfer mode (ie MSS_SPI_MODE1) did you have
to use for the DAC and why (hint see figure 1-1 in the data sheet) ?
Post Lab Part 2: Interfacing to the Philips PCF8591 I2C ADC
2.1) The ADC
The Philips PCF8591 is an 8 bit digital to analog converter (DAC) and 8
bit analog to digital converter (ADC) with a
I2C interface. Unlike SPI buses, the I2C is standard. The device
control register and read/write sequences vary with each device.
For this part of the lab you will have to do all the work of sorting
through the data sheet to interface to the device. No connection tables
or photos showing connections will be provided. This of course is
representative of actual engineering work and what we expect you to do
for your project application. Essential suggestions and debugging
hints are listed below.
2.2) The Data
Sheet
For this device, you will sort through the data sheet to
determine what you need to connect to the device and modify the I2C
code. As with the SPI data sheet, you will have to look for
the relevant information. The I2C code provided is a starting
point, but of course you will have to make some changes and
additions. Use the same
wiring approach used to wire the SPI DAC. You do not have to keep your
SPI hardware intact if you have demoed.
Software Considerations
- Take time to understand the I2C read and write protocol for this
device. (page 14 of the data sheet)
- Take time to understand the meaning of the control register and
meaning of each bit. (page 6 of the data sheet)
- Realize that the I2C address byte contain information besides the
I2C slave address. (page 5 of the data sheet)
- See the details for using the MSS library I2C read and write
functions in the associated include file. (mss_spi.h)
- It is necessary to perform an I2C wait after an I2C read or write
with the MSS_I2C_wait_complete.
- Take time to understand what initiates an analog to digital
conversion and the association of the converted value to the conversion
cycle . (page 9 of the data sheet)
Hardware Considerations:
- Read the functional description of each pin and be sure you have
it set or connected to the appropriate signal. (page 4 and 11 of the
data sheet)
- The operational voltage range for the device is between 2.5 and 6
volts. The SmartFusion digital interface uses 3.3 volts so use 3.3
volts as the supply voltage.
- It is not necessary to pull up the I2C bus signals with pull up
resistors.
- Program the analog inputs for single ended input.
- If you happen to reverse the supply voltage to the device,
chances are it is damaged. Get another device from the instructor.
- The ADC and DAC need a clock to
function. An internal clock is
built into the device, but you have to enable it by attaching the EXT
pin (12) to VSS (GND). DO NOT ground the adjacent clock pin. Leave it
unconnected.
2.3)
Debugging and Verification
- Check that each pin of the device is connected correctly. Use a
scope or logic analyzer to verify as necessary.
- Start by configuring the device to be an DAC. Write a single
digital value that will produce an analog voltage. Measure the voltage
to verify it worked. Use the Saleae protocol analyzer to verify
you are sending the correct sequence.
- Once you can do a DAC successfully, try a singe ADC cycle. You
will require to I2C reads to do this. Why? (see software consideration
6). Use the I2C protocol analyzer to verify that you are sending the
correct sequences and receiving the correct information.
- While your wiring does not need to be neat, excessively long
wires can create transmission issues. Also use a bypass capacitor
(>10uf) across the negavtive and positive supplies on the protoboard
as shown in the wiring examples for the SPI DAC device.
2.4) I2C Interfacing
Post
Lab Deliverables
Post Lab I2CScreen Shots
Screen Shot 6) Submit a screen shot of a I2C transaction showing a DAC
conversion of 0x1f. The view should use the I2C protocol
analyzer. Display values in HEX.
Screen Shot 7) Submit a screen shot of a I2C transaction showing an ADC
conversion of the voltage generated by the DAC conversion of 0x1f
(jumper the DAC output to the ADC input). The view should use the I2C
protocol analyzer. Display values in HEX.
I2C Transaction Example
See this image for an example
transaction sequence. The sequence initializes the the device, writes
the value 0xB3 to the DAC then reads from analog
channel 0. The DAC output is connected to
the ADC input. It is necessary to provide a delay between each
write/read pair to allow for device latency. The first value read
from the ADC may not be the correct value.
Demonstration
Write a software application that reads DC values and displays
them in terminal
program scaled to volts. Demonstrate
to a lab
Instructor and get a signature using the
Answer/Demo sheet.