irq.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-02-24 Bernard first version
  9. * 2006-05-03 Bernard add IRQ_DEBUG
  10. * 2016-08-09 ArdaFu add interrupt enter and leave hook.
  11. */
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #ifdef RT_USING_HOOK
  15. static void (*rt_interrupt_enter_hook)(void);
  16. static void (*rt_interrupt_leave_hook)(void);
  17. /**
  18. * @ingroup Hook
  19. * This function set a hook function when the system enter a interrupt
  20. *
  21. * @note the hook function must be simple and never be blocked or suspend.
  22. */
  23. void rt_interrupt_enter_sethook(void (*hook)(void))
  24. {
  25. rt_interrupt_enter_hook = hook;
  26. }
  27. /**
  28. * @ingroup Hook
  29. * This function set a hook function when the system exit a interrupt.
  30. *
  31. * @note the hook function must be simple and never be blocked or suspend.
  32. */
  33. void rt_interrupt_leave_sethook(void (*hook)(void))
  34. {
  35. rt_interrupt_leave_hook = hook;
  36. }
  37. #endif
  38. /* #define IRQ_DEBUG */
  39. /**
  40. * @addtogroup Kernel
  41. */
  42. /**@{*/
  43. volatile rt_uint8_t rt_interrupt_nest;
  44. /**
  45. * This function will be invoked by BSP, when enter interrupt service routine
  46. *
  47. * @note please don't invoke this routine in application
  48. *
  49. * @see rt_interrupt_leave
  50. */
  51. void rt_interrupt_enter(void)
  52. {
  53. rt_base_t level;
  54. RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq coming..., irq nest:%d\n",
  55. rt_interrupt_nest));
  56. level = rt_hw_interrupt_disable();
  57. rt_interrupt_nest ++;
  58. RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
  59. rt_hw_interrupt_enable(level);
  60. }
  61. RTM_EXPORT(rt_interrupt_enter);
  62. /**
  63. * This function will be invoked by BSP, when leave interrupt service routine
  64. *
  65. * @note please don't invoke this routine in application
  66. *
  67. * @see rt_interrupt_enter
  68. */
  69. void rt_interrupt_leave(void)
  70. {
  71. rt_base_t level;
  72. RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq leave, irq nest:%d\n",
  73. rt_interrupt_nest));
  74. level = rt_hw_interrupt_disable();
  75. rt_interrupt_nest --;
  76. RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
  77. rt_hw_interrupt_enable(level);
  78. }
  79. RTM_EXPORT(rt_interrupt_leave);
  80. /**
  81. * This function will return the nest of interrupt.
  82. *
  83. * User application can invoke this function to get whether current
  84. * context is interrupt context.
  85. *
  86. * @return the number of nested interrupts.
  87. */
  88. rt_uint8_t rt_interrupt_get_nest(void)
  89. {
  90. return rt_interrupt_nest;
  91. }
  92. RTM_EXPORT(rt_interrupt_get_nest);
  93. RTM_EXPORT(rt_hw_interrupt_disable);
  94. RTM_EXPORT(rt_hw_interrupt_enable);
  95. /**@}*/