drv_pwm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024/02/19 flyingcys first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "drv_pwm.h"
  13. #ifdef BSP_USING_PWM
  14. #define DBG_LEVEL DBG_LOG
  15. #include <rtdbg.h>
  16. #define LOG_TAG "DRV.PWM"
  17. struct cvi_pwm_dev
  18. {
  19. struct rt_device_pwm device;
  20. const char *name;
  21. rt_ubase_t reg_base;
  22. };
  23. static const uint64_t count_unit = 100000000; // 100M count per second
  24. static const uint64_t NSEC_COUNT = 1000000000; // ns
  25. static void cvi_pwm_set_config(rt_ubase_t reg_base, struct rt_pwm_configuration *cfg)
  26. {
  27. unsigned long long duty_clk, period_clk;
  28. cvi_pwm_set_polarity_high_ch(reg_base, (cfg->channel & PWM_MAX_CH));
  29. duty_clk = (cfg->pulse * count_unit) / NSEC_COUNT;
  30. cvi_pwm_set_high_period_ch(reg_base, (cfg->channel & PWM_MAX_CH), duty_clk);
  31. period_clk = (cfg->period * count_unit) / NSEC_COUNT;
  32. cvi_pwm_set_period_ch(reg_base, (cfg->channel & PWM_MAX_CH), period_clk);
  33. cvi_pwm_output_en_ch(reg_base, cfg->channel & PWM_MAX_CH);
  34. }
  35. static void cvi_pwm_get_config(rt_ubase_t reg_base, struct rt_pwm_configuration *cfg)
  36. {
  37. unsigned long long duty_clk, period_clk;
  38. duty_clk = cvi_pwm_get_high_period_ch(reg_base, (cfg->channel & PWM_MAX_CH));
  39. cfg->pulse = duty_clk * NSEC_COUNT / count_unit;
  40. period_clk = cvi_pwm_get_period_ch(reg_base, (cfg->channel & PWM_MAX_CH));
  41. cfg->period = period_clk * NSEC_COUNT / count_unit;
  42. }
  43. static rt_err_t _pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  44. {
  45. struct rt_pwm_configuration *cfg = (struct rt_pwm_configuration *)arg;
  46. struct cvi_pwm_dev *pwm_dev = (struct cvi_pwm_dev *)device->parent.user_data;
  47. unsigned long long duty_clk, period_clk;
  48. const uint64_t count_unit = 100000000; // 100M count per second
  49. const uint64_t NSEC_COUNT = 1000000000; // ns
  50. if (cfg->channel > PWM_MAX_CH)
  51. return -RT_EINVAL;
  52. switch (cmd)
  53. {
  54. case PWM_CMD_ENABLE:
  55. cvi_pwm_start_en_ch(pwm_dev->reg_base, cfg->channel & PWM_MAX_CH);
  56. break;
  57. case PWM_CMD_DISABLE:
  58. cvi_pwm_start_dis_ch(pwm_dev->reg_base, cfg->channel & PWM_MAX_CH);
  59. break;
  60. case PWM_CMD_SET:
  61. cvi_pwm_set_config(pwm_dev->reg_base, cfg);
  62. break;
  63. case PWM_CMD_GET:
  64. cvi_pwm_get_config(pwm_dev->reg_base, cfg);
  65. break;
  66. case PWM_CMD_SET_PERIOD:
  67. period_clk = (cfg->period * count_unit) / NSEC_COUNT;
  68. cvi_pwm_set_period_ch(pwm_dev->reg_base, (cfg->channel & PWM_MAX_CH), period_clk);
  69. break;
  70. case PWM_CMD_SET_PULSE:
  71. duty_clk = (cfg->pulse * count_unit) / NSEC_COUNT;
  72. cvi_pwm_set_high_period_ch(pwm_dev->reg_base, (cfg->channel & PWM_MAX_CH), duty_clk);
  73. break;
  74. default:
  75. LOG_D("cmd: %x channel: %d period: %d pulse: %d", cmd, cfg->channel, cfg->period, cfg->pulse);
  76. break;
  77. }
  78. return RT_EOK;
  79. }
  80. const static struct rt_pwm_ops cvi_pwm_ops =
  81. {
  82. .control = &_pwm_control
  83. };
  84. static struct cvi_pwm_dev cvi_pwm[] =
  85. {
  86. #ifdef BSP_USING_PWM0
  87. {
  88. .name = "pwm0",
  89. .reg_base = CVI_PWM0_BASE,
  90. },
  91. #endif
  92. #ifdef BSP_USING_PWM1
  93. {
  94. .name = "pwm1",
  95. .reg_base = CVI_PWM1_BASE,
  96. },
  97. #endif
  98. #ifdef BSP_USING_PWM2
  99. {
  100. .name = "pwm2",
  101. .reg_base = CVI_PWM2_BASE,
  102. },
  103. #endif
  104. #ifdef BSP_USING_PWM3
  105. {
  106. .name = "pwm3",
  107. .reg_base = CVI_PWM3_BASE,
  108. },
  109. #endif
  110. };
  111. int rt_hw_pwm_init(void)
  112. {
  113. int result = RT_EOK;
  114. uint8_t i;
  115. for (i = 0; i < sizeof(cvi_pwm) / sizeof(cvi_pwm[0]); i++)
  116. {
  117. result = rt_device_pwm_register(&cvi_pwm[i].device, cvi_pwm[i].name, &cvi_pwm_ops, &cvi_pwm[i]);
  118. if (result != RT_EOK)
  119. {
  120. LOG_E("device %s register failed", cvi_pwm[i].name);
  121. return -RT_ERROR;
  122. }
  123. }
  124. return RT_EOK;
  125. #if 0
  126. #ifdef BSP_USING_PWM0
  127. static struct cvi_pwm_dev cvi_pwm0;
  128. cvi_pwm0.name = "pwm0";
  129. cvi_pwm0.reg_base = CVI_PWM0_BASE;
  130. result = rt_device_pwm_register(&cvi_pwm0.device, cvi_pwm0.name, &cvi_pwm_ops, &cvi_pwm0);
  131. if (result != RT_EOK)
  132. {
  133. LOG_E("device %s register failed.", cvi_pwm0.name);
  134. return result;
  135. }
  136. #endif
  137. #ifdef BSP_USING_PWM1
  138. static struct cvi_pwm_dev cvi_pwm1;
  139. cvi_pwm1.name = "pwm1";
  140. cvi_pwm1.reg_base = CVI_PWM1_BASE;
  141. result = rt_device_pwm_register(&cvi_pwm1.device, cvi_pwm1.name, &cvi_pwm_ops, &cvi_pwm1);
  142. if (result != RT_EOK)
  143. {
  144. LOG_E("device %s register failed.", cvi_pwm1.name);
  145. return result;
  146. }
  147. #endif
  148. #ifdef BSP_USING_PWM2
  149. static struct cvi_pwm_dev cvi_pwm2;
  150. cvi_pwm2.name = "pwm2";
  151. cvi_pwm2.reg_base = CVI_PWM2_BASE;
  152. result = rt_device_pwm_register(&cvi_pwm2.device, cvi_pwm2.name, &cvi_pwm_ops, &cvi_pwm2);
  153. if (result != RT_EOK)
  154. {
  155. LOG_E("device %s register failed.", cvi_pwm2.name);
  156. return result;
  157. }
  158. #endif
  159. #ifdef BSP_USING_PWM3
  160. static struct cvi_pwm_dev cvi_pwm3;
  161. cvi_pwm3.name = "pwm3";
  162. cvi_pwm3.reg_base = CVI_PWM3_BASE;
  163. result = rt_device_pwm_register(&cvi_pwm3.device, cvi_pwm3.name, &cvi_pwm_ops, &cvi_pwm3);
  164. if (result != RT_EOK)
  165. {
  166. LOG_E("device %s register failed.", cvi_pwm3.name);
  167. return result;
  168. }
  169. #endif
  170. return RT_EOK;
  171. #endif
  172. }
  173. INIT_BOARD_EXPORT(rt_hw_pwm_init);
  174. #endif /* BSP_USING_PWM */