interrupt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * 2018/5/3 Bernard first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include "cp15.h"
  13. #include <board.h>
  14. #define MAX_HANDLERS 64
  15. extern volatile rt_uint8_t rt_interrupt_nest;
  16. /* exception and interrupt handler table */
  17. struct rt_irq_desc isr_table[MAX_HANDLERS];
  18. rt_uint32_t rt_interrupt_from_thread;
  19. rt_uint32_t rt_interrupt_to_thread;
  20. rt_uint32_t rt_thread_switch_interrupt_flag;
  21. extern int system_vectors;
  22. static void default_isr_handler(int vector, void *param)
  23. {
  24. rt_kprintf("unhandled irq: %d\n", vector);
  25. }
  26. /**
  27. * This function will initialize hardware interrupt
  28. */
  29. void rt_hw_interrupt_init(void)
  30. {
  31. uint32_t index;
  32. /* mask all of interrupts */
  33. IRQ_DISABLE_BASIC = 0x000000ff;
  34. IRQ_DISABLE1 = 0xffffffff;
  35. IRQ_DISABLE2 = 0xffffffff;
  36. for (index = 0; index < MAX_HANDLERS; index ++)
  37. {
  38. isr_table[index].handler = default_isr_handler;
  39. isr_table[index].param = NULL;
  40. #ifdef RT_USING_INTERRUPT_INFO
  41. rt_strncpy(isr_table[index].name, "unknown", RT_NAME_MAX);
  42. isr_table[index].counter = 0;
  43. #endif
  44. }
  45. /* init interrupt nest, and context in thread sp */
  46. rt_interrupt_nest = 0;
  47. rt_interrupt_from_thread = 0;
  48. rt_interrupt_to_thread = 0;
  49. rt_thread_switch_interrupt_flag = 0;
  50. }
  51. /**
  52. * This function will mask a interrupt.
  53. * @param vector the interrupt number
  54. */
  55. void rt_hw_interrupt_mask(int vector)
  56. {
  57. if (vector < 8)
  58. {
  59. IRQ_DISABLE_BASIC = (1 << vector);
  60. }
  61. else if (vector < 32)
  62. {
  63. IRQ_DISABLE1 = (1 << vector);
  64. }
  65. else
  66. {
  67. vector = vector % 32;
  68. IRQ_DISABLE2 = (1 << vector);
  69. }
  70. }
  71. /**
  72. * This function will un-mask a interrupt.
  73. * @param vector the interrupt number
  74. */
  75. void rt_hw_interrupt_umask(int vector)
  76. {
  77. if (vector < 8)
  78. {
  79. IRQ_ENABLE_BASIC = (1 << vector);
  80. }
  81. else if (vector < 32)
  82. {
  83. IRQ_ENABLE1 = (1 << vector);
  84. }
  85. else
  86. {
  87. vector = vector % 32;
  88. IRQ_ENABLE2 = (1 << vector);
  89. }
  90. }
  91. /**
  92. * This function will install a interrupt service routine to a interrupt.
  93. * @param vector the interrupt number
  94. * @param new_handler the interrupt service routine to be installed
  95. * @param old_handler the old interrupt service routine
  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 = isr_table[vector].handler;
  104. if (handler != RT_NULL)
  105. {
  106. #ifdef RT_USING_INTERRUPT_INFO
  107. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  108. #endif /* RT_USING_INTERRUPT_INFO */
  109. isr_table[vector].handler = handler;
  110. isr_table[vector].param = param;
  111. }
  112. }
  113. return old_handler;
  114. }