ledc_hal.c 2.5 KB

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