irq.c 3.3 KB
Newer Older
1 2 3
/*
 * File      : irq.c
 * This file is part of RT-Thread RTOS
D
dzzxzz 已提交
4
 * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
5
 *
B
Bernard Xiong 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20 21 22 23
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-02-24     Bernard      first version
 * 2006-05-03     Bernard      add IRQ_DEBUG
24
 * 2016-08-09     ArdaFu       add interrupt enter and leave hook.
25 26 27 28 29
 */

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

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#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

57 58 59 60 61 62 63 64
/* #define IRQ_DEBUG */

/**
 * @addtogroup Kernel
 */

/*@{*/

65
volatile rt_uint8_t rt_interrupt_nest;
66 67 68 69 70 71 72 73

/**
 * 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 已提交
74
void rt_interrupt_enter(void)
75
{
D
dzzxzz@gmail.com 已提交
76
    rt_base_t level;
B
bernard.xiong 已提交
77

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

D
dzzxzz@gmail.com 已提交
81 82
    level = rt_hw_interrupt_disable();
    rt_interrupt_nest ++;
83
    RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
D
dzzxzz@gmail.com 已提交
84
    rt_hw_interrupt_enable(level);
85
}
B
Bernard Xiong 已提交
86
RTM_EXPORT(rt_interrupt_enter);
87 88 89 90 91 92 93 94

/**
 * 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 已提交
95
void rt_interrupt_leave(void)
96
{
D
dzzxzz@gmail.com 已提交
97
    rt_base_t level;
98

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

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

109 110
/**
 * This function will return the nest of interrupt.
B
Bernard Xiong 已提交
111
 *
D
dzzxzz@gmail.com 已提交
112
 * User application can invoke this function to get whether current
113
 * context is interrupt context.
B
Bernard Xiong 已提交
114
 *
115 116 117 118
 * @return the number of nested interrupts.
 */
rt_uint8_t rt_interrupt_get_nest(void)
{
D
dzzxzz@gmail.com 已提交
119
    return rt_interrupt_nest;
120
}
B
Bernard Xiong 已提交
121 122 123 124
RTM_EXPORT(rt_interrupt_get_nest);

RTM_EXPORT(rt_hw_interrupt_disable);
RTM_EXPORT(rt_hw_interrupt_enable);
125 126

/*@}*/
127