ledc_hal.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // The HAL layer for LEDC (common part)
  14. #include "esp_attr.h"
  15. #include "hal/ledc_hal.h"
  16. void ledc_hal_init(ledc_hal_context_t *hal, ledc_mode_t speed_mode)
  17. {
  18. //Get hardware instance.
  19. hal->dev = LEDC_LL_GET_HW();
  20. hal->speed_mode = speed_mode;
  21. }
  22. void ledc_hal_get_clk_cfg(ledc_hal_context_t *hal, ledc_timer_t timer_sel, ledc_clk_cfg_t *clk_cfg)
  23. {
  24. ledc_clk_src_t clk_src = LEDC_APB_CLK;
  25. ledc_hal_get_clock_source(hal, timer_sel, &clk_src);
  26. if (clk_src == LEDC_REF_TICK) {
  27. *clk_cfg = LEDC_USE_REF_TICK;
  28. } else {
  29. *clk_cfg = LEDC_USE_APB_CLK;
  30. if (hal->speed_mode == LEDC_LOW_SPEED_MODE) {
  31. ledc_slow_clk_sel_t slow_clk = LEDC_SLOW_CLK_APB;
  32. ledc_hal_get_slow_clk_sel(hal, &slow_clk);
  33. if (slow_clk == LEDC_SLOW_CLK_RTC8M) {
  34. *clk_cfg = LEDC_USE_RTC8M_CLK;
  35. #if SOC_LEDC_SUPPORT_XTAL_CLOCK
  36. } else if (slow_clk == LEDC_SLOW_CLK_XTAL) {
  37. *clk_cfg = LEDC_USE_XTAL_CLK;
  38. #endif
  39. }
  40. }
  41. }
  42. }
  43. void ledc_hal_set_slow_clk(ledc_hal_context_t *hal, ledc_clk_cfg_t clk_cfg)
  44. {
  45. // 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.
  46. ledc_slow_clk_sel_t slow_clk_sel = LEDC_SLOW_CLK_APB;
  47. #if SOC_LEDC_SUPPORT_XTAL_CLOCK
  48. slow_clk_sel = (clk_cfg == LEDC_USE_RTC8M_CLK) ? LEDC_SLOW_CLK_RTC8M :
  49. ((clk_cfg == LEDC_USE_XTAL_CLK) ? LEDC_SLOW_CLK_XTAL : LEDC_SLOW_CLK_APB);
  50. #else
  51. slow_clk_sel = (clk_cfg == LEDC_USE_RTC8M_CLK) ? LEDC_SLOW_CLK_RTC8M : LEDC_SLOW_CLK_APB;
  52. #endif
  53. ledc_hal_set_slow_clk_sel(hal, slow_clk_sel);
  54. }