rt_drv_pwm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * File : rt_drv_pwm.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-07 aozima the first version
  23. */
  24. #include <string.h>
  25. #include <drivers/rt_drv_pwm.h>
  26. static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
  27. {
  28. rt_err_t result = RT_EOK;
  29. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  30. if (pwm->ops->control)
  31. {
  32. result = pwm->ops->control(pwm, cmd, args);
  33. }
  34. return result;
  35. }
  36. /*
  37. pos: channel
  38. void *buffer: rt_uint32_t pulse[size]
  39. size : number of pulse, only set to sizeof(rt_uint32_t).
  40. */
  41. static rt_size_t _pwm_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  42. {
  43. rt_err_t result = RT_EOK;
  44. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  45. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  46. struct rt_pwm_configuration configuration = {0};
  47. configuration.channel = pos;
  48. if (pwm->ops->control)
  49. {
  50. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  51. if (result != RT_EOK)
  52. {
  53. return 0;
  54. }
  55. *pulse = configuration.pulse;
  56. }
  57. return size;
  58. }
  59. /*
  60. pos: channel
  61. void *buffer: rt_uint32_t pulse[size]
  62. size : number of pulse, only set to sizeof(rt_uint32_t).
  63. */
  64. static rt_size_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  65. {
  66. rt_err_t result = RT_EOK;
  67. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  68. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  69. struct rt_pwm_configuration configuration = {0};
  70. configuration.channel = pos;
  71. if (pwm->ops->control)
  72. {
  73. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  74. if (result != RT_EOK)
  75. {
  76. return 0;
  77. }
  78. configuration.pulse = *pulse;
  79. result = pwm->ops->control(pwm, PWM_CMD_SET, &configuration);
  80. if (result != RT_EOK)
  81. {
  82. return 0;
  83. }
  84. }
  85. return size;
  86. }
  87. 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)
  88. {
  89. rt_err_t result = RT_EOK;
  90. memset(device, 0, sizeof(struct rt_device_pwm));
  91. device->parent.type = RT_Device_Class_Miscellaneous;
  92. device->parent.init = RT_NULL;
  93. device->parent.open = RT_NULL;
  94. device->parent.close = RT_NULL;
  95. device->parent.read = _pwm_read;
  96. device->parent.write = _pwm_write;
  97. device->parent.control = _pwm_control;
  98. device->ops = ops;
  99. device->parent.user_data = (void *)user_data;
  100. result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
  101. return result;
  102. }
  103. rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
  104. {
  105. rt_err_t result = RT_EOK;
  106. struct rt_pwm_configuration configuration = {0};
  107. if (!device)
  108. {
  109. return -RT_EIO;
  110. }
  111. configuration.channel = channel;
  112. result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
  113. return result;
  114. }
  115. rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
  116. {
  117. rt_err_t result = RT_EOK;
  118. struct rt_pwm_configuration configuration = {0};
  119. if (!device)
  120. {
  121. return -RT_EIO;
  122. }
  123. configuration.channel = channel;
  124. result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
  125. return result;
  126. }
  127. rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
  128. {
  129. rt_err_t result = RT_EOK;
  130. struct rt_pwm_configuration configuration = {0};
  131. if (!device)
  132. {
  133. return -RT_EIO;
  134. }
  135. configuration.channel = channel;
  136. configuration.period = period;
  137. configuration.pulse = pulse;
  138. result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);
  139. return result;
  140. }
  141. #ifdef RT_USING_FINSH
  142. #include <finsh.h>
  143. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_enable, pwm_enable, enable pwm by channel.);
  144. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_set, pwm_set, set pwm.);
  145. #ifdef FINSH_USING_MSH
  146. static int pwm_enable(int argc, char **argv)
  147. {
  148. int result = 0;
  149. struct rt_device_pwm *device = RT_NULL;
  150. if (argc != 3)
  151. {
  152. rt_kprintf("Usage: pwm_enable pwm1 1\n");
  153. result = -RT_ERROR;
  154. goto _exit;
  155. }
  156. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  157. if (!device)
  158. {
  159. result = -RT_EIO;
  160. goto _exit;
  161. }
  162. result = rt_pwm_enable(device, atoi(argv[2]));
  163. _exit:
  164. return result;
  165. }
  166. MSH_CMD_EXPORT(pwm_enable, pwm_enable pwm1 1);
  167. static int pwm_disable(int argc, char **argv)
  168. {
  169. int result = 0;
  170. struct rt_device_pwm *device = RT_NULL;
  171. if (argc != 3)
  172. {
  173. rt_kprintf("Usage: pwm_enable pwm1 1\n");
  174. result = -RT_ERROR;
  175. goto _exit;
  176. }
  177. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  178. if (!device)
  179. {
  180. result = -RT_EIO;
  181. goto _exit;
  182. }
  183. result = rt_pwm_disable(device, atoi(argv[2]));
  184. _exit:
  185. return result;
  186. }
  187. MSH_CMD_EXPORT(pwm_disable, pwm_disable pwm1 1);
  188. static int pwm_set(int argc, char **argv)
  189. {
  190. int result = 0;
  191. struct rt_device_pwm *device = RT_NULL;
  192. if (argc != 5)
  193. {
  194. rt_kprintf("Usage: pwm_set pwm1 1 100 50\n");
  195. result = -RT_ERROR;
  196. goto _exit;
  197. }
  198. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  199. if (!device)
  200. {
  201. result = -RT_EIO;
  202. goto _exit;
  203. }
  204. result = rt_pwm_set(device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  205. _exit:
  206. return result;
  207. }
  208. MSH_CMD_EXPORT(pwm_set, pwm_set 1 100 50);
  209. #endif /* FINSH_USING_MSH */
  210. #endif /* RT_USING_FINSH */