interrupt.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-10-19 JasonHu first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include "rt_interrupt.h"
  13. #include "riscv.h"
  14. #include "plic.h"
  15. extern rt_uint32_t rt_interrupt_nest;
  16. extern rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  17. extern rt_uint32_t rt_thread_switch_interrupt_flag;
  18. struct rt_irq_desc isr_table[INTERRUPTS_MAX];
  19. static void rt_hw_interrupt_handler(int vector, void *param)
  20. {
  21. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  22. }
  23. /**
  24. * This function will initialize hardware interrupt
  25. */
  26. void rt_hw_interrupt_init(void)
  27. {
  28. /* init interrupt controller */
  29. plic_init();
  30. rt_int32_t idx;
  31. rt_memset(isr_table, 0x00, sizeof(isr_table));
  32. for (idx = 0; idx < INTERRUPTS_MAX; idx++)
  33. {
  34. isr_table[idx].handler = rt_hw_interrupt_handler;
  35. }
  36. /* init interrupt nest, and context in thread sp */
  37. rt_interrupt_nest = 0;
  38. rt_interrupt_from_thread = 0;
  39. rt_interrupt_to_thread = 0;
  40. rt_thread_switch_interrupt_flag = 0;
  41. }
  42. /**
  43. * This function will mask a interrupt.
  44. * @param vector the interrupt number
  45. */
  46. void rt_hw_interrupt_mask(int vector)
  47. {
  48. if ((vector < 0) || (vector > IRQ_MAX_NR))
  49. {
  50. return;
  51. }
  52. plic_disable_irq(vector);
  53. }
  54. /**
  55. * This function will un-mask a interrupt.
  56. * @param vector the interrupt number
  57. */
  58. void rt_hw_interrupt_umask(int vector)
  59. {
  60. if ((vector < 0) || (vector > IRQ_MAX_NR))
  61. {
  62. return;
  63. }
  64. plic_enable_irq(vector);
  65. }
  66. /**
  67. * This function will install a interrupt service routine to a interrupt.
  68. * @param vector the interrupt number
  69. * @param handler the interrupt service routine to be installed
  70. * @param param the interrupt service function parameter
  71. * @param name the interrupt name
  72. * @return old handler
  73. */
  74. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  75. void *param, const char *name)
  76. {
  77. rt_isr_handler_t old_handler = RT_NULL;
  78. if ((vector < 0) || (vector > IRQ_MAX_NR))
  79. {
  80. return old_handler;
  81. }
  82. old_handler = isr_table[IRQ_OFFSET + vector].handler;
  83. #ifdef RT_USING_INTERRUPT_INFO
  84. rt_strncpy(isr_table[IRQ_OFFSET + vector].name, name, RT_NAME_MAX);
  85. #endif /* RT_USING_INTERRUPT_INFO */
  86. isr_table[IRQ_OFFSET + vector].handler = handler;
  87. isr_table[IRQ_OFFSET + vector].param = param;
  88. return old_handler;
  89. }