drv_pwm_ch32f10x.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. * 2021-09-23 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. };
  139. static rt_err_t ch32f1_pwm_device_enable(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration, rt_bool_t enable)
  140. {
  141. struct rtdevice_pwm_device *pwm_device;
  142. rt_uint32_t channel_index;
  143. rt_uint16_t ccx_state;
  144. pwm_device = (struct rtdevice_pwm_device *)device;
  145. channel_index = configuration->channel;
  146. if (enable == RT_TRUE)
  147. {
  148. ccx_state = TIM_CCx_Enable;
  149. }
  150. else
  151. {
  152. ccx_state = TIM_CCx_Disable;
  153. }
  154. if (channel_index <= 4 && channel_index > 0)
  155. {
  156. if (pwm_device->channel[channel_index - 1] == 0xFF)
  157. return RT_EINVAL;
  158. TIM_CCxCmd(pwm_device->periph, pwm_device->channel[channel_index - 1], ccx_state);
  159. }
  160. else
  161. {
  162. return RT_EINVAL;
  163. }
  164. TIM_Cmd(pwm_device->periph, ENABLE);
  165. return RT_EOK;
  166. }
  167. static rt_err_t ch32f1_pwm_device_get(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration)
  168. {
  169. struct rtdevice_pwm_device *pwm_device;
  170. rt_uint32_t arr_counter, ccr_counter, prescaler, sample_freq;
  171. rt_uint32_t channel_index;
  172. rt_uint32_t tim_clock;
  173. pwm_device = (struct rtdevice_pwm_device *)device;
  174. tim_clock = ch32f1_tim_clock_get(pwm_device->periph);
  175. channel_index = configuration->channel;
  176. arr_counter = pwm_device->periph->ATRLR + 1;
  177. prescaler = pwm_device->periph->PSC + 1;
  178. sample_freq = (tim_clock / prescaler) / arr_counter;
  179. /* unit:ns */
  180. configuration->period = 1000000000 / sample_freq;
  181. if (channel_index == 1)
  182. {
  183. ccr_counter = pwm_device->periph->CH1CVR + 1;
  184. configuration->pulse = ((ccr_counter * 100) / arr_counter) * configuration->period / 100;
  185. }
  186. else if (channel_index == 2)
  187. {
  188. ccr_counter = pwm_device->periph->CH2CVR + 1;
  189. configuration->pulse = ((ccr_counter * 100) / arr_counter) * configuration->period / 100;
  190. }
  191. else if (channel_index == 3)
  192. {
  193. ccr_counter = pwm_device->periph->CH3CVR + 1;
  194. configuration->pulse = ((ccr_counter * 100) / arr_counter) * configuration->period / 100;
  195. }
  196. else if (channel_index == 4)
  197. {
  198. ccr_counter = pwm_device->periph->CH4CVR + 1;
  199. configuration->pulse = ((ccr_counter * 100) / arr_counter) * configuration->period / 100;
  200. }
  201. else
  202. return RT_EINVAL;
  203. return RT_EOK;
  204. }
  205. static rt_err_t ch32f1_pwm_device_set(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration)
  206. {
  207. struct rtdevice_pwm_device *pwm_device;
  208. rt_uint32_t arr_counter, ccr_counter, prescaler, sample_freq;
  209. rt_uint32_t channel_index;
  210. rt_uint32_t tim_clock;
  211. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitType;
  212. TIM_OCInitTypeDef TIM_OCInitType;
  213. pwm_device = (struct rtdevice_pwm_device *)device;
  214. tim_clock = ch32f1_tim_clock_get(pwm_device->periph);
  215. channel_index = configuration->channel;
  216. /* change to freq, unit:Hz */
  217. sample_freq = 1000000000 / configuration->period;
  218. /*counter = (tim_clk / prescaler) / sample_freq */
  219. /*normally, tim_clk is not need div, if arr_counter over 65536, need div.*/
  220. prescaler = 1;
  221. arr_counter = (tim_clock / prescaler) / sample_freq;
  222. if (arr_counter > MAX_COUNTER)
  223. {
  224. /* need div tim_clock
  225. * and round up the prescaler value.
  226. * (tim_clock >> 16) = tim_clock / 65536
  227. */
  228. if ((tim_clock >> 16) % sample_freq == 0)
  229. prescaler = (tim_clock >> 16) / sample_freq;
  230. else
  231. prescaler = (tim_clock >> 16) / sample_freq + 1;
  232. /*counter = (tim_clk / prescaler) / sample_freq */
  233. arr_counter = (tim_clock / prescaler) / sample_freq;
  234. }
  235. /* ccr_counter = duty cycle * arr_counter */
  236. ccr_counter = (configuration->pulse * 100 / configuration->period) * arr_counter / 100;
  237. /* check arr_counter > 1, cxx_counter > 1 */
  238. if (arr_counter < MIN_COUNTER)
  239. {
  240. arr_counter = MIN_COUNTER;
  241. }
  242. if (ccr_counter < MIN_PULSE)
  243. {
  244. ccr_counter = MIN_PULSE;
  245. }
  246. /* TMRe base configuration */
  247. TIM_TimeBaseStructInit(&TIM_TimeBaseInitType);
  248. TIM_TimeBaseInitType.TIM_Period = arr_counter - 1;
  249. TIM_TimeBaseInitType.TIM_Prescaler = prescaler - 1;
  250. TIM_TimeBaseInitType.TIM_ClockDivision = TIM_CKD_DIV1;
  251. TIM_TimeBaseInitType.TIM_CounterMode = TIM_CounterMode_Up;
  252. TIM_TimeBaseInit(pwm_device->periph, &TIM_TimeBaseInitType);
  253. TIM_OCStructInit(&TIM_OCInitType);
  254. TIM_OCInitType.TIM_OCMode = TIM_OCMode_PWM1;
  255. TIM_OCInitType.TIM_OutputState = TIM_OutputState_Enable;
  256. TIM_OCInitType.TIM_Pulse = ccr_counter - 1;
  257. TIM_OCInitType.TIM_OCPolarity = TIM_OCPolarity_High;
  258. if (channel_index == 1)
  259. {
  260. TIM_OC1Init(pwm_device->periph, &TIM_OCInitType);
  261. TIM_OC1PreloadConfig(pwm_device->periph, TIM_OCPreload_Disable);
  262. }
  263. else if (channel_index == 2)
  264. {
  265. TIM_OC2Init(pwm_device->periph, &TIM_OCInitType);
  266. TIM_OC2PreloadConfig(pwm_device->periph, TIM_OCPreload_Disable);
  267. }
  268. else if (channel_index == 3)
  269. {
  270. TIM_OC3Init(pwm_device->periph, &TIM_OCInitType);
  271. TIM_OC3PreloadConfig(pwm_device->periph, TIM_OCPreload_Disable);
  272. }
  273. else if (channel_index == 4)
  274. {
  275. TIM_OC4Init(pwm_device->periph, &TIM_OCInitType);
  276. TIM_OC4PreloadConfig(pwm_device->periph, TIM_OCPreload_Disable);
  277. }
  278. else
  279. {
  280. return RT_EINVAL;
  281. }
  282. TIM_ARRPreloadConfig(pwm_device->periph, ENABLE);
  283. TIM_CtrlPWMOutputs(pwm_device->periph, ENABLE);
  284. return RT_EOK;
  285. }
  286. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  287. {
  288. struct rt_pwm_configuration *configuration;
  289. configuration = (struct rt_pwm_configuration *)arg;
  290. switch (cmd)
  291. {
  292. case PWM_CMD_ENABLE:
  293. return ch32f1_pwm_device_enable(device, configuration, RT_TRUE);
  294. case PWM_CMD_DISABLE:
  295. return ch32f1_pwm_device_enable(device, configuration, RT_FALSE);
  296. case PWM_CMD_SET:
  297. return ch32f1_pwm_device_set(device, configuration);
  298. case PWM_CMD_GET:
  299. return ch32f1_pwm_device_get(device, configuration);
  300. default:
  301. return RT_EINVAL;
  302. }
  303. }
  304. static struct rt_pwm_ops pwm_ops =
  305. {
  306. .control = drv_pwm_control};
  307. static int rt_hw_pwm_init(void)
  308. {
  309. int result = RT_EOK;
  310. int index = 0;
  311. int channel_index;
  312. for (index = 0; index < ITEM_NUM(pwm_device_list); index++)
  313. {
  314. ch32f1_tim_clock_init(pwm_device_list[index].periph);
  315. for (channel_index = 0; channel_index < sizeof(pwm_device_list[index].channel); channel_index++)
  316. {
  317. if (pwm_device_list[index].channel[channel_index] != 0xFF)
  318. {
  319. ch32f1_pwm_io_init(pwm_device_list[index].periph, pwm_device_list[index].channel[channel_index]);
  320. }
  321. }
  322. if (rt_device_pwm_register(&pwm_device_list[index].parent, pwm_device_list[index].name, &pwm_ops, RT_NULL) == RT_EOK)
  323. {
  324. LOG_D("%s register success", pwm_device_list[index].name);
  325. }
  326. else
  327. {
  328. LOG_D("%s register failed", pwm_device_list[index].name);
  329. result = -RT_ERROR;
  330. }
  331. }
  332. return result;
  333. }
  334. INIT_BOARD_EXPORT(rt_hw_pwm_init);
  335. #endif /* BSP_USING_PWM */