XMEGA Application Note


rtc32_driver.h File Reference

XMEGA 32-bit RTC driver header file. More...

#include "avr_compiler.h"

Include dependency graph for rtc32_driver.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define RTC32_GetCompareFlag()   ( RTC32.INTFLAGS & RTC32_COMPIF_bm )
 This macro returns the status of the RTC compare flag.
#define RTC32_GetCompareValue()   ( RTC32.COMP )
 This macro returns the current RTC compare value.
#define RTC32_GetOverflowFlag()   ( RTC32.INTFLAGS & RTC32_OVFIF_bm )
 This macro returns the status of the RTC overflow flag.
#define RTC32_GetPeriod()   ( RTC32.PER )
 This macro returns the current RTC period value.
#define RTC32_SetCompareValue(_cmpVal)   ( RTC32.COMP = (_cmpVal) )
 This macro sets a new RTC compare value.
#define RTC32_SyncBusy()   ( RTC32.SYNCCTRL & RTC32_SYNCBUSY_bm )
 This macro returns the RTC32 CNT/CTRL write synchronization flag.
#define RTC32_SyncCnt()   ( RTC32.SYNCCTRL |= RTC32_SYNCCNT_bm )
 This macro initiates read synchronization of the RTC32 CNT register.
#define RTC32_SyncCntBusy()   ( RTC32.SYNCCTRL & RTC32_SYNCCNT_bm )
 This macro returns the RTC32 CNT read synchronization flag.
#define RTC32_ToscBusy()   !( VBAT.STATUS & VBAT_XOSCRDY_bm )
 This macro returns the XOSC/TOSC ready flag.

Functions

uint32_t RTC32_GetCount (void)
 This function returns the current RTC count value.
void RTC32_Initialize (uint32_t period, uint32_t count, uint32_t compareValue)
 This function initializes the RTC with period, initial count and compare value.
void RTC32_Reset (void)
 This function resets the battery backup module and disables the RTC.
void RTC32_SetAlarm (uint32_t alarmTimeout)
 This function sets a timeout alarm.
void RTC32_SetCompareIntLevel (RTC32_COMPINTLVL_t intLevel)
 This function sets the RTC compare interrupt level.
void RTC32_SetCount (uint32_t count)
 This function sets a new RTC count value.
void RTC32_SetIntLevels (RTC32_OVFINTLVL_t ovfIntLevel, RTC32_COMPINTLVL_t compIntLevel)
 This function sets both compare and overflow interrupt levels in one go.
void RTC32_SetOverflowIntLevel (RTC32_OVFINTLVL_t intLevel)
 This function sets the RTC overflow interrupt level.
void RTC32_SetPeriod (uint32_t period)
 This function sets a new RTC period.
void RTC32_ToscEnable (bool use1khz)
 This function starts the 32 kHz crystal oscillator with 1 or 1024 Hz clock output.


Detailed Description

XMEGA 32-bit RTC driver header file.

This file contains the function prototypes and enumerator definitions for various configuration parameters for the XMEGA 32-bit RTC driver.

Application note:
AVR1010: Minimizing the power consumption of XMEGA devices
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
2770
Date
2009-09-11 10:55:22 +0200 (fr, 11 sep 2009)

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file rtc32_driver.h.


Define Documentation

 
#define RTC32_GetCompareFlag (  )     ( RTC32.INTFLAGS & RTC32_COMPIF_bm )

This macro returns the status of the RTC compare flag.

Returns:
Non-Zero if an RTC compare has occured, zero otherwise.

Definition at line 93 of file rtc32_driver.h.

 
#define RTC32_GetCompareValue (  )     ( RTC32.COMP )

This macro returns the current RTC compare value.

This is the compare value of the RTC, which will be compared to the count sequence. The compare value must be less than or equal to PER.

Returns:
The current compare value.

Definition at line 119 of file rtc32_driver.h.

 
#define RTC32_GetOverflowFlag (  )     ( RTC32.INTFLAGS & RTC32_OVFIF_bm )

This macro returns the status of the RTC overflow flag.

Returns:
Non-Zero if overflow has occured, zero otherwise.

Definition at line 87 of file rtc32_driver.h.

 
#define RTC32_GetPeriod (  )     ( RTC32.PER )

This macro returns the current RTC period value.

This is the period value of the RTC, which is the top of the count sequence.

Returns:
The current RTC period value.

Definition at line 101 of file rtc32_driver.h.

#define RTC32_SetCompareValue ( _cmpVal   )     ( RTC32.COMP = (_cmpVal) )

This macro sets a new RTC compare value.

This is the compare value of the RTC, which will be compared to the count sequence. The compare value must be less than the RTC period.

Parameters:
_cmpVal The new compare value.

Definition at line 110 of file rtc32_driver.h.

 
#define RTC32_SyncBusy (  )     ( RTC32.SYNCCTRL & RTC32_SYNCBUSY_bm )

This macro returns the RTC32 CNT/CTRL write synchronization flag.

Returns:
Non-Zero if RTC32 is busy synchronizing CNT/CTRL from system to RTC32 clock domain, and zero otherwise.

Definition at line 62 of file rtc32_driver.h.

Referenced by RTC32_Initialize(), RTC32_Reset(), RTC32_SetCount(), and RTC32_SetPeriod().

 
#define RTC32_SyncCnt (  )     ( RTC32.SYNCCTRL |= RTC32_SYNCCNT_bm )

This macro initiates read synchronization of the RTC32 CNT register.

Initiates synchronization of CNT register from RTC to system clock domain.

Definition at line 74 of file rtc32_driver.h.

Referenced by RTC32_GetCount(), and RTC32_SetAlarm().

 
#define RTC32_SyncCntBusy (  )     ( RTC32.SYNCCTRL & RTC32_SYNCCNT_bm )

This macro returns the RTC32 CNT read synchronization flag.

Returns:
Non-Zero if RTC32 is busy synchronizing CNT from RTC to system clock domain, zero otherwise.

Definition at line 81 of file rtc32_driver.h.

Referenced by RTC32_GetCount(), and RTC32_SetAlarm().

 
#define RTC32_ToscBusy (  )     !( VBAT.STATUS & VBAT_XOSCRDY_bm )

This macro returns the XOSC/TOSC ready flag.

Returns:
Non-Zero if TOSC is not ready, and zero otherwise.

Definition at line 68 of file rtc32_driver.h.

Referenced by RTC32_ToscEnable().


Function Documentation

uint32_t RTC32_GetCount ( void   ) 

This function returns the current RTC count value.

This function synchronizes the RTC32 module's CNT value to the system clock domain, then returns its value.

Returns:
The current RTC count value.

Definition at line 221 of file rtc32_driver.c.

References RTC32_SyncCnt, and RTC32_SyncCntBusy.

00222 {
00223         /* Synchronize the RTC module's CNT value to the system clock domain.  */
00224         RTC32_SyncCnt();
00225         do { } while ( RTC32_SyncCntBusy() );
00226         
00227         return RTC32.CNT;
00228 }

void RTC32_Initialize ( uint32_t  period,
uint32_t  count,
uint32_t  compareValue 
)

This function initializes the RTC with period, initial count and compare value.

All the synchronized registers are written at the same time to save time.

Parameters:
period RTC period. Topvalue = Period - 1.
count Initial RTC count.
compareValue Compare value.

Definition at line 106 of file rtc32_driver.c.

References RTC32_SyncBusy.

Referenced by main().

00109 {
00110         /* Disable the RTC32 module before writing to it. Wait for synch. */
00111         RTC32.CTRL &= ~RTC32_ENABLE_bm;
00112         do { } while ( RTC32_SyncBusy() );
00113         
00114         /* Write PER, COMP and CNT. */
00115         RTC32.PER = period - 1;
00116         RTC32.COMP = compareValue;
00117         RTC32.CNT = count;
00118 
00119         /* Re-enable the RTC32 module, synchronize before returning. */
00120         RTC32.CTRL |= RTC32_ENABLE_bm;
00121         do { } while ( RTC32_SyncBusy() );
00122 }

void RTC32_Reset ( void   ) 

This function resets the battery backup module and disables the RTC.

Note:
This function should be called before any of the other RTC32 driver functions.

Definition at line 63 of file rtc32_driver.c.

References ENTER_CRITICAL_REGION, LEAVE_CRITICAL_REGION, and RTC32_SyncBusy.

Referenced by main().

00064 {
00065         /* Enable R/W access to the battery backup module. */
00066         VBAT.CTRL = VBAT_ACCEN_bm;
00067 
00068     /* Reset the module. (Reset bit is protected by CCP.) */
00069     ENTER_CRITICAL_REGION();
00070     CCP = 0xD8;
00071         VBAT.CTRL = VBAT_RESET_bm;
00072     LEAVE_CRITICAL_REGION();
00073 
00074         /* The RTC32 module may be enabled after reset. Disable it now. */
00075         RTC32.CTRL &= ~RTC32_ENABLE_bm;
00076         do { } while ( RTC32_SyncBusy() );
00077 }

void RTC32_SetAlarm ( uint32_t  alarmTimeout  ) 

This function sets a timeout alarm.

This function sets a timeout alarm by adding the timeout to the current count value. If the resulting alarm value is larger than the RTC period value, it will wrap around. An RTC compare interrupt will be triggered¨ after the specified timeout.

Note:
The program code needs to check if the RTC is busy before calling this function. The RTC_Busy() function can be used to do this.

The timeout must be less than the timer period. The timeout should not be set too low, as the timeout may be missed, depending on CPU speed.

Parameters:
alarmTimeout Timeout time in RTC clock cycles (scaled).

Definition at line 175 of file rtc32_driver.c.

References RTC32_SyncCnt, and RTC32_SyncCntBusy.

00176 {
00177         uint32_t compareValue;
00178         
00179         /* Synchronize CNT from RTC to system clock domain. */
00180         RTC32_SyncCnt();
00181         do { } while ( RTC32_SyncCntBusy() );
00182         
00183         /* Calculate compare time. */
00184         compareValue = RTC32.CNT + alarmTimeout;
00185 
00186         /* Wrap on period. */
00187         if (compareValue > RTC32.PER){
00188                 compareValue -= RTC32.PER;
00189         }
00190 
00191         /* Add the timeout value to get the absolute time of the alarm. */
00192         RTC32.COMP = compareValue;
00193 }

void RTC32_SetCompareIntLevel ( RTC32_COMPINTLVL_t  intLevel  ) 

This function sets the RTC compare interrupt level.

Parameters:
intLevel The compare interrupt level.

Definition at line 139 of file rtc32_driver.c.

Referenced by ISR(), and main().

00140 {
00141         RTC32.INTCTRL = ( RTC32.INTCTRL & ~RTC32_COMPINTLVL_gm ) | intLevel;
00142 }

void RTC32_SetCount ( uint32_t  count  ) 

This function sets a new RTC count value.

This function waits for the RTC32 module to finish synchronizing the CNT and CTRL registers before writing the new count.
The function does not return until the CNT register has synchronized from the system to RTC clock domain.

Parameters:
count The new RTC count value.

Definition at line 204 of file rtc32_driver.c.

References RTC32_SyncBusy.

00205 {
00206         /* Make sure that CNT is not currently synchronizing, or write will fail. */
00207         do { } while ( RTC32_SyncBusy() );
00208 
00209         /* Write new count value and wait for synchronization before returning. */
00210         RTC32.CNT = count;
00211         do { } while ( RTC32_SyncBusy() );
00212 }

void RTC32_SetIntLevels ( RTC32_OVFINTLVL_t  ovfIntLevel,
RTC32_COMPINTLVL_t  compIntLevel 
)

This function sets both compare and overflow interrupt levels in one go.

Parameters:
ovfIntLevel The overflow interrupt level.
compIntLevel The compare interrupt level.

Definition at line 151 of file rtc32_driver.c.

00153 {
00154         RTC32.INTCTRL = ( RTC32.INTCTRL &
00155                         ~( RTC32_COMPINTLVL_gm | RTC32_OVFINTLVL_gm ) ) |
00156                         ovfIntLevel |
00157                         compIntLevel;
00158 }

void RTC32_SetOverflowIntLevel ( RTC32_OVFINTLVL_t  intLevel  ) 

This function sets the RTC overflow interrupt level.

Parameters:
intLevel The overflow interrupt level.

Definition at line 129 of file rtc32_driver.c.

00130 {
00131         RTC32.INTCTRL = ( RTC32.INTCTRL & ~RTC32_OVFINTLVL_gm ) | intLevel;
00132 }

void RTC32_SetPeriod ( uint32_t  period  ) 

This function sets a new RTC period.

This function disables the RTC32 module, writes the new period to its PER register, then re-enables the module.

Parameters:
period The new RTC period.

Definition at line 237 of file rtc32_driver.c.

References RTC32_SyncBusy.

00238 {
00239         /* Disable the RTC32 module before writing to it. Wait for synch. */
00240         RTC32.CTRL &= ~RTC32_ENABLE_bm;
00241         do { } while ( RTC32_SyncBusy() );
00242         
00243         RTC32.PER = period;
00244         
00245         /* Enable the RTC32 module. Wait for synch. */
00246         RTC32.CTRL |= RTC32_ENABLE_bm;
00247         do { } while ( RTC32_SyncBusy() );
00248 }

void RTC32_ToscEnable ( bool  use1khz  ) 

This function starts the 32 kHz crystal oscillator with 1 or 1024 Hz clock output.

Parameters:
use1khz Boolean for selecting 1 kHz or 1 Hz RTC clock rate.

Definition at line 84 of file rtc32_driver.c.

References RTC32_ToscBusy.

Referenced by main().

00085 {
00086         /* Enable 32 kHz XTAL oscillator, with 1 kHz or 1 Hz output. */
00087         if (use1khz)
00088                 VBAT.CTRL |= ( VBAT_XOSCEN_bm | VBAT_XOSCSEL_bm );
00089         else
00090                 VBAT.CTRL |= ( VBAT_XOSCEN_bm );
00091 
00092         /* Wait for oscillator to stabilize before returning. */
00093         do { } while ( RTC32_ToscBusy() );
00094 }

@DOC_TITLE@
Generated on Mon Nov 9 13:44:33 2009 for XMEGA power consumption evaluation code by doxygen 1.5.9