drv_pwm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-13 zylx first version
  9. * 2021-01-23 thread-liu Fix the timer clock frequency doubling problem
  10. */
  11. #include <board.h>
  12. #include <rtthread.h>
  13. #ifdef BSP_USING_PWM
  14. #include "drv_config.h"
  15. #include "drv_tim.h"
  16. #include <drivers/rt_drv_pwm.h>
  17. //#define DRV_DEBUG
  18. #define LOG_TAG "drv.pwm"
  19. #include <drv_log.h>
  20. #define MAX_PERIOD 65535
  21. #define MIN_PERIOD 3
  22. #define MIN_PULSE 2
  23. enum
  24. {
  25. #ifdef BSP_USING_PWM1
  26. PWM1_INDEX,
  27. #endif
  28. #ifdef BSP_USING_PWM2
  29. PWM2_INDEX,
  30. #endif
  31. #ifdef BSP_USING_PWM3
  32. PWM3_INDEX,
  33. #endif
  34. #ifdef BSP_USING_PWM4
  35. PWM4_INDEX,
  36. #endif
  37. #ifdef BSP_USING_PWM5
  38. PWM5_INDEX,
  39. #endif
  40. #ifdef BSP_USING_PWM6
  41. PWM6_INDEX,
  42. #endif
  43. #ifdef BSP_USING_PWM7
  44. PWM7_INDEX,
  45. #endif
  46. #ifdef BSP_USING_PWM8
  47. PWM8_INDEX,
  48. #endif
  49. #ifdef BSP_USING_PWM9
  50. PWM9_INDEX,
  51. #endif
  52. #ifdef BSP_USING_PWM10
  53. PWM10_INDEX,
  54. #endif
  55. #ifdef BSP_USING_PWM11
  56. PWM11_INDEX,
  57. #endif
  58. #ifdef BSP_USING_PWM12
  59. PWM12_INDEX,
  60. #endif
  61. #ifdef BSP_USING_PWM13
  62. PWM13_INDEX,
  63. #endif
  64. #ifdef BSP_USING_PWM14
  65. PWM14_INDEX,
  66. #endif
  67. #ifdef BSP_USING_PWM15
  68. PWM15_INDEX,
  69. #endif
  70. #ifdef BSP_USING_PWM16
  71. PWM16_INDEX,
  72. #endif
  73. #ifdef BSP_USING_PWM17
  74. PWM17_INDEX,
  75. #endif
  76. };
  77. struct stm32_pwm
  78. {
  79. struct rt_device_pwm pwm_device;
  80. TIM_HandleTypeDef tim_handle;
  81. rt_uint8_t channel;
  82. char *name;
  83. };
  84. static struct stm32_pwm stm32_pwm_obj[] =
  85. {
  86. #ifdef BSP_USING_PWM1
  87. PWM1_CONFIG,
  88. #endif
  89. #ifdef BSP_USING_PWM2
  90. PWM2_CONFIG,
  91. #endif
  92. #ifdef BSP_USING_PWM3
  93. PWM3_CONFIG,
  94. #endif
  95. #ifdef BSP_USING_PWM4
  96. PWM4_CONFIG,
  97. #endif
  98. #ifdef BSP_USING_PWM5
  99. PWM5_CONFIG,
  100. #endif
  101. #ifdef BSP_USING_PWM6
  102. PWM6_CONFIG,
  103. #endif
  104. #ifdef BSP_USING_PWM7
  105. PWM7_CONFIG,
  106. #endif
  107. #ifdef BSP_USING_PWM8
  108. PWM8_CONFIG,
  109. #endif
  110. #ifdef BSP_USING_PWM9
  111. PWM9_CONFIG,
  112. #endif
  113. #ifdef BSP_USING_PWM10
  114. PWM10_CONFIG,
  115. #endif
  116. #ifdef BSP_USING_PWM11
  117. PWM11_CONFIG,
  118. #endif
  119. #ifdef BSP_USING_PWM12
  120. PWM12_CONFIG,
  121. #endif
  122. #ifdef BSP_USING_PWM13
  123. PWM13_CONFIG,
  124. #endif
  125. #ifdef BSP_USING_PWM14
  126. PWM14_CONFIG,
  127. #endif
  128. #ifdef BSP_USING_PWM15
  129. PWM15_CONFIG,
  130. #endif
  131. #ifdef BSP_USING_PWM16
  132. PWM16_CONFIG,
  133. #endif
  134. #ifdef BSP_USING_PWM17
  135. PWM17_CONFIG,
  136. #endif
  137. };
  138. static rt_uint64_t tim_clock_get(TIM_HandleTypeDef *htim)
  139. {
  140. rt_uint32_t pclk1_doubler, pclk2_doubler;
  141. rt_uint64_t tim_clock;
  142. stm32_tim_pclkx_doubler_get(&pclk1_doubler, &pclk2_doubler);
  143. /* Some series may only have APBPERIPH_BASE, don't have HAL_RCC_GetPCLK2Freq */
  144. #if defined(APBPERIPH_BASE)
  145. tim_clock = (rt_uint32_t)(HAL_RCC_GetPCLK1Freq() * pclk1_doubler);
  146. #elif defined(APB1PERIPH_BASE) || defined(APB2PERIPH_BASE)
  147. if ((rt_uint32_t)htim->Instance >= APB2PERIPH_BASE)
  148. {
  149. tim_clock = (rt_uint32_t)(HAL_RCC_GetPCLK2Freq() * pclk2_doubler);
  150. }
  151. else
  152. {
  153. tim_clock = (rt_uint32_t)(HAL_RCC_GetPCLK1Freq() * pclk1_doubler);
  154. }
  155. #endif
  156. return tim_clock;
  157. }
  158. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg);
  159. static struct rt_pwm_ops drv_ops =
  160. {
  161. drv_pwm_control
  162. };
  163. static rt_err_t drv_pwm_enable(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration, rt_bool_t enable)
  164. {
  165. /* Converts the channel number to the channel number of Hal library */
  166. rt_uint32_t channel = 0x04 * (configuration->channel - 1);
  167. if (!configuration->complementary)
  168. {
  169. if (!enable)
  170. {
  171. HAL_TIM_PWM_Stop(htim, channel);
  172. }
  173. else
  174. {
  175. HAL_TIM_PWM_Start(htim, channel);
  176. }
  177. }
  178. else if (configuration->complementary)
  179. {
  180. if (!enable)
  181. {
  182. HAL_TIMEx_PWMN_Stop(htim, channel);
  183. }
  184. else
  185. {
  186. HAL_TIMEx_PWMN_Start(htim, channel);
  187. }
  188. }
  189. return RT_EOK;
  190. }
  191. static rt_err_t drv_pwm_get(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration)
  192. {
  193. /* Converts the channel number to the channel number of Hal library */
  194. rt_uint32_t channel = 0x04 * (configuration->channel - 1);
  195. rt_uint64_t tim_clock;
  196. tim_clock = tim_clock_get(htim);
  197. if (__HAL_TIM_GET_CLOCKDIVISION(htim) == TIM_CLOCKDIVISION_DIV2)
  198. {
  199. tim_clock = tim_clock / 2;
  200. }
  201. else if (__HAL_TIM_GET_CLOCKDIVISION(htim) == TIM_CLOCKDIVISION_DIV4)
  202. {
  203. tim_clock = tim_clock / 4;
  204. }
  205. /* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
  206. tim_clock /= 1000000UL;
  207. configuration->period = (__HAL_TIM_GET_AUTORELOAD(htim) + 1) * (htim->Instance->PSC + 1) * 1000UL / tim_clock;
  208. configuration->pulse = (__HAL_TIM_GET_COMPARE(htim, channel) + 1) * (htim->Instance->PSC + 1) * 1000UL / tim_clock;
  209. return RT_EOK;
  210. }
  211. static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration)
  212. {
  213. rt_uint32_t period, pulse;
  214. rt_uint64_t tim_clock, psc;
  215. /* Converts the channel number to the channel number of Hal library */
  216. rt_uint32_t channel = 0x04 * (configuration->channel - 1);
  217. tim_clock = tim_clock_get(htim);
  218. /* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
  219. tim_clock /= 1000000UL;
  220. period = (rt_uint64_t)configuration->period * tim_clock / 1000ULL ;
  221. psc = period / MAX_PERIOD + 1;
  222. period = period / psc;
  223. __HAL_TIM_SET_PRESCALER(htim, psc - 1);
  224. if (period < MIN_PERIOD)
  225. {
  226. period = MIN_PERIOD;
  227. }
  228. __HAL_TIM_SET_AUTORELOAD(htim, period - 1);
  229. pulse = (rt_uint64_t)configuration->pulse * tim_clock / psc / 1000ULL;
  230. if (pulse < MIN_PULSE)
  231. {
  232. pulse = MIN_PULSE;
  233. }
  234. /*To determine user input, output high level is required*/
  235. else if (pulse >= period)
  236. {
  237. pulse = period + 1;
  238. }
  239. __HAL_TIM_SET_COMPARE(htim, channel, pulse - 1);
  240. /* If you want the PWM setting to take effect immediately,
  241. please uncommon the following code, but it will cause the last PWM cycle not complete. */
  242. //__HAL_TIM_SET_COUNTER(htim, 0);
  243. //HAL_TIM_GenerateEvent(htim, TIM_EVENTSOURCE_UPDATE); /* Update frequency value */
  244. return RT_EOK;
  245. }
  246. static rt_err_t drv_pwm_set_period(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration)
  247. {
  248. rt_uint32_t period;
  249. rt_uint64_t tim_clock, psc;
  250. tim_clock = tim_clock_get(htim);
  251. /* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
  252. tim_clock /= 1000000UL;
  253. period = (rt_uint64_t)configuration->period * tim_clock / 1000ULL ;
  254. psc = period / MAX_PERIOD + 1;
  255. period = period / psc;
  256. __HAL_TIM_SET_PRESCALER(htim, psc - 1);
  257. if (period < MIN_PERIOD)
  258. {
  259. period = MIN_PERIOD;
  260. }
  261. __HAL_TIM_SET_AUTORELOAD(htim, period - 1);
  262. return RT_EOK;
  263. }
  264. static rt_err_t drv_pwm_set_pulse(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration)
  265. {
  266. rt_uint32_t period, pulse;
  267. rt_uint64_t tim_clock;
  268. /* Converts the channel number to the channel number of Hal library */
  269. rt_uint32_t channel = 0x04 * (configuration->channel - 1);
  270. tim_clock = tim_clock_get(htim);
  271. /* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
  272. tim_clock /= 1000000UL;
  273. period = (__HAL_TIM_GET_AUTORELOAD(htim) + 1) * (htim->Instance->PSC + 1) * 1000UL / tim_clock;
  274. pulse = (rt_uint64_t)configuration->pulse * (__HAL_TIM_GET_AUTORELOAD(htim) + 1) / period;
  275. if (pulse < MIN_PULSE)
  276. {
  277. pulse = MIN_PULSE;
  278. }
  279. else if (pulse > period)
  280. {
  281. pulse = period;
  282. }
  283. __HAL_TIM_SET_COMPARE(htim, channel, pulse - 1);
  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 = (struct rt_pwm_configuration *)arg;
  289. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)device->parent.user_data;
  290. switch (cmd)
  291. {
  292. case PWM_CMD_ENABLE:
  293. return drv_pwm_enable(htim, configuration, RT_TRUE);
  294. case PWM_CMD_DISABLE:
  295. return drv_pwm_enable(htim, configuration, RT_FALSE);
  296. case PWM_CMD_SET:
  297. return drv_pwm_set(htim, configuration);
  298. case PWM_CMD_SET_PERIOD:
  299. return drv_pwm_set_period(htim, configuration);
  300. case PWM_CMD_SET_PULSE:
  301. return drv_pwm_set_pulse(htim, configuration);
  302. case PWM_CMD_GET:
  303. return drv_pwm_get(htim, configuration);
  304. default:
  305. return -RT_EINVAL;
  306. }
  307. }
  308. static rt_err_t stm32_hw_pwm_init(struct stm32_pwm *device)
  309. {
  310. rt_err_t result = RT_EOK;
  311. TIM_HandleTypeDef *tim = RT_NULL;
  312. TIM_OC_InitTypeDef oc_config = {0};
  313. TIM_MasterConfigTypeDef master_config = {0};
  314. TIM_ClockConfigTypeDef clock_config = {0};
  315. RT_ASSERT(device != RT_NULL);
  316. tim = (TIM_HandleTypeDef *)&device->tim_handle;
  317. /* configure the timer to pwm mode */
  318. tim->Init.Prescaler = 0;
  319. tim->Init.CounterMode = TIM_COUNTERMODE_UP;
  320. tim->Init.Period = 0;
  321. tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  322. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32L4)
  323. tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  324. #endif
  325. if (HAL_TIM_Base_Init(tim) != HAL_OK)
  326. {
  327. LOG_E("%s pwm init failed", device->name);
  328. result = -RT_ERROR;
  329. goto __exit;
  330. }
  331. stm32_tim_enable_clock(tim);
  332. clock_config.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  333. if (HAL_TIM_ConfigClockSource(tim, &clock_config) != HAL_OK)
  334. {
  335. LOG_E("%s clock init failed", device->name);
  336. result = -RT_ERROR;
  337. goto __exit;
  338. }
  339. if (HAL_TIM_PWM_Init(tim) != HAL_OK)
  340. {
  341. LOG_E("%s pwm init failed", device->name);
  342. result = -RT_ERROR;
  343. goto __exit;
  344. }
  345. if(IS_TIM_MASTER_INSTANCE(tim->Instance))
  346. {
  347. master_config.MasterOutputTrigger = TIM_TRGO_RESET;
  348. master_config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  349. if (HAL_TIMEx_MasterConfigSynchronization(tim, &master_config) != HAL_OK)
  350. {
  351. LOG_E("%s master config failed", device->name);
  352. result = -RT_ERROR;
  353. goto __exit;
  354. }
  355. }
  356. oc_config.OCMode = TIM_OCMODE_PWM1;
  357. oc_config.Pulse = 0;
  358. oc_config.OCPolarity = TIM_OCPOLARITY_HIGH;
  359. oc_config.OCFastMode = TIM_OCFAST_DISABLE;
  360. oc_config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  361. oc_config.OCIdleState = TIM_OCIDLESTATE_RESET;
  362. /* config pwm channel */
  363. if (device->channel & 0x01)
  364. {
  365. if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_1) != HAL_OK)
  366. {
  367. LOG_E("%s channel1 config failed", device->name);
  368. result = -RT_ERROR;
  369. goto __exit;
  370. }
  371. }
  372. if (device->channel & 0x02)
  373. {
  374. if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_2) != HAL_OK)
  375. {
  376. LOG_E("%s channel2 config failed", device->name);
  377. result = -RT_ERROR;
  378. goto __exit;
  379. }
  380. }
  381. if (device->channel & 0x04)
  382. {
  383. if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_3) != HAL_OK)
  384. {
  385. LOG_E("%s channel3 config failed", device->name);
  386. result = -RT_ERROR;
  387. goto __exit;
  388. }
  389. }
  390. if (device->channel & 0x08)
  391. {
  392. if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_4) != HAL_OK)
  393. {
  394. LOG_E("%s channel4 config failed", device->name);
  395. result = -RT_ERROR;
  396. goto __exit;
  397. }
  398. }
  399. /* pwm pin configuration */
  400. void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
  401. HAL_TIM_MspPostInit(tim);
  402. /* enable update request source */
  403. __HAL_TIM_URS_ENABLE(tim);
  404. __exit:
  405. return result;
  406. }
  407. static void stm32_pwm_get_channel(void)
  408. {
  409. #ifdef BSP_USING_PWM1_CH1
  410. stm32_pwm_obj[PWM1_INDEX].channel |= 1 << 0;
  411. #endif
  412. #ifdef BSP_USING_PWM1_CH2
  413. stm32_pwm_obj[PWM1_INDEX].channel |= 1 << 1;
  414. #endif
  415. #ifdef BSP_USING_PWM1_CH3
  416. stm32_pwm_obj[PWM1_INDEX].channel |= 1 << 2;
  417. #endif
  418. #ifdef BSP_USING_PWM1_CH4
  419. stm32_pwm_obj[PWM1_INDEX].channel |= 1 << 3;
  420. #endif
  421. #ifdef BSP_USING_PWM2_CH1
  422. stm32_pwm_obj[PWM2_INDEX].channel |= 1 << 0;
  423. #endif
  424. #ifdef BSP_USING_PWM2_CH2
  425. stm32_pwm_obj[PWM2_INDEX].channel |= 1 << 1;
  426. #endif
  427. #ifdef BSP_USING_PWM2_CH3
  428. stm32_pwm_obj[PWM2_INDEX].channel |= 1 << 2;
  429. #endif
  430. #ifdef BSP_USING_PWM2_CH4
  431. stm32_pwm_obj[PWM2_INDEX].channel |= 1 << 3;
  432. #endif
  433. #ifdef BSP_USING_PWM3_CH1
  434. stm32_pwm_obj[PWM3_INDEX].channel |= 1 << 0;
  435. #endif
  436. #ifdef BSP_USING_PWM3_CH2
  437. stm32_pwm_obj[PWM3_INDEX].channel |= 1 << 1;
  438. #endif
  439. #ifdef BSP_USING_PWM3_CH3
  440. stm32_pwm_obj[PWM3_INDEX].channel |= 1 << 2;
  441. #endif
  442. #ifdef BSP_USING_PWM3_CH4
  443. stm32_pwm_obj[PWM3_INDEX].channel |= 1 << 3;
  444. #endif
  445. #ifdef BSP_USING_PWM4_CH1
  446. stm32_pwm_obj[PWM4_INDEX].channel |= 1 << 0;
  447. #endif
  448. #ifdef BSP_USING_PWM4_CH2
  449. stm32_pwm_obj[PWM4_INDEX].channel |= 1 << 1;
  450. #endif
  451. #ifdef BSP_USING_PWM4_CH3
  452. stm32_pwm_obj[PWM4_INDEX].channel |= 1 << 2;
  453. #endif
  454. #ifdef BSP_USING_PWM4_CH4
  455. stm32_pwm_obj[PWM4_INDEX].channel |= 1 << 3;
  456. #endif
  457. #ifdef BSP_USING_PWM5_CH1
  458. stm32_pwm_obj[PWM5_INDEX].channel |= 1 << 0;
  459. #endif
  460. #ifdef BSP_USING_PWM5_CH2
  461. stm32_pwm_obj[PWM5_INDEX].channel |= 1 << 1;
  462. #endif
  463. #ifdef BSP_USING_PWM5_CH3
  464. stm32_pwm_obj[PWM5_INDEX].channel |= 1 << 2;
  465. #endif
  466. #ifdef BSP_USING_PWM5_CH4
  467. stm32_pwm_obj[PWM5_INDEX].channel |= 1 << 3;
  468. #endif
  469. #ifdef BSP_USING_PWM6_CH1
  470. stm32_pwm_obj[PWM6_INDEX].channel |= 1 << 0;
  471. #endif
  472. #ifdef BSP_USING_PWM6_CH2
  473. stm32_pwm_obj[PWM6_INDEX].channel |= 1 << 1;
  474. #endif
  475. #ifdef BSP_USING_PWM6_CH3
  476. stm32_pwm_obj[PWM6_INDEX].channel |= 1 << 2;
  477. #endif
  478. #ifdef BSP_USING_PWM6_CH4
  479. stm32_pwm_obj[PWM6_INDEX].channel |= 1 << 3;
  480. #endif
  481. #ifdef BSP_USING_PWM7_CH1
  482. stm32_pwm_obj[PWM7_INDEX].channel |= 1 << 0;
  483. #endif
  484. #ifdef BSP_USING_PWM7_CH2
  485. stm32_pwm_obj[PWM7_INDEX].channel |= 1 << 1;
  486. #endif
  487. #ifdef BSP_USING_PWM7_CH3
  488. stm32_pwm_obj[PWM7_INDEX].channel |= 1 << 2;
  489. #endif
  490. #ifdef BSP_USING_PWM7_CH4
  491. stm32_pwm_obj[PWM7_INDEX].channel |= 1 << 3;
  492. #endif
  493. #ifdef BSP_USING_PWM8_CH1
  494. stm32_pwm_obj[PWM8_INDEX].channel |= 1 << 0;
  495. #endif
  496. #ifdef BSP_USING_PWM8_CH2
  497. stm32_pwm_obj[PWM8_INDEX].channel |= 1 << 1;
  498. #endif
  499. #ifdef BSP_USING_PWM8_CH3
  500. stm32_pwm_obj[PWM8_INDEX].channel |= 1 << 2;
  501. #endif
  502. #ifdef BSP_USING_PWM8_CH4
  503. stm32_pwm_obj[PWM8_INDEX].channel |= 1 << 3;
  504. #endif
  505. #ifdef BSP_USING_PWM9_CH1
  506. stm32_pwm_obj[PWM9_INDEX].channel |= 1 << 0;
  507. #endif
  508. #ifdef BSP_USING_PWM9_CH2
  509. stm32_pwm_obj[PWM9_INDEX].channel |= 1 << 1;
  510. #endif
  511. #ifdef BSP_USING_PWM9_CH3
  512. stm32_pwm_obj[PWM9_INDEX].channel |= 1 << 2;
  513. #endif
  514. #ifdef BSP_USING_PWM9_CH4
  515. stm32_pwm_obj[PWM9_INDEX].channel |= 1 << 3;
  516. #endif
  517. #ifdef BSP_USING_PWM10_CH1
  518. stm32_pwm_obj[PWM10_INDEX].channel |= 1 << 0;
  519. #endif
  520. #ifdef BSP_USING_PWM11_CH1
  521. stm32_pwm_obj[PWM11_INDEX].channel |= 1 << 0;
  522. #endif
  523. #ifdef BSP_USING_PWM12_CH1
  524. stm32_pwm_obj[PWM12_INDEX].channel |= 1 << 0;
  525. #endif
  526. #ifdef BSP_USING_PWM12_CH2
  527. stm32_pwm_obj[PWM12_INDEX].channel |= 1 << 1;
  528. #endif
  529. #ifdef BSP_USING_PWM13_CH1
  530. stm32_pwm_obj[PWM13_INDEX].channel |= 1 << 0;
  531. #endif
  532. #ifdef BSP_USING_PWM14_CH1
  533. stm32_pwm_obj[PWM14_INDEX].channel |= 1 << 0;
  534. #endif
  535. #ifdef BSP_USING_PWM15_CH1
  536. stm32_pwm_obj[PWM15_INDEX].channel |= 1 << 0;
  537. #endif
  538. #ifdef BSP_USING_PWM16_CH1
  539. stm32_pwm_obj[PWM16_INDEX].channel |= 1 << 0;
  540. #endif
  541. #ifdef BSP_USING_PWM17_CH1
  542. stm32_pwm_obj[PWM17_INDEX].channel |= 1 << 0;
  543. #endif
  544. }
  545. static int stm32_pwm_init(void)
  546. {
  547. int i = 0;
  548. int result = RT_EOK;
  549. stm32_pwm_get_channel();
  550. for (i = 0; i < sizeof(stm32_pwm_obj) / sizeof(stm32_pwm_obj[0]); i++)
  551. {
  552. /* pwm init */
  553. if (stm32_hw_pwm_init(&stm32_pwm_obj[i]) != RT_EOK)
  554. {
  555. LOG_E("%s init failed", stm32_pwm_obj[i].name);
  556. result = -RT_ERROR;
  557. goto __exit;
  558. }
  559. else
  560. {
  561. LOG_D("%s init success", stm32_pwm_obj[i].name);
  562. /* register pwm device */
  563. if (rt_device_pwm_register(&stm32_pwm_obj[i].pwm_device, stm32_pwm_obj[i].name, &drv_ops, &stm32_pwm_obj[i].tim_handle) == RT_EOK)
  564. {
  565. LOG_D("%s register success", stm32_pwm_obj[i].name);
  566. }
  567. else
  568. {
  569. LOG_E("%s register failed", stm32_pwm_obj[i].name);
  570. result = -RT_ERROR;
  571. }
  572. }
  573. }
  574. __exit:
  575. return result;
  576. }
  577. INIT_DEVICE_EXPORT(stm32_pwm_init);
  578. #endif /* BSP_USING_PWM */