cpuport.c 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-02-13 mojingxian first version
  9. */
  10. #include <rtthread.h>
  11. /* flag in interrupt handling */
  12. rt_uint32_t rt_interrupt_from_thread;
  13. rt_uint32_t rt_interrupt_to_thread;
  14. rt_uint32_t rt_thread_switch_interrupt_flag;
  15. /**
  16. * initializes stack of thread
  17. */
  18. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  19. rt_uint8_t *stack_addr, void *texit)
  20. {
  21. unsigned char i;
  22. unsigned long *stk;
  23. stk = (unsigned long *)stack_addr; /* Load stack pointer */
  24. /* Simulate a function call to the task with an argument */
  25. stk -= 3; /* 3 words assigned for incoming args (R0, R1, R2) */
  26. /* Now simulating vectoring to an ISR */
  27. *--stk = (unsigned long)parameter; /* R0 value - caller's incoming argument #1 */
  28. *--stk = (unsigned long)0; /* P1 value - value irrelevant */
  29. *--stk = (unsigned long)texit; /* RETS value - NO task should return with RTS. */
  30. /* however OS_CPU_Invalid_Task_Return is a safety */
  31. /* catch-allfor tasks that return with an RTS */
  32. *--stk = (unsigned long)parameter; /* R1 value - caller's incoming argument #2 */
  33. /* (not relevant in current test example) */
  34. *--stk = (unsigned long)parameter; /* R2 value - caller's incoming argument #3 */
  35. /* (not relevant in current test example) */
  36. *--stk = (unsigned long)0; /* P0 value - value irrelevant */
  37. *--stk = (unsigned long)0; /* P2 value - value irrelevant */
  38. *--stk = (unsigned long)0; /* ASTAT value - caller's ASTAT value - value */
  39. /* irrelevant */
  40. *--stk = (unsigned long)tentry; /* RETI value- pushing the start address of the task */
  41. for (i = 0; i < 35; i++) /* remaining reg values - R7:3, P5:3, */
  42. { /* 4 words of A1:0(.W,.X), LT0, LT1, */
  43. *--stk = (unsigned long)0; /* LC0, LC1, LB0, LB1,I3:0, M3:0, L3:0, B3:0, */
  44. } /* All values irrelevant */
  45. return (rt_uint8_t *)stk; /* Return top-of-stack */
  46. }
  47. void rt_hw_context_switch(rt_uint32_t from, rt_uint32_t to)
  48. {
  49. if (rt_thread_switch_interrupt_flag != 1)
  50. {
  51. rt_thread_switch_interrupt_flag = 1;
  52. rt_interrupt_from_thread = from;
  53. }
  54. rt_interrupt_to_thread = to;
  55. asm("raise 14;"); // Raise Interrupt 14 (trap)
  56. }
  57. void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to)
  58. {
  59. if (rt_thread_switch_interrupt_flag != 1)
  60. {
  61. rt_thread_switch_interrupt_flag = 1;
  62. rt_interrupt_from_thread = from;
  63. }
  64. rt_interrupt_to_thread = to;
  65. asm("raise 14;"); // Raise Interrupt 14 (trap)
  66. }
  67. void rt_hw_context_switch_to(rt_uint32_t to)
  68. {
  69. rt_thread_switch_interrupt_flag = 1;
  70. rt_interrupt_from_thread = 0;
  71. rt_interrupt_to_thread = to;
  72. asm("raise 14;"); // Raise Interrupt 14 (trap)
  73. }