xt_instr_macros.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. /* The SET_STACK implements a setting a new stack pointer (sp or a1).
  15. * to do this the need reset PS_WOE, reset WINDOWSTART, update SP, and return PS_WOE.
  16. *
  17. * Note: It has 2 implementations one for using in assembler files (*.S) and one for using in C.
  18. *
  19. * C code prototype for SET_STACK:
  20. * uint32_t ps_reg;
  21. * uint32_t w_base;
  22. * RSR(PS, ps_reg);
  23. * ps_reg &= ~(PS_WOE_MASK | PS_OWB_MASK | PS_CALLINC_MASK);
  24. * WSR(PS, ps_reg);
  25. *
  26. * RSR(WINDOWBASE, w_base);
  27. * WSR(WINDOWSTART, (1 << w_base));
  28. *
  29. * asm volatile ( "movi sp, "XTSTR( (SOC_DRAM_LOW + (SOC_DRAM_HIGH - SOC_DRAM_LOW) / 2) )"");
  30. *
  31. * RSR(PS, ps_reg);
  32. * ps_reg |= (PS_WOE_MASK);
  33. * WSR(PS, ps_reg);
  34. */
  35. #ifdef __ASSEMBLER__
  36. .macro SET_STACK new_sp tmp1 tmp2
  37. rsr.ps \tmp1
  38. movi \tmp2, ~(PS_WOE_MASK | PS_OWB_MASK | PS_CALLINC_MASK)
  39. and \tmp1, \tmp1, \tmp2
  40. wsr.ps \tmp1
  41. rsync
  42. rsr.windowbase \tmp1
  43. ssl \tmp1
  44. movi \tmp1, 1
  45. sll \tmp1, \tmp1
  46. wsr.windowstart \tmp1
  47. rsync
  48. mov sp, \new_sp
  49. rsr.ps \tmp1
  50. movi \tmp2, (PS_WOE)
  51. or \tmp1, \tmp1, \tmp2
  52. wsr.ps \tmp1
  53. rsync
  54. .endm
  55. #else
  56. #define SET_STACK(new_sp) \
  57. do { \
  58. uint32_t tmp1 = 0, tmp2 = 0; \
  59. asm volatile ( \
  60. "rsr.ps %1 \n"\
  61. "movi %2, ~" XTSTR( PS_WOE_MASK | PS_OWB_MASK | PS_CALLINC_MASK ) " \n"\
  62. "and %1, %1, %2 \n"\
  63. "wsr.ps %1 \n"\
  64. "rsync \n"\
  65. " \n"\
  66. "rsr.windowbase %1 \n"\
  67. "ssl %1 \n"\
  68. "movi %1, 1 \n"\
  69. "sll %1, %1 \n"\
  70. "wsr.windowstart %1 \n"\
  71. "rsync \n"\
  72. " \n"\
  73. "mov sp, %0 \n"\
  74. "rsr.ps %1 \n"\
  75. " \n"\
  76. "movi %2, " XTSTR( PS_WOE_MASK ) "\n"\
  77. " \n"\
  78. "or %1, %1, %2 \n"\
  79. "wsr.ps %1 \n"\
  80. "rsync \n"\
  81. : "+r"(new_sp), "+r"(tmp1), "+r"(tmp2)); \
  82. } while (0);
  83. #endif // __ASSEMBLER__