Categories
VSCP

nano second timestamp

black and white photo of clocks
Photo by Andrey Grushnikov on Pexels.com

The 64-bit nano second timestamp is now introduced for VSCP. This is what we often call a Unix timestamp with nano second resolution and which holds the number of nanoseconds since the epoch (January 1, 1970, 00:00:00 UTC).

The “old” format with separate bytes for year, month, day, hour, minute, second and a 32-bit microsecond timestamp was constructed that way to make it easy for very low-end devices to receive/send time information. This is still possible as the format is preserved. But the new format will be promoted.

Note that it is still possible to send a microsecond timestamp for devices that rely on it. Just multiply the value with 1000 and set it to timestamp_ns. Relative timestamps can easily still be used.

Two header bits 9/8 specify the frame format. They were originally set to 0b00 and would with the new format be set to 0b01. So when a new event is constructed they shoudl be set. The define VSCP_HEADER16_FRAME_VERSION_UNIX_NS in vscp.h does that. Priority is probably also bits that should be altered usually to VSCP_PRIORITY_NORMAL

Helpers to convert between the two formats are available in the helper library and in vscphelper.cpp and vscp-firmware-helper.c. They are

int64_t
vscp_fwhlp_to_unix_ns(int year, int month, int day, int hour, int minute, int second, uint32_t microsecond);

and

void
vscp_fwhlp_from_unix_ns(int64_t unix_ns,
int *year,
int *month,
int *day,
int *hour,
int *minute,
int *second,
uint32_t *microsecond);

year and month is still available in the event defines also if nanosecond timestamps are used. This to preserve the size of frames. It is recommended to set year=0xffff and month=0xff when the new timestamp is used. Still the frame versin bits in the header is the main detection to use for frame type.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.