cpuport.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * 2010-01-25 Bernard first version
  9. * 2012-05-31 aozima Merge all of the C source code into cpuport.c
  10. * 2012-08-17 aozima fixed bug: store r8 - r11.
  11. * 2012-12-23 aozima stack addr align to 8byte.
  12. * 2019-03-31 xuzhuoyi port to Cortex-M23.
  13. */
  14. #include <rtthread.h>
  15. #include "cpuport.h"
  16. /* flag in interrupt handling */
  17. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  18. rt_uint32_t rt_thread_switch_interrupt_flag;
  19. /**
  20. * This function will initialize thread stack
  21. *
  22. * @param tentry the entry of thread
  23. * @param parameter the parameter of entry
  24. * @param stack_addr the beginning stack address
  25. * @param texit the function will be called when thread exit
  26. *
  27. * @return stack address
  28. */
  29. rt_uint8_t *rt_hw_stack_init(void *tentry,
  30. void *parameter,
  31. rt_uint8_t *stack_addr,
  32. void *texit)
  33. {
  34. struct stack_frame *stack_frame;
  35. rt_uint8_t *stk;
  36. unsigned long i;
  37. stk = stack_addr + sizeof(rt_uint32_t);
  38. stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stk, 8);
  39. stk -= sizeof(struct stack_frame);
  40. stack_frame = (struct stack_frame *)stk;
  41. /* init all register */
  42. for (i = 0; i < sizeof(struct stack_frame) / sizeof(rt_uint32_t); i ++)
  43. {
  44. ((rt_uint32_t *)stack_frame)[i] = 0xdeadbeef;
  45. }
  46. stack_frame->exception_stack_frame.r0 = (unsigned long)parameter; /* r0 : argument */
  47. stack_frame->exception_stack_frame.r1 = 0; /* r1 */
  48. stack_frame->exception_stack_frame.r2 = 0; /* r2 */
  49. stack_frame->exception_stack_frame.r3 = 0; /* r3 */
  50. stack_frame->exception_stack_frame.r12 = 0; /* r12 */
  51. stack_frame->exception_stack_frame.lr = (unsigned long)texit; /* lr */
  52. stack_frame->exception_stack_frame.pc = (unsigned long)tentry; /* entry point, pc */
  53. stack_frame->exception_stack_frame.psr = 0x01000000L; /* PSR */
  54. /* return task's current stack address */
  55. return stk;
  56. }
  57. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  58. extern long list_thread(void);
  59. #endif
  60. extern rt_thread_t rt_current_thread;
  61. /**
  62. * fault exception handling
  63. */
  64. void rt_hw_hard_fault_exception(struct exception_stack_frame *contex)
  65. {
  66. rt_kprintf("psr: 0x%08x\n", contex->psr);
  67. rt_kprintf(" pc: 0x%08x\n", contex->pc);
  68. rt_kprintf(" lr: 0x%08x\n", contex->lr);
  69. rt_kprintf("r12: 0x%08x\n", contex->r12);
  70. rt_kprintf("r03: 0x%08x\n", contex->r3);
  71. rt_kprintf("r02: 0x%08x\n", contex->r2);
  72. rt_kprintf("r01: 0x%08x\n", contex->r1);
  73. rt_kprintf("r00: 0x%08x\n", contex->r0);
  74. rt_kprintf("hard fault on thread: %s\n", rt_current_thread->parent.name);
  75. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  76. list_thread();
  77. #endif
  78. while (1);
  79. }