dev_pwm.h 5.7 KB

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