periph_ctrl.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "driver/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. periph_ll_wifi_module_enable_clk_clear_rst();
  61. }
  62. void wifi_module_disable(void)
  63. {
  64. periph_ll_wifi_module_disable_clk_set_rst();
  65. }
  66. #endif // CONFIG_ESP32_WIFI_ENABLED