在PIC32上的HiTech C中设置函数指针的中断安全方式 [英] Interrupt-safe way to set function pointer in HiTech C on PIC32

查看:125
本文介绍了在PIC32上的HiTech C中设置函数指针的中断安全方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个ISR以在外部中断上触发。外部中断不一定总是启用,但是在某些情况下,我希望能够从主代码中在中断内注册一个称为ONCE的函数。在下一次中断之前,该函数可能会被另一个函数替换或删除。

I have an ISR defined to trigger on an external interrupt. The external interrupt may not always be enabled, but under certain circumstances I want to be able to register a function to be called ONCE within the interrupt from within the main code. The function might be replaced by another one, or removed, before the next interrupt.

我对PIC32上的同步技术了解不多,但是我已经提出以下内容:

I don't know much about techniques for synchronisation on the PIC32, but I've come up with the following:

volatile BOOL callbackInterrupted = FALSE;
volatile BOOL callbackWritten = FALSE;
void (*myCallback)() = NULL;

void RegisterCallback(void (*callback)())
{
    do
    {
        callbackWritten = FALSE;
        myCallback = callback;
    }
    while(callbackInterrupted);

    callbackWritten = (callback != NULL);
}

void interrupt MyExternalInterrupt() @EXTERNAL_1_VCTR
{
    // Do a bunch of other things here...

    if(callbackWritten)
    {
        myCallback();
        myCallback = NULL;
        callbackInterrupted = TRUE;
        callbackWritten = FALSE;
    }
}

虽然我很难对此进行推理。这是否确实达到了我的期望,即。防止ISR调用半设置函数指针或两次调用函数? do ... while 循环是否多余?有没有更好的方法?

I'm having trouble reasoning about it though. Does this actually do what I hope, ie. prevent the ISR calling a half-set function pointer, or calling a function twice? Is the do ... while loop superfluous? Is there a better way?

已添加:禁用此中断是不可能的。

Added: disabling this interrupt is out of the question. It is used for timing.

flag = TRUE 生成的指令:

lui         s1,0x0
ori         s1,s1,0x1
addiu       at,s1,0
or          t0,at,zero

fnc1 =& testfunc 生成的指令:

lui         a2,0x9d00
ori         a2,a2,0x50
or          a1,a2,zero
sw          a1,16376(gp)


推荐答案

假定设置布尔是原子操作(请确保反汇编并阅读手册)-我将使用一个标志来设置函数指针,但是该标志只能由ISR读取,并且只能由常规代码编写,从而为您提供一个简单的信号量。如果编写函数指针是原子的(同样,请通过反汇编进行检查),则可以使用它代替标志。

Assuming setting the bool is an atomic operation (disassemble & read the manual to be sure) - I would use a flag to set the function pointer, but this flag shold only be read by the ISR, and only written by the normal code, giving you a simple semaphore. If writing the function pointer is atomic (again, check by disassembling), you can use it instead of the flag.

就像这样(不在我的脑海)

Like this (off the top of my head)

void (*myCallback)() = NULL;        

void RegisterCallback(void (*callback)())        
{        
   myCallback = callback;
}        

void interrupt MyExternalInterrupt() @EXTERNAL_1_VCTR        
{        
   // Do a bunch of other things here...        

   if (myCallback!=NULL)
      myCallback();        
   myCallback = NULL;        
}  

编辑
,将函数指针用作标志将起作用。修改代码以显示用法。

Edit After seeing disassembled instructions, using the function pointer as a flag will work. Modified code to show usage.

这篇关于在PIC32上的HiTech C中设置函数指针的中断安全方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆