/*FUNCTION********************************************************************** * * Function Name : RTC_DRV_SetDatetime * Description : sets the RTC date and time according to the given time struct. * This function will set the RTC date and time according to the given time * struct, if start_after_set is true, the RTC oscillator will be enabled and * the counter will start. * *END**************************************************************************/ bool RTC_DRV_SetDatetime(uint32_t instance, rtc_datetime_t *datetime) { assert(datetime); uint32_t rtcBaseAddr = g_rtcBaseAddr[instance]; uint32_t srcClock = 0; uint32_t seconds = 0; /* Return error if the time provided is not valid */ if (!(RTC_HAL_IsDatetimeCorrectFormat(datetime))) { return false; } RTC_HAL_ConvertDatetimeToSecs(datetime, &seconds); if ((srcClock = CLOCK_SYS_GetExternalRefClock32kFreq()) != 32768U) { /* As the seconds register will not increment every second, we need to adjust the value * programmed to the seconds register */ seconds = seconds / (32768U / srcClock); } /* Set time in seconds */ RTC_HAL_SetDatetimeInsecs(rtcBaseAddr, seconds); return true; }
/*FUNCTION********************************************************************** * * Function Name : RTC_DRV_SetDatetime * Description : Sets the RTC date and time according to the given time struct. * This function will set the RTC date and time according to the given time * struct, if start_after_set is true, the RTC oscillator will be enabled and * the counter will start. * *END**************************************************************************/ bool RTC_DRV_SetDatetime(uint32_t instance, rtc_datetime_t *datetime) { assert(datetime); RTC_Type *rtcBase = g_rtcBase[instance]; uint32_t srcClock = 0; uint32_t seconds = 0; uint16_t preScaler = 0; uint64_t tmp = 0; /* Return error if the time provided is not valid */ if (!(RTC_HAL_IsDatetimeCorrectFormat(datetime))) { return false; } RTC_HAL_ConvertDatetimeToSecs(datetime, &seconds); if ((srcClock = CLOCK_SYS_GetRtcFreq(0U)) != 32768U) { /* As the seconds register will not increment every second, we need to adjust the value * programmed to the seconds register */ tmp = (uint64_t)seconds * (uint64_t)srcClock; preScaler = (uint32_t)(tmp & 0x7FFFU); seconds = (uint32_t)(tmp >> 15U); }
/*! * @brief RTC setting up alarm. * @param uint32_t instance : RTC instance * @param uint8_t offsetSec : offset in seconds to call interrupt. */ void rtcSetAlarm(uint32_t instance, uint8_t offsetSec) { uint32_t seconds; rtc_datetime_t date; if ((offsetSec < 1) || (offsetSec > 60)) { offsetSec = 5; } // get date time and convert to seconds RTC_DRV_GetDatetime(instance, &date); // convert to sec and add offset RTC_HAL_ConvertDatetimeToSecs(&date, &seconds); seconds += offsetSec; RTC_HAL_ConvertSecsToDatetime(&seconds, &date); // set the date time for alarm if (RTC_DRV_SetAlarm(instance, &date, true)) { } else { printf("Failed to set alarm. Alarm time is not in the future\r\n"); return; } }
/*FUNCTION********************************************************************** * * Function Name : RTC_DRV_SetAlarm * Description : sets the RTC alarm. * This function will first check if the date time has correct format. If yes, * convert the date time to seconds, and set the alarm in seconds. * *END**************************************************************************/ bool RTC_DRV_SetAlarm(uint32_t instance, rtc_datetime_t *alarmTime, bool enableAlarmInterrupt) { assert(alarmTime); uint32_t rtcBaseAddr = g_rtcBaseAddr[instance]; uint32_t srcClock = 0; uint32_t alrmSeconds = 0; uint32_t currSeconds = 0; /* Return error if the alarm time provided is not valid */ if (!(RTC_HAL_IsDatetimeCorrectFormat(alarmTime))) { return false; } RTC_HAL_ConvertDatetimeToSecs(alarmTime, &alrmSeconds); /* Get the current time */ currSeconds = RTC_HAL_GetSecsReg(rtcBaseAddr); if ((srcClock = CLOCK_SYS_GetExternalRefClock32kFreq()) != 32768U) { /* As the seconds register will not increment every second, we need to adjust the value * programmed to the alarm register */ alrmSeconds = alrmSeconds / (32768U / srcClock); } /* Make sure the alarm is for a future time */ if (alrmSeconds < currSeconds) { return false; } /* set alarm in seconds*/ RTC_HAL_SetAlarmReg(rtcBaseAddr, alrmSeconds); /* Activate or deactivate the Alarm interrupt based on user choice */ RTC_HAL_SetAlarmIntCmd(rtcBaseAddr, enableAlarmInterrupt); return true; }
void RtcInit( void ) { if ( RtcInitalized == false ) { rtc_datetime_t datetime; uint32_t srcClock = 0; uint32_t seconds = 0; uint16_t preScaler = 0; uint64_t tmp = 0; /* Enable clock gate to RTC module */ CLOCK_SYS_EnableRtcClock(0U); /* Initialize the general configuration for RTC module.*/ RTC_HAL_Init (RTC_BASE_PTR); /* Clear pending interrupts before enabling them */ NVIC_ClearPendingIRQ (RTC_IRQn); INT_SYS_EnableIRQ(RTC_IRQn); /* Configure RTC external clock */ CLOCK_SYS_RtcOscInit(0U, &rtcOscConfig); // Set a start date time and start RT. datetime.year = 2014U; datetime.month = 12U; datetime.day = 25U; datetime.hour = 19U; datetime.minute = 0; datetime.second = 0; RTC_HAL_ConvertDatetimeToSecs(&datetime, &seconds); if ( (srcClock = CLOCK_SYS_GetRtcFreq(0U)) != 32768U ) { /* As the seconds register will not increment every second, we need to adjust the value * programmed to the seconds register */ tmp = (uint64_t) seconds * (uint64_t) srcClock; preScaler = (uint32_t)(tmp & 0x7FFFU); seconds = (uint32_t)(tmp >> 15U); }
/*! * @brief RTC in alarm mode. * * This function demostrates how to use RTC as an alarm clock. */ int main(void) { uint32_t sec; uint32_t currSeconds; rtc_datetime_t date; // Init hardware. hardware_init(); PRINTF("RTC example: set up time to wake up an alarm\r\n"); // Init RTC RTC_DRV_Init(RTC_INSTANCE); /* Need to check this here as the RTC_DRV_Init() may have issued a software reset on the * module clearing all prior RTC OSC related setup */ BOARD_InitRtcOsc(); // Set a start date time and start RT. date.year = 2014U; date.month = 12U; date.day = 25U; date.hour = 19U; date.minute = 0; date.second = 0; // Set RTC time to default RTC_DRV_SetDatetime(RTC_INSTANCE, &date); while (1) { busyWait = true; // Get date time. RTC_DRV_GetDatetime(RTC_INSTANCE, &date); // print default time PRINTF("Current datetime: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n", date.year, date.month, date.day, date.hour, date.minute, date.second); // Get alarm time from user sec = 0; PRINTF("Please input the number of second to wait for alarm \r\n"); PRINTF("The second must be positive value\r\n"); while (sec < 1) { SCANF("%d",&sec); } // Get current date time. RTC_DRV_GetDatetime(RTC_INSTANCE, &date); // Convert current date time to seconds RTC_HAL_ConvertDatetimeToSecs(&date, &currSeconds); // Add sec to currSeconds currSeconds += sec; // Convert sec to date type RTC_HAL_ConvertSecsToDatetime(&currSeconds, &date); // Set alarm time if(!RTC_DRV_SetAlarm(RTC_INSTANCE, &date, true)) { PRINTF("Failed to set alarm. \r\n"); continue; } // Print alarm time PRINTF("Alarm will be occured at: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n", date.year, date.month, date.day, date.hour, date.minute, date.second); // Wait until alarm occures while(busyWait) {} PRINTF("\r\n Alarm occured !!!! "); } }
/*! * @brief RTC in alarm mode. * * This function demostrates how to use RTC as an alarm clock. */ void main(void) { uint32_t sec; uint32_t currSeconds; rtc_datetime_t date; // Init hardware. hardware_init(); // Call this function to initialize the console UART. This function // enables the use of STDIO functions (printf, scanf, etc.) dbg_uart_init(); printf("RTC example: set up time to wake up an alarm\r\n"); // Init RTC RTC_DRV_Init(RTC_INSTANCE); // Set a start date time and start RT. date.year = 2014U; date.month = 12U; date.day = 25U; date.hour = 19U; date.minute = 0; date.second = 0; // Set RTC time to default RTC_DRV_SetDatetime(RTC_INSTANCE, &date); while (1) { // Get date time. RTC_DRV_GetDatetime(RTC_INSTANCE, &date); // print default time printf("Current datetime: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n", date.year, date.month, date.day, date.hour, date.minute, date.second); // Convert current date time to seconds RTC_HAL_ConvertDatetimeToSecs(&date, &currSeconds); // Get alarm time from user sec = 0; printf("Please input the number of second to wait for alarm \r\n"); printf("The second must be positive value\r\n"); while (sec < 1) { scanf("%d",&sec); } // Add curr_seconds sec += currSeconds; // Convert sec to date type RTC_HAL_ConvertSecsToDatetime(&sec, &date); // Set alarm time if(!RTC_DRV_SetAlarm(RTC_INSTANCE, &date, true)) { printf("Failed to set alarm. \r\n"); continue; } // Print alarm time printf("Alarm will be occured at: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n", date.year, date.month, date.day, date.hour, date.minute, date.second); // Wait until alarm occures while(busyWait) {} printf("\r\n Alarm occured !!!! "); } }