cpuport.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * File : hwport.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 - 2011, 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. * 2011-02-23 Bernard the first version
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include <mb9bf506r.h>
  17. #include <core_cm3.h>
  18. /* switch flag on interrupt and thread pointer to save switch record */
  19. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  20. rt_uint32_t rt_thread_switch_interrput_flag;
  21. /* stack context in ARM Cortex-M3 */
  22. struct stack_context
  23. {
  24. rt_uint32_t r0;
  25. rt_uint32_t r1;
  26. rt_uint32_t r2;
  27. rt_uint32_t r3;
  28. rt_uint32_t r12;
  29. rt_uint32_t lr;
  30. rt_uint32_t pc;
  31. rt_uint32_t psr;
  32. };
  33. extern void rt_hw_interrupt_thread_switch(void);
  34. extern void list_thread(void);
  35. extern rt_thread_t rt_current_thread;
  36. void rt_hw_hard_fault_exception(struct stack_context* contex)
  37. {
  38. rt_kprintf("psr: 0x%08x\n", contex->psr);
  39. rt_kprintf(" pc: 0x%08x\n", contex->pc);
  40. rt_kprintf(" lr: 0x%08x\n", contex->lr);
  41. rt_kprintf("r12: 0x%08x\n", contex->r12);
  42. rt_kprintf("r03: 0x%08x\n", contex->r3);
  43. rt_kprintf("r02: 0x%08x\n", contex->r2);
  44. rt_kprintf("r01: 0x%08x\n", contex->r1);
  45. rt_kprintf("r00: 0x%08x\n", contex->r0);
  46. if (rt_current_thread != RT_NULL)
  47. {
  48. rt_kprintf("hard fault on thread: %s\n", rt_current_thread->name);
  49. #ifdef RT_USING_FINSH
  50. list_thread();
  51. #endif
  52. while (1);
  53. }
  54. else
  55. {
  56. rt_kprintf("hard fault on initialization\n");
  57. }
  58. }
  59. /**
  60. * reset MCU
  61. *
  62. */
  63. void rt_hw_cpu_reset()
  64. {
  65. NVIC_SystemReset();
  66. }
  67. /**
  68. * shutdown CPU
  69. *
  70. */
  71. void rt_hw_cpu_shutdown()
  72. {
  73. rt_kprintf("shutdown...\n");
  74. RT_ASSERT(0);
  75. }
  76. /**
  77. * This function will initialize thread stack
  78. *
  79. * @param tentry the entry of thread
  80. * @param parameter the parameter of entry
  81. * @param stack_addr the beginning stack address
  82. * @param texit the function will be called when thread exit
  83. *
  84. * @return stack address
  85. */
  86. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  87. rt_uint8_t *stack_addr, void *texit)
  88. {
  89. unsigned long *stk;
  90. stk = (unsigned long *)stack_addr;
  91. *(stk) = 0x01000000L; /* PSR */
  92. *(--stk) = (unsigned long)tentry; /* entry point, pc */
  93. *(--stk) = (unsigned long)texit; /* lr */
  94. *(--stk) = 0; /* r12 */
  95. *(--stk) = 0; /* r3 */
  96. *(--stk) = 0; /* r2 */
  97. *(--stk) = 0; /* r1 */
  98. *(--stk) = (unsigned long)parameter; /* r0 : argument */
  99. *(--stk) = 0; /* r11 */
  100. *(--stk) = 0; /* r10 */
  101. *(--stk) = 0; /* r9 */
  102. *(--stk) = 0; /* r8 */
  103. *(--stk) = 0; /* r7 */
  104. *(--stk) = 0; /* r6 */
  105. *(--stk) = 0; /* r5 */
  106. *(--stk) = 0; /* r4 */
  107. /* return task's current stack address */
  108. return (rt_uint8_t *)stk;
  109. }