/*! * @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; } }
/*! * @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 !!!! "); } }