Handling the status register

Several routines are available to assist in handling the status register. These should be used with care. Altering the status register bits in an uncontrolled way could badly affect the operation of your program.

#include <signal.h>

void WRITE_SR(const uint16_tx);

Set the value of the status register (r2).

#include <signal.h>

uint16_t READ_SR(void);

Read the value of the status register (r2).

#include <signal.h>

void BIS_SR(const uint16_t x);

Set bits in the status register (r2), using the "bis" instruction.

#include <signal.h>

void BIC_SR(const uint16_t x);

Clear bits in the status register (r2), using the "bic" instruction.

#include <signal.h>

SFR_CMD(cmd, (typeof SFR) sfr, (typeof SFR) val);

Perform an operation on an SFR, which is neither optimized nor modified by the compiler. For example:
SFR_CMD(bis.b, IE1,WDTIE); /* Enable the watchdog interrupt. */
does the same thing as
IE1 |= WDTIE;
The main reason for the user to directly access the status register in a C program is to switch between the MSP430's low power modes. Although non-interrupt code typically put the CPU into a low power mode, it is usually inside an interrupt service routine that the decision to switch back to normal operation occurs. If the interrupt service routine simply changed the status register bits, these would simply change back at as the routine exists, and the CPU would return to a low power state. To avoid this, the status register stored on the stack must be altered, so the change of processor mode occurs as the interrupt service routine exits, and the stack is popped. Two routines are defined to assist in this task.

#include <signal.h>

void _BIS_SR_IRQ(int16_t x);

Set bits in the copy of the status register stored on the statck.

#include <signal.h>

void _BIC_SR_IRQ(int16_t x);

Clear bits in the copy of the status register stored on the statck.

These functions should only be used within interrupt service routines. At present GCC issues a warning when these functions are used, but the correct code is produced. To make these functions (and the BIS_SR and BIC_SR) easier to use for switching LPM modes, the following values are defined when "io.h" is included in your source code

You can also use these names without the suffix "_bits".