rt-thread-official/src/irq.c

134 lines
3.1 KiB
C
Raw Normal View History

/*
2021-03-08 11:25:38 +08:00
* Copyright (c) 2006-2021, RT-Thread Development Team
2013-06-24 17:06:09 +08:00
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2006-02-24 Bernard first version
* 2006-05-03 Bernard add IRQ_DEBUG
* 2016-08-09 ArdaFu add interrupt enter and leave hook.
2018-11-22 14:40:43 +08:00
* 2018-11-22 Jesven rt_interrupt_get_nest function add disable irq
2021-08-15 23:04:22 +08:00
* 2021-08-15 Supperthomas fix the comment
*/
#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
2021-08-16 22:07:46 +08:00
*
2021-08-15 23:04:22 +08:00
* @brief This function set a hook function when the system enter a interrupt
2021-08-16 22:07:46 +08:00
*
* @note the hook function must be simple and never be blocked or suspend.
2021-08-16 22:07:46 +08:00
*
2021-08-15 23:04:22 +08:00
* @param hook The function point to be called
*/
void rt_interrupt_enter_sethook(void (*hook)(void))
{
rt_interrupt_enter_hook = hook;
}
2021-08-15 23:04:22 +08:00
/**
* @ingroup Hook
2021-08-16 22:07:46 +08:00
*
2021-08-15 23:04:22 +08:00
* @brief This function set a hook function when the system exit a interrupt.
2021-08-16 22:07:46 +08:00
*
* @note the hook function must be simple and never be blocked or suspend.
2021-08-16 22:07:46 +08:00
*
2021-08-15 23:04:22 +08:00
* @param hook The function point to be called
*/
void rt_interrupt_leave_sethook(void (*hook)(void))
{
rt_interrupt_leave_hook = hook;
}
2021-06-10 17:58:31 +08:00
#endif /* RT_USING_HOOK */
/**
* @addtogroup Kernel
*/
/**@{*/
2018-11-22 14:40:43 +08:00
#ifdef RT_USING_SMP
#define rt_interrupt_nest rt_cpu_self()->irq_nest
#else
volatile rt_uint8_t rt_interrupt_nest = 0;
2021-06-10 17:58:31 +08:00
#endif /* RT_USING_SMP */
2021-08-15 23:04:22 +08:00
/**
2021-08-15 23:04:22 +08:00
* @brief This function will be invoked by BSP, when enter interrupt service routine
2021-08-16 22:07:46 +08:00
*
* @note please don't invoke this routine in application
2021-08-16 22:07:46 +08:00
*
* @see rt_interrupt_leave
*/
void rt_interrupt_enter(void)
{
rt_base_t level;
level = rt_hw_interrupt_disable();
rt_interrupt_nest ++;
RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
rt_hw_interrupt_enable(level);
2021-04-12 23:08:40 +08:00
RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq has come..., irq current nest:%d\n",
rt_interrupt_nest));
}
2013-01-08 21:05:02 +08:00
RTM_EXPORT(rt_interrupt_enter);
2021-08-15 23:04:22 +08:00
/**
2021-08-15 23:04:22 +08:00
* @brief This function will be invoked by BSP, when leave interrupt service routine
2021-08-16 22:07:46 +08:00
*
* @note please don't invoke this routine in application
2021-08-16 22:07:46 +08:00
*
* @see rt_interrupt_enter
*/
void rt_interrupt_leave(void)
{
rt_base_t level;
2021-04-12 23:08:40 +08:00
RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq is going to leave, irq current nest:%d\n",
rt_interrupt_nest));
level = rt_hw_interrupt_disable();
RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
2021-07-05 18:33:22 +08:00
rt_interrupt_nest --;
rt_hw_interrupt_enable(level);
}
2013-01-08 21:05:02 +08:00
RTM_EXPORT(rt_interrupt_leave);
2021-08-15 23:04:22 +08:00
/**
2021-08-15 23:04:22 +08:00
* @brief This function will return the nest of interrupt.
2021-08-16 22:07:46 +08:00
*
* User application can invoke this function to get whether current
* context is interrupt context.
2021-08-16 22:07:46 +08:00
*
2021-08-15 23:04:22 +08:00
* @return rt_uint8_t the number of nested interrupts.
*/
RT_WEAK rt_uint8_t rt_interrupt_get_nest(void)
{
2018-11-22 14:40:43 +08:00
rt_uint8_t ret;
rt_base_t level;
level = rt_hw_interrupt_disable();
ret = rt_interrupt_nest;
rt_hw_interrupt_enable(level);
return ret;
}
2013-01-08 21:05:02 +08:00
RTM_EXPORT(rt_interrupt_get_nest);
RTM_EXPORT(rt_hw_interrupt_disable);
RTM_EXPORT(rt_hw_interrupt_enable);
/**@}*/