dport_access.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright 2010-2017 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. /*
  14. * DPORT access is used for do protection when dual core access DPORT internal register and APB register via DPORT simultaneously
  15. * This function will be initialize after FreeRTOS startup.
  16. * When cpu0 want to access DPORT register, it should notify cpu1 enter in high-priority interrupt for be mute. When cpu1 already in high-priority interrupt,
  17. * cpu0 can access DPORT register. Currently, cpu1 will wait for cpu0 finish access and exit high-priority interrupt.
  18. */
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <sdkconfig.h>
  22. #include "esp_attr.h"
  23. #include "esp_err.h"
  24. #include "esp_intr_alloc.h"
  25. #include "esp32/rom/ets_sys.h"
  26. #include "esp32/rom/uart.h"
  27. #include "soc/cpu.h"
  28. #include "soc/dport_reg.h"
  29. #include "soc/spi_periph.h"
  30. #include "freertos/FreeRTOS.h"
  31. #include "freertos/task.h"
  32. #include "freertos/semphr.h"
  33. #include "freertos/queue.h"
  34. #include "xtensa/core-macros.h"
  35. #ifndef CONFIG_FREERTOS_UNICORE
  36. static portMUX_TYPE g_dport_mux = portMUX_INITIALIZER_UNLOCKED;
  37. #define DPORT_CORE_STATE_IDLE 0
  38. #define DPORT_CORE_STATE_RUNNING 1
  39. static uint32_t volatile dport_core_state[portNUM_PROCESSORS]; //cpu is already run
  40. /* these global variables are accessed from interrupt vector, hence not declared as static */
  41. uint32_t volatile dport_access_start[portNUM_PROCESSORS]; //dport register could be accessed
  42. uint32_t volatile dport_access_end[portNUM_PROCESSORS]; //dport register is accessed over
  43. static uint32_t volatile dport_access_ref[portNUM_PROCESSORS]; //dport access reference
  44. #ifdef DPORT_ACCESS_BENCHMARK
  45. #define DPORT_ACCESS_BENCHMARK_STORE_NUM
  46. static uint32_t ccount_start[portNUM_PROCESSORS];
  47. static uint32_t ccount_end[portNUM_PROCESSORS];
  48. static uint32_t ccount_margin[portNUM_PROCESSORS][DPORT_ACCESS_BENCHMARK_STORE_NUM];
  49. static uint32_t ccount_margin_cnt;
  50. #endif
  51. static BaseType_t oldInterruptLevel[2];
  52. #endif // CONFIG_FREERTOS_UNICORE
  53. /* stall other cpu that this cpu is pending to access dport register start */
  54. void IRAM_ATTR esp_dport_access_stall_other_cpu_start(void)
  55. {
  56. #ifndef CONFIG_FREERTOS_UNICORE
  57. if (dport_core_state[0] == DPORT_CORE_STATE_IDLE
  58. || dport_core_state[1] == DPORT_CORE_STATE_IDLE) {
  59. return;
  60. }
  61. BaseType_t intLvl = portENTER_CRITICAL_NESTED();
  62. int cpu_id = xPortGetCoreID();
  63. #ifdef DPORT_ACCESS_BENCHMARK
  64. ccount_start[cpu_id] = XTHAL_GET_CCOUNT();
  65. #endif
  66. if (dport_access_ref[cpu_id] == 0) {
  67. portENTER_CRITICAL_ISR(&g_dport_mux);
  68. oldInterruptLevel[cpu_id]=intLvl;
  69. dport_access_start[cpu_id] = 0;
  70. dport_access_end[cpu_id] = 0;
  71. if (cpu_id == 0) {
  72. _DPORT_REG_WRITE(DPORT_CPU_INTR_FROM_CPU_3_REG, DPORT_CPU_INTR_FROM_CPU_3); //interrupt on cpu1
  73. } else {
  74. _DPORT_REG_WRITE(DPORT_CPU_INTR_FROM_CPU_2_REG, DPORT_CPU_INTR_FROM_CPU_2); //interrupt on cpu0
  75. }
  76. while (!dport_access_start[cpu_id]) {};
  77. REG_READ(SPI_DATE_REG(3)); //just read a APB register sure that the APB-bus is idle
  78. }
  79. dport_access_ref[cpu_id]++;
  80. if (dport_access_ref[cpu_id] > 1) {
  81. /* Interrupts are already disabled by the parent, we're nested here. */
  82. portEXIT_CRITICAL_NESTED(intLvl);
  83. }
  84. #endif /* CONFIG_FREERTOS_UNICORE */
  85. }
  86. /* stall other cpu that this cpu is pending to access dport register end */
  87. void IRAM_ATTR esp_dport_access_stall_other_cpu_end(void)
  88. {
  89. #ifndef CONFIG_FREERTOS_UNICORE
  90. int cpu_id = xPortGetCoreID();
  91. if (dport_core_state[0] == DPORT_CORE_STATE_IDLE
  92. || dport_core_state[1] == DPORT_CORE_STATE_IDLE) {
  93. return;
  94. }
  95. if (dport_access_ref[cpu_id] == 0) {
  96. assert(0);
  97. }
  98. dport_access_ref[cpu_id]--;
  99. if (dport_access_ref[cpu_id] == 0) {
  100. dport_access_end[cpu_id] = 1;
  101. portEXIT_CRITICAL_ISR(&g_dport_mux);
  102. portEXIT_CRITICAL_NESTED(oldInterruptLevel[cpu_id]);
  103. }
  104. #ifdef DPORT_ACCESS_BENCHMARK
  105. ccount_end[cpu_id] = XTHAL_GET_CCOUNT();
  106. ccount_margin[cpu_id][ccount_margin_cnt] = ccount_end[cpu_id] - ccount_start[cpu_id];
  107. ccount_margin_cnt = (ccount_margin_cnt + 1)&(DPORT_ACCESS_BENCHMARK_STORE_NUM - 1);
  108. #endif
  109. #endif /* CONFIG_FREERTOS_UNICORE */
  110. }
  111. #ifndef CONFIG_FREERTOS_UNICORE
  112. static void dport_access_init_core(void *arg)
  113. {
  114. int core_id = 0;
  115. uint32_t intr_source = ETS_FROM_CPU_INTR2_SOURCE;
  116. core_id = xPortGetCoreID();
  117. if (core_id == 1) {
  118. intr_source = ETS_FROM_CPU_INTR3_SOURCE;
  119. }
  120. ESP_INTR_DISABLE(ETS_DPORT_INUM);
  121. intr_matrix_set(core_id, intr_source, ETS_DPORT_INUM);
  122. ESP_INTR_ENABLE(ETS_DPORT_INUM);
  123. dport_access_ref[core_id] = 0;
  124. dport_access_start[core_id] = 0;
  125. dport_access_end[core_id] = 0;
  126. dport_core_state[core_id] = DPORT_CORE_STATE_RUNNING;
  127. /* If this fails then the minimum stack size for this config is too close to running out */
  128. assert(uxTaskGetStackHighWaterMark(NULL) > 128);
  129. vTaskDelete(NULL);
  130. }
  131. #endif
  132. /* Defer initialisation until after scheduler is running */
  133. void esp_dport_access_int_init(void)
  134. {
  135. #ifndef CONFIG_FREERTOS_UNICORE
  136. portBASE_TYPE res = xTaskCreatePinnedToCore(&dport_access_init_core, "dport", configMINIMAL_STACK_SIZE, NULL, 5, NULL, xPortGetCoreID());
  137. assert(res == pdTRUE);
  138. #endif
  139. }
  140. void IRAM_ATTR esp_dport_access_int_pause(void)
  141. {
  142. #ifndef CONFIG_FREERTOS_UNICORE
  143. portENTER_CRITICAL_ISR(&g_dport_mux);
  144. dport_core_state[0] = DPORT_CORE_STATE_IDLE;
  145. dport_core_state[1] = DPORT_CORE_STATE_IDLE;
  146. portEXIT_CRITICAL_ISR(&g_dport_mux);
  147. #endif
  148. }
  149. //Used in panic code: the enter_critical stuff may be messed up so we just stop everything without checking the mux.
  150. void IRAM_ATTR esp_dport_access_int_abort(void)
  151. {
  152. #ifndef CONFIG_FREERTOS_UNICORE
  153. dport_core_state[0] = DPORT_CORE_STATE_IDLE;
  154. dport_core_state[1] = DPORT_CORE_STATE_IDLE;
  155. #endif
  156. }
  157. void IRAM_ATTR esp_dport_access_int_resume(void)
  158. {
  159. #ifndef CONFIG_FREERTOS_UNICORE
  160. portENTER_CRITICAL_ISR(&g_dport_mux);
  161. dport_core_state[0] = DPORT_CORE_STATE_RUNNING;
  162. dport_core_state[1] = DPORT_CORE_STATE_RUNNING;
  163. portEXIT_CRITICAL_ISR(&g_dport_mux);
  164. #endif
  165. }
  166. /**
  167. * @brief Read a sequence of DPORT registers to the buffer, SMP-safe version.
  168. *
  169. * This implementation uses a method of the pre-reading of the APB register
  170. * before reading the register of the DPORT, without stall other CPU.
  171. * There is disable/enable interrupt.
  172. *
  173. * @param[out] buff_out Contains the read data.
  174. * @param[in] address Initial address for reading registers.
  175. * @param[in] num_words The number of words.
  176. */
  177. void IRAM_ATTR esp_dport_access_read_buffer(uint32_t *buff_out, uint32_t address, uint32_t num_words)
  178. {
  179. DPORT_INTERRUPT_DISABLE();
  180. for (uint32_t i = 0; i < num_words; ++i) {
  181. buff_out[i] = DPORT_SEQUENCE_REG_READ(address + i * 4);
  182. }
  183. DPORT_INTERRUPT_RESTORE();
  184. }
  185. /**
  186. * @brief Read value from register, SMP-safe version.
  187. *
  188. * This method uses the pre-reading of the APB register before reading the register of the DPORT.
  189. * This implementation is useful for reading DORT registers for single reading without stall other CPU.
  190. * There is disable/enable interrupt.
  191. *
  192. * @param reg Register address
  193. * @return Value
  194. */
  195. uint32_t IRAM_ATTR esp_dport_access_reg_read(uint32_t reg)
  196. {
  197. #if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM)
  198. return _DPORT_REG_READ(reg);
  199. #else
  200. uint32_t apb;
  201. unsigned int intLvl;
  202. __asm__ __volatile__ (\
  203. "rsil %[LVL], "XTSTR(CONFIG_ESP32_DPORT_DIS_INTERRUPT_LVL)"\n"\
  204. "movi %[APB], "XTSTR(0x3ff40078)"\n"\
  205. "l32i %[APB], %[APB], 0\n"\
  206. "l32i %[REG], %[REG], 0\n"\
  207. "wsr %[LVL], "XTSTR(PS)"\n"\
  208. "rsync\n"\
  209. : [APB]"=a"(apb), [REG]"+a"(reg), [LVL]"=a"(intLvl)\
  210. : \
  211. : "memory" \
  212. );
  213. return reg;
  214. #endif
  215. }
  216. /**
  217. * @brief Read value from register, NOT SMP-safe version.
  218. *
  219. * This method uses the pre-reading of the APB register before reading the register of the DPORT.
  220. * There is not disable/enable interrupt.
  221. * The difference from DPORT_REG_READ() is that the user himself must disable interrupts while DPORT reading.
  222. * This implementation is useful for reading DORT registers in loop without stall other CPU. Note the usage example.
  223. * The recommended way to read registers sequentially without stall other CPU
  224. * is to use the method esp_dport_read_buffer(buff_out, address, num_words). It allows you to read registers in the buffer.
  225. *
  226. * \code{c}
  227. * // This example shows how to use it.
  228. * { // Use curly brackets to limit the visibility of variables in macros DPORT_INTERRUPT_DISABLE/RESTORE.
  229. * DPORT_INTERRUPT_DISABLE(); // Disable interrupt only on current CPU.
  230. * for (i = 0; i < max; ++i) {
  231. * array[i] = esp_dport_access_sequence_reg_read(Address + i * 4); // reading DPORT registers
  232. * }
  233. * DPORT_INTERRUPT_RESTORE(); // restore the previous interrupt level
  234. * }
  235. * \endcode
  236. *
  237. * @param reg Register address
  238. * @return Value
  239. */
  240. uint32_t IRAM_ATTR esp_dport_access_sequence_reg_read(uint32_t reg)
  241. {
  242. #if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM)
  243. return _DPORT_REG_READ(reg);
  244. #else
  245. uint32_t apb;
  246. __asm__ __volatile__ (\
  247. "movi %[APB], "XTSTR(0x3ff40078)"\n"\
  248. "l32i %[APB], %[APB], 0\n"\
  249. "l32i %[REG], %[REG], 0\n"\
  250. : [APB]"=a"(apb), [REG]"+a"(reg)\
  251. : \
  252. : "memory" \
  253. );
  254. return reg;
  255. #endif
  256. }