rt_drv_pwm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2006-2021, 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. */
  10. #include <string.h>
  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. 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_Miscellaneous;
  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 <finsh.h>
  156. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_enable, pwm_enable, enable pwm by channel.);
  157. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_set, pwm_set, set pwm.);
  158. #ifdef RT_USING_FINSH
  159. static int pwm_enable(int argc, char **argv)
  160. {
  161. int result = 0;
  162. struct rt_device_pwm *device = RT_NULL;
  163. if (argc != 3)
  164. {
  165. rt_kprintf("Usage: pwm_enable pwm1 1\n");
  166. rt_kprintf(" pwm_enable <pwm_dev> <channel/-channel>\n");
  167. result = -RT_ERROR;
  168. goto _exit;
  169. }
  170. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  171. if (!device)
  172. {
  173. result = -RT_EIO;
  174. goto _exit;
  175. }
  176. /* If channel is complementary(1), make the channel number to nagetive */
  177. result = rt_pwm_enable(device, atoi(argv[2]));
  178. _exit:
  179. return result;
  180. }
  181. MSH_CMD_EXPORT(pwm_enable, pwm_enable <pwm_dev> <channel/-channel>);
  182. static int pwm_disable(int argc, char **argv)
  183. {
  184. int result = 0;
  185. struct rt_device_pwm *device = RT_NULL;
  186. if (argc != 3)
  187. {
  188. rt_kprintf("Usage: pwm_disable pwm1 1\n");
  189. rt_kprintf(" pwm_disable <pwm_dev> <channel/-channel> \n");
  190. result = -RT_ERROR;
  191. goto _exit;
  192. }
  193. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  194. if (!device)
  195. {
  196. result = -RT_EIO;
  197. goto _exit;
  198. }
  199. /* If channel is complementary(1), make the channel number to nagetive */
  200. result = rt_pwm_disable(device, atoi(argv[2]));
  201. _exit:
  202. return result;
  203. }
  204. MSH_CMD_EXPORT(pwm_disable, pwm_disable <pwm_dev> <channel/-channel>);
  205. static int pwm_set(int argc, char **argv)
  206. {
  207. int result = 0;
  208. struct rt_device_pwm *device = RT_NULL;
  209. if (argc != 5)
  210. {
  211. rt_kprintf("Usage: pwm_set pwm1 1 100 50\n");
  212. rt_kprintf("Usage: pwm_set <pwm_dev> <channel> <period> <pulse>\n");
  213. result = -RT_ERROR;
  214. goto _exit;
  215. }
  216. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  217. if (!device)
  218. {
  219. result = -RT_EIO;
  220. goto _exit;
  221. }
  222. result = rt_pwm_set(device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  223. _exit:
  224. return result;
  225. }
  226. MSH_CMD_EXPORT(pwm_set, pwm_set <pwm_dev> <channel> <period> <pulse>);
  227. static int pwm_get(int argc, char **argv)
  228. {
  229. int result = 0;
  230. struct rt_device_pwm *device = RT_NULL;
  231. struct rt_pwm_configuration cfg = {0};
  232. if (argc != 3)
  233. {
  234. rt_kprintf("Usage: pwm_get pwm1 1\n");
  235. rt_kprintf(" pwm_get <pwm_dev> <channel>\n");
  236. result = -RT_ERROR;
  237. goto _exit;
  238. }
  239. device = (struct rt_device_pwm *)rt_device_find(argv[1]);
  240. if (!device)
  241. {
  242. result = -RT_EIO;
  243. goto _exit;
  244. }
  245. cfg.channel = atoi(argv[2]);
  246. result = rt_pwm_get(device, &cfg);
  247. if (result != RT_EOK)
  248. {
  249. rt_kprintf("Get info of device: [%s] error.\n", argv[1]);
  250. }
  251. else
  252. {
  253. rt_kprintf("Get info of device: [%s]:\n", argv[1]);
  254. rt_kprintf("period : %d\n", cfg.period);
  255. rt_kprintf("pulse : %d\n", cfg.pulse);
  256. rt_kprintf("Duty cycle : %d%%\n", (int)(((double)(cfg.pulse)/(cfg.period)) * 100));
  257. }
  258. _exit:
  259. return result;
  260. }
  261. MSH_CMD_EXPORT(pwm_get, pwm_get <pwm_dev> <channel>);
  262. #endif /* RT_USING_FINSH */
  263. #endif /* RT_USING_FINSH */