irq.c 3.1 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
Thomas_Fly's avatar
Thomas_Fly 已提交
12
 * 2021-08-15     Supperthomas fix the comment
13 14 15 16 17
 */

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

18 19 20 21 22 23 24
#ifdef RT_USING_HOOK

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

/**
 * @ingroup Hook
Thomas_Fly's avatar
Thomas_Fly 已提交
25
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
26
 * @brief This function set a hook function when the system enter a interrupt
Thomas_Fly's avatar
Thomas_Fly 已提交
27
 *
28
 * @note the hook function must be simple and never be blocked or suspend.
Thomas_Fly's avatar
Thomas_Fly 已提交
29
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
30
 * @param hook The function point to be called
31 32 33 34 35
 */
void rt_interrupt_enter_sethook(void (*hook)(void))
{
    rt_interrupt_enter_hook = hook;
}
Thomas_Fly's avatar
Thomas_Fly 已提交
36

37 38
/**
 * @ingroup Hook
Thomas_Fly's avatar
Thomas_Fly 已提交
39
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
40
 * @brief This function set a hook function when the system exit a interrupt.
Thomas_Fly's avatar
Thomas_Fly 已提交
41
 *
42
 * @note the hook function must be simple and never be blocked or suspend.
Thomas_Fly's avatar
Thomas_Fly 已提交
43
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
44
 * @param hook The function point to be called
45 46 47 48 49
 */
void rt_interrupt_leave_sethook(void (*hook)(void))
{
    rt_interrupt_leave_hook = hook;
}
50
#endif /* RT_USING_HOOK */
51 52 53 54 55

/**
 * @addtogroup Kernel
 */

D
dogandog 已提交
56
/**@{*/
57

S
shaojinchun 已提交
58 59 60
#ifdef RT_USING_SMP
#define rt_interrupt_nest rt_cpu_self()->irq_nest
#else
61
volatile rt_uint8_t rt_interrupt_nest = 0;
62
#endif /* RT_USING_SMP */
63

Thomas_Fly's avatar
Thomas_Fly 已提交
64

65
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
66
 * @brief This function will be invoked by BSP, when enter interrupt service routine
Thomas_Fly's avatar
Thomas_Fly 已提交
67
 *
68
 * @note please don't invoke this routine in application
Thomas_Fly's avatar
Thomas_Fly 已提交
69
 *
70 71
 * @see rt_interrupt_leave
 */
D
dzzxzz 已提交
72
void rt_interrupt_enter(void)
73
{
D
dzzxzz@gmail.com 已提交
74
    rt_base_t level;
B
bernard.xiong 已提交
75

D
dzzxzz@gmail.com 已提交
76 77
    level = rt_hw_interrupt_disable();
    rt_interrupt_nest ++;
78
    RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
D
dzzxzz@gmail.com 已提交
79
    rt_hw_interrupt_enable(level);
80

mysterywolf's avatar
mysterywolf 已提交
81
    RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq has come..., irq current nest:%d\n",
82
                                rt_interrupt_nest));
83
}
B
Bernard Xiong 已提交
84
RTM_EXPORT(rt_interrupt_enter);
85

Thomas_Fly's avatar
Thomas_Fly 已提交
86

87
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
88
 * @brief This function will be invoked by BSP, when leave interrupt service routine
Thomas_Fly's avatar
Thomas_Fly 已提交
89
 *
90
 * @note please don't invoke this routine in application
Thomas_Fly's avatar
Thomas_Fly 已提交
91
 *
92 93
 * @see rt_interrupt_enter
 */
D
dzzxzz 已提交
94
void rt_interrupt_leave(void)
95
{
D
dzzxzz@gmail.com 已提交
96
    rt_base_t level;
97

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

D
dzzxzz@gmail.com 已提交
101
    level = rt_hw_interrupt_disable();
102
    RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
F
fenghuijie 已提交
103
    rt_interrupt_nest --;
D
dzzxzz@gmail.com 已提交
104
    rt_hw_interrupt_enable(level);
105
}
B
Bernard Xiong 已提交
106
RTM_EXPORT(rt_interrupt_leave);
107

Thomas_Fly's avatar
Thomas_Fly 已提交
108

109
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
110
 * @brief This function will return the nest of interrupt.
Thomas_Fly's avatar
Thomas_Fly 已提交
111
 *
D
dzzxzz@gmail.com 已提交
112
 * User application can invoke this function to get whether current
113
 * context is interrupt context.
Thomas_Fly's avatar
Thomas_Fly 已提交
114
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
115
 * @return rt_uint8_t  the number of nested interrupts.
116
 */
117
RT_WEAK rt_uint8_t rt_interrupt_get_nest(void)
118
{
S
shaojinchun 已提交
119 120 121 122 123 124 125
    rt_uint8_t ret;
    rt_base_t level;

    level = rt_hw_interrupt_disable();
    ret = rt_interrupt_nest;
    rt_hw_interrupt_enable(level);
    return ret;
126
}
B
Bernard Xiong 已提交
127 128 129 130
RTM_EXPORT(rt_interrupt_get_nest);

RTM_EXPORT(rt_hw_interrupt_disable);
RTM_EXPORT(rt_hw_interrupt_enable);
131

D
dogandog 已提交
132
/**@}*/
133