irq.c 2.9 KB
Newer Older
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-02-24     Bernard      first version
 * 2006-05-03     Bernard      add IRQ_DEBUG
10
 * 2016-08-09     ArdaFu       add interrupt enter and leave hook.
S
shaojinchun 已提交
11
 * 2018-11-22     Jesven       rt_interrupt_get_nest function add disable irq
12 13 14 15 16
 */

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

17 18 19 20 21 22 23
#ifdef RT_USING_HOOK

static void (*rt_interrupt_enter_hook)(void);
static void (*rt_interrupt_leave_hook)(void);

/**
 * @ingroup Hook
mysterywolf's avatar
mysterywolf 已提交
24
 * This function set a hook function when the system enter a interrupt
25 26 27 28 29 30 31 32 33
 *
 * @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
mysterywolf's avatar
mysterywolf 已提交
34
 * This function set a hook function when the system exit a interrupt.
35 36 37 38 39 40 41
 *
 * @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;
}
42
#endif /* RT_USING_HOOK */
43 44 45 46 47

/**
 * @addtogroup Kernel
 */

D
dogandog 已提交
48
/**@{*/
49

S
shaojinchun 已提交
50 51 52
#ifdef RT_USING_SMP
#define rt_interrupt_nest rt_cpu_self()->irq_nest
#else
53
volatile rt_uint8_t rt_interrupt_nest = 0;
54
#endif /* RT_USING_SMP */
55 56 57 58 59 60 61 62

/**
 * 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
 */
D
dzzxzz 已提交
63
void rt_interrupt_enter(void)
64
{
D
dzzxzz@gmail.com 已提交
65
    rt_base_t level;
B
bernard.xiong 已提交
66

D
dzzxzz@gmail.com 已提交
67 68
    level = rt_hw_interrupt_disable();
    rt_interrupt_nest ++;
69
    RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
D
dzzxzz@gmail.com 已提交
70
    rt_hw_interrupt_enable(level);
71

mysterywolf's avatar
mysterywolf 已提交
72
    RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq has come..., irq current nest:%d\n",
73
                                rt_interrupt_nest));
74
}
B
Bernard Xiong 已提交
75
RTM_EXPORT(rt_interrupt_enter);
76 77 78 79 80 81 82 83

/**
 * 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
 */
D
dzzxzz 已提交
84
void rt_interrupt_leave(void)
85
{
D
dzzxzz@gmail.com 已提交
86
    rt_base_t level;
87

mysterywolf's avatar
mysterywolf 已提交
88
    RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq is going to leave, irq current nest:%d\n",
D
dzzxzz@gmail.com 已提交
89
                                rt_interrupt_nest));
90

D
dzzxzz@gmail.com 已提交
91
    level = rt_hw_interrupt_disable();
92
    RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
F
fenghuijie 已提交
93
    rt_interrupt_nest --;
D
dzzxzz@gmail.com 已提交
94
    rt_hw_interrupt_enable(level);
95
}
B
Bernard Xiong 已提交
96
RTM_EXPORT(rt_interrupt_leave);
97

98 99
/**
 * This function will return the nest of interrupt.
B
Bernard Xiong 已提交
100
 *
D
dzzxzz@gmail.com 已提交
101
 * User application can invoke this function to get whether current
102
 * context is interrupt context.
B
Bernard Xiong 已提交
103
 *
104 105
 * @return the number of nested interrupts.
 */
106
RT_WEAK rt_uint8_t rt_interrupt_get_nest(void)
107
{
S
shaojinchun 已提交
108 109 110 111 112 113 114
    rt_uint8_t ret;
    rt_base_t level;

    level = rt_hw_interrupt_disable();
    ret = rt_interrupt_nest;
    rt_hw_interrupt_enable(level);
    return ret;
115
}
B
Bernard Xiong 已提交
116 117 118 119
RTM_EXPORT(rt_interrupt_get_nest);

RTM_EXPORT(rt_hw_interrupt_disable);
RTM_EXPORT(rt_hw_interrupt_enable);
120

D
dogandog 已提交
121
/**@}*/
122