Skip to content

Commit

Permalink
prepare for demo
Browse files Browse the repository at this point in the history
Co-authored-by: Albin Wallenius Woxnerud <Albin@woxnerud.se>
  • Loading branch information
vilhelmprytz and albinwwoxnerud committed May 30, 2024
1 parent c1a07ea commit 0402d00
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/FreeRTOSIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ extern void vLoggingPrintf(const char* pcFormatString, ...);
* call to FreeRTOS_gethostbyname() will return immediately, without even creating
* a socket. */
#define ipconfigUSE_DNS_CACHE (1)
#define ipconfigDNS_CACHE_NAME_LENGTH (20)
#define ipconfigDNS_CACHE_NAME_LENGTH (30)
#define ipconfigDNS_CACHE_ENTRIES (4)
#define ipconfigDNS_REQUEST_ATTEMPTS (2)

Expand Down
25 changes: 18 additions & 7 deletions src/NTPTask.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void vStartNTPClientTasks_SingleTasks(uint16_t usTaskStackSize, UBaseType_t uxTa
{
BaseType_t x;

FreeRTOS_printf(("Starting NTPTask. \n"));
// Create the NTP client task
xTaskCreate(vNTPTask, /* Task function */
"NTPSyncTask", /* Task name */
Expand Down Expand Up @@ -62,19 +63,19 @@ static void init_network_and_DNS(const char *pcHostNames[], uint32_t NTP_server_

if (NTP_server_IPs[i] == 0)
{
FreeRTOS_printf(("\n\nDNS lookup failed, retrying in 1 second. \n\n"));
FreeRTOS_printf(("DNS lookup failed, retrying in 1 second. \n"));
vTaskDelay(x1000ms); // Retry delay
}
else
{
FreeRTOS_printf(("\n\nDNS lookup successful for %s\n\n", pcHostNames[i]));
FreeRTOS_printf(("DNS lookup successful for %s\n\n", pcHostNames[i]));
break;
}

// Exit if max retries reached
if (retry == 9)
{
FreeRTOS_printf(("\n\nDNS lookup failed after multiple attempts. Exiting task.\n\n"));
FreeRTOS_printf(("DNS lookup failed after multiple attempts. Exiting task.\n"));
return;
}
}
Expand All @@ -94,13 +95,17 @@ static void vNTPTask(void *pvParameters)
const TickType_t x1000ms = 1000UL / portTICK_PERIOD_MS;
const TickType_t x10000ms = 10000UL / portTICK_PERIOD_MS;

// test demo server ntp-pvk-atomic.hejduk.se
const char *pcHostNames[NMAX] = {"sth1.ntp.se", "sth2.ntp.se", "svl1.ntp.se", "mmo1.ntp.se", "lul1.ntp.se"};

// Array to store resolved IP addresses of NTP servers
uint32_t NTP_server_IPs[NMAX] = {0};
FreeRTOS_printf(("Initializing network and dns \n"));
init_network_and_DNS(pcHostNames, NTP_server_IPs);
FreeRTOS_printf(("Initializing mutex \n"));
initTimeMutex();

FreeRTOS_printf(("Initializing datastructures \n"));
// Allocate and initialize NTP packet structures
struct ntp_r *r = malloc(sizeof(ntp_r));
struct ntp_x *x = calloc(1, sizeof(ntp_x)); // Zero-initialized
Expand Down Expand Up @@ -133,9 +138,11 @@ static void vNTPTask(void *pvParameters)
}

c.jitter = LOG2D(s.precision);
FreeRTOS_printf(("Initializing association table \n"));
assoc_table_init(NTP_server_IPs);

// Initialize time with the first NTP server
FreeRTOS_printf(("Initializing time with the first NTP server \n"));
NTP1_server_IP = NTP_server_IPs[0];
xDestinationAddress.sin_address.ulIP_IPv4 = NTP1_server_IP;
x->srcaddr = NTP1_server_IP;
Expand All @@ -148,30 +155,34 @@ static void vNTPTask(void *pvParameters)
settime(x->xmt);
gettime(1);

FreeRTOS_printf(("Starting synchronization loop\n"));
for (int i = 0; i < NMAX; i++)
{
// Update destination address with current NTP server
NTP1_server_IP = NTP_server_IPs[i];

FreeRTOS_printf(("\n\nGetting time from IP (%s): %lu.%lu.%lu.%lu\n\n", pcHostNames[i], (NTP1_server_IP & 0xFF),
FreeRTOS_printf(("Getting time from IP (%s): %lu.%lu.%lu.%lu\n\n", pcHostNames[i], (NTP1_server_IP & 0xFF),
((NTP1_server_IP >> 8) & 0xFF), ((NTP1_server_IP >> 16) & 0xFF),
((NTP1_server_IP >> 24) & 0xFF)));

prep_xmit(x); // Prepare NTP packet
xmit_packet(x); // Send request
recv_packet(r); // Receive response
FreeRTOS_printf(("Start the algorithms\n"));

// Process the received response
receive(r);
vTaskDelay(x1000ms); // Delay before next init
}
FreeRTOS_printf(("Synchronization loop finished\n"));

// return;

// Main NTP client loop
FreeRTOS_printf(("Start polling loop\n"));
for (;;)
{
// gettime(1); // Get current time
printTimestamp(c.localTime, "current time\n\n");

clock_adjust(r); // Adjust the clock

Expand Down Expand Up @@ -200,9 +211,9 @@ static void vNTPTask(void *pvParameters)
// }
// for (;;)
// {
// FreeRTOS_printf(("\n\nAdjust clock\n\n"));
// FreeRTOS_printf(("Adjust clock\n"));
// clock_adjust(); // Adjust the clock
// FreeRTOS_printf(("\n\nAdjusted clock\n\n"));
// FreeRTOS_printf(("Adjusted clock\n"));

// vTaskDelay(x100ms); // Delay before next adjustment
// }
Expand Down
8 changes: 4 additions & 4 deletions src/NTP_TDMG.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ void gettime(int override)
// calculate the difference between the current tick and the last tick
TickType_t tickDifference = currentTick - c.lastTimeStampTick;

FreeRTOS_printf(("Tick diff\n"));
FreeRTOS_printf_wrapper_double("", tickDifference);
// FreeRTOS_printf(("Tick diff\n"));
// FreeRTOS_printf_wrapper_double("", tickDifference);
if (tickDifference < 10 && !override) return; // Ignore small differences
FreeRTOS_printf(("changing local time...\n\n"));
// FreeRTOS_printf(("changing local time...\n"));

// calculate tickDifference / 1000 as integer division
TickType_t numSecondsInTicks = tickDifference / 1000;
Expand All @@ -243,7 +243,7 @@ void gettime(int override)
uint32_t tempFractions = currentFractions + newFractions;
if (tempFractions < currentFractions)
{ // Check for overflow using wrap-around condition
// FreeRTOS_printf(("Overflow detected\n\n"));
// FreeRTOS_printf(("Overflow detected\n"));
numSecondsInTicks++; // Increment the seconds part due to overflow
}

Expand Down
6 changes: 4 additions & 2 deletions src/NTP_main_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ void recv_packet(ntp_r *r)
// do conversion
// ntp_packet -> ntp_r
prv_create_ntp_r(r, &pkt);
c.localTime = r->rec;
}
else
{
Expand All @@ -192,6 +193,7 @@ void recv_packet(ntp_r *r)
*/
void prep_xmit(ntp_x *x)
{
FreeRTOS_printf(("Preparing packet for send \n"));
x->leap = s.leap;
x->stratum = s.stratum;

Expand Down Expand Up @@ -381,8 +383,8 @@ void step_time(double offset)
*/
void adjust_time(double offset)
{
FreeRTOS_printf(("Adjusting time by:\n"));
FreeRTOS_printf_wrapper_double("", offset);
// FreeRTOS_printf(("Adjusting time by:\n"));
// FreeRTOS_printf_wrapper_double("", offset);
double ntp_time = (offset);
prv_update_localTime(offset, 1);
}
Loading

0 comments on commit 0402d00

Please sign in to comment.