rt_drv_pwm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2006-2021, 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. */
  10. #include <string.h>
  11. #include <drivers/rt_drv_pwm.h>
  12. static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
  13. {
  14. rt_err_t result = RT_EOK;
  15. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  16. if (pwm->ops->control)
  17. {
  18. result = pwm->ops->control(pwm, cmd, args);
  19. }
  20. return result;
  21. }
  22. /*
  23. pos: channel
  24. void *buffer: rt_uint32_t pulse[size]
  25. size : number of pulse, only set to sizeof(rt_uint32_t).
  26. */
  27. static rt_size_t _pwm_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  28. {
  29. rt_err_t result = RT_EOK;
  30. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  31. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  32. struct rt_pwm_configuration configuration = {0};
  33. configuration.channel = pos;
  34. if (pwm->ops->control)
  35. {
  36. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  37. if (result != RT_EOK)
  38. {
  39. return 0;
  40. }
  41. *pulse = configuration.pulse;
  42. }
  43. return size;
  44. }
  45. /*
  46. pos: channel
  47. void *buffer: rt_uint32_t pulse[size]
  48. size : number of pulse, only set to sizeof(rt_uint32_t).
  49. */
  50. static rt_size_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  51. {
  52. rt_err_t result = RT_EOK;
  53. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  54. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  55. struct rt_pwm_configuration configuration = {0};
  56. configuration.channel = pos;
  57. if (pwm->ops->control)
  58. {
  59. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  60. if (result != RT_EOK)
  61. {
  62. return 0;
  63. }
  64. configuration.pulse = *pulse;
  65. result = pwm->ops->control(pwm, PWM_CMD_SET, &configuration);
  66. if (result != RT_EOK)
  67. {
  68. return 0;
  69. }
  70. }
  71. return size;
  72. }
  73. #ifdef RT_USING_DEVICE_OPS
  74. static const struct rt_device_ops pwm_device_ops =
  75. {
  76. RT_NULL,
  77. RT_NULL,
  78. RT_NULL,
  79. _pwm_read,
  80. _pwm_write,
  81. _pwm_control
  82. };
  83. #endif /* RT_USING_DEVICE_OPS */
  84. 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)
  85. {
  86. rt_err_t result = RT_EOK;
  87. memset(device, 0, sizeof(struct rt_device_pwm));
  88. #ifdef RT_USING_DEVICE_OPS
  89. device->parent.ops = &pwm_device_ops;
  90. #else
  91. device->parent.init = RT_NULL;
  92. device->parent.open = RT_NULL;
  93. device->parent.close = RT_NULL;
  94. device->parent.read = _pwm_read;
  95. device->parent.write = _pwm_write;
  96. device->parent.control = _pwm_control;
  97. #endif /* RT_USING_DEVICE_OPS */
  98. device->parent.type = RT_Device_Class_Miscellaneous;
  99. device->ops = ops;
  100. device->parent.user_data = (void *)user_data;
  101. result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
  102. return result;
  103. }
  104. rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
  105. {
  106. rt_err_t result = RT_EOK;
  107. struct rt_pwm_configuration configuration = {0};
  108. if (!device)
  109. {
  110. return -RT_EIO;
  111. }
  112. configuration.channel = channel;
  113. result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
  114. return result;
  115. }
  116. rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
  117. {
  118. rt_err_t result = RT_EOK;
  119. struct rt_pwm_configuration configuration = {0};
  120. if (!device)
  121. {
  122. return -RT_EIO;
  123. }
  124. configuration.channel = channel;
  125. result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
  126. return result;
  127. }
  128. rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
  129. {
  130. rt_err_t result = RT_EOK;
  131. struct rt_pwm_configuration configuration = {0};
  132. if (!device)
  133. {
  134. return -RT_EIO;
  135. }
  136. configuration.channel = channel;
  137. configuration.period = period;
  138. configuration.pulse = pulse;
  139. result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);
  140. return result;
  141. }
  142. #ifdef RT_USING_FINSH
  143. #include <finsh.h>
  144. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_enable, pwm_enable, enable pwm by channel.);
  145. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_set, pwm_set, set pwm.);
  146. #ifdef FINSH_USING_MSH
  147. static int pwm_enable(int argc, char **argv)
  148. {
  149. int result = 0;
  150. struct rt_device_pwm *device = RT_NULL;
  151. if (argc != 3)
  152. {
  153. rt_kprintf("Usage: pwm_enable pwm1 1\n");
  154. result = -RT_ERROR;
  155. goto _exit;
  156. }
  157. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  158. if (!device)
  159. {
  160. result = -RT_EIO;
  161. goto _exit;
  162. }
  163. result = rt_pwm_enable(device, atoi(argv[2]));
  164. _exit:
  165. return result;
  166. }
  167. MSH_CMD_EXPORT(pwm_enable, pwm_enable pwm1 1);
  168. static int pwm_disable(int argc, char **argv)
  169. {
  170. int result = 0;
  171. struct rt_device_pwm *device = RT_NULL;
  172. if (argc != 3)
  173. {
  174. rt_kprintf("Usage: pwm_enable pwm1 1\n");
  175. result = -RT_ERROR;
  176. goto _exit;
  177. }
  178. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  179. if (!device)
  180. {
  181. result = -RT_EIO;
  182. goto _exit;
  183. }
  184. result = rt_pwm_disable(device, atoi(argv[2]));
  185. _exit:
  186. return result;
  187. }
  188. MSH_CMD_EXPORT(pwm_disable, pwm_disable pwm1 1);
  189. static int pwm_set(int argc, char **argv)
  190. {
  191. int result = 0;
  192. struct rt_device_pwm *device = RT_NULL;
  193. if (argc != 5)
  194. {
  195. rt_kprintf("Usage: pwm_set pwm1 1 100 50\n");
  196. result = -RT_ERROR;
  197. goto _exit;
  198. }
  199. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  200. if (!device)
  201. {
  202. result = -RT_EIO;
  203. goto _exit;
  204. }
  205. result = rt_pwm_set(device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  206. _exit:
  207. return result;
  208. }
  209. MSH_CMD_EXPORT(pwm_set, pwm_set pwm1 1 100 50);
  210. #endif /* FINSH_USING_MSH */
  211. #endif /* RT_USING_FINSH */