I use the following to determine if executing an IRQ handler, or if in normal thread mode but with some interrupt priorities locked out, or just running as a normal thread with all interrupt priorities enabled. Uses CMSIS functions to read IPSR and BASEPRI registers.
int thread_mode ( void )
{
/* Local variables */
int mode ;
/* Read IPSR to determine if an exception is active */
if ( __get_IPSR() != 0 )
{
/* An exception is active */
mode = THREAD_MODE_EXCEPTION ;
}
/* Otherwise read BASEPRI register to determine if interrupt lockout
level is raised. */
else if ( __get_BASEPRI() != 0 )
{
/* Some interrupts locked out */
mode = THREAD_MODE_LOCKED ;
}
/* Otherwise must be normal thread mode with interrupts enabled */
else
{
mode = THREAD_MODE_NORMAL ;
}
/* Return the current operating mode */
return ( mode ) ;
} /* end of function thread_mode */