1
0

rt_drv_pwm.c 8.0 KB

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