rt_drv_pwm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2006-2023, 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. * 2022-08-31 liYony Add complementary output section to framework for management
  12. * 2022-09-24 qiyu Add dead-time and phase configuration
  13. * 2023-12-23 1ridic Add second-level command completion
  14. */
  15. #include <rtdevice.h>
  16. static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
  17. {
  18. rt_err_t result = RT_EOK;
  19. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  20. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)args;
  21. switch (cmd)
  22. {
  23. case PWMN_CMD_ENABLE:
  24. configuration->complementary = RT_TRUE;
  25. break;
  26. case PWMN_CMD_DISABLE:
  27. configuration->complementary = RT_FALSE;
  28. break;
  29. default:
  30. if (pwm->ops->control)
  31. result = pwm->ops->control(pwm, cmd, args);
  32. break;
  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_ssize_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 > 0) ? (pos) : (-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_ssize_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 > 0) ? (pos) : (-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. #ifdef RT_USING_DEVICE_OPS
  88. static const struct rt_device_ops pwm_device_ops =
  89. {
  90. RT_NULL,
  91. RT_NULL,
  92. RT_NULL,
  93. _pwm_read,
  94. _pwm_write,
  95. _pwm_control
  96. };
  97. #endif /* RT_USING_DEVICE_OPS */
  98. 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)
  99. {
  100. rt_err_t result = RT_EOK;
  101. #ifndef RT_USING_DM
  102. rt_memset(device, 0, sizeof(struct rt_device_pwm));
  103. #endif
  104. #ifdef RT_USING_DEVICE_OPS
  105. device->parent.ops = &pwm_device_ops;
  106. #else
  107. device->parent.init = RT_NULL;
  108. device->parent.open = RT_NULL;
  109. device->parent.close = RT_NULL;
  110. device->parent.read = _pwm_read;
  111. device->parent.write = _pwm_write;
  112. device->parent.control = _pwm_control;
  113. #endif /* RT_USING_DEVICE_OPS */
  114. device->parent.type = RT_Device_Class_PWM;
  115. device->ops = ops;
  116. device->parent.user_data = (void *)user_data;
  117. result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
  118. return result;
  119. }
  120. rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
  121. {
  122. rt_err_t result = RT_EOK;
  123. struct rt_pwm_configuration configuration = {0};
  124. if (!device)
  125. {
  126. return -RT_EIO;
  127. }
  128. /* Make it is positive num forever */
  129. configuration.channel = (channel > 0) ? (channel) : (-channel);
  130. /* If channel is a positive number (0 ~ n), it means using normal output pin.
  131. * If channel is a negative number (0 ~ -n), it means using complementary output pin. */
  132. if (channel > 0)
  133. {
  134. result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
  135. }
  136. else
  137. {
  138. result = rt_device_control(&device->parent, PWMN_CMD_ENABLE, &configuration);
  139. }
  140. result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
  141. return result;
  142. }
  143. rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
  144. {
  145. rt_err_t result = RT_EOK;
  146. struct rt_pwm_configuration configuration = {0};
  147. if (!device)
  148. {
  149. return -RT_EIO;
  150. }
  151. /* Make it is positive num forever */
  152. configuration.channel = (channel > 0) ? (channel) : (-channel);
  153. /* If channel is a positive number (0 ~ n), it means using normal output pin.
  154. * If channel is a negative number (0 ~ -n), it means using complementary output pin. */
  155. if (channel > 0)
  156. {
  157. result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
  158. }
  159. else
  160. {
  161. result = rt_device_control(&device->parent, PWMN_CMD_ENABLE, &configuration);
  162. }
  163. result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
  164. return result;
  165. }
  166. rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
  167. {
  168. rt_err_t result = RT_EOK;
  169. struct rt_pwm_configuration configuration = {0};
  170. if (!device)
  171. {
  172. return -RT_EIO;
  173. }
  174. configuration.channel = (channel > 0) ? (channel) : (-channel);
  175. configuration.period = period;
  176. configuration.pulse = pulse;
  177. result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);
  178. return result;
  179. }
  180. rt_err_t rt_pwm_set_period(struct rt_device_pwm *device, int channel, rt_uint32_t period)
  181. {
  182. rt_err_t result = RT_EOK;
  183. struct rt_pwm_configuration configuration = {0};
  184. if (!device)
  185. {
  186. return -RT_EIO;
  187. }
  188. configuration.channel = (channel > 0) ? (channel) : (-channel);
  189. configuration.period = period;
  190. result = rt_device_control(&device->parent, PWM_CMD_SET_PERIOD, &configuration);
  191. return result;
  192. }
  193. rt_err_t rt_pwm_set_pulse(struct rt_device_pwm *device, int channel, rt_uint32_t pulse)
  194. {
  195. rt_err_t result = RT_EOK;
  196. struct rt_pwm_configuration configuration = {0};
  197. if (!device)
  198. {
  199. return -RT_EIO;
  200. }
  201. configuration.channel = (channel > 0) ? (channel) : (-channel);
  202. configuration.pulse = pulse;
  203. result = rt_device_control(&device->parent, PWM_CMD_SET_PULSE, &configuration);
  204. return result;
  205. }
  206. rt_err_t rt_pwm_set_dead_time(struct rt_device_pwm *device, int channel, rt_uint32_t dead_time)
  207. {
  208. rt_err_t result = RT_EOK;
  209. struct rt_pwm_configuration configuration = {0};
  210. if (!device)
  211. {
  212. return -RT_EIO;
  213. }
  214. configuration.channel = (channel > 0) ? (channel) : (-channel);
  215. configuration.dead_time = dead_time;
  216. result = rt_device_control(&device->parent, PWM_CMD_SET_DEAD_TIME, &configuration);
  217. return result;
  218. }
  219. rt_err_t rt_pwm_set_phase(struct rt_device_pwm *device, int channel, rt_uint32_t phase)
  220. {
  221. rt_err_t result = RT_EOK;
  222. struct rt_pwm_configuration configuration = {0};
  223. if (!device)
  224. {
  225. return -RT_EIO;
  226. }
  227. configuration.channel = (channel > 0) ? (channel) : (-channel);
  228. configuration.phase = phase;
  229. result = rt_device_control(&device->parent, PWM_CMD_SET_PHASE, &configuration);
  230. return result;
  231. }
  232. rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *cfg)
  233. {
  234. rt_err_t result = RT_EOK;
  235. if (!device)
  236. {
  237. return -RT_EIO;
  238. }
  239. result = rt_device_control(&device->parent, PWM_CMD_GET, cfg);
  240. return result;
  241. }
  242. #ifdef RT_USING_FINSH
  243. #include <stdlib.h>
  244. #include <string.h>
  245. #include <finsh.h>
  246. enum pwm_list_parameters
  247. {
  248. PWM_LIST_PROBE = 1,
  249. PWM_LIST_ENABLE,
  250. PWM_LIST_DISABLE,
  251. PWM_LIST_GET,
  252. PWM_LIST_SET,
  253. PWM_LIST_PHASE,
  254. PWM_LIST_DEAD_TIME,
  255. };
  256. CMD_OPTIONS_STATEMENT(pwm_list)
  257. static int pwm_list(int argc, char **argv)
  258. {
  259. rt_err_t result = -RT_ERROR;
  260. char *result_str;
  261. static struct rt_device_pwm *pwm_device = RT_NULL;
  262. struct rt_pwm_configuration cfg = {0};
  263. if (argc > 1)
  264. {
  265. if (MSH_OPT_ID_GET(pwm_list) == PWM_LIST_PROBE)
  266. {
  267. if (argc == 3)
  268. {
  269. pwm_device = (struct rt_device_pwm *)rt_device_find(argv[2]);
  270. result_str = (pwm_device == RT_NULL) ? "failure" : "success";
  271. rt_kprintf("probe %s %s\n", argv[2], result_str);
  272. return (pwm_device == RT_NULL) ? -RT_ERROR : RT_EOK;
  273. }
  274. else
  275. {
  276. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  277. return -RT_EINVAL;
  278. }
  279. }
  280. else if (pwm_device == RT_NULL)
  281. {
  282. rt_kprintf("Please using 'pwm probe <device name>' first.\n");
  283. return -RT_ERROR;
  284. }
  285. switch (MSH_OPT_ID_GET(pwm_list))
  286. {
  287. case PWM_LIST_ENABLE:
  288. if (argc == 3)
  289. {
  290. result = rt_pwm_enable(pwm_device, atoi(argv[2]));
  291. result_str = (result == RT_EOK) ? "success" : "failure";
  292. rt_kprintf("%s channel %d is enabled %s \n", pwm_device->parent.parent.name, atoi(argv[2]), result_str);
  293. }
  294. else
  295. {
  296. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  297. rt_kprintf(" e.g. MSH >pwm enable 1 - PWM_CH1 nomal\n");
  298. rt_kprintf(" e.g. MSH >pwm enable -1 - PWM_CH1N complememtary\n");
  299. }
  300. break;
  301. case PWM_LIST_DISABLE:
  302. if (argc == 3)
  303. {
  304. result = rt_pwm_disable(pwm_device, atoi(argv[2]));
  305. }
  306. else
  307. {
  308. rt_kprintf("pwm disable <channel> - disable pwm channel\n");
  309. }
  310. break;
  311. case PWM_LIST_GET:
  312. cfg.channel = atoi(argv[2]);
  313. result = rt_pwm_get(pwm_device, &cfg);
  314. if (result == RT_EOK)
  315. {
  316. rt_kprintf("Info of device [%s] channel [%d]:\n", pwm_device, atoi(argv[2]));
  317. rt_kprintf("period : %d\n", cfg.period);
  318. rt_kprintf("pulse : %d\n", cfg.pulse);
  319. rt_kprintf("Duty cycle : %d%%\n", (int)(((double)(cfg.pulse) / (cfg.period)) * 100));
  320. }
  321. else
  322. {
  323. rt_kprintf("Get info of device: [%s] error.\n", pwm_device);
  324. }
  325. break;
  326. case PWM_LIST_SET:
  327. if (argc == 5)
  328. {
  329. result = rt_pwm_set(pwm_device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  330. rt_kprintf("pwm info set on %s at channel %d\n", pwm_device, (rt_base_t)atoi(argv[2]));
  331. }
  332. else
  333. {
  334. rt_kprintf("Set info of device: [%s] error\n", pwm_device);
  335. rt_kprintf("Usage: pwm set <channel> <period> <pulse>\n");
  336. }
  337. break;
  338. case PWM_LIST_PHASE:
  339. if (argc == 4)
  340. {
  341. result = rt_pwm_set_phase(pwm_device, atoi(argv[2]), atoi(argv[3]));
  342. result_str = (result == RT_EOK) ? "success" : "failure";
  343. rt_kprintf("%s phase is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
  344. }
  345. break;
  346. case PWM_LIST_DEAD_TIME:
  347. if (argc == 4)
  348. {
  349. result = rt_pwm_set_dead_time(pwm_device, atoi(argv[2]), atoi(argv[3]));
  350. result_str = (result == RT_EOK) ? "success" : "failure";
  351. rt_kprintf("%s dead_time is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
  352. }
  353. break;
  354. default:
  355. goto _usage;
  356. }
  357. }
  358. else
  359. {
  360. goto _usage;
  361. }
  362. return result;
  363. _usage:
  364. rt_kprintf("Usage: \n");
  365. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  366. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  367. rt_kprintf("pwm disable <channel> - disable pwm channel\n");
  368. rt_kprintf("pwm get <channel> - get pwm channel info\n");
  369. rt_kprintf("pwm set <channel> <period> <pulse> - set pwm channel info\n");
  370. rt_kprintf("pwm phase <channel> <phase> - set pwm phase\n");
  371. rt_kprintf("pwm dead_time <channel> <dead_time> - set pwm dead time\n");
  372. result = -RT_ERROR;
  373. return result;
  374. }
  375. CMD_OPTIONS_NODE_START(pwm_list)
  376. CMD_OPTIONS_NODE(PWM_LIST_PROBE, probe, probe pwm by name)
  377. CMD_OPTIONS_NODE(PWM_LIST_ENABLE, enable, enable pwm channel)
  378. CMD_OPTIONS_NODE(PWM_LIST_DISABLE, disable, disable pwm channel)
  379. CMD_OPTIONS_NODE(PWM_LIST_GET, get, get pwm channel info)
  380. CMD_OPTIONS_NODE(PWM_LIST_SET, set, set pwm channel info)
  381. CMD_OPTIONS_NODE(PWM_LIST_PHASE, phase, set pwm phase)
  382. CMD_OPTIONS_NODE(PWM_LIST_DEAD_TIME, dead_time, set pwm dead time)
  383. CMD_OPTIONS_NODE_END
  384. MSH_CMD_EXPORT_ALIAS(pwm_list, pwm, control pwm device, optenable);
  385. #endif /* RT_USING_FINSH */