Power management
Low power mode is a feature for which the MSP430 is designed for. It is useful because it shuts down certain areas of the CPU in order to save power. Since most embedded systems need to be energy efficient, low power modes of operation are employed.
Some microcontrollers may provide a sleep mode that shuts down the CPU clock. The MCU comes out of sleep mode when an interrupt is detected. The program resumes execution following the SLEEP instruction and returns to sleep mode after executing another SLEEP instruction.
TI MSP430 does things a bit differently. The bit settings which select the requested sleep mode are part of the status register (SR). When an interrupt occurs, the mcu stores away the SR and goes into full operational mode, Active Mode (AM). At the end of the interrupt service routine (ISR) the MCU retrieves the saved SR and returns to the previous low power mode.
I want my sensor to be as energy efficient as possible. So my purpose is to put the MCU in sleep for 30 seconds, wake up for the time required to make measurements(about 2 seconds) and then put it back to sleep. During the 30 seconds of sleep the energy generated by the Peltier cell is stored in the super-capacitor to provided extra energy to feed the dust sensor heating cycle
The first step is to get familiar with clocks, timers and low power modes
Clocks and timers
In the MSP430FR5969 MCU, the two main clock generation mechanisms are internal RC type oscillators and internal oscillators using external crystals.
The MSP430FR5969 can contain several internal oscillators. Of particular note are the Digital Controlled Oscillator (DCO) and the low frequency oscillator (VLO). Both of these are based on an RC network. RC networks can be rather inaccurate in generating precise clocks, so TI has trimmed and calibrated them to reach close to 1% accuracy. These type of oscillators are heavily affected by temperature and cannot be always be used for interfaces requiring accurate timing such as UART without causing errors. The DCO is digitally controlled because its frequency can be selected from a given set of frequencies that are calibrated at the factory.
The MSP430FR5969 can also use external crystals with internal oscillator circuitry to generate both low frequency (32kHz) and high frequency (Up to 16MHz) clocks that are as accurate as the crystal used.
The clocks available are the following:
- DCOCLK - Internal Digitally Controlled Oscillator capable of generating a factory-calibrated set of frequencies.
- VLOCLK - Internal low frequency and low accuracy oscillator for low power
- LFXTCLK - Low frequency mode of the XT1 oscillator, typically used with watch crystals or clocks of 32.768kHz.
- HFXTCLK - High frequency mode of the XT1 oscillator, used with High Frequency crystals. Not available in all MSP430 (not present in G2553 device)
The clock tree in the MSP430FR5969 is actually composed of several signals whose source is selectable. This flexibility helps power CPU and peripherals using different clocks to achieve either speed or low power. The main signals in the MSP430FR5969 include:
- Master clock (MCLK). MCLK is software selectable as a sub-frequency of LFXTCLK, VLOCLK, HFXTCLK or DCOCLK. MCLK. MCLK is used by the CPU and system.
- Sub-main clock (SMCLK). SMCLK is software selectable as a sub-frequency of LFXT1CLK, VLOCLK or DCOCLK. SMCLK. SMCLK is software selectable for individual peripheral modules.
- Auxiliary clock (ACLK). ACLK is software selectable as a sub-frequency of LFXT1CLK or VLOCLK. ACLK is software selectable for individual peripheral modules.
The CPU of the MSP430FR5969 runs only from MCLK, while other peripheral modules can be sourced by any of the clock signals or in some cases from other clock sources brought in on specific pins.
Power modes
The MSP430 features five modes of operation, yet the most popular 3 are the following (the others can be found in the datasheet):
1. Active mode (AM), I ≈ 300 uA
a. All clocks are active
2. Low-power mode 0 (LPM0), I ≈ 85uA
a. CPU is disabled
b. ACLK and SMCLK remain active, MCLK is disabled
3. Low-power mode 3 (LPM3), I ≈ 1uA
a. CPU is disabled
b. MCLK and SMCLK disabled
c. ACLK remains active
d. DCO’s dc-generator is disabled
Let's code
After this overview, let's write a simple test application.
Texas Instruments provides a great graphical tool integrated into its Code Composer Studio IDE for configuring MCU peripherals: Grace.
Using Grace is very easy: just click on the sub system you want to configure..
.. and this will lead you to a form where peripheral settings can be easily changed
In the same way we can configure Timer 0 to generate an interrupt every second
and GPIOs
The main function is very easy
int main(void)
{
Grace_init(); // Activate Grace-generated configuration
/* Entering low power mode 3 */
__enable_interrupt();
__bis_SR_register(LPM3_bits);
}
I simply invoke the initialization code generated by Grace, then I enable the interrupt and finally I enter the power mode 3. Because in power mode 3 the CPU is not running (only peripherals are active) a infinte loop is not required. The only code that is going to be executed is the code in the Timer 0 interrupt routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR_HOOK(void)
{
/* USER CODE START (section: TIMER0_A0_ISR_HOOK) */
P1OUT ^= BIT5;
/* USER CODE END (section: TIMER0_A0_ISR_HOOK) */
}