| Xmega IEC60730 Class B Library 1.0 |

All Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
classb_freq.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 CPU frequency test.
6 *
7 * \par Application note:
8 * AVR1610: Guide to IEC60730 Class B compliance with XMEGA
9 *
10 * \par Documentation
11 * For comprehensive code documentation, supported compilers, compiler
12 * settings and supported devices see readme.html
13 *
14 * \author
15 * Atmel Corporation: http://www.atmel.com \n
16 * Support email: avr@atmel.com
17 *
18 * Copyright (C) 2012 Atmel Corporation. All rights reserved.
19 *
20 * \page License
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions are met:
24 *
25 * 1. Redistributions of source code must retain the above copyright notice,
26 * this list of conditions and the following disclaimer.
27 *
28 * 2. Redistributions in binary form must reproduce the above copyright notice,
29 * this list of conditions and the following disclaimer in the documentation
30 * and/or other materials provided with the distribution.
31 *
32 * 3. The name of Atmel may not be used to endorse or promote products derived
33 * from this software without specific prior written permission.
34 *
35 * 4. This software may only be redistributed and used in connection with an
36 * Atmel AVR product.
37 *
38 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
41 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
42 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
44 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
45 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
48 * DAMAGE.
49 */
50
51
52 #include "avr_compiler.h"
53 #include "classb_freq.h"
54
55 //! \addtogroup cpu_freq
56 //@{
57
58 //! \internal \brief This counts the number of TC overflows.
59 //! This variable is used to keep the MSW (bits 31 to 16) of the TC count.
60 volatile uint16_t classb_tc_ovf_cnt = 0;
61
62 //@}
63
64 //! \brief This sets up the TC that is used in the frequency test.
65 //! This configures the period, prescaler and overflow interrupt.
66 void classb_freq_setup_timer()
67 {
68 // Set up period as configured in the header file.
69 CLASSB_TEST_TC.PER = CLASSB_TC_PER;
70
71 // Set up overflow interrupt.
72 CLASSB_TEST_TC.INTCTRLA = TC_OVFINTLVL_LO_gc;
73
74 // Start the TC with the prescaling factor configured in the header file.
75 CLASSB_TEST_TC.CTRLA = CLASSB_TC_PRESCALER_gc;
76
77 // Reset count
78 CLASSB_TEST_TC.CNT = 0;
79 }
80
81 //! \brief This is called back from the RTC interrupt.
82 //!
83 //! This function calculates the difference between the TC count and the
84 //! predefined reference. The error handler would be called if the relative
85 //! difference should be higher than the tolerance.
86 void classb_freq_callback()
87 {
88 // Read the count in the TC and include the overflow counter as MSB
89 volatile uint32_t tccount = CLASSB_TEST_TC.CNT;
90 tccount |= ((uint32_t)classb_tc_ovf_cnt) << 16;
91
92 // Compute the absolute difference between reference and counter.
93 tccount = (tccount > CLASSB_TC_COUNT_REF)?(tccount-CLASSB_TC_COUNT_REF):(CLASSB_TC_COUNT_REF-tccount);
94
95 // If the difference was larger than expected, the error should be handled
96 if (tccount > CLASSB_MAX_DIF)
97 CLASSB_ERROR_HANDLER_FREQ();
98
99 // Reset the overflow counter.
100 classb_tc_ovf_cnt = 0;
101
102 // Reset TC
103 CLASSB_TEST_TC.CNT = 0;
104 }
105
106 //! \addtogroup func_freq
107 //@{
108 //! \brief TC overflow interrupt.
109 //! This counts the number of TC overflows and the error handler is called if
110 //! the configurable maximum is exceeded. The purpose of the latter is to detect
111 //! RTC failure: the TC is reset in the RTC interrupt. If the RTC interrupt
112 //! is too slow, the number of TC overflows will exceed the limit. Note that
113 //! if the TC frequency increased abruptly, the error would probably be detected
114 //! here.
115 ISR(CLASSB_TEST_TC_OVF_vect)
116 {
117 // Increase the overflow counter. If the TC had more overflows than expected,
118 // the error should be handled.
119 if ( classb_tc_ovf_cnt++ >= CLASSB_COUNT_OVF_MAX )
120 CLASSB_ERROR_HANDLER_FREQ();
121 }
122 //@}