dev_pwm.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * 2018-05-07 aozima the first version
  9. * 2022-09-24 yuqi add phase and dead time configuration
  10. */
  11. #ifndef __DEV_PWM_H__
  12. #define __DEV_PWM_H__
  13. #include <rtthread.h>
  14. /**
  15. * @addtogroup Drivers RTTHREAD Driver
  16. * @defgroup PWM PWM
  17. *
  18. * @brief PWM driver api
  19. *
  20. * <b>Example</b>
  21. * @code {.c}
  22. * #include <rtthread.h>
  23. * #include <rtdevice.h>
  24. *
  25. * #define PWM_DEV_NAME "pwm3" // PWM设备名称
  26. * #define PWM_DEV_CHANNEL 4 // PWM通道
  27. *
  28. * struct rt_device_pwm *pwm_dev; // PWM设备句柄
  29. *
  30. * static int pwm_led_sample(int argc, char *argv[])
  31. * {
  32. * rt_uint32_t period, pulse, dir;
  33. *
  34. * period = 500000; // 周期为0.5ms,单位为纳秒ns
  35. * dir = 1; // PWM脉冲宽度值的增减方向
  36. * pulse = 0; // PWM脉冲宽度值,单位为纳秒ns
  37. *
  38. * // 查找设备
  39. * pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
  40. * if (pwm_dev == RT_NULL)
  41. * {
  42. * rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
  43. * return -RT_ERROR;
  44. * }
  45. *
  46. * // 设置PWM周期和脉冲宽度默认值
  47. * rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
  48. * // 使能设备
  49. * rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
  50. *
  51. * while (1)
  52. * {
  53. * rt_thread_mdelay(50);
  54. * if (dir)
  55. * {
  56. * pulse += 5000; // 从0值开始每次增加5000ns
  57. * }
  58. * else
  59. * {
  60. * pulse -= 5000; // 从最大值开始每次减少5000ns
  61. * }
  62. * if (pulse >= period)
  63. * {
  64. * dir = 0;
  65. * }
  66. * if (0 == pulse)
  67. * {
  68. * dir = 1;
  69. * }
  70. *
  71. * // 设置PWM周期和脉冲宽度
  72. * rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
  73. * }
  74. * }
  75. *
  76. * MSH_CMD_EXPORT(pwm_led_sample, pwm sample);
  77. * @endcode
  78. *
  79. * @ingroup Drivers
  80. */
  81. /*!
  82. * @addtogroup PWM
  83. * @{
  84. */
  85. #define PWM_CMD_ENABLE (RT_DEVICE_CTRL_BASE(PWM) + 0)
  86. #define PWM_CMD_DISABLE (RT_DEVICE_CTRL_BASE(PWM) + 1)
  87. #define PWM_CMD_SET (RT_DEVICE_CTRL_BASE(PWM) + 2)
  88. #define PWM_CMD_GET (RT_DEVICE_CTRL_BASE(PWM) + 3)
  89. #define PWMN_CMD_ENABLE (RT_DEVICE_CTRL_BASE(PWM) + 4)
  90. #define PWMN_CMD_DISABLE (RT_DEVICE_CTRL_BASE(PWM) + 5)
  91. #define PWM_CMD_SET_PERIOD (RT_DEVICE_CTRL_BASE(PWM) + 6)
  92. #define PWM_CMD_SET_PULSE (RT_DEVICE_CTRL_BASE(PWM) + 7)
  93. #define PWM_CMD_SET_DEAD_TIME (RT_DEVICE_CTRL_BASE(PWM) + 8)
  94. #define PWM_CMD_SET_PHASE (RT_DEVICE_CTRL_BASE(PWM) + 9)
  95. #define PWM_CMD_ENABLE_IRQ (RT_DEVICE_CTRL_BASE(PWM) + 10)
  96. #define PWM_CMD_DISABLE_IRQ (RT_DEVICE_CTRL_BASE(PWM) + 11)
  97. /**
  98. * @brief PWM configuration
  99. */
  100. struct rt_pwm_configuration
  101. {
  102. rt_uint32_t channel; /* 0 ~ n or 0 ~ -n, which depends on specific MCU requirements */
  103. rt_uint32_t period; /* unit:ns 1ns~4.29s:1Ghz~0.23hz */
  104. rt_uint32_t pulse; /* unit:ns (pulse<=period) */
  105. rt_uint32_t dead_time; /* unit:ns */
  106. rt_uint32_t phase; /*unit: degree, 0~360, which is the phase of pwm output, */
  107. /*
  108. * RT_TRUE : The channel of pwm is complememtary.
  109. * RT_FALSE : The channel of pwm is nomal.
  110. */
  111. rt_bool_t complementary;
  112. };
  113. struct rt_device_pwm;
  114. /**
  115. * @brief PWM operations
  116. */
  117. struct rt_pwm_ops
  118. {
  119. rt_err_t (*control)(struct rt_device_pwm *device, int cmd, void *arg);
  120. };
  121. /**
  122. * @brief PWM device
  123. */
  124. struct rt_device_pwm
  125. {
  126. struct rt_device parent;
  127. const struct rt_pwm_ops *ops;
  128. };
  129. /**
  130. * @brief register a PWM device
  131. * @param device the PWM device
  132. * @param name the name of PWM device
  133. * @param ops the operations of PWM device
  134. * @param user_data the user data of PWM device
  135. * @return rt_err_t error code
  136. */
  137. rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, const struct rt_pwm_ops *ops, const void *user_data);
  138. /**
  139. * @brief enable the PWM channel
  140. * @param device the PWM device
  141. * @param channel the channel of PWM
  142. * @return rt_err_t error code
  143. */
  144. rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel);
  145. /**
  146. * @brief disable the PWM channel
  147. * @param device the PWM device
  148. * @param channel the channel of PWM
  149. * @return rt_err_t error code
  150. */
  151. rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel);
  152. /**
  153. * @brief set the PWM channel
  154. * @param device the PWM device
  155. * @param channel the channel of PWM
  156. * @param period the period of PWM
  157. * @param pulse the pulse of PWM
  158. * @return rt_err_t error code
  159. */
  160. rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse);
  161. /**
  162. * @brief set the PWM channel period
  163. * @param device the PWM device
  164. * @param channel the channel of PWM
  165. * @param period the period of PWM
  166. * @return rt_err_t error code
  167. */
  168. rt_err_t rt_pwm_set_period(struct rt_device_pwm *device, int channel, rt_uint32_t period);
  169. /**
  170. * @brief set the PWM channel pulse
  171. * @param device the PWM device
  172. * @param channel the channel of PWM
  173. * @param pulse the period of PWM
  174. * @return rt_err_t error code
  175. */
  176. rt_err_t rt_pwm_set_pulse(struct rt_device_pwm *device, int channel, rt_uint32_t pulse);
  177. /**
  178. * @brief set the dead zone time of PWM
  179. * @param device the PWM device
  180. * @param channel the channel of PWM
  181. * @param dead_time dead zone time
  182. * @return rt_err_t error code
  183. */
  184. rt_err_t rt_pwm_set_dead_time(struct rt_device_pwm *device, int channel, rt_uint32_t dead_time);
  185. /**
  186. * @brief set the phase of PWM
  187. * @param device the PWM device
  188. * @param channel the channel of PWM
  189. * @param phase phase
  190. * @return rt_err_t error code
  191. */
  192. rt_err_t rt_pwm_set_phase(struct rt_device_pwm *device, int channel, rt_uint32_t phase);
  193. /*! @}*/
  194. #endif /* __DEV_PWM_H__ */