Forráskód Böngészése

[ntp]增加NTP_USING_AUTO_SYNC功能
重命名RTC_NTP_FIRST_SYNC_DELAY RTC_NTP_SYNC_PERIOD
完善readme

Meco Man 4 éve
szülő
commit
2a4409bc39
2 módosított fájl, 47 hozzáadás és 65 törlés
  1. 12 32
      ntp/README.md
  2. 35 33
      ntp/ntp.c

+ 12 - 32
ntp/README.md

@@ -4,7 +4,7 @@
 
 [NTP](https://baike.baidu.com/item/NTP) 是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机时间的协议。
 
-在 RT-Thread 上实现了 NTP 客户端,连接上网络后,可以获取当前 UTC 时间,并更新至 RTC 中。
+在 RT-Thread 上实现了 NTP 客户端,连接上网络后,可以获取当前 GMT 时间,并更新至 RTC 中。
 
 ## 2 使用
 
@@ -12,25 +12,21 @@
 
 ### 2.1 启用 NTP 时间自动同步
 
-如果 RT-Thread 已接入互联网,可启用 NTP 时间自动同步功能,定期同步本地时间。
-
-在 menuconfig 中启用 `RTC_SYNC_USING_NTP` 配置。启用该功能后,会自动开启 [netutils package](https://github.com/RT-Thread-packages/netutils) 的 NTP 功能。同时务必确保 RT-Thread 网络访问正常。
+如果 RT-Thread 已接入互联网,可启用 NTP 时间自动同步功能,定期同步本地时间。在 menuconfig 中启用 `NTP_USING_AUTO_SYNC` 配置。启用该功能后,NTP 会自动同步时间到 RTC。同时务必确保 RT-Thread 网络访问正常以及存在 RTC 设备。
 
 启用该配置后,还有三个配置是用户可选配置:
 
 - 在配置选项中,默认提供了 3 个 NTP 服务器,保证了 NTP 功能的可靠性。
-- `RTC_NTP_FIRST_SYNC_DELAY`: 首次执行 NTP 时间同步的延时。延时的目的在于,给网络连接预留一定的时间,尽量提高第一次执行 NTP 时间同步时的成功率。默认时间为 30S;
-- `RTC_NTP_SYNC_PERIOD`: NTP 自动同步周期,单位为秒,默认一小时(即 3600S)同步一次。
-
-### 2.2 获取 UTC 时间
+- `NTP_AUTO_SYNC_FIRST_DELAY`: 首次执行 NTP 时间同步的延时。延时的目的在于,给网络连接预留一定的时间,尽量提高第一次执行 NTP 时间同步时的成功率。默认时间为 30S;
+- `NTP_AUTO_SYNC_PERIOD`: NTP 自动同步周期,单位为秒,默认一小时(即 3600S)同步一次。
 
-[UTC 时间](https://baike.baidu.com/item/%E5%8D%8F%E8%B0%83%E4%B8%96%E7%95%8C%E6%97%B6/787659?fromtitle=UTC&fromid=5899996) 又称世界统一时间、世界标准时间、国际协调时间。北京时间为 UTC+8 时间,比 UTC 时间多 8 小时,或者理解为早 8 小时。
+### 2.2 获取 GMT 时间
 
-API: `time_t ntp_get_time(void)`
+GMT格林威治时间,即UTC+0时间。
 
-|参数                                    |描述|
-|:-----                                  |:----|
-|return                                  |`>0`: 当前 UTC 时间,`=0`:获取时间失败|
+|函数                                    |返回值                                    |描述|
+|:-----                                  |:----|:----|
+|time_t ntp_get_time(void)                                  |`>0`: 当前 GMT 时间,`=0`:获取时间失败                                  ||
 
 
 示例代码:
@@ -51,21 +47,7 @@ void main(void)
 }
 ```
 
-### 2.3 获取本地时间
-
-本地时间比 UTC 时间多了时区的概念,例如:北京时间为东八区,比 UTC 时间多 8 个小时。
-
-在 `menuconfig` 中可以设置当前时区,默认为 `8`
-
-API: `time_t ntp_get_local_time(void)`
-
-|参数                                    |描述|
-|:-----                                  |:----|
-|return                                  |`>0`: 当前本地时间,`=0`:获取时间失败|
-
-该 API 使用方法与 `ntp_get_time()` 类似
-
-### 2.4 同步本地时间至 RTC
+### 2.3 同步本地时间至 RTC
 
 如果开启 RTC 设备,还可以使用下面的命令及 API 同步 NTP 的本地时间至 RTC 设备。
 
@@ -78,11 +60,9 @@ The system time is updated. Timezone is 8.
 msh />
 ```
 
-API: `time_t ntp_sync_to_rtc(void)`
-
-|参数                                    |描述|
+|函数                                    |描述|
 |:-----                                  |:----|
-|return                                  |`>0`: 当前本地时间,`=0`:同步时间失败|
+|time_t ntp_sync_to_rtc(void)                                  |`>0`: 当前本地时间,`=0`:同步时间失败|
 
 ## 3 注意事项
 

+ 35 - 33
ntp/ntp.c

@@ -84,39 +84,39 @@ extern int closesocket(int s);
 
 #define NTP_TIMESTAMP_DELTA            2208988800ull
 
-#define LI(packet)   (uint8_t) ((packet.li_vn_mode & 0xC0) >> 6) // (li   & 11 000 000) >> 6
-#define VN(packet)   (uint8_t) ((packet.li_vn_mode & 0x38) >> 3) // (vn   & 00 111 000) >> 3
-#define MODE(packet) (uint8_t) ((packet.li_vn_mode & 0x07) >> 0) // (mode & 00 000 111) >> 0
+#define LI(packet)   (uint8_t) ((packet.li_vn_mode & 0xC0) >> 6) /* (li   & 11 000 000) >> 6 */
+#define VN(packet)   (uint8_t) ((packet.li_vn_mode & 0x38) >> 3) /* (vn   & 00 111 000) >> 3 */
+#define MODE(packet) (uint8_t) ((packet.li_vn_mode & 0x07) >> 0) /* (mode & 00 000 111) >> 0 */
 
-// Structure that defines the 48 byte NTP packet protocol.
+/* Structure that defines the 48 byte NTP packet protocol */
 typedef struct {
 
-    uint8_t li_vn_mode;      // Eight bits. li, vn, and mode.
-                             // li.   Two bits.   Leap indicator.
-                             // vn.   Three bits. Version number of the protocol.
-                             // mode. Three bits. Client will pick mode 3 for client.
+    uint8_t li_vn_mode;      /* Eight bits. li, vn, and mode */
+                             /* li.   Two bits.   Leap indicator */
+                             /* vn.   Three bits. Version number of the protocol */
+                             /* mode. Three bits. Client will pick mode 3 for client */
 
-    uint8_t stratum;         // Eight bits. Stratum level of the local clock.
-    uint8_t poll;            // Eight bits. Maximum interval between successive messages.
-    uint8_t precision;       // Eight bits. Precision of the local clock.
+    uint8_t stratum;         /* Eight bits. Stratum level of the local clock */
+    uint8_t poll;            /* Eight bits. Maximum interval between successive messages */
+    uint8_t precision;       /* Eight bits. Precision of the local clock */
 
-    uint32_t rootDelay;      // 32 bits. Total round trip delay time.
-    uint32_t rootDispersion; // 32 bits. Max error aloud from primary clock source.
-    uint32_t refId;          // 32 bits. Reference clock identifier.
+    uint32_t rootDelay;      /* 32 bits. Total round trip delay time */
+    uint32_t rootDispersion; /* 32 bits. Max error aloud from primary clock source */
+    uint32_t refId;          /* 32 bits. Reference clock identifier */
 
-    uint32_t refTm_s;        // 32 bits. Reference time-stamp seconds.
-    uint32_t refTm_f;        // 32 bits. Reference time-stamp fraction of a second.
+    uint32_t refTm_s;        /* 32 bits. Reference time-stamp seconds */
+    uint32_t refTm_f;        /* 32 bits. Reference time-stamp fraction of a second */
 
-    uint32_t origTm_s;       // 32 bits. Originate time-stamp seconds.
-    uint32_t origTm_f;       // 32 bits. Originate time-stamp fraction of a second.
+    uint32_t origTm_s;       /* 32 bits. Originate time-stamp seconds */
+    uint32_t origTm_f;       /* 32 bits. Originate time-stamp fraction of a second */
 
-    uint32_t rxTm_s;         // 32 bits. Received time-stamp seconds.
-    uint32_t rxTm_f;         // 32 bits. Received time-stamp fraction of a second.
+    uint32_t rxTm_s;         /* 32 bits. Received time-stamp seconds */
+    uint32_t rxTm_f;         /* 32 bits. Received time-stamp fraction of a second */
 
-    uint32_t txTm_s;         // 32 bits and the most important field the client cares about. Transmit time-stamp seconds.
-    uint32_t txTm_f;         // 32 bits. Transmit time-stamp fraction of a second.
+    uint32_t txTm_s;         /* 32 bits and the most important field the client cares about. Transmit time-stamp seconds */
+    uint32_t txTm_f;         /* 32 bits. Transmit time-stamp fraction of a second */
 
-} ntp_packet;                // Total: 384 bits or 48 bytes.
+} ntp_packet;                /* Total: 384 bits or 48 bytes */
 
 static ntp_packet packet = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
@@ -393,28 +393,29 @@ time_t ntp_sync_to_rtc(const char *host_name)
 }
 
 #if RT_VER_NUM > 0x40003
+#ifdef NTP_USING_AUTO_SYNC
 /* NTP first sync delay time for network connect, unit: second */
-#ifndef RTC_NTP_FIRST_SYNC_DELAY
-#define RTC_NTP_FIRST_SYNC_DELAY                 (30)
+#ifndef NTP_AUTO_SYNC_FIRST_DELAY
+#define NTP_AUTO_SYNC_FIRST_DELAY                 (30)
 #endif
 /* NTP sync period, unit: second */
-#ifndef RTC_NTP_SYNC_PERIOD
-#define RTC_NTP_SYNC_PERIOD                      (1L*60L*60L)
+#ifndef NTP_AUTO_SYNC_PERIOD
+#define NTP_AUTO_SYNC_PERIOD                      (1L*60L*60L)
 #endif
 
 static void ntp_sync_thread_enrty(void *param)
 {
     /* first sync delay for network connect */
-    rt_thread_delay(RTC_NTP_FIRST_SYNC_DELAY * RT_TICK_PER_SECOND);
+    rt_thread_delay(NTP_AUTO_SYNC_FIRST_DELAY * RT_TICK_PER_SECOND);
 
     while (1)
     {
         ntp_sync_to_rtc(NULL);
-        rt_thread_delay(RTC_NTP_SYNC_PERIOD * RT_TICK_PER_SECOND);
+        rt_thread_delay(NTP_AUTO_SYNC_PERIOD * RT_TICK_PER_SECOND);
     }
 }
 
-static int rt_rtc_ntp_sync_init(void)
+static int ntp_auto_sync_init(void)
 {
     static rt_bool_t init_ok = RT_FALSE;
     rt_thread_t thread;
@@ -424,7 +425,7 @@ static int rt_rtc_ntp_sync_init(void)
         return 0;
     }
 
-    thread = rt_thread_create("ntp_sync", ntp_sync_thread_enrty, RT_NULL, 1300, RT_THREAD_PRIORITY_MAX - 2, 20);
+    thread = rt_thread_create("ntp-sync", ntp_sync_thread_enrty, RT_NULL, 1300, RT_THREAD_PRIORITY_MAX - 2, 20);
     if (thread)
     {
         rt_thread_startup(thread);
@@ -438,8 +439,9 @@ static int rt_rtc_ntp_sync_init(void)
 
     return RT_EOK;
 }
-INIT_COMPONENT_EXPORT(rt_rtc_ntp_sync_init);
-#endif
+INIT_COMPONENT_EXPORT(ntp_auto_sync_init);
+#endif /*NTP_USING_AUTO_SYNC*/
+#endif /*RT_VER_NUM > 0x40003*/
 
 #ifdef FINSH_USING_MSH
 #include <finsh.h>