drv_pwm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_TMR1_CH1) && !defined(BSP_USING_TMR1_CH2) && \
  14. !defined(BSP_USING_TMR1_CH3) && !defined(BSP_USING_TMR1_CH4) && \
  15. !defined(BSP_USING_TMR2_CH1) && !defined(BSP_USING_TMR2_CH2) && \
  16. !defined(BSP_USING_TMR2_CH3) && !defined(BSP_USING_TMR2_CH4) && \
  17. !defined(BSP_USING_TMR3_CH1) && !defined(BSP_USING_TMR3_CH2) && \
  18. !defined(BSP_USING_TMR3_CH3) && !defined(BSP_USING_TMR3_CH4)
  19. #error "Please define at least one BSP_USING_TMRx_CHx"
  20. #endif
  21. #endif /* RT_USING_PWM */
  22. #define DRV_DEBUG
  23. #define LOG_TAG "drv.pwm"
  24. #include <drv_log.h>
  25. #define MAX_PERIOD 65535
  26. struct rt_device_pwm pwm_device;
  27. struct at32_pwm
  28. {
  29. struct rt_device_pwm pwm_device;
  30. TMR_Type* tim_handle;
  31. rt_uint8_t channel;
  32. char *name;
  33. };
  34. static struct at32_pwm at32_pwm_obj[] =
  35. {
  36. #ifdef BSP_USING_TMR1_CH1
  37. PWM1_CONFIG,
  38. #endif
  39. #ifdef BSP_USING_TMR1_CH2
  40. PWM2_CONFIG,
  41. #endif
  42. #ifdef BSP_USING_TMR1_CH3
  43. PWM3_CONFIG,
  44. #endif
  45. #ifdef BSP_USING_TMR1_CH4
  46. PWM4_CONFIG,
  47. #endif
  48. #ifdef BSP_USING_TMR2_CH1
  49. PWM5_CONFIG,
  50. #endif
  51. #ifdef BSP_USING_TMR2_CH2
  52. PWM6_CONFIG,
  53. #endif
  54. #ifdef BSP_USING_TMR2_CH3
  55. PWM7_CONFIG,
  56. #endif
  57. #ifdef BSP_USING_TMR2_CH4
  58. PWM8_CONFIG,
  59. #endif
  60. #ifdef BSP_USING_TMR3_CH1
  61. PWM9_CONFIG,
  62. #endif
  63. #ifdef BSP_USING_TMR3_CH2
  64. PWM10_CONFIG,
  65. #endif
  66. #ifdef BSP_USING_TMR3_CH3
  67. PWM11_CONFIG,
  68. #endif
  69. #ifdef BSP_USING_TMR3_CH4
  70. PWM12_CONFIG,
  71. #endif
  72. };
  73. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg);
  74. static struct rt_pwm_ops drv_ops =
  75. {
  76. drv_pwm_control
  77. };
  78. static rt_err_t drv_pwm_enable(TMR_Type* TMRx, struct rt_pwm_configuration *configuration, rt_bool_t enable)
  79. {
  80. /* Get the value of channel */
  81. rt_uint32_t channel = configuration->channel;
  82. if (!enable)
  83. {
  84. if(channel == 1)
  85. {
  86. TMR_CCxCmd(TMRx, TMR_Channel_1, TMR_CCx_Disable);
  87. }
  88. else if(channel == 2)
  89. {
  90. TMR_CCxCmd(TMRx, TMR_Channel_2, TMR_CCx_Disable);
  91. }
  92. else if(channel == 3)
  93. {
  94. TMR_CCxCmd(TMRx, TMR_Channel_3, TMR_CCx_Disable);
  95. }
  96. else if(channel == 4)
  97. {
  98. TMR_CCxCmd(TMRx, TMR_Channel_4, TMR_CCx_Disable);
  99. }
  100. }
  101. else
  102. {
  103. if(channel == 1)
  104. {
  105. TMR_CCxCmd(TMRx, TMR_Channel_1, TMR_CCx_Enable);
  106. }
  107. else if(channel == 2)
  108. {
  109. TMR_CCxCmd(TMRx, TMR_Channel_1, TMR_CCx_Enable);
  110. }
  111. else if(channel == 3)
  112. {
  113. TMR_CCxCmd(TMRx, TMR_Channel_1, TMR_CCx_Enable);
  114. }
  115. else if(channel == 4)
  116. {
  117. TMR_CCxCmd(TMRx, TMR_Channel_1, TMR_CCx_Enable);
  118. }
  119. }
  120. /* TMRx enable counter */
  121. TMR_Cmd(TMRx, ENABLE);
  122. return RT_EOK;
  123. }
  124. static rt_err_t drv_pwm_get(TMR_Type* TMRx, struct rt_pwm_configuration *configuration)
  125. {
  126. RCC_ClockType RCC_Clockstruct;
  127. rt_uint32_t ar, div, cc1, cc2, cc3, cc4;
  128. rt_uint32_t channel = configuration->channel;
  129. rt_uint64_t tim_clock;
  130. ar = TMRx->AR;
  131. div = TMRx->DIV;
  132. cc1 = TMRx->CC1;
  133. cc2 = TMRx->CC2;
  134. cc3 = TMRx->CC3;
  135. cc4 = TMRx->CC4;
  136. RCC_GetClocksFreq(&RCC_Clockstruct);
  137. tim_clock = RCC_Clockstruct.APB2CLK_Freq;
  138. /* Convert nanosecond to frequency and duty cycle. */
  139. tim_clock /= 1000000UL;
  140. configuration->period = (ar + 1) * (div + 1) * 1000UL / tim_clock;
  141. if(channel == 1)
  142. configuration->pulse = (cc1 + 1) * (div + 1) * 1000UL / tim_clock;
  143. if(channel == 2)
  144. configuration->pulse = (cc2 + 1) * (div+ 1) * 1000UL / tim_clock;
  145. if(channel == 3)
  146. configuration->pulse = (cc3 + 1) * (div + 1) * 1000UL / tim_clock;
  147. if(channel == 4)
  148. configuration->pulse = (cc4 + 1) * (div + 1) * 1000UL / tim_clock;
  149. return RT_EOK;
  150. }
  151. static rt_err_t drv_pwm_set(TMR_Type* TMRx, struct rt_pwm_configuration *configuration)
  152. {
  153. TMR_TimerBaseInitType TMR_TMReBaseStructure;
  154. TMR_OCInitType TMR_OCInitStructure;
  155. rt_uint32_t period, pulse;
  156. rt_uint64_t psc;
  157. /* Get the channel number */
  158. rt_uint32_t channel = configuration->channel;
  159. /* Init timer pin and enable clock */
  160. at32_msp_tmr_init(TMRx);
  161. /* Convert nanosecond to frequency and duty cycle. */
  162. period = (unsigned long long)configuration->period ;
  163. psc = period / MAX_PERIOD + 1;
  164. period = period / psc;
  165. /* TMRe base configuration */
  166. TMR_TimeBaseStructInit(&TMR_TMReBaseStructure);
  167. TMR_TMReBaseStructure.TMR_Period = period;
  168. TMR_TMReBaseStructure.TMR_DIV = psc - 1;
  169. TMR_TMReBaseStructure.TMR_ClockDivision = 0;
  170. TMR_TMReBaseStructure.TMR_CounterMode = TMR_CounterDIR_Up;
  171. TMR_TimeBaseInit(TMRx, &TMR_TMReBaseStructure);
  172. pulse = (unsigned long long)configuration->pulse;
  173. /* PWM1 Mode configuration: Channel1 */
  174. TMR_OCStructInit(&TMR_OCInitStructure);
  175. TMR_OCInitStructure.TMR_OCMode = TMR_OCMode_PWM1;
  176. TMR_OCInitStructure.TMR_OutputState = TMR_OutputState_Enable;
  177. TMR_OCInitStructure.TMR_Pulse = pulse;
  178. TMR_OCInitStructure.TMR_OCPolarity = TMR_OCPolarity_High;
  179. if(channel == 1)
  180. {
  181. TMR_OC1Init(TMRx, &TMR_OCInitStructure);
  182. TMR_OC1PreloadConfig(TMRx, TMR_OCPreload_Enable);
  183. }
  184. else if(channel == 2)
  185. {
  186. TMR_OC2Init(TMRx, &TMR_OCInitStructure);
  187. TMR_OC2PreloadConfig(TMRx, TMR_OCPreload_Enable);
  188. }
  189. else if(channel == 3)
  190. {
  191. TMR_OC3Init(TMRx, &TMR_OCInitStructure);
  192. TMR_OC3PreloadConfig(TMRx, TMR_OCPreload_Enable);
  193. }
  194. else if(channel == 4)
  195. {
  196. TMR_OC4Init(TMRx, &TMR_OCInitStructure);
  197. TMR_OC4PreloadConfig(TMRx, TMR_OCPreload_Enable);
  198. }
  199. TMR_ARPreloadConfig(TMRx, ENABLE);
  200. #if defined (SOC_SERIES_AT32F415)
  201. if(TMRx == TMR1)
  202. #else
  203. if(TMRx == TMR1 || TMRx == TMR8)
  204. #endif
  205. {
  206. TMR_CtrlPWMOutputs(TMRx,ENABLE);
  207. }
  208. return RT_EOK;
  209. }
  210. static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  211. {
  212. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
  213. TMR_Type *TMRx = (TMR_Type *)device->parent.user_data;
  214. switch (cmd)
  215. {
  216. case PWM_CMD_ENABLE:
  217. return drv_pwm_enable(TMRx, configuration, RT_TRUE);
  218. case PWM_CMD_DISABLE:
  219. return drv_pwm_enable(TMRx, configuration, RT_FALSE);
  220. case PWM_CMD_SET:
  221. return drv_pwm_set(TMRx, configuration);
  222. case PWM_CMD_GET:
  223. return drv_pwm_get(TMRx, configuration);
  224. default:
  225. return RT_EINVAL;
  226. }
  227. }
  228. static int rt_hw_pwm_init(void)
  229. {
  230. int i = 0;
  231. int result = RT_EOK;
  232. for(i = 0; i < sizeof(at32_pwm_obj) / sizeof(at32_pwm_obj[0]); i++)
  233. {
  234. if(rt_device_pwm_register(&at32_pwm_obj[i].pwm_device, at32_pwm_obj[i].name, &drv_ops, at32_pwm_obj[i].tim_handle) == RT_EOK)
  235. {
  236. LOG_D("%s register success", at32_pwm_obj[i].name);
  237. }
  238. else
  239. {
  240. LOG_D("%s register failed", at32_pwm_obj[i].name);
  241. result = -RT_ERROR;
  242. }
  243. }
  244. return result;
  245. }
  246. INIT_BOARD_EXPORT(rt_hw_pwm_init);