interrupt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 int system_vectors;
  26. static void default_isr_handler(int vector, void *param)
  27. {
  28. rt_kprintf("unhandled irq: %d\n", vector);
  29. }
  30. /**
  31. * This function will initialize hardware interrupt
  32. */
  33. void rt_hw_interrupt_init(void)
  34. {
  35. uint32_t index;
  36. /* mask all of interrupts */
  37. IRQ_DISABLE_BASIC = 0x000000ff;
  38. IRQ_DISABLE1 = 0xffffffff;
  39. IRQ_DISABLE2 = 0xffffffff;
  40. for (index = 0; index < MAX_HANDLERS; index ++)
  41. {
  42. isr_table[index].handler = default_isr_handler;
  43. isr_table[index].param = NULL;
  44. #ifdef RT_USING_INTERRUPT_INFO
  45. rt_strncpy(isr_table[index].name, "unknown", RT_NAME_MAX);
  46. isr_table[index].counter = 0;
  47. #endif
  48. }
  49. /* init interrupt nest, and context in thread sp */
  50. rt_interrupt_nest = 0;
  51. rt_interrupt_from_thread = 0;
  52. rt_interrupt_to_thread = 0;
  53. rt_thread_switch_interrupt_flag = 0;
  54. }
  55. /**
  56. * This function will mask a interrupt.
  57. * @param vector the interrupt number
  58. */
  59. void rt_hw_interrupt_mask(int vector)
  60. {
  61. if (vector < 8)
  62. {
  63. IRQ_DISABLE_BASIC = (1 << vector);
  64. }
  65. else if (vector < 32)
  66. {
  67. IRQ_DISABLE1 = (1 << vector);
  68. }
  69. else
  70. {
  71. vector = vector % 32;
  72. IRQ_DISABLE2 = (1 << vector);
  73. }
  74. }
  75. /**
  76. * This function will un-mask a interrupt.
  77. * @param vector the interrupt number
  78. */
  79. void rt_hw_interrupt_umask(int vector)
  80. {
  81. if (vector < 8)
  82. {
  83. IRQ_ENABLE_BASIC = (1 << vector);
  84. }
  85. else if (vector < 32)
  86. {
  87. IRQ_ENABLE1 = (1 << vector);
  88. }
  89. else
  90. {
  91. vector = vector % 32;
  92. IRQ_ENABLE2 = (1 << vector);
  93. }
  94. }
  95. /**
  96. * This function will install a interrupt service routine to a interrupt.
  97. * @param vector the interrupt number
  98. * @param new_handler the interrupt service routine to be installed
  99. * @param old_handler the old interrupt service routine
  100. */
  101. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  102. void *param, const char *name)
  103. {
  104. rt_isr_handler_t old_handler = RT_NULL;
  105. if (vector < MAX_HANDLERS)
  106. {
  107. old_handler = isr_table[vector].handler;
  108. if (handler != RT_NULL)
  109. {
  110. #ifdef RT_USING_INTERRUPT_INFO
  111. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  112. #endif /* RT_USING_INTERRUPT_INFO */
  113. isr_table[vector].handler = handler;
  114. isr_table[vector].param = param;
  115. }
  116. }
  117. return old_handler;
  118. }