Setting RTCC using SNTP

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to use the Simple Network Time Protocol (SNTP) to set the Real Time Clock Calendar (RTCC) available in the Flyport.

Introduction to SNTP

The SNTP service is a Simple Network Time Protocol, a simplified version of Network Time Protocol (NTP) for embedded devices. NTP is a networking protocol for clock synchronization between computer systems over packet-switched, variable-latency data networks.

How to use it

The openPicus APIs contain the commands to manage the RTCC. It is possible to read/write the date and time values and set an alarm, including a callback function to execute when the alarm event is triggered. To use the APIs is necessary to check the corresponding flag in the Wizard.

The result of a SNTP request will be a 32 bit word (DWORD) that is the count of seconds from January 1st 1970 to the current time. So 0 is the value related to January 1st, 1970 00h:00min:00sec, 3 is the value at January 1st 1970 00h:00min:03sec, and so on. To extract the actual time from the 32-bit value obtained from an SNTP request you can use converting functions. To convert the DWORD value to a user friendly structure in the openPicus framework, 3 processing steps are needed:

DWORD → time_t → struct tm

Using this 3 steps conversion, all the timing values become easy to access and are formatted as minutes, seconds, days, months, etc... To perform the conversion is necessary to create a new file called “heap.s”. The contents of heap.s is:

.section my_heap,heap
.space 0x0080

The heap.s file contains the commands to properly set Flyport's memory to run the time conversion functions. This file can be created and added to your project using the tool “External Libs” (available on IDE 2.3 or newer).

NOTE: Remember to enable the SNTP Client in the Wizard of the IDE. Flyport needs an active internet connection to retrieve the SNTP data!

Bill of Materials

  • 1x Flyport Module
  • 1x miniUSB programmer
  • 1x Proto Nest Board

Code example


The functions used in this tutorial are the following:

Name Function Description
 DWORD SNTPGetUTCSeconds() Reads the actual date/time from the NTP server and return a DWORD that is the count of seconds from January 1st 1970 to the current time.
 void RTCCSet(struct tm *  rtcc) Sets the date/time for the RTCC and enables the RTCC module.

Parameters: rtcc A pointer to a struct tm variable, containing the date and the time to set

 void RTCCGet(struct tm *  rtcc) Reads the actual date/time from the RTCC and put it inside a struct pointer.

Parameters: rtcc A pointer to the struct tm variable to write.


Source code for taskFlyport.c:

#include "taskFlyport.h"
#include "time.h"
 
char string_serial[50]="waiting...";
time_t now;
struct tm *ts;
struct tm mytime;
 
DWORD epoch=0;
DWORD epochtime=0xA2C2A;
char dateUTC[100];
char dateUTC1[50];
 
// to properly set GMT by adding or removing
// the hours for GMT zone (for example Rome = +1 or +2 GMT)
// negative values are supported too...
int GMT_hour_adding = 1;
 
void FlyportTask()
{
  // Flyport Wi-Fi, change in accordingly to your Flyport module
  WFConnect(WF_DEFAULT);
  while (WFStatus != CONNECTED);
  UARTWrite(1,"Flyport connected... \r\n");
  vTaskDelay(200);
  UARTWrite(1,string_serial);
  while(epoch<epochtime)
  {
    vTaskDelay(50);
    //Remember to enable the SNTP Client in the wizard.
    epoch=SNTPGetUTCSeconds();
  }
  UARTWrite(1, "done!\r\n");
  //To convert the DWORD value to a user friendly structure in the openPicus framework, 
  //3 processing steps are needed: DWORD → time_t → struct tm 
  epoch=SNTPGetUTCSeconds();
  now=(time_t)epoch;
  ts=localtime(&now);
  vTaskDelay(20);
  ts->tm_hour = (ts->tm_hour + GMT_hour_adding);
  // Correct if overflowed hour 0-24 format
  if(ts->tm_hour > 24)
  {
    ts->tm_hour = ts->tm_hour - 24;
  }
  else if(ts->tm_hour < 0)
  {
    ts->tm_hour = ts->tm_hour +24;
  }	
  sprintf (string_serial, "\nReceived date/time is: %s \r\n", asctime (ts) );
  UARTWrite(1,string_serial);
  RTCCSet(ts); //Set time
  while(1)
  {
     vTaskDelay(1000);
     RTCCGet(&mytime);//get actual data/time
     sprintf (string_serial, "\nActual date/time is: %s \r\n", asctime (&mytime) );
     UARTWrite(1,string_serial);
   }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox