ledc_hal.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "hal/assert.h"
  11. void ledc_hal_init(ledc_hal_context_t *hal, ledc_mode_t speed_mode)
  12. {
  13. //Get hardware instance.
  14. hal->dev = LEDC_LL_GET_HW();
  15. hal->speed_mode = speed_mode;
  16. }
  17. static inline ledc_clk_cfg_t ledc_hal_get_slow_clock_helper(ledc_hal_context_t *hal)
  18. {
  19. ledc_slow_clk_sel_t slow_clk;
  20. ledc_hal_get_slow_clk_sel(hal, &slow_clk);
  21. switch (slow_clk) {
  22. #if SOC_LEDC_SUPPORT_APB_CLOCK
  23. case LEDC_SLOW_CLK_APB:
  24. return LEDC_USE_APB_CLK;
  25. #endif
  26. #if SOC_LEDC_SUPPORT_PLL_DIV_CLOCK
  27. case LEDC_SLOW_CLK_PLL_DIV:
  28. return LEDC_USE_PLL_DIV_CLK;
  29. #endif
  30. case LEDC_SLOW_CLK_RTC8M:
  31. return LEDC_USE_RTC8M_CLK;
  32. #if SOC_LEDC_SUPPORT_XTAL_CLOCK
  33. case LEDC_SLOW_CLK_XTAL:
  34. return LEDC_USE_XTAL_CLK;
  35. #endif
  36. default:
  37. // Should never reach here
  38. HAL_ASSERT(false && "invalid slow clock source");
  39. return LEDC_AUTO_CLK;
  40. }
  41. }
  42. void ledc_hal_get_clk_cfg(ledc_hal_context_t *hal, ledc_timer_t timer_sel, ledc_clk_cfg_t *clk_cfg)
  43. {
  44. /* Use the following variable to retrieve the clock source used by the LEDC
  45. * hardware controller. */
  46. ledc_clk_src_t clk_src;
  47. /* Clock configuration to return to the driver. */
  48. ledc_clk_cfg_t driver_clk = LEDC_AUTO_CLK;
  49. /* Get the timer-specific mux value. */
  50. ledc_hal_get_clock_source(hal, timer_sel, &clk_src);
  51. #if SOC_LEDC_SUPPORT_REF_TICK
  52. if (clk_src == LEDC_REF_TICK) {
  53. driver_clk = LEDC_USE_REF_TICK;
  54. } else
  55. #endif
  56. {
  57. /* If the timer-specific mux is not set to REF_TICK, it either means that:
  58. * - The controler is in fast mode, and thus using APB clock (driver_clk
  59. * variable's default value)
  60. * - The controler is in slow mode and so, using a global clock,
  61. * so we have to retrieve that clock here.
  62. */
  63. if (hal->speed_mode == LEDC_LOW_SPEED_MODE) {
  64. /* If the source clock used by LEDC hardware is not REF_TICK, it is
  65. * necessary to retrieve the global clock source used. */
  66. driver_clk = ledc_hal_get_slow_clock_helper(hal);
  67. }
  68. #if SOC_LEDC_SUPPORT_HS_MODE
  69. else {
  70. driver_clk = LEDC_USE_APB_CLK;
  71. }
  72. #endif
  73. }
  74. *clk_cfg = driver_clk;
  75. }