ST AN2540 Application note

AN2540

Application note

EEPROM emulation in

STR91xF devices

Introduction

Replacing external EEPROM with emulated EEPROM from the embedded-Flash memory of the microcontroller is a complex development. This application note is aimed at readers that are already familiar with the techniques used to secure the content of evolutive information in the external EEPROM of embedded applications.This application note explains the differences between external/internal EEPROMs and embedded-Flash memory. It also gives advice on how to replace external EEPROM with emulated-EEPROM using the onchip Flash memory of STR91xF devices.

This document also focuses on some embedded aspects in emulated-EEPROM data storage, that are assumed to be known by the reader.

Overview

Electrically erasable and programmable read-only memory (EEPROM) is a key component in many embedded applications requiring non-volatile storage of data that are updated at a byte, half-word or word granularity during run time.

On the other hand, the microcontrollers used in those systems are more and more based on embedded-Flash memory. To eliminate components, save silicon area and reduce system cost, the STR91xF Flash memory could eventually replace the external EEPROM for simultaneous code and data storage.

However unlike Flash memory, external EEPROM does not require a block erase operation to free up space before data can be rewritten. A special software management is required to store data into Flash memory.

Obviously the emulation software scheme depends on many factors including the EEPROM reliability, Flash memory architecture and product requirements. Two approaches to implementation are described in detail in this application note using the on-chip Flash memory of the STR91xF microcontrollers.

June 2007

Rev 1

1/25

www.st.com

Contents

AN2540 - Application note

 

 

Contents

1

Embedded Flash memory vs. EEPROM: main differences . . . . . . . . . .

5

 

1.1

Difference in write access time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.2

Difference in writing method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.3

Difference in erase time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

2

Appropriate solution for Emulated EEPROM in the STR91xF . . . . . . . .

7

 

2.1

STR91xF on-chip Flash memory features . . . . . . . . . . . . . . . . . . . . . . . . .

7

 

2.2

STR91xF Flash memory library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

3

Implementing the EEPROM emulation . . . . . . . . . . . . . . . . . . . . . . . . . .

8

 

3.1

Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

 

3.2

1st method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

3.2.1 Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3.2.2 EEPROM software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

 

3.3

2nd method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

 

 

3.3.1

Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

 

 

3.3.2

EEPROM software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

 

3.4

Program execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

4

Embedded application aspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

 

4.1

Data granularity management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

4.1.1 Programming on a word-by-word basis . . . . . . . . . . . . . . . . . . . . . . . . . 18 4.1.2 Programming on a byte-by-byte basis . . . . . . . . . . . . . . . . . . . . . . . . . . 18

4.2 Wear-leveling: Flash endurance improvement . . . . . . . . . . . . . . . . . . . . . 19

4.2.1 Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

 

4.3

Sector header recovery in case of power loss . . . . . . . . . . . . . . . . . . . . .

20

 

4.4

Emulated EEPROM parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

 

 

4.4.1

Program/Erase parameter cycling . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

 

 

4.4.2

Program timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

22

5

Conclusion .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

6

Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

2/25

 

 

 

 

 

 

 

 

 

AN2540 - Application note

List of tables

 

 

List of tables

Table 1. Differences between Embedded Flash memory and EEPROM . . . . . . . . . . . . . . . . . . . . . . 5 Table 2. EepromFormat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Table 3. FindValidSector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Table 4. ReadVariable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Table 5. WriteVariable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Table 6. WriteVerifyVariableFull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Table 7. EepromSectorTransfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Table 8. Status combinations and actions to be taken . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Table 9. Write time related to the current implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Table 10. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

3/25

List of figures

AN2540 - Application note

 

 

List of figures

Figure 1. Header field status switching between sector0 and 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 2. Sector swap scheme: Sector1 erased . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Figure 3. Sector swap scheme: Sector0 erased . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 4. 1st method: WriteVariable flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Figure 5. Data storage procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Figure 6. Data update flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Figure 7. 2nd method: WriteVariable flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Figure 8. WriteOnebyte function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Figure 9. Sector swap scheme with four sectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

4/25

AN2540 - Application note

Embedded Flash memory vs. EEPROM: main differences

 

 

1Embedded Flash memory vs. EEPROM: main differences

Before describing the proposed concept for EEPROM emulation, it is important to remember the main differences between the embedded Flash memory of a microcontroller and serial external EEPROMs. These differences are the same for any microcontroller (that is they are not specific to STR91xF products). They are summarized in Table 1.

Table 1.

Differences between Embedded Flash memory and EEPROM

Feature

 

External EEPROM

Emulated EEPROM using on-chip Flash

 

memory

 

 

 

 

 

 

 

 

 

A few ms

 

Write time

 

– random byte: 5 to 10 ms

A few µs

 

– page: equivalent to a hundred µs per

(e.g.: 20 µs per 16-bit word)

 

 

 

 

word (5 to 10 ms per page)

 

 

 

 

 

Erase time

 

N/A

seconds (e.g.: 1.5 s)

 

 

 

 

 

 

Once started, is not CPU-dependent,

Once started, is CPU-dependent: a CPU

Write method

 

reset will stop the write process even if the

 

needs only proper supply.

 

 

 

supply stays within specifications.

 

 

 

 

 

 

Serial: a hundred µs

Parallel: a hundred ns

Read access

 

random word: 92 µs

 

very few CPU cycles per 16-bit word.

 

 

page: 22.5 µs/byte

 

 

 

 

 

 

 

1.1Difference in write access time

As the Flash memory has a shorter write access time, critical parameters can be stored faster into the emulated EEPROM than into an external serial EEPROM. The use of the Flash memory therefore improves the system robustness.

1.2Difference in writing method

One of the major differences between external and emulated EEPROM for embedded applications is the writing method.

Standalone external EEPROM: once started by the CPU, the writing of a word cannot be interrupted by a CPU reset. Only a power supply failure can interrupt the writing process, so properly sizing the decoupling capacitors can secure the write process to a standalone EEPROM.

Emulated EEPROM from an embedded Flash memory: once started by the CPU, the writing can be interrupted by a power failure and by a CPU reset. This difference should be analyzed by system designers to understand the possible impact(s) in their applications, and to determine a proper method to handle them.

5/25

Embedded Flash memory vs. EEPROM: main differences

AN2540 - Application note

 

 

1.3Difference in erase time

The difference in erase time is the other major difference between standalone EEPROM and emulated EEPROM with embedded Flash memory. Unlike Flash memory, EEPROM does not require a block erase operation to free up space before writing. Moreover, as the erase process of a block in the Flash memory takes a few seconds, power shut-down and other spurious events that may interrupt the erase process (e.g.: reset) should be considered when designing the Flash memory management software. This means that to design a robust Flash memory management software it is necessary to have a good understanding of the Flash memory erase process.

6/25

AN2540 - Application note

Appropriate solution for Emulated EEPROM in the STR91xF

 

 

2Appropriate solution for Emulated EEPROM in the STR91xF

The STR91xF microcontrollers support the hardware and software architecture necessary to emulate EEPROM memory using the on-chip Flash memory.

2.1STR91xF on-chip Flash memory features

The STR91x internal Flash memory consists of two banks: Main Flash memory (Bank 0) and Secondary Flash memory (Bank 1). The Main Flash memory is up to 512 Kbytes in size and includes up to eight 64-Kbyte sectors. The Secondary Flash memory is 32 Kbytes in size and consists of four 8-Kbyte sectors, it can be useful for the wear-leveling feature (refer to Section 4.2).

One of the STR9 embedded Flash memory features is Read-while-Write (RWW) Dual Bank operations. This means that the Main Flash memory (Bank0) can be used for code storage and the smaller Secondary Flash memory, for data storage (EEPROM emulation).

The Flash memory can be erased on a sector or bank basis, and programmed on a 16bit half-word basis.

Each bank can be programmed and erased over 100 000 cycles.

20-year data retention.

Each sector can be individually protected and unprotected against program and erase operations.

As the Flash memory has a shorter write access time, critical parameters can be stored faster in the emulated EEPROM than in an external serial EEPROM.

Interrupt servicing during program/erase is possible.

CPU program does not need to be copied into RAM during program/erase: RAM less used to perform EEPROM emulation.

Program/Erase Suspend and Resume commands supported. That is, Flash memory sector erase may be suspended while data is read from other sectors in the same Flash memory bank, and then resumed after reading.

2.2STR91xF Flash memory library

The Flash memory programming library is a set of optimized C routines. It contains all that is needed to program the Flash memory embedded in STR9 devices.

The Flash memory library contains the following source files:

91x_fmi.c, that contains the function codes

91x_fmi.h, that contains the function prototypes

To use the functions provided by the Flash memory library, these two files must be added to the project. With the STR9 software (SW) library (FMI driver) it is easy to implement the EEPROM emulation software.

7/25

ST AN2540 Application note

Implementing the EEPROM emulation

AN2540 - Application note

 

 

3 Implementing the EEPROM emulation

3.1Principle

This emulation is performed in various ways by considering the Flash memory limitations and the product requirements. Two approaches are described below in detail. Both require a minimum of two Flash memory sectors of identical size, that are allocated to non-volatile data. One that is initially erased and can be programmed byte by byte, and the other that is ready to take over when the first sector needs to be garbage-collected.

Since the STR91xF on-chip Flash memory can be programmed on a 16-bit half-word basis, the data granularity in this implementation is 16 bits.

A header field that occupies the first 16-bit half word of each sector indicates the sector status.

Each sector has four possible states:

ERASED: The sector is empty.

RECEIVE_DATA: The sector is receiving data from the full sector.

VALID_SECTOR: The sector contains valid data and its state will not change until valid data are completely transferred to the erased sector.

TRANSFER_COMPLETE: Transfer of data to the other sector is finished and this sector is no longer in use. The system can then erase it and prepare it for future data.

Figure 1. shows how to switch from one state to another for both sectors.

Figure 1. Header field status switching between sector0 and 1

 

 

 

 

 

Write data in sector0

 

 

Valid0

 

Erased1

 

 

 

 

 

 

 

 

 

 

Sector0 Full

 

 

 

Valid0

 

Receive1

Copy data from sector0

 

 

 

 

 

to sector1

 

 

 

 

Transfer data from sector0 to sector1 complete

 

 

 

 

 

 

 

 

Transfer_

 

Valid1

 

 

 

complete0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Erase0

 

 

 

 

 

 

 

 

 

Erased0

 

Valid1

Write data in sector1

 

 

 

 

 

 

 

 

 

 

Sector1 Full

 

 

 

Receive0

 

Valid1

Copy data from sector1

 

 

 

to sector0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Transfer data from sector1 to sector0 complete

 

 

Valid0

 

Transfer_

 

 

 

 

complete1

 

States related to sector0

 

 

 

 

 

 

 

 

States related to sector1

 

 

 

 

Erase1

 

 

 

 

 

 

 

 

 

 

ai14085

8/25

Loading...
+ 17 hidden pages