interrupt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. * 2013-07-06 Bernard first version
  9. * 2015-11-06 zchong support iar compiler
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "am33xx.h"
  14. #include "interrupt.h"
  15. #define AINTC_BASE AM33XX_AINTC_REGS
  16. #define MAX_HANDLERS 128
  17. extern volatile rt_atomic_t rt_interrupt_nest;
  18. /* exception and interrupt handler table */
  19. struct rt_irq_desc isr_table[MAX_HANDLERS];
  20. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  21. rt_uint32_t rt_thread_switch_interrupt_flag;
  22. /**
  23. * @addtogroup AM33xx
  24. */
  25. /*@{*/
  26. void rt_dump_aintc(void)
  27. {
  28. int k;
  29. rt_kprintf("active irq %d", INTC_SIR_IRQ(AINTC_BASE));
  30. rt_kprintf("\n--- hw mask ---\n");
  31. for (k = 0; k < 4; k++)
  32. {
  33. rt_kprintf("0x%08x, ", INTC_MIR(AINTC_BASE, k));
  34. }
  35. rt_kprintf("\n--- hw itr ---\n");
  36. for (k = 0; k < 4; k++)
  37. {
  38. rt_kprintf("0x%08x, ", INTC_ITR(AINTC_BASE, k));
  39. }
  40. rt_kprintf("\n");
  41. }
  42. const unsigned int AM335X_VECTOR_BASE = 0x4030FC00;
  43. extern void rt_cpu_vector_set_base(unsigned int addr);
  44. #ifdef __ICCARM__
  45. extern int __vector;
  46. #else
  47. extern int system_vectors;
  48. #endif
  49. static void rt_hw_vector_init(void)
  50. {
  51. unsigned int *dest = (unsigned int *)AM335X_VECTOR_BASE;
  52. #ifdef __ICCARM__
  53. unsigned int *src = (unsigned int *)&__vector;
  54. #else
  55. unsigned int *src = (unsigned int *)&system_vectors;
  56. #endif
  57. rt_memcpy(dest, src, 16 * 4);
  58. rt_cpu_vector_set_base(AM335X_VECTOR_BASE);
  59. }
  60. /**
  61. * This function will initialize hardware interrupt
  62. */
  63. void rt_hw_interrupt_init(void)
  64. {
  65. /* Reset the ARM interrupt controller */
  66. INTC_SYSCONFIG(AINTC_BASE) = INTC_SYSCONFIG_SOFTRESET;
  67. /* Wait for the reset to complete */
  68. while((INTC_SYSSTATUS(AINTC_BASE)
  69. & INTC_SYSSTATUS_RESETDONE) != INTC_SYSSTATUS_RESETDONE);
  70. /* Enable any interrupt generation by setting priority threshold */
  71. INTC_THRESHOLD(AINTC_BASE) = INTC_THRESHOLD_PRIORITYTHRESHOLD;
  72. /* initialize vector table */
  73. rt_hw_vector_init();
  74. /* init exceptions table */
  75. rt_memset(isr_table, 0x00, sizeof(isr_table));
  76. /* init interrupt nest, and context in thread sp */
  77. rt_interrupt_nest = 0;
  78. rt_interrupt_from_thread = 0;
  79. rt_interrupt_to_thread = 0;
  80. rt_thread_switch_interrupt_flag = 0;
  81. }
  82. /**
  83. * This function will mask a interrupt.
  84. * @param vector the interrupt number
  85. */
  86. void rt_hw_interrupt_mask(int vector)
  87. {
  88. INTC_MIR_SET(AINTC_BASE, vector >> 0x05) = 0x1 << (vector & 0x1f);
  89. }
  90. /**
  91. * This function will un-mask a interrupt.
  92. * @param vector the interrupt number
  93. */
  94. void rt_hw_interrupt_umask(int vector)
  95. {
  96. INTC_MIR_CLEAR(AINTC_BASE, vector >> 0x05) = 0x1 << (vector & 0x1f);
  97. }
  98. /**
  99. * This function will control the interrupt attribute.
  100. * @param vector the interrupt number
  101. */
  102. void rt_hw_interrupt_control(int vector, int priority, int route)
  103. {
  104. int fiq;
  105. if (route == 0)
  106. fiq = 0;
  107. else
  108. fiq = 1;
  109. INTC_ILR(AINTC_BASE, vector) = ((priority << 0x02) & 0x1FC) | fiq ;
  110. }
  111. int rt_hw_interrupt_get_active(int fiq_irq)
  112. {
  113. int ir;
  114. if (fiq_irq == INT_FIQ)
  115. {
  116. ir = INTC_SIR_FIQ(AINTC_BASE) & 0x7f;
  117. }
  118. else
  119. {
  120. ir = INTC_SIR_IRQ(AINTC_BASE) & 0x7f;
  121. }
  122. return ir;
  123. }
  124. void rt_hw_interrupt_ack(int fiq_irq)
  125. {
  126. if (fiq_irq == INT_FIQ)
  127. {
  128. /* new FIQ generation */
  129. INTC_CONTROL(AINTC_BASE) |= 0x02;
  130. }
  131. else
  132. {
  133. /* new IRQ generation */
  134. INTC_CONTROL(AINTC_BASE) |= 0x01;
  135. }
  136. }
  137. /**
  138. * This function will install a interrupt service routine to a interrupt.
  139. * @param vector the interrupt number
  140. * @param new_handler the interrupt service routine to be installed
  141. * @param old_handler the old interrupt service routine
  142. */
  143. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  144. void *param, const char *name)
  145. {
  146. rt_isr_handler_t old_handler = RT_NULL;
  147. if(vector < MAX_HANDLERS)
  148. {
  149. old_handler = isr_table[vector].handler;
  150. if (handler != RT_NULL)
  151. {
  152. #ifdef RT_USING_INTERRUPT_INFO
  153. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  154. #endif /* RT_USING_INTERRUPT_INFO */
  155. isr_table[vector].handler = handler;
  156. isr_table[vector].param = param;
  157. }
  158. }
  159. return old_handler;
  160. }
  161. /**
  162. * This function will trigger an interrupt.
  163. * @param vector the interrupt number
  164. */
  165. void rt_hw_interrupt_trigger(int vector)
  166. {
  167. INTC_ISR_SET(AINTC_BASE, vector>>5) = 1 << (vector & 0x1f);
  168. }
  169. void rt_hw_interrupt_clear(int vector)
  170. {
  171. INTC_ISR_CLEAR(AINTC_BASE, vector>>5) = 1 << (vector & 0x1f);
  172. }
  173. void rt_dump_isr_table(void)
  174. {
  175. int idx;
  176. for(idx = 0; idx < MAX_HANDLERS; idx++)
  177. {
  178. #ifdef RT_USING_INTERRUPT_INFO
  179. rt_kprintf("nr:%4d, name: %*.s, handler: 0x%p, param: 0x%08x\r\n",
  180. idx, RT_NAME_MAX, isr_table[idx].name,
  181. isr_table[idx].handler, isr_table[idx].param);
  182. #else
  183. rt_kprintf("nr:%4d, handler: 0x%p, param: 0x%08x\r\n",
  184. idx, isr_table[idx].handler, isr_table[idx].param);
  185. #endif
  186. }
  187. }
  188. /*@}*/