drv_pwm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * 2022-07-13 Rbb666 first version
  9. */
  10. #include "drv_pwm.h"
  11. #ifdef RT_USING_PWM
  12. #include <drivers/rt_drv_pwm.h>
  13. #include "drv_gpio.h"
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.pwm"
  16. #include <drv_log.h>
  17. struct rt_device_pwm pwm_device;
  18. struct ifx_pwm
  19. {
  20. struct rt_device_pwm pwm_device;
  21. cyhal_pwm_t *pwm_obj;
  22. rt_uint8_t channel;
  23. char *name;
  24. rt_uint8_t gpio;
  25. };
  26. static struct ifx_pwm ifx_pwm_obj[] =
  27. {
  28. #ifdef BSP_USING_PWM0_PORT0
  29. PWM0_CH0_PORT0_CONFIG,
  30. #endif
  31. #ifdef BSP_USING_PWM0_PORT2
  32. PWM0_CH7_PORT2_CONFIG,
  33. #endif
  34. #ifdef BSP_USING_PWM0_PORT5
  35. PWM0_CH7_PORT5_CONFIG,
  36. #endif
  37. #ifdef BSP_USING_PWM0_PORT7
  38. PWM0_CH7_PORT7_CONFIG,
  39. #endif
  40. #ifdef BSP_USING_PWM0_PORT9
  41. PWM0_CH7_PORT9_CONFIG,
  42. #endif
  43. #ifdef BSP_USING_PWM0_PORT10
  44. PWM0_CH7_PORT10_CONFIG,
  45. #endif
  46. #ifdef BSP_USING_PWM0_PORT12
  47. PWM0_CH7_PORT12_CONFIG,
  48. #endif
  49. #ifdef BSP_USING_PWM0_PORT13
  50. PWM0_CH3_PORT13_CONFIG,
  51. #endif
  52. };
  53. static rt_err_t drv_pwm_enable(cyhal_pwm_t *htim, struct rt_pwm_configuration *configuration, rt_bool_t enable)
  54. {
  55. /* get the value of channel */
  56. rt_uint32_t channel = configuration->channel;
  57. if (!configuration->complementary || configuration->complementary)
  58. {
  59. if (!enable)
  60. {
  61. if (channel == 3)
  62. {
  63. htim->tcpwm.resource.channel_num = channel;
  64. }
  65. else if (channel == 7)
  66. {
  67. htim->tcpwm.resource.channel_num = channel;
  68. }
  69. cyhal_pwm_stop(htim);
  70. }
  71. else
  72. {
  73. if (channel == 3)
  74. {
  75. htim->tcpwm.resource.channel_num = channel;
  76. }
  77. else if (channel == 7)
  78. {
  79. htim->tcpwm.resource.channel_num = channel;
  80. }
  81. cyhal_pwm_start(htim);
  82. }
  83. }
  84. return RT_EOK;
  85. }
  86. static rt_err_t drv_pwm_set(cyhal_pwm_t *htim, struct rt_pwm_configuration *configuration)
  87. {
  88. rt_uint64_t tim_clock;
  89. rt_uint32_t period, pulse;
  90. tim_clock = (rt_uint32_t)(htim->tcpwm.clock_hz);
  91. htim->tcpwm.resource.channel_num = configuration->channel;
  92. period = (unsigned long long)configuration->period / 1000ULL;
  93. pulse = (unsigned long long)configuration->pulse / 1000ULL;
  94. cyhal_pwm_set_period(htim, period, pulse);
  95. return RT_EOK;
  96. }
  97. static rt_err_t drv_pwm_get(cyhal_pwm_t *htim, struct rt_pwm_configuration *configuration)
  98. {
  99. uint32_t Period = Cy_TCPWM_PWM_GetPeriod0(htim->tcpwm.base, _CYHAL_TCPWM_CNT_NUMBER(htim->tcpwm.resource));
  100. uint32_t Compare = Cy_TCPWM_PWM_GetCounter(htim->tcpwm.base, _CYHAL_TCPWM_CNT_NUMBER(htim->tcpwm.resource));
  101. configuration->period = Period;
  102. configuration->pulse = Compare;
  103. return RT_EOK;
  104. }
  105. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  106. {
  107. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
  108. cyhal_pwm_t *htim = (cyhal_pwm_t *)device->parent.user_data;
  109. switch (cmd)
  110. {
  111. case PWMN_CMD_ENABLE:
  112. configuration->complementary = RT_TRUE;
  113. case PWM_CMD_ENABLE:
  114. return drv_pwm_enable(htim, configuration, RT_TRUE);
  115. case PWMN_CMD_DISABLE:
  116. configuration->complementary = RT_FALSE;
  117. case PWM_CMD_DISABLE:
  118. return drv_pwm_enable(htim, configuration, RT_FALSE);
  119. case PWM_CMD_SET:
  120. return drv_pwm_set(htim, configuration);
  121. case PWM_CMD_GET:
  122. return drv_pwm_get(htim, configuration);
  123. default:
  124. return -RT_EINVAL;
  125. }
  126. }
  127. static struct rt_pwm_ops drv_ops = {drv_pwm_control};
  128. static rt_err_t ifx_hw_pwm_init(struct ifx_pwm *device)
  129. {
  130. rt_err_t result = RT_EOK;
  131. RT_ASSERT(device != RT_NULL);
  132. if (cyhal_pwm_init_adv(device->pwm_obj, device->gpio, NC, CYHAL_PWM_LEFT_ALIGN, true, 0u, false, RT_NULL) != RT_EOK)
  133. {
  134. LOG_E("%s channel%d config failed", device->name, device->channel);
  135. result = -RT_ERROR;
  136. goto __exit;
  137. }
  138. __exit:
  139. return result;
  140. }
  141. static int rt_hw_pwm_init(void)
  142. {
  143. int i;
  144. int result = RT_EOK;
  145. for (i = 0; i < sizeof(ifx_pwm_obj) / sizeof(ifx_pwm_obj[0]); i++)
  146. {
  147. ifx_pwm_obj[i].pwm_obj = rt_malloc(sizeof(cyhal_pwm_t));
  148. RT_ASSERT(ifx_pwm_obj[i].pwm_obj != RT_NULL);
  149. /* pwm init */
  150. if (ifx_hw_pwm_init(&ifx_pwm_obj[i]) != RT_EOK)
  151. {
  152. LOG_E("%s init failed", ifx_pwm_obj[i].name);
  153. result = -RT_ERROR;
  154. goto __exit;
  155. }
  156. else
  157. {
  158. if (rt_device_pwm_register(&ifx_pwm_obj[i].pwm_device, ifx_pwm_obj[i].name, &drv_ops, ifx_pwm_obj[i].pwm_obj) == RT_EOK)
  159. {
  160. LOG_D("%s register success", ifx_pwm_obj[i].name);
  161. }
  162. else
  163. {
  164. LOG_D("%s register failed", ifx_pwm_obj[i].name);
  165. result = -RT_ERROR;
  166. }
  167. }
  168. }
  169. __exit:
  170. return result;
  171. }
  172. INIT_BOARD_EXPORT(rt_hw_pwm_init);
  173. #define PWM_DEV_NAME "pwm0"
  174. #define PWM_DEV_CHANNEL 7
  175. struct rt_device_pwm *pwm_dev;
  176. static int pwm_sample(int argc, char *argv[])
  177. {
  178. rt_uint32_t period, pulse, dir;
  179. period = 1 * 1000 * 1000;
  180. dir = 1;
  181. pulse = 0;
  182. pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
  183. if (pwm_dev == RT_NULL)
  184. {
  185. rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
  186. return -RT_ERROR;
  187. }
  188. rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
  189. rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
  190. rt_kprintf("Now PWM[%s] Channel[%d] Period[%d] Pulse[%d]\n", PWM_DEV_NAME, PWM_DEV_CHANNEL, period, pulse);
  191. while (1)
  192. {
  193. rt_thread_mdelay(50);
  194. if (dir)
  195. {
  196. pulse += 100000;
  197. }
  198. else
  199. {
  200. pulse -= 100000;
  201. }
  202. if (pulse >= period)
  203. {
  204. dir = 0;
  205. }
  206. if (0 == pulse)
  207. {
  208. dir = 1;
  209. }
  210. rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
  211. }
  212. }
  213. MSH_CMD_EXPORT(pwm_sample, <pwm0> channel7 sample);
  214. #endif