BSP430
20141115
Board Support Package for MSP430 microcontrollers
|
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) |
bsp430/utility/uptime.h data structures and functions supporting conversion between civil time and the uptime facility.
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:
#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.
#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.
#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.
#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).
typedef struct sBSP430uptimeNTPPacketHeader sBSP430uptimeNTPPacketHeader |
RFC5905 (NTP v4) packet header. Extension fields are not supported in this implementation.
typedef struct sBSP430uptimeNTPShortFormat sBSP430uptimeNTPShortFormat |
RFC5905 (NTP v4) structure to represent a short-format time (more accurately, a duration, as there is no epoch).
typedef struct sBSP430uptimeNTPTimestamp sBSP430uptimeNTPTimestamp |
RFC5905 (NTP v4) structure to represent a timestamp, measured in 2^32 Hz ticks since the NTP epoch 1900-01-01T00:00:00Z.
int iBSP430uptimeAdjustEpochFromNTP | ( | int64_t | adjustment_ntp | ) |
Adjust the uptime epoch using a relative NTP timestamp.
adjustment_ntp | the 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. |
int iBSP430uptimeAsNTP | ( | unsigned long | utt, |
uint64_t * | ntpp, | ||
int | bypass_validation | ||
) |
Convert a time to an NTP timestamp.
utt | count of ticks since the last time the uptime clock rolled over. |
ntpp | where 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_validation | a 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. |
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.
utt | the uptime clock value |
tv | where to store the converted time. The referenced object is left unchanged if the conversion fails. |
int iBSP430uptimeCheckEpochValidity | ( | ) |
Return 0 if there is an epoch that is presumed to be valid, or a negative error if no epoch is available.
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:
time_utt
is more than BSP430_UPTIME_EPOCH_VALID_OFFSET_UTT ticks earlier or later than the time the epoch was last updatedtime_utt | the time at which conversion is being proposed. |
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).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().
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.
req | pointer 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. |
resp | pointer to the response packet. This must be provided and must be valid. |
rec_ntp | the 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_ntp | optional (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_ms | optional 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_us | optional 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. |
int iBSP430uptimeSetEpochFromNTP | ( | uint64_t | epoch_ntp | ) |
Set the uptime epoch using an absolute NTP timestamp.
int iBSP430uptimeSetEpochFromTimeval | ( | const struct timeval * | tv, |
unsigned long | when_utt | ||
) |
Set the uptime epoch using an absolute Unix time value.
tv | a civil time value |
when_utt | the time within the current era at which tv is current. |
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.
ntpp | pointer to the NTP packet to be updated |
putt | pointer to the uptime clock value to be stored as transmission time. If this is a null pointer, the current time is stored. |
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.
unsigned long ulBSP430uptimeLastEpochUpdate | ( | ) |
Return the uptime clock value at which the epoch was last updated.
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.
(time_t)-1.