drv_pwm.c 6.1 KB

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