rt_drv_pwm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2006-2018, 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. 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)
  74. {
  75. rt_err_t result = RT_EOK;
  76. memset(device, 0, sizeof(struct rt_device_pwm));
  77. device->parent.type = RT_Device_Class_Miscellaneous;
  78. device->parent.init = RT_NULL;
  79. device->parent.open = RT_NULL;
  80. device->parent.close = RT_NULL;
  81. device->parent.read = _pwm_read;
  82. device->parent.write = _pwm_write;
  83. device->parent.control = _pwm_control;
  84. device->ops = ops;
  85. device->parent.user_data = (void *)user_data;
  86. result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
  87. return result;
  88. }
  89. rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
  90. {
  91. rt_err_t result = RT_EOK;
  92. struct rt_pwm_configuration configuration = {0};
  93. if (!device)
  94. {
  95. return -RT_EIO;
  96. }
  97. configuration.channel = channel;
  98. result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
  99. return result;
  100. }
  101. rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
  102. {
  103. rt_err_t result = RT_EOK;
  104. struct rt_pwm_configuration configuration = {0};
  105. if (!device)
  106. {
  107. return -RT_EIO;
  108. }
  109. configuration.channel = channel;
  110. result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
  111. return result;
  112. }
  113. rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
  114. {
  115. rt_err_t result = RT_EOK;
  116. struct rt_pwm_configuration configuration = {0};
  117. if (!device)
  118. {
  119. return -RT_EIO;
  120. }
  121. configuration.channel = channel;
  122. configuration.period = period;
  123. configuration.pulse = pulse;
  124. result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);
  125. return result;
  126. }
  127. #ifdef RT_USING_FINSH
  128. #include <finsh.h>
  129. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_enable, pwm_enable, enable pwm by channel.);
  130. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_set, pwm_set, set pwm.);
  131. #ifdef FINSH_USING_MSH
  132. static int pwm_enable(int argc, char **argv)
  133. {
  134. int result = 0;
  135. struct rt_device_pwm *device = RT_NULL;
  136. if (argc != 3)
  137. {
  138. rt_kprintf("Usage: pwm_enable pwm1 1\n");
  139. result = -RT_ERROR;
  140. goto _exit;
  141. }
  142. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  143. if (!device)
  144. {
  145. result = -RT_EIO;
  146. goto _exit;
  147. }
  148. result = rt_pwm_enable(device, atoi(argv[2]));
  149. _exit:
  150. return result;
  151. }
  152. MSH_CMD_EXPORT(pwm_enable, pwm_enable pwm1 1);
  153. static int pwm_disable(int argc, char **argv)
  154. {
  155. int result = 0;
  156. struct rt_device_pwm *device = RT_NULL;
  157. if (argc != 3)
  158. {
  159. rt_kprintf("Usage: pwm_enable pwm1 1\n");
  160. result = -RT_ERROR;
  161. goto _exit;
  162. }
  163. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  164. if (!device)
  165. {
  166. result = -RT_EIO;
  167. goto _exit;
  168. }
  169. result = rt_pwm_disable(device, atoi(argv[2]));
  170. _exit:
  171. return result;
  172. }
  173. MSH_CMD_EXPORT(pwm_disable, pwm_disable pwm1 1);
  174. static int pwm_set(int argc, char **argv)
  175. {
  176. int result = 0;
  177. struct rt_device_pwm *device = RT_NULL;
  178. if (argc != 5)
  179. {
  180. rt_kprintf("Usage: pwm_set pwm1 1 100 50\n");
  181. result = -RT_ERROR;
  182. goto _exit;
  183. }
  184. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  185. if (!device)
  186. {
  187. result = -RT_EIO;
  188. goto _exit;
  189. }
  190. result = rt_pwm_set(device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  191. _exit:
  192. return result;
  193. }
  194. MSH_CMD_EXPORT(pwm_set, pwm_set 1 100 50);
  195. #endif /* FINSH_USING_MSH */
  196. #endif /* RT_USING_FINSH */