interrupt.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * File : interrupt.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013-2014, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2018/5/3 Bernard first version
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include "cp15.h"
  17. #include <board.h>
  18. #define MAX_HANDLERS 64
  19. extern volatile rt_uint8_t rt_interrupt_nest;
  20. /* exception and interrupt handler table */
  21. struct rt_irq_desc isr_table[MAX_HANDLERS];
  22. rt_uint32_t rt_interrupt_from_thread;
  23. rt_uint32_t rt_interrupt_to_thread;
  24. rt_uint32_t rt_thread_switch_interrupt_flag;
  25. extern void rt_cpu_vector_set_base(unsigned int addr);
  26. extern int system_vectors;
  27. void rt_hw_vector_init(void)
  28. {
  29. rt_cpu_vector_set_base((unsigned int)&system_vectors);
  30. }
  31. static void default_isr_handler(int vector, void *param)
  32. {
  33. rt_kprintf("unhandled irq: %d\n", vector);
  34. }
  35. /**
  36. * This function will initialize hardware interrupt
  37. */
  38. void rt_hw_interrupt_init(void)
  39. {
  40. uint32_t index;
  41. /* mask all of interrupts */
  42. IRQ_DISABLE_BASIC = 0x000000ff;
  43. IRQ_DISABLE1 = 0xffffffff;
  44. IRQ_DISABLE2 = 0xffffffff;
  45. for (index = 0; index < MAX_HANDLERS; index ++)
  46. {
  47. isr_table[index].handler = default_isr_handler;
  48. isr_table[index].param = NULL;
  49. #ifdef RT_USING_INTERRUPT_INFO
  50. rt_strncpy(isr_table[index].name, "unknown", RT_NAME_MAX);
  51. isr_table[index].counter = 0;
  52. #endif
  53. }
  54. /* init interrupt nest, and context in thread sp */
  55. rt_interrupt_nest = 0;
  56. rt_interrupt_from_thread = 0;
  57. rt_interrupt_to_thread = 0;
  58. rt_thread_switch_interrupt_flag = 0;
  59. }
  60. /**
  61. * This function will mask a interrupt.
  62. * @param vector the interrupt number
  63. */
  64. void rt_hw_interrupt_mask(int vector)
  65. {
  66. if (vector < 8)
  67. {
  68. IRQ_DISABLE_BASIC = (1 << vector);
  69. }
  70. else if (vector < 32)
  71. {
  72. IRQ_DISABLE1 = (1 << vector);
  73. }
  74. else
  75. {
  76. vector = vector % 32;
  77. IRQ_DISABLE2 = (1 << vector);
  78. }
  79. }
  80. /**
  81. * This function will un-mask a interrupt.
  82. * @param vector the interrupt number
  83. */
  84. void rt_hw_interrupt_umask(int vector)
  85. {
  86. if (vector < 8)
  87. {
  88. IRQ_ENABLE_BASIC = (1 << vector);
  89. }
  90. else if (vector < 32)
  91. {
  92. IRQ_ENABLE1 = (1 << vector);
  93. }
  94. else
  95. {
  96. vector = vector % 32;
  97. IRQ_ENABLE2 = (1 << vector);
  98. }
  99. }
  100. /**
  101. * This function will install a interrupt service routine to a interrupt.
  102. * @param vector the interrupt number
  103. * @param new_handler the interrupt service routine to be installed
  104. * @param old_handler the old interrupt service routine
  105. */
  106. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  107. void *param, const char *name)
  108. {
  109. rt_isr_handler_t old_handler = RT_NULL;
  110. if (vector < MAX_HANDLERS)
  111. {
  112. old_handler = isr_table[vector].handler;
  113. if (handler != RT_NULL)
  114. {
  115. #ifdef RT_USING_INTERRUPT_INFO
  116. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  117. #endif /* RT_USING_INTERRUPT_INFO */
  118. isr_table[vector].handler = handler;
  119. isr_table[vector].param = param;
  120. }
  121. }
  122. return old_handler;
  123. }