interrupt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * File : interrupt.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2018, 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. */
  23. #include <rthw.h>
  24. #include <plic_driver.h>
  25. #include <platform.h>
  26. #include <encoding.h>
  27. #include <interrupt.h>
  28. #define MAX_HANDLERS PLIC_NUM_INTERRUPTS
  29. /* exception and interrupt handler table */
  30. static struct rt_irq_desc irq_desc[MAX_HANDLERS];
  31. static plic_instance_t g_plic;
  32. /**
  33. * This function will mask a interrupt.
  34. * @param vector the interrupt number
  35. */
  36. void rt_hw_interrupt_mask(int irq)
  37. {
  38. PLIC_disable_interrupt(&g_plic, irq);
  39. }
  40. /**
  41. * This function will un-mask a interrupt.
  42. * @param vector the interrupt number
  43. */
  44. void rt_hw_interrupt_unmask(int irq)
  45. {
  46. PLIC_enable_interrupt(&g_plic, irq);
  47. PLIC_set_priority(&g_plic, irq, 1);
  48. }
  49. rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
  50. {
  51. rt_kprintf("UN-handled interrupt %d occurred!!!\n", vector);
  52. return RT_NULL;
  53. }
  54. void rt_hw_interrupt_init(void)
  55. {
  56. int idx;
  57. /* config interrupt vector*/
  58. asm volatile(
  59. "la t0, SW_handler\n"
  60. "csrw mtvec, t0"
  61. );
  62. /* enable global interrupt*/
  63. PLIC_init(&g_plic,
  64. PLIC_CTRL_ADDR,
  65. PLIC_NUM_INTERRUPTS,
  66. PLIC_NUM_PRIORITIES);
  67. /* init exceptions table */
  68. for (idx = 0; idx < MAX_HANDLERS; idx++)
  69. {
  70. rt_hw_interrupt_mask(idx);
  71. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  72. irq_desc[idx].param = RT_NULL;
  73. #ifdef RT_USING_INTERRUPT_INFO
  74. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  75. irq_desc[idx].counter = 0;
  76. #endif
  77. }
  78. // enable machine external interrupt
  79. set_csr(mie, MIP_MEIP);
  80. }
  81. rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq)
  82. {
  83. return (rt_uint32_t)PLIC_claim_interrupt(&g_plic);;
  84. }
  85. void rt_hw_interrupt_ack(rt_uint32_t fiq_irq, rt_uint32_t id)
  86. {
  87. PLIC_complete_interrupt(&g_plic, id);
  88. }
  89. /**
  90. * This function will install a interrupt service routine to a interrupt.
  91. * @param vector the interrupt number
  92. * @param handler the interrupt service routine to be installed
  93. * @param param the interrupt service function parameter
  94. * @param name the interrupt name
  95. * @return old handler
  96. */
  97. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  98. void *param, const char *name)
  99. {
  100. rt_isr_handler_t old_handler = RT_NULL;
  101. if(vector < MAX_HANDLERS)
  102. {
  103. old_handler = irq_desc[vector].handler;
  104. if (handler != RT_NULL)
  105. {
  106. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  107. irq_desc[vector].param = param;
  108. #ifdef RT_USING_INTERRUPT_INFO
  109. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  110. irq_desc[vector].counter = 0;
  111. #endif
  112. }
  113. }
  114. return old_handler;
  115. }
  116. /**
  117. * This function will be call when external machine-level
  118. * interrupt from PLIC occurred.
  119. */
  120. void handle_m_ext_interrupt(void)
  121. {
  122. rt_isr_handler_t isr_func;
  123. rt_uint32_t irq;
  124. void *param;
  125. /* get irq number */
  126. irq = rt_hw_interrupt_get_active(0);
  127. /* get interrupt service routine */
  128. isr_func = irq_desc[irq].handler;
  129. param = irq_desc[irq].param;
  130. /* turn to interrupt service routine */
  131. isr_func(irq, param);
  132. rt_hw_interrupt_ack(0, irq);
  133. #ifdef RT_USING_INTERRUPT_INFO
  134. irq_desc[irq].counter ++;
  135. #endif
  136. }