首页 > 解决方案 > 在它们之间不中断地复制和重置内存

问题描述

我试图在开始读取它并设置输出之前复制我的输入触发掩码,但我怀疑输入中断有时会发生在下面这两行之间,所以我错过了最后一次更改,因为当快速更改输入时,有时输出保持其先前的状态。

trigger_mask_t tmask = gpio_trigger_mask;
gpio_trigger_mask.bits = 0;

if (!tmask.bits)
    // reading the inputs and setting the outputs here.

我的中断功能:

void gpioCbFxn(PIN_Handle handle, PIN_Id pinId)
{
    for (uint8_t i = 0; i < sizeof(GPIO_tab); i++)
    {
        if (pinId == GPIO_tab[i])
        {
            gpio_trigger_mask.bits |= (1 << i);
            return;
        }
    }
}

我的触发掩码结构:

typedef struct {
    uint16_t IN1 : 1,
             IN2 : 1,
             IN3 : 1,
             ...;
} trigger_mask_fields_t;

typedef union {
    trigger_mask_fields_t fields;
    uint16_t bits;
} trigger_mask_t;

目前我没有该芯片的调试器。

这种理论可能吗?如果是这样,我该如何解决?

标签: cembeddedinterrupttexas-instruments

解决方案


您需要在从掩码变量读取之前禁用中断,清除掩码后,您将再次启用中断。从中断的角度来看,您使这个小块具有“原子性”。

This way an interrupt, which would be served during both statements, will be pending until the enable, and it will be served then. No interrupts are lost.

This is a very similar situation as if the interrupt occurs during the first or during the second statement. Often such a statement is compiled into multiple assembly instructions, and the interrupt can be served "in the middle" of the statement.

Such a delayed interrupt is very similar to an interrupt that is served right after the second statement.


推荐阅读