irq.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * File : irq.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-02-24 Bernard first version
  23. * 2006-05-03 Bernard add IRQ_DEBUG
  24. * 2016-08-09 ArdaFu add interrupt enter and leave hook.
  25. */
  26. #include <rthw.h>
  27. #include <rtthread.h>
  28. #ifdef RT_USING_HOOK
  29. static void (*rt_interrupt_enter_hook)(void);
  30. static void (*rt_interrupt_leave_hook)(void);
  31. /**
  32. * @ingroup Hook
  33. * This function set a hook function when the system enter a interrupt
  34. *
  35. * @note the hook function must be simple and never be blocked or suspend.
  36. */
  37. void rt_interrupt_enter_sethook(void (*hook)(void))
  38. {
  39. rt_interrupt_enter_hook = hook;
  40. }
  41. /**
  42. * @ingroup Hook
  43. * This function set a hook function when the system exit a interrupt.
  44. *
  45. * @note the hook function must be simple and never be blocked or suspend.
  46. */
  47. void rt_interrupt_leave_sethook(void (*hook)(void))
  48. {
  49. rt_interrupt_leave_hook = hook;
  50. }
  51. #endif
  52. /* #define IRQ_DEBUG */
  53. /**
  54. * @addtogroup Kernel
  55. */
  56. /**@{*/
  57. volatile rt_uint8_t rt_interrupt_nest;
  58. /**
  59. * This function will be invoked by BSP, when enter interrupt service routine
  60. *
  61. * @note please don't invoke this routine in application
  62. *
  63. * @see rt_interrupt_leave
  64. */
  65. void rt_interrupt_enter(void)
  66. {
  67. rt_base_t level;
  68. RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq coming..., irq nest:%d\n",
  69. rt_interrupt_nest));
  70. level = rt_hw_interrupt_disable();
  71. rt_interrupt_nest ++;
  72. RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
  73. rt_hw_interrupt_enable(level);
  74. }
  75. RTM_EXPORT(rt_interrupt_enter);
  76. /**
  77. * This function will be invoked by BSP, when leave interrupt service routine
  78. *
  79. * @note please don't invoke this routine in application
  80. *
  81. * @see rt_interrupt_enter
  82. */
  83. void rt_interrupt_leave(void)
  84. {
  85. rt_base_t level;
  86. RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq leave, irq nest:%d\n",
  87. rt_interrupt_nest));
  88. level = rt_hw_interrupt_disable();
  89. rt_interrupt_nest --;
  90. RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
  91. rt_hw_interrupt_enable(level);
  92. }
  93. RTM_EXPORT(rt_interrupt_leave);
  94. /**
  95. * This function will return the nest of interrupt.
  96. *
  97. * User application can invoke this function to get whether current
  98. * context is interrupt context.
  99. *
  100. * @return the number of nested interrupts.
  101. */
  102. rt_uint8_t rt_interrupt_get_nest(void)
  103. {
  104. return rt_interrupt_nest;
  105. }
  106. RTM_EXPORT(rt_interrupt_get_nest);
  107. RTM_EXPORT(rt_hw_interrupt_disable);
  108. RTM_EXPORT(rt_hw_interrupt_enable);
  109. /**@}*/