drv_pwm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-26 ChuShicheng first version
  9. */
  10. #include <rtdevice.h>
  11. #include "drv_pwm.h"
  12. #include "driver/ledc.h"
  13. #ifdef BSP_USING_PWM
  14. #define DBG_LEVEL DBG_LOG
  15. #include <rtdbg.h>
  16. #define LOG_TAG "DRV.PWM"
  17. #define LEDC_MODE LEDC_LOW_SPEED_MODE
  18. #define LEDC_DUTY_DEFAULT (4095) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095
  19. #define LEDC_FREQUENCY_DEFAULT (5000) // Frequency in Hertz. Set frequency at 5 kHz
  20. struct esp_pwm
  21. {
  22. struct rt_device_pwm pwm_device;
  23. rt_uint8_t timer_num;
  24. char *name;
  25. };
  26. static struct esp_pwm esp_pwm_obj[] =
  27. {
  28. #ifdef BSP_USING_PWM0
  29. {
  30. .name = "pwm0",
  31. .timer_num = 0,
  32. }
  33. #endif /* BSP_USING_PWM0 */
  34. };
  35. static rt_err_t _pwm_set(struct esp_pwm *obj, struct rt_pwm_configuration *configuration)
  36. {
  37. float period = configuration->period;
  38. float pulse = configuration->pulse;
  39. float duty = 8191 * (pulse / period); // ((2 ** 13) - 1) * (pulse / period_hz)
  40. float period_hz = 1000000000 / configuration->period;
  41. ledc_timer_config_t ledc_timer = {
  42. .speed_mode = LEDC_MODE,
  43. .timer_num = obj->timer_num,
  44. .duty_resolution = LEDC_TIMER_13_BIT,
  45. .freq_hz = (uint32_t)period_hz, // Set output frequency at 5 kHz
  46. .clk_cfg = LEDC_AUTO_CLK
  47. };
  48. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  49. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, configuration->channel, (uint32_t)duty));
  50. // Update duty to apply the new value
  51. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, configuration->channel));
  52. return RT_EOK;
  53. }
  54. static rt_err_t _pwm_get(struct esp_pwm *obj, struct rt_pwm_configuration *configuration)
  55. {
  56. uint32_t period_hz = ledc_get_freq(LEDC_MODE, obj->timer_num);
  57. LOG_W("period_hz:%d", period_hz);
  58. uint32_t duty = ledc_get_duty(LEDC_MODE, configuration->channel);
  59. LOG_W("duty:%d", duty);
  60. configuration->period = 1000000000 / period_hz;
  61. configuration->pulse = duty * configuration->period / 8191;
  62. LOG_W("pulse:%d", configuration->pulse);
  63. return RT_EOK;
  64. }
  65. static void pwm_enabled(struct esp_pwm *obj, struct rt_pwm_configuration *configuration, rt_uint8_t enable)
  66. {
  67. if(enable)
  68. {
  69. ledc_timer_config_t ledc_timer = {
  70. .speed_mode = LEDC_MODE,
  71. .timer_num = obj->timer_num,
  72. .duty_resolution = LEDC_TIMER_13_BIT,
  73. .freq_hz = LEDC_FREQUENCY_DEFAULT, // Set output frequency at 5 kHz
  74. .clk_cfg = LEDC_AUTO_CLK
  75. };
  76. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  77. // Prepare and then apply the LEDC PWM channel configuration
  78. ledc_channel_config_t ledc_channel = {
  79. .speed_mode = LEDC_MODE,
  80. .channel = configuration->channel,
  81. .timer_sel = obj->timer_num,
  82. .intr_type = LEDC_INTR_DISABLE,
  83. .duty = LEDC_DUTY_DEFAULT, // ((2 ** 13) - 1) * (pulse / period_hz)
  84. .hpoint = 0
  85. };
  86. switch (configuration->channel)
  87. {
  88. case LEDC_CHANNEL_0:
  89. ledc_channel.gpio_num = BSP_LEDC_CH0_GPIO;
  90. break;
  91. case LEDC_CHANNEL_1:
  92. ledc_channel.gpio_num = BSP_LEDC_CH1_GPIO;
  93. break;
  94. case LEDC_CHANNEL_2:
  95. ledc_channel.gpio_num = BSP_LEDC_CH2_GPIO;
  96. break;
  97. case LEDC_CHANNEL_3:
  98. ledc_channel.gpio_num = BSP_LEDC_CH3_GPIO;
  99. break;
  100. case LEDC_CHANNEL_4:
  101. ledc_channel.gpio_num = BSP_LEDC_CH4_GPIO;
  102. break;
  103. case LEDC_CHANNEL_5:
  104. ledc_channel.gpio_num = BSP_LEDC_CH5_GPIO;
  105. break;
  106. default:
  107. break;
  108. }
  109. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  110. }
  111. else
  112. {
  113. ESP_ERROR_CHECK(ledc_stop(LEDC_MODE, configuration->channel, 0));
  114. }
  115. }
  116. static rt_err_t _pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  117. {
  118. struct esp_pwm *_pwm = rt_container_of(device, struct esp_pwm, pwm_device);
  119. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
  120. switch (cmd)
  121. {
  122. case PWM_CMD_ENABLE:
  123. pwm_enabled(_pwm, configuration, RT_TRUE);
  124. return RT_EOK;
  125. case PWM_CMD_DISABLE:
  126. pwm_enabled(_pwm, configuration, RT_FALSE);
  127. return RT_EOK;
  128. case PWM_CMD_SET:
  129. return _pwm_set(_pwm, configuration);
  130. case PWM_CMD_GET:
  131. return _pwm_get(_pwm, configuration);
  132. default:
  133. return -RT_EINVAL;
  134. }
  135. }
  136. static struct rt_pwm_ops _pwm_ops =
  137. {
  138. _pwm_control
  139. };
  140. int rt_hw_pwm_init(void)
  141. {
  142. int result = RT_EOK;
  143. for (int i = 0; i < sizeof(esp_pwm_obj) / sizeof(esp_pwm_obj[0]); i++)
  144. {
  145. esp_pwm_obj[i].timer_num = i;
  146. result = rt_device_pwm_register(&esp_pwm_obj[i].pwm_device, esp_pwm_obj[i].name, &_pwm_ops, 0);
  147. if(result != RT_EOK)
  148. {
  149. LOG_E("%s register fail.", esp_pwm_obj[i].name);
  150. }
  151. }
  152. return result;
  153. }
  154. INIT_DEVICE_EXPORT(rt_hw_pwm_init);
  155. #endif /* BSP_USING_PWM */