c - 在它们之间不中断地复制和重置内存
问题描述
我试图在开始读取它并设置输出之前复制我的输入触发掩码,但我怀疑输入中断有时会发生在下面这两行之间,所以我错过了最后一次更改,因为当快速更改输入时,有时输出保持其先前的状态。
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;
目前我没有该芯片的调试器。
这种理论可能吗?如果是这样,我该如何解决?
解决方案
您需要在从掩码变量读取之前禁用中断,清除掩码后,您将再次启用中断。从中断的角度来看,您使这个小块具有“原子性”。
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.
推荐阅读
- sql - 如何仅将数据从一个数据库表复制到sql server中的另一个数据库现有表
- mysql - “SELECT * FROM System”的 MySQL 语法错误 1064
- templates - 在 terraform 中进行配置之前操作文件
- npm - 获取最近安装的包列表 - Yarn / NPM
- https - CouchDB 无法设置 HTTPS
- r - r - 将数据帧滞后 NA 的数量
- google-cloud-platform - 无法增加每个项目配额的数据流作业
- c# - 如何在 ContextMenu 中获取 PlacementTarget.Content?
- powershell - 将生成的价值传递给下游作业
- xaml - 更改 UWP RatingControl 的大小和间距