rt_drv_pwm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2006-2022, 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-05-14 Stanley Lwin add pwm function
  10. */
  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. rt_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_PWM;
  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 > 0) ? (channel) : (-channel); /* Make it is positive num forever */
  113. configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */
  114. result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
  115. return result;
  116. }
  117. rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
  118. {
  119. rt_err_t result = RT_EOK;
  120. struct rt_pwm_configuration configuration = {0};
  121. if (!device)
  122. {
  123. return -RT_EIO;
  124. }
  125. configuration.channel = (channel > 0) ? (channel) : (-channel); /* Make it is positive num forever */
  126. configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */
  127. result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
  128. return result;
  129. }
  130. rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
  131. {
  132. rt_err_t result = RT_EOK;
  133. struct rt_pwm_configuration configuration = {0};
  134. if (!device)
  135. {
  136. return -RT_EIO;
  137. }
  138. configuration.channel = channel;
  139. configuration.period = period;
  140. configuration.pulse = pulse;
  141. result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);
  142. return result;
  143. }
  144. rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *cfg)
  145. {
  146. rt_err_t result = RT_EOK;
  147. if (!device)
  148. {
  149. return -RT_EIO;
  150. }
  151. result = rt_device_control(&device->parent, PWM_CMD_GET, cfg);
  152. return result;
  153. }
  154. #ifdef RT_USING_FINSH
  155. #include <stdlib.h>
  156. #include <string.h>
  157. #include <finsh.h>
  158. static int pwm(int argc, char **argv)
  159. {
  160. rt_err_t result = -RT_ERROR;
  161. char *result_str;
  162. static struct rt_device_pwm *pwm_device = RT_NULL;
  163. struct rt_pwm_configuration cfg = {0};
  164. if(argc > 1)
  165. {
  166. if(!strcmp(argv[1], "probe"))
  167. {
  168. if(argc == 3)
  169. {
  170. pwm_device = (struct rt_device_pwm *)rt_device_find(argv[2]);
  171. result_str = (pwm_device == RT_NULL) ? "failure" : "success";
  172. rt_kprintf("probe %s %s\n", argv[2], result_str);
  173. }
  174. else
  175. {
  176. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  177. }
  178. }
  179. else
  180. {
  181. if(pwm_device == RT_NULL)
  182. {
  183. rt_kprintf("Please using 'pwm probe <device name>' first.\n");
  184. return -RT_ERROR;
  185. }
  186. if(!strcmp(argv[1], "enable"))
  187. {
  188. if(argc == 3)
  189. {
  190. result = rt_pwm_enable(pwm_device, atoi(argv[2]));
  191. result_str = (result == RT_EOK) ? "success" : "failure";
  192. rt_kprintf("%s channel %d is enabled %s \n", pwm_device->parent.parent.name, atoi(argv[2]), result_str);
  193. }
  194. else
  195. {
  196. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  197. }
  198. }
  199. else if(!strcmp(argv[1], "disable"))
  200. {
  201. if(argc == 3)
  202. {
  203. result = rt_pwm_disable(pwm_device, atoi(argv[2]));
  204. }
  205. else
  206. {
  207. rt_kprintf("pwm disable <channel> - disable pwm channel\n");
  208. }
  209. }
  210. else if(!strcmp(argv[1], "get"))
  211. {
  212. cfg.channel = atoi(argv[2]);
  213. result = rt_pwm_get(pwm_device, &cfg);
  214. if(result == RT_EOK)
  215. {
  216. rt_kprintf("Info of device [%s] channel [%d]:\n",pwm_device, atoi(argv[2]));
  217. rt_kprintf("period : %d\n", cfg.period);
  218. rt_kprintf("pulse : %d\n", cfg.pulse);
  219. rt_kprintf("Duty cycle : %d%%\n",(int)(((double)(cfg.pulse)/(cfg.period)) * 100));
  220. }
  221. else
  222. {
  223. rt_kprintf("Get info of device: [%s] error.\n", pwm_device);
  224. }
  225. }
  226. else if (!strcmp(argv[1], "set"))
  227. {
  228. if(argc == 5)
  229. {
  230. result = rt_pwm_set(pwm_device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  231. rt_kprintf("pwm info set on %s at channel %d\n",pwm_device,atoi(argv[2]));
  232. }
  233. else
  234. {
  235. rt_kprintf("Set info of device: [%s] error\n", pwm_device);
  236. rt_kprintf("Usage: pwm set <channel> <period> <pulse>\n");
  237. }
  238. }
  239. else
  240. {
  241. rt_kprintf("pwm get <channel> - get pwm channel info\n");
  242. }
  243. }
  244. }
  245. else
  246. {
  247. rt_kprintf("Usage: \n");
  248. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  249. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  250. rt_kprintf("pwm disable <channel>- disable pwm channel\n");
  251. rt_kprintf("pwm get <channel> - get pwm channel info\n");
  252. rt_kprintf("pwm set <channel> <period> <pulse> - set pwm channel info\n");
  253. result = - RT_ERROR;
  254. }
  255. return RT_EOK;
  256. }
  257. MSH_CMD_EXPORT(pwm, pwm <device name> <option> <channel>);
  258. #endif /* RT_USING_FINSH */