BSP430
20141115
Board Support Package for MSP430 microcontrollers
|
Support for maintaining a system uptime counter. More...
Go to the source code of this file.
Data Structures | |
struct | sBSP430uptimeActivityTotals |
struct | sBSP430uptimeNTPShortFormat |
struct | sBSP430uptimeNTPTimestamp |
struct | sBSP430uptimeNTPPacketHeader |
Typedefs | |
typedef struct sBSP430uptimeActivityTotals | sBSP430uptimeActivityTotals |
typedef struct sBSP430uptimeNTPShortFormat | sBSP430uptimeNTPShortFormat |
typedef struct sBSP430uptimeNTPTimestamp | sBSP430uptimeNTPTimestamp |
typedef struct sBSP430uptimeNTPPacketHeader | sBSP430uptimeNTPPacketHeader |
Support for maintaining a system uptime counter.
This module provides routines to initialize and query a long-term clock, using a platform-selected timer sourced by an undivided ACLK.
vBSP430uptimeStart_ni() must be invoked on hardware initialization after configuring the system clocks. This is done for you in vBSP430platformInitialize_ni() if configBSP430_UPTIME is true.
Normally BSP430_PERIPH_TA0 is used for this function, but see BSP430_UPTIME_TIMER_PERIPH_CPPID if you want to select a different timer.
#define BSP430_UPTIME include <bsp430/platform.h> |
Defined to a true value if BSP430_UPTIME_TIMER_PERIPH_CPPID has been provided, making the uptime infrastructure available.
#define BSP430_UPTIME_AS_TEXT_LENGTH sizeof("HHH:MM:SS.mmm") |
Expected length for a buffer used by xBSP430uptimeAsText().
This macro may be used to allocate such a buffer.
At 32 kiHz resolution a 32-bit integer holding ticks can only represent 36 hours before wrapping, but at 10 kHz resolution the same counter might express up to 119 hours, so this allows durations up to up to 999:59:59.999 to be expressed.
#define BSP430_UPTIME_DELAY_CCIDX 1 |
The capture/compare index to be used for delays.
If configBSP430_UPTIME_DELAY is enabled this capture/compare register index will be used on the uptime timer to support delays using the Alarm-related Timer Functionality infrastructure.
#define BSP430_UPTIME_DELAY_MS | ( | delay_ms_, | |
lpm_bits_, | |||
exit_expr_ | |||
) |
Wrapper around BSP430_UPTIME_DELAY_MS_NI() suitable for use when interrupts are enabled.
#define BSP430_UPTIME_DELAY_MS_NI | ( | delay_ms_, | |
lpm_bits_, | |||
exit_expr_ | |||
) |
Macro to simplify basic delay-in-milliseconds operations.
Wrapper around BSP430_UPTIME_DELAY_UTT_NI() to support delays specified in milliseconds.
delay_ms_ | duration, in milliseconds, that application should sleep |
lpm_bits_ | as with BSP430_UPTIME_DELAY_UTT_NI() |
exit_expr_ | as with BSP430_UPTIME_DELAY_UTT_NI() |
#define BSP430_UPTIME_DELAY_UTT | ( | delay_utt_, | |
lpm_bits_, | |||
exit_expr_ | |||
) |
Wrapper around BSP430_UPTIME_DELAY_UTT_NI() suitable for use when interrupts are enabled.
#define BSP430_UPTIME_DELAY_UTT_NI | ( | delay_utt_, | |
lpm_bits_, | |||
exit_expr_ | |||
) |
Macro to simplify basic delay-in-ticks operations
This invokes lBSP430uptimeSleepUntil() in a loop for a specified duration or until another event has occurred.
delay_utt_ | duration, in uptime ticks, that application should sleep |
lpm_bits_ | the LPM mode in which the application should sleep. See corresponding parameter in lBSP430uptimeSleepUntil_ni(). |
exit_expr_ | an expression that will be tested prior to sleeping. If it evaluates to true, the delay will be aborted immediately regardless of remaining time. Pass 0 if you wish to delay regardless of other activity. |
#define BSP430_UPTIME_DIVIDING_SHIFT 0 |
Shift value dividing BSP430_UPTIME_TASSEL for the uptime clock.
#define BSP430_UPTIME_MS_TO_UTT | ( | ms_ | ) | BSP430_CORE_MS_TO_TICKS((ms_), ulBSP430uptimeConversionFrequency_Hz()) |
Convert from milliseconds to ticks of the uptime timer.
#define BSP430_UPTIME_TASSEL TASSEL_1 /* == TASSEL__ACLK */ |
Source selector for uptime clock. This should be bits suitable for storage into the TASSEL field of a timer control register. (Note that TASSEL and TBSSEL support the same values; this works when using TBx as well.)
#define BSP430_UPTIME_TIMER_PERIPH_CPPID include "bsp430_config.h" |
Define to the preprocessor-compatible identifier for a timer that should be used to maintain a continuous system clock sourced from ACLK. The define must appear in the BSP430 Configuration subsystem so that functional resource requests are correctly propagated to the underlying resource instances.
#define BSP430_UPTIME_TIMER_PERIPH_HANDLE platform or application specific |
Defined to the peripheral identifier for a timer that can be used to maintain a continuous system clock sourced from ACLK. This derives directly from BSP430_UPTIME_TIMER_PERIPH_CPPID, but is a timer peripheral handle suitable for use in code.
The corresponding HAL and primary ISR features are automatically enabled for this peripheral. CC0 ISR inclusion is influenced by configBSP430_UPTIME_TIMER_HAL_CC0_ISR.
#define BSP430_UPTIME_US_TO_UTT | ( | us_ | ) | BSP430_CORE_US_TO_TICKS((us_), ulBSP430uptimeConversionFrequency_Hz()) |
Convert from microseconds to ticks of the uptime timer.
#define BSP430_UPTIME_UTT_TO_MS | ( | utt_ | ) | BSP430_CORE_TICKS_TO_MS((utt_), ulBSP430uptimeConversionFrequency_Hz()) |
Convert from ticks of the uptime timer to milliseconds.
#define BSP430_UPTIME_UTT_TO_US | ( | utt_ | ) | BSP430_CORE_TICKS_TO_US((utt_), ulBSP430uptimeConversionFrequency_Hz()) |
Convert from ticks of the uptime timer to microseconds.
#define configBSP430_UPTIME 0 |
Define to a true value to enable the uptime infrastructure to maintain a continuous system clock. A timer that will support this must be identified; see BSP430_UPTIME_TIMER_PERIPH_CPPID.
#define configBSP430_UPTIME_DELAY 0 |
Define to a true value to support uptime-driven delays.
This flag enables infrastructure support to use the Alarm-related Timer Functionality infrastructure on the uptime timer to support application delays. The core API for this capability is lBSP430uptimeSleepUntil() and BSP430_UPTIME_DELAY_MS_NI().
#define configBSP430_UPTIME_EPOCH 0 |
Define to a true value to support uptime timer epochs.
This flag enables infrastructure support to use the Epoch Support for the Uptime Timer infrastructure to translate between civil time values such as UTC (represented as NTP timestamps or struct timeval
) and uptime clocks, by providing a precise epoch that denotes the time at which the uptime timer last rolled over.
typedef struct sBSP430uptimeActivityTotals sBSP430uptimeActivityTotals |
A structure to record the relative levels of activity and sleep.
Primarily this can be used to calculate the duty cycle of an application. This infrastructure assumes that the uptime clock runs continuously, including in sleep mode, and that no active or sleep period exceeds the duration representable in an unsigned long.
|
static |
Determine whether an uptime value is valid.
In some contexts, a clock value of zero may be used used to indicate an unassigned value, and a clock value of ~0UL is used to indicate an assigned invalid value. Determine whether the provided time is valid (i.e., not one of these reserved identifying values).
|
static |
Get access to the timer controlling the uptime infrastructure.
You might need this if you want to hook into its callbacks.
Don't hook into the overflow callback, please.
int iBSP430uptimeDelaySetEnabled_ni | ( | int | enablep | ) |
Enable or disable the alarm used by ulBSP430uptimeDelayUntil().
This delegates to iBSP430timerAlarmSetEnabled_ni().
The alarm is enabled by vBSp430uptimeStart_ni() if configBSP430_UPTIME_DELAY is enabled.
long lBSP430uptimeSleepUntil | ( | unsigned long | setting_utt, |
unsigned int | lpm_bits | ||
) |
Sleep until the uptime timer reaches a particular value.
This uses BSP430_UPTIME_DELAY_CCIDX and the Alarm-related Timer Functionality to sleep until either a specific time has been reached or an interrupt wakes the processor.
setting_utt | The uptime counter value at which the application wishes to be awakened if no events have occured before then. Note that the call is a wrapper around iBSP430timerAlarmSet_ni() and may return immediately with an error result if the specified time appears to be in the past. |
lpm_bits | The bits to be set in the status register to control the low power mode used while awaiting the wakeup time. A value such as LPM0_bits or LPM2_bits should be used depending on application needs. GIE will be added to these bits. |
|
static |
Return the per-mille activity measure.
This is the ratio of total active time to total time, expressed as parts-per-thousand. The value is rounded to the nearest integer.
sp | pointer to the activity record. |
|
static |
Return the low word of the system uptime counter.
This is suitable for use by applications that wish to configure capture/compare registers on hBSP430uptimeTimer().
unsigned int uiBSP430uptimeScaleForDisplay | ( | unsigned long long | duration_utt, |
const char ** | unitp | ||
) |
Convert a duration in ticks to a scaled value suitable for display.
This is useful when the code needs to accommodate a wide range of times, from microseconds to hours. The duration value is scaled to reduce the number of digits required to represent it, and the selected unit specifier returned in an output parameter. The cutoff for each unit is:
Time | Units |
---|---|
< 10ms | us |
< 10s | ms |
< 3min | s |
< 3h | min |
>= 3h | h |
duration_utt | a count in ticks |
unitp | a pointer to where to store the standard unit description for the scaled value |
|
static |
Return the system uptime in clock ticks.
|
static |
Return system uptime in clock ticks with disabled interrupts.
vBSP430timerSafeCounterInitialize_ni() will have been called on BSP430_UPTIME_TIMER_PERIPH_HANDLE so BSP430_TIMER_VALID_COUNTER_READ_CCIDX is reserved to support accurate counter reads.
unsigned long ulBSP430uptimeConversionFrequency_Hz | ( | void | ) |
Return the uptime clock conversion frequency.
This is the number of uptime ticks in a standard second, and is used when translating between tick measurements and second-based durations. The default value is the estimated frequency of the timer underlying the uptime clock (per ulBSP430timerFrequency_Hz_ni()), but this may be overridden by ulBSP430uptimeSetConversionFrequency_ni() when a more accurate estimate of actual frequency is available.
This value is set when vBSP430uptimeStart_ni() or vBSP430uptimeResume_ni() are invoked. Its value is undefined when the uptime timer is stopped. Reconfiguration of the timer source or the underlying clock while the timer is running may invalidate its value.
unsigned long ulBSP430uptimeSetConversionFrequency_ni | ( | unsigned long | frequency_Hz | ) |
Set the uptime frequency that will be used for tick/time conversion.
In cases where the uptime clock derives from VLOCLK, the nominal frequency used by default may be off by as much as 10% from the actual frequency. This function can be used when a more accurate estimate of actual frequency is available to decrease the error in converted times.
frequency_Hz | the frequency to be used when converting durations in ticks to durations in seconds or related units. If a value of zero is provided, the current nominal frequency will be used in subsequent conversions. |
|
static |
Adjust a timestamp if necessary to make it valid.
If the value of the timestamp happens to match one of the reserved invalid values, invert the least significant bit so the value is a valid time, albeit off by one tick.
(A legitimate uptime clock will coincidently match one of the invalid values only for two clock ticks at each 32-bit uptime clock rollover.)
|
static |
Return the system uptime in clock ticks at its full precision
This incorporates the overflow and the current counter. On the MSP430 the result is expected to be a 64-bit value of which the low 48-bits are valid.
|
static |
Adjust a captured 16-bit counter to a full resolution timestamp.
This obtains the current time, then adjusts it backwards to be the most recent time for which the low 16 bits match ctr
. The result should be a 48-bit timestamp expressed in a 64-bit unsigned integer.
void vBSP430uptimeActivityReset | ( | sBSP430uptimeActivityTotals * | sp, |
bool | from_now | ||
) |
Initialize state to record activity levels.
This can be used to measure the amount of time spent in active and sleep modes, with the following pattern:
See uiBSP430uptimeActivity_ppt() for extracting the basic duty cycle ratio.
sp | pointer to the activity record to initialize |
from_now | whether accounting should start from now. Passing false is normal, and counts active durations from power-up. If the application initialization code should be excluded, pass true to set the last sleep and wake to the current time. |
|
static |
Record activity times after waking from sleep mode.
sp | pointer to the activity record. |
|
static |
Record activity times prior to entering sleep mode.
sp | pointer to the activity record. |
void vBSP430uptimeResume_ni | ( | void | ) |
Resume the system uptime clock
The clocks are re-enabled to continue counting from their last recorded value. Also, the conversion frequency used by ulBSP430uptimeConversionFrequency_Hz() is updated based on the current timer configuration.
void vBSP430uptimeStart_ni | ( | void | ) |
Configure the system uptime clock.
The timer associated with the uptime clock is reset to zero and begins counting up.
void vBSP430uptimeSuspend_ni | ( | void | ) |
Suspend the system uptime clock.
Suspension means that the clocks are halted and interrupts from them inhibited. Counter values are not modified. vBSP430uptimeResume_ni should be invoked to release the clocks to continue counting.
The system clock may be suspended prior to entering certain low power modes.
const char* xBSP430uptimeAsText | ( | unsigned long | duration_utt, |
char * | buffer | ||
) |
Convert an uptime count to text HHH:MM:SS.mmm format.
At least the MM:SS.mmm portion is present, with minutes space-padded on the left. If the duration exceeds 59:59.999, then space-padded hours will be included as well, and minutes will be zero-padded.
The conversion factor from ulBSP430uptimeConversionFrequency_Hz() is used.
duration_utt | a duration in uptime ticks |
buffer | a pointer to a buffer large enough to hold the represented data, normally BSP430_UPTIME_AS_TEXT_LENGTH characters |
buffer
const char* xBSP430uptimeAsText_ni | ( | unsigned long | duration_utt | ) |
Convert an uptime count to text HH:MM:SS.mmm format in a static buffer.
Invokes xBSP430uptimeAsText() with a static 14-character buffer adequate for representing durations up to 999:59:59.999 in contexts where allocating storage is problematic.
duration_utt | see xBSP430uptimeAsText() |