Fujitsu Microelectronics Europe Application ote MCU-A-390104-E-V11 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 2003-04-26 V1.0; MWi 2003-04-29 V1.1: MWi; Flowcharts added Issue This document contains 19 pages. MCU-A-390104-E-V11-2 - Fujitsu Microelectronics Europe GmbH-10
SPI COMMUICATIO TO/FROM SERIAL EEPROM Warranty and Disclaimer Warranty and Disclaimer To the maximum extent permitted by applicable law, Fujitsu Microelectronics Europe GmbH restricts its warranties and its liability for all products delivered free of charge (eg. software include or header files, application examples, target boards, evaluation boards, engineering samples of IC s etc.), its performance and any consequential damages, on the use of the Product in accordance with (i) the terms of the License Agreement and the Sale and Purchase Agreement under which agreements the Product has been delivered, (ii) the technical descriptions and (iii) all accompanying written materials. In addition, to the maximum extent permitted by applicable law, Fujitsu Microelectronics Europe GmbH disclaims all warranties and liabilities for the performance of the Product and any consequential damages in cases of unauthorised decompiling and/or reverse engineering and/or disassembling. ote, all these products are intended and must only be used in an evaluation laboratory environment. 1. Fujitsu Microelectronics Europe GmbH warrants that the Product will perform substantially in accordance with the accompanying written materials for a period of 90 days form the date of receipt by the customer. Concerning the hardware components of the Product, Fujitsu Microelectronics Europe GmbH warrants that the Product 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. 2. Should a Product turn out to be defect, Fujitsu Microelectronics Europe GmbH s entire liability and the customer s exclusive remedy shall be, at Fujitsu Microelectronics Europe GmbH s sole discretion, either return of the purchase price and the license fee, or replacement of the Product or parts thereof, if the Product is returned to Fujitsu Microelectronics Europe GmbH 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 Fujitsu Microelectronics Europe GmbH, or abuse or misapplication attributable to the customer or any other third party not relating to Fujitsu Microelectronics Europe GmbH. 3. To the maximum extent permitted by applicable law Fujitsu Microelectronics Europe GmbH disclaims all other warranties, whether expressed or implied, in particular, but not limited to, warranties of merchantability and fitness for a particular purpose for which the Product is not designated. 4. To the maximum extent permitted by applicable law, Fujitsu Microelectronics Europe GmbH s and its suppliers liability is restricted to intention and gross negligence. O LIABILIT FOR COSEQUETIAL DAMAGES To the maximum extent permitted by applicable law, in no event shall Fujitsu Microelectronics Europe GmbH and its suppliers be liable for any damages whatsoever (including but without limitation, consequential and/or indirect damages for personal injury, assets of substantial value, loss of profits, interruption of business operation, loss of information, or any other monetary or pecuniary loss) arising from the use of the Product. Should one of the above stipulations be or become invalid and/or unenforceable, the remaining stipulations shall stay in full effect Fujitsu Microelectronics Europe GmbH - 3 - MCU-A-390104-E-V11
SPI COMMUICATIO TO/FROM SERIAL EEPROM Contents Contents REVISIO HISTOR... 2 WARRAT AD DISCLAIMER... 3 COTETS... 4 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-V11-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-V11
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-V11-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 = 1, SPI-CPHA = 1): 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) is disabled (ECCR-SCDE = 0). The workaround, which is presented here, is to switch the SPI-CPHA = 0 (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 = 1 (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-V11
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-V11-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-V11
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 = 1 ECCR0 = 0x10; // SCDE = 1 => CPHA = 1 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-V11-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-V11
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-V11-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-V11
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-V11-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-V11
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 = 0 : 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 = 1 : 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-V11-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-V11
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-V11-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-V11