drv_pwm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2023-10-18 zhangyan first version
  11. *
  12. */
  13. #include "rtconfig.h"
  14. #ifdef BSP_USING_PWM
  15. #define LOG_TAG "pwm_drv"
  16. #include "drv_log.h"
  17. #include "drv_pwm.h"
  18. #include "fio_mux.h"
  19. #include "fpwm.h"
  20. #ifdef RT_USING_SMART
  21. #include <ioremap.h>
  22. #endif
  23. struct phytium_pwm
  24. {
  25. const char *name;
  26. FPwmCtrl pwm_handle;
  27. struct rt_device_pwm device; /* inherit from can device */
  28. };
  29. static struct phytium_pwm pwm_dev[FPWM_NUM] =
  30. {
  31. #if defined(TARGET_E2000)
  32. {
  33. .name = "PWM0",
  34. .pwm_handle.config.instance_id = 0,
  35. },
  36. {
  37. .name = "PWM1",
  38. .pwm_handle.config.instance_id = 1,
  39. },
  40. {
  41. .name = "PWM2",
  42. .pwm_handle.config.instance_id = 2,
  43. },
  44. {
  45. .name = "PWM3",
  46. .pwm_handle.config.instance_id = 3,
  47. },
  48. {
  49. .name = "PWM4",
  50. .pwm_handle.config.instance_id = 4,
  51. },
  52. {
  53. .name = "PWM5",
  54. .pwm_handle.config.instance_id = 5,
  55. },
  56. {
  57. .name = "PWM6",
  58. .pwm_handle.config.instance_id = 6,
  59. },
  60. {
  61. .name = "PWM7",
  62. .pwm_handle.config.instance_id = 7,
  63. },
  64. #elif defined(TARGET_PHYTIUMPI)
  65. {
  66. .name = "PWM2",
  67. .pwm_handle.config.instance_id = 2,
  68. },
  69. #endif
  70. };
  71. static rt_err_t drv_pwm_config(struct phytium_pwm *pwm_dev)
  72. {
  73. RT_ASSERT(pwm_dev);
  74. u32 ret;
  75. FPwmConfig *config = NULL;
  76. FPwmCtrl *pwm_handle = &pwm_dev->pwm_handle;
  77. FIOPadSetPwmMux(pwm_handle->config.instance_id, 0);
  78. FIOPadSetPwmMux(pwm_handle->config.instance_id, 1);
  79. config = FPwmLookupConfig(pwm_handle->config.instance_id);
  80. #ifdef RT_USING_SMART
  81. config->pwm_base_addr = (uintptr)rt_ioremap((void *)config->pwm_base_addr, 0x1000);
  82. config->db_base_addr = (uintptr)rt_ioremap((void *)config->db_base_addr, 0x100);
  83. #endif
  84. ret = FPwmCfgInitialize(pwm_handle, config);
  85. if (ret != FPWM_SUCCESS)
  86. {
  87. LOG_E("Pwm config init failed.\n");
  88. return RT_ERROR;
  89. }
  90. return RT_EOK;
  91. }
  92. static rt_err_t drv_pwm_enable(struct phytium_pwm *pwm_dev, struct rt_pwm_configuration *configuration, boolean enable_pwm)
  93. {
  94. RT_ASSERT(pwm_dev);
  95. RT_ASSERT(configuration);
  96. u32 channel = configuration->channel;
  97. if (enable_pwm == RT_TRUE)
  98. {
  99. FPwmEnable(&pwm_dev->pwm_handle, channel);
  100. }
  101. else
  102. {
  103. FPwmDisable(&pwm_dev->pwm_handle, channel);
  104. }
  105. return RT_EOK;
  106. }
  107. static rt_err_t drv_pwm_set(struct phytium_pwm *pwm_dev, int cmd, struct rt_pwm_configuration *configuration)
  108. {
  109. RT_ASSERT(pwm_dev);
  110. RT_ASSERT(configuration);
  111. u32 ret;
  112. FPwmVariableConfig pwm_cfg;
  113. u32 channel = configuration->channel;
  114. memset(&pwm_cfg, 0, sizeof(pwm_cfg));
  115. pwm_cfg.tim_ctrl_mode = FPWM_MODULO;
  116. pwm_cfg.tim_ctrl_div = 50 - 1;
  117. /* Precision set to microseconds */
  118. switch (cmd)
  119. {
  120. case PWM_CMD_SET:
  121. pwm_cfg.pwm_period = configuration->period / 1000;
  122. pwm_cfg.pwm_pulse = configuration->pulse / 1000;
  123. break;
  124. case PWM_CMD_SET_PERIOD:
  125. pwm_cfg.pwm_period = configuration->period / 1000;
  126. break;
  127. case PWM_CMD_SET_PULSE:
  128. pwm_cfg.pwm_pulse = configuration->pulse / 1000;
  129. break;
  130. }
  131. /* Can be modified according to function */
  132. pwm_cfg.pwm_mode = FPWM_OUTPUT_COMPARE;
  133. pwm_cfg.pwm_polarity = FPWM_POLARITY_NORMAL;
  134. pwm_cfg.pwm_duty_source_mode = FPWM_DUTY_CCR;
  135. FPwmDisable(&pwm_dev->pwm_handle, channel);
  136. ret = FPwmVariableSet(&pwm_dev->pwm_handle, channel, &pwm_cfg);
  137. if (ret != FPWM_SUCCESS)
  138. {
  139. LOG_E("Pwm variable set failed.\n");
  140. return RT_ERROR;
  141. }
  142. FPwmEnable(&pwm_dev->pwm_handle, channel);
  143. return RT_EOK;
  144. }
  145. static rt_err_t drv_pwm_get(struct phytium_pwm *pwm_dev, struct rt_pwm_configuration *configuration)
  146. {
  147. RT_ASSERT(pwm_dev);
  148. RT_ASSERT(configuration);
  149. u32 ret;
  150. FPwmVariableConfig pwm_cfg;
  151. u32 channel = configuration->channel;
  152. memset(&pwm_cfg, 0, sizeof(pwm_cfg));
  153. ret = FPwmVariableGet(&pwm_dev->pwm_handle, channel, &pwm_cfg);
  154. if (ret != FPWM_SUCCESS)
  155. {
  156. LOG_E("Pwm variable get failed.\n");
  157. return RT_ERROR;
  158. }
  159. configuration->period = pwm_cfg.pwm_period * 1000;
  160. configuration->pulse = pwm_cfg.pwm_pulse * 1000;
  161. LOG_D("period = %d\n, pulse = %d\n", configuration->period, configuration->pulse);
  162. return RT_EOK;
  163. }
  164. static rt_err_t drv_pwm_set_dead_time(struct phytium_pwm *pwm_dev, struct rt_pwm_configuration *configuration)
  165. {
  166. RT_ASSERT(pwm_dev);
  167. RT_ASSERT(configuration);
  168. u32 ret;
  169. FPwmDbVariableConfig db_cfg;
  170. u32 channel = configuration->channel;
  171. memset(&db_cfg, 0, sizeof(db_cfg));
  172. db_cfg.db_rise_cycle = configuration->dead_time / 1000;
  173. db_cfg.db_fall_cycle = configuration->dead_time / 1000;
  174. db_cfg.db_polarity_sel = FPWM_DB_AH;
  175. db_cfg.db_in_mode = FPWM_DB_IN_MODE_PWM0;
  176. db_cfg.db_out_mode = FPWM_DB_OUT_MODE_ENABLE_RISE_FALL;
  177. FPwmDisable(&pwm_dev->pwm_handle, channel);
  178. ret = FPwmDbVariableSet(&pwm_dev->pwm_handle, &db_cfg);
  179. if (ret != FPWM_SUCCESS)
  180. {
  181. LOG_E("FPwmDbVariableSet failed.");
  182. return RT_ERROR;
  183. }
  184. FPwmEnable(&pwm_dev->pwm_handle, channel);
  185. return RT_EOK;
  186. }
  187. static rt_err_t _pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  188. {
  189. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
  190. struct phytium_pwm *pwm_dev;
  191. pwm_dev = (struct phytium_pwm *)(device->parent.user_data);
  192. switch (cmd)
  193. {
  194. case PWM_CMD_ENABLE:
  195. return drv_pwm_enable(pwm_dev, configuration, RT_TRUE);
  196. case PWM_CMD_DISABLE:
  197. return drv_pwm_enable(pwm_dev, configuration, RT_FALSE);
  198. case PWM_CMD_SET:
  199. return drv_pwm_set(pwm_dev, PWM_CMD_SET, configuration);
  200. case PWM_CMD_GET:
  201. return drv_pwm_get(pwm_dev, configuration);
  202. case PWM_CMD_SET_DEAD_TIME:
  203. return drv_pwm_set_dead_time(pwm_dev, configuration);
  204. case PWM_CMD_SET_PERIOD:
  205. return drv_pwm_set(pwm_dev, PWM_CMD_SET_PERIOD, configuration);
  206. case PWM_CMD_SET_PULSE:
  207. return drv_pwm_set(pwm_dev, PWM_CMD_SET_PULSE, configuration);
  208. default:
  209. return -RT_EINVAL;
  210. }
  211. }
  212. static const struct rt_pwm_ops _pwm_ops =
  213. {
  214. _pwm_control,
  215. };
  216. static rt_err_t pwm_controller_init(u32 pwm_id)
  217. {
  218. u32 ret = RT_EOK;
  219. ret = drv_pwm_config(&pwm_dev[pwm_id]);
  220. if (ret != FPWM_SUCCESS)
  221. {
  222. LOG_E("Pwm config failed.\n");
  223. return RT_ERROR;
  224. }
  225. ret = rt_device_pwm_register(&pwm_dev[pwm_id].device,
  226. pwm_dev[pwm_id].name,
  227. &_pwm_ops,
  228. &pwm_dev[pwm_id]);
  229. RT_ASSERT(ret == RT_EOK);
  230. return ret;
  231. }
  232. int rt_hw_pwm_init(void)
  233. {
  234. #if defined(TARGET_E2000)
  235. #if defined(RT_USING_PWM0)
  236. pwm_controller_init(FPWM0_ID);
  237. #endif
  238. #if defined(RT_USING_PWM1)
  239. pwm_controller_init(FPWM1_ID);
  240. #endif
  241. #if defined(RT_USING_PWM2)
  242. pwm_controller_init(FPWM2_ID);
  243. #endif
  244. #if defined(RT_USING_PWM3)
  245. pwm_controller_init(FPWM3_ID);
  246. #endif
  247. #if defined(RT_USING_PWM4)
  248. pwm_controller_init(FPWM4_ID);
  249. #endif
  250. #if defined(RT_USING_PWM5)
  251. pwm_controller_init(FPWM5_ID);
  252. #endif
  253. #if defined(RT_USING_PWM6)
  254. pwm_controller_init(FPWM6_ID);
  255. #endif
  256. #if defined(RT_USING_PWM7)
  257. pwm_controller_init(FPWM7_ID);
  258. #endif
  259. #elif defined(TARGET_PHYTIUMPI)
  260. #if defined(RT_USING_PWM2)
  261. pwm_controller_init(FPWM2_ID);
  262. #endif
  263. #endif
  264. return 0;
  265. }
  266. INIT_DEVICE_EXPORT(rt_hw_pwm_init);
  267. #endif