C如何定义一个可以被预处理器检查的上下文

C how to define a context that can be checked by the preprocessor

在嵌入式系统上,我有一个函数,不能从中断上下文中调用。我想确保它在编译时得到检查。我想,如果预处理器可以检查这个就好了。我想到了这样的事情:

/* function that shall not be called from interrupt-context */
void function_not_to_call_from_isr(void)
{
    #ifdef INTERRUPT_CONTEXT
        #error This shall not be called from interrupt context!
    #endif
    // ... stuff ...
}

void someISRfunction(void)
{
#define INTERRUPT_CONTEXT
    // here, the check can be performed
    function_not_to_call_from_isr();
#undef INTERRUPT_CONTEXT
}

我的问题是,是否可以做一些魔术,这样我就不必手动 define/undefine 和 INTERRUPT_CONTEXT? 喜欢:

#define INTERRUPT_SERVICE_ROUTINE(funcName) void funcName(void) // magic needed here...

有什么想法吗?

如果你想阻止从上下文调用函数,不,这在预处理器中使用你想要的方式是不可能的,特别是在编译时。

对于特定的体系结构,您可以检查您是否处于中断上下文中,然后决定要做什么。底部示例适用于带有 CMSIS 代码的 ARM Cortex-M。

void func_prohibited_in_isr(void) {
    if (__get_IPSR()) {
        //Called from IRQ, return
        return;
    }
    //Other code, executed when function is not called from IRQ
}

//IRQ function for peripheral
void IRQ_Handler(void) {
    func_prohibited_in_isr(); //Will do nothing when called
}

因此,如果您处于 IRQ 上下文中,则必须找出如何检查特定体系结构。

你不能,因为所有 #define 都在实际编译之前展开。你需要一个变量

#define INTERRUPT_CONTEXT_ENTRY do {frominterrupt++;} while(0)
#define INTERRUPT_CONTEXT_EXIT  do {frominterrupt--;} while(0)
#define FROM_INTERRUPT  (frominterrupt > 0)
#define ISR_PANIC (frominterrupt < 0)

volatile int frominterrupt = 0;


void interrupt_handler()
{
    INTERRUPT_CONTEXT_ENTRY;
    /* do something*/
    INTERRUPT_CONTEXT_EXIT;
}

void function_not_to_call_from_isr(void)
{
    if(ISR_PANIC)
    {
         /* start the suicide procedure */
         /* your program is dead anyway */
    }

    if(FROM_INTERRUPT)
    {
        /* do something - illegal call */
    }
    else
    {
        /* do normall stuff */
    }
}