pwm-esp32.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * esp32 / esp-idf pwm
  3. *
  4. * Copyright (C) 2019 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. #include "soc/ledc_reg.h"
  26. #include "driver/ledc.h"
  27. static const ledc_timer_config_t tc = {
  28. .speed_mode = LEDC_HIGH_SPEED_MODE,
  29. .duty_resolution = LEDC_TIMER_13_BIT,
  30. .timer_num = LEDC_TIMER_0,
  31. .freq_hz = 5000,
  32. .clk_cfg = LEDC_AUTO_CLK
  33. };
  34. int
  35. lws_pwm_plat_init(const struct lws_pwm_ops *lo)
  36. {
  37. ledc_channel_config_t lc = {
  38. .duty = 8191,
  39. .intr_type = LEDC_INTR_FADE_END,
  40. .speed_mode = LEDC_HIGH_SPEED_MODE,
  41. .timer_sel = LEDC_TIMER_0,
  42. };
  43. size_t n;
  44. ledc_timer_config(&tc);
  45. for (n = 0; n < lo->count_pwm_map; n++) {
  46. lc.channel = LEDC_CHANNEL_0 + lo->pwm_map[n].index;
  47. lc.gpio_num = lo->pwm_map[n].gpio;
  48. ledc_channel_config(&lc);
  49. ledc_set_duty(LEDC_HIGH_SPEED_MODE, lc.channel, 0);
  50. ledc_update_duty(LEDC_HIGH_SPEED_MODE, lc.channel);
  51. }
  52. return 0;
  53. }
  54. void
  55. lws_pwm_plat_intensity(const struct lws_pwm_ops *lo, _lws_plat_gpio_t gpio,
  56. lws_led_intensity_t inten)
  57. {
  58. size_t n;
  59. for (n = 0; n < lo->count_pwm_map; n++) {
  60. if (lo->pwm_map[n].gpio == gpio) {
  61. if (!lo->pwm_map[n].active_level)
  62. inten = 65535 - inten;
  63. ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0 +
  64. lo->pwm_map[n].index, inten >> 3);
  65. ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0 +
  66. lo->pwm_map[n].index);
  67. return;
  68. }
  69. }
  70. lwsl_err("%s: unknown gpio for pwm\n", __func__);
  71. }