rt_drv_pwm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * File : rt_drv_pwm.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-07 aozima the first version
  23. */
  24. #include <string.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
  28. {
  29. rt_err_t result = RT_EOK;
  30. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  31. if (pwm->ops->control)
  32. {
  33. result = pwm->ops->control(pwm, cmd, args);
  34. }
  35. return result;
  36. }
  37. /*
  38. pos: channel
  39. void *buffer: rt_uint32_t pulse[size]
  40. size : number of pulse, only set to sizeof(rt_uint32_t).
  41. */
  42. static rt_size_t _pwm_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  43. {
  44. rt_err_t result = RT_EOK;
  45. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  46. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  47. struct rt_pwm_configuration configuration = {0};
  48. configuration.channel = pos;
  49. if (pwm->ops->control)
  50. {
  51. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  52. if (result != RT_EOK)
  53. {
  54. return 0;
  55. }
  56. *pulse = configuration.pulse;
  57. }
  58. return size;
  59. }
  60. /*
  61. pos: channel
  62. void *buffer: rt_uint32_t pulse[size]
  63. size : number of pulse, only set to sizeof(rt_uint32_t).
  64. */
  65. static rt_size_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  66. {
  67. rt_err_t result = RT_EOK;
  68. struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
  69. rt_uint32_t *pulse = (rt_uint32_t *)buffer;
  70. struct rt_pwm_configuration configuration = {0};
  71. configuration.channel = pos;
  72. if (pwm->ops->control)
  73. {
  74. result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
  75. if (result != RT_EOK)
  76. {
  77. return 0;
  78. }
  79. configuration.pulse = *pulse;
  80. result = pwm->ops->control(pwm, PWM_CMD_SET, &configuration);
  81. if (result != RT_EOK)
  82. {
  83. return 0;
  84. }
  85. }
  86. return size;
  87. }
  88. 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)
  89. {
  90. rt_err_t result = RT_EOK;
  91. memset(device, 0, sizeof(struct rt_device_pwm));
  92. device->parent.type = RT_Device_Class_Miscellaneous;
  93. device->parent.init = RT_NULL;
  94. device->parent.open = RT_NULL;
  95. device->parent.close = RT_NULL;
  96. device->parent.read = _pwm_read;
  97. device->parent.write = _pwm_write;
  98. device->parent.control = _pwm_control;
  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(int channel)
  105. {
  106. rt_err_t result = RT_EOK;
  107. struct rt_device *device = rt_device_find("pwm");
  108. struct rt_pwm_configuration configuration = {0};
  109. if (!device)
  110. {
  111. return -RT_EIO;
  112. }
  113. configuration.channel = channel;
  114. result = rt_device_control(device, PWM_CMD_ENABLE, &configuration);
  115. return result;
  116. }
  117. rt_err_t rt_pwm_set(int channel, rt_uint32_t period, rt_uint32_t pulse)
  118. {
  119. rt_err_t result = RT_EOK;
  120. struct rt_device *device = rt_device_find("pwm");
  121. struct rt_pwm_configuration configuration = {0};
  122. if (!device)
  123. {
  124. return -RT_EIO;
  125. }
  126. configuration.channel = channel;
  127. configuration.period = period;
  128. configuration.pulse = pulse;
  129. result = rt_device_control(device, PWM_CMD_SET, &configuration);
  130. return result;
  131. }
  132. #ifdef RT_USING_FINSH
  133. #include <finsh.h>
  134. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_enable, pwm_enable, enable pwm by channel.);
  135. FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_set, pwm_set, set pwm.);
  136. #ifdef FINSH_USING_MSH
  137. static int pwm_enable(int argc, char **argv)
  138. {
  139. int result = 0;
  140. if (argc != 2)
  141. {
  142. rt_kprintf("Usage: pwm_enable 1\n");
  143. result = -RT_ERROR;
  144. goto _exit;
  145. }
  146. result = rt_pwm_enable(atoi(argv[1]));
  147. _exit:
  148. return result;
  149. }
  150. MSH_CMD_EXPORT(pwm_enable, pwm_enable 1);
  151. static int pwm_set(int argc, char **argv)
  152. {
  153. int result = 0;
  154. if (argc != 4)
  155. {
  156. rt_kprintf("Usage: pwm_set 1 100 50\n");
  157. result = -RT_ERROR;
  158. goto _exit;
  159. }
  160. result = rt_pwm_set(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
  161. _exit:
  162. return result;
  163. }
  164. MSH_CMD_EXPORT(pwm_set, pwm_set 1 100 50);
  165. #endif /* FINSH_USING_MSH */
  166. #endif /* RT_USING_FINSH */