LH75400/01/10/11 System-on-Chip Technical Bulletin Creating a Real-Time Clock (RTC) with an On-chip Timer and Software Paul Kovitz, Staff Engineer PURPOSE This document is a supplement to Product Change Information containing Errata for some early SHARP LH75400/01/10/11 series of System-on-Chip parts. When the Errata have been corrected, the information in this Technical Bulletin will no longer apply. These parts may have a suseptibility to ground noise in the the Real-Time Clock (32.768 KHz) oscillator. They therefore may require a software work around for the RealTime Clock (RTC), if the RTC was to be used as a timeof-day clock. This Technical Bulletin will use one of the chip's timers to allow an application to keep track of the elapsed time in seconds or to generate an interrupt when the elapsed time matches a preset value in spite of these difficulties. These methods will work with any revision of the silicon, so the software will not need to be changed. INTRODUCTION If your application does not require the features that the RTC provides, then this Technical Bulletin does not apply to you, and you may safely ground the RTC crystal input pin. If your application is always running in Active or Standby mode, see the Active or Standby mode section. If your application enters any other Low Power mode, see the Low Power Operation section. The 32.768 kHz oscillator has a susceptibility to ground noise induced by the on-chip System Clock (HCLK) and Phase-Locked Loop (PLL). The susceptibility manifests as extra pulses on the oscillator's output. If the SoC is in Active, Standby, or Sleep mode, then the RTC Data Register 0 and RTC Data Register 1 will increment at too high a rate. How fast the clock appears to run depends on the quality of the clock crystal, the passive components, and the attention paid to the circuit board layout. As a consequence, if the RTC is used as a time-ofday clock, the clock will run too fast. The RTC is not recommended for use in the Sleep or Stop1 modes. ACTIVE OR STANDBY MODE Unfortunately, in Active Mode and Standby Mode, the LH75400/01/10/11 PLL and HCLK are always active and therefore always producing the noise that Technical Bulletin will corrupt the RTC. You should not use the RTC if your application only uses Active or Standby Mode. This part of the work around assumes that your application has these properties: * The application requires a way to track the elapsed time in seconds (e.g., the time-of-day), and/or it needs to generate an interrupt when the elapsed time matches a pre-determined elaspsed time. * The application is running an operating system that supports software timers, or the application is not using all three timers that are clocked using HCLK (Timer 0, Timer 1, and Timer 2), or the application is using Timer 0, Timer 1, or Timer 2 to generate a periodic waveform or periodic interrupt. * The application never stops the timer that is generating the operating system clock, or in a non-operating system application, the application never stops the timer that is generating a periodic waveform or periodic interrupt. Software Algorithm Declare a 32-bit global variable that your application will use to keep track of the elapsed time in seconds: rtc_elapsed_time;// UNS_32: 32-bit unsigned type Initialize the 32-bit global variable with the same value you would have used to initialize the RTC load register. If your application requires the same feature provided by the RTC match register, (i.e., Match Register 0 and Match Register 1), declare a second 32-bit global variable to store the match value: UNS_32 rtc_match; If your application uses an operating system with a timer facility, use that facility to increment the 32-bit global variable every second. For example, set up a high priority task that wakes every second, increments the variable, and then suspends. For lower overhead if your application's operating system provides hooks to the timer interrupt, or if your application doesn't have an operating system, program your application's periodic timer interrupt to increment your elapsed time global variable after the appropriate number of timer interrupts have occurred. If you require the interval match facility, use the soft- 1 LH75400/01/10/11 Software RTC ware interrupt feature of the Vectored Interrupt Controller (VIC) to assert the RTC interrupt. This example generates a timer interrupt rate of 100 interrupts per second: (These C examples assume that the interrupt handler functions are called by a wrapper function that saves context on entry and restores context on exit). Note that the HCLK is still running in Standby mode, so even if your application enters Standby mode, the timers will still run and the timer interrupt will still move the SoC from Standby to Active mode. TIMER INTERRUPT EXAMPLE /* Modifications to the periodic timer interrupt handler */ #define TIMER_INTS_PER_SECOND 100 #define VIC_SOFT_INT_PTR ((volatile UNS_32 *)0xFFFFF018) #define VIC_SOFT_INT_CLR_PTR ((volatile UNS_32 *)0xFFFFF01C) #define RTC_ALARM 15 void timer_int_handler(void) { static UNS_32 rtc_ints_remaining = TIMER_INTS_PER_SECOND; /* other timer interrupt declarations go here */ if (0 == --rtc_ints_remaining) { rtc_elapsed_time++; rtc_ints_remaining = TIMER_INTS_PER_SECOND; if (rtc_elapsed_time == rtc_match) { *VIC_SOFT_INT_PTR = 1 << RTC_ALARM; } } /* do what the timer interrupt normally does */ } /* RTC Alarm Software Interrupt */ void rtc_soft_alarm(void) { /* clear the alarm interrupt */ *VIC_SOFT_CLR_PTR = 1 << RTC_ALARM; /* do the processing for the alarm */ } 2 Technical Bulletin Software RTC LH75400/01/10/11 LOW POWER OPERATION 2. The application that requires support from the RTC more than any other is the battery-powered application that needs to keep track of the time of day and date even when every other chip function is off. The LH75400/01/10/11 provides three very low power modes: Standby, Stop1 and Stop2. Due to the RTC oscillator errata, the only Low Power mode available to use this function is Stop2. Fortunately for battery-powered applications, Stop2 is the lowest Power mode of all these Soc's operating modes. If your application needs to use the RTC Match Interrupt to exit, copy the Global Elapsed Seconds Match variable into the RTC match register (i.e., copy the least significant 16 bits into Match Register 0, and then copy the most significant 16 bits into Match Register 1). 3. Poll the RTC Data Register until it contains a value that is 1 more than the value you loaded into the RTC Match Register. This step ensures that the value you wrote to the RTC Load Register has been transferred from the HCLK domain into the RTC clock domain. 4. Write 0b100 to the PWR DWN SEL bit field of the Reset, Clock and Power Controller (RCPC) Control Register to enter Stop2 mode. Make sure the lock bit of the RCPC Control Register is set before trying to write to the RCPC or else writes to the RCPC will be ignored. In Active or Standby mode, use the algorithims to track time described previously in `Active or Standby mode'. Use this algorithim with the RTC to count elapsed seconds in Low Power mode: 1. Copy the global variable that counts elapsed time into the RTC load register (i.e., copy the least significant 16 bits into Load Register 0, and then copy the most significant 16 bits into Load Register 1). RTC STOP2 MODE ENTRY EXAMPLE #define RTC_DATA0_PTR ((volatile UNS_32 *)0xFFFE0000) #define RTC_DATA1_PTR ((volatile UNS_32 *)0xFFFE0004) #define RTC_LOAD0_PTR ((volatile UNS_32 *)0xFFFE0014) #define RTC_LOAD1_PTR ((volatile UNS_32 *)0xFFFE0018) #define RTC_MATCH0_PTR ((volatile UNS_32 *)0xFFFE0008) #define RTC_MATCH1_PTR ((volatile UNS_32 *)0xFFFE000C) #define RCPC_CTRL_PTR ((volatile UNS_32 *)0xFFFE2000) #define RCPC_PWR_DWN_SEL_BIT 2 #define RCPC_STOP2_VAL 4 #define RCPC_LOCK_BIT 9 #define HALFWORD_MASK 0xFFFF #define HALFWORD_SIZE 16 void enter_stop2_mode(void) { /* make sure the RCPC is unlocked */ *RCPC_CTRL_PTR |= (1 << RCPC_LOCK_BIT); /* prepare the RTC registers */ *RTC_LOAD0_PTR = rtc_elapsed_time & HALFWORD_MASK; *RTC_LOAD1_PTR = rtc_elapsed_time >> HALFWORD_SIZE; *RTC_MATCH0_PTR = rtc_match & HALFWORD_MASK; *RTC_MATCH1_PTR = rtc_match >> HALFWORD_SIZE; /* make sure the load value has transferred to the RTC Data Register */ while (*RTC_DATA0_PTR < rtc_elapsed_timer + 1) ; /* wait */ /* enter Stop2 mode */ *RCPC_CTRL_PTR |= RCPC_STOP2VAL << RCPC_PWR_DWN_SEL_BIT; } Technical Bulletin 3 LH75400/01/10/11 To exit Stop2 mode, use either an external interrupt (one of the INT0-INT6 pins) or the RTC_ALARM interrupt. The interrupt handler that exits Stop2 mode should do the following: * Clear the interrupt that caused the exit from Stop2 mode. * Read the RTC Data Register until the value read from the RTC Data Register is greater than the value that was written prior to entering Stop2 mode. * Write this value into the global variable that counts Elapsed Time. #define Software RTC * Perform any other processing the interrupt service routine needs to do. * Exit the interrupt. At this point, keep track of the elapsed time using the procedure described in `Active or Standby mode'. RTC STOP2 MODE EXIT EXAMPLE This example exits Stop2 mode on RTC_ALARM interrupt: (This assumes the interrupt handler is called by a wrapper that saves the registers on entry and restores them on exit.) RTC_EOI_PTR ((volatile UNS_32 *)0xFFFE0010) void rtc_alarm_handler(void) { UNS_32 new_time; /* clear the RTC_ALARM interrupt */ *RTC_EOI_PTR = 1; /* get the time from the RTC */ do { new_time = *RTC_DATA0_PTR; new_time |= *RTC_DATA1_PTR << HALFWORD_SIZE; } while (new_time < rtc_elapsed_time + 1); rtc_elapsed_time = new_time; /* do processing required by the alarm */ } 4 Technical Bulletin Software RTC LIMITATIONS For most applications that require a real-time clock, the software methods described here will allow acceptable performance. Applications that are using all three on-chip timers for non-periodic timing functions (e.g., are only used to time brief intervals or are used with an aperiodic external clock) cannot use this software method. Technical Bulletin LH75400/01/10/11 Your application cannot use the RTC ALARM function in Active mode or Standby mode. Your application cannot use the RTC with Sleep or Stop1 mode. Your application will take as long as 1 second to enter and exit Stop2 mode due to RTC synchronization. Your application could gain as much as 1 second of error in the elapsed time every time it enters or exits Stop2 mode. 5 Software RTC LH75400/01/10/11 SPECIFICATIONS ARE SUBJECT TO CHANGE WITHOUT NOTICE. Suggested applications (if any) are for standard use; See Important Restrictions for limitations on special applications. See Limited Warranty for SHARP's product warranty. The Limited Warranty is in lieu, and exclusive of, all other warranties, express or implied. ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR USE AND FITNESS FOR A PARTICULAR PURPOSE, ARE SPECIFICALLY EXCLUDED. In no event will SHARP be liable, or in any way responsible, for any incidental or consequential economic or property damage. NORTH AMERICA EUROPE JAPAN SHARP Microelectronics of the Americas 5700 NW Pacific Rim Blvd. Camas, WA 98607, U.S.A. Phone: (1) 360-834-2500 Fax: (1) 360-834-8903 www.sharpsma.com SHARP Microelectronics Europe Division of Sharp Electronics (Europe) GmbH Sonninstrasse 3 20097 Hamburg, Germany Phone: (49) 40-2376-2286 Fax: (49) 40-2376-2232 www.sharpsme.com SHARP Corporation Electronic Components & Devices 22-22 Nagaike-cho, Abeno-Ku Osaka 545-8522, Japan Phone: (81) 6-6621-1221 Fax: (81) 6117-725300/6117-725301 www.sharp-world.com TAIWAN SINGAPORE KOREA SHARP Electronic Components (Taiwan) Corporation 8F-A, No. 16, Sec. 4, Nanking E. Rd. Taipei, Taiwan, Republic of China Phone: (886) 2-2577-7341 Fax: (886) 2-2577-7326/2-2577-7328 SHARP Electronics (Singapore) PTE., Ltd. 438A, Alexandra Road, #05-01/02 Alexandra Technopark, Singapore 119967 Phone: (65) 271-3566 Fax: (65) 271-3855 SHARP Electronic Components (Korea) Corporation RM 501 Geosung B/D, 541 Dohwa-dong, Mapo-ku Seoul 121-701, Korea Phone: (82) 2-711-5813 ~ 8 Fax: (82) 2-711-5819 CHINA HONG KONG SHARP Microelectronics of China (Shanghai) Co., Ltd. 28 Xin Jin Qiao Road King Tower 16F Pudong Shanghai, 201206 P.R. China Phone: (86) 21-5854-7710/21-5834-6056 Fax: (86) 21-5854-4340/21-5834-6057 Head Office: No. 360, Bashen Road, Xin Development Bldg. 22 Waigaoqiao Free Trade Zone Shanghai 200131 P.R. China Email: smc@china.global.sharp.co.jp SHARP-ROXY (Hong Kong) Ltd. 3rd Business Division, 17/F, Admiralty Centre, Tower 1 18 Harcourt Road, Hong Kong Phone: (852) 28229311 Fax: (852) 28660779 www.sharp.com.hk Shenzhen Representative Office: Room 13B1, Tower C, Electronics Science & Technology Building Shen Nan Zhong Road Shenzhen, P.R. China Phone: (86) 755-3273731 Fax: (86) 755-3273735 (c)2003 by SHARP Corporation Reference Code SMA03004