BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Data Structures | Macros | Typedefs | Functions
Epoch Support for the Uptime Timer

bsp430/utility/uptime.h data structures and functions supporting conversion between civil time and the uptime facility. More...

Data Structures

struct  sBSP430uptimeNTPShortFormat
 
struct  sBSP430uptimeNTPTimestamp
 
struct  sBSP430uptimeNTPPacketHeader
 

Macros

#define BSP430_UPTIME_POSIX_EPOCH_NTPIS   2208988800UL
 
#define BSP430_UPTIME_BYPASS_EPOCH_NTP   ((uint64_t)(BSP430_UPTIME_POSIX_EPOCH_NTPIS + 1388534400UL) << 32)
 
#define BSP430_UPTIME_EPOCH_UPDATE_INTERVAL_UTT   0x40000000L
 
#define BSP430_UPTIME_EPOCH_VALID_OFFSET_UTT   0x60000000L
 

Typedefs

typedef struct sBSP430uptimeNTPShortFormat sBSP430uptimeNTPShortFormat
 
typedef struct sBSP430uptimeNTPTimestamp sBSP430uptimeNTPTimestamp
 
typedef struct sBSP430uptimeNTPPacketHeader sBSP430uptimeNTPPacketHeader
 

Functions

int iBSP430uptimeCheckEpochValidity ()
 
unsigned long ulBSP430uptimeLastEpochUpdate ()
 
long lBSP430uptimeEpochAge ()
 
int iBSP430uptimeEpochEra (unsigned long time_utt)
 
int iBSP430uptimeInitializeNTPRequest (sBSP430uptimeNTPPacketHeader *ntpp)
 
int iBSP430uptimeSetNTPXmtField (sBSP430uptimeNTPPacketHeader *ntpp, unsigned long *putt)
 
int iBSP430uptimeSetEpochFromNTP (uint64_t epoch_ntp)
 
int iBSP430uptimeAdjustEpochFromNTP (int64_t adjustment_ntp)
 
int iBSP430uptimeSetEpochFromTimeval (const struct timeval *tv, unsigned long when_utt)
 
int iBSP430uptimeAsNTP (unsigned long utt, uint64_t *ntpp, int bypass_validation)
 
int iBSP430uptimeAsTimeval (unsigned long utt, struct timeval *tv)
 
time_t xBSP430uptimeAsPOSIXTime (unsigned long utt)
 
int iBSP430uptimeProcessNTPResponse (const sBSP430uptimeNTPPacketHeader *req, const sBSP430uptimeNTPPacketHeader *resp, uint64_t rec_ntp, int64_t *adjustment_ntp, long *adjustment_ms, unsigned long *rtt_us)
 

Detailed Description

bsp430/utility/uptime.h data structures and functions supporting conversion between civil time and the uptime facility.

Note
This functionality depends on configBSP430_UPTIME_EPOCH.

The uptime clock in BSP430 supports a 32-bit monotonically non-decreasing fixed-frequency counter used for low-resolution timing. Normally the clock is based on a 32 kiHz crystal; this can be divided, or the clock may be based on VLOCLK at roughly 10 kHz. Thus the minimum duration representable without wrapping is about 36 hours. The era of the uptime clock is the number of times it has wrapped since the system started. (For your amusement, the era can be found in the upper 16 bits of the overflow counter of the associated timer, but it is not used in this feature.)

In some applications it's necessary to correlate the internal time with a time standard such as UTC. While many MSP430s have a real-time clock that can assist in representing long durations, the key issue is registering the internal clock with a time standard. BSP430 supports this with the concept of an (uptime) epoch, being a representation of a time that corresponds to the start of an era, accurate to the resolution of the uptime clock.

As the era can change asynchronously to epoch updates, the following expectation affect use of the uptime epoch facility:

Homepage
http://github.com/pabigot/bsp430

Macro Definition Documentation

#define BSP430_UPTIME_BYPASS_EPOCH_NTP   ((uint64_t)(BSP430_UPTIME_POSIX_EPOCH_NTPIS + 1388534400UL) << 32)

An NTP timestamp for a time known to be within 34 years of any current server. The calculations of offset and round trip in section 8 "On-wire Protocol" of RFC5905 are valid only when the client and server are no more than 34 years apart. This is the epoch used when bypass_validity is used in iBSP430uptimeAsNTP(), as is the case when doing NTP client/server communications prior to obtaining a valid epoch.

Dependency:
configBSP430_UPTIME_EPOCH
#define BSP430_UPTIME_EPOCH_UPDATE_INTERVAL_UTT   0x40000000L

The maximum number of uptime ticks between updates to the epoch, if epoch consistency is to be maintained.

Dependency:
configBSP430_UPTIME_EPOCH
#define BSP430_UPTIME_EPOCH_VALID_OFFSET_UTT   0x60000000L

The maximum number of uptime ticks between an uptime value and the last time the epoch was updated that will not cause epoch invalidation if used to convert the uptime value.

Dependency:
configBSP430_UPTIME_EPOCH
#define BSP430_UPTIME_POSIX_EPOCH_NTPIS   2208988800UL

The time of the POSIX epoch (1970-01-01T00:00:00Z) as represented in integral seconds since the NTP epoch (1900-01-01T00:00:00Z).

Dependency:
configBSP430_UPTIME_EPOCH

Typedef Documentation

RFC5905 (NTP v4) packet header. Extension fields are not supported in this implementation.

Note
In almost no situations does a user need to inspect or mutate the fields of this structure. Use iBSP430uptimeInitializeNTPRequest() to initialize the structure, and iBSP430uptimeSetNTPXmtField() to set the transmission time.
Dependency:
configBSP430_UPTIME_EPOCH

RFC5905 (NTP v4) structure to represent a short-format time (more accurately, a duration, as there is no epoch).

Dependency:
configBSP430_UPTIME_EPOCH

RFC5905 (NTP v4) structure to represent a timestamp, measured in 2^32 Hz ticks since the NTP epoch 1900-01-01T00:00:00Z.

Dependency:
configBSP430_UPTIME_EPOCH

Function Documentation

int iBSP430uptimeAdjustEpochFromNTP ( int64_t  adjustment_ntp)

Adjust the uptime epoch using a relative NTP timestamp.

Parameters
adjustment_ntpthe number of ticks of a 2^32 Hz clock that must be added to the epoch to cause the local time to coincide with a reference clock.
Dependency:
configBSP430_UPTIME_EPOCH
int iBSP430uptimeAsNTP ( unsigned long  utt,
uint64_t *  ntpp,
int  bypass_validation 
)

Convert a time to an NTP timestamp.

Parameters
uttcount of ticks since the last time the uptime clock rolled over.
ntppwhere to store the time represented by utt as the number of 2^32 Hz ticks since the NTP epoch (1900-01-01T00:00:00Z).
bypass_validationa flag that bypasses the normal check to see that the epoch is valid. When set to a true value and the epoch is invalid, a pseudo-epoch corresponding to 2014-01-01T00:00:00Z is used so that a client NTP request packet can be formed with a valid transmission timestamp. You will need to set this flag when obtaining the rec_ntp parameter to iBSP430uptimeProcessNTPResponse() when the epoch is invalid. If the epoch is valid, this flag is ignored.
Returns
0 if conversion is successful, otherwise a negative error code.
Dependency:
configBSP430_UPTIME_EPOCH
int iBSP430uptimeAsTimeval ( unsigned long  utt,
struct timeval *  tv 
)

Determine the current time to microsecond resolution by adding the uptime clock time in utt to a configured offset.

Parameters
uttthe uptime clock value
tvwhere to store the converted time. The referenced object is left unchanged if the conversion fails.
Returns
0 on success, a negative error code if the epoch is not valid or something else went wrong.
Dependency:
configBSP430_UPTIME_EPOCH
int iBSP430uptimeCheckEpochValidity ( )

Return 0 if there is an epoch that is presumed to be valid, or a negative error if no epoch is available.

Dependency:
configBSP430_UPTIME_EPOCH
int iBSP430uptimeEpochEra ( unsigned long  time_utt)

Determine which era time_utt belongs to for the purposes of conversion using the current epoch.

Reasons for invalidity include:

Parameters
time_uttthe time at which conversion is being proposed.
Returns
a negative code if time_utt cannot be converted. Non-negative results indicate the era, relative to the era the epoch was set for, in which time_utt lies (0 for the preceding, 1 for the same, 2 for the next).
Dependency:
configBSP430_UPTIME_EPOCH
int iBSP430uptimeInitializeNTPRequest ( sBSP430uptimeNTPPacketHeader ntpp)

Initialize an NTP packet to serve as a request for the current time.

This initializes the leap-information, version, and mode flags, and sets the precision based on the local uptime clock. No timestamps are stored; see iBSP430uptimeNTPUpdateXmtField().

Returns
0 on success, a negative error code on failure.
Dependency:
configBSP430_UPTIME_EPOCH
int iBSP430uptimeProcessNTPResponse ( const sBSP430uptimeNTPPacketHeader req,
const sBSP430uptimeNTPPacketHeader resp,
uint64_t  rec_ntp,
int64_t *  adjustment_ntp,
long *  adjustment_ms,
unsigned long *  rtt_us 
)

Process an NTP response.

This calculates the offset between the local uptime clock and the time provided by an external server.

The request and response packets are validated for server stratum, mismatched, and duplicate/bogus/replayed packets. Failure of these checks results in an error return.

Warning
On successful processing, the internally-stored uptime epoch is adjusted to account for the difference between local and server times.
Parameters
reqpointer to the locally-generated request packet. This may be null if you don't care to (or cannot) validate that the response origin time matches the request transmit time.
resppointer to the response packet. This must be provided and must be valid.
rec_ntpthe time when the response packet was received locally, in NTP 2^32 Hz ticks since the NTP epoch. Use iBSP430uptimeAsNTP() to convert from a local uptime measurement.
adjustment_ntpoptional (but recommended) location into which the delta between the local time and the server will be stored (a positive value indicates that the local time is behind the server). When a valid epoch is already available, this value is suitable for passing unmodified to iBSP430uptimeAdjustEpochFromNTP(). When a valid epoch is not available, this value should be added to BSP430_UPTIME_BYPASS_EPOCH_NTP and the result passed to iBSP430uptimeSetEpochFromNTP().
adjustment_msoptional location in which the adjustment, converted to milliseconds, will be stored. This is primarily intended for diagnostics, as a value in milliseconds is more useful than the 2^32 Hz tick count provided by adjustment_ntp. If the offset cannot be represented in the space available, the signed maximum value will be stored. If the offset is not of interest, pass a null pointer. The value is not stored if this function returns an error.
rtt_usoptional location in which to store the estimated round-trip time for request and response. If the round-trip time cannot be represented (which should never happen), the maximum representable value will be stored. If the round-trip time is not of interest, pass a null pointer. The value is not stored if this function returns an error.
Returns
0 on success, a negative error code on failure
Dependency:
configBSP430_UPTIME_EPOCH
int iBSP430uptimeSetEpochFromNTP ( uint64_t  epoch_ntp)

Set the uptime epoch using an absolute NTP timestamp.

Dependency:
configBSP430_UPTIME_EPOCH
int iBSP430uptimeSetEpochFromTimeval ( const struct timeval *  tv,
unsigned long  when_utt 
)

Set the uptime epoch using an absolute Unix time value.

Parameters
tva civil time value
when_uttthe time within the current era at which tv is current.
Returns
0 on success, otherwise a negative error code.
Dependency:
configBSP430_UPTIME_EPOCH
int iBSP430uptimeSetNTPXmtField ( sBSP430uptimeNTPPacketHeader ntpp,
unsigned long *  putt 
)

Update the sBSP430uptimeNTPPacketHeader::xmt field.

This should be called just prior to (or while) transmitting an NTP request to ensure it holds the most accurate estimate of transmission time available.

Parameters
ntpppointer to the NTP packet to be updated
puttpointer to the uptime clock value to be stored as transmission time. If this is a null pointer, the current time is stored.
Returns
0 on success, a negative error code on failure.
Dependency:
configBSP430_UPTIME_EPOCH
long lBSP430uptimeEpochAge ( )

Return the number of uptime ticks since the epoch was last updated.

A negative value is returned if the epoch is not valid, or has not been updated within the last half era.

Dependency:
configBSP430_UPTIME_EPOCH
unsigned long ulBSP430uptimeLastEpochUpdate ( )

Return the uptime clock value at which the epoch was last updated.

Dependency:
configBSP430_UPTIME_EPOCH
time_t xBSP430uptimeAsPOSIXTime ( unsigned long  utt)

Return the current time expressed as seconds since the POSIX epoch.

This invokes iBSP430uptimeAsTimeval() and returns the tv_sec field of the resulting structure.

Note
If the epoch is not valid for the time, the returned value will be (time_t)-1.
Dependency:
configBSP430_UPTIME_EPOCH