ledc_hal.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // The HAL layer for LEDC (common part)
  7. #include "esp_attr.h"
  8. #include "hal/ledc_hal.h"
  9. #include "soc/soc_caps.h"
  10. #include "sdkconfig.h"
  11. #include "hal/assert.h"
  12. void ledc_hal_init(ledc_hal_context_t *hal, ledc_mode_t speed_mode)
  13. {
  14. //Get hardware instance.
  15. hal->dev = LEDC_LL_GET_HW();
  16. hal->speed_mode = speed_mode;
  17. }
  18. void ledc_hal_get_clk_cfg(ledc_hal_context_t *hal, ledc_timer_t timer_sel, ledc_clk_cfg_t *clk_cfg)
  19. {
  20. /* Use the following variable to retrieve the clock source used by the LEDC
  21. * hardware controller. */
  22. ledc_clk_src_t clk_src;
  23. /* Clock configuration to return to the driver. */
  24. ledc_clk_cfg_t driver_clk = LEDC_AUTO_CLK;
  25. /* Get the timer-specific mux value. */
  26. ledc_hal_get_clock_source(hal, timer_sel, &clk_src);
  27. #if SOC_LEDC_SUPPORT_REF_TICK
  28. if (clk_src == LEDC_REF_TICK) {
  29. driver_clk = LEDC_USE_REF_TICK;
  30. } else
  31. #endif
  32. {
  33. /* If the timer-specific mux is not set to REF_TICK, it either means that:
  34. * - The controler is in fast mode, and thus using APB clock (driver_clk
  35. * variable's default value)
  36. * - The controler is in slow mode and so, using a global clock,
  37. * so we have to retrieve that clock here.
  38. */
  39. if (hal->speed_mode == LEDC_LOW_SPEED_MODE) {
  40. /* If the source clock used by LEDC hardware is not REF_TICK, it is
  41. * necessary to retrieve the global clock source used. */
  42. ledc_slow_clk_sel_t slow_clk;
  43. ledc_hal_get_slow_clk_sel(hal, &slow_clk);
  44. driver_clk = (ledc_clk_cfg_t)slow_clk;
  45. }
  46. #if SOC_LEDC_SUPPORT_HS_MODE
  47. else {
  48. driver_clk = LEDC_USE_APB_CLK;
  49. }
  50. #endif
  51. }
  52. *clk_cfg = driver_clk;
  53. }