drv_pwm.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-1-13 Leo first version
  9. */
  10. #include <board.h>
  11. #include "drv_pwm.h"
  12. #ifdef RT_USING_PWM
  13. #if !defined(BSP_USING_TIM3_CH1) && !defined(BSP_USING_TIM3_CH2) && \
  14. !defined(BSP_USING_TIM3_CH3) && !defined(BSP_USING_TIM3_CH4)
  15. #error "Please define at least one BSP_USING_TIMx_CHx"
  16. #endif
  17. #endif /* RT_USING_PWM */
  18. #define DRV_DEBUG
  19. #define LOG_TAG "drv.pwm"
  20. #include <drv_log.h>
  21. #define MAX_PERIOD 65535
  22. struct rt_device_pwm pwm_device;
  23. struct n32_pwm
  24. {
  25. struct rt_device_pwm pwm_device;
  26. TIM_Module* tim_handle;
  27. rt_uint8_t channel;
  28. char *name;
  29. };
  30. static struct n32_pwm n32_pwm_obj[] =
  31. {
  32. #ifdef BSP_USING_TIM3_CH1
  33. PWM1_TIM3_CONFIG,
  34. #endif
  35. #ifdef BSP_USING_TIM3_CH2
  36. PWM2_TIM3_CONFIG,
  37. #endif
  38. #ifdef BSP_USING_TIM3_CH3
  39. PWM3_TIM3_CONFIG,
  40. #endif
  41. #ifdef BSP_USING_TIM3_CH4
  42. PWM4_TIM3_CONFIG,
  43. #endif
  44. };
  45. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg);
  46. static struct rt_pwm_ops drv_ops =
  47. {
  48. drv_pwm_control
  49. };
  50. static rt_err_t drv_pwm_enable(TIM_Module* TIMx, struct rt_pwm_configuration *configuration, rt_bool_t enable)
  51. {
  52. /* Get the value of channel */
  53. rt_uint32_t channel = configuration->channel;
  54. if (!enable)
  55. {
  56. if(channel == 1)
  57. {
  58. TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_DISABLE);
  59. }
  60. else if(channel == 2)
  61. {
  62. TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_DISABLE);
  63. }
  64. else if(channel == 3)
  65. {
  66. TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_DISABLE);
  67. }
  68. else if(channel == 4)
  69. {
  70. TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_DISABLE);
  71. }
  72. }
  73. else
  74. {
  75. if(channel == 1)
  76. {
  77. TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_ENABLE);
  78. }
  79. else if(channel == 2)
  80. {
  81. TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_ENABLE);
  82. }
  83. else if(channel == 3)
  84. {
  85. TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_ENABLE);
  86. }
  87. else if(channel == 4)
  88. {
  89. TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_ENABLE);
  90. }
  91. }
  92. TIM_Enable(TIMx, ENABLE);
  93. return RT_EOK;
  94. }
  95. static rt_err_t drv_pwm_get(TIM_Module* TIMx, struct rt_pwm_configuration *configuration)
  96. {
  97. RCC_ClocksType RCC_Clockstruct;
  98. rt_uint32_t ar, div, cc1, cc2, cc3, cc4;
  99. rt_uint32_t channel = configuration->channel;
  100. rt_uint64_t tim_clock;
  101. ar = TIMx->AR;
  102. div = TIMx->PSC;
  103. cc1 = TIMx->CCDAT1;
  104. cc2 = TIMx->CCDAT2;
  105. cc3 = TIMx->CCDAT3;
  106. cc4 = TIMx->CCDAT4;
  107. RCC_GetClocksFreqValue(&RCC_Clockstruct);
  108. tim_clock = RCC_Clockstruct.Pclk2Freq;
  109. /* Convert nanosecond to frequency and duty cycle. */
  110. tim_clock /= 1000000UL;
  111. configuration->period = (ar + 1) * (div + 1) * 1000UL / tim_clock;
  112. if(channel == 1)
  113. configuration->pulse = (cc1 + 1) * (div + 1) * 1000UL / tim_clock;
  114. if(channel == 2)
  115. configuration->pulse = (cc2 + 1) * (div+ 1) * 1000UL / tim_clock;
  116. if(channel == 3)
  117. configuration->pulse = (cc3 + 1) * (div + 1) * 1000UL / tim_clock;
  118. if(channel == 4)
  119. configuration->pulse = (cc4 + 1) * (div + 1) * 1000UL / tim_clock;
  120. return RT_EOK;
  121. }
  122. static rt_err_t drv_pwm_set(TIM_Module* TIMx, struct rt_pwm_configuration *configuration)
  123. {
  124. /* Init timer pin and enable clock */
  125. n32_msp_tim_init(TIMx);
  126. RCC_ClocksType RCC_Clock;
  127. RCC_GetClocksFreqValue(&RCC_Clock);
  128. rt_uint64_t input_clock;
  129. if ((TIM1 == TIMx) || (TIM8 == TIMx))
  130. {
  131. RCC_ConfigTim18Clk(RCC_TIM18CLK_SRC_SYSCLK);
  132. input_clock = RCC_Clock.SysclkFreq;
  133. }
  134. else
  135. {
  136. if (1 == (RCC_Clock.HclkFreq/RCC_Clock.Pclk1Freq))
  137. input_clock = RCC_Clock.Pclk1Freq;
  138. else
  139. input_clock = RCC_Clock.Pclk1Freq * 2;
  140. }
  141. /* Convert nanosecond to frequency and duty cycle. */
  142. rt_uint32_t period = (unsigned long long)configuration->period ;
  143. rt_uint64_t psc = period / MAX_PERIOD + 1;
  144. period = period / psc;
  145. psc = psc * (input_clock / 1000000);
  146. /* TIMe base configuration */
  147. TIM_TimeBaseInitType TIM_TIMeBaseStructure;
  148. TIM_InitTimBaseStruct(&TIM_TIMeBaseStructure);
  149. TIM_TIMeBaseStructure.Period = period;
  150. TIM_TIMeBaseStructure.Prescaler = psc - 1;
  151. TIM_TIMeBaseStructure.ClkDiv = 0;
  152. TIM_TIMeBaseStructure.CntMode = TIM_CNT_MODE_UP;
  153. TIM_InitTimeBase(TIMx, &TIM_TIMeBaseStructure);
  154. rt_uint32_t pulse = (unsigned long long)configuration->pulse;
  155. /* PWM1 Mode configuration: Channel1 */
  156. OCInitType TIM_OCInitStructure;
  157. TIM_InitOcStruct(&TIM_OCInitStructure);
  158. TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
  159. TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
  160. TIM_OCInitStructure.Pulse = pulse;
  161. TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
  162. rt_uint32_t channel = configuration->channel;
  163. if(channel == 1)
  164. {
  165. TIM_InitOc1(TIMx, &TIM_OCInitStructure);
  166. TIM_ConfigOc1Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
  167. }
  168. else if(channel == 2)
  169. {
  170. TIM_InitOc2(TIMx, &TIM_OCInitStructure);
  171. TIM_ConfigOc2Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
  172. }
  173. else if(channel == 3)
  174. {
  175. TIM_InitOc3(TIMx, &TIM_OCInitStructure);
  176. TIM_ConfigOc3Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
  177. }
  178. else if(channel == 4)
  179. {
  180. TIM_InitOc4(TIMx, &TIM_OCInitStructure);
  181. TIM_ConfigOc4Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
  182. }
  183. TIM_ConfigArPreload(TIMx, ENABLE);
  184. TIM_EnableCtrlPwmOutputs(TIMx, ENABLE);
  185. return RT_EOK;
  186. }
  187. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  188. {
  189. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
  190. TIM_Module *TIMx = (TIM_Module *)device->parent.user_data;
  191. switch (cmd)
  192. {
  193. case PWM_CMD_ENABLE:
  194. return drv_pwm_enable(TIMx, configuration, RT_TRUE);
  195. case PWM_CMD_DISABLE:
  196. return drv_pwm_enable(TIMx, configuration, RT_FALSE);
  197. case PWM_CMD_SET:
  198. return drv_pwm_set(TIMx, configuration);
  199. case PWM_CMD_GET:
  200. return drv_pwm_get(TIMx, configuration);
  201. default:
  202. return RT_EINVAL;
  203. }
  204. }
  205. static int rt_hw_pwm_init(void)
  206. {
  207. int i = 0;
  208. int result = RT_EOK;
  209. for(i = 0; i < sizeof(n32_pwm_obj) / sizeof(n32_pwm_obj[0]); i++)
  210. {
  211. if(rt_device_pwm_register(&n32_pwm_obj[i].pwm_device, n32_pwm_obj[i].name, &drv_ops, n32_pwm_obj[i].tim_handle) == RT_EOK)
  212. {
  213. LOG_D("%s register success", n32_pwm_obj[i].name);
  214. }
  215. else
  216. {
  217. LOG_D("%s register failed", n32_pwm_obj[i].name);
  218. result = -RT_ERROR;
  219. }
  220. }
  221. return result;
  222. }
  223. INIT_BOARD_EXPORT(rt_hw_pwm_init);