mcu-an-390106-e-v11

Fujitsu Microelectronics Europe
Application Note
MCU-AN-390106-E-V11
F²MC-16LX FAMILY
16-BIT MICROCONTROLLER
MB90XXX/MB96XXX
SPI COMMUNICATION VIA
I/O PORT
(for AT93CS46)
APPLICATION NOTE
SPI COMMUNICATION VIA I/O PORT
Revision History
Revision History
Date
2003-06-18
2010-06-14
Issue
V1.0; MWi
V1.1; MWi; CPOL, CPHA logic corrected
This document contains 19 pages.
MCU-AN-390106-E-V11
-2-
© Fujitsu Microelectronics Europe GmbH
SPI COMMUNICATION VIA I/O PORT
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-AN-390106-E-V11
SPI COMMUNICATION VIA I/O PORT
Contents
Contents
REVISION HISTORY.............................................................................................................. 2
WARRANTY AND DISCLAIMER........................................................................................... 3
CONTENTS ............................................................................................................................ 4
0 INTRODUCTION................................................................................................................ 5
1 AT93CS46.......................................................................................................................... 6
1.1
EEPROM .................................................................................................................. 6
1.2
Connection to MB90340............................................................................................ 6
1.3
Communication Timing ............................................................................................. 7
1.3.1
Write Cycle Timing of NM93CS46 .............................................................. 7
1.3.2
Read Cycle Timing of NM93CS46 .............................................................. 7
1.3.3
EEPROM Busy Timing................................................................................ 7
1.4
Used EEPROM Commands...................................................................................... 8
1.5
Example Code .......................................................................................................... 8
1.5.1
1.5.2
1.5.3
1.5.4
1.5.5
Initial Functions and Declarations ............................................................... 9
1.5.1.1
Flowchart .................................................................................... 9
1.5.1.2
C Code........................................................................................ 9
Write Enable and Write Disable ................................................................ 10
1.5.2.1
Flowchart .................................................................................. 10
1.5.2.2
C Code...................................................................................... 10
Write to EEPROM ..................................................................................... 12
1.5.3.1
Flowchart .................................................................................. 12
1.5.3.2
C Code...................................................................................... 13
Read from EEPROM................................................................................. 15
1.5.4.1
Flowchart .................................................................................. 15
1.5.4.2
C Code...................................................................................... 16
Example of usage of EEPROM functions in Main Function ...................... 17
1.5.5.1
Flowchart .................................................................................. 17
1.5.5.2
C Code...................................................................................... 18
2 BIBLIOGRAPHY.............................................................................................................. 19
2.1
Related Documents................................................................................................. 19
MCU-AN-390106-E-V11
-4-
© Fujitsu Microelectronics Europe GmbH
SPI COMMUNICATION VIA I/O PORT
Introduction
0 Introduction
This application note describes how to communicate via SPI using 16-Bit-MCU standard I/O
Ports with a serial EEPROM. In this note an AT93CS46 EEPROM from AtmelTM is used.
This example uses Port 9 of an MB90340 series MCU, but the code can easily modified to
other ports and/or other 16-Bit-MCU families.
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
optimization. Neither interrupts nor timer are used. Time critical program code is always
performed by simple port polling or wait loops.
Please also refer to the Application Note MCU-AN-300002-E-SPI for further SPI specification
and nomenclature.
© Fujitsu Microelectronics Europe GmbH
-5-
MCU-AN-390106-E-V11
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1 AT93CS46
THIS CHAPTER DESCRIBES HOW TO COMMUNICATE WITH THE NM93CS46 EEPROM
1.1
EEPROM
The AT93CS46 serial EEPROM from AtmelTM has 2048-Bit memory size, organized as 64
16-Bit-Words or 128 8-Bit-Bytes.
The AT93CS46 has the following pin-out:
•
CS 1
8 Vcc
SK 2
7 DC
DI 3
6 ORG
DO 4
5 GND
Pin names:
CS
SK
DI
DO
GND
ORG
DC
Vcc
1.2
Chip Select
Serial Data Clock
Serial Data Input
Serial Data Output
Ground
16-Bit or 8-Bit Organization
Don’t Connect
Power Supply (+ 5 volts)
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.
Please also note, that using Port 9 is just a suggestion. Any other Ports can be used instead.
MCU-AN-390106-E-V11
-6-
© Fujitsu Microelectronics Europe GmbH
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.3
Communication Timing
Detailed description of the timing and timing parameters can be found in the corresponding
datasheet of the AT93CS46 EEPROM.
1.3.1 Write Cycle Timing of NM93CS46
A write cycle (write to EEPROM) has the following bit timing (SPI-CPOL = 0, SPI-CPHA = 0):
CS
SK
DI
D7
D6
D5
D4
D3
D2
D1
D0
Data sampling of the EEPROM is performed at the rising edge of the SCK signal (Red
arrow).
1.3.2 Read Cycle Timing of NM93CS46
The read cycle has a different timing. The AT93CS46-EEPROM does not assert the first
data bit after CS goes “0”, but on the rising edge of the first serial clock:
CS
SK
DO
D7
D6
D5
D4
D3
D2
D1
D0
The red arrow denotes the port sampling time (Data read from).
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). After the busy state the pin goes to “1”.
In the code example below, this busy wait is simply performed by polling the DO port (P94)
in two steps. First step is waiting for “0”, the second is waiting for “1”.
Because the busy state can take up to 10 ms, a time critical software should use a timer for
this, to save CPU performance.
© Fujitsu Microelectronics Europe GmbH
-7-
MCU-AN-390106-E-V11
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.4
Used EEPROM Commands
The following 16-Bit EEPROM commands are used in the code example below:
Instr.
Op
Code
Address
READ
110
A5-A0
WEN
100
11XXXX
WRITE
101
A5-A0
D15-D0
Write data to address
WDS
100
00XXXX
D15-D0
Disable programming (write disable)
Data
Comments
Read data stored in memory
Enable programming (write enable)
Note, that for this16-Bit Mode the ORG pin has to be set to logical “1”.
1.5
Example Code
The following code shows how to establish a port communication to and from the EEPROM.
It is useful to define the EEPROM pin names to the port, so that the software is flexible, if
other ports should be used in a different project.
// define Names for used communication ports
#define ORG PDR9_P91
// 0 = 8-Bit Mode, 1 = 16-Bit Mode
#define CS PDR9_P92
#define SK PDR9_P93
#define DI PDR9_P94
#define DO PDR9_P95
MCU-AN-390106-E-V11
-8-
© Fujitsu Microelectronics Europe GmbH
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.5.1
Initial Functions and Declarations
1.5.1.1 Flowchart
InitPorts
wait
Set Port-Register
9 to “0x00”
Argument: j
Do loop of 4 “NOPs”
from “0” to “j”
Set Port 91, 92,
93, 95 to Output
for CS-, DI-, SK
and ORG-Pin
1.5.1.2 C Code
#define DATASIZE 64
// eeprom memory size in words (16 Bit)
unsigned int data[DATASIZE];
unsigned int readbuffer[DATASIZE];
void InitPorts(void)
{
PDR9 = 0x00;
DDR9 = 0x1F;
}
// data to sent to EEPROM
// data received from EEPROM
// All Low
// DI, CS, SK, ORG to output
void wait(unsigned int j)
{
volatile unsigned int i;
}
for (i = 0; i < j; i++)
{
#pragma asm
NOP
NOP
NOP
NOP
#pragma endasm
}
© Fujitsu Microelectronics Europe GmbH
-9-
MCU-AN-390106-E-V11
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.5.2
Write Enable and Write Disable
1.5.2.1 Flowchart
write_enable
write_disable
Set CS=1 (PDR92 =1)
Set CS=1 (PDR92 =1)
Set DI = 1 (Startbit,
PDR94 = 1),
perform Clock Pulse
(PDR93 = 1 -> 0)
Set DI = 1 (Startbit,
PDR94 = 1),
perform Clock Pulse
(PDR93 = 1 -> 0)
Send WEN command
(0x30). Shift
corresponding bits (MSB
first) to DI (Port94) and
toggle SK (Port93)
Send WDS command
(0x00). Toggle SK
(Port93) for 8 times
Set CS = 0 (PDR92 = 0)
Set CS = 0 (PDR92 = 0)
1.5.2.2 C Code
void write_enable(void)
{
unsigned char i, data;
CS = 1;
__asm("\tNOP");
DI = 1;
__asm("\tNOP");
SK = 1;
__asm("\tNOP");
SK = 0;
// Start-Bit
// clock pulse
data = 0x30;
for (i = 0; i < 8; i++)
{
if ((data & 0x80) == 0x80)
{
DI = 1;
}
else
{
DI = 0;
}
__asm("\tNOP");
SK = 1;
MCU-AN-390106-E-V11
// WEN command
// clock pulse
- 10 -
J
© Fujitsu Microelectronics Europe GmbH
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
data = data << 1;
SK = 0;
// use shift also as delay
L
}
CS = 0;
__asm("\tNOP");
}
void write_disable(void)
{
unsigned char i;
CS = 1;
__asm("\tNOP");
DI = 1;
__asm("\tNOP");
SK = 1;
__asm("\tNOP");
SK = 0;
// Start-Bit
// clock pulse
// WDS command (0x00)
DI = 0;
for (i = 0; i < 8; i++)
{
SK = 1;
// clock pulse
__asm("\tNOP");
SK = 0;
__asm("\tNOP");
}
CS = 0;
__asm("\tNOP");
}
© Fujitsu Microelectronics Europe GmbH
- 11 -
MCU-AN-390106-E-V11
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.5.3
Write to EEPROM
1.5.3.1 Flowchart
write_eeprom
Set CS = 1 (P92 = 1)
Set DI = 1 (Startbit,
PDR94 = 1), perform Clock
Pulse (PDR93 = 1 -> 0)
Send WRITE command
including address. Shift
corresponding bits (MSB
first) to DI (Port94) and
toggle SK (Port93)
Send 16-Bit data. Shift
corresponding bits (MSB
first) to DI (Port94) and
toggle SK (Port93)
Set CS = 0 (P92 = 0);
Wait some ms
Set CS = 1 (P92 = 1)
Busy ?
(DO == 1) ?
N
Y
Ready ?
(DO == 0) ?
N
Y
Set CS = 0 (P92 = 0)
MCU-AN-390106-E-V11
- 12 -
© Fujitsu Microelectronics Europe GmbH
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.5.3.2 C Code
void write_eeprom(unsigned char adr)
{
unsigned char command, i;
unsigned int dout;
CS = 1;
__asm("\tNOP");
DI = 1;
__asm("\tNOP");
SK = 1;
__asm("\tNOP");
SK = 0;
// Start-Bit
// clock pulse
command = (adr & 0x3F) | 0x40;
// Address and Write-Instr.
for (i = 0; i < 8; i++)
{
if ((command & 0x80) == 0x80)
{
DI = 1;
}
else
{
DI = 0;
}
__asm("\tNOP");
SK = 1;
command = command << 1;
SK = 0;
// clock pulse
// use shift also as delay
}
dout = data[adr];
// Write Data
for (i = 0; i < 16; i++)
{
if ((dout & 0x8000) == 0x8000)
{
DI = 1;
}
else
{
DI = 0;
}
__asm("\tNOP");
SK = 1;
// clock Pulse
dout = dout << 1;
// use shift also as delay
SK = 0;
}
CS = 0;
wait(1);
L
© Fujitsu Microelectronics Europe GmbH
- 13 -
MCU-AN-390106-E-V11
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
// Next function (waiting for busy release) is made by
//
polling. Please note, that for the AT93CS46 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.
CS = 1;
while(DO == 1);
// wait for eeprom busy
while(DO == 0);
// wait for eeprom busy release
CS = 0;
__asm("\tNOP");
L
}
MCU-AN-390106-E-V11
- 14 -
© Fujitsu Microelectronics Europe GmbH
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.5.4
Read from EEPROM
1.5.4.1 Flowchart
read_eeprom
Set CS = 1 (P92 = 1)
Set DI = 1 (Startbit,
PDR94 = 1), perform Clock
Pulse (PDR93 = 1 -> 0)
Send READ command
including address. Shift
corresponding bits (MSB
first) to DI (Port94) and
toggle SK (Port93)
Receive 16-Bit data. Shift
corresponding bits (MSB
first) from DO (Port95) and
toggle SK (Port93)
Set CS = 0 (P92 = 0);
© Fujitsu Microelectronics Europe GmbH
- 15 -
MCU-AN-390106-E-V11
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.5.4.2 C Code
void read_eeprom(unsigned char adr)
{
unsigned char command, i;
unsigned int din;
CS = 1;
__asm("\tNOP");
DI = 1;
__asm("\tNOP");
SK = 1;
__asm("\tNOP");
SK = 0;
// Start-Bit
// Clock pulse
command = (adr & 0x3F) | 0x80;
// Address and Read-Instr.
for (i = 0; i < 8; i++)
{
if ((command & 0x80) == 0x80)
{
DI = 1;
}
else
{
DI = 0;
}
__asm("\tNOP");
SK = 1;
command = command << 1;
SK = 0;
// clock pulse
// use shift also as delay
}
din = 0;
for (i = 0; i < 16; i++)
{
SK = 1;
din = din << 1;
SK = 0;
if (DO == 1)
{
din |= 0x0001;
}
}
readbuffer[adr] = din;
CS = 0;
__asm("\tNOP");
}
MCU-AN-390106-E-V11
- 16 -
© Fujitsu Microelectronics Europe GmbH
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.5.5
Example of usage of EEPROM functions in Main Function
1.5.5.1 Flowchart
main
Create data
(not in example)
ORG = 1 (P91 = 1)
Read data: Call
read_eeprom
Call write_enable
All data
read ?
Write data: Call
write_eeprom
N
READ
Y
All data
written ?
Y
N
WRITE
© Fujitsu Microelectronics Europe GmbH
- 17 -
MCU-AN-390106-E-V11
SPI COMMUNICATION VIA I/O PORT
Chapter 1 AT93CS46
1.5.5.2 C Code
void main(void)
{
unsigned char i, error, dummy;
InitPorts();
// Set Ports to correct Input/Output
// Put code or function call here to produce data for EEPROM
// write data
ORG = 1;
// Set 16-Bit Data Organization
write_enable();
for (i = 0; i < DATASIZE; i++)
{
write_eeprom(i);
}
// Complete write
write_disable();
// Do something else ...
// read data
for (i = 0; i < DATASIZE; i++)
{
read_eeprom(i);
}
// Complete read-out
// 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-AN-390106-E-V11
- 18 -
© Fujitsu Microelectronics Europe GmbH
SPI COMMUNICATION VIA I/O PORT
Chapter 2 Bibliography
2 Bibliography
THIS CHAPTER NAMES THE RELATED DOCUMENTS
2.1
Related Documents
•
MCU-AN-300002-E-SPI
Fujitsu Application Note about SPI Modes
•
DS AT93CS46
AtmelTM data sheet of AT93CS46
© Fujitsu Microelectronics Europe GmbH
- 19 -
MCU-AN-390106-E-V11