periph_ctrl.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "driver/periph_ctrl.h"
  17. static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  18. static uint8_t ref_counts[PERIPH_MODULE_MAX] = {0};
  19. void periph_module_enable(periph_module_t periph)
  20. {
  21. assert(periph < PERIPH_MODULE_MAX);
  22. portENTER_CRITICAL_SAFE(&periph_spinlock);
  23. if (ref_counts[periph] == 0) {
  24. periph_ll_enable_clk_clear_rst(periph);
  25. }
  26. ref_counts[periph]++;
  27. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  28. }
  29. void periph_module_disable(periph_module_t periph)
  30. {
  31. assert(periph < PERIPH_MODULE_MAX);
  32. portENTER_CRITICAL_SAFE(&periph_spinlock);
  33. ref_counts[periph]--;
  34. if (ref_counts[periph] == 0) {
  35. periph_ll_disable_clk_set_rst(periph);
  36. }
  37. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  38. }
  39. void periph_module_reset(periph_module_t periph)
  40. {
  41. assert(periph < PERIPH_MODULE_MAX);
  42. portENTER_CRITICAL_SAFE(&periph_spinlock);
  43. periph_ll_reset(periph);
  44. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  45. }
  46. IRAM_ATTR void wifi_bt_common_module_enable(void)
  47. {
  48. portENTER_CRITICAL_SAFE(&periph_spinlock);
  49. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  50. periph_ll_wifi_bt_module_enable_clk_clear_rst();
  51. }
  52. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]++;
  53. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  54. }
  55. IRAM_ATTR void wifi_bt_common_module_disable(void)
  56. {
  57. portENTER_CRITICAL_SAFE(&periph_spinlock);
  58. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]--;
  59. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  60. periph_ll_wifi_bt_module_disable_clk_set_rst();
  61. }
  62. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  63. }