硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2320|回复: 2
收起左侧

[LwIP] 如何使用LWIP的SNTP客户端?

[复制链接]

19

主题

72

回帖

129

积分

初级会员

积分
129
发表于 2021-3-26 17:37:20 | 显示全部楼层 |阅读模式
如何使用LWIP的SNTP客户端?

sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername();

sntp_init();

已知了这些API,如何获取到SNTP服务器的时间?

如果得到时间之后,就可以绑定RTC了。


回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
111540
QQ
发表于 2021-3-27 08:18:48 | 显示全部楼层
回复

使用道具 举报

19

主题

72

回帖

129

积分

初级会员

积分
129
 楼主| 发表于 2021-3-29 11:00:37 | 显示全部楼层
eric2013 发表于 2021-3-27 08:18
https://stackoverflow.com/questions/61932743/how-does-the-sntp-app-of-the-lwip-component-set-the-tim ...

感谢硬汉哥,已解决。

  1. struct datetime
  2. {
  3.     unsigned int  year;
  4.     unsigned char month;
  5.     unsigned char day;
  6.     unsigned char weekday;
  7.     unsigned char hours;
  8.     unsigned char minutes;
  9.     unsigned char seconds;
  10.     unsigned int mseconds;
  11. };

  12. /**
  13. * @brief Set the RTC to a specified time
  14. * @param datetime  An application specific date and time structure pointer with
  15. *                  the current time
  16. * @return  True on success, False otherwise
  17. */
  18. bool set_datetime(datetime_t datetime)
  19. {
  20.     RTC_DateTypeDef sdate;
  21.     RTC_TimeTypeDef stime;
  22.     bool rc = false;

  23.     if (datetime != NULL)
  24.     {
  25.         sdate.WeekDay = datetime->weekday;
  26.         sdate.Year = datetime->year - 2000;
  27.         sdate.Month = datetime->month;
  28.         sdate.Date = datetime->day;

  29.         // All internal time is 24 hour GMT/UTC
  30.         stime.Hours = datetime->hours;
  31.         stime.Minutes = datetime->minutes;
  32.         stime.Seconds = datetime->seconds;
  33.         stime.TimeFormat = RTC_HOURFORMAT_24;
  34.         stime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  35.         stime.StoreOperation = RTC_STOREOPERATION_SET;

  36.         if ((HAL_RTC_SetDate(&hrtc, &sdate, RTC_FORMAT_BIN) == HAL_OK) &&
  37.                 (HAL_RTC_SetTime(&hrtc, &stime, RTC_FORMAT_BIN) == HAL_OK))
  38.         {
  39.             rc = true;
  40.         }
  41.     }

  42.     return rc;
  43. }

  44. /**
  45. * @brief Get the RTC date and time
  46. * @param datetime  Pointer to an application date and time structure in which to
  47. *                  return the current date and time
  48. * @return   True on success and a filled in date and time structure with the current
  49. *           date and time, False otherwise
  50. */
  51. bool get_datetime(datetime_t datetime)
  52. {
  53.     RTC_DateTypeDef date;
  54.     RTC_TimeTypeDef time;
  55.     bool rc = false;

  56.     if (datetime != NULL)
  57.     {
  58.         // We *MUST* read the RTC in this order to unlock the shadow registers
  59.         if ((HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN) == HAL_OK) &&
  60.                 (HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN) == HAL_OK))
  61.         {
  62.             datetime->year = date.Year + 2000;
  63.             datetime->month = date.Month;
  64.             datetime->day = date.Date;
  65.             datetime->weekday = date.WeekDay;
  66.             datetime->hours = time.Hours;
  67.             datetime->minutes = time.Minutes;
  68.             datetime->seconds = time.Seconds;
  69.             // SecondFraction is a scaler for SubSeconds increments per second that counts down
  70.             datetime->mseconds = ((time.SecondFraction - time.SubSeconds) * 1000) / (time.SecondFraction + 1);

  71.             rc = true;
  72.         }
  73.     }

  74.     return rc;
  75. }

复制代码



  1. #define SNTP_SERVER_DNS   1

  2. #include "bsp_rtc.h"
  3. #include <time.h>

  4. #define SNTP_SET_SYSTEM_TIME_NTP(sec, us)  do { \
  5.     struct datetime now = {0}; struct tm now_time; \
  6.     time_t ut = (unsigned int)((unsigned int)sec + (unsigned int)2085978496L); \
  7.     localtime_r(&ut, &now_time); \
  8.     now.year = now_time.tm_year + 1900; \
  9.     now.month = now_time.tm_mon + 1; \
  10.     now.day = now_time.tm_mday; \
  11.     now.weekday = now_time.tm_wday; \
  12.     now.hours = now_time.tm_hour + 8; \
  13.     now.minutes = now_time.tm_min; \
  14.     now.seconds = now_time.tm_sec; \
  15.     now.mseconds = (((unsigned int)(((uint64_t)(us) * 1000000UL) >> 32)) / 1000); \
  16.     set_datetime(&now); \
  17. } while(0);
复制代码

  1. static void set_sntp_server_list(void)
  2. {
  3.     uint32_t server_list[SNTP_MAX_SERVERS] =
  4.     {
  5.         0x279148D2,
  6.         0x42041876,
  7.         0x5F066CCA,
  8.         0x0B6C1978,
  9.         0x0B0C5CB6,
  10.     };

  11.     ip_addr_t sntp_server;

  12.     for (int i = 0; i < SNTP_MAX_SERVERS; i++)
  13.     {
  14.         sntp_server.addr = server_list[i];
  15.         sntp_setserver(i, &sntp_server);
  16.     }
  17. }

  18. /**
  19. * @brief Initialize SNTP, only need to call it once
  20. */
  21. void initialize_sntp(void)
  22. {
  23.     sntp_setoperatingmode(SNTP_OPMODE_POLL);
  24.     set_sntp_server_list();
  25.     sntp_init();
  26. }
复制代码

在应用层调用一次initialize_sntp()即可



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-11-1 07:33 , Processed in 0.241339 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表