dport_access.c 10 KB

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