rt_drv_pwm.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <rtdevice.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_set_period(struct rt_device_pwm *device, int channel, rt_uint32_t period)
  145. {
  146. rt_err_t result = RT_EOK;
  147. struct rt_pwm_configuration configuration = {0};
  148. if (!device)
  149. {
  150. return -RT_EIO;
  151. }
  152. configuration.channel = channel;
  153. configuration.period = period;
  154. result = rt_device_control(&device->parent, PWM_CMD_SET_PERIOD, &configuration);
  155. return result;
  156. }
  157. rt_err_t rt_pwm_set_pulse(struct rt_device_pwm *device, int channel, rt_uint32_t pulse)
  158. {
  159. rt_err_t result = RT_EOK;
  160. struct rt_pwm_configuration configuration = {0};
  161. if (!device)
  162. {
  163. return -RT_EIO;
  164. }
  165. configuration.channel = channel;
  166. configuration.pulse = pulse;
  167. result = rt_device_control(&device->parent, PWM_CMD_SET_PULSE, &configuration);
  168. return result;
  169. }
  170. rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *cfg)
  171. {
  172. rt_err_t result = RT_EOK;
  173. if (!device)
  174. {
  175. return -RT_EIO;
  176. }
  177. result = rt_device_control(&device->parent, PWM_CMD_GET, cfg);
  178. return result;
  179. }
  180. #ifdef RT_USING_FINSH
  181. #include <stdlib.h>
  182. #include <string.h>
  183. #include <finsh.h>
  184. static int pwm(int argc, char **argv)
  185. {
  186. rt_err_t result = -RT_ERROR;
  187. char *result_str;
  188. static struct rt_device_pwm *pwm_device = RT_NULL;
  189. struct rt_pwm_configuration cfg = {0};
  190. if(argc > 1)
  191. {
  192. if(!strcmp(argv[1], "probe"))
  193. {
  194. if(argc == 3)
  195. {
  196. pwm_device = (struct rt_device_pwm *)rt_device_find(argv[2]);
  197. result_str = (pwm_device == RT_NULL) ? "failure" : "success";
  198. rt_kprintf("probe %s %s\n", argv[2], result_str);
  199. }
  200. else
  201. {
  202. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  203. }
  204. }
  205. else
  206. {
  207. if(pwm_device == RT_NULL)
  208. {
  209. rt_kprintf("Please using 'pwm probe <device name>' first.\n");
  210. return -RT_ERROR;
  211. }
  212. if(!strcmp(argv[1], "enable"))
  213. {
  214. if(argc == 3)
  215. {
  216. result = rt_pwm_enable(pwm_device, atoi(argv[2]));
  217. result_str = (result == RT_EOK) ? "success" : "failure";
  218. rt_kprintf("%s channel %d is enabled %s \n", pwm_device->parent.parent.name, atoi(argv[2]), result_str);
  219. }
  220. else
  221. {
  222. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  223. }
  224. }
  225. else if(!strcmp(argv[1], "disable"))
  226. {
  227. if(argc == 3)
  228. {
  229. result = rt_pwm_disable(pwm_device, atoi(argv[2]));
  230. }
  231. else
  232. {
  233. rt_kprintf("pwm disable <channel> - disable pwm channel\n");
  234. }
  235. }
  236. else if(!strcmp(argv[1], "get"))
  237. {
  238. cfg.channel = atoi(argv[2]);
  239. result = rt_pwm_get(pwm_device, &cfg);
  240. if(result == RT_EOK)
  241. {
  242. rt_kprintf("Info of device [%s] channel [%d]:\n",pwm_device, atoi(argv[2]));
  243. rt_kprintf("period : %d\n", cfg.period);
  244. rt_kprintf("pulse : %d\n", cfg.pulse);
  245. rt_kprintf("Duty cycle : %d%%\n",(int)(((double)(cfg.pulse)/(cfg.period)) * 100));
  246. }
  247. else
  248. {
  249. rt_kprintf("Get info of device: [%s] error.\n", pwm_device);
  250. }
  251. }
  252. else if (!strcmp(argv[1], "set"))
  253. {
  254. if(argc == 5)
  255. {
  256. result = rt_pwm_set(pwm_device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  257. rt_kprintf("pwm info set on %s at channel %d\n",pwm_device,atoi(argv[2]));
  258. }
  259. else
  260. {
  261. rt_kprintf("Set info of device: [%s] error\n", pwm_device);
  262. rt_kprintf("Usage: pwm set <channel> <period> <pulse>\n");
  263. }
  264. }
  265. else
  266. {
  267. rt_kprintf("pwm get <channel> - get pwm channel info\n");
  268. }
  269. }
  270. }
  271. else
  272. {
  273. rt_kprintf("Usage: \n");
  274. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  275. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  276. rt_kprintf("pwm disable <channel>- disable pwm channel\n");
  277. rt_kprintf("pwm get <channel> - get pwm channel info\n");
  278. rt_kprintf("pwm set <channel> <period> <pulse> - set pwm channel info\n");
  279. result = - RT_ERROR;
  280. }
  281. return RT_EOK;
  282. }
  283. MSH_CMD_EXPORT(pwm, pwm [option]);
  284. #endif /* RT_USING_FINSH */