cpu_ll.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include <stdint.h>
  15. #include "soc/soc_caps.h"
  16. #include "soc/dport_access.h"
  17. #include "soc/system_reg.h"
  18. #include "esp_bit_defs.h"
  19. #include "soc/assist_debug_reg.h"
  20. #include "esp_attr.h"
  21. #include "riscv/csr.h"
  22. /*performance counter*/
  23. #define CSR_PCER_MACHINE 0x7e0
  24. #define CSR_PCMR_MACHINE 0x7e1
  25. #define CSR_PCCR_MACHINE 0x7e2
  26. /*fast gpio*/
  27. #define CSR_GPIO_OEN_USER 0x803
  28. #define CSR_GPIO_IN_USER 0x804
  29. #define CSR_GPIO_OUT_USER 0x805
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. static inline int IRAM_ATTR cpu_ll_get_core_id(void)
  34. {
  35. #if SOC_CPU_CORES_NUM == 1
  36. return 0; // No need to check core ID on single core hardware
  37. #else
  38. int cpuid;
  39. cpuid = RV_READ_CSR(mhartid);
  40. return cpuid;
  41. #endif
  42. }
  43. static inline void cpu_ll_enable_cycle_count(void)
  44. {
  45. RV_WRITE_CSR(CSR_PCER_MACHINE,1);
  46. RV_WRITE_CSR(CSR_PCMR_MACHINE,1);
  47. return;
  48. }
  49. static inline uint32_t IRAM_ATTR cpu_ll_get_cycle_count(void)
  50. {
  51. uint32_t result;
  52. result = RV_READ_CSR(CSR_PCCR_MACHINE);
  53. return result;
  54. }
  55. static inline void IRAM_ATTR cpu_ll_set_cycle_count(uint32_t val)
  56. {
  57. RV_WRITE_CSR(CSR_PCCR_MACHINE, val);
  58. }
  59. static inline void* cpu_ll_get_sp(void)
  60. {
  61. void *sp;
  62. asm volatile ("mv %0, sp;" : "=r" (sp));
  63. return sp;
  64. }
  65. static inline void cpu_ll_init_hwloop(void)
  66. {
  67. // Nothing needed here for ESP32-C3
  68. }
  69. static inline void cpu_ll_set_breakpoint(int id, uint32_t pc)
  70. {
  71. RV_WRITE_CSR(tselect,id);
  72. RV_SET_CSR(CSR_TCONTROL,TCONTROL_MTE);
  73. RV_SET_CSR(CSR_TDATA1, TDATA1_USER|TDATA1_MACHINE|TDATA1_EXECUTE);
  74. RV_WRITE_CSR(tdata2,pc);
  75. return;
  76. }
  77. static inline void cpu_ll_clear_breakpoint(int id)
  78. {
  79. RV_WRITE_CSR(tselect,id);
  80. RV_CLEAR_CSR(CSR_TCONTROL,TCONTROL_MTE);
  81. RV_CLEAR_CSR(CSR_TDATA1, TDATA1_USER|TDATA1_MACHINE|TDATA1_EXECUTE);
  82. return;
  83. }
  84. static inline uint32_t cpu_ll_ptr_to_pc(const void* addr)
  85. {
  86. return ((uint32_t) addr);
  87. }
  88. static inline void* cpu_ll_pc_to_ptr(uint32_t pc)
  89. {
  90. return (void*) ((pc & 0x3fffffff) | 0x40000000);
  91. }
  92. static inline void cpu_ll_set_watchpoint(int id,
  93. const void* addr,
  94. size_t size,
  95. bool on_read,
  96. bool on_write)
  97. {
  98. uint32_t addr_napot;
  99. RV_WRITE_CSR(tselect,id);
  100. RV_SET_CSR(CSR_TCONTROL, TCONTROL_MPTE | TCONTROL_MTE);
  101. RV_SET_CSR(CSR_TDATA1, TDATA1_USER|TDATA1_MACHINE);
  102. RV_SET_CSR_FIELD(CSR_TDATA1, TDATA1_MATCH, 1);
  103. // add 0 in napot encoding
  104. addr_napot = ((uint32_t) addr) | ((size >> 1) - 1);
  105. if (on_read) {
  106. RV_SET_CSR(CSR_TDATA1, TDATA1_LOAD);
  107. }
  108. if (on_write) {
  109. RV_SET_CSR(CSR_TDATA1, TDATA1_STORE);
  110. }
  111. RV_WRITE_CSR(tdata2,addr_napot);
  112. return;
  113. }
  114. static inline void cpu_ll_clear_watchpoint(int id)
  115. {
  116. RV_WRITE_CSR(tselect,id);
  117. RV_CLEAR_CSR(CSR_TCONTROL,TCONTROL_MTE);
  118. RV_CLEAR_CSR(CSR_TDATA1, TDATA1_USER|TDATA1_MACHINE);
  119. RV_CLEAR_CSR_FIELD(CSR_TDATA1,TDATA1_MATCH);
  120. RV_CLEAR_CSR(CSR_TDATA1, TDATA1_MACHINE);
  121. RV_CLEAR_CSR(CSR_TDATA1, TDATA1_LOAD|TDATA1_STORE|TDATA1_EXECUTE);
  122. return;
  123. }
  124. FORCE_INLINE_ATTR bool cpu_ll_is_debugger_attached(void)
  125. {
  126. return REG_GET_BIT(ASSIST_DEBUG_CORE_0_DEBUG_MODE_REG, ASSIST_DEBUG_CORE_0_DEBUG_MODULE_ACTIVE);
  127. }
  128. static inline void cpu_ll_break(void)
  129. {
  130. asm volatile("ebreak\n");
  131. return;
  132. }
  133. static inline void cpu_ll_set_vecbase(const void* vecbase)
  134. {
  135. uintptr_t vecbase_int = (uintptr_t)vecbase;
  136. vecbase_int |= 1; // Set MODE field to treat MTVEC as a vector base address
  137. RV_WRITE_CSR(mtvec, vecbase_int);
  138. }
  139. static inline void cpu_ll_waiti(void)
  140. {
  141. if (cpu_ll_is_debugger_attached() && DPORT_REG_GET_BIT(SYSTEM_CPU_PER_CONF_REG, SYSTEM_CPU_WAIT_MODE_FORCE_ON) == 0) {
  142. /* when SYSTEM_CPU_WAIT_MODE_FORCE_ON is disabled in WFI mode SBA access to memory does not work for debugger,
  143. so do not enter that mode when debugger is connected */
  144. return;
  145. }
  146. asm volatile ("wfi\n");
  147. }
  148. static inline void cpu_ll_enable_dedic_gpio_output(uint32_t mask)
  149. {
  150. RV_WRITE_CSR(CSR_GPIO_OEN_USER, mask);
  151. }
  152. static inline void cpu_ll_write_dedic_gpio_all(uint32_t value)
  153. {
  154. RV_WRITE_CSR(CSR_GPIO_OUT_USER, value);
  155. }
  156. static inline uint32_t cpu_ll_read_dedic_gpio_in(void)
  157. {
  158. uint32_t value = RV_READ_CSR(CSR_GPIO_IN_USER);
  159. return value;
  160. }
  161. static inline uint32_t cpu_ll_read_dedic_gpio_out(void)
  162. {
  163. uint32_t value = RV_READ_CSR(CSR_GPIO_OUT_USER);
  164. return value;
  165. }
  166. static inline void cpu_ll_write_dedic_gpio_mask(uint32_t mask, uint32_t value)
  167. {
  168. RV_SET_CSR(CSR_GPIO_OUT_USER, mask & value);
  169. RV_CLEAR_CSR(CSR_GPIO_OUT_USER, mask & ~(value));
  170. }
  171. #ifdef __cplusplus
  172. }
  173. #endif