ledc_hal.c 2.6 KB

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