kehuadong

rt-thread模糊到清晰系列: irq.c

 

#include <rthw.h>
#include <rtthread.h>

// 进入离开中断的钩子函数 #ifdef RT_USING_HOOK
static void (*rt_interrupt_enter_hook)(void); static void (*rt_interrupt_leave_hook)(void); /** * @ingroup Hook * This function set a hook function when the system enter a interrupt * * @note the hook function must be simple and never be blocked or suspend. */ void rt_interrupt_enter_sethook(void (*hook)(void)) { rt_interrupt_enter_hook = hook; } /** * @ingroup Hook * This function set a hook function when the system exit a interrupt. * * @note the hook function must be simple and never be blocked or suspend. */ void rt_interrupt_leave_sethook(void (*hook)(void)) { rt_interrupt_leave_hook = hook; } #endif /* #define IRQ_DEBUG */ /** * @addtogroup Kernel */ /**@{*/
// 多核架构中每个核心独立计算进入嵌套的中断层数 #ifdef RT_USING_SMP
#define rt_interrupt_nest rt_cpu_self()->irq_nest #else volatile rt_uint8_t rt_interrupt_nest = 0; #endif
// 中断进入/离开, 以及获取嵌套层数 /** * This function will be invoked by BSP, when enter interrupt service routine * * @note please don't invoke this routine in application * * @see rt_interrupt_leave */ void rt_interrupt_enter(void) { rt_base_t level;
/*

#define RT_DEBUG_LOG(type, message) \
do \
{ \
if (type) \
rt_kprintf message; \
} \
while (0)

*/
    RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq coming..., irq nest:%d\n",
                                rt_interrupt_nest));

    level = rt_hw_interrupt_disable();
    rt_interrupt_nest ++;
    RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
    rt_hw_interrupt_enable(level);
}
RTM_EXPORT(rt_interrupt_enter);

/**
 * This function will be invoked by BSP, when leave interrupt service routine
 *
 * @note please don't invoke this routine in application
 *
 * @see rt_interrupt_enter
 */
void rt_interrupt_leave(void)
{
    rt_base_t level;

    RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq leave, irq nest:%d\n",
                                rt_interrupt_nest));

    level = rt_hw_interrupt_disable();
    rt_interrupt_nest --;
    RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
    rt_hw_interrupt_enable(level);
}
RTM_EXPORT(rt_interrupt_leave);

/**
 * This function will return the nest of interrupt.
 *
 * User application can invoke this function to get whether current
 * context is interrupt context.
 *
 * @return the number of nested interrupts.
 */
RT_WEAK rt_uint8_t rt_interrupt_get_nest(void)
{
    rt_uint8_t ret;
    rt_base_t level;

    level = rt_hw_interrupt_disable();
    ret = rt_interrupt_nest;
    rt_hw_interrupt_enable(level);
    return ret;
}
RTM_EXPORT(rt_interrupt_get_nest);

RTM_EXPORT(rt_hw_interrupt_disable);
RTM_EXPORT(rt_hw_interrupt_enable);

/**@}*/

 

posted on 2020-11-08 15:18  kehuadong  阅读(264)  评论(0编辑  收藏  举报

导航