drv_pwm_ch32f20x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-01-21 charlown first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <board.h>
  13. #ifdef BSP_USING_PWM
  14. #define LOG_TAG "drv.pwm"
  15. #include <drv_log.h>
  16. #ifndef ITEM_NUM
  17. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  18. #endif
  19. #define MAX_COUNTER 65535
  20. #define MIN_COUNTER 2
  21. #define MIN_PULSE 2
  22. struct rtdevice_pwm_device
  23. {
  24. struct rt_device_pwm parent;
  25. TIM_TypeDef *periph;
  26. rt_uint8_t channel[4];
  27. char *name;
  28. };
  29. /*
  30. * channel = 0xFF: the channel is not use.
  31. */
  32. struct rtdevice_pwm_device pwm_device_list[] =
  33. {
  34. #ifdef BSP_USING_TIM1_PWM
  35. {
  36. .periph = TIM1,
  37. .name = "pwm1",
  38. #ifdef BSP_USING_TIM1_PWM_CH1
  39. .channel[0] = TIM_Channel_1,
  40. #else
  41. .channel[0] = 0xFF,
  42. #endif
  43. #ifdef BSP_USING_TIM1_PWM_CH2
  44. .channel[1] = TIM_Channel_2,
  45. #else
  46. .channel[1] = 0xFF,
  47. #endif
  48. #ifdef BSP_USING_TIM1_PWM_CH3
  49. .channel[2] = TIM_Channel_3,
  50. #else
  51. .channel[2] = 0xFF,
  52. #endif
  53. #ifdef BSP_USING_TIM1_PWM_CH4
  54. .channel[3] = TIM_Channel_4,
  55. #else
  56. .channel[3] = 0xFF,
  57. #endif
  58. },
  59. #endif
  60. #ifdef BSP_USING_TIM2_PWM
  61. {
  62. .periph = TIM2,
  63. .name = "pwm2",
  64. #ifdef BSP_USING_TIM2_PWM_CH1
  65. .channel[0] = TIM_Channel_1,
  66. #else
  67. .channel[0] = 0xFF,
  68. #endif
  69. #ifdef BSP_USING_TIM2_PWM_CH2
  70. .channel[1] = TIM_Channel_2,
  71. #else
  72. .channel[1] = 0xFF,
  73. #endif
  74. #ifdef BSP_USING_TIM2_PWM_CH3
  75. .channel[2] = TIM_Channel_3,
  76. #else
  77. .channel[2] = 0xFF,
  78. #endif
  79. #ifdef BSP_USING_TIM2_PWM_CH4
  80. .channel[3] = TIM_Channel_4,
  81. #else
  82. .channel[3] = 0xFF,
  83. #endif
  84. },
  85. #endif
  86. #ifdef BSP_USING_TIM3_PWM
  87. {
  88. .periph = TIM3,
  89. .name = "pwm3",
  90. #ifdef BSP_USING_TIM3_PWM_CH1
  91. .channel[0] = TIM_Channel_1,
  92. #else
  93. .channel[0] = 0xFF,
  94. #endif
  95. #ifdef BSP_USING_TIM3_PWM_CH2
  96. .channel[1] = TIM_Channel_2,
  97. #else
  98. .channel[1] = 0xFF,
  99. #endif
  100. #ifdef BSP_USING_TIM3_PWM_CH3
  101. .channel[2] = TIM_Channel_3,
  102. #else
  103. .channel[2] = 0xFF,
  104. #endif
  105. #ifdef BSP_USING_TIM3_PWM_CH4
  106. .channel[3] = TIM_Channel_4,
  107. #else
  108. .channel[3] = 0xFF,
  109. #endif
  110. },
  111. #endif
  112. #ifdef BSP_USING_TIM4_PWM
  113. {
  114. .periph = TIM4,
  115. .name = "pwm4",
  116. #ifdef BSP_USING_TIM4_PWM_CH1
  117. .channel[0] = TIM_Channel_1,
  118. #else
  119. .channel[0] = 0xFF,
  120. #endif
  121. #ifdef BSP_USING_TIM4_PWM_CH2
  122. .channel[1] = TIM_Channel_2,
  123. #else
  124. .channel[1] = 0xFF,
  125. #endif
  126. #ifdef BSP_USING_TIM4_PWM_CH3
  127. .channel[2] = TIM_Channel_3,
  128. #else
  129. .channel[2] = 0xFF,
  130. #endif
  131. #ifdef BSP_USING_TIM4_PWM_CH4
  132. .channel[3] = TIM_Channel_4,
  133. #else
  134. .channel[3] = 0xFF,
  135. #endif
  136. },
  137. #endif
  138. #ifdef BSP_USING_TIM5_PWM
  139. {
  140. .periph = TIM5,
  141. .name = "pwm5",
  142. #ifdef BSP_USING_TIM5_PWM_CH1
  143. .channel[0] = TIM_Channel_1,
  144. #else
  145. .channel[0] = 0xFF,
  146. #endif
  147. #ifdef BSP_USING_TIM5_PWM_CH2
  148. .channel[1] = TIM_Channel_2,
  149. #else
  150. .channel[1] = 0xFF,
  151. #endif
  152. #ifdef BSP_USING_TIM5_PWM_CH3
  153. .channel[2] = TIM_Channel_3,
  154. #else
  155. .channel[2] = 0xFF,
  156. #endif
  157. #ifdef BSP_USING_TIM5_PWM_CH4
  158. .channel[3] = TIM_Channel_4,
  159. #else
  160. .channel[3] = 0xFF,
  161. #endif
  162. },
  163. #endif
  164. #ifdef BSP_USING_TIM8_PWM
  165. {
  166. .periph = TIM8,
  167. .name = "pwm8",
  168. #ifdef BSP_USING_TIM8_PWM_CH1
  169. .channel[0] = TIM_Channel_1,
  170. #else
  171. .channel[0] = 0xFF,
  172. #endif
  173. #ifdef BSP_USING_TIM8_PWM_CH2
  174. .channel[1] = TIM_Channel_2,
  175. #else
  176. .channel[1] = 0xFF,
  177. #endif
  178. #ifdef BSP_USING_TIM8_PWM_CH3
  179. .channel[2] = TIM_Channel_3,
  180. #else
  181. .channel[2] = 0xFF,
  182. #endif
  183. #ifdef BSP_USING_TIM8_PWM_CH4
  184. .channel[3] = TIM_Channel_4,
  185. #else
  186. .channel[3] = 0xFF,
  187. #endif
  188. },
  189. #endif
  190. #ifdef BSP_USING_TIM9_PWM
  191. {
  192. .periph = TIM9,
  193. .name = "pwm9",
  194. #ifdef BSP_USING_TIM9_PWM_CH1
  195. .channel[0] = TIM_Channel_1,
  196. #else
  197. .channel[0] = 0xFF,
  198. #endif
  199. #ifdef BSP_USING_TIM9_PWM_CH2
  200. .channel[1] = TIM_Channel_2,
  201. #else
  202. .channel[1] = 0xFF,
  203. #endif
  204. #ifdef BSP_USING_TIM9_PWM_CH3
  205. .channel[2] = TIM_Channel_3,
  206. #else
  207. .channel[2] = 0xFF,
  208. #endif
  209. #ifdef BSP_USING_TIM9_PWM_CH4
  210. .channel[3] = TIM_Channel_4,
  211. #else
  212. .channel[3] = 0xFF,
  213. #endif
  214. },
  215. #endif
  216. #ifdef BSP_USING_TIM10_PWM
  217. {
  218. .periph = TIM10,
  219. .name = "pwm10",
  220. #ifdef BSP_USING_TIM10_PWM_CH1
  221. .channel[0] = TIM_Channel_1,
  222. #else
  223. .channel[0] = 0xFF,
  224. #endif
  225. #ifdef BSP_USING_TIM10_PWM_CH2
  226. .channel[1] = TIM_Channel_2,
  227. #else
  228. .channel[1] = 0xFF,
  229. #endif
  230. #ifdef BSP_USING_TIM10_PWM_CH3
  231. .channel[2] = TIM_Channel_3,
  232. #else
  233. .channel[2] = 0xFF,
  234. #endif
  235. #ifdef BSP_USING_TIM10_PWM_CH4
  236. .channel[3] = TIM_Channel_4,
  237. #else
  238. .channel[3] = 0xFF,
  239. #endif
  240. },
  241. #endif
  242. };
  243. static rt_err_t ch32f2_pwm_device_enable(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration, rt_bool_t enable)
  244. {
  245. struct rtdevice_pwm_device *pwm_device;
  246. rt_uint32_t channel_index;
  247. rt_uint16_t ccx_state;
  248. pwm_device = (struct rtdevice_pwm_device *)device;
  249. channel_index = configuration->channel;
  250. if (enable == RT_TRUE)
  251. {
  252. ccx_state = TIM_CCx_Enable;
  253. }
  254. else
  255. {
  256. ccx_state = TIM_CCx_Disable;
  257. }
  258. if (channel_index <= 4 && channel_index > 0)
  259. {
  260. if (pwm_device->channel[channel_index - 1] == 0xFF)
  261. return RT_EINVAL;
  262. TIM_CCxCmd(pwm_device->periph, pwm_device->channel[channel_index - 1], ccx_state);
  263. }
  264. else
  265. {
  266. return RT_EINVAL;
  267. }
  268. TIM_Cmd(pwm_device->periph, ENABLE);
  269. return RT_EOK;
  270. }
  271. static rt_err_t ch32f2_pwm_device_get(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration)
  272. {
  273. struct rtdevice_pwm_device *pwm_device;
  274. rt_uint32_t arr_counter, ccr_counter, prescaler, sample_freq;
  275. rt_uint32_t channel_index;
  276. rt_uint32_t tim_clock;
  277. pwm_device = (struct rtdevice_pwm_device *)device;
  278. tim_clock = ch32f2_tim_clock_get(pwm_device->periph);
  279. channel_index = configuration->channel;
  280. arr_counter = pwm_device->periph->ATRLR + 1;
  281. prescaler = pwm_device->periph->PSC + 1;
  282. sample_freq = (tim_clock / prescaler) / arr_counter;
  283. /* unit:ns */
  284. configuration->period = 1000000000 / sample_freq;
  285. if (channel_index == 1)
  286. {
  287. ccr_counter = pwm_device->periph->CH1CVR + 1;
  288. configuration->pulse = ((ccr_counter * 100) / arr_counter) * configuration->period / 100;
  289. }
  290. else if (channel_index == 2)
  291. {
  292. ccr_counter = pwm_device->periph->CH2CVR + 1;
  293. configuration->pulse = ((ccr_counter * 100) / arr_counter) * configuration->period / 100;
  294. }
  295. else if (channel_index == 3)
  296. {
  297. ccr_counter = pwm_device->periph->CH3CVR + 1;
  298. configuration->pulse = ((ccr_counter * 100) / arr_counter) * configuration->period / 100;
  299. }
  300. else if (channel_index == 4)
  301. {
  302. ccr_counter = pwm_device->periph->CH4CVR + 1;
  303. configuration->pulse = ((ccr_counter * 100) / arr_counter) * configuration->period / 100;
  304. }
  305. else
  306. return RT_EINVAL;
  307. return RT_EOK;
  308. }
  309. static rt_err_t ch32f2_pwm_device_set(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration)
  310. {
  311. struct rtdevice_pwm_device *pwm_device;
  312. rt_uint32_t arr_counter, ccr_counter, prescaler, sample_freq;
  313. rt_uint32_t channel_index;
  314. rt_uint32_t tim_clock;
  315. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitType;
  316. TIM_OCInitTypeDef TIM_OCInitType;
  317. pwm_device = (struct rtdevice_pwm_device *)device;
  318. tim_clock = ch32f2_tim_clock_get(pwm_device->periph);
  319. channel_index = configuration->channel;
  320. /* change to freq, unit:Hz */
  321. sample_freq = 1000000000 / configuration->period;
  322. /*counter = (tim_clk / prescaler) / sample_freq */
  323. /*normally, tim_clk is not need div, if arr_counter over 65536, need div.*/
  324. prescaler = 1;
  325. arr_counter = (tim_clock / prescaler) / sample_freq;
  326. if (arr_counter > MAX_COUNTER)
  327. {
  328. /* need div tim_clock
  329. * and round up the prescaler value.
  330. * (tim_clock >> 16) = tim_clock / 65536
  331. */
  332. if ((tim_clock >> 16) % sample_freq == 0)
  333. prescaler = (tim_clock >> 16) / sample_freq;
  334. else
  335. prescaler = (tim_clock >> 16) / sample_freq + 1;
  336. /*counter = (tim_clk / prescaler) / sample_freq */
  337. arr_counter = (tim_clock / prescaler) / sample_freq;
  338. }
  339. /* ccr_counter = duty cycle * arr_counter */
  340. ccr_counter = (configuration->pulse * 100 / configuration->period) * arr_counter / 100;
  341. /* check arr_counter > 1, cxx_counter > 1 */
  342. if (arr_counter < MIN_COUNTER)
  343. {
  344. arr_counter = MIN_COUNTER;
  345. }
  346. if (ccr_counter < MIN_PULSE)
  347. {
  348. ccr_counter = MIN_PULSE;
  349. }
  350. /* TMRe base configuration */
  351. TIM_TimeBaseStructInit(&TIM_TimeBaseInitType);
  352. TIM_TimeBaseInitType.TIM_Period = arr_counter - 1;
  353. TIM_TimeBaseInitType.TIM_Prescaler = prescaler - 1;
  354. TIM_TimeBaseInitType.TIM_ClockDivision = TIM_CKD_DIV1;
  355. TIM_TimeBaseInitType.TIM_CounterMode = TIM_CounterMode_Up;
  356. TIM_TimeBaseInit(pwm_device->periph, &TIM_TimeBaseInitType);
  357. TIM_OCStructInit(&TIM_OCInitType);
  358. TIM_OCInitType.TIM_OCMode = TIM_OCMode_PWM1;
  359. TIM_OCInitType.TIM_OutputState = TIM_OutputState_Enable;
  360. TIM_OCInitType.TIM_Pulse = ccr_counter - 1;
  361. TIM_OCInitType.TIM_OCPolarity = TIM_OCPolarity_High;
  362. if (channel_index == 1)
  363. {
  364. TIM_OC1Init(pwm_device->periph, &TIM_OCInitType);
  365. TIM_OC1PreloadConfig(pwm_device->periph, TIM_OCPreload_Disable);
  366. }
  367. else if (channel_index == 2)
  368. {
  369. TIM_OC2Init(pwm_device->periph, &TIM_OCInitType);
  370. TIM_OC2PreloadConfig(pwm_device->periph, TIM_OCPreload_Disable);
  371. }
  372. else if (channel_index == 3)
  373. {
  374. TIM_OC3Init(pwm_device->periph, &TIM_OCInitType);
  375. TIM_OC3PreloadConfig(pwm_device->periph, TIM_OCPreload_Disable);
  376. }
  377. else if (channel_index == 4)
  378. {
  379. TIM_OC4Init(pwm_device->periph, &TIM_OCInitType);
  380. TIM_OC4PreloadConfig(pwm_device->periph, TIM_OCPreload_Disable);
  381. }
  382. else
  383. {
  384. return RT_EINVAL;
  385. }
  386. TIM_ARRPreloadConfig(pwm_device->periph, ENABLE);
  387. TIM_CtrlPWMOutputs(pwm_device->periph, ENABLE);
  388. return RT_EOK;
  389. }
  390. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  391. {
  392. struct rt_pwm_configuration *configuration;
  393. configuration = (struct rt_pwm_configuration *)arg;
  394. switch (cmd)
  395. {
  396. case PWM_CMD_ENABLE:
  397. return ch32f2_pwm_device_enable(device, configuration, RT_TRUE);
  398. case PWM_CMD_DISABLE:
  399. return ch32f2_pwm_device_enable(device, configuration, RT_FALSE);
  400. case PWM_CMD_SET:
  401. return ch32f2_pwm_device_set(device, configuration);
  402. case PWM_CMD_GET:
  403. return ch32f2_pwm_device_get(device, configuration);
  404. default:
  405. return RT_EINVAL;
  406. }
  407. }
  408. static struct rt_pwm_ops pwm_ops =
  409. {
  410. .control = drv_pwm_control};
  411. static int rt_hw_pwm_init(void)
  412. {
  413. int result = RT_EOK;
  414. int index = 0;
  415. int channel_index;
  416. for (index = 0; index < ITEM_NUM(pwm_device_list); index++)
  417. {
  418. ch32f2_tim_clock_init(pwm_device_list[index].periph);
  419. for (channel_index = 0; channel_index < sizeof(pwm_device_list[index].channel); channel_index++)
  420. {
  421. if (pwm_device_list[index].channel[channel_index] != 0xFF)
  422. {
  423. ch32f2_pwm_io_init(pwm_device_list[index].periph, pwm_device_list[index].channel[channel_index]);
  424. }
  425. }
  426. if (rt_device_pwm_register(&pwm_device_list[index].parent, pwm_device_list[index].name, &pwm_ops, RT_NULL) == RT_EOK)
  427. {
  428. LOG_D("%s register success", pwm_device_list[index].name);
  429. }
  430. else
  431. {
  432. LOG_D("%s register failed", pwm_device_list[index].name);
  433. result = -RT_ERROR;
  434. }
  435. }
  436. return result;
  437. }
  438. INIT_BOARD_EXPORT(rt_hw_pwm_init);
  439. #endif /* BSP_USING_PWM */