/*! \brief 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.\n * The function does not return until the CNT register has synchronized from * the system to RTC clock domain. * * \param count The new RTC count value. */ void RTC32_SetCount( uint32_t count ) { /* Make sure that CNT is not currently synchronizing, or write will fail. */ do { } while ( RTC32_SyncBusy() ); /* Write new count value and wait for synchronization before returning. */ RTC32.CNT = count; do { } while ( RTC32_SyncBusy() ); }
/*! \brief 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.\n * * \param period The new RTC period. */ void RTC32_SetPeriod( uint32_t period ) { /* Disable the RTC32 module before writing to it. Wait for synch. */ RTC32.CTRL &= ~RTC32_ENABLE_bm; do { } while ( RTC32_SyncBusy() ); RTC32.PER = period; /* Enable the RTC32 module. Wait for synch. */ RTC32.CTRL |= RTC32_ENABLE_bm; do { } while ( RTC32_SyncBusy() ); }
/*! \brief 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. * * \param period RTC period. Topvalue = Period - 1. * \param count Initial RTC count. * \param compareValue Compare value. */ void RTC32_Initialize( uint32_t period, uint32_t count, uint32_t compareValue ) { /* Disable the RTC32 module before writing to it. Wait for synch. */ RTC32.CTRL &= ~RTC32_ENABLE_bm; do { } while ( RTC32_SyncBusy() ); /* Write PER, COMP and CNT. */ RTC32.PER = period - 1; RTC32.COMP = compareValue; RTC32.CNT = count; /* Re-enable the RTC32 module, synchronize before returning. */ RTC32.CTRL |= RTC32_ENABLE_bm; do { } while ( RTC32_SyncBusy() ); }
static void RTC32_Initialize( uint32_t period,uint32_t count,uint32_t compareValue ) { // Enable R/W access to the battery backup module. VBAT.CTRL = VBAT_ACCEN_bm; // Reset the module. (Reset bit is protected by CCP.) {uint8_t flags = cpu_irq_save(); CCP = 0xD8; VBAT.CTRL = VBAT_RESET_bm; cpu_irq_restore(flags);} // Enable the failure detection. VBAT.CTRL |= VBAT_XOSCFDEN_bm; /* A delay is needed to give the voltage in the backup system some time * to stabilise. */ delay_us(200); VBAT.CTRL |= VBAT_XOSCEN_bm | VBAT_XOSCSEL_bm; /* Disable the RTC32 module before writing to it. Wait for synch. */ RTC32.CTRL &= ~RTC32_ENABLE_bm; do { } while ( RTC32_SyncBusy() ); /* Write PER, COMP and CNT. */ RTC32.PER = period - 1; RTC32.COMP = compareValue; RTC32.CNT = count; /* Re-enable the RTC32 module, synchronize before returning. */ RTC32.CTRL |= RTC32_ENABLE_bm; do { } while ( RTC32_SyncBusy() ); }