trap.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #define INT_IRQ 0x00
  28. #define INT_FIQ 0x01
  29. extern struct rt_thread *rt_current_thread;
  30. #ifdef RT_USING_FINSH
  31. extern long list_thread(void);
  32. #endif
  33. struct rt_hw_register
  34. {
  35. rt_uint32_t r0;
  36. rt_uint32_t r1;
  37. rt_uint32_t r2;
  38. rt_uint32_t r3;
  39. rt_uint32_t r4;
  40. rt_uint32_t r5;
  41. rt_uint32_t r6;
  42. rt_uint32_t r7;
  43. rt_uint32_t r8;
  44. rt_uint32_t r9;
  45. rt_uint32_t r10;
  46. rt_uint32_t fp;
  47. rt_uint32_t ip;
  48. rt_uint32_t sp;
  49. rt_uint32_t lr;
  50. rt_uint32_t pc;
  51. rt_uint32_t cpsr;
  52. rt_uint32_t ORIG_r0;
  53. };
  54. /**
  55. * this function will show registers of CPU
  56. *
  57. * @param regs the registers point
  58. */
  59. void rt_hw_show_register (struct rt_hw_register *regs)
  60. {
  61. rt_kprintf("Execption:\n");
  62. rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n",
  63. regs->r0, regs->r1, regs->r2, regs->r3);
  64. rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n",
  65. regs->r4, regs->r5, regs->r6, regs->r7);
  66. rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n",
  67. regs->r8, regs->r9, regs->r10);
  68. rt_kprintf("fp :0x%08x ip :0x%08x\n",
  69. regs->fp, regs->ip);
  70. rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n",
  71. regs->sp, regs->lr, regs->pc);
  72. rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
  73. }
  74. /**
  75. * When ARM7TDMI comes across an instruction which it cannot handle,
  76. * it takes the undefined instruction trap.
  77. *
  78. * @param regs system registers
  79. *
  80. * @note never invoke this function in application
  81. */
  82. void rt_hw_trap_udef(struct rt_hw_register *regs)
  83. {
  84. rt_hw_show_register(regs);
  85. rt_kprintf("undefined instruction\n");
  86. rt_kprintf("thread - %s stack:\n", rt_current_thread->name);
  87. #ifdef RT_USING_FINSH
  88. list_thread();
  89. #endif
  90. rt_hw_cpu_shutdown();
  91. }
  92. /**
  93. * The software interrupt instruction (SWI) is used for entering
  94. * Supervisor mode, usually to request a particular supervisor
  95. * function.
  96. *
  97. * @param regs system registers
  98. *
  99. * @note never invoke this function in application
  100. */
  101. void rt_hw_trap_swi(struct rt_hw_register *regs)
  102. {
  103. rt_hw_show_register(regs);
  104. rt_kprintf("software interrupt\n");
  105. rt_hw_cpu_shutdown();
  106. }
  107. /**
  108. * An abort indicates that the current memory access cannot be completed,
  109. * which occurs during an instruction prefetch.
  110. *
  111. * @param regs system registers
  112. *
  113. * @note never invoke this function in application
  114. */
  115. void rt_hw_trap_pabt(struct rt_hw_register *regs)
  116. {
  117. rt_hw_show_register(regs);
  118. rt_kprintf("prefetch abort\n");
  119. rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->name);
  120. #ifdef RT_USING_FINSH
  121. list_thread();
  122. #endif
  123. rt_hw_cpu_shutdown();
  124. }
  125. /**
  126. * An abort indicates that the current memory access cannot be completed,
  127. * which occurs during a data access.
  128. *
  129. * @param regs system registers
  130. *
  131. * @note never invoke this function in application
  132. */
  133. void rt_hw_trap_dabt(struct rt_hw_register *regs)
  134. {
  135. rt_hw_show_register(regs);
  136. rt_kprintf("data abort\n");
  137. rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->name);
  138. #ifdef RT_USING_FINSH
  139. list_thread();
  140. #endif
  141. rt_hw_cpu_shutdown();
  142. }
  143. /**
  144. * Normally, system will never reach here
  145. *
  146. * @param regs system registers
  147. *
  148. * @note never invoke this function in application
  149. */
  150. void rt_hw_trap_resv(struct rt_hw_register *regs)
  151. {
  152. rt_kprintf("not used\n");
  153. rt_hw_show_register(regs);
  154. rt_hw_cpu_shutdown();
  155. }
  156. extern struct rt_irq_desc irq_desc[];
  157. extern rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq);
  158. extern void rt_hw_interrupt_ack(rt_uint32_t fiq_irq, rt_uint32_t id);
  159. void rt_hw_trap_irq()
  160. {
  161. rt_isr_handler_t isr_func;
  162. rt_uint32_t irq;
  163. void *param;
  164. /* get irq number */
  165. irq = rt_hw_interrupt_get_active(INT_IRQ);
  166. /* get interrupt service routine */
  167. isr_func = irq_desc[irq].handler;
  168. param = irq_desc[irq].param;
  169. /* turn to interrupt service routine */
  170. isr_func(irq, param);
  171. rt_hw_interrupt_ack(INT_IRQ, irq);
  172. #ifdef RT_USING_INTERRUPT_INFO
  173. irq_desc[irq].counter ++;
  174. #endif
  175. }
  176. void rt_hw_trap_fiq()
  177. {
  178. rt_isr_handler_t isr_func;
  179. rt_uint32_t irq;
  180. void *param;
  181. /* get irq number */
  182. irq = rt_hw_interrupt_get_active(INT_FIQ);
  183. /* get interrupt service routine */
  184. isr_func = irq_desc[irq].handler;
  185. param = irq_desc[irq].param;
  186. /* turn to interrupt service routine */
  187. isr_func(irq, param);
  188. rt_hw_interrupt_ack(INT_FIQ, irq);
  189. #ifdef RT_USING_INTERRUPT_INFO
  190. irq_desc[irq].counter ++;
  191. #endif
  192. }