trap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * File : trap.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-01-13 weety modified from mini2440
  23. * 2015-04-15 ArdaFu Split from AT91SAM9260 BSP
  24. */
  25. #include <rtthread.h>
  26. #include <rthw.h>
  27. #include <interrupt.h>
  28. extern struct rt_thread *rt_current_thread;
  29. #ifdef RT_USING_FINSH
  30. extern long list_thread(void);
  31. #endif
  32. struct rt_hw_register
  33. {
  34. rt_uint32_t r0;
  35. rt_uint32_t r1;
  36. rt_uint32_t r2;
  37. rt_uint32_t r3;
  38. rt_uint32_t r4;
  39. rt_uint32_t r5;
  40. rt_uint32_t r6;
  41. rt_uint32_t r7;
  42. rt_uint32_t r8;
  43. rt_uint32_t r9;
  44. rt_uint32_t r10;
  45. rt_uint32_t fp;
  46. rt_uint32_t ip;
  47. rt_uint32_t sp;
  48. rt_uint32_t lr;
  49. rt_uint32_t pc;
  50. rt_uint32_t cpsr;
  51. rt_uint32_t ORIG_r0;
  52. };
  53. /**
  54. * this function will show registers of CPU
  55. *
  56. * @param regs the registers point
  57. */
  58. void rt_hw_show_register (struct rt_hw_register *regs)
  59. {
  60. rt_kprintf("Execption:\n");
  61. rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n",
  62. regs->r0, regs->r1, regs->r2, regs->r3);
  63. rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n",
  64. regs->r4, regs->r5, regs->r6, regs->r7);
  65. rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n",
  66. regs->r8, regs->r9, regs->r10);
  67. rt_kprintf("fp :0x%08x ip :0x%08x\n",
  68. regs->fp, regs->ip);
  69. rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n",
  70. regs->sp, regs->lr, regs->pc);
  71. rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
  72. }
  73. /**
  74. * When ARM7TDMI comes across an instruction which it cannot handle,
  75. * it takes the undefined instruction trap.
  76. *
  77. * @param regs system registers
  78. *
  79. * @note never invoke this function in application
  80. */
  81. void rt_hw_trap_udef(struct rt_hw_register *regs)
  82. {
  83. rt_hw_show_register(regs);
  84. rt_kprintf("undefined instruction\n");
  85. rt_kprintf("thread - %s stack:\n", rt_current_thread->name);
  86. #ifdef RT_USING_FINSH
  87. list_thread();
  88. #endif
  89. rt_hw_cpu_shutdown();
  90. }
  91. /**
  92. * The software interrupt instruction (SWI) is used for entering
  93. * Supervisor mode, usually to request a particular supervisor
  94. * function.
  95. *
  96. * @param regs system registers
  97. *
  98. * @note never invoke this function in application
  99. */
  100. void rt_hw_trap_swi(struct rt_hw_register *regs)
  101. {
  102. rt_hw_show_register(regs);
  103. rt_kprintf("software interrupt\n");
  104. rt_hw_cpu_shutdown();
  105. }
  106. /**
  107. * An abort indicates that the current memory access cannot be completed,
  108. * which occurs during an instruction prefetch.
  109. *
  110. * @param regs system registers
  111. *
  112. * @note never invoke this function in application
  113. */
  114. void rt_hw_trap_pabt(struct rt_hw_register *regs)
  115. {
  116. rt_hw_show_register(regs);
  117. rt_kprintf("prefetch abort\n");
  118. rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->name);
  119. #ifdef RT_USING_FINSH
  120. list_thread();
  121. #endif
  122. rt_hw_cpu_shutdown();
  123. }
  124. /**
  125. * An abort indicates that the current memory access cannot be completed,
  126. * which occurs during a data access.
  127. *
  128. * @param regs system registers
  129. *
  130. * @note never invoke this function in application
  131. */
  132. void rt_hw_trap_dabt(struct rt_hw_register *regs)
  133. {
  134. rt_hw_show_register(regs);
  135. rt_kprintf("data abort\n");
  136. rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->name);
  137. #ifdef RT_USING_FINSH
  138. list_thread();
  139. #endif
  140. rt_hw_cpu_shutdown();
  141. }
  142. /**
  143. * Normally, system will never reach here
  144. *
  145. * @param regs system registers
  146. *
  147. * @note never invoke this function in application
  148. */
  149. void rt_hw_trap_resv(struct rt_hw_register *regs)
  150. {
  151. rt_kprintf("not used\n");
  152. rt_hw_show_register(regs);
  153. rt_hw_cpu_shutdown();
  154. }
  155. void rt_hw_trap_irq()
  156. {
  157. rt_isr_handler_t isr_func;
  158. rt_uint32_t irqstat;
  159. rt_uint32_t irq;
  160. void *param;
  161. extern struct rt_irq_desc irq_desc[];
  162. /* get irq number */
  163. irqstat = rt_hw_interrupt_get_active(INT_IRQ, &irq);
  164. if (irqstat == 0)
  165. {
  166. rt_kprintf("No interrupt occur\n");
  167. rt_hw_interrupt_ack(INT_IRQ);
  168. return;
  169. }
  170. /* get interrupt service routine */
  171. isr_func = irq_desc[irq].handler;
  172. param = irq_desc[irq].param;
  173. /* turn to interrupt service routine */
  174. isr_func(irq, param);
  175. rt_hw_interrupt_ack(INT_IRQ);
  176. irq_desc[irq].counter ++;
  177. }
  178. void rt_hw_trap_fiq()
  179. {
  180. rt_isr_handler_t isr_func;
  181. rt_uint32_t irqstat;
  182. rt_uint32_t irq;
  183. void *param;
  184. extern struct rt_irq_desc irq_desc[];
  185. /* get irq number */
  186. irqstat = rt_hw_interrupt_get_active(INT_FIQ, &irq);
  187. if (irqstat == 0)
  188. {
  189. rt_kprintf("No interrupt occur\n");
  190. rt_hw_interrupt_ack(INT_FIQ);
  191. return;
  192. }
  193. /* get interrupt service routine */
  194. isr_func = irq_desc[irq].handler;
  195. param = irq_desc[irq].param;
  196. /* turn to interrupt service routine */
  197. isr_func(irq, param);
  198. rt_hw_interrupt_ack(INT_FIQ);
  199. irq_desc[irq].counter ++;
  200. }