RT-Thread在Lan8720a和 lwip基础上移植ntp流程

开发环境:野火的stm32f407,rt-thread studio版本是版本: 2.2.6,stm32f4的资源包为0.2.2。以rt-thread中lan8720和lwip协议栈的使用文章创建的工程为基础。
新建ntptest.c测试文件,移植以上参考的ntp代码,整理后如下:
#include
#include
#define server_ip 114.67.237.130
#define server_port 123
#define timeout 10
//宏定义
#define ntp_timestamp_delta 2208988800ull //number of seconds between 1900 and 1970,1900-1970的时间差
#define sec_time_zone + (86060) //beijing,gmt+8, 时区差
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 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 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 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.
} ntp_packet; // total: 48 bytes.
int sockfd=-1;
int maxfd1;
fd_set readfds;
struct timeval timeout;
struct sockaddr_in servaddr;
int len;
int i;
uint8_t buf[48];
long local_timestamp;//uint32_t local_timestamp;
ntp_packet packet ;
struct tm * net_time;
uint8_t ntp_data[48]; //48字节的报文
int bsp_ntp_init(void)
{
ntp_data[0]=0xa3;
//00100011, 0xa3,100 版本4
//00011011, 0x1b,011 版本3
//00010011, 0x13,010 版本2
//00001011, 0x0b,001 版本1
//后面分别发送了4个不同版本,都可以收到数据。
for(i=1;i0)
{
//close(sockfd);
closesocket(sockfd);
sockfd=-1;
}
if ((sockfd = socket(af_inet, sock_dgram, 0)) < 0)
{
return -1;
}
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) != 0)
{
//close(sockfd);
closesocket(sockfd);
sockfd=-1;
return -1;
}
return 0;
}
int bsp_read_ntp(void)
{
if(sockfd<0)
{
if(bsp_ntp_init() 0)
{
if (fd_isset(sockfd, &readfds))
{
if ((len = recv(sockfd, buf, sizeof(buf), 0)) < 0)
{
//close(sockfd);
closesocket(sockfd);
sockfd=-1;
return -1;
}
packet.txtm_s = buf[40]<<24 | buf[40+1]<<16|buf[40+2]tm_mon)+1, net_time->tm_mday, net_time->tm_hour,net_time->tm_min,net_time->tm_sec); //打印出时间
}
}
return 0;
}
主函数调用,代码如下:
extern int bsp_ntp_init(void);
extern int bsp_read_ntp(void);
int main(void)
{
int count = 1;
rt_thread_mdelay(5000);
bsp_ntp_init();
while (count++)
{
log_d(hello rt-thread!);
rt_thread_mdelay(1000);
bsp_read_ntp();
}
return rt_eok;
}
编译无报错,下载测试,测试结果如下:
选择其中一个时间戳,利用时间戳转换工具比对,结果如下:
结果正常。

内地否了的“不限量”5G套餐中国联通在香港推出
基于Linux在2018年的8个发展预测
我们要学会如何快速读paper
基于Multisim的负反馈放大电路分析
有源高功率因数预热启动电子镇流器
RT-Thread在Lan8720a和 lwip基础上移植ntp流程
100G应用部署提速需完备测试需求
如何实现硬盘的raid
人工智能与物联网时代,硬件创业者如何把握新机遇?
影响焊接结构疲劳强度的因素有哪些
RT12864与SPCE061的连接电路
未来十年,中国半导体设备公司将进入世界八强
小功率LED驱动器中的电路保护
联发科mt6755相当于骁龙的哪款处理器
60V耐压DC-DC降压BUCK电路,WIFI模块单片机供电方案原理图:ECP2459(SOP23-6)
客户冲击自研芯片,华为、谷歌成英伟达最大隐患
单片机的应用范围
叶绿素含量测定仪的优点和缺点分别是什么
thx203h开关电路图大全(五款thx203h开关电路)
美的空调提升用户感受_美的空调怎么样