Xmega IEC60730 Class B Library  1.0
 All Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
frequency/UserApplication.c
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation.*/
2 /**
3  * \file
4  *
5  * \brief This is the demo application for the CPU frequency test.
6  *
7  * The test basically checks periodically that the relationship
8  * between the RTC and CPU frequencies (measured through a
9  * Timer/Counter, TC) is within a configurable range.
10  *
11  * The main program consists of a loop where the device does not
12  * do anything as long as there is no error in the CPU frequency.
13  * An LED is on while the system is operating correctly. However,
14  * if the error flag should be set, the main program would exit the
15  * loop and switch off the LED.
16  *
17  * In order to test that the self-diagnostic routine is correct,
18  * the CPU frequency can be modified on-the-fly by pressing the
19  * button SW0. By default the system runs from the internal 2 Mhz
20  * oscillator. The interrupt for button press changes the system clock
21  * to the internal 32 Mhz oscillator. The CPU frequency test will
22  * then fail because the frequency of the system does not match the
23  * configuration.
24  *
25  *
26  * \par Application note:
27  * AVR1610: Guide to IEC60730 Class B compliance with XMEGA
28  *
29  * \par Documentation
30  * For comprehensive code documentation, supported compilers, compiler
31  * settings and supported devices see readme.html
32  *
33  * \author
34  * Atmel Corporation: http://www.atmel.com \n
35  * Support email: avr@atmel.com
36  *
37  * Copyright (C) 2012 Atmel Corporation. All rights reserved.
38  *
39  * \page License
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions are met:
43  *
44  * 1. Redistributions of source code must retain the above copyright notice,
45  * this list of conditions and the following disclaimer.
46  *
47  * 2. Redistributions in binary form must reproduce the above copyright notice,
48  * this list of conditions and the following disclaimer in the documentation
49  * and/or other materials provided with the distribution.
50  *
51  * 3. The name of Atmel may not be used to endorse or promote products derived
52  * from this software without specific prior written permission.
53  *
54  * 4. This software may only be redistributed and used in connection with an
55  * Atmel AVR product.
56  *
57  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
58  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
59  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
60  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
61  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
63  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
64  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
67  * DAMAGE.
68  */
69 
70 
71 #include "avr_compiler.h"
72 #include "classb_freq.h"
73 
74 #if __AVR_ATxmega256A3BU__ | __ATxmega256A3BU__
75 # define LEDPORT PORTR
76 # define SWITCHPORT PORTE
77 # define SWITCH_INT0_vect PORTE_INT0_vect
78 # define XPLAIN_PULLUP 0x00
79 #elif defined(__AVR_ATxmega128A1__) | defined(__ATxmega128A1__)
80 # define LEDPORT PORTE
81 # define SWITCHPORT PORTF
82 # define SWITCH_INT0_vect PORTF_INT0_vect
83 # define XPLAIN_PULLUP PORT_OPC_PULLUP_gc
84 #endif
85 
86 
87 //! \brief This is the error flag.
88 NO_INIT volatile uint8_t classb_error;
89 
90 
91 //! \brief This function sets up the LED and switch.
92 //! The LED is switched on to show that the system is working correctly.
93 //! The switch is configured to generate an interrupt after being pressed.
95 {
96  // Set direction and state of LED pins
97  LEDPORT.DIRSET = PIN0_bm;
98  PORTCFG.MPCMASK = PIN0_bm;
99  LEDPORT.PIN0CTRL |= PORT_INVEN_bm;
100 
101  // Set sensitivity, polarity and pull-up for button PIN
102  SWITCHPORT.PIN5CTRL = PORT_ISC_FALLING_gc | PORT_INVEN_bm | XPLAIN_PULLUP;
103  SWITCHPORT.INT0MASK |= PIN5_bm;
104  SWITCHPORT.INTCTRL |= PORT_INT0LVL_LO_gc;
105 
106  // Enable LOW level interrupts in the INT controller
107  PMIC.CTRL |= PMIC_LOLVLEN_bm;
108 
109  // Turn on LED that signals correct operation.
110  LEDPORT.OUTSET = PIN0_bm;
111 }
112 
113 
114 int main(void)
115 {
116  // Set up led and switch
118  // Setup and start RTC and TC
119  classb_rtc_setup();
121 
122  // Enable interrupts globally
123  sei();
124 
125  while(!classb_error) {
126  /* Do nothing */
127  };
128 
129  // If we reach this point, an error has been detected.
130  // Disable interrupts and turn off LED.
131  cli();
132  LEDPORT.OUTCLR = PIN0_bm;
133 }
134 
135 //! \brief Interrupt service routine for button press.
136 //! This will change the clock frequency to 32MHz, which should be detected by the frequency test.
137 ISR(PORTE_INT0_vect)
138 {
139  // Enable 32 Mhz internal oscillator and wait until the oscillator is stable.
140  OSC.CTRL |= OSC_RC32MEN_bm;
141  while (!(OSC.STATUS & OSC_RC32MRDY_bm));
142 
143  // Select the oscillator as source for the system clock.
144  // This requires writing first to the configuration change protection register.
145  CCP = CCP_IOREG_gc;
146  CLK.CTRL = CLK_SCLKSEL_RC32M_gc;
147 }
148 
149 
150