AN2302 Freescale Semiconductor / Motorola, AN2302 Datasheet

no-image

AN2302

Manufacturer Part Number
AN2302
Description
EEPROM Emulation for the MC9S12C32
Manufacturer
Freescale Semiconductor / Motorola
Datasheet
Application Note
AN2302/D
Rev. 1, 1/2003
EEPROM Emulation for the
MC9S12C32
By: Stuart Robb
Introduction
Motorola, East Kilbride
Many applications require EEPROM (Electrically Erasable Programmable
Read-Only Memory) for non-volatile data storage. EEPROM is typically
characterised by the ability to erase and write individual bytes of memory many
times over, with programmed locations retaining their data over an extended
period when the power supply is removed.
Most MC9S12 Flash microcontrollers include on-chip EEPROM but some omit
EEPROM for reduced price. MC9S12 microcontrollers which do not have on-
chip EEPROM, such as the MC9S12C family, can store non-volatile data in the
on-chip Flash memory using the software described in this application note,
thus saving the cost of an external EEPROM.
This application note describes a software method for emulating EEPROM
using the on-chip Flash memory of the MC9S12C32. This method may be used
on other MC9S12 flash microcontrollers that do not have on-chip EEPROM and
may also be used to provide additional EEPROM data storage on MC9S12
flash microcontrollers that have insufficient EEPROM for a particular
application. This software makes no assumptions about the way in which the
non-volatile data will be stored or updated. Data variables may be of arbitrary
size and may be updated at random. It is possible that a more optimised
approach may exist for a less general case.
The software described in this application note may be configured to allow
interrupts to be serviced during the programming and erasure of non-volatile
data. Alternatively, a "call-back" function may be enabled, allowing inputs to be
polled or a watchdog to be refreshed during programming and erasure.
Freescale Semiconductor, Inc.
For More Information On This Product,
Go to: www.freescale.com
© Motorola, Inc., 2003

Related parts for AN2302

AN2302 Summary of contents

Page 1

... Freescale Semiconductor, Inc. Application Note AN2302/D Rev. 1, 1/2003 EEPROM Emulation for the MC9S12C32 By: Stuart Robb Motorola, East Kilbride Introduction Many applications require EEPROM (Electrically Erasable Programmable Read-Only Memory) for non-volatile data storage. EEPROM is typically characterised by the ability to erase and write individual bytes of memory many times over, with programmed locations retaining their data over an extended period when the power supply is removed ...

Page 2

... Freescale Semiconductor, Inc. AN2302/D EEPROM Emulation software features: • • • • • • • Implementation There are two common methods used to store non-volatile data in Flash. One method is to keep a copy of all non-volatile data variables in a buffer in RAM. The data is "saved" to Flash as often as is considered necessary by programming the entire buffer contents to a pre-erased Flash sector ...

Page 3

... This would leave 20,000 - 7000 = 13,000 program/erase cycles for the remaining 6 variables average of 2166 updates each. MOTOROLA For More Information On This Product, EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Implementation 3 ...

Page 4

... Freescale Semiconductor, Inc. AN2302/D There are two strategies that can be employed (within the context of the software described in this paper) to increase the effective permitted number of data updates. The first is simply to increase the number of Flash sectors allocated to non-volatile data storage. If the non-volatile data variables fill one entire sector, the permitted number of data updates is Number of Sectors x Program/Erase cycles per Sector ...

Page 5

... MOTOROLA For More Information On This Product swpgm f NVMOP Equation 1 are slightly under 50 s, depending on the selected swpgm t era f Equation 2 are around 21ms. era EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Implementation is within the range of NVMOP 25 f bus 4000 NVMOP 5 ...

Page 6

... Freescale Semiconductor, Inc. AN2302/D erasure of the Flash. The Flash area which is overlaid by RAM is no longer available for use. NOTE: If the non-maskable interrupt is enabled (X-bit cleared), then it cannot subsequently be disabled by software. In this case, the RAM must be re- mapped to the top of the memory map, the vector table must be copied to RAM, and the non-maskable interrupt service routine must be executed from RAM ...

Page 7

... Bank 1 (old data) (current data) Bank 3 (erased) Bank 4 (old data) Bank 5 (old data) Bank 6 (old data) Bank 7 (old data) EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Implementation Unchanged data copied and new data written by WriteEeprom() Bank 2 This sector erased before ...

Page 8

... Freescale Semiconductor, Inc. AN2302/D $0000 $03FF $4000 $C000 $F800 $FF80 $FFFF Figure 2. Example MC9S12C32 Memory Map with EEPROM Emulation and Interrupts In the Figure 2 example, the RAM is remapped to the top of the memory map, enabling interrupts to be serviced during Flash programming and erasure. ...

Page 9

... In the user's application, all non-volatile data variables are accessed using the functions ReadEeprom and WriteEeprom. The user's application files which access non-volatile data variables should therefore include the file EE_Emulation.h. MOTOROLA For More Information On This Product, EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description 9 ...

Page 10

... Freescale Semiconductor, Inc. AN2302/D EE_Emulation.h The file EE_Emulation contains function prototypes and the configuration parameters for Eeprom emulation of non-volatile data variables. This file should be included in all files which call any function in EE_Emulation.c. The following values must be correctly defined by the user: IRQ_DURING_PROG This value can optionally be defined either in this file the compiler command line ...

Page 11

... VT_SIZE This value is only required if interrupts must be serviced during programming (IRQ_DURING_PROG defined). This value matches the size of the VectorTable array and will normally be 126. MOTOROLA For More Information On This Product, EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description 11 ...

Page 12

... Freescale Semiconductor, Inc. AN2302/D EE_Emulation.c The file EE_Emulation contains the functions required for Eeprom emulation of non-volatile data variables which are executed from Flash. All functions in this file are located in the DEFAULT code segment. The user should not modify this file. ReadEeprom The ReadEeprom function is used to obtain the current value of a non-volatile data variable ...

Page 13

... EEPROM source address to get current location size = Copy one byte of EEPROM address to destination address Increment EEPROM address and destination address by 1 Decrement size by 1 Figure 3. ReadEeprom Flow Diagram EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description Yes RestoreCCR Return 13 ...

Page 14

... Freescale Semiconductor, Inc. AN2302/D WriteEeprom The WriteEeprom function is used to update a non-volatile data variable with a new value. InitEeprom must have been called once before this function is called. Whenever this function is called, a whole new bank is programmed. This will take approximately (EEPROM_SIZE_BYTES + Furthermore, if there are no more erased banks in the current sector, then the next Flash sector will be erased, this will take approximately 20ms ...

Page 15

... Yes ProgEepromWord: Program new bank with data word from old bank. Next EEPROM address Figure 4. WriteEeprom Flow Diagram EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description No No Return FAIL No Bank erased? Yes No EEPROM destination ...

Page 16

... Freescale Semiconductor, Inc. AN2302/D 16 Yes No ProgEepromWord: Program word comprising new data Next EEPROM address Decrement size by 2 EEPROM Errors when address = end Yes programming? of bank? No ProgEepromWord: Program new bank with data word from old bank ActiveBank = oldBank Next EEPROM address Return status Figure 5 ...

Page 17

... This function initialises the Flash prescaler, copies the required functions to RAM and determines the bank with the most recent data. Prototype: void InitEeprom(void) Parameters: void Return: void Example: InitEeprom(); MOTOROLA For More Information On This Product, EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description 17 ...

Page 18

... Freescale Semiconductor, Inc. AN2302/D 18 InitEeprom Initialise Flash clock prescaler InitRAMFuncs Start with bank 0 Read bank Status word Status word = erased? No Status roll-over Yes detected? Yes Largest Status word less than EEPROM_BANKS? No Yes ActiveBank = this bank Checked all Next bank No banks? Figure 6. InitEeprom Flow Diagram ...

Page 19

... InitRAMFuncs(void) Parameters: void Return: void Example: InitRAMFuncs(); MOTOROLA For More Information On This Product, InitRAMFuncs IRQ_DURING_PROG defined CopyToRAM: Copy RAM functions to RAM Return Figure 7. InitRAMFuncs Flow Diagram EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description CopyToRAM: Copy VectorTable to RAM 19 ...

Page 20

... Freescale Semiconductor, Inc. AN2302/D CopyToRAM This static function is called by InitRAMFuncs to copy code and data from Flash to RAM not callable by the application. Prototype: void CopyToRAM(UINT8 *src, UINT8 *dest, UINT16 size) Parameters: src dest size Return: void Example: CopyToRAM((UINT8 *)FLASH_COPY_START, (UINT8 *)RAM_FUNCS_START, RAM_FUNCS_SIZE); ...

Page 21

... ProgEepromWord Interrupts during programming? Yes ProgFlash: program flash word Interrupts during programming? Yes prog word = data? Yes Increment flash address pointer Return status Figure 9. ProgramEepromWord Flow Diagram EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description No MaskInterrupts No RestoreCCR No status = FAIL 21 ...

Page 22

... Freescale Semiconductor, Inc. AN2302/D Parameters: eepromAddr Return PASS FAIL Example: status = EraseEepromBank(eepromAddr); 22 address of the (first) flash sector( erased flash erase commands executed successfully flash erase commands failed EraseEepromBank Interrupts during erase? MaskInterrupts Yes ProgFlash: erase flash sector Interrupts during erase? ...

Page 23

... CCR MaskInterrupts Store CCR Set I-mask bit in CCR Return Figure 11. MaskInterrupts flow diagram value to store in CCR RestoreCCR Transfer stored CCR value back into CCR Return Figure 12. RestoreCCR flow diagram EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description 23 ...

Page 24

... Freescale Semiconductor, Inc. AN2302/D EE_RAMFuncs.h The file EE_Emulation contains function prototypes for the files in EE_RAMFuncs.c. Remember to add function prototypes for any user defined interrupt service routines added to EE_RAMFuncs.c. EECALLBACK should be defined in this file to enable EECallBackFunc. This file is included in EE_Emulation.c and VectorTable.c. ...

Page 25

... ACCERR or PVIOL bits were not set when the program or erase command was executed. the ACCERR or PVIOL bits were set when the program or erase command was executed. EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description 25 ...

Page 26

... Freescale Semiconductor, Inc. AN2302/D 26 ProgFlash Write ACCERR | PVIOL to Flash status register to clear error flags if set Write data to progAddr Write command to Flash command register Write CBEIF to Flash status register to launch the command ACCERR or PVIOL flags set ? Command Complete flag EECallBackFunc No EECALLBACK defined Figure 13 ...

Page 27

... This value should be defined if the PLL will be enabled. PLL initialisation code is not included in this example. REFDV If PLL_ENABLED is defined, the value for the REFDV register should be defined so that the BUSCLK_FREQ_KHZ macro can be evaluated. MOTOROLA For More Information On This Product, EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Software Description 27 ...

Page 28

... Freescale Semiconductor, Inc. AN2302/D SYNR If PLL_ENABLED is defined, the value for the SYNR register should be defined so that the BUSCLK_FREQ_KHZ macro can be evaluated. TIMER_PRESCALER This value is only required for the example if interrupts must be serviced during _FACTOR programming (IRQ_DURING_PROG defined). This value defines the Timer Prescaler Factor, permitted values are 16, 32, 64, and 128 ...

Page 29

... Finally, all the RAM function names that are not explicitly called must be listed under the ENTRIES command. MOTOROLA For More Information On This Product, 0x100 TO 0x110; 0x800 TO 0x800; 0x0FD0 TO 0xFFF; INTO DUMMY_RAM; INTO RAM_FUNCS; INTO FLASH_REGS; /* list all RAM function names here */ /* eg EECallBackFunc */ EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Example Project 29 ...

Page 30

... Freescale Semiconductor, Inc. AN2302/D EE Emulation no IRQ This target is provided to build the final application with interrupts disabled during programming and erasure. The files EE_Emulation.c and main.c are compiled and linked together with the ROM library generated by the "EE RAM functions, no IRQ" target, generating the absolute file EE_Emulation.abs. The linker directive " ...

Page 31

... The linker directive "-M" is specified on the linker command line so that a map file is generated. The linker directive "-B" is specified on the linker command line so that an s- record file is generated. MOTOROLA For More Information On This Product, EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Example Project 31 ...

Page 32

... Freescale Semiconductor, Inc. AN2302/D The ROM library is placed at the addresses specified in the associated prm file, EERAMfuncsIRQ.prm, listed below: NAMES END SECTIONS TIMER_REGS = NO_INIT FLASH_REGS = NO_INIT DUMMY_RAM = READ_WRITE RAM_FUNCS = READ_ONLY END PLACEMENT DEFAULT_RAM DEFAULT_ROM,RAM_FUNCTIONS FLASH_REG TIMER_REG END ENTRIES ProgFlash TIMER0_ISR END The RAM functions defined within code segment RAM_FUNCTIONS are placed in section RAM_FUNCS ...

Page 33

... TO 0xFF7F; /* min 2 complete sectors */ 0xC000 TO 0xC3FF; 0xD100 TO 0xD7FF; INTO FLASH_CODE; INTO FLASH_COPY; INTO FLASH_EEPROM; /* EEPROM_STAT must be first */ INTO RAM; INTO FLASH_REGS; INTO TIMER_REGS; /* reset vector only */ /* 0xFFFFD100 + 0xFF00 = 0xD000 = FLASH_COPY_START */ EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Example Project 33 ...

Page 34

... Freescale Semiconductor, Inc. AN2302/D The application code is placed in section FLASH_CODE. The section FLASH_EEPROM is reserved for EEPROM emulation data and contains all the variables in segment EEPROM_VARS. This section must be large enough for EEPROM_BANKS x EEPROM_SIZE_BYTES. The structure containing the Flash control registers is placed in the section FLASH_REGS that is located at the default address ...

Page 35

... Symbol Only" radio button is checked. Click on "Open". The symbolic information from the ROM library should be loaded into the debugger and you should be able to debug your ROM library. EEPROM Emulation for the MC9S12C32 Go to: www.freescale.com AN2302/D Example Project 35 ...

Page 36

... Motorola and the Stylized M Logo are registered in the U.S. Patent and Trademark Office. digital dna is a trademark of Motorola, Inc. All other product or service names are the property of their respective owners. Motorola, Inc Equal Opportunity/Affirmative Action Employer. © Motorola, Inc. 2003 AN2302/D Go to: www.freescale.com ...

Related keywords