drv_pwm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (c) 2006-2022, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-10 Zohar_Lee first version
  9. * 2020-07-10 lik format file
  10. */
  11. #include "drv_pwm.h"
  12. #ifdef RT_USING_PWM
  13. #ifdef BSP_USING_PWM
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.pwm"
  16. #include <drv_log.h>
  17. #if !defined(BSP_USING_PWM0) && !defined(BSP_USING_PWM1) && !defined(BSP_USING_PWM2) && !defined(BSP_USING_PWM3) && !defined(BSP_USING_PWM4) && !defined(BSP_USING_PWM5)
  18. #error "Please define at least one BSP_USING_PWMx"
  19. /* this driver can be disabled at menuconfig ? RT-Thread Components ? Device Drivers */
  20. #endif
  21. #define MIN_PERIOD 2
  22. #define MIN_PULSE 1
  23. #ifdef BSP_USING_PWM0
  24. #ifndef PWM0_CFG
  25. #define PWM0_CFG \
  26. { \
  27. .name = "pwm0", \
  28. .PWMx = PWM0, \
  29. .pwm_initstruct.clk_div = PWM_CLKDIV_8, \
  30. .pwm_initstruct.mode = PWM_MODE_INDEP, \
  31. .pwm_initstruct.cycleA = 10000, \
  32. .pwm_initstruct.hdutyA = 5000, \
  33. .pwm_initstruct.initLevelA = 1, \
  34. .pwm_initstruct.cycleB = 10000, \
  35. .pwm_initstruct.hdutyB = 5000, \
  36. .pwm_initstruct.initLevelB = 1, \
  37. .pwm_initstruct.HEndAIEn = 0, \
  38. .pwm_initstruct.NCycleAIEn = 0, \
  39. .pwm_initstruct.HEndBIEn = 0, \
  40. .pwm_initstruct.NCycleBIEn = 0, \
  41. }
  42. #endif /* PWM0_CFG */
  43. #endif /* BSP_USING_PWM0 */
  44. #ifdef BSP_USING_PWM1
  45. #ifndef PWM1_CFG
  46. #define PWM1_CFG \
  47. { \
  48. .name = "pwm1", \
  49. .PWMx = PWM1, \
  50. .pwm_initstruct.clk_div = PWM_CLKDIV_8, \
  51. .pwm_initstruct.mode = PWM_MODE_INDEP, \
  52. .pwm_initstruct.cycleA = 10000, \
  53. .pwm_initstruct.hdutyA = 5000, \
  54. .pwm_initstruct.initLevelA = 1, \
  55. .pwm_initstruct.cycleB = 10000, \
  56. .pwm_initstruct.hdutyB = 5000, \
  57. .pwm_initstruct.initLevelB = 1, \
  58. .pwm_initstruct.HEndAIEn = 0, \
  59. .pwm_initstruct.NCycleAIEn = 0, \
  60. .pwm_initstruct.HEndBIEn = 0, \
  61. .pwm_initstruct.NCycleBIEn = 0, \
  62. }
  63. #endif /* PWM1_CFG */
  64. #endif /* BSP_USING_PWM1 */
  65. #ifdef BSP_USING_PWM2
  66. #ifndef PWM2_CFG
  67. #define PWM2_CFG \
  68. { \
  69. .name = "pwm2", \
  70. .PWMx = PWM2, \
  71. .pwm_initstruct.clk_div = PWM_CLKDIV_8, \
  72. .pwm_initstruct.mode = PWM_MODE_INDEP, \
  73. .pwm_initstruct.cycleA = 10000, \
  74. .pwm_initstruct.hdutyA = 5000, \
  75. .pwm_initstruct.initLevelA = 1, \
  76. .pwm_initstruct.cycleB = 10000, \
  77. .pwm_initstruct.hdutyB = 5000, \
  78. .pwm_initstruct.initLevelB = 1, \
  79. .pwm_initstruct.HEndAIEn = 0, \
  80. .pwm_initstruct.NCycleAIEn = 0, \
  81. .pwm_initstruct.HEndBIEn = 0, \
  82. .pwm_initstruct.NCycleBIEn = 0, \
  83. }
  84. #endif /* PWM2_CFG */
  85. #endif /* BSP_USING_PWM2 */
  86. #ifdef BSP_USING_PWM3
  87. #ifndef PWM3_CFG
  88. #define PWM3_CFG \
  89. { \
  90. .name = "pwm3", \
  91. .PWMx = PWM3, \
  92. .pwm_initstruct.clk_div = PWM_CLKDIV_8, \
  93. .pwm_initstruct.mode = PWM_MODE_INDEP, \
  94. .pwm_initstruct.cycleA = 10000, \
  95. .pwm_initstruct.hdutyA = 5000, \
  96. .pwm_initstruct.initLevelA = 1, \
  97. .pwm_initstruct.cycleB = 10000, \
  98. .pwm_initstruct.hdutyB = 5000, \
  99. .pwm_initstruct.initLevelB = 1, \
  100. .pwm_initstruct.HEndAIEn = 0, \
  101. .pwm_initstruct.NCycleAIEn = 0, \
  102. .pwm_initstruct.HEndBIEn = 0, \
  103. .pwm_initstruct.NCycleBIEn = 0, \
  104. }
  105. #endif /* PWM3_CFG */
  106. #endif /* BSP_USING_PWM3 */
  107. #ifdef BSP_USING_PWM4
  108. #ifndef PWM4_CFG
  109. #define PWM4_CFG \
  110. { \
  111. .name = "pwm4", \
  112. .PWMx = PWM4, \
  113. .pwm_initstruct.clk_div = PWM_CLKDIV_8, \
  114. .pwm_initstruct.mode = PWM_MODE_INDEP, \
  115. .pwm_initstruct.cycleA = 10000, \
  116. .pwm_initstruct.hdutyA = 5000, \
  117. .pwm_initstruct.initLevelA = 1, \
  118. .pwm_initstruct.cycleB = 10000, \
  119. .pwm_initstruct.hdutyB = 5000, \
  120. .pwm_initstruct.initLevelB = 1, \
  121. .pwm_initstruct.HEndAIEn = 0, \
  122. .pwm_initstruct.NCycleAIEn = 0, \
  123. .pwm_initstruct.HEndBIEn = 0, \
  124. .pwm_initstruct.NCycleBIEn = 0, \
  125. }
  126. #endif /* PWM4_CFG */
  127. #endif /* BSP_USING_PWM4 */
  128. #ifdef BSP_USING_PWM5
  129. #ifndef PWM5_CFG
  130. #define PWM5_CFG \
  131. { \
  132. .name = "pwm5", \
  133. .PWMx = PWM5, \
  134. .pwm_initstruct.clk_div = PWM_CLKDIV_8, \
  135. .pwm_initstruct.mode = PWM_MODE_INDEP, \
  136. .pwm_initstruct.cycleA = 10000, \
  137. .pwm_initstruct.hdutyA = 5000, \
  138. .pwm_initstruct.initLevelA = 1, \
  139. .pwm_initstruct.cycleB = 10000, \
  140. .pwm_initstruct.hdutyB = 5000, \
  141. .pwm_initstruct.initLevelB = 1, \
  142. .pwm_initstruct.HEndAIEn = 0, \
  143. .pwm_initstruct.NCycleAIEn = 0, \
  144. .pwm_initstruct.HEndBIEn = 0, \
  145. .pwm_initstruct.NCycleBIEn = 0, \
  146. }
  147. #endif /* PWM5_CFG */
  148. #endif /* BSP_USING_PWM5 */
  149. struct swm_pwm_cfg
  150. {
  151. const char *name;
  152. PWM_TypeDef *PWMx;
  153. PWM_InitStructure pwm_initstruct;
  154. };
  155. struct swm_pwm_device
  156. {
  157. struct swm_pwm_cfg *pwm_cfg;
  158. struct rt_device_pwm pwm_device;
  159. };
  160. static struct swm_pwm_cfg swm_pwm_cfg[] =
  161. {
  162. #ifdef BSP_USING_PWM0
  163. PWM0_CFG,
  164. #endif
  165. #ifdef BSP_USING_PWM1
  166. PWM1_CFG,
  167. #endif
  168. #ifdef BSP_USING_PWM2
  169. PWM2_CFG,
  170. #endif
  171. #ifdef BSP_USING_PWM3
  172. PWM3_CFG,
  173. #endif
  174. #ifdef BSP_USING_PWM4
  175. PWM4_CFG,
  176. #endif
  177. #ifdef BSP_USING_PWM5
  178. PWM5_CFG,
  179. #endif
  180. };
  181. static struct swm_pwm_device pwm_obj[sizeof(swm_pwm_cfg) / sizeof(swm_pwm_cfg[0])] = {0};
  182. static rt_err_t swm_pwm_enable(struct rt_device_pwm *pwm_device, struct rt_pwm_configuration *configuration, rt_bool_t enable)
  183. {
  184. struct swm_pwm_cfg *pwm_cfg = RT_NULL;
  185. RT_ASSERT(pwm_device != RT_NULL);
  186. pwm_cfg = pwm_device->parent.user_data;
  187. if (!enable)
  188. {
  189. if (PWM_CH_A == configuration->channel)
  190. {
  191. PWM_Stop(pwm_cfg->PWMx, 1, 0);
  192. }
  193. if (PWM_CH_B == configuration->channel)
  194. {
  195. PWM_Stop(pwm_cfg->PWMx, 0, 1);
  196. }
  197. }
  198. else
  199. {
  200. if (PWM_CH_A == configuration->channel)
  201. {
  202. PWM_Start(pwm_cfg->PWMx, 1, 0);
  203. }
  204. if (PWM_CH_B == configuration->channel)
  205. {
  206. PWM_Start(pwm_cfg->PWMx, 0, 1);
  207. }
  208. }
  209. return RT_EOK;
  210. }
  211. static rt_err_t swm_pwm_get(struct rt_device_pwm *pwm_device, struct rt_pwm_configuration *configuration)
  212. {
  213. rt_uint64_t tim_clock;
  214. tim_clock = SystemCoreClock / 8;
  215. struct swm_pwm_cfg *pwm_cfg = RT_NULL;
  216. RT_ASSERT(pwm_device != RT_NULL);
  217. pwm_cfg = pwm_device->parent.user_data;
  218. /* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
  219. tim_clock /= 1000000UL;
  220. configuration->period = PWM_GetCycle(pwm_cfg->PWMx, configuration->channel) * 1000UL / tim_clock;
  221. configuration->pulse = PWM_GetHDuty(pwm_cfg->PWMx, configuration->channel) * 1000UL / tim_clock;
  222. return RT_EOK;
  223. }
  224. static rt_err_t swm_pwm_set(struct rt_device_pwm *pwm_device, struct rt_pwm_configuration *configuration)
  225. {
  226. rt_uint32_t period, pulse;
  227. rt_uint64_t tim_clock;
  228. tim_clock = SystemCoreClock / 8;
  229. struct swm_pwm_cfg *pwm_cfg = RT_NULL;
  230. RT_ASSERT(pwm_device != RT_NULL);
  231. pwm_cfg = pwm_device->parent.user_data;
  232. /* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
  233. /* when SystemCoreClock = 120MHz, configuration->period max 4.369ms */
  234. /* when SystemCoreClock = 20MHz, configuration->period max 26.214ms */
  235. tim_clock /= 1000000UL;
  236. period = (unsigned long long)configuration->period * tim_clock / 1000ULL;
  237. pulse = (unsigned long long)configuration->pulse * tim_clock / 1000ULL;
  238. if (period < MIN_PERIOD)
  239. {
  240. period = MIN_PERIOD;
  241. }
  242. if (pulse < MIN_PULSE)
  243. {
  244. pulse = MIN_PULSE;
  245. }
  246. PWM_SetCycle(pwm_cfg->PWMx, configuration->channel, period);
  247. PWM_SetHDuty(pwm_cfg->PWMx, configuration->channel, pulse);
  248. return RT_EOK;
  249. }
  250. static rt_err_t swm_pwm_control(struct rt_device_pwm *pwm_device, int cmd, void *arg)
  251. {
  252. RT_ASSERT(pwm_device != RT_NULL);
  253. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
  254. switch (cmd)
  255. {
  256. case PWM_CMD_ENABLE:
  257. return swm_pwm_enable(pwm_device, configuration, RT_TRUE);
  258. case PWM_CMD_DISABLE:
  259. return swm_pwm_enable(pwm_device, configuration, RT_FALSE);
  260. case PWM_CMD_SET:
  261. return swm_pwm_set(pwm_device, configuration);
  262. case PWM_CMD_GET:
  263. return swm_pwm_get(pwm_device, configuration);
  264. default:
  265. return -RT_EINVAL;
  266. }
  267. }
  268. static struct rt_pwm_ops pwm_ops =
  269. {
  270. swm_pwm_control};
  271. int swm_pwm_init(void)
  272. {
  273. int i = 0;
  274. int result = RT_EOK;
  275. for (i = 0; i < sizeof(swm_pwm_cfg) / sizeof(swm_pwm_cfg[0]); i++)
  276. {
  277. pwm_obj[i].pwm_cfg = &swm_pwm_cfg[i];
  278. if (pwm_obj[i].pwm_cfg->PWMx == PWM0)
  279. {
  280. #ifdef BSP_USING_PWM0A
  281. PORT_Init(PORTC, PIN2, FUNMUX0_PWM0A_OUT, 0);
  282. #endif
  283. #ifdef BSP_USING_PWM0B
  284. PORT_Init(PORTC, PIN4, FUNMUX0_PWM0B_OUT, 0);
  285. #endif
  286. }
  287. else if (pwm_obj[i].pwm_cfg->PWMx == PWM1)
  288. {
  289. #ifdef BSP_USING_PWM1A
  290. PORT_Init(PORTC, PIN3, FUNMUX1_PWM1A_OUT, 0);
  291. #endif
  292. #ifdef BSP_USING_PWM1B
  293. PORT_Init(PORTC, PIN5, FUNMUX1_PWM1B_OUT, 0);
  294. #endif
  295. }
  296. else if (pwm_obj[i].pwm_cfg->PWMx == PWM2)
  297. {
  298. #ifdef BSP_USING_PWM2A
  299. PORT_Init(PORTN, PIN4, FUNMUX0_PWM2A_OUT, 0);
  300. #endif
  301. #ifdef BSP_USING_PWM2B
  302. PORT_Init(PORTN, PIN6, FUNMUX0_PWM2B_OUT, 0);
  303. #endif
  304. }
  305. else if (pwm_obj[i].pwm_cfg->PWMx == PWM3)
  306. {
  307. #ifdef BSP_USING_PWM3A
  308. PORT_Init(PORTN, PIN3, FUNMUX1_PWM3A_OUT, 0);
  309. #endif
  310. #ifdef BSP_USING_PWM3B
  311. PORT_Init(PORTN, PIN5, FUNMUX1_PWM3B_OUT, 0);
  312. #endif
  313. }
  314. else if (pwm_obj[i].pwm_cfg->PWMx == PWM4)
  315. {
  316. #ifdef BSP_USING_PWM4A
  317. PORT_Init(PORTN, PIN8, FUNMUX0_PWM4A_OUT, 0);
  318. #endif
  319. #ifdef BSP_USING_PWM4B
  320. PORT_Init(PORTN, PIN10, FUNMUX0_PWM4B_OUT, 0);
  321. #endif
  322. }
  323. else if (pwm_obj[i].pwm_cfg->PWMx == PWM5)
  324. {
  325. #ifdef BSP_USING_PWM5A
  326. PORT_Init(PORTN, PIN7, FUNMUX1_PWM5A_OUT, 0);
  327. #endif
  328. #ifdef BSP_USING_PWM5B
  329. PORT_Init(PORTN, PIN9, FUNMUX1_PWM5B_OUT, 0);
  330. #endif
  331. }
  332. PWM_Init(pwm_obj[i].pwm_cfg->PWMx, &(pwm_obj[i].pwm_cfg->pwm_initstruct));
  333. result = rt_device_pwm_register(&pwm_obj[i].pwm_device, pwm_obj[i].pwm_cfg->name, &pwm_ops, pwm_obj[i].pwm_cfg);
  334. if(result != RT_EOK)
  335. {
  336. LOG_E("%s register fail.", pwm_obj[i].pwm_cfg->name);
  337. }
  338. else
  339. {
  340. LOG_D("%s register success.", pwm_obj[i].pwm_cfg->name);
  341. }
  342. }
  343. return result;
  344. }
  345. INIT_DEVICE_EXPORT(swm_pwm_init);
  346. #endif /* BSP_USING_PWM */
  347. #endif /* RT_USING_PWM */