periph_ctrl.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2015-2020 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "freertos/FreeRTOS.h"
  15. #include "hal/clk_gate_ll.h"
  16. #include "esp_attr.h"
  17. #include "driver/periph_ctrl.h"
  18. static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  19. static uint8_t ref_counts[PERIPH_MODULE_MAX] = {0};
  20. void periph_module_enable(periph_module_t periph)
  21. {
  22. assert(periph < PERIPH_MODULE_MAX);
  23. portENTER_CRITICAL_SAFE(&periph_spinlock);
  24. if (ref_counts[periph] == 0) {
  25. periph_ll_enable_clk_clear_rst(periph);
  26. }
  27. ref_counts[periph]++;
  28. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  29. }
  30. void periph_module_disable(periph_module_t periph)
  31. {
  32. assert(periph < PERIPH_MODULE_MAX);
  33. portENTER_CRITICAL_SAFE(&periph_spinlock);
  34. ref_counts[periph]--;
  35. if (ref_counts[periph] == 0) {
  36. periph_ll_disable_clk_set_rst(periph);
  37. }
  38. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  39. }
  40. void periph_module_reset(periph_module_t periph)
  41. {
  42. assert(periph < PERIPH_MODULE_MAX);
  43. portENTER_CRITICAL_SAFE(&periph_spinlock);
  44. periph_ll_reset(periph);
  45. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  46. }
  47. IRAM_ATTR void wifi_bt_common_module_enable(void)
  48. {
  49. portENTER_CRITICAL_SAFE(&periph_spinlock);
  50. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  51. periph_ll_wifi_bt_module_enable_clk_clear_rst();
  52. }
  53. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]++;
  54. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  55. }
  56. IRAM_ATTR void wifi_bt_common_module_disable(void)
  57. {
  58. portENTER_CRITICAL_SAFE(&periph_spinlock);
  59. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]--;
  60. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  61. periph_ll_wifi_bt_module_disable_clk_set_rst();
  62. }
  63. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  64. }
  65. void wifi_module_enable(void)
  66. {
  67. periph_ll_wifi_module_enable_clk_clear_rst();
  68. }
  69. void wifi_module_disable(void)
  70. {
  71. periph_ll_wifi_module_disable_clk_set_rst();
  72. }