SPI Configuration

Download Report

Transcript SPI Configuration

Serial Sinkron
SPI & I2C Interface
Teknik Antarmuka Komputer
(TKE 4145)
Eka Maulana
maulana.lecture.ub.ac.id
Antarmuka Serial Sinkron
I2C
SPI
Antarmuka Serial Asinkron
Comparison of Serial Interfaces
Outline
• What is SPI?
• SPI Configuration
• SPI Operation
• Master Slave Setup
• SPI Transactions
• SPI Peripheral Types
• SPI and Microcontrollers
• ESBUS
SPI
(Serial Peripheral Interface)
• Developed by Motorola
•Also known as MicroWire (National
Semiconductor), QSPI (Queued), MicrowirePlus
•Synchronous Serial Communication
SPI Bus
SPI Configuration
• Primarily used for serial
communication between a
host processor and
peripherals.
• Can also connect 2
processors via SPI
• SPI works in a master slave
configuration with the
master being the host
microcontroller for example
and the slave being the
peripheral
SPI Operation
• For SPI, there are Serial Clocks (SCLK), Chip Select lines (CS),
Serial Data In (SDI) and Serial Data Out( SDO)
• There is only one master, there number of slaves depends
on the number of chip select lines of the master.
• Synchronous operation, latch on rising or falling edge of
clock, SDI on rising edge, SDO on falling edge
• Operates in 1 to 2 MHz range
• Master sends out clocks and chip selects. Activates the
slaves it wants to communicate with
Master Slave Setup
• In this setup, there are 3
slave devices. The SDO
lines are tied together to
the SDI line of the master.
• The master determines
which chip it is talking to by
the CS lines. For the slaves
that are not being talked to,
the data output goes to a Hi
Z state
• Multiple Independent Slave
Configuration
Master Slave Setup
Multiple slave cascaded
• In this example, each slave is cascaded so that the
output of one slave is the input of another. When
cascading, they are treated as one slave and
connecting to the same chip select
SPI Timing Diagram
EEPROM Read
SPI Transaction
SPI Peripheral Types
• Converters (ADC, DAC)
• Memories (EEPROM, RAM’s,Flash)
• Sensors (Temperature, Humidity, Pressure)
• Real Time Clocks
• Misc- Potentiometers, LCD controllers, UART’s, USB
controller, CAN controller,amplifiers
Peripherals
• Vendors that make these peripherals :
• Atmel –EEPROM, Dig. POT’s
• Infineon- Pressure Sensors, Humidity Sensors
• Maxim- ADC, DAC, UART,
• TI- DSP’s, ADC, DAC
• National Semiconductor- Temperature Sensors,
LCD/USB controllers
SPI and Microcontrollers
• Motorola 68HC12 Has SPI built in hardware. Easy
to integrate. See EE583
• Intel 8051 Depending on Models, Most Cygnal
products have I2C and some have SPI also built in
hardware for easy integration.
PIM_9C32
Block Diagram
SPI module
Pin
PM2
PM3
PM4
PM5
68HCS12 SPI0 Signals
SPI0 signal
Name
MISO
Master-In-Slave-Out
MOSI
Master-Out-Slave-In
SCK
Serial Clock
SS
Slave Select
MASTER
MOSI
7 6 5 4 3 2 1 0
SCK
SCK
MISO
MISO
SP0DR
SLAVE
7 6 5 4 3 2 1 0
SP0DR
SS
SS
MOSI
$DA
$D8
The SPI module
SPIxCON register
Initializing SPI2
 Pin 12 of PORTD is connected to the memory chip select (CS):
#define CSEE
_RD12
// select line for Serial EEPROM
#define TCSEE
_TRISD12
// tris control for CSEE pin
 Peripheral initialization:
// 1. init the SPI peripheral
TCSEE = 0;
// make SSEE pin output
CSEE = 1;
// de-select the Serial EEPROM
SPI2CON1 = SPI_MASTER;
// select mode
SPI2STAT = SPI_ENABLE;
// enable the peripheral
 Where:
#define SPI_MASTER
0x0120 // 8-bit master mode, CKE=1, CKP =0
#define SPI_ENABLE
0x8000 // enable SPI port, clear status
 A small function that will be used to transfer data to and from the serial EEPROM device:
// send one byte of data and receive one back at the same time
int WriteSPI2( int data)
{
SPI2BUF = data;
// write to buffer for TX
while( !SPI2STATbits.SPIRBF);
// wait transfer completion
return SPI2BUF;
// read the received value
} // WriteSPI2
The 25LC256 Serial EEPROM
 // 25LC256 Serial EEPROM commands
 #define SEE_WRSR
1
// write status register
 #define SEE_WRITE
2
// write command
 #define SEE_READ
3
// read command
 #define SEE_WDI
4
// write disable
 #define SEE_STAT
5
// read status register
 #define SEE_WEN
6
// write enable
25LC256 Status Register
// 2. Check the Serial EEPROM status
CSEE = 0;
// select the Serial EEPROM
writeSPI2( SEE_STAT);
// send a READ STATUS COMMAND
i = writeSPI2( 0);
// send/receive
CSEE = 1;
// deselect, terminate command
// 2.1 send a Write Enable command
CSEE = 0;
// select the Serial EEPROM
WriteSPI2( SEE_WEN);
// write enable command
CSEE = 1;
// deselect, terminate command
Writing to the EEPROM
// send a Write command
CSEE = 0;
//
WriteSPI2( SEE_WRITE);
//
WriteSPI2( addr_MSB);
//
WriteSPI2( addr_LSB);
//
WriteSPI2( data);
//
// continue writing more data…
CSEE = 1;
select the Serial EEPROM
write command
address MSB first
address LSB (word aligned)
send 8-bit of data
// wait until any work in progress is completed
while ( ReadSR() & 0x1);
// check the WIP flag
Reading from the EEPROM
// perform a read sequence
CSEE = 0;
// select the Serial EEPROM
WriteSPI2( SEE_READ);
// read command
WriteSPI2( addr_MSB);
// address MSB first
WriteSPI2( addr_LSB);
// address LSB (word aligned)
data = WriteSPI2( 0);
// send dummy, read msb
// continue reading a second byte, a third…
CSEE = 1;
I2C
I2C Interface Examples
A few examples of devices using the I2C interface :
 1Mbit Serial EEPROMS: 24xx1025
 18-bit delta sigma ADCs: MCP3421
 16-bit delta sigma ADCs: MCP3425
 12-bit SAR ADCs:MCP3221
 12-bit D/A: MCP4725
 Integrated Temperature Sensor (+/-0.5C): MCP9803
 I/O Expander 8/16-bit: MCP23016/MCP2308
Consider additionally:
 Battery gauges
 Audio codecs
 GPS receivers
 LCD displays controllers
 Card readers
I2C Data Transfer Rules
Two simple rules dictate how to operate from here:
 When the SCL line is low, and only at this time, the SDA line can change.
 When the SCL line is high, the SDA line status indicates the value of a bit.
Two exceptions to rule 1 create special conditions that are used to delimit the
beginning and end of each transaction between two devices on the bus. When
SCL is high:
 A START condition is indicated by the SDA line changing from high to low
 A STOP condition is indicated by the SDA line changing from low to high
The 24LC00 Serial EEPROM
I2C has been for years the favorite choice for serial EEPROM users and
manufacturers for two reasons:
 Only two pins (I/Os) are required to communicate with the device, enabling the
embedded control designer to use very low pin count (inexpensive)
microcontrollers.
 Just four pads (areas of contacts on the chip) are required for a Serial EEPROM
device. Two provide the power supply and the remaining two are the I2C lines.
This reduces the silicon area overhead due to the contacts on a typically very
small device -- hence the high impact/cost of the pads.
In practice most I2C serial EEPROMs have at least a fifth contact/pin (WP) to
protect the device contents by disabling writing.
Talking to the Serial EEPROM
The SEE Grammar
Using the following simple notation:
 S = START sequence
 P = STOP sequence
 A/N = Acknowledge bit
 0xXX= data byte (hex notation)
 0bXXXXXXXX =data byte (in binary notation)
 and using brackets () to indicate parts of the conversation produced by a slave we can represent a
typical I2C protocol message in a compact notation
Here is an example of a read command sequence for a 24LC00 (128 bit) SEE:
 S 0b01010001 (A) (Data) NP
A Write Example
 S 0b01010000 (A) ByteAddress (A) Data (A) P
I2CxCON register
Initializing the I2C module
void InitSEE( long fcy)
// fcy = processor operating frequency in Hz (system clock)
{
// Configure I2C for 7 bit address mode 100kHz
OpenI2C1( I2C_ON | I2C_IDLE_CON | I2C_7BIT_ADD | I2C_STR_EN
| I2C_GCALL_DIS | I2C_SM_DIS | I2C_IPMI_DIS,
(fcy /(2*BUS_FRQ))-1);
IdleI2C1();
T1CON=0x8030;
TMR1=0;
while( TMR1< 100);
} //InitSEE
Sending the Address
int addressSEE( long add)
if (I2C1STATbits.ACKSTAT==0)
// send the address selection command
break;
// repeat if SEE busy
StopI2C1();
{
IdleI2C1();
int cmd;
(3)
} // while waiting for ACK
// 1. Form SEE command + address msb
// 3. send byte address
MasterWriteI2C1( add);
cmd= 0xA0|((add>>7)&0xE);
IdleI2C1();
// 2. WRITE(!) the Address msb
// try send command and repeat until
ACK is received
while( 1)
{
StartI2C1();
IdleI2C1();
// send command and address msb(3)
MasterWriteI2C1( cmd+WRITE_CMD);
IdleI2C1();
byte
// 4. exit returning the cmd
return cmd;
} // addressSEE
Logic Analyzer Capture
Writing a 16-bit value
S 0b01010000 (A) ByteAddress (A) DataLSB (A) DataMSB (A) P
void iWriteSEE( long add, int v)
// SEE write command sequence
{
int cmd;
// 1. select address
cmd = AddressSEE( add);
// 2. stream data out
MasterWriteI2C1( v&0xFF);
IdleI2C1();
MasterWriteI2C1( v>>8);
IdleI2C1();
// 3. terminate the command sequence
StopI2C1();
IdleI2C1();
} // iWriteSEE
Reading a 16-bit value
S 0b01010000 (A) ByteAddress(A) P
S 0b01010001 (A) (DataLSB) [A] (Data MSB) NP
Address Selection
Data Read
int iReadSEE( long add)
// random access read command sequence
AckI2C1(); IdleI2C1();
{
r|= (MasterReadI2C1()<<8);
int cmd, r;
// 1. select address
cmd = AddressSEE( add);
StopI2C1();
IdleI2C1();
// 4. terminate read sequence (send NACK
then STOP)
NotAckI2C1(); IdleI2C1();
StopI2C1(); IdleI2C1();
// 2. read command
StartI2C1(); IdleI2C1();
MasterWriteI2C1( cmd+READ_CMD);
IdleI2C1();
// 3. stream data in (will continue
until NACK is sent)
r= MasterReadI2C1( );
return r;
} // iReadSEE
Notes for the PIC MCU Experts
In addition to the SPI options available on most PIC microcontroller
(offered by the SSP and MSSP modules), such as:
Selectable clock polarity
Selectable clock edge
Master or Slave mode operation
The PIC24 SPI interface module adds several new capabilities, including:
A 16-bit transfer mode
Data input sampling phase selection
Framed transmission mode
Frame synch pulse control (polarity and edge selectable)
Enhanced Mode (8 deep transmit and receive FIFOs)
Using the new Enhanced Mode, with 8 levels deep FIFOs , up to 8 words of
data (16 bytes) can be written or retrieved from the SPI buffers in short
bursts, leaving more time to the CPU to process the data in between.
Tips
and
Tricks
Safety measures suggested to reduce the risk of SEE data
corruption:
Ensure adequate power supply decoupling (a capacitor) is
provided close to the memory device.
A pull up resistor (10k Ohm) is provided on the Chip Select line,
to avoid floating during the microcontroller power up and
reset.
An additional pull down resistor (10k Ohm) can be provided on
the SCK clock line to avoid clocking of the peripheral during
boundary scan and other board testing procedures.
Verify clean and fast power-up and down slope are provided to
the microcontroller to guarantee reliable Power On Reset
operation. If necessary add an external voltage supervisor (see
MCP809 devices for example).
Tips and Tricks
A number of software methods can be employed to prevent a program bug might
trigger the write routine:
 Avoid reading and especially updating the NVM content right after power up.
Allow a few milliseconds for the power supply to stabilize (application
dependent).
 Add a software write-enable flag, and demand that the calling application set
the flag before calling the write routine, possibly after verifying some essential
application specific entry condition.
 Add a stack level counter; each function in the stack of calls implemented by the
library should increments the counter upon entry and decrement it on exit. The
write routine should refuse to perform if the counter is not at the expected
level.
 Some refuse to use the NVM memory locations corresponding to the first
address (0x0000) and/or the last address (0xffff) believing they could be more
likely to be the subject to corruption.
 Store two copies of each essential piece of data, performing two separate calls
to the write routine. If each copy contains even a simple checksum, it will be
easy, when reading it back, to discard the corrupted one and recover.
Suggested Excercises
 Several functions in the library are performing locking loops that could reduce
the overall application performance. By utilizing the SPI port interrupts
implement a non blocking version of the library.
 Enable the new SPI 16-bit mode to accelerate basic read and write operation.
 Develop (circular) buffered versions of the read and write functions.