cancel
Showing results for 
Search instead for 
Did you mean: 

SNTP Implementaion with LWIP on STM32H743 Series

User431
Associate II

Hello everyone,

I'm new to the forum and also new to the ST Community. I'm currently working on a project where I need to implement SNTP on the H7 series microcontroller using LWIP.

I have gone through the LWIP documentation, but I'm still unable implement SNTP. I have generated all file and configured them but still no output.  Can someone please guide me on how to get started with this?. I have already checked other post for SNTP but still unable to understand what steps to take.

Any help or pointers would be greatly appreciated. Thank you in advance!"

6 REPLIES 6
Pavel A.
Evangelist III

SNTP is basically a subset of NTP v3 (RFC4330). So look up NTP with IEEE 1588 (feature existing in STM32). LwIP does not support IEEE 1588 (as it sits below IP) but can coexist with it. You can begin from simple (S)NTP with LwIP - which is UDP on port 135 - then add IEEE 1588.  Good luck!

 

Don't you just send an empty packet, and get a current time stamp in return?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Sending a packet to NTP server is one of operation modes (soliciting), it is required for measuring round-trip. But there are different modes when a server sends  unsolicited updates, and another mode that has a separate digital 1PPS signal for sync, instead of measuring delays. It's complicated...

User431
Associate II

Hey I followed this post https://community.st.com/t5/stm32cubeide-mcus/lwip-sntp-what-does-it-actually-do/td-p/319642/page/2

and after all setup.

1: IOC file setup (within LWIP setup)

User431_0-1699694031632.png

following is custom code written to test sntp setup:

#include "main.h"
#include "cmsis_os.h"
#include "lwip.h"
#include "usart.h"
#include "gpio.h"
#include "string.h"

#include "lwip/api.h"
#include "lwip/dns.h"

#include "sntp.h"
#include "sntp_opts.h"

extern struct netif gnetif;

void Print_Char(char c)
{
  HAL_UART_Transmit(&huart3, (uint8_t *)&c, 1, 100);
}

void Print_String(char *str)
{
  uint8_t len = strlen(str);
  HAL_UART_Transmit(&huart3, (uint8_t *)str, len, 2000);
}

void Print_IP(uint32_t ip)
{
  char buff[20] = {0};
  uint8_t bytes[4];
  bytes[3] = ip & 0xFF;
  bytes[2] = (ip >> 8) & 0xFF;
  bytes[1] = (ip >> 16) & 0xFF;
  bytes[0] = (ip >> 24) & 0xFF;
  sprintf(buff, "%d.%d.%d.%d\n", bytes[3], bytes[2], bytes[1], bytes[0]);
  HAL_UART_Transmit(&huart3, (uint8_t *)buff, strlen(buff), 1000);
}

void SNTP_Task(void *argument)
{
  struct netconn *conn, *newconn;
  err_t err;
  struct netbuf *buf;
  void *data;
  u16_t len;

  char data_buff[100];
  ip4_addr_t dns_ip;
  ip4_addr_t setdns_ip;

  ipaddr_aton("8.8.8.8",&setdns_ip);

  dns_setserver(2,&setdns_ip);

  Print_String("\nDNS server 1:");
  Print_String(ip4addr_ntoa( dns_getserver(0)));
  Print_String("\nDNS server 2:");
  Print_String(ip4addr_ntoa( dns_getserver(1)));
  Print_String("\nDNS server 3:");
  Print_String(ip4addr_ntoa( dns_getserver(2)));


  Print_String("\nTime Server: ");
  Print_String(sntp_getservername(0));
  Print_String("\nTime Server IP: ");
  Print_IP(sntp_getserver(0));
  Print_Char('\n');
  Print_String("\nTime Server: ");
  Print_String(sntp_getservername(1));
  Print_String("\nTime Server IP: ");
  Print_IP(sntp_getserver(1));
  Print_Char("\n");

}


void dhcpPollTask(void *argument)
{
  uint8_t got_ip_flag = 0;
  struct dhcp *dhcp;

  Print_String("DHCP client started\n");
  Print_String("Acquiring IP address\n");

  for (;;)
  {
	osDelay(5000);

	if (got_ip_flag == 0)
	{
  	if (dhcp_supplied_address(&gnetif))
  	{
   	   got_ip_flag = 1;
   	       	Print_String("\nGot IP Address as:");
   	       	Print_IP(gnetif.ip_addr.addr);
   	       	Print_String("\nSubnet Mask is:");
   	       	Print_IP(gnetif.netmask.addr);
   	       	Print_String("\nGateway IP Address:");
   	       	Print_IP(gnetif.gw.addr);

    	/* new task to handle SNTPr*/
   	 if(sntp_enabled())
   	 {
   		sntp_stop();
   	 }

   	 ip4_addr_t settime_ip1;
   	 ip4_addr_t settime_ip2;

   	 ipaddr_aton("40.81.94.65",&settime_ip1);
   	 ipaddr_aton("216.239.35.8",&settime_ip2);

   	 sntp_setoperatingmode(SNTP_OPMODE_POLL);
   	 sntp_setserver(0, &settime_ip1);
   	 sntp_setserver(1, &settime_ip2);
   	 //sntp_setservername(0, "time.windows.com");
   	 //sntp_setservername(1, "time.google.com");
   	 sntp_init();
    	sys_thread_new("SNTP_Task", SNTP_Task, NULL, 1024, osPriorityNormal);

  	}
  	else
  	{
    	Print_Char('.');

    	dhcp = (struct dhcp *)netif_get_client_data(&gnetif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);

    	/* DHCP timeout */
    	if (dhcp->tries > 4)
    	{
      	/* Stop DHCP */
      	dhcp_stop(&gnetif);
      	Print_String("\nCould not acquire IP address. DHCP timeout\n");
      	osThreadSuspend(NULL);
    	}
  	}
	}
  }
}


void Add_User_Threads()
{
  /* creat a new task to check if got IP */
  sys_thread_new("dhcpPollTask", dhcpPollTask, NULL, 256, osPriorityNormal);
}

to run this code created a new file and added to main.c as

User431_4-1699694655068.png

But when I debug i dint get correct  SNTP server address.

User431_3-1699694582613.png

I triend both ip and dns method.

checked for dns setting of sntp

User431_1-1699694249111.png

I may be getting error in sntp.c file of lwip in line

User431_2-1699694442290.png

Any help please.

Thank you in advance!

Pavel A.
Evangelist III

>I may be getting error in sntp.c file of lwip in line

Which error do you see there? ERR_INPROGRESS or other?

User431
Associate II

the dns query is unable to find correct ip.

also the direct ip method without dns is also not working. as sntp server is set to random value.