sntp简介
lwip&sntp配置
- sntp初始化,设置sntp服务器地址,和操作模式
//初始化sntp服务
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_init();
// 设置sntp服务器地址,sntp服务器可以设置多个,支持的服务器数量上限由LWIP_DHCP_MAX_NTP_SERVERS 确定
// 默认1个,这里只设置一个
sntp_setserver(0, &sntp_server_addr);
- 编写rtc更新函数
void update_rtc(uint64_t sntp_time)
{
struct tm *time;
sd2650_time_t rtc_time_temp;
sntp_time += (8 * 60 * 60); // 北京时间是东8区需偏移8小时
time = localtime((time_t*)&sntp_time);
rtc_time_temp.hour = time->tm_hour;
rtc_time_temp.minute = time->tm_min;
rtc_time_temp.second = time->tm_sec;
rtc_time_temp.month = (time->tm_mon) + 1;
rtc_time_temp.year = (time->tm_year) + 1900 - 2000;
rtc_time_temp.day = time->tm_mday;
rtc_time_temp.week = time->tm_wday;
sd2650_set_time(&rtc_time_temp);
#if SNTP_TASK_DEBUG
xil_printf("sntp update rtc.\r\n");
xil_printf("recv time %d-%d-%d %d:%d:%d\r\n",\
rtc_time_temp.year,
rtc_time_temp.month,
rtc_time_temp.day,
rtc_time_temp.hour,
rtc_time_temp.minute,
rtc_time_temp.second);
#endif
}
- 修改sntp.c,加入rtc更新函数。
/**
* SNTP processing of received timestamp
*/
static void
sntp_process(u32_t *receive_timestamp)
{
/* convert SNTP time (1900-based) to unix GMT time (1970-based)
* if MSB is 0, SNTP time is 2036-based!
*/
u32_t rx_secs = lwip_ntohl(receive_timestamp[0]);
int is_1900_based = ((rx_secs & 0x80000000) != 0);
u32_t t = is_1900_based ? (rx_secs - DIFF_SEC_1900_1970) : (rx_secs + DIFF_SEC_1970_2036);
time_t tim = t;
#if SNTP_CALC_TIME_US
u32_t us = lwip_ntohl(receive_timestamp[1]) / 4295;
SNTP_SET_SYSTEM_TIME_US(t, us);
/* display local time from GMT time */
LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s, %"U32_F" us", ctime(&tim), us));
#else /* SNTP_CALC_TIME_US */
/* change system time and/or the update the RTC clock */
update_rtc(t);
/* display local time from GMT time */
LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s", ctime(&tim)));
#endif /* SNTP_CALC_TIME_US */
LWIP_UNUSED_ARG(tim);
}
zynq sntp.c中函数未定义问题解决办法
编译时会发现BSP中sntp.c中的函数好像并没有被编译到工程中,我们将BSP中的sntp.c和sntp.h复制到我们自己的工程中即可解决此问题。