cpuport.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * 2021/02/19 Bernard Implement rt_hw_context_switch_interrupt in C
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  13. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  14. volatile rt_uint32_t rt_thread_switch_interrupt_flag = 0;
  15. rt_base_t rt_hw_interrupt_disable(void)
  16. {
  17. rt_base_t level;
  18. __asm__ __volatile__("pushfl ; popl %0 ; cli":"=g" (level): :"memory");
  19. return level;
  20. }
  21. void rt_hw_interrupt_enable(rt_base_t level)
  22. {
  23. __asm__ __volatile__("pushl %0 ; popfl": :"g" (level):"memory", "cc");
  24. }
  25. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
  26. {
  27. if (rt_thread_switch_interrupt_flag == 0)
  28. rt_interrupt_from_thread = from;
  29. rt_interrupt_to_thread = to;
  30. rt_thread_switch_interrupt_flag = 1;
  31. return ;
  32. }
  33. /**
  34. * This function will initialize thread stack
  35. *
  36. * @param tentry the entry of thread
  37. * @param parameter the parameter of entry
  38. * @param stack_addr the beginning stack address
  39. * @param texit the function will be called when thread exit
  40. *
  41. * @return stack address
  42. */
  43. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  44. rt_uint8_t *stack_addr, void *texit)
  45. {
  46. unsigned long *stk;
  47. stk = (unsigned long *)stack_addr;
  48. *(--stk) = (unsigned long)parameter;
  49. *(--stk) = (unsigned long)texit;
  50. *(--stk) = 0x200; /*flags*/
  51. *(--stk) = 0x08; /*cs*/
  52. *(--stk) = (unsigned long)tentry; /*eip*/
  53. *(--stk) = 0; /*irqno*/
  54. *(--stk) = 0x10; /*ds*/
  55. *(--stk) = 0x10; /*es*/
  56. *(--stk) = 0; /*eax*/
  57. *(--stk) = 0; /*ecx*/
  58. *(--stk) = 0; /*edx*/
  59. *(--stk) = 0; /*ebx*/
  60. *(--stk) = 0; /*esp*/
  61. *(--stk) = 0; /*ebp*/
  62. *(--stk) = 0; /*esi*/
  63. *(--stk) = 0; /*edi*/
  64. /* return task's current stack address */
  65. return (rt_uint8_t *)stk;
  66. }