periph_ctrl.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2015-2016 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. #include <esp_types.h>
  14. #include "esp_intr.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/semphr.h"
  17. #include "freertos/xtensa_api.h"
  18. #include "soc/dport_reg.h"
  19. #include "driver/periph_ctrl.h"
  20. static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  21. /* Static functions to return register address & mask for clk_en / rst of each peripheral */
  22. static uint32_t get_clk_en_mask(periph_module_t periph);
  23. static uint32_t get_rst_en_mask(periph_module_t periph);
  24. static uint32_t get_clk_en_reg(periph_module_t periph);
  25. static uint32_t get_rst_en_reg(periph_module_t periph);
  26. void periph_module_enable(periph_module_t periph)
  27. {
  28. portENTER_CRITICAL(&periph_spinlock);
  29. DPORT_SET_PERI_REG_MASK(get_clk_en_reg(periph), get_clk_en_mask(periph));
  30. DPORT_CLEAR_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph));
  31. portEXIT_CRITICAL(&periph_spinlock);
  32. }
  33. void periph_module_disable(periph_module_t periph)
  34. {
  35. portENTER_CRITICAL(&periph_spinlock);
  36. DPORT_CLEAR_PERI_REG_MASK(get_clk_en_reg(periph), get_clk_en_mask(periph));
  37. DPORT_SET_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph));
  38. portEXIT_CRITICAL(&periph_spinlock);
  39. }
  40. void periph_module_reset(periph_module_t periph)
  41. {
  42. portENTER_CRITICAL(&periph_spinlock);
  43. DPORT_SET_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph));
  44. DPORT_CLEAR_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph));
  45. portEXIT_CRITICAL(&periph_spinlock);
  46. }
  47. static uint32_t get_clk_en_mask(periph_module_t periph)
  48. {
  49. switch(periph) {
  50. case PERIPH_RMT_MODULE:
  51. return DPORT_RMT_CLK_EN;
  52. case PERIPH_LEDC_MODULE:
  53. return DPORT_LEDC_CLK_EN;
  54. case PERIPH_UART0_MODULE:
  55. return DPORT_UART_CLK_EN;
  56. case PERIPH_UART1_MODULE:
  57. return DPORT_UART1_CLK_EN;
  58. case PERIPH_UART2_MODULE:
  59. return DPORT_UART2_CLK_EN;
  60. case PERIPH_I2C0_MODULE:
  61. return DPORT_I2C_EXT0_CLK_EN;
  62. case PERIPH_I2C1_MODULE:
  63. return DPORT_I2C_EXT1_CLK_EN;
  64. case PERIPH_I2S0_MODULE:
  65. return DPORT_I2S0_CLK_EN;
  66. case PERIPH_I2S1_MODULE:
  67. return DPORT_I2S1_CLK_EN;
  68. case PERIPH_TIMG0_MODULE:
  69. return DPORT_TIMERGROUP_CLK_EN;
  70. case PERIPH_TIMG1_MODULE:
  71. return DPORT_TIMERGROUP1_CLK_EN;
  72. case PERIPH_PWM0_MODULE:
  73. return DPORT_PWM0_CLK_EN;
  74. case PERIPH_PWM1_MODULE:
  75. return DPORT_PWM1_CLK_EN;
  76. case PERIPH_PWM2_MODULE:
  77. return DPORT_PWM2_CLK_EN;
  78. case PERIPH_PWM3_MODULE:
  79. return DPORT_PWM3_CLK_EN;
  80. case PERIPH_UHCI0_MODULE:
  81. return DPORT_UHCI0_CLK_EN;
  82. case PERIPH_UHCI1_MODULE:
  83. return DPORT_UHCI1_CLK_EN;
  84. case PERIPH_PCNT_MODULE:
  85. return DPORT_PCNT_CLK_EN;
  86. case PERIPH_SPI_MODULE:
  87. return DPORT_SPI01_CLK_EN;
  88. case PERIPH_HSPI_MODULE:
  89. return DPORT_SPI2_CLK_EN;
  90. case PERIPH_VSPI_MODULE:
  91. return DPORT_SPI3_CLK_EN;
  92. case PERIPH_SPI_DMA_MODULE:
  93. return DPORT_SPI_DMA_CLK_EN;
  94. case PERIPH_SDMMC_MODULE:
  95. return DPORT_WIFI_CLK_SDIO_HOST_EN;
  96. case PERIPH_SDIO_SLAVE_MODULE:
  97. return DPORT_WIFI_CLK_SDIOSLAVE_EN;
  98. case PERIPH_CAN_MODULE:
  99. return DPORT_CAN_CLK_EN;
  100. case PERIPH_EMAC_MODULE:
  101. return DPORT_WIFI_CLK_EMAC_EN;
  102. case PERIPH_RNG_MODULE:
  103. return DPORT_WIFI_CLK_RNG_EN;
  104. case PERIPH_WIFI_MODULE:
  105. return DPORT_WIFI_CLK_WIFI_EN_M;
  106. case PERIPH_BT_MODULE:
  107. return DPORT_WIFI_CLK_BT_EN_M;
  108. case PERIPH_WIFI_BT_COMMON_MODULE:
  109. return DPORT_WIFI_CLK_WIFI_BT_COMMON_M;
  110. case PERIPH_BT_BASEBAND_MODULE:
  111. return DPORT_BT_BASEBAND_EN;
  112. case PERIPH_BT_LC_MODULE:
  113. return DPORT_BT_LC_EN;
  114. default:
  115. return 0;
  116. }
  117. }
  118. static uint32_t get_rst_en_mask(periph_module_t periph)
  119. {
  120. switch(periph) {
  121. case PERIPH_RMT_MODULE:
  122. return DPORT_RMT_RST;
  123. case PERIPH_LEDC_MODULE:
  124. return DPORT_LEDC_RST;
  125. case PERIPH_UART0_MODULE:
  126. return DPORT_UART_RST;
  127. case PERIPH_UART1_MODULE:
  128. return DPORT_UART1_RST;
  129. case PERIPH_UART2_MODULE:
  130. return DPORT_UART2_RST;
  131. case PERIPH_I2C0_MODULE:
  132. return DPORT_I2C_EXT0_RST;
  133. case PERIPH_I2C1_MODULE:
  134. return DPORT_I2C_EXT1_RST;
  135. case PERIPH_I2S0_MODULE:
  136. return DPORT_I2S0_RST;
  137. case PERIPH_I2S1_MODULE:
  138. return DPORT_I2S1_RST;
  139. case PERIPH_TIMG0_MODULE:
  140. return DPORT_TIMERGROUP_RST;
  141. case PERIPH_TIMG1_MODULE:
  142. return DPORT_TIMERGROUP1_RST;
  143. case PERIPH_PWM0_MODULE:
  144. return DPORT_PWM0_RST;
  145. case PERIPH_PWM1_MODULE:
  146. return DPORT_PWM1_RST;
  147. case PERIPH_PWM2_MODULE:
  148. return DPORT_PWM2_RST;
  149. case PERIPH_PWM3_MODULE:
  150. return DPORT_PWM3_RST;
  151. case PERIPH_UHCI0_MODULE:
  152. return DPORT_UHCI0_RST;
  153. case PERIPH_UHCI1_MODULE:
  154. return DPORT_UHCI1_RST;
  155. case PERIPH_PCNT_MODULE:
  156. return DPORT_PCNT_RST;
  157. case PERIPH_SPI_MODULE:
  158. return DPORT_SPI01_RST;
  159. case PERIPH_HSPI_MODULE:
  160. return DPORT_SPI2_RST;
  161. case PERIPH_VSPI_MODULE:
  162. return DPORT_SPI3_RST;
  163. case PERIPH_SPI_DMA_MODULE:
  164. return DPORT_SPI_DMA_RST;
  165. case PERIPH_SDMMC_MODULE:
  166. return DPORT_SDIO_HOST_RST;
  167. case PERIPH_SDIO_SLAVE_MODULE:
  168. return DPORT_SDIO_RST;
  169. case PERIPH_CAN_MODULE:
  170. return DPORT_CAN_RST;
  171. case PERIPH_EMAC_MODULE:
  172. return DPORT_EMAC_RST;
  173. case PERIPH_WIFI_MODULE:
  174. case PERIPH_BT_MODULE:
  175. case PERIPH_WIFI_BT_COMMON_MODULE:
  176. case PERIPH_BT_BASEBAND_MODULE:
  177. case PERIPH_BT_LC_MODULE:
  178. return 0;
  179. default:
  180. return 0;
  181. }
  182. }
  183. static bool is_wifi_clk_peripheral(periph_module_t periph)
  184. {
  185. /* A small subset of peripherals use WIFI_CLK_EN_REG and
  186. CORE_RST_EN_REG for their clock & reset registers */
  187. switch(periph) {
  188. case PERIPH_SDMMC_MODULE:
  189. case PERIPH_SDIO_SLAVE_MODULE:
  190. case PERIPH_EMAC_MODULE:
  191. case PERIPH_RNG_MODULE:
  192. case PERIPH_WIFI_MODULE:
  193. case PERIPH_BT_MODULE:
  194. case PERIPH_WIFI_BT_COMMON_MODULE:
  195. case PERIPH_BT_BASEBAND_MODULE:
  196. case PERIPH_BT_LC_MODULE:
  197. return true;
  198. default:
  199. return false;
  200. }
  201. }
  202. static uint32_t get_clk_en_reg(periph_module_t periph)
  203. {
  204. return is_wifi_clk_peripheral(periph) ? DPORT_WIFI_CLK_EN_REG : DPORT_PERIP_CLK_EN_REG;
  205. }
  206. static uint32_t get_rst_en_reg(periph_module_t periph)
  207. {
  208. return is_wifi_clk_peripheral(periph) ? DPORT_CORE_RST_EN_REG : DPORT_PERIP_RST_EN_REG;
  209. }