XMEGA Application Note | |||||
#include "avr_compiler.h"
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. |
This file contains the function prototypes and enumerator definitions for various configuration parameters for the XMEGA 32-bit RTC driver.
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 RTC32_GetCompareFlag | ( | ) | ( RTC32.INTFLAGS & RTC32_COMPIF_bm ) |
This macro returns the status of the RTC compare flag.
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.
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.
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.
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.
_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.
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.
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.
Definition at line 68 of file rtc32_driver.h.
Referenced by RTC32_ToscEnable().
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.
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.
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.
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.
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.
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.
intLevel | The compare interrupt level. |
Definition at line 139 of file rtc32_driver.c.
Referenced by ISR(), and main().
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.
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.
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.
intLevel | The overflow interrupt level. |
Definition at line 129 of file rtc32_driver.c.
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.
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.
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 }
Generated on Mon Nov 9 13:44:33 2009 for XMEGA power consumption evaluation code by ![]() |