XMEGA Application Note


rtc32_driver.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00054 #include "avr_compiler.h"
00055 #include "rtc32_driver.h"
00056 
00057 
00063 void RTC32_Reset( void )
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 }
00078 
00084 void RTC32_ToscEnable( bool use1khz )
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 }
00095 
00096 
00106 void RTC32_Initialize( uint32_t period,
00107                        uint32_t count,
00108                        uint32_t compareValue )
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 }
00123 
00124 
00129 void RTC32_SetOverflowIntLevel( RTC32_OVFINTLVL_t intLevel )
00130 {
00131         RTC32.INTCTRL = ( RTC32.INTCTRL & ~RTC32_OVFINTLVL_gm ) | intLevel;
00132 }
00133 
00134 
00139 void RTC32_SetCompareIntLevel( RTC32_COMPINTLVL_t intLevel )
00140 {
00141         RTC32.INTCTRL = ( RTC32.INTCTRL & ~RTC32_COMPINTLVL_gm ) | intLevel;
00142 }
00143 
00144 
00151 void RTC32_SetIntLevels( RTC32_OVFINTLVL_t ovfIntLevel,
00152                          RTC32_COMPINTLVL_t compIntLevel )
00153 {
00154         RTC32.INTCTRL = ( RTC32.INTCTRL &
00155                         ~( RTC32_COMPINTLVL_gm | RTC32_OVFINTLVL_gm ) ) |
00156                         ovfIntLevel |
00157                         compIntLevel;
00158 }
00159 
00175 void RTC32_SetAlarm( uint32_t alarmTimeout )
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 }
00194 
00204 void RTC32_SetCount( uint32_t count )
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 }
00213 
00221 uint32_t RTC32_GetCount( void )
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 }
00229 
00237 void RTC32_SetPeriod( uint32_t period )
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 }
@DOC_TITLE@
Generated on Mon Nov 9 13:44:26 2009 for XMEGA power consumption evaluation code by doxygen 1.5.9