2009-07-03 06:48:23 +08:00
|
|
|
/*
|
2021-03-08 11:25:38 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-06-24 17:06:09 +08:00
|
|
|
*
|
2018-09-14 22:37:43 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2006-02-24 Bernard first version
|
|
|
|
* 2006-05-03 Bernard add IRQ_DEBUG
|
2016-08-09 11:29:11 +08:00
|
|
|
* 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
|
2022-01-08 07:35:44 +08:00
|
|
|
* 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to irq.c
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
2022-01-08 07:35:44 +08:00
|
|
|
#ifndef __on_rt_interrupt_enter_hook
|
|
|
|
#define __on_rt_interrupt_enter_hook() __ON_HOOK_ARGS(rt_interrupt_enter_hook, ())
|
|
|
|
#endif
|
|
|
|
#ifndef __on_rt_interrupt_leave_hook
|
|
|
|
#define __on_rt_interrupt_leave_hook() __ON_HOOK_ARGS(rt_interrupt_leave_hook, ())
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
|
2016-08-09 11:29:11 +08:00
|
|
|
|
|
|
|
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
|
|
|
*
|
2021-08-28 23:31:35 +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-30 07:28:36 +08:00
|
|
|
* @param hook the function point to be called
|
2016-08-09 11:29:11 +08:00
|
|
|
*/
|
|
|
|
void rt_interrupt_enter_sethook(void (*hook)(void))
|
|
|
|
{
|
|
|
|
rt_interrupt_enter_hook = hook;
|
|
|
|
}
|
2021-08-15 23:04:22 +08:00
|
|
|
|
2016-08-09 11:29:11 +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
|
|
|
*
|
2021-08-28 23:31:35 +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-30 07:28:36 +08:00
|
|
|
* @param hook the function point to be called
|
2016-08-09 11:29:11 +08:00
|
|
|
*/
|
|
|
|
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 */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Kernel
|
|
|
|
*/
|
|
|
|
|
2016-08-19 10:08:35 +08:00
|
|
|
/**@{*/
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-11-22 14:40:43 +08:00
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
#define rt_interrupt_nest rt_cpu_self()->irq_nest
|
|
|
|
#else
|
2018-12-08 10:41:38 +08:00
|
|
|
volatile rt_uint8_t rt_interrupt_nest = 0;
|
2021-06-10 17:58:31 +08:00
|
|
|
#endif /* RT_USING_SMP */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2021-08-15 23:04:22 +08:00
|
|
|
|
2009-07-03 06:48:23 +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
|
|
|
*
|
2021-08-28 23:31:35 +08:00
|
|
|
* @note Please don't invoke this routine in application
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2009-07-03 06:48:23 +08:00
|
|
|
* @see rt_interrupt_leave
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_interrupt_enter(void)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-21 11:33:25 +08:00
|
|
|
rt_base_t level;
|
2009-12-25 20:18:53 +08:00
|
|
|
|
2012-12-21 11:33:25 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
rt_interrupt_nest ++;
|
2016-08-09 11:29:11 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
|
2012-12-21 11:33:25 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2021-04-12 23:01:35 +08:00
|
|
|
|
2021-04-12 23:08:40 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq has come..., irq current nest:%d\n",
|
2021-04-12 23:01:35 +08:00
|
|
|
rt_interrupt_nest));
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
RTM_EXPORT(rt_interrupt_enter);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2021-08-15 23:04:22 +08:00
|
|
|
|
2009-07-03 06:48:23 +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
|
|
|
*
|
2021-08-28 23:31:35 +08:00
|
|
|
* @note Please don't invoke this routine in application
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2009-07-03 06:48:23 +08:00
|
|
|
* @see rt_interrupt_enter
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_interrupt_leave(void)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-21 11:33:25 +08:00
|
|
|
rt_base_t level;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2021-04-12 23:08:40 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq is going to leave, irq current nest:%d\n",
|
2012-12-21 11:33:25 +08:00
|
|
|
rt_interrupt_nest));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-21 11:33:25 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2016-08-09 11:29:11 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
|
2021-07-05 18:33:22 +08:00
|
|
|
rt_interrupt_nest --;
|
2012-12-21 11:33:25 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2011-06-15 07:59:42 +08:00
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
RTM_EXPORT(rt_interrupt_leave);
|
2011-06-12 18:01:48 +08:00
|
|
|
|
2021-08-15 23:04:22 +08:00
|
|
|
|
2011-06-15 07:59:42 +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
|
|
|
*
|
2012-06-18 20:21:03 +08:00
|
|
|
* User application can invoke this function to get whether current
|
2011-06-15 07:59:42 +08:00
|
|
|
* context is interrupt context.
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @return the number of nested interrupts.
|
2011-06-15 07:59:42 +08:00
|
|
|
*/
|
2019-07-12 11:15:20 +08:00
|
|
|
RT_WEAK rt_uint8_t rt_interrupt_get_nest(void)
|
2011-06-15 07:59:42 +08:00
|
|
|
{
|
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;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
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);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2016-08-19 10:08:35 +08:00
|
|
|
/**@}*/
|
2011-06-15 07:59:42 +08:00
|
|
|
|