Enabling and Disabling Global Interrupts

Top  Previous  Next

There are times whern you may wish to disable interrupts. This is usually needed when you have to ensure that a short sequence of code cannot be interrupted. The emphasis must be on short; it is a mistake to disable interrupts for anything except the briefest possible period.

 

Controlling interrupts globally on the C6000 is surprisingly difficult for several reasons, not the least of which is that you have to prevent the compiler from optimising your code and moving sequences you want to protect out of the interrupt-free zone.

 

The following functions can be used to control the processors global interrupt enable:

 

c6xint_off

#include <c6xint.h>

unsigned int c6xint_off(void);

stand-alone

This function disables interrupts by clearing the GIE bit in the CSR; it returns the original CSR value.  It is implemented as a true function to dissuade the compiler from performing unfortunate code reordering.

c6xint_restore

#include <c6xint.h>

void c6xint_restore(unsigned int old_csr_value);

stand-alone

This function restores a previously saved CSR value returned by c6xint_off.  It is implemented as a true function to dissuade the compiler from performing unfortunate code reordering.

 

The standard way to use these functions is:

 

unsigned int old;

. . .

old = c6xint_off();  // interrupts off

//

// do something with interrupts disabled

//

c6xint_restore(old); // restore old CSR/GIE state

 

Do not be tempted to disable and enable interrupts by simply clearing and setting GIE directly in the CSR. Doing so can lead to unexpected behaviour because of code reordering by the compiler and unhelpful processor behaviour involving PGIE.

 

Note that indiscriminate global disabling of interrupts is likely to have a bad effect on performance.