Xmega IEC60730 Class B Library  1.0
 All Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
interrupts/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 in an demo application for the interrupt monitor.
7  *
8  * This application sets up a periodic interrupt: a timer/counter (TC)
9  * overflow interrupt. Further, the application configures the
10  * interrupt monitor in order to check that the frequency of the
11  * interrupt is correct. Two switch interrupts are configured. The
12  * first one changes the frequency of the TC interrupt. The second
13  * one deactivates the interrupt in the monitor.
14  *
15  * \par Application note:
16  * AVR1610: Guide to IEC60730 Class B compliance with XMEGA
17  *
18  * \par Documentation
19  * For comprehensive code documentation, supported compilers, compiler
20  * settings and supported devices see readme.html
21  *
22  * \author
23  * Atmel Corporation: http://www.atmel.com \n
24  * Support email: avr@atmel.com
25  *
26  * Copyright (C) 2012 Atmel Corporation. All rights reserved.
27  *
28  * \page License
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions are met:
32  *
33  * 1. Redistributions of source code must retain the above copyright notice,
34  * this list of conditions and the following disclaimer.
35  *
36  * 2. Redistributions in binary form must reproduce the above copyright notice,
37  * this list of conditions and the following disclaimer in the documentation
38  * and/or other materials provided with the distribution.
39  *
40  * 3. The name of Atmel may not be used to endorse or promote products derived
41  * from this software without specific prior written permission.
42  *
43  * 4. This software may only be redistributed and used in connection with an
44  * Atmel AVR product.
45  *
46  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
47  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
49  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
50  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
53  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
56  * DAMAGE.
57  */
58 
59 #include "avr_compiler.h"
61 
62 //! \name Configuration parameters
63 //@{
64 
65 //!\brief Frequency in Hz of the T/C interrupt.
66 //!
67 //! This is used to compute the value written to the PER register.
68 #define F_TC_INT 30
69 
70 //!\brief Tolerance for number of T/C interrupts (%).
71 //!
72 #define TC_INT_TOL 15
73 
74 //!\brief TC prescaler
75 //!
76 //! The TC runs on the system clock scaled down by this parameter.
77 //! Possible values are 1, 2, 4, 8, 64, 256 or 1024.
78 #define TC_PRESCALER 8
79 
80 //! \brief This multiplies the TC period in SW0 interrupt.
81 #define PER_CHANGE 4
82 
83 //@}
84 
85 
86 //! \name Internal parameters
87 //@{
88 
89 //! \brief Reference for number of TC interrupts
90 //!
91 #define TC_INT_COUNT_REF (uint32_t) ( ( (1e0L * F_TC_INT) * CLASSB_RTC_INT_PERIOD) / CLASSB_RTC_FREQ )
92 
93 //! \brief Label for the TC prescaler group configuration
94 #define TC_PRESCALER_gc LABEL(TC_CLKSEL_DIV, TC_PRESCALER, _gc)
95 
96 //! \brief TC frequency
97 //!
98 //! The frequency of the TC is F_CPU divided by the prescaling factor.
99 #define TC_FREQ (uint32_t) (F_CPU/TC_PRESCALER)
100 
101 //! \brief TC period
102 //!
103 //! The 16-bit TC will generate an interrupt when the count reaches this value.
104 #define TC_PER (uint32_t) (TC_FREQ / F_TC_INT)
105 
106 //@}
107 
108 //! \name Global variables
109 //@{
110 
111 //! \brief Global error flag
112 NO_INIT volatile uint8_t classb_error;
113 
114 //@}
115 
116 
117 //! \name Board configuration
118 //@{
119 #if __AVR_ATxmega256A3BU__ | __ATxmega256A3BU__ | __DOXYGEN__
120  # define LEDPORT PORTR
121  # define SWITCHPORT0 PORTE
122  # define SWITCHPORT1 PORTF
123  # define SWITCH0_INT0_vect PORTE_INT0_vect
124  # define SWITCH1_INT0_vect PORTF_INT0_vect
125  # define XPLAIN_PULLUP 0x00
126 #elif defined(__AVR_ATxmega128A1__) | defined(__ATxmega128A1__)
127  # define LEDPORT PORTE
128  # define SWITCHPORT0 PORTF
129  # define SWITCHPORT0 PORTF
130  # define SWITCH_INT0_vect PORTF_INT0_vect
131  # define XPLAIN_PULLUP PORT_OPC_PULLUP_gc
132 #endif
133 //@}
134 
135 //! \brief Setup LEDs and buttons.
137 
138  // Set direction and state of LEDs
139  LEDPORT.DIRSET = PIN0_bm|PIN1_bm;
140  PORTCFG.MPCMASK = PIN0_bm|PIN1_bm;
141  LEDPORT.PIN0CTRL |= PORT_INVEN_bm;
142 
143  // Set sensitivity, polarity and pullup for button SW0
144  SWITCHPORT0.PIN5CTRL |= PORT_ISC_FALLING_gc | PORT_INVEN_bm;
145  SWITCHPORT0.INT0MASK |= PIN5_bm;
146  SWITCHPORT0.INTCTRL |= PORT_INT0LVL_LO_gc;
147 
148  // Set sensitivity, polarity and pullup for button SW1
149  SWITCHPORT1.PIN1CTRL |= PORT_ISC_FALLING_gc | PORT_INVEN_bm;
150  SWITCHPORT1.INT0MASK |= PIN1_bm;
151  SWITCHPORT1.INTCTRL |= PORT_INT0LVL_LO_gc;
152 
153  // Enable LOW level interrupts in the INT controller
154  PMIC.CTRL |= PMIC_LOLVLEN_bm;
155 
156  // Turn on LED that signals correct operation.
157  LEDPORT.OUTSET = PIN0_bm;
158 }
159 
160 /*! \brief Setup TC interrupt
161  *!
162  *! This sets up the TC so that it generates an overflow interrupt periodically.
163  *! This interrupt will be checked with the interrupt monitor.
164  */
166  // Set the T/C period
167  TCD0.PER = TC_PER;
168  // Enable LOW level overflow interrupt.
169  TCD0.INTCTRLA = TC_OVFINTLVL_LO_gc;
170  // Set the prescaler (and start TC)
171  TCD0.CTRLA = TC_PRESCALER_gc;
172 }
173 
174 int main(void)
175 {
176  // Initial setup
179 
180  // Register the interrupt in the monitor
182 
183  // Setup the RTC. The monitor is called back from the RTC interrupt.
184  classb_rtc_setup();
185 
186  // Interrupts enabled in the system
187  sei();
188 
189  // Enable the monitoring of the TC interrupt
191 
192  while(!classb_error) {
193  // Do nothing
194  };
195 
196  // If this is executed there has been an error. Turn off LEDs
197  LEDPORT.OUTCLR = PIN0_bm|PIN1_bm;
198 }
199 
200 
201 /*! \brief Interrupt for SW0 press: change TC period.
202  *
203  * This interrupt can be used to test the interrupt monitor. After pressing SW0 the
204  * period of the TC is changed, which means that the frequency of the TC interrupt is
205  * modified. This should be detected by the interrupt monitor, which should then set
206  * the global error flag \ref classb_error.
207  */
208 ISR(SWITCH0_INT0_vect) {
209  TCD0.PER = PER_CHANGE * TC_PER;
210 }
211 
212 /*! \brief Interrupt for SW1 press: disable TC interrupt monitoring.
213  *
214  * If the TC interrupt does not need to be monitored anymore the state of the interrupt
215  * can be changed to DISABLE.
216  * \note This interrupt could be executed more than once because of button bouncing.
217  * If \ref CLASSB_STRICT is defined the monitor would then set \ref classb_error.
218  */
219 ISR(SWITCH1_INT0_vect) {
221 }
222 
223 
224 /*! \brief TC overflow interrupt
225  *
226  * The interrupt counter is incremented by calling \ref classb_intmon_increase().
227  * After that an LED is toggled.
228  */
229 ISR(TCD0_OVF_vect) {
230 
232 
233  // Toggle LED.
234  LEDPORT.OUTTGL = PIN1_bm;
235 }