使用NTP自动更新时间
介绍了开启 NTP 软件包(一个运行在 RT_Thread 上的 NTP 客户端),并且当连接上网络后,利用这个软件包,获取当前的 UTC 时间,并更新至 RTC(实时时钟)中。
1. 背景介绍
NTP 网络时间协议(Network Time Protocol),是用来同步网络中各个计算机时间的协议。
2. 打开客户端
打开 Env 工具输入 menuconfig 按照下面的路径即可打开 ntp 客户端。
3. 开机启动自动更新时间
启动时,自动更新时间,配置如下图:
启动时代码如下 :
File System initialized!
W25Q256 mount successful!
Loongson系统初始化完成!
msh />
===phy FULLDUPLEX MODE
Link is up in FULL DUPLEX mode
Link is with 100M Speed
toy_read0 = 0x17d75863, toy_read1 = 0x14.
rtcDate is 20.5.30 - 23:22:6
toy_read0 = 0x17d75860, toy_read1 = 0x14.
rtcDate is 20.5.30 - 23:22:6lls
如果开启 RTC 设备,还可以使用下面的命令及 API 同步 NTP 的本地时间至 RTC 设备。
命令效果如下
msh />ntp_sync
toy_read0 = 0x17d798c7, toy_read1 = 0x14.
rtcDate is 20.5.30 - 23:38:13
toy_read0 = 0x17d798d0, toy_read1 = 0x14.
rtcDate is 20.5.30 - 23:38:13Get local time from NTP server: Sat May 30 23:38:13 2020
The system time is updated. Timezone is 8.
msh />
The system time is updated. Timezone is 8.
msh />