Xmega IEC60730 Class B Library  1.0
 All Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
registers/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 a demo application for the CPU registers test.
6  *
7  * The test is called from a button interrupt. If there is an error
8  * the CPU goes to an infinite loop or sets the global error flag
9  * \ref classb_error. This depends on the kind of error and the
10  * user configuration.
11  *
12  * \par Application note:
13  * AVR1610: Guide to IEC60730 Class B compliance with XMEGA
14  *
15  * \par Documentation
16  * For comprehensive code documentation, supported compilers, compiler
17  * settings and supported devices see readme.html
18  *
19  * \author
20  * Atmel Corporation: http://www.atmel.com \n
21  * Support email: avr@atmel.com
22  *
23  * Copyright (C) 2012 Atmel Corporation. All rights reserved.
24  *
25  * \page License
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions are met:
29  *
30  * 1. Redistributions of source code must retain the above copyright notice,
31  * this list of conditions and the following disclaimer.
32  *
33  * 2. Redistributions in binary form must reproduce the above copyright notice,
34  * this list of conditions and the following disclaimer in the documentation
35  * and/or other materials provided with the distribution.
36  *
37  * 3. The name of Atmel may not be used to endorse or promote products derived
38  * from this software without specific prior written permission.
39  *
40  * 4. This software may only be redistributed and used in connection with an
41  * Atmel AVR product.
42  *
43  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
44  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
45  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
46  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
47  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
49  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
53  * DAMAGE.
54  */
55 #include "avr_compiler.h"
56 #include "classb_cpu.h"
57 
58 NO_INIT volatile uint8_t classb_error;
59 
60 // TODO: add doc
61 #if __AVR_ATxmega256A3BU__ | __ATxmega256A3BU__
62 # define LEDPORT PORTR
63 # define SWITCHPORT PORTE
64 # define SWITCH_INT0_vect PORTE_INT0_vect
65 # define XPLAIN_PULLUP 0x00
66 #elif defined(__AVR_ATxmega128A1__) | defined(__ATxmega128A1__)
67 # define LEDPORT PORTE
68 # define SWITCHPORT PORTF
69 # define SWITCH_INT0_vect PORTF_INT0_vect
70 # define XPLAIN_PULLUP PORT_OPC_PULLUP_gc
71 #endif
72 
73 //! \brief Sets up LEDs and switches.
75 
76  // Set direction and state of LED pin
77  LEDPORT.DIRSET = PIN0_bm;
78  PORTCFG.MPCMASK = PIN0_bm;
79  LEDPORT.PIN0CTRL |= PORT_INVEN_bm;
80 
81  // Set sensitivity, polarity and pullup for button PIN
82  SWITCHPORT.PIN5CTRL = PORT_ISC_FALLING_gc | PORT_INVEN_bm | XPLAIN_PULLUP;
83  SWITCHPORT.INT0MASK |= PIN5_bm;
84  SWITCHPORT.INTCTRL |= PORT_INT0LVL_LO_gc;
85 
86  // Enable LOW level interrupts in the INT controller
87  PMIC.CTRL |= PMIC_LOLVLEN_bm;
88 
89  // Turn on LED that signals correct operation.
90  LEDPORT.OUTSET = PIN0_bm;
91 }
92 /*! \brief
93  * Main entry point of the application.
94  * \callgraph
95  */
96 int main(void)
97 {
99 
100  // Enable interrupts globally
101  sei();
102 
103  while(!classb_error) {
104  /* Do nothing */
105  };
106 
107  // If we reach here, an error has been detected. Turn off LED.
108  LEDPORT.OUTCLR = PIN0_bm;
109 }
110 
111 
112 /*! \brief
113  * The button interrupt is used to call the self-diagnostic routine.
114  * \callgraph
115  */
116 ISR(SWITCH_INT0_vect) {
117 
118  // Disable interrupts globally
119  cli();
120 
121  // Run the CPU test, the return value is not considered because
122  // the test changes the global \ref classb_error in the case of an error.
124 
125  // Enable interrupts globally.
126  sei();
127 }