rt_drv_pwm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. */
  14. #include <rtdevice.h>
  15. static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
  16. {
  17. rt_err_t result = RT_EOK;
  18. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  19. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)args;
  20. switch (cmd)
  21. {
  22. case PWMN_CMD_ENABLE:
  23. configuration->complementary = RT_TRUE;
  24. break;
  25. case PWMN_CMD_DISABLE:
  26. configuration->complementary = RT_FALSE;
  27. break;
  28. default:
  29. if(pwm->ops->control)
  30. result = pwm->ops->control(pwm, cmd, args);
  31. break;
  32. }
  33. return result;
  34. }
  35. /*
  36. pos: channel
  37. void *buffer: rt_uint32_t pulse[size]
  38. size : number of pulse, only set to sizeof(rt_uint32_t).
  39. */
  40. static rt_ssize_t _pwm_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  41. {
  42. rt_err_t result = RT_EOK;
  43. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  44. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  45. struct rt_pwm_configuration configuration = {0};
  46. configuration.channel = (pos > 0) ? (pos) : (-pos);
  47. if (pwm->ops->control)
  48. {
  49. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  50. if (result != RT_EOK)
  51. {
  52. return 0;
  53. }
  54. *pulse = configuration.pulse;
  55. }
  56. return size;
  57. }
  58. /*
  59. pos: channel
  60. void *buffer: rt_uint32_t pulse[size]
  61. size : number of pulse, only set to sizeof(rt_uint32_t).
  62. */
  63. static rt_ssize_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  64. {
  65. rt_err_t result = RT_EOK;
  66. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  67. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  68. struct rt_pwm_configuration configuration = {0};
  69. configuration.channel = (pos > 0) ? (pos) : (-pos);
  70. if (pwm->ops->control)
  71. {
  72. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  73. if (result != RT_EOK)
  74. {
  75. return 0;
  76. }
  77. configuration.pulse = *pulse;
  78. result = pwm->ops->control(pwm, PWM_CMD_SET, &configuration);
  79. if (result != RT_EOK)
  80. {
  81. return 0;
  82. }
  83. }
  84. return size;
  85. }
  86. #ifdef RT_USING_DEVICE_OPS
  87. static const struct rt_device_ops pwm_device_ops =
  88. {
  89. RT_NULL,
  90. RT_NULL,
  91. RT_NULL,
  92. _pwm_read,
  93. _pwm_write,
  94. _pwm_control
  95. };
  96. #endif /* RT_USING_DEVICE_OPS */
  97. 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)
  98. {
  99. rt_err_t result = RT_EOK;
  100. rt_memset(device, 0, sizeof(struct rt_device_pwm));
  101. #ifdef RT_USING_DEVICE_OPS
  102. device->parent.ops = &pwm_device_ops;
  103. #else
  104. device->parent.init = RT_NULL;
  105. device->parent.open = RT_NULL;
  106. device->parent.close = RT_NULL;
  107. device->parent.read = _pwm_read;
  108. device->parent.write = _pwm_write;
  109. device->parent.control = _pwm_control;
  110. #endif /* RT_USING_DEVICE_OPS */
  111. device->parent.type = RT_Device_Class_PWM;
  112. device->ops = ops;
  113. device->parent.user_data = (void *)user_data;
  114. result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
  115. return result;
  116. }
  117. rt_err_t rt_pwm_enable(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. /* Make it is positive num forever */
  126. configuration.channel = (channel > 0) ? (channel) : (-channel);
  127. /* If channel is a positive number (0 ~ n), it means using normal output pin.
  128. * If channel is a negative number (0 ~ -n), it means using complementary output pin. */
  129. if(channel > 0)
  130. {
  131. result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
  132. }
  133. else
  134. {
  135. result = rt_device_control(&device->parent, PWMN_CMD_ENABLE, &configuration);
  136. }
  137. result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
  138. return result;
  139. }
  140. rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
  141. {
  142. rt_err_t result = RT_EOK;
  143. struct rt_pwm_configuration configuration = {0};
  144. if (!device)
  145. {
  146. return -RT_EIO;
  147. }
  148. /* Make it is positive num forever */
  149. configuration.channel = (channel > 0) ? (channel) : (-channel);
  150. /* If channel is a positive number (0 ~ n), it means using normal output pin.
  151. * If channel is a negative number (0 ~ -n), it means using complementary output pin. */
  152. if(channel > 0)
  153. {
  154. result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
  155. }
  156. else
  157. {
  158. result = rt_device_control(&device->parent, PWMN_CMD_ENABLE, &configuration);
  159. }
  160. result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
  161. return result;
  162. }
  163. rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
  164. {
  165. rt_err_t result = RT_EOK;
  166. struct rt_pwm_configuration configuration = {0};
  167. if (!device)
  168. {
  169. return -RT_EIO;
  170. }
  171. configuration.channel = (channel > 0) ? (channel) : (-channel);
  172. configuration.period = period;
  173. configuration.pulse = pulse;
  174. result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);
  175. return result;
  176. }
  177. rt_err_t rt_pwm_set_period(struct rt_device_pwm *device, int channel, rt_uint32_t period)
  178. {
  179. rt_err_t result = RT_EOK;
  180. struct rt_pwm_configuration configuration = {0};
  181. if (!device)
  182. {
  183. return -RT_EIO;
  184. }
  185. configuration.channel = (channel > 0) ? (channel) : (-channel);
  186. configuration.period = period;
  187. result = rt_device_control(&device->parent, PWM_CMD_SET_PERIOD, &configuration);
  188. return result;
  189. }
  190. rt_err_t rt_pwm_set_pulse(struct rt_device_pwm *device, int channel, rt_uint32_t pulse)
  191. {
  192. rt_err_t result = RT_EOK;
  193. struct rt_pwm_configuration configuration = {0};
  194. if (!device)
  195. {
  196. return -RT_EIO;
  197. }
  198. configuration.channel = (channel > 0) ? (channel) : (-channel);
  199. configuration.pulse = pulse;
  200. result = rt_device_control(&device->parent, PWM_CMD_SET_PULSE, &configuration);
  201. return result;
  202. }
  203. rt_err_t rt_pwm_set_dead_time(struct rt_device_pwm *device, int channel, rt_uint32_t dead_time)
  204. {
  205. rt_err_t result = RT_EOK;
  206. struct rt_pwm_configuration configuration = {0};
  207. if (!device)
  208. {
  209. return -RT_EIO;
  210. }
  211. configuration.channel = (channel > 0) ? (channel) : (-channel);
  212. configuration.dead_time = dead_time;
  213. result = rt_device_control(&device->parent, PWM_CMD_SET_DEAD_TIME, &configuration);
  214. return result;
  215. }
  216. rt_err_t rt_pwm_set_phase(struct rt_device_pwm *device, int channel, rt_uint32_t phase)
  217. {
  218. rt_err_t result = RT_EOK;
  219. struct rt_pwm_configuration configuration = {0};
  220. if (!device)
  221. {
  222. return -RT_EIO;
  223. }
  224. configuration.channel = (channel > 0) ? (channel) : (-channel);
  225. configuration.phase = phase;
  226. result = rt_device_control(&device->parent, PWM_CMD_SET_PHASE, &configuration);
  227. return result;
  228. }
  229. rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *cfg)
  230. {
  231. rt_err_t result = RT_EOK;
  232. if (!device)
  233. {
  234. return -RT_EIO;
  235. }
  236. result = rt_device_control(&device->parent, PWM_CMD_GET, cfg);
  237. return result;
  238. }
  239. #ifdef RT_USING_FINSH
  240. #include <stdlib.h>
  241. #include <string.h>
  242. #include <finsh.h>
  243. static int pwm(int argc, char **argv)
  244. {
  245. rt_err_t result = -RT_ERROR;
  246. char *result_str;
  247. static struct rt_device_pwm *pwm_device = RT_NULL;
  248. struct rt_pwm_configuration cfg = {0};
  249. if(argc > 1)
  250. {
  251. if(!strcmp(argv[1], "probe"))
  252. {
  253. if(argc == 3)
  254. {
  255. pwm_device = (struct rt_device_pwm *)rt_device_find(argv[2]);
  256. result_str = (pwm_device == RT_NULL) ? "failure" : "success";
  257. rt_kprintf("probe %s %s\n", argv[2], result_str);
  258. }
  259. else
  260. {
  261. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  262. }
  263. }
  264. else
  265. {
  266. if(pwm_device == RT_NULL)
  267. {
  268. rt_kprintf("Please using 'pwm probe <device name>' first.\n");
  269. return -RT_ERROR;
  270. }
  271. if(!strcmp(argv[1], "enable"))
  272. {
  273. if(argc == 3)
  274. {
  275. result = rt_pwm_enable(pwm_device, atoi(argv[2]));
  276. result_str = (result == RT_EOK) ? "success" : "failure";
  277. rt_kprintf("%s channel %d is enabled %s \n", pwm_device->parent.parent.name, atoi(argv[2]), result_str);
  278. }
  279. else
  280. {
  281. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  282. rt_kprintf(" e.g. MSH >pwm enable 1 - PWM_CH1 nomal\n");
  283. rt_kprintf(" e.g. MSH >pwm enable -1 - PWM_CH1N complememtary\n");
  284. }
  285. }
  286. else if(!strcmp(argv[1], "disable"))
  287. {
  288. if(argc == 3)
  289. {
  290. result = rt_pwm_disable(pwm_device, atoi(argv[2]));
  291. }
  292. else
  293. {
  294. rt_kprintf("pwm disable <channel> - disable pwm channel\n");
  295. }
  296. }
  297. else if(!strcmp(argv[1], "get"))
  298. {
  299. cfg.channel = atoi(argv[2]);
  300. result = rt_pwm_get(pwm_device, &cfg);
  301. if(result == RT_EOK)
  302. {
  303. rt_kprintf("Info of device [%s] channel [%d]:\n",pwm_device, atoi(argv[2]));
  304. rt_kprintf("period : %d\n", cfg.period);
  305. rt_kprintf("pulse : %d\n", cfg.pulse);
  306. rt_kprintf("Duty cycle : %d%%\n",(int)(((double)(cfg.pulse)/(cfg.period)) * 100));
  307. }
  308. else
  309. {
  310. rt_kprintf("Get info of device: [%s] error.\n", pwm_device);
  311. }
  312. }
  313. else if (!strcmp(argv[1], "set"))
  314. {
  315. if(argc == 5)
  316. {
  317. result = rt_pwm_set(pwm_device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  318. rt_kprintf("pwm info set on %s at channel %d\n",pwm_device,(rt_base_t)atoi(argv[2]));
  319. }
  320. else
  321. {
  322. rt_kprintf("Set info of device: [%s] error\n", pwm_device);
  323. rt_kprintf("Usage: pwm set <channel> <period> <pulse>\n");
  324. }
  325. }
  326. else if(!strcmp(argv[1], "phase"))
  327. {
  328. if(argc == 4)
  329. {
  330. result = rt_pwm_set_phase(pwm_device, atoi(argv[2]),atoi(argv[3]));
  331. result_str = (result == RT_EOK) ? "success" : "failure";
  332. rt_kprintf("%s phase is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
  333. }
  334. }
  335. else if(!strcmp(argv[1], "dead_time"))
  336. {
  337. if(argc == 4)
  338. {
  339. result = rt_pwm_set_dead_time(pwm_device, atoi(argv[2]),atoi(argv[3]));
  340. result_str = (result == RT_EOK) ? "success" : "failure";
  341. rt_kprintf("%s dead_time is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
  342. }
  343. }
  344. else
  345. {
  346. rt_kprintf("Usage: \n");
  347. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  348. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  349. rt_kprintf("pwm disable <channel> - disable pwm channel\n");
  350. rt_kprintf("pwm get <channel> - get pwm channel info\n");
  351. rt_kprintf("pwm set <channel> <period> <pulse> - set pwm channel info\n");
  352. rt_kprintf("pwm phase <channel> <phase> - set pwm phase\n");
  353. rt_kprintf("pwm dead_time <channel> <dead_time> - set pwm dead time\n");
  354. result = -RT_ERROR;
  355. }
  356. }
  357. }
  358. else
  359. {
  360. rt_kprintf("Usage: \n");
  361. rt_kprintf("pwm probe <device name> - probe pwm by name\n");
  362. rt_kprintf("pwm enable <channel> - enable pwm channel\n");
  363. rt_kprintf("pwm disable <channel> - disable pwm channel\n");
  364. rt_kprintf("pwm get <channel> - get pwm channel info\n");
  365. rt_kprintf("pwm set <channel> <period> <pulse> - set pwm channel info\n");
  366. rt_kprintf("pwm phase <channel> <phase> - set pwm phase\n");
  367. rt_kprintf("pwm dead_time <channel> <dead_time> - set pwm dead time\n");
  368. result = -RT_ERROR;
  369. }
  370. return RT_EOK;
  371. }
  372. MSH_CMD_EXPORT(pwm, pwm [option]);
  373. #endif /* RT_USING_FINSH */