Foreword
In the previous chapter, we used the W5500_EVB_PICO development board to do Ping data to test IP connectivity, so in this chapter we will conduct the W5500_EVB_PICO SNTP test.
What is NTP?
NTP (Network Time Protocol) is a network time protocol based on UDP. It is a protocol used for network time synchronization. It synchronizes the computer clocks in the network to UTC, and then cooperates with the offset adjustment of each time zone to achieve precise synchronization. There are many servers that provide NTP time synchronization, such as Microsoft’s NTP time synchronization server. Using the time synchronization function provided by the NTP server can enable our device clock system to run correctly.
NTP message format
The NTP message format is shown in the figure above, and its field meanings are as follows:
LI leap second identifier, occupies 2 bits
VN version number, occupies 3 bits, indicating the NTP version number, currently 3
Mode mode, occupies 3 bits, indicating the mode
stratum (layer), occupies 8 bits
Poll test interval, occupies 8 bits, indicating the maximum interval between consecutive information
Precision precision, occupies 8 bits, and represents the accuracy of the local clock
Root Delay, which occupies 8 bits, represents the total round-trip delay between the main reference sources.
Root Dispersion root discrete, occupies 8 bits, represents the nominal error related to the main reference source
Reference Identifier reference clock identifier, which occupies 8 bits, is used to identify a special reference source.
Reference timestamp, 64bits timestamp, the latest time when the local clock was modified.
Original timestamp, time sent by the client, 64bits.
Receive timestamp, the time received by the server, 64 bits.
Transmission timestamp, the time when the server sent the response, 64 bits.
Authenticator (optional)
Connection method
The development board and host are both connected to the router LAN port
Get network timeTest
1. Related code
When we open the sntp_client.c file of the library file in the routine, we can see two functions related to ntp, namely the SNTP_init function and the SNRTP_run function. The first parameter of the SNTP_init function is the socket number, and the second parameter is the server to obtain the time. ip, the third parameter is the time zone in which the time is to be obtained (China is the East Eighth District, the corresponding number can be found in the sntp.c file), the fourth parameter is the cache buf; then the SNTP_run function is passed in a datetime structure type Parameters, there are corresponding parameters in the structure.
void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf) { NTP_SOCKET = s; NTPformat.dstaddr[0] = ntp_server[0]; NTPformat.dstaddr[1] = ntp_server[1]; NTPformat.dstaddr[2] = ntp_server[2]; NTPformat.dstaddr[3] = ntp_server[3]; time_zone = tz; data_buf = buf; uint8_t Flag; NTPformat.leap = 0; /* leap indicator */ NTPformat.version = 4; /* version number */ NTPformat.mode = 3; /* mode */ NTPformat.stratum = 0; /* stratum */ NTPformat.poll = 0; /* poll interval */ NTPformat.precision = 0; /* precision */ NTPformat.rootdelay = 0; /* root delay */ NTPformat.rootdisp = 0; /* root dispersion */ NTPformat.refid = 0; /* reference ID */ NTPformat.reftime = 0; /* reference time */ NTPformat.org = 0; /* origin timestamp */ NTPformat.rec = 0; /* receive timestamp */ NTPformat.xmt = 1; /* transmit timestamp */ Flag = (NTPformat.leap<<6) + (NTPformat.version<<3) + NTPformat.mode; //one byte Flag memcpy(ntpmessage,(void const*)( & amp;Flag),1); } int8_t SNTP_run(datetime *time) { uint16_t RSR_len; uint32_t destip = 0; uint16_t destport; uint16_t startindex = 40; //last 8-byte of data_buf[size is 48 byte] is xmt, so the startindex should be 40 switch(getSn_SR(NTP_SOCKET)) { case SOCK_UDP: if ((RSR_len = getSn_RX_RSR(NTP_SOCKET)) > 0) { if (RSR_len > MAX_SNTP_BUF_SIZE) RSR_len = MAX_SNTP_BUF_SIZE; // if Rx data size is lager than TX_RX_MAX_BUF_SIZE recvfrom(NTP_SOCKET, data_buf, RSR_len, (uint8_t *) & amp;destip, & amp;destport); get_seconds_from_ntp_server(data_buf,startindex); time->yy = Nowdatetime.yy; time->mo = Nowdatetime.mo; time->dd = Nowdatetime.dd; time->hh = Nowdatetime.hh; time->mm = Nowdatetime.mm; time->ss = Nowdatetime.ss; ntp_retry_cnt=0; close(NTP_SOCKET); return 1; } if(ntp_retry_cnt<0xFFFF) { if(ntp_retry_cnt==0)//first send request, no need to wait { sendto(NTP_SOCKET,ntpmessage,sizeof(ntpmessage),NTPformat.dstaddr,ntp_port); ntp_retry_cnt + + ; } else // send request again? it should wait for a while { if((ntp_retry_cnt % 0xFFF) == 0) //wait time { sendto(NTP_SOCKET,ntpmessage,sizeof(ntpmessage),NTPformat.dstaddr,ntp_port); #ifdef _SNTP_DEBUG_ printf("ntp retry: %d\r\\ ", ntp_retry_cnt); #endif ntp_retry_cnt + + ; } } } else //ntp retry fail { ntp_retry_cnt=0; #ifdef _SNTP_DEBUG_ printf("ntp retry failed!\r\\ "); #endif close(NTP_SOCKET); } break; case SOCK_CLOSED: socket(NTP_SOCKET,Sn_MR_UDP,ntp_port,0); break; } // Return value // 0 - failed / 1 - success return 0; }
datetime type structure
typedef struct _datetime { uint16_t yy; uint8_t mo; uint8_t dd; uint8_t hh; uint8_t mm; uint8_t ss; } datetime;
2. Testing phenomenon
After the compilation and burning is completed, open the serial monitor, reset W5500_EVB_PICO, and then the network configuration information will start to be printed on the serial port, and then the time will start to be obtained. If the time is printed once per second on the serial port, it means success.
Related links:
Routine link in this chapter