irq.c 2.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#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

44 45 46 47 48 49
/* #define IRQ_DEBUG */

/**
 * @addtogroup Kernel
 */

D
dogandog 已提交
50
/**@{*/
51

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

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

D
dzzxzz@gmail.com 已提交
69
    RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq coming..., irq nest:%d\n",
70
                                rt_interrupt_nest));
71

D
dzzxzz@gmail.com 已提交
72 73
    level = rt_hw_interrupt_disable();
    rt_interrupt_nest ++;
74
    RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
D
dzzxzz@gmail.com 已提交
75
    rt_hw_interrupt_enable(level);
76
}
B
Bernard Xiong 已提交
77
RTM_EXPORT(rt_interrupt_enter);
78 79 80 81 82 83 84 85

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

D
dzzxzz@gmail.com 已提交
90 91
    RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq leave, irq nest:%d\n",
                                rt_interrupt_nest));
92

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

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

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

RTM_EXPORT(rt_hw_interrupt_disable);
RTM_EXPORT(rt_hw_interrupt_enable);
122

D
dogandog 已提交
123
/**@}*/
124