BSP430
20141115
Board Support Package for MSP430 microcontrollers
|
Clock-related functions implemented on all MSP430 MCUs. More...
#include <bsp430/core.h>
Go to the source code of this file.
Typedefs | |
typedef enum eBSP430clockSource | eBSP430clockSource |
Clock-related functions implemented on all MSP430 MCUs.
This module declares functions and macros of general use in managing MSP430 clocks. The interface here is independent of the underlying clock peripheral, but in some cases behavior is refined or clarified in the peripheral-specific header.
This module will include the peripheral-specific header if a functional presence preprocessor symbol is available that is recognized as a supported clock peripheral. Recognized peripherals and the corresponding BSP430 header are:
#define BSP430_CLOCK_CLEAR_FAULTS_NI | ( | ) | peripheral specific |
Clear faults associated with clocks
This clears the state bits associated with fault in all clock systems supported by the peripheral. If a clock still exhibits a fault condition, the bits will be set again automatically, though perhaps not immediately.
#define BSP430_CLOCK_FAULT_RECHECK_DELAY_US 50 |
The delay after clearing clock faults before recurrent faults are expected.
When a clock fault is cleared, the clock peripheral will automatically set the fault indicator again if the clock is still faulted. However there is no evidence in the documentation that this is done instantly. BSP430 infrastructure code will delay this many microseconds after clearing faults before checking to see whether they are still present.
Delay suggested by SLAU144I "2xx Family Users Guide" section 5.2.7.1 "Sourcing MCLK from a Crystal". This applies to using XT2 and for a specific MCU family, but we're guessing it's a sufficient delay to detect faults in other configurations. For DCO faults it's probably too long.
#define BSP430_CLOCK_IS_FAULTED_NI | ( | ) | peripheral specific |
Check whether any clock has a fault condition.
This checks all available clocks on the system. Use peripheral-specific checks for specific clocks.
#define BSP430_CLOCK_LFXT1_IS_FAULTED_NI | ( | ) | peripheral specific |
Check whether the LFXT1 crystal has a fault condition.
The implementation of this is specific to the clock peripheral.
#define BSP430_CLOCK_LFXT1_STABILIZATION_DELAY_CYCLES BSP430_CORE_MS_TO_TICKS(50, BSP430_CLOCK_PUC_MCLK_HZ) |
Define this to the number of MCLK cycles that iBSP430clockConfigureLFXT1_ni should delay, after clearing oscillator faults, before checking for oscillator stability. This must be a compile-time constant integer compatible with unsigned long
.
Crystal stabilization can take hundreds of milliseconds. If this value is too short, iBSP430clockConfigureLFXT1_ni may prematurely decide that the crystal is working; if it is too long, the return from iBSP430clockConfigureLFXT1_ni is delayed.
The default value is chosen to reflect a 50msec delay at the PUC MCLK frequency of roughly 1MHz. This allows BSP430_CORE_WATCHDOG_CLEAR() to be invoked within the loop to prevent a watchdog reset while waiting for stabilization. It does assume that an unstable crystal will indicate a fault within this period, which may not be true.
#define BSP430_CLOCK_LFXT1_XCAP include <bsp430/platform.h> |
Oscillator capacitor setting for use by iBSP430clockConfigureLFXT1_ni.
Most MSP430 clock systems allow control of internal capacitors for the low-frequency crystal. The specific setting is platform-dependent; the value for the setting is peripheral-dependent. A default value is provided in each peripheral header.
The value of this macro should be the bits that need to be set in the peripheral-specific register to control capacitance, normally in the form of a header constant such as XCAP_1 or XCAP10PF.
#define BSP430_CLOCK_NOMINAL_MCLK_HZ 7948800UL |
A constant representing the desired clock speed of the master clock.
If BSP430_PLATFORM_BOOT_CONFIGURE_CLOCKS is true, vBSP430platformInitialize_ni() will use this value to initialize the system clocks. The value should not be used after this point, as ulBSP430clockMCLK_Hz_ni() is more likely to reflect the actual MCLK rate.
#define BSP430_CLOCK_NOMINAL_MODCLK_HZ peripheral specific |
Nominal rate of MODCLK.
MODCLK is an internally-generated high frequency clock source available with certain clock systems (UCS, CS_A, and CS4). The supporting peripheral header will define this if the feature is available, and leave it undefined otherwise.
#define BSP430_CLOCK_NOMINAL_VLOCLK_HZ platform specific around 10-12 kHz |
Nominal frequency of VLOCLK, in Hz.
The value is constant for all platforms using a specific clock peripheral, and is defined in the peripheral header. It is usually in the range of 10-12 kHz, but is highly sensitive to temperature and voltage. The actual value may be off by as much as 20%, making the clock relatively useless for anything that requires accuracy (such as serial baud rates).
#define BSP430_CLOCK_NOMINAL_XT1CLK_HZ 32768U |
Nominal rate of the external low-frequency crystal.
This is expected to be a 32 KiHz watch crystal, so that's what the default is.
#define BSP430_CLOCK_NOMINAL_XT2CLK_HZ include <bsp430/platform.h> |
Nominal rate of a secondary external clock (or crystal), in Hz.
This must be defined externally if eBSP430clockSRC_XT2CLK is to be used. Normally this would be done in the platform-specified header obtained through <bsp430/platform.h>.
If the platform does not support a secondary external clock, or it is normally left unpopulated, this macro will be undefined.
#define BSP430_CLOCK_OSC_CLEAR_FAULT_NI | ( | ) | do { IFG1 &= ~OFIFG; } while (0) |
Clear the system oscillator fault flag.
This clears the bit the special function register which reflects roll-up oscillator faults throughout the system.
#define BSP430_CLOCK_OSC_IS_FAULTED_NI | ( | ) | (IFG1 & OFIFG) |
Check whether the system oscillator fault flag is set.
This checks the special function register which reflects roll-up oscillator faults throughout the system.
#define BSP430_CLOCK_PUC_MCLK_HZ platform specific around 1 MHz |
Nominal frequency of MCLK at power-up, in Hz.
The value is constant for all platforms using a specific clock peripheral, and is defined in the peripheral header. It is usually around 1 MHz.
#define BSP430_CLOCK_US_TO_NOMINAL_MCLK | ( | delay_us_ | ) | BSP430_CORE_US_TO_TICKS((delay_us_), BSP430_CLOCK_NOMINAL_MCLK_HZ) |
A function macro that converts a value specified in microseconds to an approximate duration counted in MCLK cycles.
This is primarily used to provide the parameter to BSP430_CORE_DELAY_CYCLES when the desired delay is specified by time rather than cycles.
delay_us_ | the delay in microseconds. Expected, but not required, to be a compile-time integer constant compatible with unsigned long. |
delay_us_
is large, it may be necessary to cast the delay_us_
to unsigned long long
to avoid integer overflow. #define BSP430_CLOCK_XT2_IS_FAULTED_NI | ( | ) | (1) |
Same as BSP430_CLOCK_LFXT1_IS_FAULTED_NI() but checks XT2.
XT2 is considered to be permanently faulted for clock peripherals and MCUs on which XT2 is not supported.
Check whether the XT2 crystal has a fault condition.
The implementation of this is specific to the clock peripheral.
#define BSP430_CLOCK_XT2_IS_FAULTED_NI | ( | ) | peripheral specific |
Same as BSP430_CLOCK_LFXT1_IS_FAULTED_NI() but checks XT2.
XT2 is considered to be permanently faulted for clock peripherals and MCUs on which XT2 is not supported.
Check whether the XT2 crystal has a fault condition.
The implementation of this is specific to the clock peripheral.
#define BSP430_CLOCK_XT2_STABILIZATION_DELAY_CYCLES (20000UL) |
Same as BSP430_CLOCK_LFXT1_STABILIZATION_DELAY_CYCLES except applies to XT2.
#define BSP430_PERIPH_CS_IS_CS4 (defined(__MSP430_HAS_CS__) && defined(SELA)) |
Identify use of the CS peripheral on FR4xx/2xx devices.
TI fails to distinguish the three types of CS module in the three FRAM families. The one on the FR4xx/2xx is so different it shares nothing but the module name. Empirically they can be distinguished because the FR4xx/2xx version has a single-bit SELA field.
#define BSP430_PERIPH_CS_IS_CSA (defined(__MSP430_HAS_CS_A__) || (defined(__MSP430_HAS_CS__) && defined(DCOFSEL2))) |
Identify use of the CS peripheral on FR58xx devices.
TI fails to distinguish the three types of CS module in the three FRAM families. The original one on the FR57xx is slightly different from the one on the FR58xx family (which was originally identified by TI as CS_A). Empirically CS_A can be distinguished by more bits in the DCOFSEL field.
typedef enum eBSP430clockSource eBSP430clockSource |
Constants used for MCU-independent specification of clocks.
Use these instead of peripheral-specific values like SELA_1 or LFXT1S_2 to identify clock sources by their function. This also permits finer control within iBSP430clockConfigureACLK_ni(), allowing that to select a secondary source given oscillator faults in the primary source.
enum eBSP430clockSource |
Constants used for MCU-independent specification of clocks.
Use these instead of peripheral-specific values like SELA_1 or LFXT1S_2 to identify clock sources by their function. This also permits finer control within iBSP430clockConfigureACLK_ni(), allowing that to select a secondary source given oscillator faults in the primary source.
Enumerator | |
---|---|
eBSP430clockSRC_NONE |
A marker value indicating that no clock is selected. Generally this is not an appropriate value to use as a parameter. |
eBSP430clockSRC_XT1CLK |
XT1 is the primary external clock. It is generally a low-frequency watch crystal running at 32 KiHz. The nominal speed for this clock is BSP430_CLOCK_NOMINAL_XT1CLK_HZ. |
eBSP430clockSRC_VLOCLK |
VLOCLK is a low-speed internal clock, normally around 10-12 kHz. It is relatively imprecise. The nominal speed for this clock is BSP430_CLOCK_NOMINAL_VLOCLK_HZ. |
eBSP430clockSRC_REFOCLK |
REFOCLK is an internally low power trimmed 32 KiHz oscillator supported in the UCS peripheral |
eBSP430clockSRC_DCOCLK |
DCOCLK is whatever the DCO clock frequency is. It's probably related to BSP430_CLOCK_NOMINAL_MCLK_HZ, but may be a multiple of that. |
eBSP430clockSRC_DCOCLKDIV |
DCOCLKDIV is a divided DCO clock. It's probably BSP430_CLOCK_NOMINAL_MCLK_HZ, but may be a multiple of that. |
eBSP430clockSRC_SMCLK_PU_DEFAULT |
The default clock source for SMCLK. The selected clock is peripheral-dependent and likely to be one of eBSP430clockSRC_DCOCLK or eBSP430clockSRC_DCOCLKDIV. |
eBSP430clockSRC_XT2CLK |
A secondary external clock. This is only recognized if BSP430_CLOCK_NOMINAL_XT2CLK_HZ has been provided. |
eBSP430clockSRC_HFXTCLK |
Convenience alias for CS_A peripherals which renamed this clock. |
eBSP430clockSRC_MODCLK |
Internal low-power oscillator that exists only on CS_A and CS4 peripherals. |
eBSP430clockSRC_LFMODCLK |
Internal low-power oscillator that exists only on CS_A and CS4 peripherals. |
eBSP430clockSRC_XT1CLK_OR_VLOCLK |
Fallback: use XT1CLK if BSP430_CLOCK_LFXT1_IS_FAULTED_NI() is false, otherwise use VLOCLK. The main value of this is in BC2-based clocks, where selecting XT1CLK as the source for ACLK will cause ACLK to fall back to VLOCLK if the crystal is faulted, but using the resulting ACLK as a timer source will not work. |
eBSP430clockSRC_XT1CLK_OR_REFOCLK |
Fallback: use XT1CLK if BSP430_CLOCK_LFXT1_IS_FAULTED_NI() is false, otherwise use REFOCLK. Similar to eBSP430clockSRC_XT1CLK_OR_VLOCLK for UCS-based systems. |
eBSP430clockSRC_XT1CLK_FALLBACK |
Fallback: use XT1CLK if BSP430_CLOCK_LFXT1_IS_FAULTED_NI() is false, otherwise use the most precise available internal reference clock. Generally this will be equivalent to eBSP430clockSRC_XT1CLK_OR_VLOCLK or eBSP430clockSRC_XT1CLK_OR_REFOCLK, depending on what clock peripheral is available. Use ulBSP430clockACLK_Hz_ni() to determine what the actual (nominal) speed is. |
eBSP430clockSRC_UNKNOWN_1 |
A unique unrecognized value. Not a valid source for clock configuration. |
eBSP430clockSRC_UNKNOWN_4 |
A unique unrecognized value. Not a valid source for clock configuration. |
eBSP430clockSRC_UNKNOWN_5 |
A unique unrecognized value. Not a valid source for clock configuration. |
eBSP430clockSRC_UNKNOWN_6 |
A unique unrecognized value. Not a valid source for clock configuration. |
eBSP430clockSRC_UNKNOWN_7 |
A unique unrecognized value. Not a valid source for clock configuration. |
eBSP430clockSRC_TCLK |
A value representing an external clock, potentially used as a timer source. Not a valid source for clock configuration, and will never compare equal to a clock source value. Only returned from xBSP430timerClockSource(). |
eBSP430clockSRC_ITCLK |
A value representing an inverted external clock, potentially used as a timer source. Not a valid source for clock configuration, and will never compare equal to a clock source value. Only returned from xBSP430timerClockSource(). |
int iBSP430clockConfigureACLK_ni | ( | eBSP430clockSource | sel, |
unsigned int | dividing_shift | ||
) |
Configure ACLK to a source clock.
The peripheral-specific implementation will configure ACLK to source from the requested clock.
sel | the source from which the clock should be selected. Note that the values permit an internal decision, e.g. to prefer LFXT1 but to use an alternative if that is faulted. Configuration is rejected if the requested clock source does not exist on the platform. |
dividing_shift | exponent of a divider used to reduce jitter and/or clock rate. Not all peripherals support this; a value of zero is implicitly used in that situation. |
int iBSP430clockConfigureLFXT1_ni | ( | int | enablep, |
int | loop_limit | ||
) |
Configure (or deconfigure) XT1 as a clock source.
The peripheral-specific implementation will use iBSP430platformConfigurePeripheralPins_ni with BSP430_PERIPH_LFXT1 to configure the crystal. If crystal functionality has been requested, it then clears oscillator faults, delays BSP430_CLOCK_LFXT1_STABILIZATION_DELAY_CYCLES, then detects whether the crystal is functioning. It terminates with success once the oscillator remains unfaulted after the delay, and otherwise repeats the clear/delay/check process as specified by loop_limit
.
enablep | Pass a nonzero value to configure XIN/XOUT for crystal functionality and to loop until the crystal is stabilized or has failed to stabilize. Pass a zero value to turn off the crystal function: this reconfigures pins to their digital port function, disables the crystal, and clears any crystal capacitance setting. |
loop_limit | The number of times the stabilization check should be repeated. If stabilization has not been achieved after this many loops, assume the crystal is absent and configure for VLOCLK. A negative value indicates the process should loop until stabilization is detected. A zero value is equivalent to passing a zero value as enablep . |
int iBSP430clockConfigureSMCLK_ni | ( | eBSP430clockSource | sel, |
unsigned int | dividing_shift | ||
) |
Configure SMCLK to a source clock.
The peripheral-specific implementation will configure SMCLK to source from the requested clock.
sel | the source from which the clock should be selected. Note that the values permit an internal decision, e.g. to prefer LFXT1 but to use an alternative if that is faulted. Configuration is rejected if the requested clock source does not exist on the platform. |
dividing_shift | exponent of a divider used to reduce jitter and/or clock rate. Not all peripherals support this; a value of zero is implicitly used in that situation. |
int iBSP430clockConfigureXT2_ni | ( | int | enablep, |
int | loop_limit | ||
) |
Same as iBSP430clockConfigureLFXT1_ni() but for XT2.
The function is not implemented if XT2CLK is not supported on the platform.
|
static |
True iff the two clock sources are synchronous.
|
static |
Interruptible-preserving wrapper for uiBSP430clockACLK_Hz_ni()
|
static |
Return the best available estimate of slow ACLK frequency.
Same as ulBSP430clockACLK_Hz_ni() but for use where ACLK is a low-frequency clock.
|
static |
Interruptible-preserving wrapper for ulBSP430clockACLK_Hz_ni()
unsigned long ulBSP430clockACLK_Hz_ni | ( | void | ) |
Return the best available estimate of ACLK frequency.
Depending on clock configuration, this will return one of BSP430_CLOCK_NOMINAL_XT1CLK_HZ, BSP430_CLOCK_NOMINAL_VLOCLK_HZ, or another constant or possibly measured value.
A value of 0 may be returned if the clock is configured for an unidentified external source.
unsigned long ulBSP430clockConfigureMCLK_ni | ( | unsigned long | mclk_Hz | ) |
Configure MCLK to a desired frequency.
The peripheral-specific implementation will configure MCLK to a frequency as close as possible to the requested frequency. The actual frequency may be higher or lower than the requested one.
Peripheral-specific notes:
The <bsp430/periph/bc2.h> implementation (2xx family) is normally limited to the calibrated DCO values recorded within the MCU, which are a subset of 1 MHz, 8 MHz, 12 MHz, and 16 MHz. The peripheral will initially be configured to the closest available calibrated frequency. Where a crystal is available configBSP430_BC2_TRIM_TO_MCLK may be used to allow this routine to adjust the DCO to match the specific frequency as closely as possible. The invocation of iBSP430bc2TrimToMCLK_ni() that is used to do this is inhibited if ACLK appears to derive from VLOCLK.
The <bsp430/periph/fllplus.h> implementation (4xx family) relies on presence of a stable LFXT1, and configures to the requested frequency.
The <bsp430/periph/ucs.h> implementation (5xx/6xx family) will set the configuration to the power-up defaults unless BSP430_UCS_TRIM_DCOCLKDIV is available. If it is, then any frequency supported by the MCU may be configured.
The <bsp430/periph/cs.h> implementation (FR5xx family) will select the closest calibrated frequency supported by the MCU. There is no facility for adjusting this to an arbitrary frequency.
mclk_Hz
zero is a short-hand for using BSP430_CLOCK_PUC_MCLK_HZ, the result may not be to restore the clock to its power-up configuration. To avoid manipulating the clocks during platform initialization, see BSP430_PLATFORM_BOOT_CONFIGURE_CLOCKS.mclk_Hz | Desired frequency for the master clock, in Hz. If zero, BSP430_CLOCK_PUC_MCLK_HZ is used. |
|
static |
Interruptible-preserving wrapper for ulBSP430clockMCLK_Hz_ni
unsigned long ulBSP430clockMCLK_Hz_ni | ( | void | ) |
Return the best available estimate of MCLK frequency.
Depending on clock capabilities, this may simply return BSP430_CLOCK_NOMINAL_MCLK_HZ, or it may return a value calculated from observations.
|
static |
Interruptible-preserving wrapper for ulBSP430clockSMCLK_Hz_ni
unsigned long ulBSP430clockSMCLK_Hz_ni | ( | void | ) |
Return the best available estimate of SMCLK frequency.
eBSP430clockSource xBSP430clockACLKSource | ( | ) |
Return a reconstructed source for ACLK.
eBSP430clockSource xBSP430clockMCLKSource | ( | ) |
Return a reconstructed source for MCLK.
eBSP430clockSource xBSP430clockSMCLKSource | ( | ) |
Return a reconstructed source for SMCLK.