Xmega IEC60730 Class B Library  1.0
 All Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
classb_rtc_common.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  * Driver for the RTC.
7  *
8  * \par Application note:
9  * AVR1610: Guide to IEC60730 Class B compliance with XMEGA
10  *
11  * \par Documentation
12  * For comprehensive code documentation, supported compilers, compiler
13  * settings and supported devices see readme.html
14  *
15  * \author
16  * Atmel Corporation: http://www.atmel.com \n
17  * Support email: avr@atmel.com
18  *
19  * Copyright (C) 2012 Atmel Corporation. All rights reserved.
20  *
21  * \page License
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions are met:
25  *
26  * 1. Redistributions of source code must retain the above copyright notice,
27  * this list of conditions and the following disclaimer.
28  *
29  * 2. Redistributions in binary form must reproduce the above copyright notice,
30  * this list of conditions and the following disclaimer in the documentation
31  * and/or other materials provided with the distribution.
32  *
33  * 3. The name of Atmel may not be used to endorse or promote products derived
34  * from this software without specific prior written permission.
35  *
36  * 4. This software may only be redistributed and used in connection with an
37  * Atmel AVR product.
38  *
39  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
42  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
43  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
49  * DAMAGE.
50  */
51 
52 
53 #include "classb_rtc_common.h"
54 
55 //! \addtogroup rtc_driver
56 //@{
57 
58 //! \internal \brief RTC32 Initialization.
59 //!
60 //! This function initializes the VBAT module and enables the oscillator used by the RTC32.
61 #if defined(RTC32) || defined(__DOXYGEN__)
62 void vbat_init(void)
63 {
64  // Reset the battery backup system
65  CCP = CCP_IOREG_gc;
66  VBAT.CTRL = VBAT_RESET_bm;
67 
68  // Enable access to VBAT
69  VBAT.CTRL |= VBAT_ACCEN_bm;
70 
71  // Choose 1024Hz oscillator output and enable crystal oscillator failure monitor
72  VBAT.CTRL |= VBAT_XOSCFDEN_bm | VBAT_XOSCSEL_bm;
73  /* This delay is needed to give the voltage in the backup system some
74  * time to stabilize before we turn on the oscillator. If we do not
75  * have this delay we may get a failure detection.
76  */
77  delay_us(200);
78 
79  // Enable the crystal oscillator
80  VBAT.CTRL |= VBAT_XOSCEN_bm;
81 
82  // Wait until the crystal oscillator ready flag is set
83  while (!(VBAT.STATUS & VBAT_XOSCRDY_bm));
84 }
85 #endif
86 
87 //! \internal \brief This function waits until RTC synchronizes.
88 bool rtc_is_busy( void ) {
89  #ifdef RTC32
90  return RTC32.SYNCCTRL & RTC32_SYNCBUSY_bm;
91  #else
92  return RTC.STATUS & RTC_SYNCBUSY_bm;
93  #endif
94 }
95 
96 
97 //! \brief This function sets up the RTC to be used in Class B tests.
98 //!
99 //! The RTC oscillator is enabled, compare interrupts are configured and RTC is enabled.
101  #ifdef RTC32
102  // If we use a device with RTC32 module, the VBAT system must be initialized before we use the RTC module
103  // This sets a 1024 Hz RTC32 from the 32.768 kHz crystal oscillator
104  vbat_init();
105  #else
106  // Use the RTC with 1024 Hz from internal 32.768 kHz RC oscillator
107  OSC.CTRL |= OSC_RC32KEN_bm;
108  while (!(OSC.STATUS & OSC_RC32KRDY_bm));
109  CLK.RTCCTRL = CLK_RTCSRC_RCOSC_gc | CLK_RTCEN_bm;
110  #endif
111 
112  // Set up the RTC to generate compare interrupt after RTC_TICK_REF [classb_freq.h] ticks and start the RTC.
113  RTC_TEST.CTRL &= ~RTC_TEST_START_bm; // Stop RTC
114  while (rtc_is_busy());
115  RTC_TEST.PER = 0xffff; // Maximum 16bit period, for device compatibility
116  RTC_TEST.CNT = 0;
117  RTC_TEST.COMP = CLASSB_RTC_INT_PERIOD;
118  while (rtc_is_busy());
119  RTC_TEST.INTCTRL = RTC_TEST_COMPINTLVL_LO_gc;
120  RTC_TEST.INTFLAGS = RTC_TEST_COMPIF_bm;
121  // Start RTC
122  RTC_TEST.CTRL |= RTC_TEST_START_bm;
123 }
124 
125 //! \brief This is the RTC compare interrupt.
126 //!
127 //! Actions required in Class B tests are implemented as callbacks. Note that this
128 //! requires a number of symbols to be defined, either in a header file or at the
129 //! compiler level.
130 //!
131 //! It is possible to add user-defined code to the RTC interrupt through
132 //! \ref CLASSB_ACTIONS_RTC().
133 ISR(RTC_TEST_COMP_vect) {
134 
135  // Reset the RTC
136  while (rtc_is_busy());
137  RTC_TEST.CNT = 0;
138  // Wait until the RTC module is ready, then start the RTC timer.
139  while (rtc_is_busy());
140  RTC_TEST.CTRL |= RTC_TEST_START_bm;
141 
142  #ifdef CLASSB_FREQ_TEST
144  #endif
145 
146  #ifdef CLASSB_INT_MON
148  #endif
149 
150 
151  // User-configurable actions
153 }
154 
155 //@}