Files

58 lines
1.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/******************************************************************************
* @file app_defer.c
* @brief ISR→任务上下文的延后执行标志位实现
* @author cyWu <1917507415@qq.com>
* @date 2026.08.31
* @version V1.0.0,首次发布
******************************************************************************/
#include "system/includes.h"
#include "app_defer.h"
/** 位掩码,volatilerequest() 在中断上下文写,take()/is_pending()/cancel()
* 在任务上下文读写,两侧都用 local_irq_disable/enable 包一下读改写,
* 避免"任务侧正在清 A 位时中断侧设 B 位"这种读改写竞态把 B 位冲丢。 */
static volatile uint8_t s_flags;
void app_defer_request(AppDeferBit_t bit)
{
if (bit >= APP_DEFER_MAX) {
return;
}
local_irq_disable();
s_flags |= (uint8_t)(1u << bit);
local_irq_enable();
}
uint8_t app_defer_take(AppDeferBit_t bit)
{
uint8_t was_set;
if (bit >= APP_DEFER_MAX) {
return 0;
}
local_irq_disable();
was_set = (s_flags & (uint8_t)(1u << bit)) ? 1 : 0;
s_flags &= (uint8_t)~(1u << bit);
local_irq_enable();
return was_set;
}
uint8_t app_defer_is_pending(AppDeferBit_t bit)
{
if (bit >= APP_DEFER_MAX) {
return 0;
}
return (s_flags & (uint8_t)(1u << bit)) ? 1 : 0;
}
void app_defer_cancel(AppDeferBit_t bit)
{
if (bit >= APP_DEFER_MAX) {
return;
}
local_irq_disable();
s_flags &= (uint8_t)~(1u << bit);
local_irq_enable();
}