00001
00054 #include "avr_compiler.h"
00055 #include "rtc32_driver.h"
00056
00057
00063 void RTC32_Reset( void )
00064 {
00065
00066 VBAT.CTRL = VBAT_ACCEN_bm;
00067
00068
00069 ENTER_CRITICAL_REGION();
00070 CCP = 0xD8;
00071 VBAT.CTRL = VBAT_RESET_bm;
00072 LEAVE_CRITICAL_REGION();
00073
00074
00075 RTC32.CTRL &= ~RTC32_ENABLE_bm;
00076 do { } while ( RTC32_SyncBusy() );
00077 }
00078
00084 void RTC32_ToscEnable( bool use1khz )
00085 {
00086
00087 if (use1khz)
00088 VBAT.CTRL |= ( VBAT_XOSCEN_bm | VBAT_XOSCSEL_bm );
00089 else
00090 VBAT.CTRL |= ( VBAT_XOSCEN_bm );
00091
00092
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
00111 RTC32.CTRL &= ~RTC32_ENABLE_bm;
00112 do { } while ( RTC32_SyncBusy() );
00113
00114
00115 RTC32.PER = period - 1;
00116 RTC32.COMP = compareValue;
00117 RTC32.CNT = count;
00118
00119
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
00180 RTC32_SyncCnt();
00181 do { } while ( RTC32_SyncCntBusy() );
00182
00183
00184 compareValue = RTC32.CNT + alarmTimeout;
00185
00186
00187 if (compareValue > RTC32.PER){
00188 compareValue -= RTC32.PER;
00189 }
00190
00191
00192 RTC32.COMP = compareValue;
00193 }
00194
00204 void RTC32_SetCount( uint32_t count )
00205 {
00206
00207 do { } while ( RTC32_SyncBusy() );
00208
00209
00210 RTC32.CNT = count;
00211 do { } while ( RTC32_SyncBusy() );
00212 }
00213
00221 uint32_t RTC32_GetCount( void )
00222 {
00223
00224 RTC32_SyncCnt();
00225 do { } while ( RTC32_SyncCntBusy() );
00226
00227 return RTC32.CNT;
00228 }
00229
00237 void RTC32_SetPeriod( uint32_t period )
00238 {
00239
00240 RTC32.CTRL &= ~RTC32_ENABLE_bm;
00241 do { } while ( RTC32_SyncBusy() );
00242
00243 RTC32.PER = period;
00244
00245
00246 RTC32.CTRL |= RTC32_ENABLE_bm;
00247 do { } while ( RTC32_SyncBusy() );
00248 }