Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller AN020101-0804 ZiLOG Worldwide Headquarters * 532 Race Street * San Jose, CA 95126 Telephone: 408.558.8500 * Fax: 408.558.8300 * www.ZiLOG.com This publication is subject to replacement by a later edition. To determine whether a later edition exists, or to request copies of publications, contact: ZiLOG Worldwide Headquarters 532 Race Street San Jose, CA 95126 Telephone: 408.558.8500 Fax: 408.558.8300 www.zilog.com ZiLOG is a registered trademark of ZiLOG Inc. in the United States and in other countries. All other products and/or service names mentioned herein may be trademarks of the companies with which they are associated. Information Integrity The information contained within this document has been verified according to the general principles of electrical and mechanical engineering. Any applicable source code illustrated in the document was either written by an authorized ZiLOG employee or licensed consultant. Permission to use these codes in any form, besides the intended application, must be approved through a license agreement between both parties. ZiLOG will not be responsible for any code(s) used beyond the intended application. Contact the local ZiLOG Sales Office to obtain necessary license agreements. Document Disclaimer (c)2004 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. Except with the express written approval ZiLOG, use of information, devices, or technology as critical components of life support systems is not authorized. No licenses or other rights are conveyed, implicitly or otherwise, by this document under any intellectual property rights. AN020101-0804 Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller iii Table of Contents List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .v Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Z8 Encore! XPTM 4K Series Flash Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . 1 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Developing the Application with Z8 Encore! XPTM MCU . . . . . . . . . . . . . . . . . . . 3 Software Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Demonstrating the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Appendix A--References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Appendix B--Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 AN020101-0804 Table of Contents Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller iv List of Figures Figure 1. Preprocessor Definitions in Project Settings Dialog Box . . . . . . . . . . . 5 AN020101-0804 List of Figures Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller v List of Tables Table 1. List of References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 AN020101-0804 List of Tables Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 1 Abstract This Application Note discusses the fail-safe clock feature of the Z8 Encore! XPTM 4K Series microcontroller. The source code file associated with this Application Note, AN0201-SC01.zip, demonstrates the fail-safe clock feature, and is available on zilog.com. Z8 Encore! XPTM 4K Series Flash Microcontrollers ZiLOG's Z8 Encore!(R) products are based on the new eZ8 CPU and introduce Flash memory to ZiLOG's extensive line of 8-bit microcontrollers. Flash memory in-circuit programming capability allows for faster development time and program changes in the field. The high-performance register-to-register based architecture of the eZ8 core maintains backward compatibility with ZiLOG's popular Z8 MCU. Z8 Encore!(R) MCUs combine a 20 MHz core with Flash memory, linear-register SRAM, and an extensive array of on-chip peripherals. The Z8 Encore! XPTM 4K Series of devices support up to 4 KB of Flash program memory and 1 KB register RAM. An on-chip temperature sensor allows temperature measurement over a range of -40C to +105 C. These devices include two enhanced 16-bit timer blocks featuring PWM and Capture and Compare capabilities. An on-chip Internal Precision Oscillator (5 MHz/32 kHz) can be used as a trimmable clock source requiring no external components. The Z8 Encore! XPTM devices include 128 bytes of Non Volatile Data Storage (NVDS) memory where individual bytes can be written or read. The full-duplex UART, besides providing serial communications and IrDA encoding and decoding capability, also supports multidrop address processing in hardware. The rich set of on-chip peripherals make the Z8 Encore! XPTM MCUs suitable for a variety of applications including motor control, security systems, home appliances, personal electronic devices, and sensors. AN020101-0804 Abstract Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 2 Discussion The Z8 Encore! XPTM Series of devices use five possible user-selectable clocking schemes, as listed below. * * * * * On-chip precision trimmed RC oscillator On-chip oscillator using off-chip crystal or resonator On-chip oscillator using external RC network External clock drive On-chip low precision Watch-Dog Timer oscillator The user must select one of the above oscillators by writing appropriate hex values to the Oscillator Control Register (OSCCTL). The Z8 Encore! XPTM Series of devices generate an interrupt when the primary oscillator fails, provided the fail-safe clock feature is enabled. To maintain system functionality in this situation, the clock failure recovery circuitry automatically forces the Watch-Dog Timer (WDT) oscillator to drive the system clock. The Watch-Dog Timer oscillator must be enabled to allow the recovery. Although this oscillator runs at a much lower frequency than the original system clock, the CPU continues to operate as before. The user can add the software routines to the clock failure vector to provide either a remedy for the oscillator failure or issue a clock failure alert. This automatic switch-over is not available if the Watch-Dog Timer is the primary oscillator. It is also unavailable if the Watch-Dog Timer oscillator is disabled. The following section discusses the software implementation of the fail-safe clock option. Note: For a detailed explanation of the OSCCTL and the WDT peripheral, refer to the Z8 Encore! XPTM 4K Series Product Specification (PS0228). AN020101-0804 Discussion Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 3 Developing the Application with Z8 Encore! XPTM MCU This section discusses the software implementation and demonstration of the failsafe clock feature in detail. Software Implementation System clock selection, GPIO and timer initialization, and the oscillator failure interrupt routine are discussed below. Selecting the System Clock The init_external_systemclock () function selects the type of oscillator used with the Z8 Encore! XPTM 4K Series MCU. The Z8 Encore! XPTM 4K Series Development Board uses an 8 MHz off-chip ceramic resonator. The Oscillator Control Register must be unlocked before writing. Writing the two-step sequence E7h followed by 18h unlocks the Oscillator Control Register. The register automatically locks upon successful completion of a register write to the OSCCTL. The following sequence writes to the Oscillator Control Register: OSCCTL = 0xE7; OSCCTL = 0x18; OSCCTL = 0x72; // Unlock sequence for OSCCTL write With the above setting, the internal precision oscillator is enabled, the WDT oscillator is enabled, failure detection and recovery of the primary oscillator is enabled, and the external RC oscillator is enabled as the system clock. Initializing the GPIO The Development Board used to implement the fail-safe clock option has three LEDs --- D2, D3 and D4. In this example, the GPIO pins are configured as output pins to switch the D2, D3, and D4 LEDs on or off. The D4 LED is connected to Port C, pin 3 (PC3), the D3 LED is connected to Port A, pin 7 (PA7), and the D2 LED is connected to Port A, pin 6 (PA6). For schematic details of these connections, please refer to the Z8 Encore! XPTM 4K Series Development Kit User Manual (UM0166). After these connections are made, the init_led_gpio() function initializes the GPIO ports. On the target board, PA6 and PA7 control the D2 and D3 LEDs, respectively, and PC3 controls the D4 LED. As a result of this initialization, each of these port pins are configured as output ports. Port C pins provide a current sourced output capable of driving an LED without requiring an external resistor. The current output is programmed to source 13 mA. AN020101-0804 Developing the Application with Z8 Encore! XPTM MCU Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 4 This mode is enabled through the Alternate Function Subregisters. For details about the Alternate Function Subregisters, please refer to the Z8 Encore! XPTM 4K Series Product Specification (PS0228). Initializing the Timer Timer0 is initialized to generate an interrupt every 4.05 milliseconds with an external oscillator frequency of 8 MHz. The init_timer0(void) function loads appropriate values to the T0H, T0L, T0RH and T0RL registers, loads the prescale value, and selects the timer-operating mode. The interrupt service routine (ISR) for the timer void isr_timer0(void) sets the ProgStat variable to RUN mode. The Oscillator Failure Interrupt Routine The SET_VECTOR (POTRAP, Oscillator_Failure) function called in the main routine initializes the vector location PORTAP (003Ah) with the address of the oscillator_failure(void) interrupt routine. When the primary oscillator fails, the program jumps to the oscillator_failure (void) ISR which disables the external system clock and sets the internal precision clock either to 5.5 MHz or 32 kHz based on the #define statement in the project setting. Then, the program execution continues as before. The details of this operation is demonstrated in the next section. Demonstrating the Application This section lists the equipment used and the procedure followed to demonstrate the fail-safe clock feature. Equipment Used * * Z8 Encore! XPTM Development Board PC with HyperTerminal application Procedure The procedure to demonstrate the fail-safe clock feature is provided below. 1. Download and extract the AN0201-SC01.zip file. 2. Launch ZDS II-Z8 Encore!(R) and open the fail-safe clock project from the extracted zip folder. 3. Click Project Settings. The Project Settings dialog box appears. 4. Select the Preprocessor category from the C tab. AN020101-0804 Developing the Application with Z8 Encore! XPTM MCU Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 5 5. Add the _LOW_FREQ definition to the Preprocessor Definitions field. This definition is required to select 5.5 MHz as the frequency of the internal precision oscillator. Figure 1 depicts the Preprocessor Definitions field in the Project Settings dialog box. Figure 1. Preprocessor Definitions in Project Settings Dialog Box 6. Compile and build the code. 7. Download the .lod output file to the target Z8 Encore! XPTM board, using ZDS II. 8. Execute the code. 9. Observe that the D2, D3, and D4 LEDs flash sequentially. 10. Short pin 7 of the Z8 Encore! XPTM MCU (U5) to GND to create a clock failure condition. As a result, an oscillator failure interrupt is immediately generated and the internal precision oscillator begins to function. 11. Observe that the rate at which the three LEDs flash is reduced when compared to the observation made in step 9. AN020101-0804 Developing the Application with Z8 Encore! XPTM MCU Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 6 12. Remove the short between pin 7 of U5 and ground. 13. To test the fail-safe clock feature at the internal precision oscillator frequency of 32 kHz, open the project and remove the _LOW_FREQ definition in the Preprocessor Definitions field. As a result of this modification, 32 kHz is selected as the frequency of the internal precision oscillator. 14. Recompile and build the project. Download the .lod output file to the target board and execute the program. 15. Short pin 7 of U5 to ground, and observe the frequency at which the LED flashes. The rate at which these LEDs flash should be greatly reduced compared to the observations made in steps 9 and 11. Summary This Application Note discusses the fail-safe clock feature of the Z8 Encore! XPTM 4K Series microcontroller. With this fail-safe clock feature enabled, both program execution and system operation continue even if the external oscillator fails. The user can generate an audiovisual alarm that indicates the condition of the primary clock failure. As a result, the user can decide whether to allow the system to operate at a lower frequency, or to switch the system off for maintenance or troubleshooting purposes. AN020101-0804 Summary Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 7 Appendix A--References Further details about the Z8 Encore!(R) products can be found in the references listed in Table 1. Table 1. List of References Topic Document Name eZ8 CPU eZ8 CPU User Manual (UM0128) Z8 Encore! XPTM 4K Series Microcontrollers Z8 Encore! XPTM 4K Series Product Specification (PS0228) Z8 Encore! XPTM 4K Series Development Kit User Manual (UM0166) ZDS II-IDE AN020101-0804 ZiLOG Developer Studio ll-Z8 Encore!(R) User Manual (UM0130) Appendix A--References Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 8 Appendix B--Source Code This appendix provides a listing of all source code associated with the application described in this document. A companion source code file, AN0201-SC01.zip, is available on zilog.com. The following C file is listed in this section: * main.c /* ********************************************************************** * File: main.c * Description: This program demonstrates how the system designed with * Z8Encore XP can switch over to fail-safe clock mode when the primary * oscillator fails. For testing purposes, the primary oscillator is * made to fail by connecting the XP F042A clock input to GND (e.g. * connect the J2 pin{2, 4, 6, 8, 10, 12, 14, 16} to pin 7 of the U5). * When connected, a clock failure will occur and the system clock will * switch from the external 8MHz clock (assembled on the evaluation * board) to an internal 5.5MHz clock or 32KHz clock based on the * selection. Observe the blinking rate of the D2, D3, and D4 LEDs on the * evaluation board, which slows down very little when switched to 5.5 * MHz and operates much more slowly when switched to 32KHz. * * Copyright 2004 ZiLOG Inc. ALL RIGHTS RESERVED. * * The source code in this file was written by an * authorized ZiLOG employee or a licensed consultant. * The source code has been verified to the fullest * extent possible. * * Permission to use this code is granted on a royalty-free * basis. However users are cautioned to authenticate the * code contained herein. * * ZiLOG DOES NOT GUARANTEE THE VERACITY OF THE SOFTWARE. ********************************************************************** */ #include #define #define #define #define RUN BUSY OSC_UNLOCK_SEQ1 OSC_UNLOCK_SEQ2 AN020101-0804 0 1 0xE7 0x18 Appendix B--Source Code Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 9 int ProgStat = BUSY; // Program status variable /********************************************************************/ void init_led_gpio(void) { PAADDR = PACTL &= 0x01; 0x3F; // PA Data Dir = // PA6-PA7 as Output PCADDR = PCCTL &= LEDEN = LEDLVLH = LEDLVLL = 0x01; 0xF7; 0x08; 0x08; 0x00; // // // // PC Data Dir PC3 as Output PC3 uses LED drive PC3 Led Driver current = 13ma } /********************************************************************/ // Turns off ALL LEDs void turn_off_led(void) { PAOUT |= 0xC0; PCOUT |= 0x08; } // Turn off PA6-PA7 leds // Turn off PC3 Led /********************************************************************/ // Timer 0 interrupt that is shifting LEDs #pragma interrupt void isr_timer0(void) { ProgStat = RUN; } // ********************************************************************** // Primary clock oscillator fail trap interrupt #pragma interrupt void Oscillator_Failure(void) { AN020101-0804 Appendix B--Source Code Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 10 OSCCTL = OSC_UNLOCK_SEQ1; OSCCTL = OSC_UNLOCK_SEQ2; #if defined(_LOW_FREQ) OSCCTL = 0xB0; #else OSCCTL = 0xB1; #endif // Unlock sequence for OSCTL write // Crystal oscillator is disabled // Internal precision oscillator as // system clock at 5.5MHz // // // // // // // // Internal precision oscillator is enabled. WDT oscillator enabled Failure detection and recovery of primary oscillator is enabled Internal precision oscillator as system clock at 32kHz for demonstration purposes } // ********************************************************************** //Initialize Timer0 void init_timer0(void) { SET_VECTOR(TIMER0, isr_timer0); T0H = 0xFF; // Timer High T0L = 0xFF; // Timer Low T0RH = 0x7F; T0RL = 0xFF; // Reload Compare High // Reload Compare Low T0CTL1 = 0xE9; IRQ0ENH |= 0x20; IRQ0ENL &= 0xDF; // Continuous/Prescale // Set Interrupt Priority Nominal // Set Interrupt Priority Nominal } // ********************************************************************** // Initialize system clock source to use external crystal void init_external_systemclock() AN020101-0804 Appendix B--Source Code Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 11 { OSCCTL = OSC_UNLOCK_SEQ1; OSCCTL = OSC_UNLOCK_SEQ2; OSCCTL =0x72; // Unlock sequence for OSCTL write // // // // // // // // Crystal oscillator is enabled Internal precision oscillator is disabled WDT oscillator enabled Failure detection and recovery of primary oscillator is enabled Crystal or external RC oscillator as system clock } // ********************************************************************** // Main program begins here // This program blinks LED-3 on the evaluation board main () { int ledstate = 1; // LED display state SET_VECTOR(POTRAP, Oscillator_Failure);// Pass the vector number //and the ISR address init_external_systemclock(); // Initialize external // system clock source turn_off_led(); // Turns off ALL LED's init_led_gpio(); // Initializes LED ports // (Port A and Port C) DI(); // Disable Interrupts init_timer0(); // Initialize Timer-0 EI(); while(1) { if(ProgStat != BUSY) // Enable Interrupts // Infinite while loop // Wait for Timer Interrupt { switch(ledstate) { case 1: turn_off_led(); break; case 2: PCOUT &= 0xF7; break; AN020101-0804 // Shift LED state machine // Turns off ALL LED's // Turn On LED Red Appendix B--Source Code Application Note Fail-Safe Clock Feature of the Z8 Encore! XPTM Microcontroller 12 case 3: PAOUT &= 0x7F; break; case 4: PAOUT &= 0xBF; break; } ProgStat = BUSY; ledstate++; if(ledstate == 5) ledstate = 1; // Turn On LED Yellow // Turn On LED Green // Change to wait state // Shift LED // Start shifting LEDs all // again } } // End of main program // End of file AN020101-0804 Appendix B--Source Code