|
楼主 |
发表于 2021-3-29 11:00:37
|
显示全部楼层
感谢硬汉哥,已解决。
- struct datetime
- {
- unsigned int year;
- unsigned char month;
- unsigned char day;
- unsigned char weekday;
- unsigned char hours;
- unsigned char minutes;
- unsigned char seconds;
- unsigned int mseconds;
- };
- /**
- * @brief Set the RTC to a specified time
- * @param datetime An application specific date and time structure pointer with
- * the current time
- * @return True on success, False otherwise
- */
- bool set_datetime(datetime_t datetime)
- {
- RTC_DateTypeDef sdate;
- RTC_TimeTypeDef stime;
- bool rc = false;
- if (datetime != NULL)
- {
- sdate.WeekDay = datetime->weekday;
- sdate.Year = datetime->year - 2000;
- sdate.Month = datetime->month;
- sdate.Date = datetime->day;
- // All internal time is 24 hour GMT/UTC
- stime.Hours = datetime->hours;
- stime.Minutes = datetime->minutes;
- stime.Seconds = datetime->seconds;
- stime.TimeFormat = RTC_HOURFORMAT_24;
- stime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- stime.StoreOperation = RTC_STOREOPERATION_SET;
- if ((HAL_RTC_SetDate(&hrtc, &sdate, RTC_FORMAT_BIN) == HAL_OK) &&
- (HAL_RTC_SetTime(&hrtc, &stime, RTC_FORMAT_BIN) == HAL_OK))
- {
- rc = true;
- }
- }
- return rc;
- }
- /**
- * @brief Get the RTC date and time
- * @param datetime Pointer to an application date and time structure in which to
- * return the current date and time
- * @return True on success and a filled in date and time structure with the current
- * date and time, False otherwise
- */
- bool get_datetime(datetime_t datetime)
- {
- RTC_DateTypeDef date;
- RTC_TimeTypeDef time;
- bool rc = false;
- if (datetime != NULL)
- {
- // We *MUST* read the RTC in this order to unlock the shadow registers
- if ((HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN) == HAL_OK) &&
- (HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN) == HAL_OK))
- {
- datetime->year = date.Year + 2000;
- datetime->month = date.Month;
- datetime->day = date.Date;
- datetime->weekday = date.WeekDay;
- datetime->hours = time.Hours;
- datetime->minutes = time.Minutes;
- datetime->seconds = time.Seconds;
- // SecondFraction is a scaler for SubSeconds increments per second that counts down
- datetime->mseconds = ((time.SecondFraction - time.SubSeconds) * 1000) / (time.SecondFraction + 1);
- rc = true;
- }
- }
- return rc;
- }
复制代码
- #define SNTP_SERVER_DNS 1
- #include "bsp_rtc.h"
- #include <time.h>
- #define SNTP_SET_SYSTEM_TIME_NTP(sec, us) do { \
- struct datetime now = {0}; struct tm now_time; \
- time_t ut = (unsigned int)((unsigned int)sec + (unsigned int)2085978496L); \
- localtime_r(&ut, &now_time); \
- now.year = now_time.tm_year + 1900; \
- now.month = now_time.tm_mon + 1; \
- now.day = now_time.tm_mday; \
- now.weekday = now_time.tm_wday; \
- now.hours = now_time.tm_hour + 8; \
- now.minutes = now_time.tm_min; \
- now.seconds = now_time.tm_sec; \
- now.mseconds = (((unsigned int)(((uint64_t)(us) * 1000000UL) >> 32)) / 1000); \
- set_datetime(&now); \
- } while(0);
复制代码
- static void set_sntp_server_list(void)
- {
- uint32_t server_list[SNTP_MAX_SERVERS] =
- {
- 0x279148D2,
- 0x42041876,
- 0x5F066CCA,
- 0x0B6C1978,
- 0x0B0C5CB6,
- };
- ip_addr_t sntp_server;
- for (int i = 0; i < SNTP_MAX_SERVERS; i++)
- {
- sntp_server.addr = server_list[i];
- sntp_setserver(i, &sntp_server);
- }
- }
- /**
- * @brief Initialize SNTP, only need to call it once
- */
- void initialize_sntp(void)
- {
- sntp_setoperatingmode(SNTP_OPMODE_POLL);
- set_sntp_server_list();
- sntp_init();
- }
复制代码
在应用层调用一次initialize_sntp()即可
|
|