interrupt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * File : interrupt.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017-2021, 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. * 2018-02-08 RT-Thread the first version
  23. * 2020-03-02 Howard Su Use structure to access registers
  24. */
  25. #include <rthw.h>
  26. #include <rtthread.h>
  27. #include "interrupt.h"
  28. extern rt_uint32_t rt_interrupt_nest;
  29. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  30. rt_uint32_t rt_thread_switch_interrupt_flag;
  31. static struct rt_irq_desc isr_table[INTERRUPTS_MAX];
  32. static void rt_hw_interrupt_handler(int vector, void *param)
  33. {
  34. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  35. }
  36. /**
  37. * This function will initialize hardware interrupt
  38. */
  39. void rt_hw_interrupt_init(void)
  40. {
  41. rt_int32_t idx;
  42. rt_memset(isr_table, 0x00, sizeof(isr_table));
  43. for (idx = 0; idx < INTERRUPTS_MAX; idx ++)
  44. {
  45. isr_table[idx].handler = rt_hw_interrupt_handler;
  46. }
  47. /* init interrupt nest, and context in thread sp */
  48. rt_interrupt_nest = 0;
  49. rt_interrupt_from_thread = 0;
  50. rt_interrupt_to_thread = 0;
  51. rt_thread_switch_interrupt_flag = 0;
  52. /* set base_addr reg */
  53. INTC->base_addr_reg = 0x00000000;
  54. /* clear enable */
  55. INTC->en_reg[0] = 0x00000000;
  56. INTC->en_reg[1] = 0x00000000;
  57. /* mask interrupt */
  58. INTC->mask_reg[0] = 0xFFFFFFFF;
  59. INTC->mask_reg[1] = 0xFFFFFFFF;
  60. /* clear pending */
  61. INTC->pend_reg[0] = 0x00000000;
  62. INTC->pend_reg[1] = 0x00000000;
  63. /* set priority */
  64. INTC->resp_reg[0] = 0x00000000;
  65. INTC->resp_reg[1] = 0x00000000;
  66. /* close fiq interrupt */
  67. INTC->ff_reg[0] = 0x00000000;
  68. INTC->ff_reg[1] = 0x00000000;
  69. }
  70. /**
  71. * This function will mask a interrupt.
  72. * @param vector the interrupt number
  73. */
  74. void rt_hw_interrupt_mask(int vector)
  75. {
  76. int index;
  77. if ((vector < 0) || (vector > INTERRUPTS_MAX))
  78. {
  79. return;
  80. }
  81. index = (vector & 0xE0) != 0;
  82. vector = (vector & 0x1F);
  83. INTC->mask_reg[index] |= 1 << vector;
  84. }
  85. /**
  86. * This function will un-mask a interrupt.
  87. * @param vector the interrupt number
  88. */
  89. void rt_hw_interrupt_umask(int vector)
  90. {
  91. int index;
  92. if ((vector < 0) || (vector > INTERRUPTS_MAX))
  93. {
  94. return;
  95. }
  96. index = (vector & 0xE0) != 0;
  97. vector = (vector & 0x1F);
  98. INTC->mask_reg[index] &= ~(1 << vector);
  99. }
  100. /**
  101. * This function will install a interrupt service routine to a interrupt.
  102. * @param vector the interrupt number
  103. * @param handler the interrupt service routine to be installed
  104. * @param param the interrupt service function parameter
  105. * @param name the interrupt name
  106. * @return old handler
  107. */
  108. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  109. void *param, const char *name)
  110. {
  111. rt_isr_handler_t old_handler = RT_NULL;
  112. int index;
  113. if ((vector < 0) || (vector > INTERRUPTS_MAX))
  114. {
  115. return old_handler;
  116. }
  117. old_handler = isr_table[vector].handler;
  118. #ifdef RT_USING_INTERRUPT_INFO
  119. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  120. #endif /* RT_USING_INTERRUPT_INFO */
  121. isr_table[vector].handler = handler;
  122. isr_table[vector].param = param;
  123. index = (vector & 0xE0) != 0;
  124. vector = (vector & 0x1F);
  125. INTC->pend_reg[index] &= ~(0x1 << vector);
  126. INTC->en_reg[index] |= 0x1 << vector;
  127. return old_handler;
  128. }
  129. void rt_interrupt_dispatch(rt_uint32_t fiq_irq)
  130. {
  131. void *param;
  132. int vector;
  133. rt_isr_handler_t isr_func;
  134. int index;
  135. vector = INTC->vector_reg - INTC->base_addr_reg;
  136. vector = vector >> 2;
  137. isr_func = isr_table[vector].handler;
  138. param = isr_table[vector].param;
  139. /* jump to fun */
  140. isr_func(vector, param);
  141. /* clear pend bit */
  142. index = (vector & 0xE0) != 0;
  143. vector = (vector & 0x1F);
  144. INTC->pend_reg[index] &= ~(0x1 << vector);
  145. #ifdef RT_USING_INTERRUPT_INFO
  146. isr_table[vector].counter ++;
  147. #endif
  148. }