1
0

drv_pwm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  10. #include <board.h>
  11. #ifdef RT_USING_PWM
  12. #include "drv_config.h"
  13. //#define DRV_DEBUG
  14. #define LOG_TAG "drv.pwm"
  15. #include <drv_log.h>
  16. #define MAX_PERIOD 65535
  17. #define MIN_PERIOD 3
  18. #define MIN_PULSE 2
  19. extern void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
  20. enum
  21. {
  22. #ifdef BSP_USING_PWM1
  23. PWM1_INDEX,
  24. #endif
  25. #ifdef BSP_USING_PWM2
  26. PWM2_INDEX,
  27. #endif
  28. #ifdef BSP_USING_PWM3
  29. PWM3_INDEX,
  30. #endif
  31. #ifdef BSP_USING_PWM4
  32. PWM4_INDEX,
  33. #endif
  34. #ifdef BSP_USING_PWM5
  35. PWM5_INDEX,
  36. #endif
  37. #ifdef BSP_USING_PWM6
  38. PWM6_INDEX,
  39. #endif
  40. #ifdef BSP_USING_PWM7
  41. PWM7_INDEX,
  42. #endif
  43. #ifdef BSP_USING_PWM8
  44. PWM8_INDEX,
  45. #endif
  46. #ifdef BSP_USING_PWM9
  47. PWM9_INDEX,
  48. #endif
  49. #ifdef BSP_USING_PWM10
  50. PWM10_INDEX,
  51. #endif
  52. #ifdef BSP_USING_PWM11
  53. PWM11_INDEX,
  54. #endif
  55. #ifdef BSP_USING_PWM12
  56. PWM12_INDEX,
  57. #endif
  58. #ifdef BSP_USING_PWM13
  59. PWM13_INDEX,
  60. #endif
  61. #ifdef BSP_USING_PWM14
  62. PWM14_INDEX,
  63. #endif
  64. #ifdef BSP_USING_PWM15
  65. PWM15_INDEX,
  66. #endif
  67. #ifdef BSP_USING_PWM16
  68. PWM16_INDEX,
  69. #endif
  70. #ifdef BSP_USING_PWM17
  71. PWM17_INDEX,
  72. #endif
  73. };
  74. struct stm32_pwm
  75. {
  76. struct rt_device_pwm pwm_device;
  77. TIM_HandleTypeDef tim_handle;
  78. rt_uint8_t channel;
  79. char *name;
  80. };
  81. static struct stm32_pwm stm32_pwm_obj[] =
  82. {
  83. #ifdef BSP_USING_PWM1
  84. PWM1_CONFIG,
  85. #endif
  86. #ifdef BSP_USING_PWM2
  87. PWM2_CONFIG,
  88. #endif
  89. #ifdef BSP_USING_PWM3
  90. PWM3_CONFIG,
  91. #endif
  92. #ifdef BSP_USING_PWM4
  93. PWM4_CONFIG,
  94. #endif
  95. #ifdef BSP_USING_PWM5
  96. PWM5_CONFIG,
  97. #endif
  98. #ifdef BSP_USING_PWM6
  99. PWM6_CONFIG,
  100. #endif
  101. #ifdef BSP_USING_PWM7
  102. PWM7_CONFIG,
  103. #endif
  104. #ifdef BSP_USING_PWM8
  105. PWM8_CONFIG,
  106. #endif
  107. #ifdef BSP_USING_PWM9
  108. PWM9_CONFIG,
  109. #endif
  110. #ifdef BSP_USING_PWM10
  111. PWM10_CONFIG,
  112. #endif
  113. #ifdef BSP_USING_PWM11
  114. PWM11_CONFIG,
  115. #endif
  116. #ifdef BSP_USING_PWM12
  117. PWM12_CONFIG,
  118. #endif
  119. #ifdef BSP_USING_PWM13
  120. PWM13_CONFIG,
  121. #endif
  122. #ifdef BSP_USING_PWM14
  123. PWM14_CONFIG,
  124. #endif
  125. #ifdef BSP_USING_PWM15
  126. PWM15_CONFIG,
  127. #endif
  128. #ifdef BSP_USING_PWM16
  129. PWM16_CONFIG,
  130. #endif
  131. #ifdef BSP_USING_PWM17
  132. PWM17_CONFIG,
  133. #endif
  134. };
  135. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg);
  136. static struct rt_pwm_ops drv_ops =
  137. {
  138. drv_pwm_control
  139. };
  140. static rt_err_t drv_pwm_enable(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration, rt_bool_t enable)
  141. {
  142. /* Converts the channel number to the channel number of Hal library */
  143. rt_uint32_t channel = 0x04 * (configuration->channel - 1);
  144. if (!enable)
  145. {
  146. HAL_TIM_PWM_Stop(htim, channel);
  147. }
  148. else
  149. {
  150. HAL_TIM_PWM_Start(htim, channel);
  151. }
  152. return RT_EOK;
  153. }
  154. static rt_err_t drv_pwm_get(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration)
  155. {
  156. /* Converts the channel number to the channel number of Hal library */
  157. rt_uint32_t channel = 0x04 * (configuration->channel - 1);
  158. rt_uint64_t tim_clock;
  159. #if defined(SOC_SERIES_STM32F4)
  160. if (htim->Instance == TIM9 || htim->Instance == TIM10 || htim->Instance == TIM11)
  161. #elif defined(SOC_SERIES_STM32L4)
  162. if (htim->Instance == TIM15 || htim->Instance == TIM16 || htim->Instance == TIM17)
  163. #elif defined(SOC_SERIES_STM32F1)
  164. if (0)
  165. #endif
  166. {
  167. tim_clock = HAL_RCC_GetPCLK2Freq() * 2;
  168. }
  169. else
  170. {
  171. #if defined(SOC_SERIES_STM32L4)
  172. tim_clock = HAL_RCC_GetPCLK1Freq();
  173. #else
  174. tim_clock = HAL_RCC_GetPCLK1Freq() * 2;
  175. #endif
  176. }
  177. if (__HAL_TIM_GET_CLOCKDIVISION(htim) == TIM_CLOCKDIVISION_DIV2)
  178. {
  179. tim_clock = tim_clock / 2;
  180. }
  181. else if (__HAL_TIM_GET_CLOCKDIVISION(htim) == TIM_CLOCKDIVISION_DIV4)
  182. {
  183. tim_clock = tim_clock / 4;
  184. }
  185. /* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
  186. tim_clock /= 1000000UL;
  187. configuration->period = (__HAL_TIM_GET_AUTORELOAD(htim) + 1) * (htim->Instance->PSC + 1) * 1000UL / tim_clock;
  188. configuration->pulse = (__HAL_TIM_GET_COMPARE(htim, channel) + 1) * (htim->Instance->PSC + 1) * 1000UL / tim_clock;
  189. return RT_EOK;
  190. }
  191. static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration)
  192. {
  193. rt_uint32_t period, pulse;
  194. rt_uint64_t tim_clock, psc;
  195. /* Converts the channel number to the channel number of Hal library */
  196. rt_uint32_t channel = 0x04 * (configuration->channel - 1);
  197. #if defined(SOC_SERIES_STM32F4)
  198. if (htim->Instance == TIM9 || htim->Instance == TIM10 || htim->Instance == TIM11)
  199. #elif defined(SOC_SERIES_STM32L4)
  200. if (htim->Instance == TIM15 || htim->Instance == TIM16 || htim->Instance == TIM17)
  201. #elif defined(SOC_SERIES_STM32F1)
  202. if (0)
  203. #endif
  204. {
  205. tim_clock = HAL_RCC_GetPCLK2Freq() * 2;
  206. }
  207. else
  208. {
  209. #if defined(SOC_SERIES_STM32L4)
  210. tim_clock = HAL_RCC_GetPCLK1Freq();
  211. #else
  212. tim_clock = HAL_RCC_GetPCLK1Freq() * 2;
  213. #endif
  214. }
  215. /* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
  216. tim_clock /= 1000000UL;
  217. period = (unsigned long long)configuration->period * tim_clock / 1000ULL ;
  218. psc = period / MAX_PERIOD + 1;
  219. period = period / psc;
  220. __HAL_TIM_SET_PRESCALER(htim, psc - 1);
  221. if (period < MIN_PERIOD)
  222. {
  223. period = MIN_PERIOD;
  224. }
  225. __HAL_TIM_SET_AUTORELOAD(htim, period - 1);
  226. pulse = (unsigned long long)configuration->pulse * tim_clock / psc / 1000ULL;
  227. if (pulse < MIN_PULSE)
  228. {
  229. pulse = MIN_PULSE;
  230. }
  231. else if (pulse > period)
  232. {
  233. pulse = period;
  234. }
  235. __HAL_TIM_SET_COMPARE(htim, channel, pulse - 1);
  236. __HAL_TIM_SET_COUNTER(htim, 0);
  237. /* Update frequency value */
  238. HAL_TIM_GenerateEvent(htim, TIM_EVENTSOURCE_UPDATE);
  239. return RT_EOK;
  240. }
  241. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  242. {
  243. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
  244. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)device->parent.user_data;
  245. switch (cmd)
  246. {
  247. case PWM_CMD_ENABLE:
  248. return drv_pwm_enable(htim, configuration, RT_TRUE);
  249. case PWM_CMD_DISABLE:
  250. return drv_pwm_enable(htim, configuration, RT_FALSE);
  251. case PWM_CMD_SET:
  252. return drv_pwm_set(htim, configuration);
  253. case PWM_CMD_GET:
  254. return drv_pwm_get(htim, configuration);
  255. default:
  256. return RT_EINVAL;
  257. }
  258. }
  259. static rt_err_t stm32_hw_pwm_init(struct stm32_pwm *device)
  260. {
  261. rt_err_t result = RT_EOK;
  262. TIM_HandleTypeDef *tim = RT_NULL;
  263. TIM_OC_InitTypeDef oc_config = {0};
  264. TIM_MasterConfigTypeDef master_config = {0};
  265. TIM_ClockConfigTypeDef clock_config = {0};
  266. RT_ASSERT(device != RT_NULL);
  267. tim = (TIM_HandleTypeDef *)&device->tim_handle;
  268. /* configure the timer to pwm mode */
  269. tim->Init.Prescaler = 0;
  270. tim->Init.CounterMode = TIM_COUNTERMODE_UP;
  271. tim->Init.Period = 0;
  272. tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  273. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32L4)
  274. tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  275. #endif
  276. if (HAL_TIM_Base_Init(tim) != HAL_OK)
  277. {
  278. LOG_E("%s time base init failed", device->name);
  279. result = -RT_ERROR;
  280. goto __exit;
  281. }
  282. clock_config.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  283. if (HAL_TIM_ConfigClockSource(tim, &clock_config) != HAL_OK)
  284. {
  285. LOG_E("%s clock init failed", device->name);
  286. result = -RT_ERROR;
  287. goto __exit;
  288. }
  289. if (HAL_TIM_PWM_Init(tim) != HAL_OK)
  290. {
  291. LOG_E("%s pwm init failed", device->name);
  292. result = -RT_ERROR;
  293. goto __exit;
  294. }
  295. master_config.MasterOutputTrigger = TIM_TRGO_RESET;
  296. master_config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  297. if (HAL_TIMEx_MasterConfigSynchronization(tim, &master_config) != HAL_OK)
  298. {
  299. LOG_E("%s master config failed", device->name);
  300. result = -RT_ERROR;
  301. goto __exit;
  302. }
  303. oc_config.OCMode = TIM_OCMODE_PWM1;
  304. oc_config.Pulse = 0;
  305. oc_config.OCPolarity = TIM_OCPOLARITY_HIGH;
  306. oc_config.OCFastMode = TIM_OCFAST_DISABLE;
  307. /* config pwm channel */
  308. if (device->channel & 0x01)
  309. {
  310. if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_1) != HAL_OK)
  311. {
  312. LOG_E("%s channel1 config failed", device->name);
  313. result = -RT_ERROR;
  314. goto __exit;
  315. }
  316. }
  317. if (device->channel & 0x02)
  318. {
  319. if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_2) != HAL_OK)
  320. {
  321. LOG_E("%s channel2 config failed", device->name);
  322. result = -RT_ERROR;
  323. goto __exit;
  324. }
  325. }
  326. if (device->channel & 0x04)
  327. {
  328. if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_3) != HAL_OK)
  329. {
  330. LOG_E("%s channel3 config failed", device->name);
  331. result = -RT_ERROR;
  332. goto __exit;
  333. }
  334. }
  335. if (device->channel & 0x08)
  336. {
  337. if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_4) != HAL_OK)
  338. {
  339. LOG_E("%s channel4 config failed", device->name);
  340. result = -RT_ERROR;
  341. goto __exit;
  342. }
  343. }
  344. /* pwm pin configuration */
  345. HAL_TIM_MspPostInit(tim);
  346. /* enable update request source */
  347. __HAL_TIM_URS_ENABLE(tim);
  348. __exit:
  349. return result;
  350. }
  351. static void pwm_get_channel(void)
  352. {
  353. #ifdef BSP_USING_PWM2_CH4
  354. stm32_pwm_obj[PWM2_INDEX].channel |= 1 << 3;
  355. #endif
  356. #ifdef BSP_USING_PWM3_CH1
  357. stm32_pwm_obj[PWM3_INDEX].channel |= 1 << 0;
  358. #endif
  359. #ifdef BSP_USING_PWM3_CH2
  360. stm32_pwm_obj[PWM3_INDEX].channel |= 1 << 1;
  361. #endif
  362. #ifdef BSP_USING_PWM3_CH3
  363. stm32_pwm_obj[PWM3_INDEX].channel |= 1 << 2;
  364. #endif
  365. #ifdef BSP_USING_PWM3_CH4
  366. stm32_pwm_obj[PWM3_INDEX].channel |= 1 << 3;
  367. #endif
  368. #ifdef BSP_USING_PWM4_CH2
  369. stm32_pwm_obj[PWM4_INDEX].channel |= 1 << 1;
  370. #endif
  371. #ifdef BSP_USING_PWM4_CH3
  372. stm32_pwm_obj[PWM4_INDEX].channel |= 1 << 2;
  373. #endif
  374. #ifdef BSP_USING_PWM5_CH1
  375. stm32_pwm_obj[PWM5_INDEX].channel |= 1 << 1;
  376. #endif
  377. #ifdef BSP_USING_PWM5_CH2
  378. stm32_pwm_obj[PWM5_INDEX].channel |= 1 << 2;
  379. #endif
  380. #ifdef BSP_USING_PWM5_CH3
  381. stm32_pwm_obj[PWM5_INDEX].channel |= 1 << 3;
  382. #endif
  383. }
  384. static int stm32_pwm_init(void)
  385. {
  386. int i = 0;
  387. int result = RT_EOK;
  388. pwm_get_channel();
  389. for (i = 0; i < sizeof(stm32_pwm_obj) / sizeof(stm32_pwm_obj[0]); i++)
  390. {
  391. /* pwm init */
  392. if (stm32_hw_pwm_init(&stm32_pwm_obj[i]) != RT_EOK)
  393. {
  394. LOG_E("%s init failed", stm32_pwm_obj[i].name);
  395. result = -RT_ERROR;
  396. goto __exit;
  397. }
  398. else
  399. {
  400. LOG_D("%s init success", stm32_pwm_obj[i].name);
  401. /* register pwm device */
  402. if (rt_device_pwm_register(rt_calloc(1, sizeof(struct rt_device_pwm)), stm32_pwm_obj[i].name, &drv_ops, &stm32_pwm_obj[i].tim_handle) == RT_EOK)
  403. {
  404. LOG_D("%s register success", stm32_pwm_obj[i].name);
  405. }
  406. else
  407. {
  408. LOG_E("%s register failed", stm32_pwm_obj[i].name);
  409. result = -RT_ERROR;
  410. }
  411. }
  412. }
  413. __exit:
  414. return result;
  415. }
  416. INIT_DEVICE_EXPORT(stm32_pwm_init);
  417. #endif /* RT_USING_PWM */