interrupt.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018/10/01 Bernard The first version
  9. * 2018/12/27 Jesven Change irq enable/disable to cpu0
  10. */
  11. #include <plic.h>
  12. #include "encoding.h"
  13. #include "riscv.h"
  14. #include "interrupt.h"
  15. struct rt_irq_desc irq_desc[MAX_HANDLERS];
  16. static rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
  17. {
  18. rt_kprintf("UN-handled interrupt %d occurred!!!\n", vector);
  19. return RT_NULL;
  20. }
  21. int rt_hw_plic_irq_enable(int irq_number)
  22. {
  23. plic_irq_enable(irq_number);
  24. return 0;
  25. }
  26. int rt_hw_plic_irq_disable(int irq_number)
  27. {
  28. plic_irq_disable(irq_number);
  29. return 0;
  30. }
  31. /**
  32. * This function will un-mask a interrupt.
  33. * @param vector the interrupt number
  34. */
  35. void rt_hw_interrupt_umask(int vector)
  36. {
  37. plic_set_priority(vector, 1);
  38. rt_hw_plic_irq_enable(vector);
  39. }
  40. /**
  41. * This function will install a interrupt service routine to a interrupt.
  42. * @param vector the interrupt number
  43. * @param new_handler the interrupt service routine to be installed
  44. * @param old_handler the old interrupt service routine
  45. */
  46. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  47. void *param, const char *name)
  48. {
  49. rt_isr_handler_t old_handler = RT_NULL;
  50. if (vector < MAX_HANDLERS)
  51. {
  52. old_handler = irq_desc[vector].handler;
  53. if (handler != RT_NULL)
  54. {
  55. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  56. irq_desc[vector].param = param;
  57. #ifdef RT_USING_INTERRUPT_INFO
  58. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  59. irq_desc[vector].counter = 0;
  60. #endif
  61. }
  62. }
  63. return old_handler;
  64. }
  65. void rt_hw_interrupt_init()
  66. {
  67. /* Enable machine external interrupts. */
  68. /* set_csr(sie, SIP_SEIP); */
  69. int idx = 0;
  70. /* init exceptions table */
  71. for (idx = 0; idx < MAX_HANDLERS; idx++)
  72. {
  73. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  74. irq_desc[idx].param = RT_NULL;
  75. #ifdef RT_USING_INTERRUPT_INFO
  76. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  77. irq_desc[idx].counter = 0;
  78. #endif
  79. }
  80. /*init plic*/
  81. plic_init();
  82. }