Professional Documents
Culture Documents
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
2005
Printed in Japan
[MEMO]
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).
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.
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.
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.
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.
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
↑ ↑
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.
Data size
Valid or invalid flag (see 2.1.3)
Memory size of one block
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.
1 byte 1 byte
Can support up to
3.5 KB total
Block 4
Block 3
Block 2
Block 1
Block 0
0000H
(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.
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).
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 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
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).
Be sure to meet all of the conditions listed in Table 2-1 before executing the EEPROM emulation program.
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.
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.
The resources used by this program are listed in Table 3-2 below.
Resource Description
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.
Example 1. When using two blocks (blocks 14 and 15) as EEPROM blocks
EEPROM_BLOCK EQU (14)
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.
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{
};
(1) EEPROM read processing (__eeprom_read): Reads from the EEPROM area the data of the set size.
(2) EEPROM write processing (EEPROMWrite): Writes to the EEPROM area the data of the set size.
Table 3-11. Mode Transition Processing (from Self Programming Mode to Normal Mode)
Table 3-12. Mode Transition Processing (from Normal Mode to Self Programming Mode)
[Overview]
The data defined with the structure is read from specified storage address.
__eeprom_read
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
[Overview]
The data of the specified number is written to a valid block from the storage address.
__eeprom_write
Yes
Block found?
No
Specify first valid block Search writable area
(EEPROMBlockInit) (EEPROMWriteTopSearch)
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
[Overview]
The currently used blocks of the flash memory that is allocated as EEPROM is searched.
EEPROMUseBlockSearch
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
[Overview]
If there are no valid blocks among the blocks specified for EEPROM, the first specified block is set as valid.
EEPROMBlockInit
No
Erased?
Yes
Set block as valid
(Setvalid)
No
Valid flag set?
Yes
CY = 0 CY = 1
(initialize successful ) (initialize failure)
Initialize completion
[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
No
Erased?
Yes
Write next block's data
Set start address
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
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
Delimiter≠ FFH
Delimiter checked?
Delimiter = FFH
Check data erasure of
write area
No
Is data FF?
Yes
CY = 0
(Search successful)
RET
No
End of block?
Yes
CY = 1
(Search failure)
RET
[Overview]
Reads the storage address of the latest data.
EEPROMDataSearch
No
Delimiter ≠ 00H
Delimiter checked?
Delimiter = 00H
Copy address to DE register
(return value)
No
Search successful?
Yes
CY = 0 CY = 1
(Search successful) (Search failure)
RET
User access processings EEPROM emulation processings Flash memory control processings
__eerom_read EEPROMUseBlockSearch
EEPROMDataSearch
__eeprom_write SelfFlashModeOn
SelfFlashModeOff
EEPROMUseBlockSearch
FlashBlockErase
FlashEEPROMWrite
EEPROMWriteTopSearch
FlashBlockErase
FlashEEPROMWrite
SetValid
SetInvalid
EEPROMDataSearch
FlashEEPROMWrite
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
↑
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.
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
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.
1 byte 1 byte
Can support up to
3.5 KB total
Block 4
Block 3
Block 2
Block 1
Block 0
0000H
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.
(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
<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).
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 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
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).
(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 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
Be sure to meet all of the conditions listed in Table 4-1 before executing the EEPROM emulation program.
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.
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.
The resources used by this program are listed in Table 5-2 below.
Resource Description
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.
(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)
LENG EQU (4) ; Data length (size including data number and delimiter
(2 bytes, total))
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.
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{
};
(1) EEPROM read processing (__eeprom_read): Reads from the EEPROM area the data of the set size.
(2) EEPROM write processing (__eeprom_write): Writes to the EEPROM area the data of the set size.
Table 5-12. Mode Transition Processing (from Self Programming Mode to Normal Mode)
Table 5-13. Mode Transition Processing (from Normal Mode to Self Programming Mode)
[Overview]
The data defined with the structure is read from specified storage address.
__eeprom_read
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
[Overview]
The data of the specified number is written to a valid block from the storage address.
__eeprom_write
Yes
Block found?
No
Specify first valid block Search writable area
(EEPROMBlockInit) (EEPROMWriteTopSearch)
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
[Overview]
The currently used blocks of the flash memory that is allocated as EEPROM is searched.
EEPROMUseBlockSearch
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
[Overview]
If there are no valid blocks among the blocks specified for EEPROM, the first specified block is set as valid.
EEPROMBlockInit
No
Erased?
Yes
Set block as valid
(Setvalid)
No
Valid flag set?
Yes
CY = 0 CY = 1
(initialize successful) (initialize failure)
Initialize completion
[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
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
No
Write successful?
2 Yes
Yes
All data complete?
No
4
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
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
No
Is data FF?
Yes
CY = 0
(Search successful)
RET
No
End of block?
Yes
CY = 1
(Search failure)
RET
[Overview]
Reads the storage address of the latest data.
EEPROMDataSearch
Yes
Data number matched?
No
Delimiter ≠ 00H
Delimiter checked?
Delimiter = 00H
Copy address to DE register
(return value)
Yes
Is data FF?
No
No
Search successful ?
Yes
CY = 0 CY = 1
(Search successful) (Search failure)
RET
User access processings EEPROM emulation processings Flash memory control processings
__eerprom_read getpara
EEPROMUseBlockSearch
EEPROMDataSearch
__eeprom_write getpara
SelfFlashModeOn
SelfFlashModeOff
EEPROMUseBlockSearch
FlashBlockErase
FlashEEPROMWrite
EEPROMWriteTopSearch
FlashBlockErase
FlashEEPROMWrite
SetValid
EEPROMDataSearch SetInvalid
FlashEEPROMWrite
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; 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>
;---------------------------------------------------------------------
; 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
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
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
;
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
;
EP_WRITE_E:
POP BC
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
;-------------------------------------------------------------------
; 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:
;
; Copies data to new block
;
PUSH HL ; Copies data to be written to DE register
POP DE
;
; Sets new block as valid because data transfer is completed
;
;
; Sets old block as invalid
;
EP_BCHG_E:
POP HL
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,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
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:
;-------------------------------------------------------------------
; 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
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
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
; 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:
;-------------------------------------------------------------------
; 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:
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
END
;*******************************************************************************
;
; 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
;
;*******************************************************************************
;-------------------------------------------------------------------------------
; 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
; Write test
CALL !__eeprom_write ; Calls EEPROM write function
BC $LOOP ; Jumps to LOOP if error
MOV [HL], A
MOV A, #00H ; Sets EEPROM storage data
MOV [HL+1], A
; Read test
CALL !__eeprom_read ; Calls EEPROM read function
BC $LOOP ; Jumps to LOOP if error
LOOP:
BR $LOOP ; Completes EEPROM processing
end
/****************************************************************
78K0S/Kx1+ EEPROM Emulation sample main program
****************************************************************/
/***************************************
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;
/* Writes to EEPROM */
if(_eeprom_write(&(s_eeprom_data.uc_eeprom_data[0])))
{
/* Write failure */
uc_eeprom_func_result = FALSE;
}
else
{
/* 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);
}
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; 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>
;---------------------------------------------------------------------
; 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
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
;
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.
__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
;
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
;
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
;-------------------------------------------------------------------
; 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.
;-------------------------------------------------------------------
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
;
;
; 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
;
; 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
;
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:
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:
;-------------------------------------------------------------------
; 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,
;-------------------------------------------------------------------
; 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
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)
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)
;-------------------------------------------------------------------
; 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
FL_WR_W2:
INCW DE ; Read address + 1
INCW HL ; Write address + 1
DBNZ C,$FL_WR_W1 ; Loops for specified number of bytes
END
;*******************************************************************************
;
; 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
;
;*******************************************************************************
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
; Tests writing
CALL !__eeprom_write ; Calls EEPROM data 0 write function
BC $LOOP ; Jumps to LOOP if error
; 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
LOOP:
BR $LOOP ; Completes EEPROM processing
end
/****************************************************************
78K0S/Kx1+ EEPROM Emulation sample main program
****************************************************************/
/***************************************
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;
}
else
{
/* Write successful */
uc_eeprom_func_result = TRUE;
}
}
else
{
/* Read successful */
uc_eeprom_func_result = TRUE;
}
while(1);
}
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.
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