periph_ctrl.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "hal/clk_gate_ll.h"
  8. #include "esp_attr.h"
  9. #include "esp_private/periph_ctrl.h"
  10. static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  11. static uint8_t ref_counts[PERIPH_MODULE_MAX] = {0};
  12. void periph_module_enable(periph_module_t periph)
  13. {
  14. assert(periph < PERIPH_MODULE_MAX);
  15. portENTER_CRITICAL_SAFE(&periph_spinlock);
  16. if (ref_counts[periph] == 0) {
  17. periph_ll_enable_clk_clear_rst(periph);
  18. }
  19. ref_counts[periph]++;
  20. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  21. }
  22. void periph_module_disable(periph_module_t periph)
  23. {
  24. assert(periph < PERIPH_MODULE_MAX);
  25. portENTER_CRITICAL_SAFE(&periph_spinlock);
  26. ref_counts[periph]--;
  27. if (ref_counts[periph] == 0) {
  28. periph_ll_disable_clk_set_rst(periph);
  29. }
  30. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  31. }
  32. void periph_module_reset(periph_module_t periph)
  33. {
  34. assert(periph < PERIPH_MODULE_MAX);
  35. portENTER_CRITICAL_SAFE(&periph_spinlock);
  36. periph_ll_reset(periph);
  37. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  38. }
  39. #if CONFIG_ESP32_WIFI_ENABLED
  40. IRAM_ATTR void wifi_bt_common_module_enable(void)
  41. {
  42. portENTER_CRITICAL_SAFE(&periph_spinlock);
  43. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  44. periph_ll_wifi_bt_module_enable_clk_clear_rst();
  45. }
  46. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]++;
  47. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  48. }
  49. IRAM_ATTR void wifi_bt_common_module_disable(void)
  50. {
  51. portENTER_CRITICAL_SAFE(&periph_spinlock);
  52. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]--;
  53. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  54. periph_ll_wifi_bt_module_disable_clk_set_rst();
  55. }
  56. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  57. }
  58. void wifi_module_enable(void)
  59. {
  60. portENTER_CRITICAL_SAFE(&periph_spinlock);
  61. periph_ll_wifi_module_enable_clk_clear_rst();
  62. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  63. }
  64. void wifi_module_disable(void)
  65. {
  66. portENTER_CRITICAL_SAFE(&periph_spinlock);
  67. periph_ll_wifi_module_disable_clk_set_rst();
  68. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  69. }
  70. #endif // CONFIG_ESP32_WIFI_ENABLED