interrupt.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <rthw.h>
  2. #include "plic_driver.h"
  3. #include "platform.h"
  4. #define MAX_HANDLERS PLIC_NUM_INTERRUPTS
  5. extern rt_uint32_t rt_interrupt_nest;
  6. /* exception and interrupt handler table */
  7. struct rt_irq_desc irq_desc[MAX_HANDLERS];
  8. rt_uint32_t rt_interrupt_from_thread;
  9. rt_uint32_t rt_interrupt_to_thread;
  10. rt_uint32_t rt_thread_switch_interrupt_flag;
  11. volatile plic_instance_t g_plic;
  12. /**
  13. * This function will mask a interrupt.
  14. * @param vector the interrupt number
  15. */
  16. void rt_hw_interrupt_mask(int irq)
  17. {
  18. PLIC_disable_interrupt(&g_plic, irq);
  19. return;
  20. }
  21. /**
  22. * This function will un-mask a interrupt.
  23. * @param vector the interrupt number
  24. */
  25. void rt_hw_interrupt_unmask(int irq)
  26. {
  27. PLIC_enable_interrupt(&g_plic, irq);
  28. PLIC_set_priority(&g_plic, irq, 1);
  29. return;
  30. }
  31. rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
  32. {
  33. rt_kprintf("UN-handled interrupt %d occurred!!!\n", vector);
  34. return RT_NULL;
  35. }
  36. void rt_hw_interrupt_init(void)
  37. {
  38. int idx;
  39. /* config interrupt vector*/
  40. asm volatile(
  41. "la t0, trap_entry\n"
  42. "csrw mtvec, t0"
  43. );
  44. /* enable global interrupt*/
  45. PLIC_init(&g_plic,
  46. PLIC_CTRL_ADDR,
  47. PLIC_NUM_INTERRUPTS,
  48. PLIC_NUM_PRIORITIES);
  49. /* init exceptions table */
  50. for(idx=0; idx < MAX_HANDLERS; idx++)
  51. {
  52. rt_hw_interrupt_mask(idx);
  53. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  54. irq_desc[idx].param = RT_NULL;
  55. #ifdef RT_USING_INTERRUPT_INFO
  56. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  57. irq_desc[idx].counter = 0;
  58. #endif
  59. }
  60. /* init interrupt nest, and context in thread sp */
  61. rt_interrupt_nest = 0;
  62. rt_interrupt_from_thread = 0;
  63. rt_interrupt_to_thread = 0;
  64. rt_thread_switch_interrupt_flag = 0;
  65. }
  66. rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq)
  67. {
  68. //volatile rt_uint32_t irqstat;
  69. rt_uint32_t id = PLIC_claim_interrupt(&g_plic);
  70. return id;
  71. }
  72. void rt_hw_interrupt_ack(rt_uint32_t fiq_irq, rt_uint32_t id)
  73. {
  74. PLIC_complete_interrupt(&g_plic, id);
  75. return;
  76. }
  77. /**
  78. * This function will install a interrupt service routine to a interrupt.
  79. * @param vector the interrupt number
  80. * @param handler the interrupt service routine to be installed
  81. * @param param the interrupt service function parameter
  82. * @param name the interrupt name
  83. * @return old handler
  84. */
  85. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  86. void *param, char *name)
  87. {
  88. rt_isr_handler_t old_handler = RT_NULL;
  89. if(vector < MAX_HANDLERS)
  90. {
  91. old_handler = irq_desc[vector].handler;
  92. if (handler != RT_NULL)
  93. {
  94. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  95. irq_desc[vector].param = param;
  96. #ifdef RT_USING_INTERRUPT_INFO
  97. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  98. irq_desc[vector].counter = 0;
  99. #endif
  100. }
  101. }
  102. return old_handler;
  103. }