| Xmega IEC60730 Class B Library 1.0 |

All Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wdt/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
6 * This is the demo application for the watchdog timer (WDT) test.
7 *
8 * The self-diagnostic routine for the WDT is executed before
9 * the main function is called. If the WDT is not working properly,
10 * the device will hang.
11 *
12 * This demo application sets up an interrupt for SW0, switches
13 * an LED on and then stays in a loop as long as the system is
14 * on a working state. In this loop the WDT is reset according to
15 * its open and closed period settings. If the error flag should be
16 * set, the main program would exit the loop and switch off the LED.
17 *
18 * The following procedure can be followed in order to test that
19 * the correct behavior of the WDT. When button SW0 is pressed, the
20 * WDT is reset too early and it will issue a system reset. When
21 * button SW1 is pressed, the WDT is reset too late and a system reset
22 * will be issued as well. This "unexpected" resets should be caught
23 * by the self-diagnostic routine, which in this demo is configured
24 * to set the error flag, setup the WDT and continue to the main application.
25 * Therefore, after pressing any of the buttons the system is set on
26 * a safe state.
27 *
28 *
29 * \par Application note:
30 * AVR1610: Guide to IEC60730 Class B compliance with XMEGA
31 *
32 * \par Documentation
33 * For comprehensive code documentation, supported compilers, compiler
34 * settings and supported devices see readme.html
35 *
36 * \author
37 * Atmel Corporation: http://www.atmel.com \n
38 * Support email: avr@atmel.com
39 *
40 * Copyright (C) 2012 Atmel Corporation. All rights reserved.
41 *
42 * \page License
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions are met:
46 *
47 * 1. Redistributions of source code must retain the above copyright notice,
48 * this list of conditions and the following disclaimer.
49 *
50 * 2. Redistributions in binary form must reproduce the above copyright notice,
51 * this list of conditions and the following disclaimer in the documentation
52 * and/or other materials provided with the distribution.
53 *
54 * 3. The name of Atmel may not be used to endorse or promote products derived
55 * from this software without specific prior written permission.
56 *
57 * 4. This software may only be redistributed and used in connection with an
58 * Atmel AVR product.
59 *
60 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
63 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
64 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
67 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
70 * DAMAGE.
71 */
72
73 #include "avr_compiler.h"
74 #include "util/delay.h"
75
76 NO_INIT volatile uint8_t classb_error;
77
78 #if __AVR_ATxmega256A3BU__ | __ATxmega256A3BU__ | __DOXYGEN__
79 # define LEDPORT PORTR
80 # define SWITCHPORT0 PORTE
81 # define SWITCHPORT1 PORTF
82 # define SWITCH0_INT0_vect PORTE_INT0_vect
83 # define SWITCH1_INT0_vect PORTF_INT0_vect
84 # define XPLAIN_PULLUP 0x00
85 #elif defined(__AVR_ATxmega128A1__) | defined(__ATxmega128A1__)
86 # define LEDPORT PORTE
87 # define SWITCHPORT0 PORTF
88 # define SWITCHPORT0 PORTF
89 # define SWITCH_INT0_vect PORTF_INT0_vect
90 # define XPLAIN_PULLUP PORT_OPC_PULLUP_gc
91 #endif
92
93 /*! \brief Hardware setup function for button- and LED functionality.
94 */
95 void setup_led_switches() {
96 // Set direction and state of LED pins
97 LEDPORT.DIRSET = PIN0_bm | PIN1_bm;
98 PORTCFG.MPCMASK = PIN0_bm | PIN1_bm;
99 LEDPORT.PIN0CTRL |= PORT_INVEN_bm;
100
101 // Set sensitivity, polarity and pullup for button SW0
102 SWITCHPORT0.PIN5CTRL |= PORT_ISC_FALLING_gc | PORT_INVEN_bm;
103 SWITCHPORT0.INT0MASK |= PIN5_bm;
104 SWITCHPORT0.INTCTRL |= PORT_INT0LVL_LO_gc;
105
106 // Set sensitivity, polarity and pullup for button SW1
107 SWITCHPORT1.PIN1CTRL |= PORT_ISC_FALLING_gc | PORT_INVEN_bm;
108 SWITCHPORT1.INT0MASK |= PIN1_bm;
109 SWITCHPORT1.INTCTRL |= PORT_INT0LVL_LO_gc;
110
111 // Enable LOW level interrupts in the INT controller
112 PMIC.CTRL |= PMIC_LOLVLEN_bm;
113
114 // Turn on LED that signals correct operation.
115 LEDPORT.OUTSET = PIN0_bm;
116 }
117
118 //! This delay is longer than the closed window setting, but shorter than the total period.
119 void correct_timing() { _delay_ms(600); }
120 //! This delay is shorter than the closed window setting.
121 void fast_timing() { _delay_ms(100); }
122 //! This delay is longer than the total period.
123 void slow_timing() { _delay_ms(1000); }
124
125 //! This is a function pointer, initialized to the correct delay.
126 void (*our_delay)(void) = &correct_timing;
127
128 int main(void)
129 {
130
131 // Initial setup
132 setup_led_switches();
133
134 // Enable interrupts
135 sei();
136
137 while(classb_error!=1) {
138 //This calls one of the three delay functions
139 our_delay();
140 LEDPORT.OUTTGL = PIN1_bm;
141 watchdog_reset();
142 };
143
144 // If we reach here, an error has been detected. Turn off LED.
145 LEDPORT.OUTCLR = PIN0_bm;
146
147 }
148
149
150 /*! \brief
151 * Button SW0 interrupt function. The WDT will be updated too early.
152 */
153 ISR(SWITCH0_INT0_vect) {
154 our_delay = &fast_timing;
155 }
156
157 /*! \brief
158 * Button SW1 interrupt function. The WDT will be updated too late.
159 */
160 ISR(SWITCH1_INT0_vect) {
161 our_delay = &slow_timing;
162 }