Chapter 7 Synchronous Serial SPI and I2C interfaces.

Slides:



Advertisements
Similar presentations
INPUT-OUTPUT ORGANIZATION
Advertisements

Chapter 4 NUMB3RS.
Dr. Rabie A. Ramadan Al-Azhar University Lecture 3
FPGA Configuration. Introduction What is configuration? – Process for loading data into the FPGA Configuration Data Source Configuration Data Source FPGA.
EXTERNAL COMMUNICATIONS DESIGNING AN EXTERNAL 3 BYTE INTERFACE Mark Neil - Microprocessor Course 1 External Memory & I/O.
Programmable Interval Timer
I/O Unit.
Processor System Architecture
LOGSYS Development Environment of Embedded Systems Tamás Raikovich Béla Fehér Péter Laczkó Budapest University of Technology and Economics Department of.
SPI Serial Peripheral Interface. SPI Serial Peripheral Interface is communication between two devices, one bit at a time sequential one bit at time over.
Lecture 8: Serial Interfaces
CS-334: Computer Architecture
Introduction of Holtek HT-46 series MCU
Serial Communication Buses: I 2 C and SPI By Brody Dunn.
Serial Peripheral Interface (SPI)
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
GURSHARAN SINGH TATLA PIN DIAGRAM OF 8085 GURSHARAN SINGH TATLA
Chapter 8 Communication Introduction to serial communication interfaces. Examples of use of the SPI module.
Synchronous Serial IO Send a separate clock line with data
Programming 8-bit PIC Microcontrollers in C Martin Bates Elsevier 2008.
INPUT-OUTPUT ORGANIZATION
Chapter 1 The First Flight Creating the first project and saying “Hello to the World”
Serial Peripheral Interface (SPI) Bus. SPI Bus There is no official specification for the SPI bus. It is necessary to consult the data sheets of the devices.
Serial Peripheral Interface Module MTT M SERIAL PERIPHERAL INTERFACE (SPI)
LSU 10/22/2004Serial I/O1 Programming Unit, Lecture 5.
4.0 rtos implementation part II
Input/Output mechanisms
7/23 Inter-chip Serial Communication: SPI and I 2 C Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee.
1 SERIAL PORT INTERFACE FOR MICROCONTROLLER EMBEDDED INTO INTEGRATED POWER METER Mr. Borisav Jovanović, Prof.dr Predrag Petković, Prof.dr. Milunka Damnjanović,
Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing.
The 8253 Programmable Interval Timer
MICROPROCESSOR INPUT/OUTPUT
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Principles of I/0 hardware.
Lecture 20: Communications Lecturers: Professor John Devlin Mr Robert Ross.
DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK
(More) Interfacing concepts. Introduction Overview of I/O operations Programmed I/O – Standard I/O – Memory Mapped I/O Device synchronization Readings:
Advanced Microprocessor1 I/O Interface Programmable Interval Timer: 8254 Three independent 16-bit programmable counters (timers). Each capable in counting.
8279 KEYBOARD AND DISPLAY INTERFACING
PCA9557: REMOTE 8-BIT I 2 C AND SMBus LOW- POWER I/O EXPANDER.
MICROCONTROLLER SYSTEMS Part 1. Figure 1.1Elements of a digital controller CPU Central Processing Unit Input Peripherals Output Peripherals ROM Read Only.
1 Synchronous Serial IO Send a separate clock line with data –SPI (serial peripheral interface) protocol –I 2 C (or I2C) protocol Encode a clock with data.
Chapter 2 Introducing the PIC Mid-Range Family and the 16F84A The aims of this chapter are to introduce: The PIC mid-range family, in overview The overall.
Accessing I/O Devices Processor Memory BUS I/O Device 1 I/O Device 2.
Embedded Network Interface (ENI). What is ENI? Embedded Network Interface Originally called DPO (Digital Product Option) card Printer without network.
©2008 R. Gupta, UCSD COSMOS Summer 2008 Peripheral Interfaces Rajesh K. Gupta Computer Science and Engineering University of California, San Diego.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose:  This course provides an overview of the serial communication.
8279 KEYBOARD AND DISPLAY INTERFACING
S4525A Peripherals & Enhanced FLASH 1 © 1999 Microchip Technology Incorporated. All Rights Reserved. S4525A Peripherals & Enhanced FLASH 1 Peripherals.
Chapter 5 - Interrupts.
Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.
Serial Peripheral Interface SPI I2C (i-squared cee)
بسم الله الرحمن الرحيم MEMORY AND I/O.
BIRLA VISHVAKARMA MAHAVIDYALAY SUBJECT: SPI PROTOCOL & MAX7221 DISPLAY INTERFACING SUBMITTED BY: KISHAN AVASTHI( ) MANSI CHANDEGARA( )
CEng3361/18 CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT Spring 2007 Recitation 01.
CORDLESS MOUSE FEATURES BY TV REMOTE USING PIC MICROCONTROLLER
Serial Sinkron SPI & I2C Interface
I/O SYSTEMS MANAGEMENT Krishna Kumar Ahirwar ( )
Serial Communication Buses: I2C and SPI
Microcontroller Applications
Chapter 11: Inter-Integrated Circuit (I2C) Interface
1 Input-Output Organization Computer Organization Computer Architectures Lab Peripheral Devices Input-Output Interface Asynchronous Data Transfer Modes.
EE 107 Fall 2017 Lecture 7 Serial Buses – I2C Direct Memory Access
I2C PROTOCOL SPECIFICATION
Communication Lines Fundamentals.
Introduction to Microprocessors and Microcontrollers
Serial EEPROM (Atmel 24C-512)
I2C Protocol and RTC Interfacing
8253 – PROGRAMMABLE INTERVAL TIMER (PIT). What is a Timer? Timer is a specialized type of device that is used to measure timing intervals. Timers can.
Presentation transcript:

Chapter 7 Synchronous Serial SPI and I2C interfaces

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used in this lesson:  MPLAB X, Integrated Development Environment (v1.8 or later, free)  MPLAB XC16, C compiler (v1.11 or later, free) The following pieces of documentation will be used during this lesson:  PIC24FJ128GA010 Datasheet –DS39747 (latest rev.) Make sure they are available and/or installed and ready to use on your computer. You can download them from Microchip web site at: And The following hardware tools are recommended:  Explorer 16 Demonstration Board  PIC24FJ128GA010 PIM  24LC00 or similar Serial EEPROM  2 x 4.5K Ohm resistors

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Synchronous Serial Interfaces  I 2 C  SPI

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) SPI Bus

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Asynchronous Serial Interface

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Comparison of Serial Interfaces

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) The SPI module

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) SPIxCON register

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Writing to the EEPROM // send a Write command CSEE = 0; // select the Serial EEPROM WriteSPI2( SEE_WRITE); // write command WriteSPI2( addr_MSB); // address MSB first WriteSPI2( addr_LSB); // address LSB (word aligned) WriteSPI2( data); // send 8-bit of data // continue writing more data… CSEE = 1; // wait until any work in progress is completed while ( ReadSR() & 0x1); // check the WIP flag

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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;

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) I2C Interface Examples A few examples of devices using the I 2 C 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

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Application Specific Protocols Examples of application specific protocols based on the I 2 C interface are:  SMBus™, the System Management Bus, used in personal computers (and servers) for low speed system management applications  IPMI™, the Intelligent Platform Management Interface, used to monitor the system health and manage systems (mostly servers) remotely.  PMBus™, the Power Management Bus, used by advanced digitally controlled power supply units to monitor, configure and control power.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) The 24LC00 Serial EEPROM I 2 C 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 I 2 C 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 I 2 C serial EEPROMs have at least a fifth contact/pin (WP) to protect the device contents by disabling writing.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Talking to the Serial EEPROM

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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 I 2 C protocol message in a compact notation Here is an example of a read command sequence for a 24LC00 (128 bit) SEE:  S 0b (A) (Data) NP

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) A Write Example  S 0b (A) ByteAddress (A) Data (A) P

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) I2CxCON register

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Sending the Address int addressSEE( long add) // send the address selection command // repeat if SEE busy { int cmd; // 1. Form SEE command + address msb (3) cmd= 0xA0|((add>>7)&0xE); // 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(); if (I2C1STATbits.ACKSTAT==0) break; StopI2C1(); IdleI2C1(); } // while waiting for ACK // 3. send byte address MasterWriteI2C1( add); IdleI2C1(); // 4. exit returning the cmd byte return cmd; } // addressSEE

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Logic Analyzer Capture

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Writing a 16-bit value 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 S 0b (A) ByteAddress (A) DataLSB (A) DataMSB (A) P

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Reading a 16-bit value int iReadSEE( long add) // random access read command sequence { int cmd, r; // 1. select address cmd = AddressSEE( add); StopI2C1(); IdleI2C1(); // 2. read command StartI2C1(); IdleI2C1(); MasterWriteI2C1( cmd+READ_CMD); IdleI2C1(); // 3. stream data in (will continue until NACK is sent) r= MasterReadI2C1( ); AckI2C1(); IdleI2C1(); r|= (MasterReadI2C1()<<8); // 4. terminate read sequence (send NACK then STOP) NotAckI2C1(); IdleI2C1(); StopI2C1(); IdleI2C1(); return r; } // iReadSEE S 0b (A) ByteAddress(A) P S 0b (A) (DataLSB) [A] (Data MSB) NP Address Selection Data Read

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) I2C Serial EEPROM Summary

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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).

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) 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.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Recommended Readings  Eady, F. (2004), Networking and Internetworking with Microcontrollers, Newnes, Burlinghton, MA  An entertaining introduction to serial communication in embedded control.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Online Resources  Total Endurance Software   This tool will help you estimate the endurance to expect from a given NVM device in your actual application conditions. It will give you an indication of the total number of e/w cycles or the number of expected years of your application life before a certain target failure-rate is reached.