Application Note EEPROM Handling in Remote Control Devices Using the Crimzon® MCU

AN026201-0707

Abstract choice when selecting the right device for your appli- cation. At Zilog, we are continually evolving our In many remote control applications, an EEPROM is Crimzon microcontroller roadmap so that you can used to permanently store important . Care must expect new, smaller packages and added-feature sili- be taken, however, to prevent data corruption caused con that will further enhance your product offerings. by incomplete WRITE cycles, power interruptions, and external noise. Discussion This application note focuses on proper use of the An EEPROM is commonly used in remote control ® Crimzon MCU in remote control devices to ensure applications to save data when the power is off. in . Focus is on the opera- There are numerous EEPROM suppliers in the mar- tional flow of the Crimzon MCU. ket. Most use the standard I2C communication for the communication interface. (Refer to the EEPROM Note: The source code associated with this specification for details on using the I2C interface.) reference design is available under Crimzon Applications Code Library Table 1 provides a sample of the Atmel serial in Application Sample Libraries on EEPROM product list. www.zilog.com. In remote control devices, EEPROMs are used for Crimzon Microcontrollers two primary purposes: With one of the broadest device databases available • One is to store the basic code numbers for user- today, Zilog's Crimzon line of microcontrollers offers programmed data and for factory-loaded data. the design flexibility required to create a wide variety A 1K EEPROM is usually adequate for this of infrared products. Our living database of infrared purpose. remote control codes, ZBASE®, allows you to sup- port the latest infrared applications on the market, • The other is to store the learning signal data in including universal remote controls, wireless key- a learning remote control. For such purposes, boards, wireless mice, and game controllers. Zilog’s EEPROMs from 4 K to 32 K are used depend- advanced algorithms squeeze more ing on the number of keys to be learned. device data into the available ROM. You get more Different EEPROMs can have different page sizes device support for your money because Crimzon (from 4 to 256 bytes) and address pins (A0 - microcontrollers are simpler to integrate. A2). Designers must take care to study the device’s interface method and select the appropriate page Zilog’s broad range of cost-effective, low-power read/write method. Crimzon microcontrollers provides the ability to meet the requirements of a range of device footprints: 20-pin, 28-pin, 40-pin and 48-pin in DIP, SOIC and SSOP. Memory is available in sizes from 4 K to 64 K ROM and up to 128 K OTP, giving you greater

AN026201-0707 Page 1 of 8 EEPROM Handling in Remote Control Devices Using the Crimzon® MCU

Table 1. Atmel's 2-Wire Serial EEPROM Family

Size Page Size Maximum Addresses Device (Bytes) (Bytes) Per Bus Used AT24C11 1 K 4 1 None AT24C01A 1 K 8 8 A0, A1, A2 AT24C02 1 K 8 8 A0, A1, A2 AT24C04 4 K 16 4 A1, A2 AT24C08A 8 K 16 2 A2 AT24C16A 16 K 16 1 None AT24C164 16 K 16 8 A0, A1, A2 AT24C32A 32 K 32 8 A0, A1, A2 AT24C64A 64 K 32 8 A0, A1, A2 AT24C128 128 K 64 4 A0, A1 AT24C128B 128 K 64 8 A0, A1, A2 AT24C256 256 K 64 4 A0, A1 AT24C256B 256 K 64 8 A0, A1, A2 AT24C512 512 K 128 4 A0, A1 AT24C512B 512 K 128 8 A0, A1, A2 AT24C1024 1 M 256 2 A1

Hardware Architecture • SCL: Serial Clock Input The Crimzon MCU 8-pin small package is shown in • SDA: Serial Data Figure 1. The SCL is driven either directly by an MCU output pin with push-pull, or by an open-drain output using a 2 KΩ pull-up resistor. Ports P34 or P37 are push- pull outputs that can be used for that purpose. Any pin of ports P20-P27 can also be used as push-pull output or open-drain with 2 KΩ pull-up resistor.

The SDA must be driven by an open-drain output with 2 KΩ pull-up resistor. The EEPROM sends an Figure 1. Crimzon MCU, 8-Pin Small Package ACK signal by pulling the SDA pin down at the 9th bit period. The SDA pin must therefore be a bidirec- The pins provide access to the following functions: tional pin that supports the open-drain output config- uration. Any pin of Crimzon MCU ports P20-P27 can be used to drive the SDA pin. Configure the port as • A0 - A2: Address Inputs open-drain with a 2 KΩ pull-up resistor during data • WP: Write Protect pulses. After sending 8 clocks, the MCU pin must be

AN026201-0707 Page 2 of 8 EEPROM Handling in Remote Control Devices Using the Crimzon® MCU

configured as input pin to read the ACK at the 9th clock. See Figure 2.

Figure 2. Output Acknowledge Timing Diagram

Software Implementation for Read/ interfacing the Crimzon MCU to an EEPROM via Write Operations I2C.

An EEPROM supports random access, sequential Figure 3 illustrates the write operation. Figure 4 access, and page access. (Refer to your EEPROM illustrates the page write operation. A flowchart for specification for further information.) Zilog applica- EEPROM handling is provided in Appendix A – tion note AN0036 also provides example code for Flowcharts.

Figure 3. Byte Write Operation

Figure 4. Page Write Operation

AN026201-0707 Page 3 of 8 EEPROM Handling in Remote Control Devices Using the Crimzon® MCU

Prevention of Data Corruption 1. Eliminate single-key-triggered WRITE pro- cesses. An EEPROM is designed for permanent . The data is preserved even though the power is In some applications, a manufacturer may want removed during battery replacement in remote con- the remote to remember the device mode (TV, trol. However, there is still a risk of corrupting the VCR, DVD, Satellite, Cable, Audio, etc.). The EEPROM data during a write operation. device mode is usually modified when a device key is pressed. In that case, store the device Causes of data corruption in EEPROMs include: mode status in RAM only. Avoid an EEPROM WRITE after a single key press and you will 1. Incomplete Write Cycle avoid data corruption if the device suddenly loses Vcc power. The I2C interface is a static bus, and the MCU can control the speed of that bus while per- For example, the user can easily press a device forming other activities. However, if the MCU key by accident while removing the battery. is interrupted and loses track of the EEPROM With a single-key-triggered WRITE procedure, writing procedure, the EEPROM data may get the accidental key press triggers a WRITE of corrupted. device mode data into EEPROM. Since the battery is being removed, however, the Vcc 2. Power Interrupt power naturally drops below the RESET volt- An EEPROM needs between 5 ms and 10 ms age for the Crimzon MCU. The MCU then to write data into its permanent storage area. If stops operation. As Vcc drops below the the power is interrupted prior to the WRITE EEPROM operating voltage, data integrity is completion, the new data will be lost. Existing jeopardized. Data will likely be corrupted. data may also be corrupted. It is important to ensure that the power is stable during the Trigger an EEPROM WRITE process by a EEPROM WRITE cycle. multiple key press sequence and such acciden- tal can be almost completely avoided. 3. External Noises/Pulses 2. Low Voltage Detection Other events may inject extra pulses into the I2C bus. For example, if the I2C bus is multi- The Low Voltage Detection flag (bit 1 of LVD plexed with a key scanning matrix, pulses can register 0x0C in bank 0x0D) will be set when be injected into the bus when a user presses a the voltage is lower than the VLVD (typically key on the keypad. 2.4 V) when low voltage detection is enabled. Write only to the EEPROM when voltage is Prevention of EEPROM Corruption During high. WRITE Operations Quality Check on EEPROM data There are simple rules to follow that will help you avoid EEPROM data corruption. The chance of EEPROM data corruption can be min- imized by a WRITE at the right time. The EEPROM should be written only when the pro- gram modifies the data. For example, if the When the EEPROM is read, you must still verify that EEPROM contains the user-programmed code num- the data is totally reliable. Different EEPROM sizes ber of each device, perform a WRITE to the have different page sizes for WRITE and READ EEPROM only when the programmed code number operations. If the PAGE WRITE process stops before is modified, or upon power-up initialization. it has completed, the whole page of data will be cor-

AN026201-0707 Page 4 of 8 EEPROM Handling in Remote Control Devices Using the Crimzon® MCU

rupted. If the power is lost before the required 5 ms to 0x5A). If the RAM data is valid, there is no need to 10 ms WRITE cycle, some data (pages or bytes) will read the EEPROM. Proceed with normal operation. be lost. Such situations occur during, Stop Mode Recovery, Watchdog timeout reset, and when the RESET pin 1. and Signature Byte goes LOW. A checksum byte and a signature byte can be If the RAM data is not valid, the MCU should read inserted into a page of data (e.g. 16 byte). If the the EEPROM by page. Verify the checksum and sig- firmware writes a page each time, then do not nature byte for the data validity. If the EEPROM data read the EEPROM as a single byte. Instead, is valid, copy the EEPROM data into RAM and treat read a page and verify the checksum and signa- it as valid data. Such situations occur after battery ture byte to ensure that the data is valid. replacement. If the corrupted data are all 0x00, the test will If neither the RAM nor EEPROM data are valid, per- also match the checksum of 0x00. Therefore, form a complete initialization for the RAM and use a signature byte (e.g. 0xA5) to ensure that EEPROM. This situation happens on the first power the checksum won't be the same as the cor- up of the remote. It is also applicable in the first rupted data. These checksum and signature power-up if the manufacturer uses a pre-programmed bytes are applicable for both EEPROM data EEPROM. and RAM data verification.

When the jumper selection state is needed, read the 2. Software Check on Validity of Data jumper from time to time instead of storing the data The firmware should check whether the data is into RAM or the EEPROM. The exception is that valid. For example, if there are only 200 TVs when the jumper is multiplexing with other peripher- listed in the codeset, any code number that is als, it can only be read at the power-up stage. In that higher than the known maximum should be instance, the MCU must store the jumper data into treated as invalid data. RAM or EEPROM. The chance of reading a wrong jumper is much lower than the potential for corrupt- 3. Minimize the Number of EEPROM READs ing data in RAM or in the EEPROM. After the Crimzon MCU reads the EEPROM, data is stored in internal RAM. The MCU can Summary obtain all the subsequent information it needs from the data stored in RAM. You can there- You can simplify remote control design and opera- fore minimize the number of EEPROM tion by using the techniques described in this applica- accesses. Doing so reduces the time required to tion note. By applying the principles described, the access data, and minimizes the possibility of Crimzon MCU will ensure that data integrity is main- reading the wrong data from external memory. tained. When the MCU needs to read data from the EEPROM, check the validity of that data. Take appropriate precautions when the MCU must write Power Up Initialization and Operation data to the EEPROM. By ensuring data integrity, the Upon power-up or reset, the MCU should check remote control will operate as it should, and the end internal RAM data to see whether it is valid. The user will be a satisfied customer. internal RAM data check should consist of the check- sum (checksum of all useful data including code number, critical status, user/factory selection from jumper of user interface) and a signature byte (e.g.

AN026201-0707 Page 5 of 8 EEPROM Handling in Remote Control Devices Using the Crimzon® MCU

Reference Documentation The documents associated with the Crimzon MCU MCU available on www.zilog.com are provided below: • Using the Z8 MCU as an I2C Bus Master, AN0036 • Z8 CPU User Manual (UM0016)

AN026201-0707 Page 6 of 8 EEPROM Handling in Remote Control Devices Using the Crimzon® MCU

Appendix A – Flowcharts A flowchart illustrating program flow for EEPROM handling in a remote control is provided in Figure 5.

Start

Yes

RAM Valid?

No

Yes EEPROM Load EEPROM data to valid? RAM

No

Initialize RAM

Normal operation

End

Figure 5. EEPROM Handling Routine for a Remote Control Application

AN026201-0707 Page 7 of 8 EEPROM Handling in Remote Control Devices Using the Crimzon® MCU

Warning: DO NOT USE IN LIFE SUPPORT

LIFE SUPPORT POLICY ZILOG'S PRODUCTS ARE NOT AUTHORIZED FOR USE AS CRITICAL COMPONENTS IN LIFE SUPPORT DEVICES OR SYSTEMS WITHOUT THE EXPRESS PRIOR WRITTEN APPROVAL OF THE PRESIDENT AND GENERAL COUNSEL OF ZILOG CORPORATION.

As used herein Life support devices or systems are devices which (a) are intended for surgical implant into the body, or (b) support or sustain life and whose failure to perform when properly used in accordance with instructions for use provided in the labeling can be reasonably expected to result in a significant injury to the user. A critical component is any component in a life support device or system whose failure to perform can be reasonably expected to cause the failure of the life support device or system or to affect its safety or effectiveness.

Document Disclaimer ©2007 by Zilog, Inc. All rights reserved. Information in this publication concerning the devices, applications, or technology described is intended to suggest possible uses and may be superseded. ZILOG, INC. DOES NOT ASSUME LIABILITY FOR OR PROVIDE A REPRESENTATION OF ACCURACY OF THE INFORMATION, DEVICES, OR TECHNOLOGY DESCRIBED IN THIS DOCUMENT. ZILOG ALSO DOES NOT ASSUME LIABILITY FOR INTELLECTUAL PROPERTY INFRINGEMENT RELATED IN ANY MANNER TO USE OF INFORMATION, DEVICES, OR TECHNOLOGY DESCRIBED HEREIN OR OTHERWISE. The information contained within this document has been verified according to the general principles of electrical and mechanical engineering.

Crimzon is a registered trademarks of Zilog, Inc. All other product or service names are the property of their respective owners.

AN026201-0707 Page 8 of 8 8