The following document contains information on Cypress products.
Colophon The products described in this document are designed, developed and manufactured as contemplated for general use, including without limitation, ordinary industrial use, general office use, personal use, and household use, but are not designed, developed and manufactured as contemplated (1) for any use that includes fatal risks or dangers that, unless extremely high safety is secured, could have a serious effect to the public, and could lead directly to death, personal injury, severe physical damage or other loss (i.e., nuclear reaction control in nuclear facility, aircraft flight control, air traffic control, mass transport control, medical life support system, missile launch control in weapon system), or (2) for any use where chance of failure is intolerable (i.e., submersible repeater and artificial satellite). Please note that Spansion will not be liable to you and/or any third party for any claims or damages arising in connection with above-mentioned uses of the products. Any semiconductor devices have an inherent chance of failure. ou must protect against injury, damage or loss from such failures by incorporating safety design measures into your facility and equipment such as redundancy, fire protection, and prevention of over-current levels and other abnormal operating conditions. If any products described in this document represent goods or technologies subject to certain restrictions on export under the Foreign Exchange and Foreign Trade Law of Japan, the US Export Administration Regulations or the applicable laws of any other country, the prior authorization by the respective government entity will be required for export of those products. Trademarks and otice The contents of this document are subject to change without notice. This document may contain information on a Spansion product under development by Spansion. Spansion reserves the right to change or discontinue work on any product without notice. The information in this document is provided as is without warranty or guarantee of any kind as to its accuracy, completeness, operability, fitness for particular purpose, merchantability, non-infringement of third-party rights, or any other warranty, express, implied, or statutory. Spansion assumes no liability for any damages of any kind arising out of the use of the information in this document. Copyright 2013 Spansion Inc. All rights reserved. Spansion, the Spansion logo, MirrorBit, MirrorBit Eclipse TM, ORAD TM and combinations thereof, are trademarks and registered trademarks of Spansion LLC in the United States and other countries. Other names used are for informational purposes only and may be trademarks of their respective owners.
Fujitsu Microelectronics Europe Application ote MCU-A-390104-E-V12 F²MC-8L/16LX FAMIL 8/16-BIT MICROCOTROLLER MB90340 SPI COMMUICATIO TO/FROM SERIAL EEPROM (for M93CS46) APPLICATIO OTE
SPI COMMUICATIO TO/FROM SERIAL EEPROM Revision History Revision History Date Issue 2003-04-26 V1.0; MWi 2003-04-29 V1.1: MWi; Flowcharts added 2010-06-11 V1.2; MWi; CPHA, CPOL logic corrected This document contains 19 pages. MCU-A-390104-E-V12-2 - Fujitsu Microelectronics Europe GmbH-10
SPI COMMUICATIO TO/FROM SERIAL EEPROM Warranty and Disclaimer Warranty and Disclaimer The use of the deliverables (e.g. software, application examples, target boards, evaluation boards, starter kits, schematics, engineering samples of IC s etc.) is subject to the conditions of Fujitsu Microelectronics Europe GmbH ( FME ) as set out in (i) the terms of the License Agreement and/or the Sale and Purchase Agreement under which agreements the Product has been delivered, (ii) the technical descriptions and (iii) all accompanying written materials. Please note that the deliverables are intended for and must only be used for reference in an evaluation laboratory environment. The software deliverables are provided on an as-is basis without charge and are subject to alterations. It is the user s obligation to fully test the software in its environment and to ensure proper functionality, qualification and compliance with component specifications. Regarding hardware deliverables, FME warrants that they will be free from defects in material and workmanship under use and service as specified in the accompanying written materials for a duration of 1 year from the date of receipt by the customer. Should a hardware deliverable turn out to be defect, FME s entire liability and the customer s exclusive remedy shall be, at FME s sole discretion, either return of the purchase price and the license fee, or replacement of the hardware deliverable or parts thereof, if the deliverable is returned to FME in original packing and without further defects resulting from the customer s use or the transport. However, this warranty is excluded if the defect has resulted from an accident not attributable to FME, or abuse or misapplication attributable to the customer or any other third party not relating to FME or to unauthorised decompiling and/or reverse engineering and/or disassembling. FME does not warrant that the deliverables do not infringe any third party intellectual property right (IPR). In the event that the deliverables infringe a third party IPR it is the sole responsibility of the customer to obtain necessary licenses to continue the usage of the deliverable. In the event the software deliverables include the use of open source components, the provisions of the governing open source license agreement shall apply with respect to such software deliverables. To the maximum extent permitted by applicable law FME disclaims all other warranties, whether express or implied, in particular, but not limited to, warranties of merchantability and fitness for a particular purpose for which the deliverables are not designated. To the maximum extent permitted by applicable law, FME s liability is restricted to intention and gross negligence. FME is not liable for consequential damages. Should one of the above stipulations be or become invalid and/or unenforceable, the remaining stipulations shall stay in full effect. The contents of this document are subject to change without a prior notice, thus contact FME about the latest one. Fujitsu Microelectronics Europe GmbH - 3 - MCU-A-390104-E-V12
SPI COMMUICATIO TO/FROM SERIAL EEPROM Contents Contents REVISIO HISTOR... 2 WARRAT AD DISCLAIMER... ERROR! BOOKMARK OT DEFIED. COTETS... 3 0 ITRODUCTIO... 5 1 M93CS46... 6 1.1 EEPROM... 6 1.2 Connection to MB90340... 6 1.3 Communication Timing... 7 1.3.1 Write Cycle Timing of M93CS46... 7 1.3.2 Read Cycle Timing of M93CS46... 7 1.3.3 EEPROM Busy Timing... 7 1.3.4 Leading Zeros... 8 1.4 Used EEPROM Commands... 8 1.5 Example Code... 8 1.5.1 Initial Functions and Declarations... 9 1.5.1.1 Flowchart... 9 1.5.1.2 C Code... 10 1.5.2 Write Enable and Write Disable... 11 1.5.2.1 Flowchart... 11 1.5.2.2 C Code... 12 1.5.3 Write to EEPROM... 13 1.5.3.1 Flowchart... 13 1.5.3.2 C Code... 14 1.5.4 Read from EEPROM... 15 1.5.4.1 Flowchart... 15 1.5.4.2 C Code... 16 1.5.5 Example of usage of EEPROM functions in Main Function... 17 1.5.5.1 Flowchart... 17 1.5.5.2 C Code... 18 2 BIBLIOGRAPH... 19 2.1 Related Documents... 19 MCU-A-390104-E-V12-4 - Fujitsu Microelectronics Europe GmbH-10
SPI COMMUICATIO TO/FROM SERIAL EEPROM Introduction 0 Introduction This application note describes how to communicate via SPI using the MB90340-UART with a serial EEPROM. In this note a M93CS46 EEPROM from ational Semiconductor is used. Please note, that this document only gives a rough overview about the communication. The described source codes were written for understanding not for code size or speed. either interrupts nor timer were used. Time critical program code is always performed by simple flag polling or wait loops. Please also refer to the Application ote mcu-an-300002-e-spi for further SPI specification and nomenclature. Fujitsu Microelectronics Europe GmbH - 5 - MCU-A-390104-E-V12
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1 M93CS46 THIS CHAPTER DESCRIBES HOW TO COMMUICATE WITH THE M93CS46 EEPROM 1.1 EEPROM The M93CS46 serial EEPROM from ational Semiconductor has 2048-Bit memory size, organized as 64 16-Bit-Words. The M93CS46 has the following pin-out: CS 1 8 Vcc SK 2 7 PRE DI 3 6 PE DO 4 5 GD Pin names: CS SK DI DO GD PE PRE* Vcc Chip Select Serial Data Clock Serial Data Input Serial Data Output Ground Program Enable Protect Register Enable Power Supply (+ 5 volts) 1.2 Connection to MB90340 The EEPROM can be connected as in the following schematic. Please note, that no power supply pins and other MCU-Pins are drawn than those for the connection to the EEPROM. * * PRE is not used in the code example below. MCU-A-390104-E-V12-6 - Fujitsu Microelectronics Europe GmbH-10
1.3 Communication Timing SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 Detailed description of the timing and timing parameters can be found in the corresponding datasheet of the M93CS46 EEPROM. 1.3.1 Write Cycle Timing of M93CS46 A write cycle (write to EEPROM) has the following bit timing (SPI-CPOL = 0, SPI-CPHA = 0): CS SCK SOT (DI) D7 D6 D5 D4 D3 D2 D1 D0 Data sampling of the EEPROM is performed at the rising edge of the SCK signal. 1.3.2 Read Cycle Timing of M93CS46 Unfortunately the read cycle has a different timing. The M93CS46-EEPROM does not assert the first data bit after CS goes 0, but on the rising edge of the first serial clock: CS SCK SI (DO) D7 D6 D5 D4 D3 D2 D1 D0 The red arrow denotes the sampling time of the UART, if the clock delay by a half cycle (SPI-CPHA = 0) is disabled (ECCR-SCDE = 0). The workaround, which is presented here, is to switch the SPI-CPHA = 1 (SCDE = 0) just before reading the EEPROM out. This can be performed, because writing to and reading from the EEPROM does not overlap. After reading the bits, the communication is set back to SPI-CPHA = 0 (ECCR-SCDE = 1). 1.3.3 EEPROM Busy Timing After writing data to the EEPROM it signals a busy state by setting a 0 to the serial output (DO/SI). After the busy state the pin goes to 1. In the code example below, this busy wait is simply performed by polling the serial input of the UART (ESCR-SIOP) in two steps. First step is waiting for 0, the second is waiting for 1. Fujitsu Microelectronics Europe GmbH - 7 - MCU-A-390104-E-V12
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 Because the busy state can take up to 10 ms, a time critical software should use a timer for this, to save CPU performance. 1.3.4 Leading Zeros The commands to the EEPROM are 9-Bit or 25-Bit wide. This does not fit into the 8-Bit pattern of the UART synchronous mode. In the code example below a workaround for this is used. Because the EEPROM ignores leading Zero-Bits, the first byte is used to adjust the bit stream, using leading Zeros. Example: A WE (Write enable) command is sent to the EEPROM, which is composed of the following bit stream: 1 0011 XXXX. These are 9 Bits. The UART sends the following bit stream instead: 0000 0001 0011 XXXX. The blue 0s are the leading Zeros, which are ignored by the EEPROM. This bit stream is represented by the bytes 0x01 and 0x3X (MSB first). 1.4 Used EEPROM Commands The following EEPROM commands are used in the code example below: Instr. Op Code Address Data PE Pin Comments READ 110 A5-A0 X Read data stored in memory WE 100 11XXXX 1 Enable programming (write enable) WRITE 101 A5-A0 D15-D0 1 Write data to address WDS 100 00XXXX D15-D0 1 Disable programming (write disable) Commands which need the PRE-Pin are not used in the code example below. 1.5 Example Code The following code shows how to establish a communication to and from the EEPROM. MCU-A-390104-E-V12-8 - Fujitsu Microelectronics Europe GmbH-10
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.1 Initial Functions and Declarations 1.5.1.1 Flowchart InitUART InitPorts wait Set Baud rate Set clock inversion (ESCR-SCES = 1) Set clock delay (ECCR-SCDE = 1) Set Port-Register 9 to 0x00 Set Port 90, 91, 92 to Output for CS-, PE- and PRE-Pin Argument: j Do loop of 4 OPs from 0 to j Reception and Transmission enable Set Mode 2, enable SCLK, enable SOT Set MSB first Fujitsu Microelectronics Europe GmbH - 9 - MCU-A-390104-E-V12
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.1.2 C Code #define DATASIZE 64 // eeprom memory size in words (16 Bit) unsigned int data[datasize]; // data to sent to EEPROM unsigned int readbuffer[datasize]; // data received from EEPROM void InitUART(void) BGR0 = 15; // 1M Bit/s @ 16 MHz ESCR0 = 0x01; // SCES = 1 => CPOL = 0 ECCR0 = 0x10; // SCDE = 1 => CPHA = 0 SCR0 = 0x03; // reception and transmission enable SMR0 = 0x83; // Mode 2, SCLK enable, SOT enable SSR0 = 0x04; // MSB first, no interrupts void InitPorts(void) // Bit#2: CS, Bit#1: PE, Bit#0: PRE PDR9 = 0x00; // All Low DDR9 = 0x07; // CS, PE, PRE to output PDR0 = 0x00; DDR0 = 0xFF; // LED-Port on Flash-CA-100P-340 void wait(unsigned int j) volatile unsigned int i; for (i = 0; i < j; i++) #pragma asm OP OP OP OP #pragma endasm MCU-A-390104-E-V12-10 - Fujitsu Microelectronics Europe GmbH-10
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.2 Write Enable and Write Disable 1.5.2.1 Flowchart write_enable Set CS=1 (PDR92 =1) write_disable Set CS=1 (PDR92 =1) TDRE == 0? TDRE == 0? Send Start-Bit with leading zeros (TDR = 0x01) Send Start-Bit with leading zeros (TDR = 0x01) TDRE == 0? TDRE == 0? Send WE command (TDR = 0x30) Send WDS command (TDR = 0x00) Transmission end? Transmission end? CS = 0 (PDR92 = 0) CS = 0 (PDR92 = 0) Fujitsu Microelectronics Europe GmbH - 11 - MCU-A-390104-E-V12
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.2.2 C Code void write_enable(void) PDR9_P92 = 1; // CS = 1 while (SSR0_TDRE == 0); TDR0 = 0x01; while (SSR0_TDRE == 0); TDR0 = 0x30; while (ECCR0 & 0x01); while (!(ECCR0 & 0x01)); // Start-Bit (with "leading zeros") // WE command // wait for start of transmission // (or ongoing) // wait for transmission finished PDR9_P92 = 0; // CS = 0 void write_disable(void) PDR9_P92 = 1; // CS = 1 while (SSR0_TDRE == 0); TDR0 = 0x01; while (SSR0_TDRE == 0); TDR0 = 0x00; while (ECCR0 & 0x01); while (!(ECCR0 & 0x01)); // Start-Bit (with "leading zeros") // WDS command // wait for start of transmission // (or ongoing) // wait for transmission finished PDR9_P92 = 0; // CS = 0 ote, that the expression(eccr & 0x01) masks the Transmission Bus Idle Bit (TBI). MCU-A-390104-E-V12-12 - Fujitsu Microelectronics Europe GmbH-10
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.3 Write to EEPROM 1.5.3.1 Flowchart Write_eeprom CS = 1 (P92 = 1) TDRE == 0? Send LSB Send Start-Bit with leading zeros (TDRE = 0x01) Calculate Command/Address-Byte Transmission end? CS = 0 (P92 = 0) Wait some ms TDRE == 0? CS = 1 (P92 = 1) Send Command/Address- Byte Busy? (SIOP == 1)? TDRE == 0? Send MSB TDRE == 0? Ready? (SIOP == 0)? CS = 0 (P92 = 0) Fujitsu Microelectronics Europe GmbH - 13 - MCU-A-390104-E-V12
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.3.2 C Code void write_eeprom(unsigned char adr) unsigned char dout, command; PDR9_P92 = 1; // CS = 1 while (SSR0_TDRE == 0); TDR0 = 0x01; // Start-Bit (with "leading zeros") command = (adr & 0x3F) 0x40; // Address and Write-Instruction dout = command; while (SSR0_TDRE == 0); TDR0 = dout; dout = (data[adr] >> 8) & 0xFF; // MSB while (SSR0_TDRE == 0); TDR0 = dout; dout = data[adr] & 0xFF; while (SSR0_TDRE == 0); TDR0 = dout; while (ECCR0 & 0x01); while (!(ECCR0 & 0x01)); // LSB // wait for start of transmission // (or ongoing) // wait for transmission finished PDR9_P92 = 0; // CS = 0 wait(1); // ext function (waiting for busy release) is made by // polling. Please note, that for the M93CS46 EEPROM the // wait time can take till 10 ms! I. e. the CPU is then // also busy. For fast application a timer should be used, // which generates an interrupt after 10 ms from here, // so that the CPU can perform other jobs in this time. PDR9_P92 = 1; // CS = 1 while(escr0_siop == 1); // wait for eeprom busy while(escr0_siop == 0); // wait for eeprom busy release PDR9_P92 = 0; // CS = 0 ote, that the expression(eccr & 0x01) masks the Transmission Bus Idle Bit (TBI). MCU-A-390104-E-V12-14 - Fujitsu Microelectronics Europe GmbH-10
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.4 Read from EEPROM 1.5.4.1 Flowchart Read_eeprom CS = 1 (P92 = 1) Send Start-Bit with leading zeros (TDRE = 0x01) Calculate Command/Address- Byte Clear possible reception errors (CRE = 1) Disable Clock delay for reception (SCDE = 0) Send dummy data to produce SCLK RDRF == 0? RDRF == 0? Flush Reception Register Read MSB Send Command/Address- Byte Send dummy data to produce SCLK RDRF == 0? RDRF == 0? Flush Reception Register Read LSB Re-enable Clock delay (SCDE = 1) CS = 0 (P92 = 0) Fujitsu Microelectronics Europe GmbH - 15 - MCU-A-390104-E-V12
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.4.2 C Code void read_eeprom(unsigned char adr) unsigned char din, command, dout; PDR9_P92 = 1; // CS = 1 TDR0 = 0x01; // Start-Bit (with "leading zeros") command = (adr & 0x3F) 0x80; // Address and Write-Instruction dout = command; while (SSR0_RDRF == 0); // transmission finished (via // reception)? din = RDR0; // flush reception register TDR0 = dout; while (SSR0_RDRF == 0); din = RDR0; SCR0_CRE = 1; // transmission finished (via // reception)? // flush reception register // Clear possible errors, reset // reception state machine // OTE: Make sure, that SCK is "0" while setting SCDE to "0" // (ECCR0 = 0x00;) // In this case (1M bps) no check is needed. Be careful with // slower baud rates! ECCR0 = 0x00; TDR0 = 0x00; // SCDE = 0 => CPHA = 1 : eeded // for special read timing of // used EEPROM (may be not // necessary for other EEPROMs) // set dummy byte to produce SCLK while (SSR0_RDRF == 0); din = RDR0; readbuffer[adr] = (din << 8); while (SSR0_TDRE == 0); TDR0 = 0x00; // transmission finished (via // reception)? // MSB // set dummy byte to produce SCLK while (SSR0_RDRF == 0); din = RDR0; // LSB readbuffer[adr] = (readbuffer[adr] din); ECCR0 = 0x10; // SCDE = 1 => CPHA = 0 : Set back // for write timing PDR9_P92 = 0; // CS = 0 ote: Because reception is enabled and the UART is synchronous master, the UART always receives (dummy) data from SI. Therefore the Reception Data Register Full Flag (RDRF) can be used to determine transmission end, because UART generates the serial clock during (dummy) transmission. This is an alternative to using the Transmission Bus Idle Flag (TBI). MCU-A-390104-E-V12-16 - Fujitsu Microelectronics Europe GmbH-10
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.5 Example of usage of EEPROM functions in Main Function 1.5.5.1 Flowchart main Create data (not in example) Flush Reception Register PE = 1 (P91 = 1) Call write_enable Clear possible Reception Errors Write data: Call write_eeprom Read data: Call read_eeprom All data written? All data read? READ PE = 0 (P91 = 0) WRITE Fujitsu Microelectronics Europe GmbH - 17 - MCU-A-390104-E-V12
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 1 M93CS46 1.5.5.2 C Code void main(void) unsigned char i, error, dummy; InitPorts(); InitUART(); // Initialize PRE, PE, and CS // Init UART for SPI communication // Put code or function call here to produce data for EEPROM // write data PDR9_P91 = 1; write_enable(); for (i = 0; i < DATASIZE; i++) write_eeprom(i); write_disable(); PDR9_P91 = 0; // Do something else... // PE = 1 must be set here // PE = 0 must be set here // read data dummy = RDR0; // flush read buffer SCR0_CRE = 1; // clear possible overruns for (i = 0; i < DATASIZE; i++) read_eeprom(i); // Add further code here... The code above writes to/reads from all 64 16-Bit-Words of the EEPROM. The functions void write_eeprom(unsigned char address) and void read_eeprom(unsigned char address) can also be used separately for single address access. MCU-A-390104-E-V12-18 - Fujitsu Microelectronics Europe GmbH-10
SPI COMMUICATIO TO/FROM SERIAL EEPROM Chapter 2 Bibliography 2 Bibliography THIS CHAPTER AMES THE RELATED DOCUMETS 2.1 Related Documents mcu-an-300002-e-spi Fujitsu Application ote about SPI Modes DS 93CS46 ational Semiconductor data sheet of M93CS46 Fujitsu Microelectronics Europe GmbH - 19 - MCU-A-390104-E-V12