void rtc_write(time_t t) { if (! rtc_inited) { rtc_init(); } // Convert timestamp to struct tm struct tm *timeinfo = localtime(&t); S_RTC_TIME_DATA_T rtc_datetime; // Convert S_RTC_TIME_DATA_T to struct tm rtc_datetime.u32Year = timeinfo->tm_year + YEAR0; rtc_datetime.u32Month = timeinfo->tm_mon + 1; rtc_datetime.u32Day = timeinfo->tm_mday; rtc_datetime.u32DayOfWeek = timeinfo->tm_wday; rtc_datetime.u32Hour = timeinfo->tm_hour; rtc_datetime.u32Minute = timeinfo->tm_min; rtc_datetime.u32Second = timeinfo->tm_sec; rtc_datetime.u32TimeScale = RTC_CLOCK_24; // NOTE: Timing issue with write to RTC registers. This delay is empirical, not rational. RTC_SetDateAndTime(&rtc_datetime); //nu_nop(6000); wait_us(100); }
void rtc_write(time_t t) { if (! rtc_isenabled()) { rtc_init(); } // Convert timestamp to struct tm struct tm timeinfo; if (_rtc_localtime(t, &timeinfo, RTC_FULL_LEAP_YEAR_SUPPORT) == false) { return; } S_RTC_TIME_DATA_T rtc_datetime; // Convert S_RTC_TIME_DATA_T to struct tm rtc_datetime.u32Year = timeinfo.tm_year + YEAR0; rtc_datetime.u32Month = timeinfo.tm_mon + 1; rtc_datetime.u32Day = timeinfo.tm_mday; rtc_datetime.u32DayOfWeek = timeinfo.tm_wday; rtc_datetime.u32Hour = timeinfo.tm_hour; rtc_datetime.u32Minute = timeinfo.tm_min; rtc_datetime.u32Second = timeinfo.tm_sec; rtc_datetime.u32TimeScale = RTC_CLOCK_24; // NOTE: Timing issue with write to RTC registers. This delay is empirical, not rational. RTC_SetDateAndTime(&rtc_datetime); wait_us(100); }
/** * @brief Initialize RTC module and start counting * * @param[in] sPt Specify the time property and current date and time. It includes: \n * u32Year: Year value, range between 2000 ~ 2099. \n * u32Month: Month value, range between 1 ~ 12. \n * u32Day: Day value, range between 1 ~ 31. \n * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY / * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY / * RTC_SATURDAY] \n * u32Hour: Hour value, range between 0 ~ 23. \n * u32Minute: Minute value, range between 0 ~ 59. \n * u32Second: Second value, range between 0 ~ 59. \n * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n * u8AmPm: [RTC_AM / RTC_PM] \n * * @return None * * @details This function is used to: \n * 1. Write initial key to let RTC start count. \n * 2. Input parameter indicates start date/time. \n * 3. User has to make sure that parameters of RTC date/time are reasonable. \n * @note Null pointer for using default starting date/time. */ void RTC_Open(S_RTC_TIME_DATA_T *sPt) { RTC_T *pRTC; if((__PC()&NS_OFFSET) == NS_OFFSET) { pRTC = RTC_NS; } else { pRTC = RTC; } pRTC->INIT = RTC_INIT_KEY; if(pRTC->INIT != RTC_INIT_ACTIVE_Msk) { pRTC->INIT = RTC_INIT_KEY; while(pRTC->INIT != RTC_INIT_ACTIVE_Msk) {} } if(sPt == 0) { ; /* No RTC date/time data */ } else { /* Set RTC date and time */ RTC_SetDateAndTime(sPt); } }
/** * @brief Initialize RTC setting and start counting * * @param[in] sPt Specify the time property and current date and time. It includes: \n * u32Year: Year value. \n * u32Month: Month value. \n * u32Day: Day value. \n * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY / * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY / * RTC_SATURDAY] \n * u32Hour: Hour value. \n * u32Minute: Minute value. \n * u32Second: Second value. \n * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n * u8AmPm: [RTC_AM / RTC_PM] \n * * @return None * * @details This function is used to: \n * 1. Write initial key to let RTC start count. \n * 2. Input parameter indicates start date/time. \n * @note Null pointer for using default starting date/time. */ void RTC_Open(S_RTC_TIME_DATA_T *sPt) { RTC->INIR = RTC_INIT_KEY; if(RTC->INIR != 0x1) { RTC->INIR = RTC_INIT_KEY; while(RTC->INIR != 0x1); } if(sPt == NULL) return ; /* Set RTC date and time */ RTC_SetDateAndTime(sPt); /* Waiting for RTC settings stable */ while((RTC->AER & RTC_AER_ENF_Msk) == RTC_AER_ENF_Msk); }
void RTCUtils_SetDateTime(const RTCUtils_DateTime_t *dateTime) { S_RTC_TIME_DATA_T rtcData; uint32_t primask; // Populate RTC data rtcData.u32Year = 2000 + dateTime->year; rtcData.u32Month = dateTime->month; rtcData.u32Day = dateTime->day; rtcData.u32DayOfWeek = dateTime->dayOfWeek; rtcData.u32Hour = dateTime->hour; rtcData.u32Minute = dateTime->minute; rtcData.u32Second = dateTime->second; rtcData.u32TimeScale = RTC_CLOCK_24; // Protect against concurrent SetDateTime and GetDateTime primask = Thread_IrqDisable(); if(!RTCUtils_isRTCOpen) { // Enable LXT clock SYS_UnlockReg(); CLK_EnableXtalRC(CLK_PWRCTL_LXTEN_Msk); CLK_WaitClockReady(CLK_STATUS_LXTSTB_Msk); CLK_EnableModuleClock(RTC_MODULE); SYS_LockReg(); // Initialize RTC with supplied data RTC_Open(&rtcData); // Atomic, set as last to minimize CS in GetDateTime RTCUtils_isRTCOpen = 1; } else { // Update RTC data RTC_SetDateAndTime(&rtcData); } Thread_IrqRestore(primask); }