rt_drv_pwm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * 2022-07-25 liYony fix complementary outputs and add usage information in finsh
  11. */
  12. #include <rtdevice.h>
  13. static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
  14. {
  15. rt_err_t result = RT_EOK;
  16. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  17. if (pwm->ops->control)
  18. {
  19. result = pwm->ops->control(pwm, cmd, args);
  20. }
  21. return result;
  22. }
  23. /*
  24. pos: channel
  25. void *buffer: rt_uint32_t pulse[size]
  26. size : number of pulse, only set to sizeof(rt_uint32_t).
  27. */
  28. static rt_size_t _pwm_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  29. {
  30. rt_err_t result = RT_EOK;
  31. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  32. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  33. struct rt_pwm_configuration configuration = {0};
  34. configuration.channel = (pos > 0) ? (pos) : (-pos);
  35. if (pwm->ops->control)
  36. {
  37. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  38. if (result != RT_EOK)
  39. {
  40. return 0;
  41. }
  42. *pulse = configuration.pulse;
  43. }
  44. return size;
  45. }
  46. /*
  47. pos: channel
  48. void *buffer: rt_uint32_t pulse[size]
  49. size : number of pulse, only set to sizeof(rt_uint32_t).
  50. */
  51. static rt_size_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  52. {
  53. rt_err_t result = RT_EOK;
  54. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  55. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  56. struct rt_pwm_configuration configuration = {0};
  57. configuration.channel = (pos > 0) ? (pos) : (-pos);
  58. if (pwm->ops->control)
  59. {
  60. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  61. if (result != RT_EOK)
  62. {
  63. return 0;
  64. }
  65. configuration.pulse = *pulse;
  66. result = pwm->ops->control(pwm, PWM_CMD_SET, &configuration);
  67. if (result != RT_EOK)
  68. {
  69. return 0;
  70. }
  71. }
  72. return size;
  73. }
  74. #ifdef RT_USING_DEVICE_OPS
  75. static const struct rt_device_ops pwm_device_ops =
  76. {
  77. RT_NULL,
  78. RT_NULL,
  79. RT_NULL,
  80. _pwm_read,
  81. _pwm_write,
  82. _pwm_control
  83. };
  84. #endif /* RT_USING_DEVICE_OPS */
  85. 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)
  86. {
  87. rt_err_t result = RT_EOK;
  88. rt_memset(device, 0, sizeof(struct rt_device_pwm));
  89. #ifdef RT_USING_DEVICE_OPS
  90. device->parent.ops = &pwm_device_ops;
  91. #else
  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. #endif /* RT_USING_DEVICE_OPS */
  99. device->parent.type = RT_Device_Class_PWM;
  100. device->ops = ops;
  101. device->parent.user_data = (void *)user_data;
  102. result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
  103. return result;
  104. }
  105. rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
  106. {
  107. rt_err_t result = RT_EOK;
  108. struct rt_pwm_configuration configuration = {0};
  109. if (!device)
  110. {
  111. return -RT_EIO;
  112. }
  113. configuration.channel = (channel > 0) ? (channel) : (-channel); /* Make it is positive num forever */
  114. configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */
  115. result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
  116. return result;
  117. }
  118. rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
  119. {
  120. rt_err_t result = RT_EOK;
  121. struct rt_pwm_configuration configuration = {0};
  122. if (!device)
  123. {
  124. return -RT_EIO;
  125. }
  126. configuration.channel = (channel > 0) ? (channel) : (-channel); /* Make it is positive num forever */
  127. configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */
  128. result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
  129. return result;
  130. }
  131. rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
  132. {
  133. rt_err_t result = RT_EOK;
  134. struct rt_pwm_configuration configuration = {0};
  135. if (!device)
  136. {
  137. return -RT_EIO;
  138. }
  139. configuration.channel = (channel > 0) ? (channel) : (-channel);
  140. configuration.period = period;
  141. configuration.pulse = pulse;
  142. result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);
  143. return result;
  144. }
  145. rt_err_t rt_pwm_set_period(struct rt_device_pwm *device, int channel, rt_uint32_t period)
  146. {
  147. rt_err_t result = RT_EOK;
  148. struct rt_pwm_configuration configuration = {0};
  149. if (!device)
  150. {
  151. return -RT_EIO;
  152. }
  153. configuration.channel = (channel > 0) ? (channel) : (-channel);
  154. configuration.period = period;
  155. result = rt_device_control(&device->parent, PWM_CMD_SET_PERIOD, &configuration);
  156. return result;
  157. }
  158. rt_err_t rt_pwm_set_pulse(struct rt_device_pwm *device, int channel, rt_uint32_t pulse)
  159. {
  160. rt_err_t result = RT_EOK;
  161. struct rt_pwm_configuration configuration = {0};
  162. if (!device)
  163. {
  164. return -RT_EIO;
  165. }
  166. configuration.channel = (channel > 0) ? (channel) : (-channel);
  167. configuration.pulse = pulse;
  168. result = rt_device_control(&device->parent, PWM_CMD_SET_PULSE, &configuration);
  169. return result;
  170. }
  171. rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *cfg)
  172. {
  173. rt_err_t result = RT_EOK;
  174. if (!device)
  175. {
  176. return -RT_EIO;
  177. }
  178. result = rt_device_control(&device->parent, PWM_CMD_GET, cfg);
  179. return result;
  180. }
  181. #ifdef RT_USING_FINSH
  182. #include <stdlib.h>
  183. #include <string.h>
  184. #include <finsh.h>
  185. static int pwm(int argc, char **argv)
  186. {
  187. rt_err_t result = -RT_ERROR;
  188. char *result_str;
  189. static struct rt_device_pwm *pwm_device = RT_NULL;
  190. struct rt_pwm_configuration cfg = {0};
  191. if(argc > 1)
  192. {
  193. if(!strcmp(argv[1], "probe"))
  194. {
  195. if(argc == 3)
  196. {
  197. pwm_device = (struct rt_device_pwm *)rt_device_find(argv[2]);
  198. result_str = (pwm_device == RT_NULL) ? "failure" : "success";
  199. rt_kprintf("probe %s %s\n", argv[2], result_str);
  200. }
  201. else
  202. {
  203. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  204. }
  205. }
  206. else
  207. {
  208. if(pwm_device == RT_NULL)
  209. {
  210. rt_kprintf("Please using 'pwm probe <device name>' first.\n");
  211. return -RT_ERROR;
  212. }
  213. if(!strcmp(argv[1], "enable"))
  214. {
  215. if(argc == 3)
  216. {
  217. result = rt_pwm_enable(pwm_device, atoi(argv[2]));
  218. result_str = (result == RT_EOK) ? "success" : "failure";
  219. rt_kprintf("%s channel %d is enabled %s \n", pwm_device->parent.parent.name, atoi(argv[2]), result_str);
  220. }
  221. else
  222. {
  223. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  224. rt_kprintf(" e.g. MSH >pwm enable 1 - PWM_CH1 nomal\n");
  225. rt_kprintf(" e.g. MSH >pwm enable -1 - PWM_CH1N complememtary\n");
  226. }
  227. }
  228. else if(!strcmp(argv[1], "disable"))
  229. {
  230. if(argc == 3)
  231. {
  232. result = rt_pwm_disable(pwm_device, atoi(argv[2]));
  233. }
  234. else
  235. {
  236. rt_kprintf("pwm disable <channel> - disable pwm channel\n");
  237. }
  238. }
  239. else if(!strcmp(argv[1], "get"))
  240. {
  241. cfg.channel = atoi(argv[2]);
  242. result = rt_pwm_get(pwm_device, &cfg);
  243. if(result == RT_EOK)
  244. {
  245. rt_kprintf("Info of device [%s] channel [%d]:\n",pwm_device, atoi(argv[2]));
  246. rt_kprintf("period : %d\n", cfg.period);
  247. rt_kprintf("pulse : %d\n", cfg.pulse);
  248. rt_kprintf("Duty cycle : %d%%\n",(int)(((double)(cfg.pulse)/(cfg.period)) * 100));
  249. }
  250. else
  251. {
  252. rt_kprintf("Get info of device: [%s] error.\n", pwm_device);
  253. }
  254. }
  255. else if (!strcmp(argv[1], "set"))
  256. {
  257. if(argc == 5)
  258. {
  259. result = rt_pwm_set(pwm_device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  260. rt_kprintf("pwm info set on %s at channel %d\n",pwm_device,atoi(argv[2]));
  261. }
  262. else
  263. {
  264. rt_kprintf("Set info of device: [%s] error\n", pwm_device);
  265. rt_kprintf("Usage: pwm set <channel> <period> <pulse>\n");
  266. }
  267. }
  268. else
  269. {
  270. rt_kprintf("pwm get <channel> - get pwm channel info\n");
  271. }
  272. }
  273. }
  274. else
  275. {
  276. rt_kprintf("Usage: \n");
  277. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  278. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  279. rt_kprintf("pwm disable <channel> - disable pwm channel\n");
  280. rt_kprintf("pwm get <channel> - get pwm channel info\n");
  281. rt_kprintf("pwm set <channel> <period> <pulse> - set pwm channel info\n");
  282. result = - RT_ERROR;
  283. }
  284. return RT_EOK;
  285. }
  286. MSH_CMD_EXPORT(pwm, pwm [option]);
  287. #endif /* RT_USING_FINSH */