You are on page 1of 105

Application Note

78K0S/Kx1+
8-Bit Single-Chip Microcontrollers
TM
EEPROM Emulation

µPD78F9200 µPD78F9500
µPD78F9201 µPD78F9501
µPD78F9202 µPD78F9502
µPD78F9210 µPD78F9510
µPD78F9211 µPD78F9511
µPD78F9212 µPD78F9512
µPD78F9221
µPD78F9222
µPD78F9232
µPD78F9234

Document No. U17379EJ2V0AN00 (2nd edition)


Date Published November 2007 NS

2005
Printed in Japan
[MEMO]

2 Application Note U17379EJ2V0AN


NOTES FOR CMOS DEVICES

1 VOLTAGE APPLICATION WAVEFORM AT INPUT PIN


Waveform distortion due to input noise or a reflected wave may cause malfunction. If the input of the
CMOS device stays in the area between VIL (MAX) and VIH (MIN) due to noise, etc., the device may
malfunction. Take care to prevent chattering noise from entering the device when the input level is fixed,
and also in the transition period when the input level passes through the area between VIL (MAX) and
VIH (MIN).

2 HANDLING OF UNUSED INPUT PINS


Unconnected CMOS device inputs can be cause of malfunction. If an input pin is unconnected, it is
possible that an internal input level may be generated due to noise, etc., causing malfunction. CMOS
devices behave differently than Bipolar or NMOS devices. Input levels of CMOS devices must be fixed
high or low by using pull-up or pull-down circuitry. Each unused pin should be connected to VDD or GND
via a resistor if there is a possibility that it will be an output pin. All handling related to unused pins must
be judged separately for each device and according to related specifications governing the device.

3 PRECAUTION AGAINST ESD


A strong electric field, when exposed to a MOS device, can cause destruction of the gate oxide and
ultimately degrade the device operation. Steps must be taken to stop generation of static electricity as
much as possible, and quickly dissipate it when it has occurred. Environmental control must be
adequate. When it is dry, a humidifier should be used. It is recommended to avoid using insulators that
easily build up static electricity. Semiconductor devices must be stored and transported in an anti-static
container, static shielding bag or conductive material. All test and measurement tools including work
benches and floors should be grounded. The operator should be grounded using a wrist strap.
Semiconductor devices must not be touched with bare hands. Similar precautions need to be taken for
PW boards with mounted semiconductor devices.

4 STATUS BEFORE INITIALIZATION


Power-on does not necessarily define the initial status of a MOS device. Immediately after the power
source is turned ON, devices with reset functions have not yet been initialized. Hence, power-on does
not guarantee output pin levels, I/O settings or contents of registers. A device is not initialized until the
reset signal is received. A reset operation must be executed immediately after power-on for devices
with reset functions.

5 POWER ON/OFF SEQUENCE


In the case of a device that uses different power supplies for the internal operation and external
interface, as a rule, switch on the external power supply after switching on the internal power supply.
When switching the power supply off, as a rule, switch off the external power supply and then the
internal power supply. Use of the reverse power on/off sequences may result in the application of an
overvoltage to the internal elements of the device, causing malfunction and degradation of internal
elements due to the passage of an abnormal current.
The correct power on/off sequence must be judged separately for each device and according to related
specifications governing the device.

6 INPUT OF SIGNAL DURING POWER OFF STATE


Do not input signals or an I/O pull-up power supply while the device is not powered. The current
injection that results from input of such a signal or I/O pull-up power supply may cause malfunction and
the abnormal current that passes in the device at this time may cause degradation of internal elements.
Input of signals during the power off state must be judged separately for each device and according to
related specifications governing the device.

Application Note U17379EJ2V0AN 3


EEPROM is a trademark of NEC Electronics Corporation.
SuperFlash is a registered trademark of Silicon Storage Technology, Inc. in several countries including the
United States and Japan.

Caution: This product uses SuperFlash® technology licensed from Silicon Storage Technology, Inc.

• The information in this document is current as of November, 2007. The information is subject to
change without notice. For actual design-in, refer to the latest publications of NEC Electronics data
sheets or data books, etc., for the most up-to-date specifications of NEC Electronics products. Not
all products and/or types are available in every country. Please check with an NEC Electronics sales
representative for availability and additional information.
• No part of this document may be copied or reproduced in any form or by any means without the prior
written consent of NEC Electronics. NEC Electronics assumes no responsibility for any errors that may
appear in this document.
• NEC Electronics does not assume any liability for infringement of patents, copyrights or other intellectual
property rights of third parties by or arising from the use of NEC Electronics products listed in this document
or any other liability arising from the use of such products. No license, express, implied or otherwise, is
granted under any patents, copyrights or other intellectual property rights of NEC Electronics or others.
• Descriptions of circuits, software and other related information in this document are provided for illustrative
purposes in semiconductor product operation and application examples. The incorporation of these
circuits, software and information in the design of a customer's equipment shall be done under the full
responsibility of the customer. NEC Electronics assumes no responsibility for any losses incurred by
customers or third parties arising from the use of these circuits, software and information.
• While NEC Electronics endeavors to enhance the quality, reliability and safety of NEC Electronics products,
customers agree and acknowledge that the possibility of defects thereof cannot be eliminated entirely. To
minimize risks of damage to property or injury (including death) to persons arising from defects in NEC
Electronics products, customers must incorporate sufficient safety measures in their design, such as
redundancy, fire-containment and anti-failure features.
• NEC Electronics products are classified into the following three quality grades: "Standard", "Special" and
"Specific".
The "Specific" quality grade applies only to NEC Electronics products developed based on a customer-
designated "quality assurance program" for a specific application. The recommended applications of an NEC
Electronics product depend on its quality grade, as indicated below. Customers must check the quality grade of
each NEC Electronics product before using it in a particular application.
"Standard": Computers, office equipment, communications equipment, test and measurement equipment, audio
and visual equipment, home electronic appliances, machine tools, personal electronic equipment
and industrial robots.
"Special": Transportation equipment (automobiles, trains, ships, etc.), traffic control systems, anti-disaster
systems, anti-crime systems, safety equipment and medical equipment (not specifically designed
for life support).
"Specific": Aircraft, aerospace equipment, submersible repeaters, nuclear reactor control systems, life
support systems and medical equipment for life support, etc.

The quality grade of NEC Electronics products is "Standard" unless otherwise expressly specified in NEC
Electronics data sheets or data books, etc. If customers wish to use NEC Electronics products in applications
not intended by NEC Electronics, they must contact an NEC Electronics sales representative in advance to
determine NEC Electronics' willingness to support a given application.

(Note)
(1) "NEC Electronics" as used in this statement means NEC Electronics Corporation and also includes its
majority-owned subsidiaries.
(2) "NEC Electronics products" means any product developed or manufactured by or for NEC Electronics (as
defined above).

M8E 02. 11-1

4 Application Note U17379EJ2V0AN


INTRODUCTION

Target readers This application note is intended for users who understand the functions of the
78K0S/Kx1+ with on-chip flash memory and who will use this product to design
application systems.

Purpose The purpose of this application note is to inform users concerning the use of the
78K0S/Kx1+ flash memory self programming functions, and the method for storing data
(writing constant data using application) during EEPROM emulation of flash memory.

Organization This manual is generally organized into the following sections.


• EEPROM emulation function
• EEPROM emulation program
• Sample program list

How to read this manual It is assumed that the reader of this manual has general knowledge in the fields of
electrical engineering, logic circuits, and microcontrollers.
† To learn more about the 78K0S/Kx1+’s hardware functions:
→ See the user’s manual of each 78K0S/Kx1+ product.
† To learn more about the 78K0S/Kx1+’s flash memory self programming functions:
→ See the flash memory chapter in the user’s manual for each 78K0S/Kx1+ product.
† To gain a general understanding of functions:
→ Read this manual in the order of the CONTENTS. The mark “<R>” shows major
revised points. The revised points can be easily searched by copying an “<R>” in
the PDF file and specifying it in the “Find what:” field.

Convention Data significance: Higher digits on the left and lower digits on the right
Active low representation: xxx (overscore over pin or signal name)
Note: Footnote for item marked with Note in the text
Caution: Information requiring particular attention
Remark: Supplementary information
Numeral representation: Binary..................xxxx or xxxxB
Decimal ...............xxxx
Hexadecimal .......xxxxH

Related Documents The related documents indicated in this publication may include preliminary versions.
However, preliminary versions are not marked as such.

Document Name Document No.

78K0/KB1+ User’s Manual U17446E


78K0/KA1+ User’s Manual U16898E
78K0/KY1+ User’s Manual U16994E
78K0/KU1+ User’s Manual U18172E
µPD78F9510, 78F9511, 78F9512 Microcontrollers User’s Manual U18372E
µPD78F9500, 78F9501, 78F9502 Microcontrollers User’s Manual U18681E
78K0S/Kx1+ EEPROM Emulation Application Note This manual
78K/0S Series Instructions User’s Manual U11047E

Application Note U17057EJ2V0AN 5


CONTENTS

CHAPTER 1 OVERVIEW OF FLASH MEMORY SELF PROGRAMMING.......................................... 8


1.1 Self-Programmable Flash Memory Area ........................................................................................ 8

CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)............ 9


2.1 Main Specifications for EEPROM Emulation................................................................................. 9
2.1.1 EEPROM emulation data block............................................................................................................ 11
2.1.2 Data structure ...................................................................................................................................... 11
2.1.3 Valid and invalid flags .......................................................................................................................... 14
2.2 EEPROM Emulation Program Execution Conditions ................................................................. 17

CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD,


ASSEMBLYLANGUAGE).................................................................................................. 18
3.1 Configuration of EEPROM Emulation Program .......................................................................... 18
3.2 Resources Used by EEPROM Emulation Program..................................................................... 18
3.3 Use of EEPROM Emulation Program ........................................................................................... 19
3.3.1 Initial values for user settings............................................................................................................... 19
3.3.2 Calling of user processing for EEPROM emulation.............................................................................. 20
3.4 Description of EEPROM Emulation Program .............................................................................. 22
3.4.1 User access processing for EEPROM emulation................................................................................. 22
3.4.2 EEPROM emulation control processing (for internal processing) ........................................................ 24
3.4.3 Flash memory control processing ........................................................................................................ 26
3.5 Flowchart of EEPROM Emulation Program................................................................................. 29
3.5.1 Flowcharts of EEPROM emulation access processings ...................................................................... 29
3.5.2 Flowcharts of EEPROM emulation control processings....................................................................... 31
3.6 List of EEPROM Emulation Processings..................................................................................... 36

<R> CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD) ..... 37

4.1 Main Specifications for EEPROM Emulation............................................................................... 37


4.1.1 EEPROM emulation data block............................................................................................................ 39
4.1.2 Data structure ...................................................................................................................................... 39
4.1.3 Valid and invalid flags .......................................................................................................................... 42
4.2 EEPROM Emulation Program Execution Conditions ................................................................. 45

<R> CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD,


ASSEMBLY LANGUAGE)................................................................................................. 46

5.1 Configuration of EEPROM Emulation Program .......................................................................... 46


5.2 Resources Used by EEPROM Emulation Program..................................................................... 46
5.3 Use of EEPROM Emulation Program ........................................................................................... 47
5.3.1 Initial values for user settings............................................................................................................... 47
5.3.2 Calling of user processing for EEPROM emulation.............................................................................. 48

6 Application Note U17379EJ2V0AN


5.4 Description of EEPROM Emulation Program.............................................................................. 50
5.4.1 User access processing for EEPROM emulation..................................................................................50
5.4.2 EEPROM emulation control processing (for internal processing) .........................................................52
5.4.3 Flash memory control processing .........................................................................................................54
5.5 Flowchart of EEPROM Emulation Program................................................................................. 57
5.5.1 Flowcharts of EEPROM emulation access processings .......................................................................57
5.5.2 Flowcharts of EEPROM emulation control processings........................................................................59
5.6 List of EEPROM Emulation Processings..................................................................................... 65

APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD) ................... 66


A.1 Main Sample Program (EEPROM.asm) (Fixed-Length Data Method, Assembly Language)... 66
A.2 Test Sample Program (Fixed-Length Single-Data Method, Assembly Language) ................. 79
A.3 Test Sample Program (Fixed-Length Single-Data Method, C Language)................................ 82

<R> APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD) .............. 84


B.1 Main Sample Program (EEPROM.asm) (Fixed-Length Multiple-Data Method, Assembly
Language) ...................................................................................................................................... 84
B.2 Test Sample Program (Fixed-Length Multiple-Data Method, Assembly Language) .............. 98
B.3 Test Sample Program (Fixed-Length Multiple-Data Method, C Language) ........................... 101

<R> APPENDIX C REVISION HISTORY ...................................................................................................... 104


C.1 Major Revisions in This Edition ................................................................................................. 104

Application Note U17379EJ2V0AN 7


CHAPTER 1 OVERVIEW OF FLASH MEMORY SELF PROGRAMMING

The 78K0S/Kx1+ enables writing from an application program to flash memory (i.e., “flash memory self
programming”).
This application note describes how to store (reading or writing as with EEPROM) any data to the flash memory by
using the self programming function.

Remark For a more detailed description of flash memory self programming, see the flash memory chapter in the
user’s manual for each 78K0S/Kx1+ products.

1.1 Self-Programmable Flash Memory Area

The area that is used for erasing, blank checks, and verification during flash memory control operations is specified
in block units. The block numbers that can be specified are listed in Figure 1-1.

Caution Any areas other than the product’s flash memory area cannot be accessed.

Figure 1-1. Allocation of Block Numbers

1F00H Block 31 (256 bytes)


1E00H Block 30 (256 bytes)
1D00H Block 29 (256 bytes)
1C00H Block 28 (256 bytes)
1B00H Block 27 (256 bytes)
1A00H Block 26 (256 bytes)
1900H Block 25 (256 bytes)
1800H Block 24 (256 bytes)
1700H Block 23 (256 bytes)
1600H Block 22 (256 bytes)
1500H Block 21 (256 bytes)
1400H Block 20 (256 bytes)
1300H Block 19 (256 bytes)
1200H Block 18 (256 bytes)
1100H Block 17 (256 bytes)
1000H Block 16 (256 bytes)
0F00H Block 15 (256 bytes) 0F00H Block 15 (256 bytes)
0E00H Block 14 (256 bytes) 0E00H Block 14 (256 bytes)
0D00H Block 13 (256 bytes) 0D00H Block 13 (256 bytes)
0C00H Block 12 (256 bytes) 0C00H Block 12 (256 bytes)
0B00H Block 11 (256 bytes) 0B00H Block 11 (256 bytes)
0A00H Block 10 (256 bytes) 0A00H Block 10 (256 bytes)
0900H Block 9 (256 bytes) 0900H Block 9 (256 bytes)
0800H Block 8 (256 bytes) 0800H Block 8 (256 bytes)
0700H Block 7 (256 bytes) 0700H Block 7 (256 bytes) 0700H Block 7 (256 bytes)
0600H Block 6 (256 bytes) 0600H Block 6 (256 bytes) 0600H Block 6 (256 bytes)
0500H Block 5 (256 bytes) 0500H Block 5 (256 bytes) 0500H Block 5 (256 bytes)
0400H Block 4 (256 bytes) 0400H Block 4 (256 bytes) 0400H Block 4 (256 bytes)
0300H Block 3 (256 bytes) 0300H Block 3 (256 bytes) 0300H Block 3 (256 bytes) 0300H Block 3 (256 bytes)
0200H Block 2 (256 bytes) 0200H Block 2 (256 bytes) 0200H Block 2 (256 bytes) 0200H Block 2 (256 bytes)
0100H Block 1 (256 bytes) 0100H Block 1 (256 bytes) 0100H Block 1 (256 bytes) 0100H Block 1 (256 bytes)
0000H Block 0 (256 bytes) 0000H Block 0 (256 bytes) 0000H Block 0 (256 bytes) 0000H Block 0 (256 bytes)
1 KB 2 KB 4 KB 8 KB

8 Application Note U17379EJ2V0AN


<R> CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)

2.1 Main Specifications for EEPROM Emulation

EEPROM emulation is a function that is used to use a portion of the flash memory as rewritable data ROM, by
using the self programming function of the flash memory. The provided sample program (see APPENDIX A SAMPLE
PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)) can be used in combination with a user program to
perform read or write processing, as with EEPROM.
Note that the data length and number of rewrites is restricted, because the internal flash memory can only be
rewritten a limited number of times. Next, the basic specifications of the provided sample program and how to
calculate the number of rewrites is described.

Basic specifications of the provided sample program and how to calculate the number of rewrites

x Data format for saving

Data (2 bytes) Delimiter (1 byte)

↑ ↑
Any data Indicates the write status. Used for data search.

Remark The data size can be set, starting from 1 byte. The upper size limit depends on the RAM size.

x Number of flash memory block rewrites


(256 − 2) / 3 = 84 times (rounded to the nearest integer)

Data size
Valid or invalid flag (see 2.1.3)
Memory size of one block

x Number of blocks to be used as the EEPROM area


Two blocks (MIN.) are used.
Remark These blocks are required to prevent data losses due to problems, such as power cut-off and power
interruption during block erasure.

x Number of erasures of one block


100 times
Remark This is 1000 times in the microcontroller specifications; however, it is 100 times for the provided sample
program, in order to reduce the EEPROM emulation control program size.

x Maximum number of rewrites


84 ¯ 2 ¯ 100 = 16,800 times

Number of block erasures


Number of blocks to be used
Number of rewrites of one block

Application Note U17379EJ2V0AN 9


CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)

In the provided sample program, data is handled in 2-byte units, and 84 rewrites can be performed per block (256
bytes), in combination with a delimiter (1 byte) that indicates the end of data. Furthermore, since an area of at least
two consecutive blocks is required to avoid losses caused, for example, by an unexpected power supply voltage drop,
a total of 168 rewrites can be performed when two blocks are used. In addition, since a block can be erased up to
100 times in the provided sample program, data can be rewritten up to 16,800 times when two blocks are used.
At least two consecutive blocks must be secured to allocate the flash memory for storing data. These blocks can
be set freely by the user.
Figure 2-1 shows a memory map and data structure example in which the program size is 3.5 KB and blocks 14
and 15 are set to be used for EEPROM emulation.

Figure 2-1. Memory Map and Data Structure Example


(When Program Size Is 3.5 KB and Blocks 14 and 15 Are Set as Data Area for Use EEPROM Emulation)

1 byte 1 byte

FFFFH Valid/invalid flag


Special function register (SFR) Not used
FF00H FFH FFH
FEFFH Valid
Internal high-speed RAM
FE00H 00H FFH
FDFFH
Invalid
Reserved 00H 00H
0F00H 0F01H
1000H
0FFFH 0FFFH
Block 15 Block 15
0F00H
0EFFH
Block 14 0F10H
0E00H 0F00H
0DFFH Block 14
Block 13
DataNote 0E10H
Block 12
Data Data Data Data DataNote 0 E 0 0 H
Block 11
Data 1 Data 2 Delimiter
Block 10
0E03H 0E04H 0E05H

User program Block 9 2 bytes 1 byte


+
EEPROM emulation
program :

Can support up to
3.5 KB total
Block 4

Block 3

Block 2

Block 1

Block 0
0000H

Note Data is stored successively.


Remark Data structure in Figure 2-1 shows the example when used the provided sample program.

10 Application Note U17379EJ2V0AN


CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)

2.1.1 EEPROM emulation data block


The EEPROM emulation program requires at least two consecutive blocks for storing data.
As long as these blocks do not overlap the user program area, they can be freely set by the user.

2.1.2 Data structure


Data that is stored as part of EEPROM emulation consists of data (1-byte unit) and a delimiter (1 byte).

Figure 2-2. Data Structure

1-byte unit 1 byte


Data Delimiter

(1) Data
Any value from 00H to FFH can be set. The size can be set, starting from 1 byte. Note, however, that, the
larger the size, the more the number of rewrites will decrease.

(2) Delimiter
The delimiter’s value is fixed as 00H. Delimiters are written to enable detection of unsuccessful data writing,
such as in cases where power interruptions or other problems occurred during a data write operation.
Whether data writing has completed normally is judged by writing a delimiter area last. If a delimiter (00H)
cannot be read correctly, it is likely that a problem will occur when writing data, so the corresponding data is
not used.
If a search finds an abnormal delimiter in the latest data, the data written before that data, having a normal
delimiter, is read as the latest data.

(3) Normal flow of data storage and search operations


The normal flow of the data storage and search operations are described below (in this example, the blocks
specified for EEPROM emulation are blocks 14 and 15).

(Status 1) Block 14 is set as a valid block.


Block 14 +0
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data FFH 0E02H

Application Note U17379EJ2V0AN 11


CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)

(Status 2) Data (11H and 22H) is written.


Block 14 +0 +1 +2
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 11H 22H 00H 0E02H
: FFH 0E06H

(Status 3) Data (22H and 33H) is written.


Block 14 +0 +1 +2
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 11H 22H 00H 0E02H
Data 22H 33H 00H 0E06H
: FFH 0E0AH

(Status 4) Data (20H and 30H) is written.


Block 14 +0 +1 +2
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 11H 22H 00H 0E02H
Data 22H 33H 00H 0E06H
Data 20H 30H 00H 0E0AH
: FFH 0E0EH

(Status 5) Data (20H and 30H) is read.


Block 14 +0 +1 +2
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data a 11H 22H 00H 0E02H
Data b 22H 33H 00H 0E06H
Data c 20H 30H 00H 0E0AH
Data d FFH 0E0EH

Erase status FFH 0EFEH

How to read
<1> Since data a has a different data number, the operation goes to the next data.
<2> Since data b has a matching data number, its delimiter is checked, and since the delimiter value is 00H
(normal), data of 2 bytes is stored as the latest data, and the operation goes to the next data.
<3> Since data c has a matching data number, its delimiter is checked, and since the delimiter value is 00H
(normal), data of 2 bytes is stored as the latest data, and the operation goes to the next data.
<4> Check whether the block has been erased to the end.
<5> The read value therefore becomes the latest stored data (data c).

12 Application Note U17379EJ2V0AN


CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)

The following describes the flow of operations when a problem such as a power interruption occurs while
storing data (in this example, the blocks specified for EEPROM emulation are blocks 14 and 15).

(Status 1) Block 14 is set as a valid block.


Block 14 +0
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data FFH 0E02H

(Status 2) Data number 1 (for data values 11H, 22H) is written.


Block 14 +0 +1 +2
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 11H 22H 00H 0E02H
: FFH 0E06H

(Status 3) Power interruption occurs while data number 1 (for data values 22H, 33H) is being written and
delimiter cannot be written correctly (value other than 00H is written)
Block 14 +0 +1 +2
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 11H 22H 00H 0E02H
Data 22H 33H 01H 0E06H
: FFH 0E0AH

(Status 4) Data number 1 is read.


Block 14 +0 +1 +2
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data a 11H 22H 00H 0E02H
Data b 22H 33H 01H 0E06H
Data c FFH 0E0AH

Erase status FFH 0EFEH

How to read
<1> Since data a has a matching data number, its delimiter is checked, and since the delimiter value is 00H
(normal), data of 2 bytes is stored as the latest data, the operation goes to the next data.
<2> Since data b has a matching data number, its delimiter is checked, and since the delimiter value is 01H
(abnormal), the operation goes to the next data.
<3> Check whether the block has been erased to the end.
<4> The read value therefore becomes the latest stored data (data a).

Application Note U17379EJ2V0AN 13


CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)

2.1.3 Valid and invalid flags


Valid and invalid flags are placed at the start of the block as a total of 2 bytes of data specified in 1-byte units. As
such, valid and invalid flags indicate the valid or invalid status of data stored in the corresponding block.
When the valid flag’s value is 00H and the invalid flag’s value is FFH, the corresponding block is valid. In all other
cases, the block is invalid.
Data is stored sequentially to a valid block, and if that block becomes full, the valid/invalid flag setting makes the
next block valid and the previously valid block invalid. In the event that the next block becomes full or a power
interruption or other problem occurs while transferring data to the next block, this procedure enables the data up to
that point to be saved in order to prevent loss of data.
The operation flow of valid and invalid flags is described below.

(Status 1) Initial status


Block n Block n + 1
Valid flag FFH Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data FFH Data FFH
: : : :
Data FFH Data FFH

(Status 2) Write 00H to valid flag for block n


Block n Block n + 1
Valid flag 00H Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data FFH Data FFH
: : : :
Data FFH Data FFH

(Status 3) Write data to block n


Block n Block n + 1
Valid flag 00H Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data Data Data FFH
: : : :
Data FFH Data FFH

(Status 4) Data is full in block n


Block n Block n + 1
Valid flag 00H Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data Data Data FFH
: : : :
Data Data Data FFH

14 Application Note U17379EJ2V0AN


CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)

(Status 5) Write latest data to block n + 1


Block n Block n + 1
Valid flag 00H Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data Data Data Data
: : : :
Data Data Data FFH

(Status 6) Write 00H to valid flag for block n + 1


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag FFH Invalid flag FFH
Data Data Data Data
: : : :
Data Data Data FFH

(Status 7) Write 00H to invalid flag for block n


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag 00H Invalid flag FFH
Data Data Data Data
: : : :
Data Data Data FFH

(Status 8) Data is full in block n + 1


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag 00H Invalid flag FFH
Data Data Data Data
: : : :
Data Data Data Data

(Status 9) Erase block n


Block n Block n + 1
Valid flag FFH Valid flag 00H
Invalid flag FFH Invalid flag FFH
Data FFH Data Data
: : : :
Data FFH Data Data

Application Note U17379EJ2V0AN 15


CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)

(Status 10) Write latest data to block n


Block n Block n + 1
Valid flag FFH Valid flag 00H
Invalid flag FFH Invalid flag FFH
Data Data Data Data
: : : :
Data FFH Data Data

(Status 11) Write 00H to valid flag for block n


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag FFH Invalid flag FFH
Data Data Data Data
: : : :
Data FFH Data Data

(Status 12) Write 00H invalid flag for block n + 1


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag FFH Invalid flag 00H
Data Data Data Data
: : : :
Data FFH Data Data

16 Application Note U17379EJ2V0AN


CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)

2.2 EEPROM Emulation Program Execution Conditions

Be sure to meet all of the conditions listed in Table 2-1 before executing the EEPROM emulation program.

Table 2-1. Conditions for EEPROM Emulation Operations

Item Description
Secure stack area During EEPROM emulation program operations, the stack used by the user program is
(Assembly language: 22 bytes) inherited and used. In addition, the stack area described on the left starting from the stack
address at the start of EEPROM emulation program execution is required. See 3.2 Resources
Used by EEPROM Emulation Program for further description of this stack.
EEPROM emulation program The area must be secured as RAM dedicated to EEPROM emulation, where read and write
RAM data are stored temporarily. Secure the area described on the left in the internal high-speed
(Assembly language: 8 bytes) RAM as a data buffer.
Operation of watchdog timer Since no instruction can be executed while flash memory control processing is being
(WDT) performed during execution of the EEPROM emulation program, flash memory control
processing clears the WDT counter. At this time, set the overflow time to 10 ms or longer so
that no overflow occurs in WDT.
Prohibit reset Do not reset this microcontroller during EEPROM emulation program operations. When a reset
occurs, any data in the flash memory being accessed becomes undefined.
Prohibit power cut-off or Be sure to apply a stable voltage to the microcontroller during EEPROM emulation program
interruption operations. When a power cut-off or interruption occurs, any data in the flash memory being
accessed becomes undefined.

Caution All interrupts are disabled during write processing of the EEPROM emulation program. After
completion of EEPROM emulation program write processing, the interrupt mask status returns to
the status before the EEPROM emulation program write processing, and interrupts are enabled.

Remark For details on the watchdog timer operation and prohibitions on power cut-off or interruption, refer to
Cautions on self programming function in the flash memory chapter in the user’s manual for each
78K0S/Kx1+ product.

Application Note U17379EJ2V0AN 17


<R> CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD,
ASSEMBLYLANGUAGE)

This is an application program that uses the self programming function of the flash memory in order to use the
flash memory as EEPROM memory for storing data, etc.

3.1 Configuration of EEPROM Emulation Program

Table 3-1 lists the files that comprise this program.

Table 3-1. File Configuration

File Name Function Type

EEPROM.asm EEPROM emulation processing Assembler


This processing includes not only read and write operations for EEPROM source
emulation but also data search and block transfer processing.

3.2 Resources Used by EEPROM Emulation Program

The resources used by this program are listed in Table 3-2 below.

Table 3-2. Resources

Resource Description

RAM RAM for EEPROM emulation 8 bytes


Stack 22 bytes
EEPROM write processing 22 bytes
EEPROM read processing 12 bytes
ROM EEPROM emulation processing 295 bytes
Flash memory control processing 173 bytes
Total 468 bytes

18 Application Note U17379EJ2V0AN


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

3.3 Use of EEPROM Emulation Program

EEPROM emulation described in this chapter uses at least two blocks of flash memory. Two bytes of data can be
referred and updated to flash memory during EEPROM emulation.
When this EEPROM emulation program is embedded in the user application, the conditions are met (see 2.2
EEPROM Emulation Program Execution Conditions), and the specified program is executed, EEPROM emulation
can be performed.
The following explains how the “fixed-length data method, assembly language” program can be used to perform
EEPROM emulations.

3.3.1 Initial values for user settings


The user must set the following items as initial values for the EEPROM emulation program.
• The first block number used as EEPROM
• The number of blocks used as EEPROM
These initial value items are included in EEPROM.asm.

(1) Block numbers used as EEPROM


Specify the block numbers to be used for EEPROM emulation. The set blocks must be consecutive for more
than 2 blocks. Set the blocks so that they do not overlap the user program area.
The number of EEPROM rewrite cycles can be increased by increasing the number of blocks used as
EEPROM. Regardless of the amount of data used for EEPROM emulation, we recommend that any area that
is not being used as a program area should be set for use in EEPROM emulation.

Example 1. When using two blocks (blocks 14 and 15) as EEPROM blocks
EEPROM_BLOCK EQU (14)

EEPROM_BLOCK_NO EQU (2)


2. When using four blocks (blocks 12, 13, 14, and 15) as EEPROM blocks
EEPROM_BLOCK EQU (12)
EEPROM_BLOCK_NO EQU (4)

(2) Data length used by user


The data length to be stored in the EEPROM must be set by the user.
Set the data length with the data size and the delimiter (1 byte), because EEPROM emulation requires a
delimiter.

Example When the data to be stored is 2 bytes


LENG EQU (3) ; Data length (including the delimiter)

(3) Number of erase retrials


The number of retrials is set in accordance with the time (MAX. value) required for the number of flash
memory block erasures performed.
In the sample program of this manual (see APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH
SINGLE-DATA METHOD)), it is set (0.4 seconds) under the conditions, TA = −10 to +85°C, 4.5 V ≤ VDD ≤ 5.5
V, and NERASE ≤ 100 times.
To set it under different conditions, set it larger than the block erasure time divided by 8.5 ms. The time for
one erase is 8.5 ms.

Application Note U17379EJ2V0AN 19


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Remark In the sample program of this manual, the number of retrials is specified as 255 times, maximum
(about 2.16 seconds). If the erase time is longer than 2.1 seconds, the SelfFlashBlockErase
function must be changed.

3.3.2 Calling of user processing for EEPROM emulation


Two types of processing are provided for use when performing EEPROM emulations in a user program: EEPROM
read and write processing.

<EEPROM read and write processing>


EEPROM read and write processing facilitates EEPROM emulation by setting and calling a specific argument of a
data address.
The assembler version and C language version of EEPROM read and write processing is included in main.asm
and main.c, respectively.
The following variable and structure (RAM) are used both for reading and writing in EEPROM emulation.

For main.asm (assembler version), use the variable EEPROM_DATA defined below.
EEPROM_DATA: DS 2 ; Data

EEPROM_DELIMITER: DS 1 ; Delimiter

For main.c (C language version), use the structure eeprom_data defined below.
Struct eeprom_data{

unsigned char uc_eeprom_data[2] ; Data

unsigned char uc_delimiter ; Delimiter

};

(1) EEPROM read processing (__eeprom_read): Reads from the EEPROM area the data of the set size.

For main.asm (assembler version)


• Argument:
Store the EEPROM_DATA address to the AX register and execute subroutine call the _eeprom_read
function.
• Return value (CY flag):
The return value is either CY=0 indicating normal completion of data read or CY=1 indicating abnormal
completion. If the result is an abnormal completion, an error will occur if data with the specified number is
not written even once.

For main.c (C language version)


• Argument:
Execute the _eeprom_read function by using the address to the eeprom_data structure as the argument.
• Return value (error flag):
The return value is either return value=0 indicating normal completion of data read or return value =1
indicating abnormal completion. If the result is an abnormal completion, an error will occur if data with the
specified number is not written even once.

20 Application Note U17379EJ2V0AN


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

(2) EEPROM write processing (EEPROMWrite): Writes to the EEPROM area the data of the set size.

For main.asm (assembler version)


• Argument:
Store the EEPROM_DATA address to the AX register and execute the _eeprom_write function after
setting the data to be written to EEPROM_DATA and the delimiter to EEPROM_DELIMITER.
• Return value (CY flag):
The return value is either CY=0 indicating normal completion of data write or CY=1 indicating abnormal
completion. If the result is an abnormal completion, an error will occur if data with the specified number is
not written even once.

For main.c (C language version)


• Argument:
Execute the _eeprom_write function by using the address to the eeprom_data structure as the argument.
• Return value (error flag):
The return value is either return value=0 indicating normal completion of data write or return value =1
indicating abnormal completion.

Application Note U17379EJ2V0AN 21


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

3.4 Description of EEPROM Emulation Program

3.4.1 User access processing for EEPROM emulation


Tables 3-3 and 3-4 list the processing that is accessed by users and used to perform read and write operations as
part of EEPROM emulation.

Table 3-3. EEPROM Read Processing

(a) Assembler version

Processing name __eeprom_read (user access function)


ROM size 29 bytes
Stack size 5 levels (10 bytes)
Input AX: Address of variable
Return value Normal completion: CY=0
Abnormal completion: CY=1
Description of The latest data at the specified address is read from the EEPROM to the storage address.
operation 1: Searches for blocks used as EEPROM.
2: Searches for address of latest data from valid blocks.
3: Reads latest data from searched addresses.

(b) C language version

Processing name _eeprom_read (user access function)


ROM size 29 bytes
Stack size 5 levels (10 bytes)
Input AX: Pointer of structure
Return value Normal completion: error flag=0
Abnormal completion: error flag=1
Description of The latest data at the specified address is read from the EEPROM to the storage address.
operation 1: Searches for blocks used as EEPROM.
2: Searches for address of latest data from valid blocks.
3: Reads latest data from searched addresses.

22 Application Note U17379EJ2V0AN


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Table 3-4. EEPROM Write Processing

(a) Assembler version

Processing name __eeprom_write (user access function)


ROM size 58 bytes
Stack size 11 levels (22 bytes)
Input AX: Address of variable
Return value Normal completion: CY=0
Abnormal completion: CY=1
Description of The data is written from to the storage address the EEPROM.
operation 1: Searches for blocks used as EEPROM.
2: Sets as valid the block specified first, if there are no valid blocks.
3: Searches for addresses of valid blocks which can be written.
4: Performs an operation to shift to the next block, if the valid blocks are full and cannot be written.
5: Creates write data.
6: Writes to valid blocks.

(b) C language version

Processing name _eeprom_write (user access function)


ROM size 58 bytes
Stack size 11 levels (10 bytes)
Input AX: Pointer of structure
Return value Normal completion: error flag=0
Abnormal completion: error flag=1
Description of The data is written from the storage address to the EEPROM.
operation 1: Searches for blocks used as EEPROM.
2: Sets as valid the block specified first, if there are no valid blocks.
3: Searches for addresses of valid blocks which can be written.
4: Performs an operation to shift to the next block, if the valid blocks are full and cannot be written.
5: Creates write data.
6: Writes to valid blocks..

Application Note U17379EJ2V0AN 23


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

3.4.2 EEPROM emulation control processing (for internal processing)


Tables 3-5 to 3-9 list the processing used to control emulation as part of EEPROM emulation.

Table 3-5. EEPROM Block Search Processing

Processing name EEPROMUseBlockSearch


ROM size 28 bytes
Stack size 2 levels (4 bytes)
Input None
Output Normal completion: CY=0, A=Block table number (01H to FEH)
Abnormal completion: CY=1, A=The next end block
Registers used A
Description of Searches for currently used blocks in flash memory allocated as EEPROM.
operation

Table 3-6. EEPROM Block Initialize Processing

Processing name EEPROMBlockInit


ROM size 17 bytes
Stack size 6 levels (12 bytes)
Input None
Output Normal completion: CY=0
Abnormal completion: CY=1
Registers used A, X, B, C, D, E
Description of If there are no valid blocks among the blocks specified for EEPROM, the first specified block is set as
operation currently being used (valid).
Returns with CY = 0 if secured normally.
Returns with CY = 1 if not secured normally.

Table 3-7. EEPROM Block Change Processing

Processing name EEPROMUseBlockChange


ROM size 59 bytes
Stack size 6 levels (12 bytes)
Input A=Currently used block number
Output Normal completion: CY=0, C=Block table number (02H to FEH), Zero flag (Z)=1
Abnormal completion: CY=1, Zero flag (Z)=0
Registers used A, X, B, C, D, E
Description of If the currently used blocks are full of data, this function searches for the next block to be used and copies
operation data to that block.
1: Sets block to be used next.
2: Erases block to be used next.
3: Transfers the latest data from a valid block to the next block.
4: Sets the next block to be used as valid.
5: Sets currently valid blocks as invalid.
6: Stores to the new block number “CurrentB_No”.

24 Application Note U17379EJ2V0AN


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Table 3-8. EEPROM Block Data Write Top Search Processing

Processing name EEPROMWriteTopSearch


ROM size 44 bytes
Stack size 3 levels (6 bytes)
Input A: Currently searched block table number
Output Successful search: CY = 0, sets the write address to AX.
Search failure: CY = 1
Registers used A, X, B, D, E
Description of Searches for specified block’s write address.
operation Completes normally only if the data area fits within the block at 0FFH.

Table 3-9. EEPROM Latest Data Search Processing

Processing name EEPROMDataSearch


ROM size 33 bytes
Stack size 3 levels (6 bytes)
Input A: Currently used block table number
Output Normal completion: CY=0, DE=Address of the latest data
Abnormal completion: CY=1, E=0
Registers used A, X, D, E
Description of Reads the storage address of the latest data.
operation

Application Note U17379EJ2V0AN 25


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

3.4.3 Flash memory control processing


Tables 3-10 to 3-17 list the processing used to control the flash memory as part of EEPROM emulation.

Table 3-10. Block Erase

Processing name SelfFlashBlockErase


ROM size 27 bytes
Stack size 3 levels (6 bytes)
Input A: Number of block to be erased
Output Normal completion: CY=0
Abnormal completion: CY=1
Registers used B
Description of Erases the specified block and performs a blank check.
operation

Table 3-11. Mode Transition Processing (from Self Programming Mode to Normal Mode)

Processing name SelfFlashModeOff


ROM size 31 bytes
Stack size 1-level (2 bytes)
Input None
Output None
Registers used A, X
Description of Releases self programming mode.
operation

Table 3-12. Mode Transition Processing (from Normal Mode to Self Programming Mode)

Processing name SelfFlashModeOn


ROM size 35 bytes
Stack size 1-level (2 bytes)
Input None
Output None
Registers used A, X
Description of Sets self programming mode.
operation

26 Application Note U17379EJ2V0AN


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Table 3-13. Block Erase Processing

Processing name FlashBlockErase


ROM size 15 bytes
Stack size 1-level (2 bytes)
Input A: Block number
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used A
Description of Erases the specified block.
operation

Table 3-14. Flash Self Programming Function Calling Processing

Processing name SubFlashSelfPrg


ROM size 12 bytes
Stack size 1-level (2 bytes)
Input None
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used A
Description of Calls flash self programming function.
operation

Table 3-15. Block Blank Check Processing

Processing name FlashBlockBlankCheck


ROM size 17 bytes
Stack size 1 level (2 bytes)
Input A: Specified block number
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used A
Description of Performs a blank check of the specified block.
operation

Table 3-16. Processing for Setting Block as Valid

Processing name SetValid


ROM size 9 bytes
Stack size 1 level (2 bytes)
Input A: Block number
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used A, X, B, C, D, E
Description of Sets as valid the block used.
operation

Application Note U17379EJ2V0AN 27


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Table 3-17. EEPROM Data Write Processing

Processing name FlashEEPROMWrite


ROM size 54 bytes
Stack size 5 levels (10 bytes)
Input DE: Write start address
C: Write data count
AX: Write data storage address
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used D, E
Description of Writes data to EEPROM and internally verifies the data.
operation

28 Application Note U17379EJ2V0AN


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

3.5 Flowchart of EEPROM Emulation Program

3.5.1 Flowcharts of EEPROM emulation access processings


Figures 3-1 and 3-2 show flowcharts of access processings called by users to perform read or write operations as
part of EEPROM emulation.

Figure 3-1. Flowchart of EEPROM Read Processing

[Overview]
The data defined with the structure is read from specified storage address.

__eeprom_read

Searches for currently used blocks


(EEPROMUseBlockSearch)

No
Block found?

Yes
Searches for latest data
(EEPROMDataSearch)

Data found? No

Yes
Store latest data
to specified address

CY = 0 CY = 1
(read successful) (read failure)

RET

Application Note U17379EJ2V0AN 29


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Figure 3-2. Flowchart of EEPROM Write Processing

[Overview]
The data of the specified number is written to a valid block from the storage address.

__eeprom_write

Shift to flash mode


(SelfFlashModeOn)

Search for latest block


(EEPROMUseBlockSearch)

Yes
Block found?

No
Specify first valid block Search writable area
(EEPROMBlockInit) (EEPROMWriteTopSearch)

Set smallest block number


to valid block
Yes
Area found?
Write data to EEPROM area
No
(FlashEEPROMWrite)
Change block because there is
no free area
(EEPROMUseBlockChange)

Block change No
successful?
A

Yes
Write successful? Yes

A No
Release flash mode Release flash mode
(SelfFlashModeOff) (SelfFlashModeOff)

CY = 1 CY = 0
(write failure ) (write successful )

RET

30 Application Note U17379EJ2V0AN


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

3.5.2 Flowcharts of EEPROM emulation control processings


Figures 3-3 to 3-7 show flowcharts of emulation control processings used during EEPROM emulation.

Figure 3-3. Flowchart of Currently Used EEPROM Block Search Function

[Overview]
The currently used blocks of the flash memory that is allocated as EEPROM is searched.

EEPROMUseBlockSearch

Set start block number

Check valid or invalid flag of block

Yes
Valid block checked?

No
Store valid block number
Move to next block
to CurentB_No

No
All set blocks completed?

Yes
CY = 1 CY = 0
(search failure) (search successful)

RET

Application Note U17379EJ2V0AN 31


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Figure 3-4. Flowchart of EEPROM Block Initialize Processing

[Overview]
If there are no valid blocks among the blocks specified for EEPROM, the first specified block is set as valid.

EEPROMBlockInit

Set start block number

Erase specified block


(SelfFlashBlockErase)

No
Erased?

Yes
Set block as valid
(Setvalid)

No
Valid flag set?

Yes
CY = 0 CY = 1
(initialize successful ) (initialize failure)

Initialize completion

32 Application Note U17379EJ2V0AN


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Figure 3-5. Flowchart of EEPROM Use Block Change Processing

[Overview]
If the currently used blocks are full of data, this function searches for the next block to be used and copies data to new
block.

EEPROMUseBlockChange

Set block number of next block

Erase next block to be used


(SelfFlashBlockErase)

No
Erased?

Yes
Write next block's data
Set start address

Write updated data to


start address of next block
(FlashEEPROMWrite)

No
Write successful?

Yes
Set valid flag to new block
(Setvalid)

No
Is valid flag normal?

Yes
Set invalid flag to old block
(Setinvalid)

No
Is invalid flag normal?

Yes
CY = 0 CY = 1
(update successful) (update failure)

RET

Application Note U17379EJ2V0AN 33


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Figure 3-6. Flowchart of EEPROM Use Block Data Write Top Search Processing

[Overview]
Searches for specified block’s write area.
Completes normally only if the data area fits within the block at 0xFFH.

EEPROMWriteTopSearch

Set for specified block’s


start address.

Delimiter≠ FFH
Delimiter checked?

Delimiter = FFH
Check data erasure of
write area

No
Is data FF?

Yes
CY = 0
(Search successful)

RET

Update address to be searched

No
End of block?

Yes
CY = 1
(Search failure)

RET

34 Application Note U17379EJ2V0AN


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

Figure 3-7. Flowchart of EEPROM Latest Data Search Processing

[Overview]
Reads the storage address of the latest data.

EEPROMDataSearch

Set for specified block’s


start address.

Set address of data to be


checked next

Block exceeded Yes


by next address?

No

Delimiter ≠ 00H
Delimiter checked?

Delimiter = 00H
Copy address to DE register
(return value)

Update address to be searched

No
Search successful?

Yes
CY = 0 CY = 1
(Search successful) (Search failure)

RET

Application Note U17379EJ2V0AN 35


CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)

3.6 List of EEPROM Emulation Processings

A call tree of EEPROM emulation processings is shown below.

Figure 3-8. Call Tree

User access processings EEPROM emulation processings Flash memory control processings

__eerom_read EEPROMUseBlockSearch

EEPROMDataSearch

__eeprom_write SelfFlashModeOn

SelfFlashModeOff

EEPROMUseBlockSearch

EEPROMBlockInit SelfFlashBlockErase FlashBlockBlankCheck

FlashBlockErase

FlashEEPROMWrite

EEPROMWriteTopSearch

EEPROMUseBlockChange SelfFlashBlockErase FlashBlockBlankCheck

FlashBlockErase

FlashEEPROMWrite

SetValid

SetInvalid
EEPROMDataSearch

FlashEEPROMWrite

36 Application Note U17379EJ2V0AN


<R> CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)

4.1 Main Specifications for EEPROM Emulation

EEPROM emulation is a function that is used to use a portion of the flash memory as rewritable data ROM, by
using the self programming function of the flash memory. The provided sample program (see APPENDIX B SAMPLE
PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)) can be used in combination with a user program to
perform read or write processing, as with EEPROM.
Note that the data length and number of rewrites is restricted, because the internal flash memory can only be
rewritten a limited number of times. Next, the basic specifications of the provided sample program and how to
calculate the number of rewrites is described.

Basic specifications of the provided sample program and how to calculate the number of rewrites

x Data format for saving

Data number (1 byte) Data (2 bytes) Delimiter (1 byte)


Number distinguishing the data to be saved (Two types of data numbers are used in the provided sample program.)
Remark The data size can be set, starting from 1 byte. The upper size limit depends on the RAM size.

x Number of flash memory block rewrites


(256 − 2) / 4 − 1 = 62 times (rounded to the nearest integer)

The number of data copied in the switching of block


Data size
Valid or invalid flag (see 4.1.3)
Memory size of one block

x Number of blocks to be used as the EEPROM area


Two blocks (MIN.) are used.
Remark These blocks are required to prevent data losses due to problems, such as power cut-off and power
interruption during block erasure.

x Number of erasures of one block


100 times
Remark This is 1000 times in the microcontroller specifications; however, it is 100 times for the provided sample
program, in order to reduce the EEPROM emulation control program size.

x Maximum number of rewrites


62 ¯ 2 ¯ 100 = 12,400 times

Number of block erasures


Number of blocks to be used
Number of rewrites of one block

In the provided sample program, data is handled in 2-byte units, and 62 rewrites can be performed per block (256
bytes), in combination with a delimiter (1 byte) that indicates the end of data. Furthermore, since an area of at least

Application Note U17379EJ2V0AN 37


CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)

two consecutive blocks is required to avoid losses caused, for example, by an unexpected power supply voltage drop,
a total of 124 rewrites can be performed when two blocks are used. In addition, since a block can be erased up to
100 times in the provided sample program, data can be rewritten up to 12,400 times when two blocks are used.
If, however, the total number of bytes of all EEPROM storage data (data + delimiter) types is not sufficiently large
with respect to the storage block size (i.e., the total number of bytes of the data is larger than half the block size),
EEPROM emulation may not function correctly. In particular, the transfer of data between blocks may occur
frequently and the number of data rewrites may significantly decrease.
At least two consecutive blocks must be secured to allocate the flash memory for storing data. These blocks can
be set freely by the user.
Figure 4-1 shows a memory map and data structure example in which the user program size is 3.5 KB and blocks
14 and 15 are set to be used for EEPROM emulation.

Figure 4-1. Memory Map and Data Structure Example


(When User Program Size Is 3.5 KB and Blocks 14 and 15 Are Set as Data Area for Use EEPROM Emulation)

1 byte 1 byte

FFFFH Valid/invalid flag


Special function register (SFR) Not used
FF00H FFH FFH
FEFFH Valid
Internal high-speed RAM
FE00H 00H FFH
FDFFH
Invalid
Reserved 00H 00H
0F00H 0F01H
1000H
0FFFH 0FFFH
Block 15 Block 15
0F00H
0EFFH
Block 14 0F10H
0E00H 0F00H
0DFFH Block 14
Block 13
DataNote 0E10H
Block 12
Data Data Data DataNote 0 E 0 0 H
Block 11
Data No. Data 1 Data 2 Delimiter
Block 10
0E02H 0E03H 0E04H 0E05H

User program Block 9 1 byte 2 bytes 1 byte


+
EEPROM emulation
program :

Can support up to
3.5 KB total
Block 4

Block 3

Block 2

Block 1

Block 0
0000H

Note Data is stored successively.


Remark Data structure in Figure 2-1 shows the example when used the provided sample program.

38 Application Note U17379EJ2V0AN


CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)

4.1.1 EEPROM emulation data block


The EEPROM emulation program requires at least two consecutive blocks for storing data.
As long as these blocks do not overlap the user program area, they can be freely set by the user.

4.1.2 Data structure


Data that is stored as part of EEPROM emulation consists of a data number (1 byte), data (1-byte unit), and a
delimiter (1 byte).

Figure 4-2. Data Structure

1 byte 1-byte unit 1 byte


Data No. Data Delimiter

(1) Data number


Data numbers are used to distinguish data to be read or written. Any data number value from 00H to FEH is
valid.
The user should assign a data number to each particular type of data before using the data.
Basically, each time a write program is executed, data is written in 4-byte units, beginning from the start of the
block.
To read the latest data, search from the start of the block in 4-byte units to see whether or not the same data
number exists. If several instances of data having the same data number are found, the data that is closest to
the data end point is regarded as the latest data.
If the data number is FFH, it is judged as the data end point. After flash memory is erased, all data is
assigned the value FFH, so when FFH is read at the location of a data number, it is judged as the data end
point and the search is terminated. Accordingly, only data numbers from 00H to FEH are valid.

Remark Two types of data numbers are used in the provided sample program.

(2) Data
00H to FFH can be set by any value that is to be stored. The data size can be set, starting from 1 byte, but
the upper size limit depends on the RAM size that can be used. Note, however, that, the larger the size, the
more the number of rewrites will decrease. It is specified as 2 bytes (the data length (LENG) is defined as 4)
in the provided sample program.

(3) Delimiter
The delimiter’s value is fixed as 00H. Delimiters are written to enable detection of unsuccessful data writing,
such as in cases where power interruptions or other problems occurred during a data write operation.
Whether data writing has completed normally is judged by writing a delimiter area last. If a delimiter (00H)
cannot be read correctly, it is likely that a problem will occur when writing data, so the corresponding data is
not used.
If a search finds an abnormal delimiter in the latest data, the data written before that data (followed by the data
closest to the next end point), having a normal delimiter, is read as the latest data.

(4) Normal flow of data storage and search operations


The normal flow of the data storage and search operations are described below (in this example, the blocks
specified for EEPROM emulation are blocks 14 and 15).

Application Note U17379EJ2V0AN 39


CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)

(Status 1) Block 14 is set as a valid block.


Block 14 +0
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data FFH 0E02H

(Status 2) Data number 1 (for data values 11H and 22H) is written.
Block 14 +0 +1 +2 +3
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 01H 11H 22H 00H 0E02H
: FFH 0E06H

(Status 3) Data number 2 (for data values 22H, 33H) is written.


Block 14 +0 +1 +2 +3
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 01H 11H 22H 00H 0E02H
Data 02H 22H 33H 00H 0E06H
: FFH 0E0AH

(Status 4) Data number 2 (for data values 20H, 30H) is written.


Block 14 +0 +1 +2 +3
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 01H 11H 22H 00H 0E02H
Data 02H 22H 33H 00H 0E06H
Data 02H 20H 30H 00H 0E0AH
: FFH 0E0EH

(Status 5) Data number 2 is read.


Block 14 +0 +1 +2 +3
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data a 01H 11H 22H 00H 0E02H
Data b 02H 22H 33H 00H 0E06H
Data c 02H 20H 30H 00H 0E0AH
Data d FFH 0E0EH

<1> Since data a has a different data number, the operation goes to the next data.
<2> Since data b has a matching data number, its delimiter is checked, and since the delimiter value is 00H
(normal), data of 2 bytes is stored as the latest data, and the operation goes to the next data.
<3> Since data c has a matching data number, its delimiter is checked, and since the delimiter value is 00H
(normal), data of 2 bytes is stored as the latest data, and the operation goes to the next data.
<4> The data number for data d is FFH (end point), so the read operation is terminated.
<5> The read value therefore becomes the latest stored data (data c).

40 Application Note U17379EJ2V0AN


CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)

The following describes the flow of operations when a problem such as a power interruption occurs while
storing data (in this example, the blocks specified for EEPROM emulation are blocks 14 and 15).

(Status 1) Block 14 is set as a valid block.


Block 14 +0
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data FFH 0E02H

(Status 2) Data number 1 (for data values 11H, 22H) is written.


Block 14 +0 +1 +2 +3
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 01H 11H 22H 00H 0E02H
: FFH 0E06H

(Status 3) Power interruption occurs while data number 1 (for data values 22H, 33H) is being written and
delimiter cannot be written correctly (value other than 00H is written)
Block 14 +0 +1 +2 +3
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 01H 11H 22H 00H 0E02H
Data 01H 22H 33H 01H 0E06H
: FFH 0E0AH

(Status 4) Data number 1 is read.


Block 14 +0 +1 +2 +3
Valid flag 00H 0E00H
Invalid flag FFH 0E01H
Data 01H 11H 22H 00H 0E02H
Data 01H 22H 33H 01H 0E06H
: FFH 0E0AH

How to read
<1> Since data a has a matching data number, its delimiter is checked, and since the delimiter value is 00H
(normal), data of 2 bytes is stored as the latest data, the operation goes to the next data.
<2> Since data b has a matching data number, its delimiter is checked, and since the delimiter value is 01H
(abnormal), the operation goes to the next data.
<3> The data number for data c is FFH (end point), so the read operation is terminated.
<4> The read value therefore becomes the latest stored data (data a).

Application Note U17379EJ2V0AN 41


CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)

4.1.3 Valid and invalid flags


Valid and invalid flags are placed at the start of the block as a total of 2 bytes of data specified in 1-byte units. As
such, valid and invalid flags indicate the valid or invalid status of data stored in the corresponding block.
When the valid flag’s value is 00H and the invalid flag’s value is FFH, the corresponding block is valid. In all other
cases, the block is invalid.
Data is stored sequentially to a valid block, and if that block becomes full, a search is executed to find blocks (at
least two blocks are required) for storing the subsequent data, after which data is transferred to those blocks (this data
is only the latest data for each data number). After the data transfer is completed, the valid/invalid flag setting makes
the next block valid and the previously valid block invalid. In the event that the next block becomes full or a power
interruption or other problem occurs while transferring data to the next block, this operation enables the data up to that
point to be saved in order to prevent loss of data.
The operation flow of valid and invalid flags is described below.

(Status 1) Initial status


Block n Block n + 1
Valid flag FFH Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data FFH Data FFH
: : : :
Data FFH Data FFH

(Status 2) Write 00H to valid flag for block n


Block n Block n + 1
Valid flag 00H Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data FFH Data FFH
: : : :
Data FFH Data FFH

(Status 3) Write data to block n


Block n Block n + 1
Valid flag 00H Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data Data Data FFH
: : : :
Data FFH Data FFH

(Status 4) Data is full in block n


Block n Block n + 1
Valid flag 00H Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data Data Data FFH
: : : :
Data Data Data FFH

42 Application Note U17379EJ2V0AN


CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)

(Status 5) The latest data to be updated is written after the latest data whose data number has not been updated is
transferred to block n + 1.
Block n Block n + 1
Valid flag 00H Valid flag FFH
Invalid flag FFH Invalid flag FFH
Data Data Data Data
: : : :
Data Data Data FFH

(Status 6) Write 00H to valid flag for block n + 1


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag FFH Invalid flag FFH
Data Data Data Data
: : : :
Data Data Data FFH

(Status 7) Write 00H to invalid flag for block n


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag 00H Invalid flag FFH
Data Data Data Data
: : : :
Data Data Data FFH

(Status 8) Data is full in block n + 1


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag 00H Invalid flag FFH
Data Data Data Data
: : : :
Data Data Data Data

(Status 9) Erase block n


Block n Block n + 1
Valid flag FFH Valid flag 00H
Invalid flag FFH Invalid flag FFH
Data FFH Data Data
: : : :
Data FFH Data Data

Application Note U17379EJ2V0AN 43


CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)

(Status 10) The latest data to be updated is written after the latest data whose data number has not been updated
is transferred to block n.
Block n Block n + 1
Valid flag FFH Valid flag 00H
Invalid flag FFH Invalid flag FFH
Data Data Data Data
: : : :
Data FFH Data Data

(Status 11) Write 00H to valid flag for block n


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag FFH Invalid flag FFH
Data Data Data Data
: : : :
Data FFH Data Data

(Status 12) Write 00H invalid flag for block n + 1


Block n Block n + 1
Valid flag 00H Valid flag 00H
Invalid flag FFH Invalid flag 00H
Data Data Data Data
: : : :
Data FFH Data Data

44 Application Note U17379EJ2V0AN


CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)

4.2 EEPROM Emulation Program Execution Conditions

Be sure to meet all of the conditions listed in Table 4-1 before executing the EEPROM emulation program.

Table 4-1. Conditions for EEPROM Emulation Operations

Item Description
Secure stack area During EEPROM emulation program operations, the stack used by the user program is
(Assembly language: 22 bytes) inherited and used. In addition, the stack area described on the left starting from the stack
address at the start of EEPROM emulation program execution is required. See 5.2 Resources
Used by EEPROM Emulation Program for further description of this stack.
EEPROM emulation program The area must be secured as RAM dedicated to EEPROM emulation, where read and write
RAM data are stored temporarily. Secure the area described on the left in the internal high-speed
(Assembly language: 11 bytes) RAM as a data buffer. In addition to the RAM for the program described on the left, only the
stack area is used by the EEPROM emulation program.
Operation of watchdog timer Since no instruction can be executed while flash memory control processing is being
(WDT) performed during execution of the EEPROM emulation program, flash memory control
processing clears the WDT counter. At this time, set the overflow time to 10 ms or longer so
that no overflow occurs in WDT.
Prohibit reset Do not reset this microcontroller during EEPROM emulation program operations. When a reset
occurs, any data in the flash memory being accessed becomes undefined.
Prohibit power cut-off or Be sure to apply a stable voltage to the microcontroller during EEPROM emulation program
interruption operations. When a power cut-off or interruption occurs, any data in the flash memory being
accessed becomes undefined.

Caution All interrupts are disabled during write processing of the EEPROM emulation program. After
completion of EEPROM emulation program write processing, the interrupt mask status returns to
the status before the EEPROM emulation program write processing, and interrupts are enabled.

Remark For details on the watchdog timer operation and prohibitions on power cut-off or interruption, refer to
Cautions on self programming function in the flash memory chapter in the user’s manual for each
78K0S/Kx1+ product.

Application Note U17379EJ2V0AN 45


<R> CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD,
ASSEMBLY LANGUAGE)

This is an application program that uses the self programming function of the flash memory in order to use the
flash memory as EEPROM memory for storing data, etc.

5.1 Configuration of EEPROM Emulation Program

Table 5-1 lists the files that comprise this program.

Table 5-1. File Configuration

File Name Function Type

EEPROM.asm EEPROM emulation processing Assembler


This processing includes not only read and write operations for EEPROM source
emulation but also data search and block transfer processing.

5.2 Resources Used by EEPROM Emulation Program

The resources used by this program are listed in Table 5-2 below.

Table 5-2. Resources

Resource Description

RAM RAM for EEPROM emulation 11 bytes


Stack 22 bytes
EEPROM write processing 22 bytes
EEPROM read processing 12 bytes
ROM EEPROM emulation processing 303 bytes
Flash memory control processing 173 bytes
Total 476 bytes

46 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

5.3 Use of EEPROM Emulation Program

EEPROM emulation described in this chapter uses at least two blocks of flash memory. Two bytes of data can be
referred and updated to flash memory during EEPROM emulation.
When this EEPROM emulation program is embedded in the user application, the conditions are met (see 4.2
EEPROM Emulation Program Execution Conditions), and the specified program is executed, EEPROM emulation
can be performed.
The following explains how the “fixed-length multiple-data method, assembly language” program can be used to
perform EEPROM emulations.

5.3.1 Initial values for user settings


The user must set the following items as initial values for the EEPROM emulation program.
• The first block number of blocks used as EEPROM (defined as constant EEPROM_BLOCK)
• The number of blocks used as EEPROM (defined as constant EEPROM_BLOCK_NO)
• Amount of data used by user, data length (defined as constant DATA_NO_MAX and LENG, respectively)
These initial value items are included in EEPROM.asm.

(1) The block numbers and the number of blocks used as EEPROM
Specify the block numbers of blocks to be used for EEPROM emulation. The set blocks must be consecutive
for more than 2 blocks. Set the blocks so that they do not overlap the user program area.
The number of EEPROM rewrite cycles can be increased by increasing the number of blocks used as
EEPROM. Regardless of the amount of data used for EEPROM emulation, we recommend that any area that
is not being used as a program area should be set for use in EEPROM emulation.

Example 1. When using two blocks (blocks 14 and 15) as EEPROM blocks
EEPROM_BLOCK EQU (14)

EEPROM_BLOCK_NO EQU (2)


2. When using four blocks (blocks 12, 13, 14, and 15) as EEPROM blocks
EEPROM_BLOCK EQU (12)
EEPROM_BLOCK_NO EQU (4)

(2) Amount of data and data length used by user


The amount of data and data length to be stored in the EEPROM must be set by the user.
Set the data length with the data size, data number (1 byte), and the delimiter (1 byte), because EEPROM
emulation requires data number and a delimiter.

Example When using two types of 2 bytes data


DATA_NO_MAX EQU (2) ; When two units of data (data numbers 0 and 1) are to be used

LENG EQU (4) ; Data length (size including data number and delimiter

(2 bytes, total))

(3) Number of erase retrials


The number of retrials is set in accordance with the time (MAX. value) required for the number of flash
memory block erasures performed.
In the sample program of this manual (see APPENDIX B SAMPLE PROGRAM LIST (see Appendix B FIXED-
LENGTH MULTIPLE-DATA METHOD)), it is set (0.4 seconds) under the conditions, TA = −10 to +85°C, 4.5 V
≤ VDD ≤ 5.5 V, and NERASE ≤ 100 times.

Application Note U17379EJ2V0AN 47


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

To set it under different conditions, set it larger than the block erasure time divided by 8.5 ms. The time for
one erase is 8.5 ms.

Remark In the sample program of this manual, the number of retrials is specified as 255 times, maximum
(about 2.16 seconds). If the erase time is longer than 2.1 seconds, the SelfFlashBlockErase
function must be changed.

5.3.2 Calling of user processing for EEPROM emulation


Two types of processing are provided for use when performing EEPROM emulations in a user program: EEPROM
read and write processing.

<EEPROM read and write processing>


EEPROM read and write processing facilitates EEPROM emulation by calling address of variable or structure as
specific argument.
The assembler version and C language version of EEPROM read and write processing is included in main.asm
and main.c, respectively.
The following variable and structure (RAM) are used both for reading and writing in EEPROM emulation.

For main.asm (assembler version), use the variable EEPROM_DATA defined below.
EEPROM_DO: DS 1 ; Data number

EEPROM_DATA: DS 2 ; Data

EEPROM_DELIMITER: DS 1 ; Delimiter

For main.c (C language version), use the structure eeprom_data defined below.
Struct eeprom_data{

unsigned char uc_data_no ; Data number

unsigned char uc_eeprom_data[2] ; Data

unsigned char uc_delimiter ; Delimiter

};

(1) EEPROM read processing (__eeprom_read): Reads from the EEPROM area the data of the set size.

For main.asm (assembler version)


• Argument:
Store the EEPROM_NO address to the AX register and call the __eeprom_read function as a subroutine,
after setting to EEPROM_NO the data number of the data to be read.
• Return value (CY flag):
The return value is either CY=0 indicating normal completion of data read or CY=1 indicating abnormal
completion. If the result is an abnormal completion, be sure to check whether or not the data number is
within the set range. An error will occur if data with the specified number is not written even once.

For main.c (C language version)


• Argument:
Execute the _eeprom_read function by using the address to the eeprom_data structure as the argument,
after setting to uc_data_no of eeprom_data structure the data number of the data to be read.

48 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

• Return value (error flag):


The return value is either return value=0 indicating normal completion of data read or return value =1
indicating abnormal completion. If the result is an abnormal completion, be sure to check whether or not
the data number is within the set range. An error will occur if data with the specified number is not written
even once.

(2) EEPROM write processing (__eeprom_write): Writes to the EEPROM area the data of the set size.

For main.asm (assembler version)


• Argument:
Store the EEPROM_NO address to the AX register and execute the __eeprom_write function, after
setting the data number of the data to be written, data, and delimiter to EEPROM_NO, EEPROM_DATA,
and EEPROM_DELIMITER, respectively.
• Return value (CY flag):
The return value is either CY=0 indicating normal completion of data write or CY=1 indicating abnormal
completion. If the result is an abnormal completion, be sure to check whether or not the data number is
within the set range. An error will occur if data with the specified number is not written even once.

For main.c (C language version)


• Argument:
Store the EEPROM_NO address to the AX register and execute the __eeprom_write function, after
setting the data number of the data to be written, data, and delimiter to uc_data_no eeprom_data
structure, uc_eeprom_data, and uc_delimiter, respectively.
• Return value (error flag):
The return value is either return value=0 indicating normal completion of data write or return value =1
indicating abnormal completion.

Application Note U17379EJ2V0AN 49


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

5.4 Description of EEPROM Emulation Program

5.4.1 User access processing for EEPROM emulation


Tables 5-3 and 5-4 list the processing that is accessed by users and used to perform read and write operations as
part of EEPROM emulation.

Table 5-3. EEPROM Read Processing

(a) Assembler version

Processing name __eeprom_read (user access function)


ROM size 31 bytes
Stack size 6 levels (12 bytes)
Input AX: Address of variable
Return value Normal completion: CY=0
Abnormal completion: CY=1
Description of The latest data at the specified data number is read from the EEPROM to the storage address.
operation 1: Searches for blocks used as EEPROM.
2: Searches for address of latest data from valid blocks.
3: Reads latest data from searched addresses.

(b) C language version

Processing name _eeprom_read (user access function)


ROM size 31 bytes
Stack size 6 levels (12 bytes)
Input AX: Pointer of structure
Return value Normal completion: error flag=0
Abnormal completion: error flag=1
Description of The latest data at the specified data number is read from the EEPROM to the storage address.
operation 1: Searches for blocks used as EEPROM.
2: Searches for address of latest data from valid blocks.
3: Reads latest data from searched addresses.

50 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Table 5-4. EEPROM Write Processing

(a) Assembler version

Processing name __eeprom_write (user access function)


ROM size 63 bytes
Stack size 11 levels (22 bytes)
Input AX: Address of variable
Return value Normal completion: CY=0
Abnormal completion: CY=1
Description of The data of specified number is written from the storage address to the EEPROM.
operation 1: Searches for blocks used as EEPROM.
2: Sets as valid the block specified first, if there are no valid blocks.
3: Searches for addresses of valid blocks which can be written.
4: Performs an operation to shift to the next block, if the valid blocks are full and cannot be written.
5: Creates write data.
6: Writes to valid blocks.

(b) C language version

Processing name _eeprom_write (user access function)


ROM size 63 bytes
Stack size 11 levels (10 bytes)
Input AX: Pointer of structure
Return value Normal completion: error flag=0
Abnormal completion: error flag=1
Description of The data of specified number is written from the storage address to the EEPROM.
operation 1: Searches for blocks used as EEPROM.
2: Sets as valid the block specified first, if there are no valid blocks.
3: Searches for addresses of valid blocks which can be written.
4: Performs an operation to shift to the next block, if the valid blocks are full and cannot be written.
5: Creates write data.
6: Writes to valid blocks..

Application Note U17379EJ2V0AN 51


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

5.4.2 EEPROM emulation control processing (for internal processing)


Tables 5-5 to 5-10 list the processing used to control emulation as part of EEPROM emulation.

Table 5-5. EEPROM Parameter Acquisition Processing

Processing name getprara


ROM size 8 bytes
Stack size 1-level (2 bytes)
Input AX: pointer of structure
Output A=data number. Copies also to RQDATA_No.
HL=data address
Description of Reads from the argument (pointer) for the user to call a function, the content of its structure and acquires the
operation required parameters.

Table 5-6. EEPROM Block Search Processing

Processing name EEPROMUseBlockSearch


ROM size 27 bytes
Stack size 2-level (4 bytes)
Input None
Output Normal completion: CY=0, A=Block table number (01H to FEH)
Abnormal completion: CY=1, A=The next end block
Registers used A
Description of Searches for currently used blocks in flash memory allocated as EEPROM.
operation

Table 5-7. EEPROM Block Initialize Processing

Processing name EEPROMBlockInit


ROM size 19 bytes
Stack size 6-level (12 bytes)
Input None
Output Normal completion: CY=0
Abnormal completion: CY=1
Registers used A, X, B, C, D, E
Description of If there are no valid blocks among the blocks specified for EEPROM, the first specified block is set as
operation currently being used (valid).
Returns with CY = 0 if secured normally.
Returns with CY = 1 if not secured normally.

52 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Table 5-8. EEPROM Block Change Processing

Processing name EEPROMUseBlockChange


ROM size 88 bytes
Stack size 6 levels (12 bytes)
Input A=Currently used block number
Output Normal completion: CY=0
Abnormal completion: CY=1
Registers used A, X, B, C, D, E
Description of If the currently used blocks are full of data, this function searches for the next block to be used and copies
operation data to that block.
1: Sets block to be used next.
2: Erases block to be used next.
3: Transfers the latest data from a valid block to the next block.
4: Sets the next block to be used as valid.
5: Sets currently valid blocks as invalid.
6: Stores to the new block number “CurrentB_No”.

Table 5-9. EEPROM Block Data Write Top Search Processing

Processing name EEPROMWriteTopSearch


ROM size 26 bytes
Stack size 3 levels (6 bytes)
Input A: Currently searched block table number
Output Successful search: CY = 0, sets the write address to AX.
Search failure: CY = 1
Registers used A, AX
Description of Searches for specified block’s write address.
operation Completes normally only if the data area fits within the block at 0FFH.

Table 5-10. EEPROM Latest Data Search Processing

Processing name EEPROMDataSearch


ROM size 41 bytes
Stack size 2 levels (4 bytes)
Input A: Currently used block table number, X: Search data number 5
Output Normal completion: CY=0, DE=Address of the latest data
Abnormal completion: CY=1, E=0
Registers used A, D, E
Description of Reads the storage address of the latest data corresponding to the specified number.
operation

Application Note U17379EJ2V0AN 53


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

5.4.3 Flash memory control processing


Tables 5-11 to 5-18 list the processing used to control the flash memory as part of EEPROM emulation.

Table 5-11. Block Erase

Processing name SelfFlashBlockErase


ROM size 27 bytes
Stack size 3 levels (8 bytes)
Input A: Number of block to be erased
Output Normal completion: CY=0
Abnormal completion: CY=1
Registers used B
Description of Erases the specified block and performs a blank check.
operation

Table 5-12. Mode Transition Processing (from Self Programming Mode to Normal Mode)

Processing name SelfFlashModeOff


ROM size 31 bytes
Stack size 1 level (2 bytes)
Input None
Output None
Registers used A, X
Description of Releases self programming mode.
operation

Table 5-13. Mode Transition Processing (from Normal Mode to Self Programming Mode)

Processing name SelfFlashModeOn


ROM size 35 bytes
Stack size 1 level (2 bytes)
Input None
Output None
Registers used A, X
Description of Sets self programming mode.
operation

54 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Table 5-14. Block Erase Processing

Processing name FlashBlockErase


ROM size 15 bytes
Stack size 1 level (2 bytes)
Input A: Block number
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used A
Description of Erases the specified block.
operation

Table 5-15. Flash Self Programming Function Calling Processing

Processing name SubFlashSelfPrg


ROM size 12 bytes
Stack size 1 level (2 bytes)
Input None
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used A
Description of Calls flash self programming function.
operation

Table 5-16. Block Blank Check Processing

Processing name FlashBlockBlankCheck


ROM size 17 bytes
Stack size 1 level (2 bytes)
Input A: Specified block number
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used A
Description of Performs a blank check of the specified block.
operation

Table 5-17. Processing for Setting Block as Valid

Processing name SetValid


ROM size 9 bytes
Stack size 1 level (2 bytes)
Input A: Block number
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used A, X, C, D, E
Description of Sets as valid the block used.
operation

Application Note U17379EJ2V0AN 55


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Table 5-18. EEPROM Data Write Processing

Processing name FlashEEPROMWrite


ROM size 54 bytes
Stack size 5 levels (10 bytes)
Input DE: Write start address
C: Write data count
AX: Write data storage address
Output Normal completion: Zero flag (Z)=1
Abnormal completion: Zero flag (Z)=0
Registers used D, E
Description of Writes data to EEPROM and internally verifies the data.
operation

56 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

5.5 Flowchart of EEPROM Emulation Program

5.5.1 Flowcharts of EEPROM emulation access processings


Figures 5-1 and 5-2 show flowcharts of access processings called by users to perform read or write operations as
part of EEPROM emulation.

Figure 5-1. Flowchart of EEPROM Read Processing

[Overview]
The data defined with the structure is read from specified storage address.

__eeprom_read

Searches for currently used blocks


(EEPROMUseBlockSearch)

No
Block found?

Yes
Searches for latest data
(EEPROMDataSearch)

Data found? No

Yes
Store latest data to specified
data area of structure

CY = 0 CY = 1
(read successful) (read failure)

RET

Application Note U17379EJ2V0AN 57


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Figure 5-2. Flowchart of EEPROM Write Processing

[Overview]
The data of the specified number is written to a valid block from the storage address.

__eeprom_write

Shift to flash mode


(SelfFlashModeOn)

Search for latest block


(EEPROMUseBlockSearch)

Yes
Block found?

No
Specify first valid block Search writable area
(EEPROMBlockInit) (EEPROMWriteTopSearch)

Set smallest block number


to valid block
Yes
Area found?
Write data to EEPROM area
No
(FlashEEPROMWrite)
Change block because there is
no free area
(EEPROMUseBlockChange)

Block change No
successful?
A

Yes
Write successful? Yes

A No
Release flash mode Release flash mode
(SelfFlashModeOff) (SelfFlashModeOff)

CY = 1 CY = 0
(write failure) (write successful)

RET

58 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

5.5.2 Flowcharts of EEPROM emulation control processings


Figures 5-3 to 5-7 show flowcharts of emulation control processings used during EEPROM emulation.

Figure 5-3. Flowchart of Currently Used EEPROM Block Search Function

[Overview]
The currently used blocks of the flash memory that is allocated as EEPROM is searched.

EEPROMUseBlockSearch

Set start block number

Check valid or invalid flag of block

Yes
Valid block checked?

No
Store valid block number
Move to next block
to CurentB_No

No
All set blocks completed?

Yes
CY = 1 CY = 0
(search failure) (search successful)

RET

Application Note U17379EJ2V0AN 59


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Figure 5-4. Flowchart of EEPROM Block Initialize Processing

[Overview]
If there are no valid blocks among the blocks specified for EEPROM, the first specified block is set as valid.

EEPROMBlockInit

Set start block number

Erase specified block


(SelfFlashBlockErase)

No
Erased?

Yes
Set block as valid
(Setvalid)

No
Valid flag set?

Yes
CY = 0 CY = 1
(initialize successful) (initialize failure)

Initialize completion

60 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Figure 5-5. Flowchart of EEPROM Use Block Change Processing (1/2)

[Overview]
If the currently used blocks are full of data, this function searches for the next block to be used and copies data to new
block.

EEPROMUseBlockChange

Set block number of


next block

Erase next block to be used


(SelfFlashBlockErase)

No
Erased?

Yes
Write next block's data
Set start address
1
Initialize data number

4
Data number Yes
to be written?
No
Search latest data
(EEPROMDataSearch)

No
2 Latest data detected?

Yes

Set write data

Application Note U17379EJ2V0AN 61


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Figure 5-5. Flowchart of EEPROM Use Block Change Processing (2/2)

Write target data


to next block
(FlashEEPROMWrite)
1

No
Write successful?

2 Yes

Set data to be written


next and address

Yes
All data complete?

No
4

Set valid flag to new block


(Setvalid)

No
Is valid flag normal?

Yes
Set invalid flag to old block
(Setinvalid)

No
Is invalid flag normal?

Yes
CY = 0 CY = 1
(update successful) (update failure)

RET

62 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Figure 5-6. Flowchart of EEPROM Use Block Data Write Top Search Processing

[Overview]
Searches for specified block’s write area.
Completes normally only if the data area fits within the block at 0xFFH.

EEPROMWriteTopSearch

Set specified block’s


start address.

Check data number

No
Is data FF?

Yes
CY = 0
(Search successful)

RET

Update address to be searched

No
End of block?

Yes
CY = 1
(Search failure)

RET

Application Note U17379EJ2V0AN 63


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

Figure 5-7. Flowchart of EEPROM Latest Data Search Processing

[Overview]
Reads the storage address of the latest data.

EEPROMDataSearch

Set data number of


EEPROM area

Yes
Data number matched?

No

Delimiter ≠ 00H
Delimiter checked?

Delimiter = 00H
Copy address to DE register
(return value)

Yes
Is data FF?

No

Update address to be searched

No
Search successful ?

Yes
CY = 0 CY = 1
(Search successful) (Search failure)

RET

64 Application Note U17379EJ2V0AN


CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)

5.6 List of EEPROM Emulation Processings

A call tree of EEPROM emulation processings is shown below.

Figure 5-8. Call Tree

User access processings EEPROM emulation processings Flash memory control processings

__eerprom_read getpara

EEPROMUseBlockSearch

EEPROMDataSearch

__eeprom_write getpara
SelfFlashModeOn

SelfFlashModeOff

EEPROMUseBlockSearch

EEPROMBlockInit SelfFlashBlockErase FlashBlockBlankCheck

FlashBlockErase

FlashEEPROMWrite
EEPROMWriteTopSearch

EEPROMUseBlockChange SelfFlashBlockErase FlashBlockBlankCheck

FlashBlockErase

FlashEEPROMWrite

SetValid

EEPROMDataSearch SetInvalid

FlashEEPROMWrite

Application Note U17379EJ2V0AN 65


<R> APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

A.1 Main Sample Program (EEPROM.asm) (Fixed-Length Single-Data Method, Assembly


Language)

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; System:78K/0S EEPROM emulation program
; (fixed-length single-data method)
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PUBLIC __eeprom_write
PUBLIC __eeprom_read
;---------------------------------------------------------------------
; Option byte, protect byte
;---------------------------------------------------------------------
OPTBYTE CSEG AT 0080H
DB 10011100B
; ||||||||
; |||||||+-- INTOSC (Stop enable by software)
; |||||++--- OSCSEL1/0 (High speed Internal OSC)
; ||||+----- RMCE (Reset)
; |||+------ <1>
; |++------- DEFOSTS1/0 (Min)
; +--------- <1>

DB 11111111B ; Protected byte: Bit(6-2) = 11111B


; Protected area: None
; Unprotected area: Blocks 0 to 15
; Be sure to release the protection for blocks
; set for EEPROM emulation
;---------------------------------------------------------------------
; Block numbers of blocks used as EEPROM **User settings **
;---------------------------------------------------------------------
;ex) When using block numbers 14 and 15
EEPROM_BLOCK EQU (14) ; Setting blocks must be consecutive.
EEPROM_BLOCK_NO EQU (2) ; Number of blocks used

;---------------------------------------------------------------------
; Data length of data to be written **User settings**
;---------------------------------------------------------------------
LENG EQU (3) ; Data length (including the delimiter)

;---------------------------------------------------------------------
; Other EQU settings
;---------------------------------------------------------------------
CMD_INTERNAL_VERIFY2 EQU (00000010B) ; Internal verify 2
CMD_BLOCK_ERASE EQU (00000011B) ; Block erase
CMD_BLANK_CHECK EQU (00000100B) ; Blank check

66 Application Note U17379EJ2V0AN


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

CMD_BYTE_WRITE EQU (00000101B) ; 1-byte write


NO_ERR EQU (00000000B) ; No errors
ERR_FPRERR EQU (00000001B) ; PFCMD write error
ERR_VCERR EQU (00000010B) ; Erase/write/internal verify error
ERR_WEPRERR EQU (00000100B) ; Protect error
FALSE EQU (0FFH) ; “FALSE” result
TRUE EQU (00H) ; “TRUE” result
DATATOP EQU (2) ; Data write start address
LENGB EQU LENG-1 ; Data length − 1
NEXT_BLOCK_WADDR EQU DATATOP+LENG ; Next block write address (only single data
; is valid)
ERASE_RETRY EQU (48) ; Number of retries during erasure (time for
; 100 erasures at 4.0 to 5.5 V)
EEPROM_END_BLOCK EQU EEPROM_BLOCK+EEPROM_BLOCK_NO-1
;---------------------------------------------------------------------
; RAM settings
;---------------------------------------------------------------------
EP_RAM1 DSEG saddr
d2MaskStatus: DS 2 ; RAM for storing mask data
d2SetAdr: DS 2 ; RAM for storing data transfer destination address
; during block transition
CurentB_No: DS 1 ; RAM for storing block being used during EEPROM
; read/write processing
ci: DS 1 ; General-purpose loop counter

EEPROM_PROG CSEG
;-------------------------------------------------------------------
; Function name: __eeprom_read (user access function)
; Input: AX = Structure pointer
; Output: CY=0 (Normal completion) or CY=1 (abnormal completion)
; Summary: Reads latest data to the storage address.
; 1. Searches for blocks being used as EEPROM.
; 2. Searches for latest data address among valid blocks.
; 3. Reads latest data from searched address.
;-------------------------------------------------------------------
;bit _eeprom_read(struct_eeprom_data *)

__eeprom_read:
PUSH HL
PUSH AX

MOVW HL,AX ; Copies to HL from read data address

CALL !EEPROMUseBlockSearch ; Searches for blocks currently used as EEPROM


BC $EP_READ_FL2 ; Abnormal completion

PUSH DE ; Saves register value


CALL !EEPROMDataSearch ; Sets address of latest data of specified data number to DE

Application Note U17379EJ2V0AN 67


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

; Searched block number is input to A register by using


; EEPROMUseBlockSearch function.
BC $EP_READ_FL ; Exits if data not found

MOV ci,#LENGB ; Sets size, excluding delimiter

EP_READ_LP:
MOV A,[DE] ; Reads EEPROM data
MOV [HL],A ; Stores data
INCW DE ; Updates read address (to be performed later due to
; fixed length)
INCW HL ; Updates storage address
DBNZ ci,$EP_READ_LP ; Copies for data length
CLR1 CY ; Return value = Normal completion (TRUE)

EP_READ_FL:
POP DE
EP_READ_FL2:
POP AX
POP HL
RET

;-------------------------------------------------------------------
; Function name: __eeprom_write (user access function)
; Input: AX = Structure pointer
; Return value: CY = 0(Normal completion) or CY = 1 (abnormal completion)
; Summary: Writes data with specified number from the storage address to EEPROM.
; 1. Searches for blocks being used as EEPROM.
; 2. If there are no valid blocks, the first specified block is set as valid.
; 3. Searches for addresses to enable writing to valid block.
; 4. If the valid blocks are full and cannot be written to, the operation moves to
; the next block.
; 5. Creates write data.
; 6. Writes to valid blocks.
;-------------------------------------------------------------------
;bit _eeprom_write(struct_eeprom_data *)

__eeprom_write:
PUSH HL
PUSH AX
PUSH DE
PUSH BC
MOVW HL,AX ; Copies to HL from write data address
CALL !SelfFlashModeOn ; Sets flash mode
CALL !EEPROMUseBlockSearch ; Searches block currently used as EEPROM
BNC $EP_WRITE_4 ; Branches if currently used blocks are found
;
; Starts using new block because no currently used blocks are found

68 Application Note U17379EJ2V0AN


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

;
CALL !EEPROMBlockInit ; If no currently used blocks are found, sets block with
; smallest number specified as EEPROM as valid block
BC $EP_WRITE_FL ; Branches and completes if securing has failed

EP_WRITE_2:
MOVW AX,#EEPROM_BLOCK*100H+DATATOP
MOV CurentB_No,A ; Determines block number to be used

;
; Data write processing
;

EP_WRITE_3:
MOVW DE,AX ; Sets current write address to DE
MOVW AX,HL ; Reads structure address
XCHW AX,DE ; Sets structure address to DE
MOV C,#LENG
CALL !FlashEEPROMWrite ; Writes data to EEPROM
BZ $EP_WRITE_TR

EP_WRITE_FL:
CALL !SelfFlashModeOff ; Releases flash mode
SET1 CY ; Return value = Abnormal completion (FALSE)
BR $EP_WRITE_E

;
; If current block is valid
;
EP_WRITE_4:
CALL !EEPROMWriteTopSearch ; Searches writable area (stores write address to AX)
BNC $EP_WRITE_3 ; Goes to data writing because area was found

;
; Moves to next block because there is no free space in area
;

CALL !EEPROMUseBlockChange ; Changes used block


BC $EP_WRITE_FL ; Exits with abnormal completion
;
; Completes with normal completion
;
EP_WRITE_TR:
CALL !SelfFlashModeOff ; Releases flash mode
CLR1 CY ; Return value = Normal completion (TRUE)

EP_WRITE_E:
POP BC

Application Note U17379EJ2V0AN 69


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

POP DE
POP AX
POP HL
RET

;-------------------------------------------------------------------
; Function name: EEPROMUseBlockSearch
; Input: None
; Output :
; Normal completion: CY = 0, A = block table number (00H to FEH)
; Abnormal completion: CY = 1, A = the next end block
; Registers used: A
; Summary: Searches for currently used blocks in flash memory that are allocated to EEPROM.
;-------------------------------------------------------------------
EEPROMUseBlockSearch:
PUSH HL
MOV ci,#EEPROM_BLOCK_NO ; Number of loops = Number of EEPROM setting blocks
MOVW HL,#EEPROM_BLOCK*100H ; Sets start block number
EP_BSRC_T:
MOV A,[HL+1] ; Reads invalid flag
INC A ; 00 if invalid flag = FF
OR A,[HL] ; 00 if valid flag = 00
ADD A,#0FFH ; Sets carry if other than 00
BNC $EP_BSRC_E ; Block is valid if CY = 00
INC H ; Moves to next block if invalid
DBNZ ci,$EP_BSRC_T ; Loops until completed for number of blocks
EP_BSRC_E:
MOV A,H ; Sets block number
MOV CurentB_No,A ; Stores used block number to RAM
POP HL
RET

;-------------------------------------------------------------------
; Function name: EEPROMBlockInit
; Input: None
; Output :
; Normal completion: CY = 0
; Abnormal completion: CY = 1
; Registers used: A, X, B, C, D, E
; Summary: If none of the blocks to be used as EEPROM are valid blocks,
; the first specified block is set as currently used (valid).
; Returns with CY = 0 if secured normally
; Returns with CY = 1 if not secured
;-------------------------------------------------------------------
EEPROMBlockInit:
PUSH HL
MOV A,#EEPROM_BLOCK ; Sets block with earliest number
CALL !SelfFlashBlockErase ; Erases specified block

70 Application Note U17379EJ2V0AN


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

BC $EP_BINI_FL ; Abnormal completion


CALL !Setvalid ; Sets block as valid
BZ $EP_BINI_E ; Branches if normal completion
SET1 CY ; An error occurs if not set as valid
EP_BINI_FL:
EP_BINI_E:
POP HL
RET

;-------------------------------------------------------------------
; Function name: EEPROMUseBlockChange
; Input: X = Number of currently used block
; Output:
; Normal completion: CY = 0, Block table number (02H to FEH)
; Stores the new block number to “CurrentB_No”.
; Abnormal completion: CY = 1
; Registers used: A, X, B, C, D, E
; Summary: If the currently used blocks are full of data, this function searches for the
; next block to be used and copies data to that block.
; 1. Sets block to be used next.
; 2. Erases block to be used next.
; 3. Transfers the latest data from a valid block to the next block.
; 4. Sets the next block to be used as valid
; 5. Sets currently valid blocks as invalid.
;-------------------------------------------------------------------
EEPROMUseBlockChange:
PUSH HL
CMP A,#EEPROM_END_BLOCK ; Compares with end block
INC A ; Moves to next block if residue is found
BC $EP_BCHG_2 ; If residue is not found,
MOV A,#EEPROM_BLOCK ; select start block
;
; Erases block to be used next and prepares for use
;

EP_BCHG_2:
CALL !SelfFlashBlockErase ; Erases block to be used next
BC $EP_BCHG_E ; Exits if block cannot be erased

EP_BCHG_3:
MOV X,#DATATOP ; sets next block write start address
MOVW d2SetAdr,AX ; Saves address to parameter area

;
; Copy processing: processing for directly writing from old block to new block
;

EP_BCHG_T:

Application Note U17379EJ2V0AN 71


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

;
; Copies data to new block
;
PUSH HL ; Copies data to be written to DE register
POP DE

MOV C,#LENG ; Loops for data length


MOVW AX,d2SetAdr ; Sets address to which data is written
CALL !FlashEEPROMWrite ; Writes data to new EEPROM area
SET1 CY ; Sets error flag in advance
BNZ $EP_BCHG_E ; Exits if abnormal completion

;
; Sets new block as valid because data transfer is completed
;

MOV A,d2SetAdr+1 ; Reads block number


CALL !Setvalid ; Writes valid flag
SET1 CY ; Sets error flag
BNZ $EP_BCHG_E ; Exits if abnormal completion

;
; Sets old block as invalid
;

XCH A,CurentB_No ; Sets old block number


MOV X,#1 ; Sets invalid flag address
CALL !Setinvalid ; Sets invalid flag
SET1 CY ; Sets error flag in advance
BNZ $EP_BCHG_E ; Exits if abnormal completion
CLR1 CY ; Clears flag
;
; Returns new block number (CurentB_No) with normal completion
;
MOV A,CurentB_No ; Sets new block number
MOV X,#NEXT_BLOCK_WADDR ; Sets new write address

EP_BCHG_E:
POP HL
RET

72 Application Note U17379EJ2V0AN


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

;-------------------------------------------------------------------
; Function name: EEPROMWriteTopSearch
; Input: A = Search block table number
; Output :
; Search successful: CY = 0, sets write address to AX
; Search failure: CY = 1
; Registers used: A,X,B,D,E
; Summary: Searches area that can be written at specified block
; Completes normally only if data number area fits within block at 0FFH
;-------------------------------------------------------------------
EEPROMWriteTopSearch:
PUSH HL
MOV H,A
MOV L,#DATATOP

EP_WTSR_T:
MOV A,[HL+LENGB] ; Reads data
CMP A,#0FFH ; Is delimiter in unwritten state (FFH)?
BNZ $EP_WTSR_2 ; Checks next data if other than FF

; Checks if all data is (FFH) by taking power interruption


; into consideration
PUSH HL ; Copies current search location to DE
POP DE
MOV B,#LENGB ; Sets number of data (number of loops)

EP_WTSR_1:
MOV A,[DE] ; Reads data
CMP A,#0FFH ; Is data not written (FFH)?
BNZ $EP_WTSR_2 ; Checks next data if written data is found
INC E ; Increments check address
DBNZ B, $EP_WTSR_1 ; Is all data checked?
BR $EP_WTSR_3 ; Completes search if all data is unwritten

EP_WTSR_2:
MOV A,L ; Updates address
ADD A,#LENG
MOV L,A
BC $EP_WTSR_3 ; Exits with error if block is completed
ADD A,#LENG-1 ; Continues if rest is at least LENG
BNC $EP_WTSR_T ; Sets CY if otherwise

EP_WTSR_3:

MOVW AX,HL ; Return value = Address of search pointer


POP HL
RET

Application Note U17379EJ2V0AN 73


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

;-------------------------------------------------------------------
; Function name: EEPROMDataSearch
; Input: A = Currently used block table number
; (block number of block set for EEPROM use by user definition)
; Output:
; Normal completion: CY = 0, DE = Address of latest data
; Abnormal completion: CY = 1, E = 0
; Registers used: A, X, D, E
; Summary: Reads storage address of latest data.
;-------------------------------------------------------------------
EEPROMDataSearch:
PUSH HL
MOV E,#0 ; Sets initial value of result as error
MOV H,A ; Sets higher address of block
MOV A,#DATATOP ; Sets lower address

EP_DASR_1: ; Data number is not looked at, because of single-data fixed length
MOV L,A ; Updates data address
ADD A,#LENG ; Checks if next address exceeds block
BC $EP_DASR_E ; Completes if block is exceeded
XCH A,X ; Stores updated address to X register

MOV A,[HL+LENGB] ; Reads delimiter


CMP A,#00H ; Read data is formed if delimiter is other than “00”
BNZ $EP_DASR_2 ; With fixed-length single data, searches until end of block
; to omit write data failed during power interruption
PUSH HL ; If delimiter is valid,
POP DE ; copy address to DE
EP_DASR_2:
XCH A,X ; Returns next address to A register
BR $EP_DASR_1 ; Reads next data

EP_DASR_E:
MOV A,E ; Extracts lower address of data
CMP A,#DATATOP ; Sets CY flag if error (A = 0 if data is not found)
POP HL
RET

;-------------------------------------------------------------------
; Function name: SelfFlashBlockErase
; Input : A = Erase block number
; Output: CY = 0 (Normal completion) or CY = 1 (abnormal completion)
; Registers used: B
; Summary: Erases the specified block and perform blank check.
;-------------------------------------------------------------------
SelfFlashBlockErase:
PUSH AX
MOV X,A ; Saves block number

74 Application Note U17379EJ2V0AN


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

MOV B,#ERASE_RETRY ; Sets number of retries during erasure


BR $SF_BKER_ST ; Starts from blank check

SF_BKER_RE:
MOV A,X ; Sets erase block
CALL !FlashBlockErase ; Erase processing
BNZ $SF_BKER_01 ; Abnormal completion
MOV A,X ; Sets blank check block

SF_BKER_ST:
CALL !FlashBlockBlankCheck ; Blank check processing
BZ $SF_BKER_TR ; Blank check normally completed
SF_BKER_01:
DBNZ B,$SF_BKER_RE ; Checks retry count
SF_BKER_FL:
SET1 CY ; Return value = Abnormal completion

SF_BKER_TR:
POP AX
RET

;-------------------------------------------------------------------
; Function name: SelfFlashModeOff
; Input: None
; Output: None
; Registers used: A, X
; Summary: Releases self programming mode
;-------------------------------------------------------------------
SelfFlashModeOff:
MOV FLCMD,#0
MOV PFS,#NO_ERR
MOV PFCMD,#0A5H ; PFCMD register control
MOV FLPMC,#00H ; FLPMC register control (set value)
MOV FLPMC,#0FFH ; FLPMC register control (inverted set value)
MOV FLPMC,#00H ; Sets normal mode: FLPMC register control
; (set value)
BT PFS.0,$SelfFlashModeOff ; Checks completion of write to specific register
EI
MOVW AX,d2MaskStatus
MOV MK1,A ; Delete this line when using 78K0S/KY1+
XCH A,X
MOV MK0,A ; Restores interrupt mask flag
RET

;-------------------------------------------------------------------
; Function name: SelfFlashModeOn
; Input: None
; Output: None

Application Note U17379EJ2V0AN 75


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

; Registers used: A, X
; Summary: Processing to set self programming mode
;-------------------------------------------------------------------
SelfFlashModeOn:
MOV A,MK0
XCH A,X
MOV A,MK1 ; Delete this line when using 78K0S/KY1+
MOVW d2MaskStatus,AX ; Saves interrupt mask flag
MOV MK0,#11111111B
MOV MK1,#11111110B ; Masks interrupts except INTFLC
; Delete this line when using 78K0S/KY1+
DI
SF_MDON_LP:
MOV PFS,#NO_ERR
MOV PFCMD,#0A5H ; PFCMD register control
MOV FLPMC,#01H ; FLPMC register control (set value)
MOV FLPMC,#0FEH ; FLPMC register control (inverted set value)
MOV FLPMC,#01H ; Sets self programming mode: FLPMC register control
;(set value)
NOP
HALT
BT PFS.0,$SF_MDON_LP ; Checks completion of write to specific register
RET

;-------------------------------------------------------------------
; Function name: FlashBlockErase
; Input: A = Block number
; Output: Z (ZeroFlag) = Normal completion (1) or abnormal completion (0)
; Registers used: A
; Summary: Erases specified blocks.
;-------------------------------------------------------------------
FlashBlockErase:
MOV FLCMD,#CMD_BLOCK_ERASE ; Sets flash control command (block erase)
MOV FLAPH,A ; Sets blank check block number
MOV FLAPL,#00H
MOV FLAPHC,A
MOV FLAPLC,#00H
;
; Continue to process actual flash self programming
;
;--------------------------------------------------------------------
; Function name: SubFlashSelfPrg
; Input: None
; Output: Z (ZeroFlag) = Normal completion (1) or abnormal completion (0)
; Registers used: A
; Processing: Calling of flash self programming function
;--------------------------------------------------------------------
SubFlashSelfPrg:

76 Application Note U17379EJ2V0AN


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

MOV PFS,#NO_ERR ; Clears flash status register


MOV WDTE,#0ACH ; Clears and restarts WDT
HALT ; Start self programming
MOV A,PFS
CMP A,#NO_ERR
RET

;-------------------------------------------------------------------
; Function name: FlashBlockBlankCheck
; Input: A = Block number
; Output: Z(ZeroFlag) = Normal completion (1) or abnormal completion (0)
; Registers used: A
; Summary: Performs blank check of specified blocks.
;-------------------------------------------------------------------
FlashBlockBlankCheck:
MOV FLCMD,#CMD_BLANK_CHECK ; Sets flash control command (block blank check)
MOV FLAPH,A ; Sets blank check block number
MOV FLAPL,#00H
MOV FLAPHC,A
MOV FLAPLC,#0FFH
BR $SubFlashSelfPrg

;-------------------------------------------------------------------
; Function name: Setvalid
; Input: A = Target block number
; Output: Z (ZeroFlag) = Normal completion (1) or abnormal completion (0)
; Registers used: A, X, C, D, E
; Summary: Sets used block as valid
;-------------------------------------------------------------------
Setvalid:
MOV X,#0 ; AX indicates address of valid flag
Setinvalid:
MOVW DE,#Setinvalid-1 ; Indicates 0 as data to be written
MOV C,#1 ; Write data is 1 byte
;
; The following proceeds to write function
;

;-------------------------------------------------------------------
; Function name: FlashEEPROMWrite
; Input: DE = Write start address
; C = Number of data blocks to be written
; AX = Write data storage address
; Output: Z(ZeroFlag) = Normal completion (1) or abnormal completion (0)
; Registers used: D, E
; Summary: Writes data to EEPROM and internally verifies the data.
;-------------------------------------------------------------------
FlashEEPROMWrite:

Application Note U17379EJ2V0AN 77


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

PUSH HL
PUSH AX
PUSH BC
MOVW HL,AX
MOV FLCMD,#CMD_BYTE_WRITE ; Sets flash control command (byte write)
FL_WR_W1:
MOVW AX,HL
MOV FLAPH,A
XCH A,X
MOV FLAPL,A ; Sets write/verify address
MOV A,[DE]
MOV FLW,A ; Sets write data
CALL !SubFlashSelfPrg
BZ $FL_WR_W2 ; Exits if normal completion
POP BC
BR $FL_WR_E
FL_WR_W2:
INCW DE ; Read address + 1
INCW HL ; Write address + 1
DBNZ C,$FL_WR_W1 ; Loops for specified number of bytes

POP BC ; Completes writing


MOV FLCMD,#CMD_INTERNAL_VERIFY2 ; Sets flash control command (internal verify)
MOV FLAPH,A
XCH A,X
MOV FLAPL,A ; Sets verify start address
DEC C
ADD A,C
XCH A,X
ADDC A,#0
MOV FLAPHC,A
XCH A,X
MOV FLAPLC,A ; Sets verify end address
CALL !SubFlashSelfPrg
FL_WR_E:
POP AX
POP HL
RET

END

78 Application Note U17379EJ2V0AN


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

A.2 Test Sample Program (Fixed-Length Single-Data Method, Assembly Language)

;*******************************************************************************
;
; 78K0S/Kx1+ EEPROM Emulation sample main program
;
;*******************************************************************************

;===============================================================================
;
; EEPROM EXTRN declaration
;
;===============================================================================
EXTRN __eeprom_read ; EEPROM read function
EXTRN __eeprom_write ; EEPROM write function

;===============================================================================
;
; RAM definition
;
;===============================================================================
XRAM DSEG SADDR
EEPROM_DATA: DS 2 ; EEPROM data
DELIMITER: DS 1 ; Delimiter

;===============================================================================
;
; Stack area securement
;
;===============================================================================
XSTK DSEG AT 0FEE0H
STACKEND:
DS 20H ; Secures 32 bytes of stack area
STACKTOP: ; Stack area start address = FF00H

;===============================================================================
;
; Vector table settings
;
;===============================================================================
XVCT CSEG AT 0000H
DW RESET_START ;(00) RESET

;*******************************************************************************
;
; Initialization processing after reset release
;
;*******************************************************************************

Application Note U17379EJ2V0AN 79


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

XMAIN CSEG UNIT


RESET_START:

;-------------------------------------------------------------------------------
; Stack pointer setting
;-------------------------------------------------------------------------------
MOVW AX, #STACKTOP
MOVW SP, AX ; Sets stack pointer

;-------------------------------------------------------------------------------
; Watchdog timer setting
;-------------------------------------------------------------------------------
MOV WDTM, #01110111B ; Stops watchdog timer operation

;-------------------------------------------------------------------------------
; Clock frequency setting
;-------------------------------------------------------------------------------
MOV PPCC, #00000000B ; Clock supplied to peripheral hardware fxp = fx (= 8MHz)
MOV PCC, #00000000B ; CPU clock fcpu = fxp (= 8MHz)
MOV LSRCM, #00000001B ; Stops low-speed internal oscillator

;*******************************************************************************
;
; Main processing
;
;*******************************************************************************
MOV DELIMITER, #00H ; Initializes delimiter

MOVW AX, #EEPROM_DATA ; Transfers EEPROM data address to HL


MOVW HL, AX

PUSH AX ; Saves EEPROM data address

MOV A, #01H ; Sets EEPROM storage data


MOV [HL], A
MOV A, #10H ; Sets EEPROM storage data
MOV [HL+1], A

POP AX ; Restores EEPROM data address

; Write test
CALL !__eeprom_write ; Calls EEPROM write function
BC $LOOP ; Jumps to LOOP if error

; Clears data for test


PUSH AX ; Saves EEPROM data address

MOV A, #00H ; Sets EEPROM storage data

80 Application Note U17379EJ2V0AN


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

MOV [HL], A
MOV A, #00H ; Sets EEPROM storage data
MOV [HL+1], A

POP AX ; Restores EEPROM data address

; Read test
CALL !__eeprom_read ; Calls EEPROM read function
BC $LOOP ; Jumps to LOOP if error

NOP ; Executes NOP if normal completion

LOOP:
BR $LOOP ; Completes EEPROM processing

end

Application Note U17379EJ2V0AN 81


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

A.3 Test Sample Program (Fixed-Length Single-Data Method, C Language)

/****************************************************************
78K0S/Kx1+ EEPROM Emulation sample main program
****************************************************************/

#define TRUE 0x01;


#define FALSE 0x00;

/***************************************
Function declaration
***************************************/

// Assembler function
extern bit _eeprom_write(unsigned char *); // Writes to EEPROM
extern bit _eeprom_read(unsigned char *); // Reads to EEPROM

/***************************************
EEPROM data structure declaration
***************************************/
typedef struct eeprom_data{
unsigned char uc_eeprom_data[2]; // Data
unsigned char uc_delimiter; // Delimiter
} struct_eeprom_data;

/****************************************************************
Main function
****************************************************************/
void main(void)
{
/* Variable declaration */
struct_eeprom_data s_eeprom_data;
unsigned char uc_eeprom_func_result;

/* Use hwinit function to set peripheral settings */

/* Initializes EEPROM data */


s_eeprom_data.uc_eeprom_data[0] = 0x01;
s_eeprom_data.uc_eeprom_data[1] = 0x10;
s_eeprom_data.uc_delimiter = 0;

/* Writes to EEPROM */
if(_eeprom_write(&(s_eeprom_data.uc_eeprom_data[0])))
{
/* Write failure */
uc_eeprom_func_result = FALSE;
}
else

82 Application Note U17379EJ2V0AN


APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)

{
/* Write successful */
uc_eeprom_func_result = TRUE;
}

/* Reads EEPROM */
if(_eeprom_read(&(s_eeprom_data.uc_eeprom_data[0])))
{
/* Read failure */
uc_eeprom_func_result = FALSE;
}
else
{
/* Read successful */
uc_eeprom_func_result = TRUE;
}

while(1);
}

Application Note U17379EJ2V0AN 83


<R> APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

B.1 Main Sample Program (EEPROM.asm) (Fixed-Length Multiple-Data Method, Assembly


Language)

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; System:78K/0S EEPROM emulation program (fixed-length data method)
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PUBLIC __eeprom_write
PUBLIC __eeprom_read
;---------------------------------------------------------------------
; Option byte, protect byte
;---------------------------------------------------------------------
OPTBYTE CSEG AT 0080H
DB 10011100B
; ||||||||
; |||||||+-- INTOSC (Stop enable by software)
; |||||++--- OSCSEL1/0 (High speed Internal OSC)
; ||||+----- RMCE (Reset)
; |||+------ <1>
; |++------- DEFOSTS1/0 (Min)
; +--------- <1>

DB 11111111B ; Protected byte: Bit(6-2) = 11111B


; Protected area: None
; Unprotected area: Blocks 0 to 15
; Be sure to release the protection for blocks
; set for EEPROM emulation
;---------------------------------------------------------------------
; Block numbers of blocks used as EEPROM **User settings**
;---------------------------------------------------------------------
;ex) When using block numbers 14 and 15
EEPROM_BLOCK EQU (14) ; Setting blocks must be consecutive.
EEPROM_BLOCK_NO EQU (2) ; Number of blocks used

;---------------------------------------------------------------------
; The number of data to be written **User settings**
;---------------------------------------------------------------------
DATA_NO_MAX EQU (2) ;ex) When two units of data (data numbers 0 and 1) are to be
; used
LENG EQU (4) ; Data length (size including data number and delimiter
; (2 bytes, total))

;---------------------------------------------------------------------
; Other EQU settings
;---------------------------------------------------------------------
CMD_INTERNAL_VERIFY2 EQU (00000010B) ; Internal verify 2

84 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

CMD_BLOCK_ERASE EQU (00000011B) ; Block erase


CMD_BLANK_CHECK EQU (00000100B) ; Blank check
CMD_BYTE_WRITE EQU (00000101B) ; 1-byte write
NO_ERR EQU (00000000B) ; No errors
ERR_FPRERR EQU (00000001B) ; PFCMD write error
ERR_VCERR EQU (00000010B) ; Erase/write/internal verify error
ERR_WEPRERR EQU (00000100B) ; Protect error
FALSE EQU (0FFH) ; “FALSE” result
TRUE EQU (00H) ; “TRUE” result
DATATOP EQU (2) ; Data write start address
LENGB EQU LENG-1 ; Data length − 1
ERASE_RETRY EQU (48) ; Number of retries during erasure (time for
; 100 erasures at 4.0 to 5.5 V)
EEPROM_END_BLOCK EQU EEPROM_BLOCK+EEPROM_BLOCK_NO-1
;---------------------------------------------------------------------
; RAM settings
;---------------------------------------------------------------------
EP_RAM1 DSEG saddr
d2MaskStatus: DS 2 ; RAM for storing mask data
RWPointer: DS 2 ; For storing start address of structure pointer
d2SetAdr: DS 2 ; RAM for storing data transfer destination address
; during block transition
ci: DS 1 ; General-purpose loop counter
CurentB_No: DS 1 ; Number of currently used block
RQDATA_No: DS 1 ; Work area to save requested data number

EEPROM_PROG CSEG
;-------------------------------------------------------------------
; Function name: __eeprom_read (user access function)
; Input: AX = Structure pointer
; Return value: CY = 0 (Normal completion) or CY = 1 (abnormal completion)
; Summary: Reads latest data to the storage address.
; 1. Searches for blocks being used as EEPROM.
; 2. Searches for latest data address among valid blocks.
; 3. Reads latest data from searched address.
;-------------------------------------------------------------------
;bit _eeprom_read(struct_eeprom_data *)

__eeprom_read:
PUSH HL
PUSH AX
CALL !getpara ; Extracts parameter from structure
MOV X,A ; Saves data number to X in advance
CALL !EEPROMUseBlockSearch ; Searches for blocks currently used as EEPROM
BC $EP_READ_FL2 ; Branches and completes if abnormal completion
;
; Searches for A: block number and X: data number
;

Application Note U17379EJ2V0AN 85


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

PUSH DE ; Saves work register


CALL !EEPROMDataSearch ; Sets address of latest data of specified data number to DE
BC $EP_READ_FL ; Exits if data not found

MOV ci,#LENG-2
;
; Copies searched data to data area of structure
;
EP_READ_LP:
INCW DE
MOV A,[DE] ; Reads EEPROM data
MOV [HL],A ; Stores data
INCW HL ; Updates storage address
DBNZ ci,$EP_READ_LP ; Copies for data length

EP_READ_FL:
POP DE
EP_READ_FL2:
POP AX
POP HL
RET

;
;-------------------------------------------------------------------
; Function name: getpara
; Input: AX = Structure pointer
; Return value: A = data number. Copies also to RQDATA_No.
; HL = data address
; Summary: Reads from the argument (pointer) for the user to call
; a function, the content of its structure and acquires
; the required parameters.
;-------------------------------------------------------------------

getpara:
MOVW HL,AX ; Sets Structure pointer to HL
MOVW RWPointer,AX ; Copies to work area
MOV A,[HL] ; Reads data number
MOV RQDATA_No,A ; Copies to work area
INCW HL ; Sets data address to HL
RET

;-------------------------------------------------------------------
; Function name: __eeprom_write (user access function)
; Input: AX = Structure pointer
; Return value: CY = 0(Normal completion) or CY = 1 (abnormal completion)
; Summary: Writes data with specified number from the storage address to EEPROM.
; 1. Searches for blocks being used as EEPROM.
; 2. If there are no valid blocks, the first specified block is set as valid.

86 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

; 3. Searches for addresses to enable writing to valid block.


; 4. If the valid blocks are full and cannot be written to, the operation moves to
; the next block.
; 5. Creates write data.
; 6. Writes to valid blocks.
;-------------------------------------------------------------------
;bit _eeprom_write(struct_eeprom_data *)

__eeprom_write:
PUSH HL
PUSH AX
PUSH DE
PUSH BC
CALL !getpara ; Decomposes parameter from argument
CALL !SelfFlashModeOn ; Sets flash mode
CALL !EEPROMUseBlockSearch ; Searches block currently used as EEPROM
BNC $EP_WRITE_4 ; Branches if currently used blocks are found
;
; Starts using new block because no currently used blocks are found
;
CALL !EEPROMBlockInit ; If no currently used blocks are found, sets block with
; smallest number specified as EEPROM as valid block
BC $EP_WRITE_FL ; Branches and completes if securing has failed

EP_WRITE_2:
MOVW AX,#EEPROM_BLOCK*100H+DATATOP
MOV CurentB_No,A ; Determines block number to be used

;
; Data write processing
;

EP_WRITE_3:
MOVW DE,AX ; Sets current write address to DE
MOVW AX,RWPointer ; Reads structure address
XCHW AX,DE ; Sets structure address to DE
MOV C,#LENG
CALL !FlashEEPROMWrite ; Writes data to EEPROM
BZ $EP_WRITE_TR

EP_WRITE_FL:
CALL !SelfFlashModeOff ; Releases flash mode
SET1 CY ; Return value = Abnormal completion (FALSE)
BR $EP_WRITE_E

;
; If current block is valid
;

Application Note U17379EJ2V0AN 87


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

EP_WRITE_4:
CALL !EEPROMWriteTopSearch ; Searches writable area
BNC $EP_WRITE_3 ; Goes to data writing because area was found

;
; Moves to next block because there is no free space in area
;

CALL !EEPROMUseBlockChange ; Changes used block


BC $EP_WRITE_FL ; Exits with abnormal completion
;
; Completes with normal completion
;
EP_WRITE_TR:
CALL !SelfFlashModeOff ; Releases flash mode
CLR1 CY ; Return value = Normal completion (TRUE)

EP_WRITE_E:
POP BC
POP DE
POP AX
POP HL
RET

;-------------------------------------------------------------------
; Function name: EEPROMUseBlockSearch
; Input: None
; Output:
; Normal completion: CY = 0
; A, CurrentB_No = block table number (00H to FEH)
; Abnormal completion: CY = 1
; A, CurrentB_No = the next end block
; Registers used: A
; Summary: Searches for currently used blocks in flash memory
; that are allocated to EEPROM.
;-------------------------------------------------------------------
EEPROMUseBlockSearch:
PUSH HL
MOV ci,#EEPROM_BLOCK_NO ; Number of loops = Number of EEPROM setting blocks
MOVW HL,#EEPROM_BLOCK*100H ; Sets start address of block

EP_BSRC_T:
MOV A,[HL+1] ; Reads invalid flag
INC A ; 00 if invalid flag = FF
OR A,[HL] ; 00 if valid flag = 00
ADD A,#0FFH ; Sets carry if other than 00
BNC $EP_BSRC_E ; Block is valid if CY = 00
INC H ; Moves to next block if invalid

88 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

DBNZ ci,$EP_BSRC_T ; Loops until completed for number of blocks


EP_BSRC_E:
MOV A,H ; Sets block number
MOV CurentB_No,A ; Stores used block number to RAM
POP HL
RET

;-------------------------------------------------------------------
; Function name: EEPROMBlockInit
; Input: None
; Output :
; Normal completion: CY = 0
; Abnormal completion: CY = 1
; Registers used: A, X, B, C, D, E
; Summary: If none of the blocks to be used as EEPROM are valid blocks,
; the first specified block is set as currently used (valid).
; Returns with CY = 0 if secured normally
; Returns with CY = 1 if not secured
;-------------------------------------------------------------------
EEPROMBlockInit:
PUSH HL
MOV A,#EEPROM_BLOCK ; Sets block with earliest number
CALL !SelfFlashBlockErase ; Erases specified block
BC $EP_BINI_FL ; Abnormal completion
CALL !Setvalid ; Sets block as valid
BZ $EP_BINI_E ; Branches if normal completion
SET1 CY ; An error occurs if not set as valid
EP_BINI_FL:
EP_BINI_E:
POP HL
RET

;-------------------------------------------------------------------
; Function name: EEPROMUseBlockChange
; Input: X = Number of currently used block
; Output:
; Normal completion: CY = 0, Zero flag = 1, C = Block table number (02H to FEH)
; Stores to the new block number “CurrentB_No”.
; Abnormal completion: CY = 1, Zero flag = 0
; Registers used: A, X, B, C, D, E
; Summary: If the currently used blocks are full of data, this function searches for
; the next block to be used and copies data to that block.
; 1. Sets block to be used next.
; 2. Erases block to be used next.
; 3. Transfers the latest data from a valid block to the next block.
; 4. Sets the next block to be used as valid
; 5. Sets currently valid blocks as invalid.
;-------------------------------------------------------------------

Application Note U17379EJ2V0AN 89


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

EEPROMUseBlockChange:
MOV A,CurentB_No ; Reads number of currently used block
CMP A,#EEPROM_END_BLOCK ; Compares with end block
INC A ; Moves to next block if residue is found
BC $EP_BCHG_2 ; If residue is not found,
MOV A,#EEPROM_BLOCK ; select start block
;
; Erases block to be used next and prepares for use
;

EP_BCHG_2:
CALL !SelfFlashBlockErase ; Erases block to be used next
BC $EP_BCHG_E ; Exits if block cannot be erased

EP_BCHG_3:
MOV X,#DATATOP ; sets next block write start address
MOVW d2SetAdr,AX ; Saves address to parameter area

;
; Copy processing: processing for directly writing from old block to new block
;

MOV ci,#DATA_NO_MAX ; Sets repeat count


EP_BCHG_T:
;
; Searches data with old block
;
MOV A,#DATA_NO_MAX
SUB A,ci ; Calculates target data number
MOV X,A ; Sets data number
CMP A,RQDATA_No ; Is data number same as requested data number?
BNZ $EP_BCHG_5 ; If same as write data,
MOVW AX,RWPointer ; reads structure pointer
MOVW DE,AX ; and sets data number to DE register
BR $EP_BCHG_6

EP_BCHG_5: ; Otherwise, searches data


MOV A,CurentB_No ; Sets block number
CALL !EEPROMDataSearch ; Searches address of latest data number
; and sets address of original data to DE
BC $EP_BCHG_8 ; Branches if no data is found

;
; Copies data to new block
;
EP_BCHG_6:
MOV C,#LENG ; Loops for data length
MOVW AX,d2SetAdr ; Sets address to which data is written

90 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

CALL !FlashEEPROMWrite ; Writes data to new EEPROM area


SET1 CY ; Sets error flag in advance
BNZ $EP_BCHG_E ; Exits if abnormal completion

;
; Updates to next write address when writing is completed
;
EP_BCHG_7:
MOV A,d2SetAdr ; Sets write address + data length
ADD A,#LENG ; as new write address
MOV d2SetAdr,A ; to write next data

EP_BCHG_8:
DBNZ ci,$EP_BCHG_T ; Repeats until largest data number

;
; Sets new block as valid because data transfer is completed
;

EP_BCHG_9:
MOV A,d2SetAdr+1 ; Reads block number
CALL !Setvalid ; Writes valid flag
SET1 CY ; Sets error flag
BNZ $EP_BCHG_E ; Exits if abnormal completion

;
; Sets old block as invalid
;

MOV A,CurentB_No ; Sets old block number


MOV X,#1 ; Sets invalid flag address
CALL !Setinvalid ; Sets invalid flag
ADD A,#0FFH ; Sets CY if as above

EP_BCHG_E:
RET

;-------------------------------------------------------------------
; Function name: EEPROMWriteTopSearch
; Input: A = Search block table number
; Output :
; Search successful: CY = 0, sets write address to AX
; Search failure: CY = 1
; Registers used: A,X
; Summary: Searches area that can be written at specified block
; Completes normally only if data number area fits within block at 0FFH
;-------------------------------------------------------------------
EEPROMWriteTopSearch:

Application Note U17379EJ2V0AN 91


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

PUSH HL
MOV H,A
MOV L,#DATATOP

EP_WTSR_T:
MOV A,[HL] ; Reads data
CMP A,#0FFH ; Is delimiter in unwritten state (FFH)?
BZ $EP_WTSR_1 ; Exits with search completion
MOV A,L ; Updates address
ADD A,#LENG
MOV L,A
BC $EP_WTSR_1 ; Exits with error if block is completed
ADD A,#LENG-1 ; Continues if rest is at least LENG
BNC $EP_WTSR_T ; Sets CY if otherwise

EP_WTSR_1:

MOVW AX,HL ; Return value = Address of search pointer


POP HL
RET

;-------------------------------------------------------------------
; Function name: EEPROMDataSearch
; Input: A = Currently used block table number
; X = Search data number
; (block number of block set for EEPROM use by user definition)
; Output:
; Normal completion: CY = 0, DE = Address of latest data
; Abnormal completion: CY = 1, E = 0
; Registers used: A, D, E
; Summary: Reads storage address of latest data.
;-------------------------------------------------------------------
EEPROMDataSearch:
PUSH HL
MOV E,#0 ; Sets initial value of result as error
MOV H,A ; Sets higher address of block
MOV L,#DATATOP ; Sets lower address
;
; Compares data number. And if it has a matching data number, checks its delimiter
;
EP_DASR_T:
MOV A,[HL] ; Reads data number
CMP A,X ; Compares with search number
BNZ $EP_DASR_2 ; Branches if search number does not match
MOV A,[HL+LENG-1] ; Reads delimiter
CMP A,#00H
BNZ $EP_DASR_1 ; For shortening program,
PUSH HL ; if delimiter is valid,

92 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

POP DE ; copies address to DE


EP_DASR_1:
MOV A,[HL]
;
; Searches with next address until block end for whether free block can be found
;
EP_DASR_2:
CMP A,#0FFH ; Identifies whether data end
BZ $EP_DASR_E ; Exits if data end
MOV A,L ; Updates address
ADD A,#LENG
MOV L,A
BNC $EP_DASR_T ; Repeats within block
;
; Block search is completed
;
EP_DASR_E:
MOV A,E ; Extracts lower address of data
CMP A,#DATATOP ; Sets CY flag if error
POP HL
RET

;-------------------------------------------------------------------
; Function name: SelfFlashBlockErase
; Input : A = Erase block number
; Output: CY = 0 (Normal completion) or CY = 1 (abnormal completion)
; Registers used: B
; Summary: Erases the specified block and perform blank check.
;-------------------------------------------------------------------
SelfFlashBlockErase:
PUSH AX
MOV X,A ; Saves block number
MOV B,#ERASE_RETRY ; Sets number of retries during erasure
BR $SF_BKER_ST ; Starts from blank check

SF_BKER_RE:
MOV A,X ; Sets erase block
CALL !FlashBlockErase ; Erase processing
BNZ $SF_BKER_01 ; Abnormal completion
MOV A,X ; Sets blank check block

SF_BKER_ST:
CALL !FlashBlockBlankCheck ; Blank check processing
BZ $SF_BKER_TR ; Blank check normally completed
SF_BKER_01:
DBNZ B,$SF_BKER_RE ; Checks retry count
SF_BKER_FL:
SET1 CY ; Return value = Abnormal completion

Application Note U17379EJ2V0AN 93


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

SF_BKER_TR:
POP AX
RET

;-------------------------------------------------------------------
; Function name: SelfFlashModeOff
; Input: None
; Output: None
; Registers used: A, X
; Summary: Processing to release self programming mode
;-------------------------------------------------------------------
SelfFlashModeOff:
MOV FLCMD,#0
MOV PFS,#NO_ERR
MOV PFCMD,#0A5H ; PFCMD register control
MOV FLPMC,#00H ; FLPMC register control (set value)
MOV FLPMC,#0FFH ; FLPMC register control (inverted set value)
MOV FLPMC,#00H ; Sets normal mode: FLPMC register control (set
; value)
BT PFS.0,$SelfFlashModeOff ; Checks completion of write to specific register
EI
MOVW AX,d2MaskStatus
MOV MK1,A ; Delete this line when using 78K0S/KY1+
XCH A,X
MOV MK0,A ; Restores interrupt mask flag
RET

;-------------------------------------------------------------------
; Function name: SelfFlashModeOn
; Input: None
; Output: None
; Registers used: A, X
; Summary: Processing to set self programming mode
;-------------------------------------------------------------------
SelfFlashModeOn:
MOV A,MK0
XCH A,X
MOV A,MK1 ; Delete this line when using 78K0S/KY1+
MOVW d2MaskStatus,AX ; Saves interrupt mask flag
MOV MK0,#11111111B
MOV MK1,#11111110B ; Masks interrupts except INTFLC
; Delete this line when using 78K0S/KY1+
DI
SF_MDON_LP:
MOV PFS,#NO_ERR
MOV PFCMD,#0A5H ; PFCMD register control
MOV FLPMC,#01H ; FLPMC register control (set value)

94 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

MOV FLPMC,#0FEH ; FLPMC register control (inverted set value)


MOV FLPMC,#01H ; Sets self programming mode: FLPMC register control
; (set value)

NOP
HALT
BT PFS.0,$SF_MDON_LP ; Checks completion of write to specific register
RET

;-------------------------------------------------------------------
; Function name: FlashBlockErase
; Input: A = Block number
; Output: Z (ZeroFlag) = Normal completion (1) or abnormal completion (0)
; Registers used: A
; Summary: Erases specified blocks.
;-------------------------------------------------------------------
FlashBlockErase:
MOV FLCMD,#CMD_BLOCK_ERASE ; Sets flash control command (block erase)
MOV FLAPH,A ; Sets blank check block number
MOV FLAPL,#00H
MOV FLAPHC,A
MOV FLAPLC,#00H
;
; Continue to process actual flash self programming
;
;--------------------------------------------------------------------
; Function name: SubFlashSelfPrg
; Input: A = Block number
; Output: Z (ZeroFlag) = Normal completion (1) or abnormal completion (0)
; Registers used: A
; Processing: Calling of flash self programming function
;--------------------------------------------------------------------
SubFlashSelfPrg:
MOV PFS,#NO_ERR ; Clears flash status register
MOV WDTE,#0ACH ; Clears and restarts WDT
HALT ; Start self programming
MOV A,PFS
CMP A,#NO_ERR
RET
;-------------------------------------------------------------------
; Function name: FlashBlockBlankCheck
; Input: A = Block number
; Output: Z(ZeroFlag) = Normal completion (1) or abnormal completion (0)
; Registers used: A
; Summary: Performs blank check of specified blocks.
;-------------------------------------------------------------------
FlashBlockBlankCheck:
MOV FLCMD,#CMD_BLANK_CHECK ; Sets flash control command (block blank check)

Application Note U17379EJ2V0AN 95


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

MOV FLAPH,A ; Sets blank check block number


MOV FLAPL,#00H
MOV FLAPHC,A
MOV FLAPLC,#0FFH
BR $SubFlashSelfPrg

;-------------------------------------------------------------------
; Function name: Setvalid
; Input: A = Target block number
; Output: Z (ZeroFlag) = Normal completion (1) or abnormal completion (0)
; Registers used: A, X, C, D, E
; Summary: Sets used block as valid
;-------------------------------------------------------------------
Setvalid:
MOV X,#0 ; AX indicates address of valid flag
Setinvalid:
MOVW DE,#Setinvalid-1 ; Indicates 0 as data to be written
MOV C,#1 ; Write data is 1 byte
;
; The following proceeds to write function
;

;-------------------------------------------------------------------
; Function name: FlashEEPROMWrite
; Input: DE = Write start address
; C = Number of data to be written
; AX = Storage address for data to be written
; Output: Z(Zero Flag) = Normal completion (1) or abnormal completion (0)
; Registers used: D, E
; Summary: Performs write to and internal verification of EEPROM.
;-------------------------------------------------------------------
FlashEEPROMWrite:
PUSH HL
PUSH AX
PUSH BC
MOVW HL,AX
MOV FLCMD,#CMD_BYTE_WRITE ; Sets flash control command (byte write)
FL_WR_W1:
MOVW AX,HL
MOV FLAPH,A
XCH A,X
MOV FLAPL,A ; Sets write/verify address
MOV A,[DE]
MOV FLW,A ; Sets write data
CALL !SubFlashSelfPrg
BZ $FL_WR_W2 ; Exits if normal completion
POP BC
BR $FL_WR_E

96 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

FL_WR_W2:
INCW DE ; Read address + 1
INCW HL ; Write address + 1
DBNZ C,$FL_WR_W1 ; Loops for specified number of bytes

POP BC ; Completes writing


MOV FLCMD,#CMD_INTERNAL_VERIFY2 ; Sets flash control command (internal verify)
MOV FLAPH,A
XCH A,X
MOV FLAPL,A ; Sets verify start address
DEC C
ADD A,C
XCH A,X
ADDC A,#0
MOV FLAPHC,A
XCH A,X
MOV FLAPLC,A ; Sets verify end address
CALL !SubFlashSelfPrg
FL_WR_E:
POP AX
POP HL
RET

END

Application Note U17379EJ2V0AN 97


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

B.2 Test Sample Program (Fixed-Length Multiple-Data Method, Assembly Language)

;*******************************************************************************
;
; 78K0S/Kx1+ EEPROM Emulation sample main program
;
;*******************************************************************************

;===============================================================================
;
; EEPROM EXTRN declaration
;
;===============================================================================
EXTRN __eeprom_read ; EEPROM read function
EXTRN __eeprom_write ; EEPROM write function

;===============================================================================
;
; RAM definition
;
;===============================================================================
XRAM DSEG SADDR
EEPROM_NO: DS 1 ; Data number
EEPROM_DATA: DS 2 ; EEPROM data
DELIMITER: DS 1 ; Delimiter

;===============================================================================
;
; Stack area securement
;
;===============================================================================
XSTK DSEG AT 0FED0H
STACKEND:
DS 30H ; Secures 32 bytes of stack area
STACKTOP: ; Stack area start address = FF00H

;===============================================================================
;
; Vector table settings
;
;===============================================================================
XVCT CSEG AT 0000H
DW RESET_START ;(00) RESET

;*******************************************************************************
;
; Initialization processing after reset release
;

98 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

;*******************************************************************************
XMAIN CSEG UNIT
RESET_START:

;-------------------------------------------------------------------------------
; Stack pointer setting
;-------------------------------------------------------------------------------
MOVW AX, #STACKTOP
MOVW SP, AX ; Sets stack pointer

;-------------------------------------------------------------------------------
; Watchdog timer setting
;-------------------------------------------------------------------------------
MOV WDTM, #01110111B ; Stops watchdog timer operation

;-------------------------------------------------------------------------------
; Clock frequency setting
;-------------------------------------------------------------------------------
MOV PPCC, #00000000B ; Clock supplied to peripheral hardware fxp = fx (= 8MHz)
MOV PCC, #00000000B ; CPU clock fcpu = fxp (= 8MHz)
MOV LSRCM, #00000001B ; Stops low-speed internal oscillator

;*******************************************************************************
;
; Main processing
;
;*******************************************************************************
MOV DELIMITER, #00H ; Initializes delimiter

MOVW AX, #EEPROM_NO ; Transfers EEPROM data address to HL


MOVW HL, AX

; Sets write data to EEPROM data 0


PUSH AX ; Saves EEPROM data address

MOV A, #00H ; Sets data number of EEPROM data 0


MOV [HL], A

MOV A, #01H ; Sets storage data of EEPROM data 0


MOV [HL+1], A
MOV A, #10H ; Sets storage data of EEPROM data 0
MOV [HL+2], A

POP AX ; Restores EEPROM data address

; Tests writing
CALL !__eeprom_write ; Calls EEPROM data 0 write function
BC $LOOP ; Jumps to LOOP if error

Application Note U17379EJ2V0AN 99


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

; Sets EEPROM data 1 write data


PUSH AX ; Saves EEPROM data address

MOV A, #01H ; Sets data number of EEPROM data 1


MOV [HL], A

MOV A, #80H ; Sets storage data of EEPROM data 1


MOV [HL+1], A
MOV A, #5AH ; Sets storage data of EEPROM data 1
MOV [HL+2], A

POP AX ; Restores EEPROM data address

; Tests writing
CALL !__eeprom_write ; Calls EEPROM data 1 write function
BC $LOOP ; Jumps to LOOP if error

; Tests reading
PUSH AX ; Saves EEPROM data address

MOV A, #00H ; Sets data number of EEPROM data 0


MOV [HL], A

POP AX ; Restores EEPROM data address

CALL !__eeprom_read ; Calls EEPROM read function


BC $LOOP ; Jumps to LOOP if error

PUSH AX ; Saves EEPROM data address

MOV A, #01H ; Sets data number of EEPROM data 1


MOV [HL], A

POP AX ; Restores EEPROM data address

CALL !__eeprom_read ; Calls EEPROM read function


BC $LOOP ; Jumps to LOOP if error

NOP ; Executes NOP if normal completion

LOOP:
BR $LOOP ; Completes EEPROM processing

end

100 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

B.3 Test Sample Program (Fixed-Length Multiple-Data Method, C Language)

/****************************************************************
78K0S/Kx1+ EEPROM Emulation sample main program
****************************************************************/

#define TRUE 0x01;


#define FALSE 0x00;

/***************************************
Function declaration
***************************************/
// Assembler function
extern bit _eeprom_write(unsigned char *); // Writes to EEPROM
extern bit _eeprom_read(unsigned char *); // Reads to EEPROM

/***************************************
EEPROM data structure declaration
***************************************/
typedef struct eeprom_data{
unsigned char uc_data_no; // Data number
unsigned char uc_eeprom_data[2]; // Data
unsigned char uc_delimiter; // Delimiter
} struct_eeprom_data;

/****************************************************************
Main function
****************************************************************/
void main(void)
{

/* Variable declaration */
struct_eeprom_data s_eeprom_data;
unsigned char uc_eeprom_func_result;

/* Use hwinit function to set peripheral settings */

/* Initializes EEPROM data 0 */


s_eeprom_data.uc_data_no = 0;
s_eeprom_data.uc_eeprom_data[0] = 0x01;
s_eeprom_data.uc_eeprom_data[1] = 0x10;
s_eeprom_data.uc_delimiter = 0;

/* Writes to EEPROM data 0 */


if(_eeprom_write(&(s_eeprom_data.uc_data_no)))
{
/* Write failure */
uc_eeprom_func_result = FALSE;

Application Note U17379EJ2V0AN 101


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

}
else
{
/* Write successful */
uc_eeprom_func_result = TRUE;
}

/* Initializes EEPROM data 1 */


s_eeprom_data.uc_data_no = 1;
s_eeprom_data.uc_eeprom_data[0] = 0x80;
s_eeprom_data.uc_eeprom_data[1] = 0x5A;
s_eeprom_data.uc_delimiter = 0;

/* Writes to EEPROM data 1 */


if(_eeprom_write(&(s_eeprom_data.uc_data_no)))
{
/* Write failure */
uc_eeprom_func_result = FALSE;
}
else
{
/* Write successful */
uc_eeprom_func_result = TRUE;
}

/* Specifies reading of EEPROM data 0 */


s_eeprom_data.uc_data_no = 0;

/* Reads EEPROM data 0 */


if(_eeprom_read(&(s_eeprom_data.uc_data_no)))
{
/* Read failure */
uc_eeprom_func_result = FALSE;
}
else
{
/* Read successful */
uc_eeprom_func_result = TRUE;
}

/* Reads specified EEPROM data1 */


s_eeprom_data.uc_data_no = 1;

/* Reads EEPROM data 1 */


if(_eeprom_read(&(s_eeprom_data.uc_data_no)))
{
/* Read failure */
uc_eeprom_func_result = FALSE;

102 Application Note U17379EJ2V0AN


APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)

}
else
{
/* Read successful */
uc_eeprom_func_result = TRUE;
}

while(1);
}

Application Note U17379EJ2V0AN 103


<R> APPENDIX C REVISION HISTORY

C.1 Major Revisions in This Edition

Page Description
CHAPTER 2 EEPROM EMULATION FUNCTION (FIXED-LENGTH SINGLE-DATA METHOD)
p. 9 Full modification of chapter.
CHAPTER 3 EEPROM EMULATION PROGRAM (FIXED-LENGTH SINGLE-DATA METHOD, ASSEMBLYLANGUAGE)
p. 18 Full modification of chapter.
CHAPTER 4 EEPROM EMULATION FUNCTION (FIXED-LENGTH MULTIPLE-DATA METHOD)
p. 37 Addition of chapter.
CHAPTER 5 EEPROM EMULATION PROGRAM (FIXED-LENGTH MULTIPLE-DATA METHOD, ASSEMBLY LANGUAGE)
p. 46 Addition of chapter.
APPENDIX A SAMPLE PROGRAM LIST (FIXED-LENGTH SINGLE-DATA METHOD)
p. 66 Full modification of chapter.
APPENDIX B SAMPLE PROGRAM LIST (FIXED-LENGTH MULTIPLE-DATA METHOD)
p. 84 Addition of chapter.
APPENDIX C RIVISION HISTORY
p. 104 Addition of chapter.

104 Application Note U17379EJ2V0AN


For further information,
please contact:
NEC Electronics Corporation
1753, Shimonumabe, Nakahara-ku,
Kawasaki, Kanagawa 211-8668,
Japan
Tel: 044-435-5111
http://www.necel.com/

[America] [Europe] [Asia & Oceania]

NEC Electronics America, Inc. NEC Electronics (Europe) GmbH NEC Electronics (China) Co., Ltd
2880 Scott Blvd. Arcadiastrasse 10 7th Floor, Quantum Plaza, No. 27 ZhiChunLu Haidian
Santa Clara, CA 95050-2554, U.S.A. 40472 Düsseldorf, Germany District, Beijing 100083, P.R.China
Tel: 408-588-6000 Tel: 0211-65030 Tel: 010-8235-1155
800-366-9782 http://www.eu.necel.com/ http://www.cn.necel.com/
http://www.am.necel.com/
Hanover Office Shanghai Branch
Podbielskistrasse 166 B Room 2509-2510, Bank of China Tower,
30177 Hannover 200 Yincheng Road Central,
Tel: 0 511 33 40 2-0 Pudong New Area, Shanghai, P.R.China P.C:200120
Tel:021-5888-5400
Munich Office http://www.cn.necel.com/
Werner-Eckert-Strasse 9
81829 München Shenzhen Branch
Tel: 0 89 92 10 03-0 Unit 01, 39/F, Excellence Times Square Building,
Stuttgart Office No. 4068 Yi Tian Road, Futian District, Shenzhen,
Industriestrasse 3 P.R.China P.C:518048
70565 Stuttgart Tel:0755-8282-9800
Tel: 0 711 99 01 0-0 http://www.cn.necel.com/
United Kingdom Branch
NEC Electronics Hong Kong Ltd.
Cygnus House, Sunrise Parkway Unit 1601-1613, 16/F., Tower 2, Grand Century Place,
Linford Wood, Milton Keynes 193 Prince Edward Road West, Mongkok, Kowloon, Hong Kong
MK14 6NP, U.K. Tel: 2886-9318
Tel: 01908-691-133 http://www.hk.necel.com/
Succursale Française
9, rue Paul Dautier, B.P. 52 NEC Electronics Taiwan Ltd.
78142 Velizy-Villacoublay Cédex 7F, No. 363 Fu Shing North Road
France Taipei, Taiwan, R. O. C.
Tel: 01-3067-5800 Tel: 02-8175-9600
http://www.tw.necel.com/
Sucursal en España
Juan Esplandiu, 15
NEC Electronics Singapore Pte. Ltd.
28007 Madrid, Spain
238A Thomson Road,
Tel: 091-504-2787
#12-08 Novena Square,
Tyskland Filial Singapore 307684
Täby Centrum Tel: 6253-8311
Entrance S (7th floor) http://www.sg.necel.com/
18322 Täby, Sweden
Tel: 08 638 72 00 NEC Electronics Korea Ltd.
11F., Samik Lavied’or Bldg., 720-2,
Filiale Italiana
Yeoksam-Dong, Kangnam-Ku,
Via Fabio Filzi, 25/A
Seoul, 135-080, Korea
20124 Milano, Italy
Tel: 02-558-3737
Tel: 02-667541
http://www.kr.necel.com/
Branch The Netherlands
Steijgerweg 6
5616 HS Eindhoven
The Netherlands
Tel: 040 265 40 10

G0706

You might also like