AN2594STApplicationnoteEEPROMemulationinSTM32F101xxandSTM32F103xxmicrocontrollersIntroductionManyapplications require EEPROM (electrically erasableprogrammableread-onlymemory)for non-volatiledata storage.Forlow-costpurposes,the STM32F101xxandSTM32F103xxdevicesdonotuseEEPROM.Instead,they implementEEPROMemulationusing the embedded Flash memory.Thisapplicationnoteexplains thedifferencesbetweenexternalEEPROMand embeddedFlash memory, and it describes a software method for emulating EEPROM using the on-chipFlashmemoryoftheSTM32F101xxandSTM32F103xxdevices.Thisdocumentalsofocusesonsomeembeddedaspects inemulatedEEPROMdatastorage, that the reader is assumed to know.GlossaryMedium-densitydevicesareSTM32F101xxandSTM32F103xxmicrocontrollerswheretheFlashmemorydensityrangesbetween32and128Kbytes.High-density devices are STM32F101xx and STM32F103xx microcontrollers where theFlashmemorydensity rangesbetween256and512Kbytes.Rev 2June20081/16www.st.com
June 2008 Rev 2 1/16 AN2594 Application note EEPROM emulation in STM32F101xx and STM32F103xx microcontrollers Introduction Many applications require EEPROM (electrically erasable programmable read-only memory) for non-volatile data storage. For low-cost purposes, the STM32F101xx and STM32F103xx devices do not use EEPROM. Instead, they implement EEPROM emulation using the embedded Flash memory. This application note explains the differences between external EEPROM and embedded Flash memory, and it describes a software method for emulating EEPROM using the onchip Flash memory of the STM32F101xx and STM32F103xx devices. This document also focuses on some embedded aspects in emulated EEPROM data storage, that the reader is assumed to know. Glossary Medium-density devices are STM32F101xx and STM32F103xx microcontrollers where the Flash memory density ranges between 32 and 128 Kbytes. High-density devices are STM32F101xx and STM32F103xx microcontrollers where the Flash memory density ranges between 256 and 512 Kbytes. www.st.com
ContentsAN2594Contents1EmbeddedFlashmemoryversusEEPROM:maindifferences51.1Differenceinwriteaccesstime51.26Differenceinwritingmethod1.36Differenceinerasetime2ImplementingEEPROMemulation12.1Principle-2.1.18Applicationexample2.1.2EEPROMsoftwaredescription..8311Embeddedapplication aspects3.111Datagranularitymanagement.113.1.1Programming on a word-by-word basis...113.1.2Programming on a byte-by-byte basis3.211Wearleveling:Flashmemoryendurance improvement.113.2.1Wear-leveling implementation example3.312Page header recovery in case of power loss3.413Cycling capability4Revision history15S2/16
Contents AN2594 2/16 Contents 1 Embedded Flash memory versus EEPROM: main differences . . . . . . . 5 1.1 Difference in write access time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.2 Difference in writing method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.3 Difference in erase time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2 Implementing EEPROM emulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1 Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.1 Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.1.2 EEPROM software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3 Embedded application aspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.1 Data granularity management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.1.1 Programming on a word-by-word basis . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.1.2 Programming on a byte-by-byte basis . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.2 Wear leveling: Flash memory endurance improvement . . . . . . . . . . . . . . 11 3.2.1 Wear-leveling implementation example . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.3 Page header recovery in case of power loss . . . . . . . . . . . . . . . . . . . . . . 12 3.4 Cycling capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 4 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
AN2594ListoftablesList of tablesTable 1.DifferencesbetweenembeddedFlashmemoryandEEPROM......5Table 2..·..13Status combinations andactionstobetaken....Table 3.Maximum number of variables stored in emulated EEPROM (with 10 000 cycles) ......14Table 4........15Document revision historyST3/16
AN2594 List of tables 3/16 List of tables Table 1. Differences between embedded Flash memory and EEPROM . . . . . . . . . . . . . . . . . . . . . . 5 Table 2. Status combinations and actions to be taken . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Table 3. Maximum number of variables stored in emulated EEPROM (with 10 000 cycles) . . . . . . 14 Table 4. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
ListoffiguresAN2594ListoffiguresFigure 1...7Headerstatusswitchingbetweenpage0andpage1Figure 2...8EEPROMvariableformat....:8Figure 3.Dataupdateflow............10Figure 4.WriteVariableflowchart.........-...12Figure 5.Pageswapschemewithfourpages (wearleveling)SA4/16
List of figures AN2594 4/16 List of figures Figure 1. Header status switching between page0 and page1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Figure 2. EEPROM variable format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 3. Data update flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 4. WriteVariable flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Figure 5. Page swap scheme with four pages (wear leveling). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
AN2594EmbeddedFlashmemoryversusEEPROM:maindifferences1EmbeddedFlashmemoryversusEEPROM:maindifferencesElectrically erasable andprogrammableread-only memory (EEPROM) is a key componentofmanyembedded applicationsthat requirenon-volatilestorageof dataupdated witha byteor word granularity duringrun time.Ontheotherhand,themicrocontrollers usedinthosesystems areeachtimemore oftenbasedonembeddedFlashmemory.Toeliminatecomponents,savesiliconspaceandreduce systemcost, the STM32F10xxxFlash memory may be used instead of EEPROM forsimultaneous code and data storage.UnlikeFlashmemory,however,externalEEPROMdoesnotrequireaneraseoperationtofree up space beforedata can be rewritten.Hencea special software management isrequiredtostoredata intoembeddedFlashmemory.Obviouslytheemulationsoftwareschemedependsonmanyfactors,includingtheEEPROMreliability,thearchitectureoftheFlashmemoryused,andtheproductrequirements.The main differences between embeddedFlashmemoryandexternal serial EEPROMaregenerictoanymicrocontrollerthatusethe sameFlashmemorytechnology(it isnot specifictotheSTM32F10xxxfamilyproducts).ThemajordifferencesaresummarizedinTable1.Table 1.DifferencesbetweenembeddedFlashmemoryandEEPROMEmulated EEPROM using on-chipFlashFeatureExternalEEPROMmemory-afewms-randombyte:5to10msWrite timeWord program time: 20 μs-page:a hundred μsper word (5 to10msperpage)N/AErasetimePage/MassErasetime:20msoncestarted,isCPU-dependent:aCPUoncestarted,isnotCPU-dependentWrite methodreset will stopthe writeprocess even if theonlyneedspropersupply.suppliedpowerstayswithinspecifications- serial: a hundred μs-parallel:a hundred nsReadaccess- random word: 92 μs-veryfewCPUcyclesperword.-Accesstime:35ns-page:22.5μsperbyte-from 10kilocycles to 100 kilocycles (theuseofmanyon-chipFlashmemorypagesWrite/Erasefrom 10 kilocycles to 1 000isequivalenttoincreasingthenumberofcycleskilocycleswrite cycles)seeSection3.4:Cyclingcapability1.1Difference in write access timeAsFlashmemorieshaveashorterwriteaccesstime,critical parameterscanbestoredfasterintheemulatedEEPROMthaninanexternalserialEEPROM,therebyimprovingdatastorage.ST5/16
AN2594 Embedded Flash memory versus EEPROM: main differences 5/16 1 Embedded Flash memory versus EEPROM: main differences Electrically erasable and programmable read-only memory (EEPROM) is a key component of many embedded applications that require non-volatile storage of data updated with a byte or word granularity during run time. On the other hand, the microcontrollers used in those systems are each time more often based on embedded Flash memory. To eliminate components, save silicon space and reduce system cost, the STM32F10xxx Flash memory may be used instead of EEPROM for simultaneous code and data storage. Unlike Flash memory, however, external EEPROM does not require an erase operation to free up space before data can be rewritten. Hence a special software management is required to store data into embedded Flash memory. Obviously the emulation software scheme depends on many factors, including the EEPROM reliability, the architecture of the Flash memory used, and the product requirements. The main differences between embedded Flash memory and external serial EEPROM are generic to any microcontroller that use the same Flash memory technology (it is not specific to the STM32F10xxx family products). The major differences are summarized in Table 1. 1.1 Difference in write access time As Flash memories have a shorter write access time, critical parameters can be stored faster in the emulated EEPROM than in an external serial EEPROM, thereby improving data storage. Table 1. Differences between embedded Flash memory and EEPROM Feature External EEPROM Emulated EEPROM using on-chip Flash memory Write time – a few ms – random byte: 5 to 10 ms – page: a hundred µs per word (5 to 10 ms per page) Word program time: 20 µs Erase time N/A Page/Mass Erase time: 20 ms Write method – once started, is not CPU-dependent – only needs proper supply. once started, is CPU-dependent: a CPU reset will stop the write process even if the supplied power stays within specifications. Read access – serial: a hundred µs – random word: 92 µs – page: 22.5 µs per byte – parallel: a hundred ns – very few CPU cycles per word. – Access time: 35 ns Write/Erase cycles – from 10 kilocycles to 1 000 kilocycles – from 10 kilocycles to 100 kilocycles (the use of many on-chip Flash memory pages is equivalent to increasing the number of write cycles) see Section 3.4: Cycling capability