ledc_hal.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "soc/soc_caps.h"
  17. void ledc_hal_init(ledc_hal_context_t *hal, ledc_mode_t speed_mode)
  18. {
  19. //Get hardware instance.
  20. hal->dev = LEDC_LL_GET_HW();
  21. hal->speed_mode = speed_mode;
  22. }
  23. void ledc_hal_get_clk_cfg(ledc_hal_context_t *hal, ledc_timer_t timer_sel, ledc_clk_cfg_t *clk_cfg)
  24. {
  25. ledc_clk_src_t clk_src = LEDC_APB_CLK;
  26. ledc_hal_get_clock_source(hal, timer_sel, &clk_src);
  27. if (clk_src == LEDC_REF_TICK) {
  28. *clk_cfg = LEDC_USE_REF_TICK;
  29. } else {
  30. *clk_cfg = LEDC_USE_APB_CLK;
  31. if (hal->speed_mode == LEDC_LOW_SPEED_MODE) {
  32. ledc_slow_clk_sel_t slow_clk = LEDC_SLOW_CLK_APB;
  33. ledc_hal_get_slow_clk_sel(hal, &slow_clk);
  34. if (slow_clk == LEDC_SLOW_CLK_RTC8M) {
  35. *clk_cfg = LEDC_USE_RTC8M_CLK;
  36. #if SOC_LEDC_SUPPORT_XTAL_CLOCK
  37. } else if (slow_clk == LEDC_SLOW_CLK_XTAL) {
  38. *clk_cfg = LEDC_USE_XTAL_CLK;
  39. #endif
  40. }
  41. }
  42. }
  43. }
  44. void ledc_hal_set_slow_clk(ledc_hal_context_t *hal, ledc_clk_cfg_t clk_cfg)
  45. {
  46. // 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.
  47. ledc_slow_clk_sel_t slow_clk_sel = LEDC_SLOW_CLK_APB;
  48. #if SOC_LEDC_SUPPORT_XTAL_CLOCK
  49. slow_clk_sel = (clk_cfg == LEDC_USE_RTC8M_CLK) ? LEDC_SLOW_CLK_RTC8M :
  50. ((clk_cfg == LEDC_USE_XTAL_CLK) ? LEDC_SLOW_CLK_XTAL : LEDC_SLOW_CLK_APB);
  51. #else
  52. slow_clk_sel = (clk_cfg == LEDC_USE_RTC8M_CLK) ? LEDC_SLOW_CLK_RTC8M : LEDC_SLOW_CLK_APB;
  53. #endif
  54. ledc_hal_set_slow_clk_sel(hal, slow_clk_sel);
  55. }