lp_periph_ctrl.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "esp_private/lp_periph_ctrl.h"
  8. #include "hal/lp_periph_clk_ctrl_ll.h"
  9. static portMUX_TYPE lp_periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  10. static uint8_t ref_counts[LP_PERIPH_MODULE_MAX] = {0};
  11. void lp_periph_module_enable(lp_periph_module_t lp_periph)
  12. {
  13. portENTER_CRITICAL(&lp_periph_spinlock);
  14. if (ref_counts[lp_periph] == 0) {
  15. // Enable clock and clear reset
  16. lp_periph_ll_enable_clk_clear_rst(lp_periph);
  17. }
  18. ref_counts[lp_periph]++;
  19. portEXIT_CRITICAL(&lp_periph_spinlock);
  20. }
  21. void lp_periph_module_disable(lp_periph_module_t lp_periph)
  22. {
  23. portENTER_CRITICAL(&lp_periph_spinlock);
  24. ref_counts[lp_periph]--;
  25. if (ref_counts[lp_periph] == 0) {
  26. // Disable clock and set reset
  27. lp_periph_ll_disable_clk_set_rst(lp_periph);
  28. }
  29. portEXIT_CRITICAL(&lp_periph_spinlock);
  30. }
  31. void lp_periph_module_reset(lp_periph_module_t lp_periph)
  32. {
  33. portENTER_CRITICAL(&lp_periph_spinlock);
  34. lp_periph_ll_reset(lp_periph);
  35. portEXIT_CRITICAL(&lp_periph_spinlock);
  36. }
  37. void lp_periph_set_clk_src(lp_periph_module_t lp_periph, soc_module_clk_t clk_src)
  38. {
  39. portENTER_CRITICAL(&lp_periph_spinlock);
  40. lp_periph_ll_set_clk_src(lp_periph, clk_src);
  41. portEXIT_CRITICAL(&lp_periph_spinlock);
  42. }