Serial Interconnect Buses— I2C (SMB) and SPI Serial Peripheral

advertisement
Purpose of Serial Interconnect
Buses
Serial Interconnect Buses—
I2C (SMB) and SPI
55:036
Embedded Systems and Systems
Software
Purpose of Serial Interconnect
Buses
• Provide low-cost—i.e low wire/pin count—
connection between IC devices
• There are lots of serial bus “standards”
–
–
–
–
–
–
–
I2C
SMB
SPI
Microwire
Maxim 3-wire
Maxim/Dallas 1-wire
etc.
We’ll focus on these.
• Provide low-cost—i.e low wire/pin count—
connection between IC devices
• There are lots of serial bus “standards”
–
–
–
–
–
–
–
I2C
SMB
SPI
Microwire
Maxim 3-wire
Maxim/Dallas 1-wire
etc.
Serial Peripheral Interface (SPI)
• Originally developed by Motorola
• Synchronous, serial protocol
– Data timing is controlled by an explicit clock
signal (SCK)
• Master-slave
– Master device controls the clock
• Bi-directional data exchange
– data clocked into and out-of device at same
time
1
SPI signals
SPI Data Loop
• SS (CS) (Slave Select, Chip Select)
– When SS is low the slave is enabled
• SCK (Serial Clock)
SSPSR
SDO
SDI
SDI
SDO
SSPSR
– Controls the sending and reading of data
• SD0 (Serial Data Out)
SSPBUF
SSPBUF
– Carries data OUT of the device
SCK
Control
• SDI (Serial Data In)
– Carries data INTO the device
SS
Slave
SPI Data Loop
Serial Buffer: This is the register read and
written by your program
Internal Shift Register: Loaded by
SPI data or from SSPBUF
SSPSR
SDI
SDI
SDO
SSPBUF
SCK
Control
Master
SSPSR
SSPSR
SSPBUF
SSPBUF
SCK
SS
Control
Master
SPI Data Loop
SDO
SCK
SDO
SDI
SDI
SDO
SSPBUF
SCK
Control
Slave
Control
Master
SSPSR
SCK
SS
Control
Slave
2
SPI Data Loop
SSPSR
SDO
SDI
SDI
SDO
SSPBUF
SCK
SPI Data Loop
SSPSR
SSPSR
SSPBUF
SSPBUF
SS
PIC 18F452 SPI Module
SDO
RC5/SD0
SDI
RC4/SDI
SSPBUF
3
PIR1
…
SDO
…
SSPIF
(flag)
Control SCK RC3/SCK
SSPSR
SCK
Control
Control
SS
Control
Slave
Master
Master generates the clock the controls the data transfer
internal Shift Reg.
SDI
SCK
Slave
Master
SDI
SSPBUF
SCK
Control
SDO
Master controls which slave is selected by asserting the
slave’s SS
PIC 18F452 SPI Module
Actually
the SPI
Interface is
part of a
multifunctional
PIC module
called MSSP
(Master
Synchronous
Serial Port)
that supports
both SPI
and I2C
internal Shift Reg.
SDO
RC5/SD0
SDI
RC4/SDI
SSPBUF
3
PIR1
…
…
SSPIF
(flag)
Control SCK RC3/SCK
3
PIC 18F452 SPI Module
internal Shift Reg.
SDO
RC5/SD0
SDI
RC4/SDI
SSPBUF
A write to SSPBUF
initiates both SDO and
SDI transfers
PIC 18F452 SPI Module
internal Shift Reg.
RC5/SD0
SDI
RC4/SDI
SSPBUF
3
…
SDO
3
…
SSPIF
(flag)
…
…
SSPIF
(flag)
Control SCK RC3/SCK
SPI Timing
A write to SSPBUF
initiates both SDO and
SDI transfers
At completion of transfer
SSPIF is set (must be
cleared prior to next
transfer). SSPBUF
now holds SDI input
Control SCK RC3/SCK
SPI Modes
4
SPI Modes
Connecting Multiple SPI Devices
Mode selection is
controlled by three
bits:
CKP (Clock Polarity)
CKE (Clock edge select)
SMP (SPI sample time)
…
arbitrary
pins
Determining the right
mode to use for a
given device can be
tricky (see section 15.3
in the text)
Connecting Multiple SPI Devices
…
arbitrary
pins
SDO SDI SCK
SS SDO SDI SCK
(RC5)(RC4)(RC3)
PIC18F452 (Master)
SPI Slave 1
…
SS SDO SDI SCK
SPI Slave K
Connecting Multiple SPI Devices
…
SDO SDI SCK
SS SDO SDI SCK
(RC5)(RC4)(RC3)
PIC18F452 (Master)
SPI Slave 1
Slave selection is NOT handled by the MSSP
Unit
…
SS SDO SDI SCK
SPI Slave K
arbitrary
pins
SDO SDI SCK
SS SDO SDI SCK
(RC5)(RC4)(RC3)
PIC18F452 (Master)
SPI Slave 1
Note: On the QwikFlash Boards
the MAX522 DAC is permanently
connected to the SPI interface
(RC0 is used for SS)
…
SS SDO SDI SCK
SPI Slave K
Pins RC3-RC5
(SCK, SDI, SDO)
are also brought
out to the H2
expansion header
to allow connection
of additional SPI
devices
5
Using SPI with C18 C
• Setting up the SPI Unit
Function: Initialize the SSP module.
Include: spi.h
Prototype: void OpenSPI( unsigned char sync_mode,
unsigned char bus_mode,
unsigned char smp_phase);
Using SPI with C18 C
Setting up the SPI Unit (continued)
smp_phase
One of the following values, defined in spi.h:
SMPEND Input data sample at end of data out
SMPMID Input data sample at middle of data out
Remarks: This function sets up the SSP module for use with a SPI
bus device.
File Name: spi_open.c
Code Example: OpenSPI(SPI_FOSC_16, MODE_00, SMPEND);
Using SPI with C18 C
Setting up the SPI Unit (continued)
Arguments: sync_mode
One of the following values, defined in spi.h:
SPI_FOSC_4 SPI Master mode, clock = FOSC/4
SPI_FOSC_16 SPI Master mode, clock = FOSC/16
SPI_FOSC_64 SPI Master mode, clock = FOSC/64
SPI_FOSC_TMR2 SPI Master mode, clock = TMR2 output/2
SLV_SSON SPI Slave mode, /SS pin control enabled
SLV_SSOFF SPI Slave mode, /SS pin control disabled
bus_mode
One of the following values, defined in spi.h:
MODE_00 Setting for SPI bus Mode 0,0
MODE_01 Setting for SPI bus Mode 0,1
MODE_10 Setting for SPI bus Mode 1,0
MODE_11 Setting for SPI bus Mode 1,1
Using SPI with C18 C
Writing to the SPI bus:
WriteSPI
putcSPI
Function: Write a byte to the SPI bus.
Include: spi.h
Prototype: unsigned char WriteSPI(unsigned char data_out );
unsigned char putcSPI( unsigned char data_out );
Arguments: data_out Value to be written to the SPI bus.
Remarks: This function writes a single data byte out and then checks for a write
collision. putcSPI is defined to be WriteSPI in spi.h.
Return Value: 0 if no write collision occurred
-1 if a write collision occurred
File Name: spi_writ.c
#define in spi.h
Code Example: WriteSPI(‘a’);
6
Using SPI with C18 C
Another Serial Bus
Reading from the SPI bus:
ReadSPI
getcSPI
Function: Read a byte from the SPI bus.
Include: spi.h
Prototype: unsigned char ReadSPI( void );
unsigned char getcSPI( void );
Remarks: This function initiates a SPIx bus cycle for the acquisition of a byte of
data. getcSPI is defined to be ReadSPI in spi.h.
Return Value: This function returns a byte of data read during a SPI read cycle.
File Name: spi_read.c
#define in spi.h
• I2C (Inter-IC)
– Two-wire serial bus protocol developed by Philips
Semiconductors nearly 20 years ago
– Enables peripheral ICs to communicate using simple
communication hardware
– Data transfer rates up to 100 kbits/s and 7-bit
addressing possible in normal mode
– 3.4 Mbits/s and 10-bit addressing in fast-mode
– Common devices capable of interfacing to I2C bus:
• EPROMS, Flash, and some RAM memory, real-time clocks,
watchdog timers, and microcontrollers
Code Example: char x;
x = ReadSPI();
I2C bus structure
I2C bus structure
SCL
SDA
PIC18f452 uses
pins RC3/RC4.
Drivers are opendrain so pullup
resisters are needed
See section 17.3
in the text.
< 400 pF
SCL
SDA
Microcontroller
(master)
EEPROM
(servant)
Addr=0x01
LCDcontroller
(servant)
Temp.
Sensor
(servant)
Addr=0x02
Microcontroller
(master)
< 400 pF
EEPROM
(servant)
Addr=0x03
Addr=0x01
Addr=0x02
SDA
SDA
SDA
SDA
SDA
SDA
SDA
SCL
SCL
SCL
SCL
SCL
SCL
SCL
Start condition
Sending 0
Sending 1
Stop condition
Start condition
Addr=0x03
SDA
SCL
Sending 0
Sending 1
From
receiver
From
Servant
LCDcontroller
(servant)
Temp.
Sensor
(servant)
Stop condition
From
receiver
From
Servant
D
D
C
C
S
T
A
R
T
A
6
A
5
A
0
R
/
w
A
C
K
D
8
D
7
D
0
A
C
K
S
T
O
P
S
T
A
R
T
A
6
A
5
A
0
R
/
w
A
C
K
D
8
D
7
D
0
A
C
K
S
T
O
P
Typical read/write cycle
Typical read/write cycle
start
condition
7-bit address
R/W
8-bit data
7
Download