EEPROM Emulation Driver For M68HC908 Microcontrollers, Rev. 0 PDF
EEPROM Emulation Driver For M68HC908 Microcontrollers, Rev. 0 PDF
Rev. 0, 08/2006
Application Note
1 Introduction 1
Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1 Overview 1.3 References . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Acronyms and Abbreviations . . . . . . . . . . . . . 2
Electrically erasable, programmable, read-only memory 2 EEPROM Emulation Scheme . . . . . . . . . . . . . . . . 3
(EEPROM), which can be byte- or word-programmed 2.1 System Architecture. . . . . . . . . . . . . . . . . . . . 3
and erased, is often used in automotive electronic 2.2 EEPROM Emulation Software Layout . . . . . . 3
2.3 EEPROM Emulation Memory Layout . . . . . . 6
control units (ECUs). This flexibility for program and
2.4 EEPROM Emulation Operations . . . . . . . . . . 8
erase operations makes it suitable for data storage of 2.5 Limitation of COP Support . . . . . . . . . . . . . . 10
application variables that must be maintained when 2.6 Notes and Limitations . . . . . . . . . . . . . . . . . 10
power is removed and need to be updated individually 3 Preparation and Running Environment. . . . . . . . 11
3.1 Preparation . . . . . . . . . . . . . . . . . . . . . . . . . 11
during run-time. For the devices without EEPROM 3.2 Use EEPROM Emulation Driver . . . . . . . . . 12
memory, the page-erasable Flash memory can be used to 4 EEPROM Emulation Driver. . . . . . . . . . . . . . . . . 13
emulate the EEPROM through EEPROM emulation 4.1 Function Introduction . . . . . . . . . . . . . . . . . . 13
software. 4.2 Function Calling Relationship . . . . . . . . . . . 14
4.3 Global Parameters and Macros . . . . . . . . . . 15
The EEPROM emulation driver for the M68HC908 4.4 Function Return Code . . . . . . . . . . . . . . . . . 17
4.5 High Level Functions (User Level Functions)18
implements the fixed-length data record scheme on
4.6 Middle Level Functions . . . . . . . . . . . . . . . . 27
0.5um SGF Flash. The EEPROM functionalities to be 4.7 Low Level Functions . . . . . . . . . . . . . . . . . 37
emulated include the following: organizing data records, Appendix A: Performance Data
initializing and de-initializing EEPROM, reporting A.1 Code Size and Stack Usage . . . . . . . . . . . . 44
A.2 Read / Write Times . . . . . . . . . . . . . . 44
EEPROM status, and reading, writing, and deleting data
records. The demo code shows how to use the EEPROM
emulation driver.
1.2 Features
The EEPROM emulation driver for the M68HC908 provides the following features:
• Implements the fixed-length record scheme with two Flash sector clusters.
• Hierarchical design supports standalone/synchronous applications.
• All driver functions can run directly from Flash, except that the low-level, high-voltage
(program/erase) functions have to run from RAM, to minimize RAM usage.
• Supports computer operating properly (COP) service every 240us @ a 8-MHz bus clock.
• Assembly source code release.
• C calling convention compliant.
• Ready-to-use demo illustrates the usage of the driver.
1.3 References
The following references were used to write this document:
1. AN2302r1: “EEPROM Emulation for the MC9S12C32”
2. AN2183: “Using FLASH as EEPROM on the MC68HC908GP32”
3. MPC5500EEWP: “EEPROM Emulation with MPC5500 Family Microcontrollers”
4. CPU08 Central Processor Unit Reference Manual
5. “MC68HC08JL3/H Technical Data,” Rev. 4
6. “MC68HC08GP32/H Technical Data,” Rev. 6, 08/2002
7. HC908 SGF NVM Standard Software Driver User’s Manual, V1.2
Before a user’s application calls the high-level EEPROM emulation driver, the user interface global data
has to be provided by the user. The active cluster configuration data is used by the EED to record the
active cluster information. The EED driver calls low-level SSD functions to implement Flash operations.
The EED sets the SSD parameter data before calling SSD functions. The internal used variables are used
by the EED driver internally.
All of the above global parameters are defined in a parameter section named EMUParaSec in
EED_Para.asm. For a detailed description of each parameter, please refer to Section 4.3, “Global
Parameters and Macros.”
0x80
Global Parameter Data
(31 Bytes)
0x9E
0x9F User Data Buffer (2 bytes)
0xA1 Others*
0xFD
Stack for High Level
0xFF Function Calling
Figure 3 RAM Memory Layout Example for 128-byte Part
For 128-byte parts, 2 bytes of user buffer is reserved for the user, from 0x9F to 0xA0. Note that “Others”
in Figure 3 is for a user employing Metrowerks CodeWarrior to debug into the low-level SSD functions;
this 11-byte region will be occupied by CodeWarrior debugger. Otherwise, this region can be reserved as
user buffer as well. The user can also adopt other memory layouts—Figure 3 is just one example. For a
part with more RAM, the user can reserve more RAM for user data buffer.
The EEPROM emulation driver adopts the M68HC908 family’s Flash to emulate the EEPROM. Two
clusters are needed for EEPROM emulation: active and alternative. Each cluster contains one or more
contiguous erasable pages. These two clusters cannot overlap each other, but they do not need to be
contiguous. They should contain the same number of pages, and this number is user configurable.
Cluster Status
Erase Cycles
Status
ID 0
Data 0
Status1
ID 1
Data 1
Status
ID 2
Data 2
Status2
ID 1
Data 1 (updated)
…
Blank
NOTES:
1 The old value of data record 1
2
The new value of data record 1
Figure 5 EEPROM Cluster Memory Layout
The data record cannot be updated directly on the same location. Instead, a new record with the new
value will be written to the EEPROM, and the read routine will check the latest valid one as the final
value of specific data.
Swapping stages: (assuming cluster A is the active cluster and cluster B is the alternative cluster)
• Cluster A is CLUSTER_STATUS_ACTIVE and cluster B is CLUSTER_STATUS_BLANKED.
This is the initial state of cluster swapping.
• Cluster A is CLUSTER_STATUS_ACTIVE and cluster B is CLUSTER_STATUS_STARTED.
The data record copying is in progress. If swapping fails in this stage, all the data record accesses
can also direct to the original active cluster, cluster A. Cluster B should be re-initialized as the
alternative one (CLUSTER_STATUS_BLANKED state) and perform the swapping again.
• Cluster A is CLUSTER_STATUS_ACTIVE and cluster B is also CLUSTER_STATUS_ACTIVE.
All the latest data records are copied completely and the data accessing can target cluster B. If
swapping fails in this stage, the user only needs to initialize cluster A as the alternative cluster
(CLUSTER_STATUS_BLANKED state).
• Cluster A is CLUSTER_STATUS_ERASED and cluster B is CLUSTER_STATUS_ACTIVE.
Cluster A is fully erased but not ready for emulation. It is needed to continue initializing cluster A
as the alternative cluster (CLUSTER_STATUS_BLANKED state).
• Cluster A is CLUSTER_STATUS_BLANKED and cluster B is CLUSTER_STATUS_ACTIVE.
This is the final state of cluster swapping.
However, the deleted data record can be re-written into the EEPROM. The read routine will determine
the latest data record.
• It is strongly recommended not to program or erase the same Flash location while using the EED
to operate it.
• The EED is in assembly source code release, so the compiling optimization options do not impact
the correctness of the EED.
3.1.2 Macros
Before using the EEPROM emulation driver, the following macros should be set properly to meet a
specific need:
• EED_ERASE_PAGE_SIZE
The Flash modules embedded in the M68HC908 series have two sizes of Flash page for erasing:
28 and 64 bytes. Before using the EED Flash driver on a M68HC908 part, it is very important to
set the correct size of Flash erase page. The macro EED_ERASE_PAGE_SIZE is used to control
the size of Flash page:
The default setting is:
EED_ERASE_PAGE_SIZE: EQU $80
• USER_DATA_LENGTH
The user data length in a fixed length record. The user data length is configurable via this macro
before compilation. Its valid value range is [1, 0xFD]. Otherwise, a compilation error message
“User data length is out of range [1, 0xFD]!” will be reported.
The default setting is:
USER_DATA_LENGTH: EQU $2
• CLUSTER_0_START and CLUSTER_1_START
The starting address for two clusters. These two values should be page alignment.
API
Function name Caller functions Functions to be called
Hierarchies
CLOCKSCALAR 1 byte The scaling factor based on the bus clock used for Flash
operations.1
STARTADDR 2 bytes The start address of the Flash area to be operated.
ENDADDR 2 bytes The end address of the Flash area to be operated.
BUFFER 2 bytes The start address of the source data buffer used for
programming and verification.
FLASHCR 2 bytes Address of the Flash control register.2
FLASHPR 2 bytes Address of the Flash protection register. 2
NOTES:
1 CLOCKSCALAR is used to manually control the delay timings for Flash operation via software
instructions. It equals 8 times the value of the bus clock in MHz and then rounded down to the
nearest integer. The formula is shown as following:
CLOCKSCALAR = INT [Bus Clock (MHz) * 8]
For example, if the bus clock is 2.45 MHz, 8 times of the bus clock in MHz is 19.6, so that
CLOCKSCALAR should be set to 19.
BUS_CLOCK, the bus clock in Hz was defined in “SSD_Flash.inc”. Then the macro of
NVM_CLOCK_SCALAR, also defined in “SSD_Flash.inc”, can be used to set the global variable
of CLOCKSCALAR.
NVM_CLOCK_SCALAR: EQU ((BUS_CLOCK * 8) / 1000000)
The following assembly instruction shows how to use the above macro to set the value of
CLOCKSCALAR.
MOV #NVM_CLOCK_SCALAR, CLOCKSCALAR
2 The two SSD global parameters, FLASHCR and FLASHPR, should be set correctly before
calling SSD functions. Two macros, FLCR and FLBPR, were defined in EED_Flash.inc. The
following assembly instructions show how to use the above macros to set the value of FLASHCR
and FLASHPR.
LDHX #FLCR
STHX FLASHCR ; set Flash control register address
LDHX #FLBPR
STHX FLASHPR ; set Flash block protection register address
For details about the SSD global parameters, please refer to the SGF NVM SSD for HC908 User’s
Manual.
hvType 1 byte The type of High Voltage operation in stack. 0x5A: FlashProgram,
0xA5: FlashEraseCOP.
hvPosition 2 bytes The RAM location of High Voltage operation locates.
nextRecID 1 byte To save the valid record ID for the next parsing of the cluster. During
cluster swapping, this parameter can skip the non-existing record IDs.
emuBuffer 2 bytes 2-byte internal buffer, used to hold the data for programming or save
the location of latest record.
4.3.5 Macros
Name Description
RECORD_LENGTH The total record length. It is composed of user data, record status and record
ID, i.e., USER_DATA_LENGTH + 2.
RECORD_ID_MAX The maximum value of record identifier. It’s reserved for EED internal usage.
4.5.1 FSL_InitEeprom
4.5.1.1 Description
FSL_InitEeprom determines the active cluster based on the user’s EEPROM configuration macros.
4.5.1.2 Procedure
1. Allocate some space for high voltage function on stack;
2. Check the cluster status field to find the active cluster;
3. If no active cluster is found, erase, blank check the first cluster, change its status to
CLUSTER_STATUS_ACTIVE, and set active cluster index to 0 in global parameters;
4. If the active cluster is found, save its index;
5. Erase, blank check, and then change the alternative cluster status to
CLUSTER_STATUS_BLANKED;
6. De-allocate the stack for high voltage function;
7. Return.
4.5.1.3 Definition
unsigned char FSL_InitEeprom (void);
4.5.1.7 Tips
The starting address of both clusters should be configured as page alignment. The two clusters have the
same number of Flash pages, and the two ranges of clusters should not be overlapped.
If it is the first time using EEPROM emulation, FSL_InitEeprom will initialize both clusters and select
the first one as the active cluster. If continuing to use the emulation, FSL_InitEeprom will determine
which cluster is the active one and initialize the alternative cluster.
Once there are two cluster marked as active, FSL_InitEeprom will scan two clusters and choose the one
has more free space than the final active cluster, then initialize the other one as alternative cluster.
If any record in the active cluster has an invalid record status, FSL_InitEeprom will set “emuBlank” to
“emuEndAddr”, which will result in a cluster swapping in the following record writing.
Interface global parameters “recID”, “source”, and “failedAddress” are reused in this function.
4.5.1.8 Troubleshooting
Table 10. Troubleshooting for FSL_InitEeprom
EE_ERROR_NOT_BLANK The Flash memories are not blank. Check the Flash memory if it can be
erased correctly.
EE_ERROR_VERIFY Corresponding source data and content Check the Flash memory if it can be
of destination location mismatch. programmed correctly.
4.5.2 FSL_ReadEeprom
4.5.2.1 Description
This function reads the specific data record. The starting address of the record data will be returned.
4.5.2.2 Procedure
1. Check that data ID is within the valid ID range;
2. Check the data in the active cluster;
3. If data ID is not found in the active block, return error code;
4. Get the starting address of the record data;
5. Return.
4.5.2.3 Definition
unsigned char FSL_ReadEeprom (void);
recID 1 byte The required data ID. Can be any value from 0 ~
(RECORD_ID_MAX – 1).
source 2 bytes The address of record data. Used as an output argument.
emuStartAddr 2 bytes The starting address of the active cluster.
emuEndAddr 2 bytes The end address of the active cluster.
emuBlank 2 bytes The blank location of the active cluster.
4.5.2.7 Tips
This function does not read out the data into the user’s buffer. Instead, it returns the starting address of the
user data in the data record and saves this address into “source” variable.
Interface global parameter “failedAddress” is reused in this function.
4.5.2.8 Troubleshooting
Table 13. Troubleshooting for FSL_ReadEeprom
EE_ERROR_NOFND Record not found in cluster. Write the data record to the
EEPROM first.
EE_ERROR_IDRNG Record identifier exceeds the valid range. Check the data ID, which should be
within the valid ID range.
4.5.3 FSL_WriteEeprom
4.5.3.1 Description
This function writes a data record to the EEPROM emulation. While there is not enough free space in the
active cluster, this routine will perform cluster swapping to clean up the EEPROM.
4.5.3.2 Procedure
1. Allocate some space for high voltage function on stack;
2. Check the validity of user provided record ID;
3. Check if there is enough free space for the data to be written in the active cluster;
4. If not enough free space, perform cluster swapping. After swapping, if still not enough free space
in the active cluster, return error code;
5. Write the data record to the active cluster;
4.5.3.3 Definition
unsigned char FSL_WriteEeprom (void);
— FSL_CopyRecord
– FSL_Program
— FSL_InitCluster
– FSL_Erase
– FSL_Program
• FSL_CopyRecord
— FSL_Program
4.5.3.7 Tips
The record data length is limited within the values of MIN_DATA_LENGTH and
MAX_DATA_LENGTH, which are configured in EED_Flash.inc. Please refer to Section 4.3.5,
“Macros” for details. The record data length cannot be set to 0, which will result in a compiling error.
If there is not enough free space for writing a new data record, FSL_WriteEeprom will trigger a cluster
swapping to copy all the latest valid data copies to the alternative cluster and remove the old and invalid
data records. This procedure may take a little longer (depending on the number of valid data records) to
complete the write operation.
Interface global parameters “erasingCycles” and “failedAddress” are reused in this function.
4.5.3.8 Troubleshooting
Table 4-16
EE_ERROR_IDRNG Record identifier exceeds the valid Check the data ID, which should be
range. within the valid ID range.
EE_ERROR_NOMEM No enough EEPROM memory. Use more Flash pages to enlarge the
emulated EEPROM size.
EE_ERROR_CSTAT Cluster status error. Call FSL_InitEeprom to re-initialize
the EEPROM.
EE_ERROR_NOT_BLANK The Flash memories are not blank. Check the Flash memory if it can be
erased correctly.
EE_ERROR_VERIFY Corresponding source data and Check the Flash memory if it can be
content of destination location programmed correctly.
mismatch.
4.5.4 FSL_DeleteRecord
4.5.4.1 Description
This function deletes a data record in EEPROM. It does not physically remove the data record from the
EEPROM. Instead, it only changes the record status to a special value (RECORD_STATUS_DELETED).
This deleted record will be discarded during the next cluster swapping.
4.5.4.2 Procedure
1. Allocate some space for high voltage function on stack;
2. Check the validity of the record ID;
3. Search the record in the active cluster;
4. If it is found, update the record status to RECORD_STATUS_DELETED;
5. If it is not found, return error code;
6. De-allocate the stack for high voltage function;
7. Return.
4.5.4.3 Definition
unsigned char FSL_DeleteRecord (void);
recID 1 byte The required data ID. Can be any value from 0 ~
(RECORD_ID_MAX – 1).
emuStartAddr 2 bytes The starting address of the active cluster.
emuEndAddr 2 bytes The end address of the active cluster.
emuBlank 2 bytes The blank location of the active cluster.
4.5.4.7 Tips
The deleted data record can be re-written to EEPROM when needed.
Interface global parameter “failedAddress” is reused in this function.
4.5.4.8 Troubleshooting
Table 19. Troubleshooting for FSL_DeleteRecord
4.5.5 FSL_DeinitEeprom
4.5.5.1 Description
This function releases all the Flash used to EEPROM emulation. After de-initialized, the Flash pages for
emulation are fully erased.
4.5.5.2 Procedure
1. Allocate some space for high voltage function on stack;
2. Erase the Flash pages used for EEPROM emulation;
3. De-allocate the stack for high voltage function;
4. Return.
4.5.5.3 Definition
unsigned char FSL_DeinitEeprom (void);
4.5.5.7 Tips
Only the Flash pages used for EEPROM emulation will be erased.
4.5.5.8 Troubleshooting
Table 21. Troubleshooting for FSL_DeinitEeprom
EE_ERROR_NOT_BLANK The Flash memories are not blank Check the Flash memory if it
can be erased correctly.
4.5.6 FSL_ReportEepromStatus
Reports cluster erasure cycles and checks the cluster and data record statuses.
4.5.6.1 Procedure
1. Check both cluster statuses, if any invalid value existing, return error code;
2. Fetch the erasing cycles from active cluster erasing cycle field;
3. Go through entire active cluster to check the data record status with record ID of
RECORD_ID_MAX. If the status of any data record is not correct, fill its address to
“failedAddress” and set the error return code;
4. Return.
4.5.6.2 Definition
unsigned char FSL_ReportEepromStatus (void);
4.5.6.6 Tips
The erasure cycles of the cluster is counted from the time EEPROM is set up and will be accumulated
after each erasure. Once the EEPROM is de-initialized, the erasure cycles will be reset to 0.
This function can only check the cluster and data record status. It cannot report error when the user’s data
are incorrect.
If any of the records have invalid record status, FSL_ReportEepromStatus will set “emuBlank” to
“emuEndAddr”, which will result in a cluster swapping in the following record writing.
The value of failedAddress is valid only when the return code is EE_ERROR_RSTAT. And the value of
erasingCycles is valid only when the return code is not EE_ERROR_CSTAT.
4.5.6.7 Troubleshooting
Table 24. Troubleshooting for FSL_ReportEepromStatus
4.6.1 FSL_Erase
4.6.1.1 Description
FSL_Erase will erase a range of contiguous Flash pages and verify them. It encapsulates two low-level
SSD functions: FlashEraseCOP and BlankCheck. Input parameters are used directly without any check.
4.6.1.2 Procedure
1. Check if FlashEraseCOP function is already in stack. If not, copy it into stack;
2. Call SSD FlashEraseCOP to erase a single Flash page;
3. Call SSD BlankCheck to verify the Flash page;
4. Repeat steps 3 and 4 until all Flash pages are erased and verified;
5. Return.
4.6.1.3 Definition
unsigned char FSL_Erase (void);
STARTADDR 2 bytes The starting address of the first Flash It should be page alignment.
pages to be erased.
ENDADDR 2 bytes The end address of the last Flash
pages to be erased.
4.6.1.7 Tips
At the entry of this function, it checks if the required high voltage function is on stack or not by
evaluating the value of hvType. If not, copy the required high voltage function onto stack specified by
hvPostion.
This function requires that the STARTADDR should be on Flash page alignment.
FlashEraseCOP can erase only one Flash page per call. The ENDADDR is required to be pushed onto
stack before calling FlashEraseCOP. FSL_Erase will erase Flash pages one by one.
4.6.1.8 Troubleshooting
Table 27. Troubleshooting for FSL_Erase
EE_ERROR_NOT_BLANK The Flash memories Check the Flash memory if it can be erased
are not blank correctly.
4.6.2 FSL_Program
4.6.2.1 Description
FSL_Program will program specified data to destination address in Flash and verify the data. It
encapsulates two low-level SSD functions: FlashProgram and ProgramVerify. Input parameters are used
directly without any check. At the most, 2 bytes can be programmed in a single call.
4.6.2.2 Procedure
1. Check if the FlashProgram function is already in stack. If not, copy it into stack;
2. Program the data contained in emuBuffer to Flash;
3. Verify the data programmed in Flash;
4. Return.
4.6.2.3 Definition
unsigned char FSL_Program (void);
STARTADDR 2 bytes The starting address in Flash to be Used as an input and output
written to. argument.
ENDADDR 2 bytes The end address in Flash to be written The range determined by
to. STARTADDR and ENDADDR
should be within a same Flash
row.
emuBuffer 2 bytes Source data to be programmed. At most 2 bytes can be
programmed per call.
4.6.2.7 Tips
At the entry of this function, it checks if the required high voltage function is on stack or not by
evaluating the value of hvType. If not, copy the required high voltage function onto stack specified by
hvPostion.
FlashProgram will update the value of STARTADDR. To verify the Flash result after programming,
STARTADDR should be pushed onto stack before calling FlashProgram.
Since the Flash requires row programming (i.e., only up to one row can be programmed per call), the
programming range specified by STARTADDR and ENDADDR should be within the same row.
4.6.2.8 Troubleshooting
Table 30. Troubleshooting for FSL_Program
EE_ERROR_VERIFY Corresponding source data and Check the Flash memory if it can be
content of destination location programmed correctly.
mismatch.
4.6.3 FSL_CopyRecord
4.6.3.1 Description
FSL_CopyRecord writes user data into Flash in a record format.
4.6.3.2 Procedure
1. Program and verify the record status and record status is changed to
RECORD_STATUS_STARTED;
2. Program and verify the record ID;
3. Program and verify the record data byte by byte;
4.6.3.3 Definition
unsigned char FSL_CopyRecord (void);
4.6.3.7 Tips
To reduce the RAM usage, global variable “failedAddress” is reused here to save the buffer address of the
record data.
4.6.3.8 Troubleshooting
Table 33. Troubleshooting for FSL_CopyRecord
4.6.4 FSL_InitCluster
4.6.4.1 Description
FSL_InitCluster initializes one cluster to make it ready for EEPROM emulation.
4.6.4.2 Procedure
1. Check cluster status field;
2. If the cluster status is CLUSTER_STATUS_BLANKED, return directly;
3. If the cluster status is CLUSTER_STATUS_STARTED or CLUSTER_STATUS_ACTIVE, its
erasing cycles will be add 1;
4. Otherwise, its erasing cycles will be 1;
5. Erase and verify the cluster;
6. Write the new erasing cycles;
7. Change the cluster status to CLUSTER_STATUS_BLANKED;
8. Return.
4.6.4.3 Definition
unsigned char FSL_InitCluster (void);
4.6.4.7 Tips
The cluster after initialization contains only the cluster status field and erasure cycle field.
If the cluster status to be initialized is CLUSTER_STATUS_BLANKED, its erasing cycles will be kept
the same. If the cluster status is CLUSTER_STATUS_STARTED or CLUSTER_STATUS_ACTIVE, its
erasing cycles will be add 1 based on its current erasing cycles. For all other cases, its erasing cycles will
be reset to 1.
4.6.4.8 Troubleshooting
Table 36. Troubleshooting for FSL_InitCluster
EE_ERROR_NOT_BLANK The Flash memories are Check the Flash memory if it can be erased
not blank. correctly.
EE_ERROR_VERIFY Corresponding source Check the Flash memory if it can be
data and content of programmed correctly.
destination location
mismatch.
4.6.5 FSL_SwapCluster
4.6.5.1 Description
FSL_SwapCluster copies the latest valid records from the active cluster to the alternative cluster.
4.6.5.2 Procedure
1. If the status of alternative cluster is not CLUSTER_STATUS_BLANKED, return cluster status
error code;
2. Change the alternative cluster status to CLUSTER_STATUS_STARTED;
3. Push the recID onto stack;
4. Fetch the latest data record of a specific ID from the active cluster;
5. Copy this record to the alternative cluster;
6. Update the alternative cluster blank space pointer and next record ID;
7. Repeat steps 4–6 until all latest data records are copied to alternative cluster;
8. Change the alternative cluster status to CLUSTER_STATUS_ACTIVE;
9. Initialize the old active cluster;
10. Pull recID from stack;
11. Update the active cluster index in the EEPROM configuration;
12. Return.
4.6.5.3 Definition
unsigned char FSL_SwapCluster (void);
4.6.5.7 Tips
In this function, all the latest valid records should be copied from the active cluster to the alternative
cluster. The global variable recID is reused. To save its original value, recID is pushed onto the stack
before it is reused and restored from the stack before exiting.
4.6.5.8 Troubleshooting
Table 39. Troubleshooting for FSL_SwapCluster
4.6.6 FSL_SearchRecord
4.6.6.1 Description
FSL_SearchRecord searches the record with a specific record ID in a cluster. This function will parse the
whole cluster to determine the latest copy of data record.
4.6.6.2 Procedure
1. Clear the emuBuffer with 0xFFFF;
2. Initialize nextRecID to RECORD_ID_MAX;
3. Fetch one record from the cluster;
4. If the recID is RECORD_ID_MAX, go to step 5, otherwise, go to step 9;
5. Check the record status;
6. If it is RECORD_STATUS_COMPLETED, RECORD_STATUS_STARTED or
RECORD_STATUS_DELETED, skip this data record;
7. If it is RECORD_STATUS_ERASED, it is the end of the valid data record region;
8. Otherwise, it’s an invalid record, set return code to EE_ERROR_RSTAT and go to step 16;
9. If the record ID of the current record is not the one being searching for, skip this data record;
10. Otherwise, check the record status;
11. If it is RECORD_STATUS_COMPLETED, save the record address to emuBuffer;
12. If it is RECORD_STATUS_STARTED, skip this wrong data record;
13. Otherwise, clear emuBuffer and skip this wrong data record;
14. For the steps 9–13, if the ID of current record is greater than recID and less than nextRecID,
update nextRecID as the current record ID;
15. Repeat steps 3 and 14 until you reach the end of the cluster or encounter the blank location;
16. Return (The location of error record or the starting address of cluster free space is saved in
failedAddress.).
4.6.6.3 Definition
unsigned char FSL_SearchRecord (void);
4.6.6.7 Tips
None.
4.6.6.8 Troubleshooting
Table 42. Troubleshooting for FSL_SearchRecord
4.7.1 FlashEraseCOP
4.7.1.1 Description
This function supports page erase with COP service enabled. Furthermore, this function only provides the
4ms-page erase timing. The page erase size depends on the hardware and can either be 64 or 128 bytes.
This function supports COP service period of minimum 240 us at a 8-MHz bus clock by splitting a long
erase pulse into pieces of shorter pulses. COP service period is scaled with bus clock if the user selects a
different bus clock. For instance, if a 4-MHz bus clock is used, minimum COP period to be supported is
then 480 us.
To minimize code size, this function does not perform any range checking on parameters. So the user
must ensure the relevant global parameters are correctly initialized.
This function does not have return code. Please use BlankCheck to confirm the target region is erased.
4.7.1.2 Procedure
1. Set the erase loop counter based on required timing.
2. Set ERASE bit in the Flash control register;
3. Read the Flash protection register;
4. Bump COP;
5. Write to any Flash address within the erase region with any data;
6. Wait for time Tnvs (>=10 us);
7. Set HVEN bit in the Flash control register;
8. Delay for Tcop;
9. Clear ERASE bit in the Flash control register;
10. For page erase, wait for Tnvh (>=5 us);
11. Clear HVEN bit in the Flash control register;
12. Bump COP;
13. Decrease the erase loop counter. If it is not 0, go to step2, else go to next step;
14. Return.
4.7.1.3 Definition
void FlashEraseCOP (void);
FLASHCR 2 bytes Address of the Flash Please consult hardware spec for actual
control register. address. Typical value is 0xFE08.
FLASHPR 2 bytes Address of the Flash Please consult hardware spec for actual
protection register. address. Typical value is 0xFF7E.
CLOCKSCALAR 1 byte The scaling factor 8 times of bus clock scaled in MHz and rounded
based on the bus clock. down to the nearest integer.
STARTADDR 2 bytes The start address of Should be within valid Flash region.
Flash area to be
erased.
4.7.1.6 Tips
Set the Flash protection bits correctly before calling erase function.
Writing to the COP control register when erase voltage is turned on will interfere with erase operation,
because the COP register is located in the Flash array. Thus writing to COP can only occur between erase
pulses.
The function uses the STARTADDR as the erase interlock write address. Because there is no error
checking on parameters, the user must ensure the STARTADDR is within the Flash region to be erased.
This function cannot erase the Flash array in which it resides.
It is highly recommended that interrupts be disabled during program/erase operations.
4.7.1.7 Troubleshooting
This section is intentionally blank.
Flash Control Register HVEN, MASS, ERASE and PGM Read, Write
Flash Block Protection Register BPR[7:0] Read
COP BIT[7:0] Write
4.7.2 BlankCheck
4.7.2.1 Description
This function reads the memory and compares against 0xFF (logic state of erased bit cells).
If the blank check fails, the error code of SGF_ERROR_NOT_BLANK (0x30) will be returned in
register A. The first non-blank address can be derived from the global variable of STARTADDR.
To minimize code size, this function does not perform any range checking on parameters. The user must
ensure the relevant global parameters are correctly initialized.
There is no COP service in this function.
4.7.2.2 Procedure
1. Compare STARTADDR with ENDADDR; if greater then go to step 4;
2. Compare the data at STARTADDR against 0xFF and update STARTADDR at the same time;
3. If not equal, then load error code SGF_ERROR_NOT_BLANK to A and go to step 5; otherwise
go to step 1;
4. Load SGF_OK to A;
5. Return.
4.7.2.3 Definition
unsigned char BlankCheck(void);
STARTADDR 2 bytes The start address of area to be Can be any readable location.
checked.
ENDADDR 2 bytes The end address of area to be Should equal to or greater than the start
checked. address. Otherwise, no checking will be
performed.
4.7.2.6 Tips
If ENDADDR is less than STARTADDR, the function returns SGF_OK without any checking.
If SGF_ERROR_NOT_BLANK is returned in A, the first non-blank address is equal to the value of
STARTADDR minus 1.
4.7.2.7 Troubleshooting
Table 47. Troubleshooting for BlankCheck
SGF_ERROR_NOT_BLANK There is a non-blank byte (i.e. not Erase the region again.
0xFF) within the region.
4.7.3 FlashProgram
4.7.3.1 Description
This function is used to program source data to the specified Flash area. It only supports row
programming. That is, only up to one row (32 bytes or 64 bytes depending on hardware) can be
programmed per call. Users should ensure all the addresses to be programmed are within one row;
otherwise, programming may fail. Please refer to the User’s Manual of M68HC908 parts for more
information.
To minimize code size, this function does not perform any range checking on parameters. The user must
ensure the relevant global parameters are correctly initialized.
COP is not serviced inside this function.
This function does not have return code. Please use ProgramVerify to confirm the target region is
programmed correctly.
4.7.3.2 Procedure
1. Set PGM bit in the Flash control register and clear other bits;
2. Read the Flash protection register;
3. Write to any Flash address within the programming row with any data;
4. Wait for time Tnvs (>=10 us);
5. Set HVEN bit in the Flash control register;
6. Wait for time Tpgs (>=5 us);
7. Write source data byte to the Flash destination address to be programmed;
8. Wait for time Tprog (30 us to 40 us);
9. Increment the source address and destination address; go to step 7 until the destination address
exceeds the end address;
10. Clear PGM bit in the Flash control register;
11. Wait for time Tnvh (>=5 us);
12. Clear HVEN bit in the Flash control register;
13. Return.
4.7.3.3 Definition
void FlashProgram (void);
FLASHCR 2 bytes Address of the Flash control Please consult hardware spec for actual address.
register. Typical value is 0xFE08.
FLASHPR 2 bytes Address of the Flash Please consult hardware spec for actual address.
protection register. Typical value is 0xFF7E.
CLOCKSCALAR 1 bytes The scaling factor based on 8 times of bus clock scaled in MHz and rounded
the bus clock. down to the nearest integer.
STARTADDR 2 bytes The start address of Flash Should be within valid Flash region.
area to be programmed.
ENDADDR 2 bytes The end address of Flash area Should be within valid Flash region. And must be
to be programmed. equal to or greater than STARTADDR.
BUFFER 2 bytes The start address of the Should be within readable memory.
source data buffer used for
programming.
4.7.3.6 Tips
Set the Flash protection bits correctly before calling program function.
Because the function does not perform any checking on parameters, the user must ensure that
ENDADDR is not less than STARTADDR and the program region is within valid Flash range.
The time required to program an entire row may be longer than the COP timeout period (the smallest
possible COP timeout period is about 240 microseconds). Based on the programming performance data
shown in the appendix, users decide how many bytes to be programmed per call in order to avoid COP
timeout.
It is highly recommended that interrupts be disabled during program/erase operations.
4.7.3.7 Troubleshooting
This section is intentionally blank.
Flash Control Register HVEN, MASS, ERASE and PGM Read, Write
Flash Block Protection Register BPR[7:0] Read
4.7.4 ProgramVerify
4.7.4.1 Description
This function is used to verify that the data programmed into the Flash or EEPROM matches the source
data.
If the verification fails, the error code of SGF_ERROR_VERIFY (0x40) will be returned in the register
A. The first failed address can be derived from the global variable of STARTADDR.
To minimize code size, this function does not perform any range checking on parameters. The user must
ensure the relevant global parameters are correctly initialized.
There is no COP service in this function.
4.7.4.2 Procedure
1. Set SGF_OK to A;
2. Compare STARTADDR with ENDADDR; if greater then go to 9;
3. Get the data at STARTADDR and increment STARTADDR at the same time;
4. Compare the data with the content of the source buffer;
5. If not equal, go to step 8;
6. Update the source data buffer address;
7. Go to step 1;
8. Load error code SGF_ERROR_VERIFY to A;
9. Return.
4.7.4.3 Definition
unsigned char ProgramVerify (void);
STARTADDR 2 bytes The start address of area to Can be any readable location.
be verified.
ENDADDR 2 bytes The end address of area to Should equal to or greater than the start
be verified. address. Otherwise, no checking will be
performed.
BUFFER 2 bytes The start address of the Should be within readable memory.
source data buffer used for
verification.
4.7.4.6 Tips
If ENDADDR is less than STARTADDR, the function returns SGF_OK without any checking.
If SGF_ERROR_VERIFY is returned in A, the first failed address is equal to the value of STARTADDR
minus 1.
4.7.4.7 Troubleshooting
Table 52. Troubleshooting for ProgramVerify
SGF_ERROR_VERIFY There is a mismatch between the source Erase the region and re-program the
data and programmed Flash region. source data.
FSL_InitEeprom 79
FSL_WriteEeprom 82
FSL_ReadEeprom 2
FSL_DeleteRecord 76
FSL_ReportEepromStatus 2
FSL_DeinitEeprom 77
MAX Stack Usage 82
NOTES:
1 The stack usage contains the stack allocated
NOTE
The data above was collected based on the MC68HC908JL3 parts. For other
M68HC908 parts, similar performance data is expected.
The “No swap” writing time for FSL_WriteEeprom is collected when the
write will not trigger cluster swapping.
The “Swap” writing time for FSL_WriteEeprom is collected when the write
will trigger cluster swapping (The cluster is full of records, only two of them
have a same record ID. All other records have different record ID).
The minimal time for FSL_ReadEeprom is collected when the active cluster
contains only 1 record, while the maximum read time is collected when the
active cluster is full of records.
The timings for FSL_DeleteRecord and FSL_ReportEepromStatus are
collected when the active cluster is full of records.
AN3040
Rev. 0, 08/2006