Xmega Application Note


rtc32_driver.c File Reference

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

#include "avr_compiler.h"
#include "rtc32_driver.h"
Include dependency graph for rtc32_driver.c:

Go to the source code of this file.

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_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.

Detailed Description

XMEGA 32-bit RTC driver source file.

This file contains the function implementations the XMEGA 32-bit RTC driver.

The driver is not intended for size and/or speed critical code, since most functions are just a few lines of code, and the function call overhead decreases code performance.

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
5159
Date
2009-06-09 19:05:02 +0200 (ti, 09 jun 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.c.


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 185 of file rtc32_driver.c.

References RTC32_SyncCnt, and RTC32_SyncCntBusy.

00186 {
00187         /* Synchronize the RTC module's CNT value to the system clock domain.  */
00188         RTC32_SyncCnt();
00189         do { } while ( RTC32_SyncCntBusy() );
00190         
00191         return RTC32.CNT;
00192 }

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 70 of file rtc32_driver.c.

References RTC32_SyncBusy.

Referenced by vbat_init().

00073 {
00074         /* Disable the RTC32 module before writing to it. Wait for synch. */
00075         RTC32.CTRL &= ~RTC32_ENABLE_bm;
00076         do { } while ( RTC32_SyncBusy() );
00077         
00078         /* Write PER, COMP and CNT. */
00079         RTC32.PER = period - 1;
00080         RTC32.COMP = compareValue;
00081         RTC32.CNT = count;
00082 
00083         /* Re-enable the RTC32 module, synchronize before returning. */
00084         RTC32.CTRL |= RTC32_ENABLE_bm;
00085         do { } while ( RTC32_SyncBusy() );
00086 }

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 139 of file rtc32_driver.c.

References RTC32_SyncCnt, and RTC32_SyncCntBusy.

Referenced by main().

00140 {
00141         uint32_t compareValue;
00142         
00143         /* Synchronize CNT from RTC to system clock domain. */
00144         RTC32_SyncCnt();
00145         do { } while ( RTC32_SyncCntBusy() );
00146         
00147         /* Calculate compare time. */
00148         compareValue = RTC32.CNT + alarmTimeout;
00149 
00150         /* Wrap on period. */
00151         if (compareValue > RTC32.PER){
00152                 compareValue -= RTC32.PER;
00153         }
00154 
00155         /* Add the timeout value to get the absolute time of the alarm. */
00156         RTC32.COMP = compareValue;
00157 }

void RTC32_SetCompareIntLevel ( RTC32_COMPINTLVL_t  intLevel  ) 

This function sets the RTC compare interrupt level.

Parameters:
intLevel The compare interrupt level.

Definition at line 103 of file rtc32_driver.c.

Referenced by main(), and vbat_init().

00104 {
00105         RTC32.INTCTRL = ( RTC32.INTCTRL & ~RTC32_COMPINTLVL_gm ) | intLevel;
00106 }

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 168 of file rtc32_driver.c.

References RTC32_SyncBusy.

00169 {
00170         /* Make sure that CNT is not currently synchronizing, or write will fail. */
00171         do { } while ( RTC32_SyncBusy() );
00172 
00173         /* Write new count value and wait for synchronization before returning. */
00174         RTC32.CNT = count;
00175         do { } while ( RTC32_SyncBusy() );
00176 }

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 115 of file rtc32_driver.c.

00117 {
00118         RTC32.INTCTRL = ( RTC32.INTCTRL &
00119                         ~( RTC32_COMPINTLVL_gm | RTC32_OVFINTLVL_gm ) ) |
00120                         ovfIntLevel |
00121                         compIntLevel;
00122 }

void RTC32_SetOverflowIntLevel ( RTC32_OVFINTLVL_t  intLevel  ) 

This function sets the RTC overflow interrupt level.

Parameters:
intLevel The overflow interrupt level.

Definition at line 93 of file rtc32_driver.c.

00094 {
00095         RTC32.INTCTRL = ( RTC32.INTCTRL & ~RTC32_OVFINTLVL_gm ) | intLevel;
00096 }

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 201 of file rtc32_driver.c.

References RTC32_SyncBusy.

00202 {
00203         /* Disable the RTC32 module before writing to it. Wait for synch. */
00204         RTC32.CTRL &= ~RTC32_ENABLE_bm;
00205         do { } while ( RTC32_SyncBusy() );
00206         
00207         RTC32.PER = period;
00208         
00209         /* Enable the RTC32 module. Wait for synch. */
00210         RTC32.CTRL |= RTC32_ENABLE_bm;
00211         do { } while ( RTC32_SyncBusy() );
00212 }

@DOC_TITLE@
Generated on Thu Jun 24 16:35:07 2010 for AVR1321: Using the XMEGA 32bit RTC by doxygen 1.6.1