periph_ctrl.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. IRAM_ATTR void wifi_bt_common_module_enable(void)
  40. {
  41. portENTER_CRITICAL_SAFE(&periph_spinlock);
  42. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  43. periph_ll_wifi_bt_module_enable_clk_clear_rst();
  44. }
  45. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]++;
  46. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  47. }
  48. IRAM_ATTR void wifi_bt_common_module_disable(void)
  49. {
  50. portENTER_CRITICAL_SAFE(&periph_spinlock);
  51. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]--;
  52. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  53. periph_ll_wifi_bt_module_disable_clk_set_rst();
  54. }
  55. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  56. }
  57. void wifi_module_enable(void)
  58. {
  59. periph_ll_wifi_module_enable_clk_clear_rst();
  60. }
  61. void wifi_module_disable(void)
  62. {
  63. periph_ll_wifi_module_disable_clk_set_rst();
  64. }