drv_pwm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2019-03-11 wangyq the first version
  21. * 2019-11-01 wangyq update libraries
  22. * 2021-04-20 liuhy the second version
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #include <board.h>
  28. #include "es_conf_info_pwm.h"
  29. #ifdef RT_USING_PWM
  30. static void pwm_set_duty(ald_timer_handle_t *timer_initstruct, ald_timer_channel_t ch, uint32_t ns)
  31. {
  32. uint64_t tmp = (uint64_t)ald_cmu_get_pclk_clock() * ns / 1000000000 /
  33. (timer_initstruct->init.prescaler + 1);
  34. if (ch == ALD_TIMER_CHANNEL_1)
  35. WRITE_REG(timer_initstruct->perh->CCVAL1, (uint32_t)tmp);
  36. else if (ch == ALD_TIMER_CHANNEL_2)
  37. WRITE_REG(timer_initstruct->perh->CCVAL2, (uint32_t)tmp);
  38. else if (ch == ALD_TIMER_CHANNEL_3)
  39. WRITE_REG(timer_initstruct->perh->CCVAL3, (uint32_t)tmp);
  40. else if (ch == ALD_TIMER_CHANNEL_4)
  41. WRITE_REG(timer_initstruct->perh->CCVAL4, (uint32_t)tmp);
  42. }
  43. static rt_err_t es32f3_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  44. {
  45. rt_err_t ret = RT_EOK;
  46. uint64_t _arr,bus_speed,tmp;
  47. uint32_t _maxcnt,_ccep_ch_en = 0U;
  48. ald_timer_channel_t pwm_channel;
  49. ald_timer_oc_init_t tim_ocinit;
  50. ald_timer_handle_t *timer_initstruct = (ald_timer_handle_t *)device->parent.user_data;
  51. struct rt_pwm_configuration *cfg = (struct rt_pwm_configuration *)arg;
  52. RT_ASSERT(timer_initstruct != RT_NULL);
  53. /* select pwm output channel */
  54. if (1 == cfg->channel)
  55. {
  56. pwm_channel = ALD_TIMER_CHANNEL_1;
  57. _ccep_ch_en = timer_initstruct->perh->CCEP & TIMER_CCEP_CC1EN_MSK;
  58. }
  59. else if (2 == cfg->channel)
  60. {
  61. pwm_channel = ALD_TIMER_CHANNEL_2;
  62. _ccep_ch_en = timer_initstruct->perh->CCEP & TIMER_CCEP_CC2EN_MSK;
  63. }
  64. else if (3 == cfg->channel)
  65. {
  66. pwm_channel = ALD_TIMER_CHANNEL_3;
  67. _ccep_ch_en = timer_initstruct->perh->CCEP & TIMER_CCEP_CC3EN_MSK;
  68. }
  69. else if (4 == cfg->channel)
  70. {
  71. pwm_channel = ALD_TIMER_CHANNEL_4;
  72. _ccep_ch_en = timer_initstruct->perh->CCEP & TIMER_CCEP_CC4EN_MSK;
  73. }
  74. else
  75. return RT_EINVAL;
  76. switch (cmd)
  77. {
  78. case PWM_CMD_ENABLE:
  79. ald_timer_pwm_start(timer_initstruct, pwm_channel);
  80. break;
  81. case PWM_CMD_DISABLE:
  82. ald_timer_pwm_stop(timer_initstruct, pwm_channel);
  83. break;
  84. case PWM_CMD_SET:
  85. /*当通道没开的时候:关通道,设置输出模式和极性,初始化通道*/
  86. if(!_ccep_ch_en)
  87. {
  88. tim_ocinit.oc_mode = ES_PWM_OC_MODE;
  89. tim_ocinit.oc_polarity = ES_PWM_OC_POLARITY;
  90. tim_ocinit.oc_fast_en = DISABLE;
  91. tim_ocinit.ocn_polarity = ALD_TIMER_OCN_POLARITY_HIGH;
  92. tim_ocinit.ocn_idle = ALD_TIMER_OCN_IDLE_RESET;
  93. tim_ocinit.oc_idle = ALD_TIMER_OC_IDLE_RESET;
  94. ald_timer_oc_config_channel(timer_initstruct, &tim_ocinit, pwm_channel);
  95. }
  96. bus_speed = (uint64_t)ald_cmu_get_pclk_clock();
  97. /*判断外设的计数器最大值*/
  98. #ifdef ES32F36xx
  99. if((timer_initstruct->perh == GP32C4T0)||(timer_initstruct->perh == GP32C4T1))
  100. {
  101. _maxcnt = 0xFFFFFFFF;
  102. }
  103. else _maxcnt = 0xFFFF;
  104. #else
  105. _maxcnt = 0xFFFF;
  106. #endif
  107. /*当最大分频 <= _maxcnt时:估计大概的分频,加快速度 */
  108. tmp = bus_speed * (cfg->period)/1000000000/_maxcnt;
  109. timer_initstruct->init.prescaler = (tmp > 2U) ? (tmp - 2U) : 0U ; /*bus_speed < 500000000*/
  110. /* count registers max , auto adjust prescaler */
  111. do
  112. {
  113. _arr = bus_speed * (cfg->period) / 1000000000 /(++timer_initstruct->init.prescaler);
  114. }
  115. while (_arr > _maxcnt);
  116. WRITE_REG(timer_initstruct->perh->AR, (uint32_t)_arr);
  117. timer_initstruct->init.period = (uint32_t)_arr;
  118. /* update prescaler */
  119. WRITE_REG(timer_initstruct->perh->PRES, --timer_initstruct->init.prescaler);
  120. pwm_set_duty(timer_initstruct, pwm_channel, cfg->pulse);
  121. break;
  122. case PWM_CMD_GET:
  123. cfg->pulse = ald_timer_read_capture_value(timer_initstruct, pwm_channel) * 100 /
  124. READ_REG(timer_initstruct->perh->AR);
  125. break;
  126. default:
  127. break;
  128. }
  129. return ret;
  130. }
  131. const static struct rt_pwm_ops es32f3_pwm_ops =
  132. {
  133. es32f3_pwm_control
  134. };
  135. int rt_hw_pwm_init(void)
  136. {
  137. rt_err_t ret = RT_EOK;
  138. ald_gpio_init_t gpio_initstructure;
  139. gpio_initstructure.mode = ALD_GPIO_MODE_OUTPUT;
  140. gpio_initstructure.od = ALD_GPIO_PUSH_PULL;
  141. gpio_initstructure.pupd = ALD_GPIO_PUSH_UP;
  142. gpio_initstructure.odrv = ALD_GPIO_OUT_DRIVE_NORMAL;
  143. gpio_initstructure.flt = ALD_GPIO_FILTER_DISABLE;
  144. gpio_initstructure.type = ALD_GPIO_TYPE_CMOS;
  145. #ifdef BSP_USING_AD16C4T0_PWM /* 4 channels */
  146. static struct rt_device_pwm ad16c4t0_pwm_dev;
  147. static ald_timer_handle_t ad16c4t0_timer_initstruct;
  148. ad16c4t0_timer_initstruct.perh = AD16C4T;
  149. ald_timer_pwm_init(&ad16c4t0_timer_initstruct);
  150. /* gpio initialization */
  151. #if defined(ES_AD16C4T0_CH1_GPIO_FUNC)&&defined(ES_AD16C4T0_CH1_GPIO_PORT)&&defined(ES_AD16C4T0_CH1_GPIO_PIN)
  152. gpio_initstructure.func = ES_AD16C4T0_CH1_GPIO_FUNC;
  153. ald_gpio_init(ES_AD16C4T0_CH1_GPIO_PORT, ES_AD16C4T0_CH1_GPIO_PIN, &gpio_initstructure);
  154. #endif
  155. #if defined(ES_AD16C4T0_CH2_GPIO_FUNC)&&defined(ES_AD16C4T0_CH2_GPIO_PORT)&&defined(ES_AD16C4T0_CH2_GPIO_PIN)
  156. gpio_initstructure.func = ES_AD16C4T0_CH2_GPIO_FUNC;
  157. ald_gpio_init(ES_AD16C4T0_CH2_GPIO_PORT, ES_AD16C4T0_CH2_GPIO_PIN, &gpio_initstructure);
  158. #endif
  159. #if defined(ES_AD16C4T0_CH3_GPIO_FUNC)&&defined(ES_AD16C4T0_CH3_GPIO_PORT)&&defined(ES_AD16C4T0_CH3_GPIO_FUNC)
  160. gpio_initstructure.func = ES_AD16C4T0_CH3_GPIO_FUNC;
  161. ald_gpio_init(ES_AD16C4T0_CH3_GPIO_PORT, ES_AD16C4T0_CH3_GPIO_PIN, &gpio_initstructure);
  162. #endif
  163. #if defined(ES_AD16C4T0_CH4_GPIO_FUNC)&&defined(ES_AD16C4T0_CH4_GPIO_PORT)&&defined(ES_AD16C4T0_CH4_GPIO_PIN)
  164. gpio_initstructure.func = ES_AD16C4T0_CH4_GPIO_FUNC;
  165. ald_gpio_init(ES_AD16C4T0_CH4_GPIO_PORT, ES_AD16C4T0_CH4_GPIO_PIN, &gpio_initstructure);
  166. #endif
  167. ret = rt_device_pwm_register(&ad16c4t0_pwm_dev, ES_DEVICE_NAME_AD16C4T0_PWM, &es32f3_pwm_ops,
  168. &ad16c4t0_timer_initstruct);
  169. #endif
  170. #ifdef BSP_USING_GP16C4T0_PWM /* 4 channels */
  171. static struct rt_device_pwm gp16c4t0_pwm_dev;
  172. static ald_timer_handle_t gp16c4t0_timer_initstruct;
  173. gp16c4t0_timer_initstruct.perh = GP16C4T0;
  174. ald_timer_pwm_init(&gp16c4t0_timer_initstruct);
  175. /* gpio initialization */
  176. #if defined(ES_GP16C4T0_CH1_GPIO_FUNC)&&defined(ES_GP16C4T0_CH1_GPIO_PORT)&&defined(ES_GP16C4T0_CH1_GPIO_PIN)
  177. gpio_initstructure.func = ES_GP16C4T0_CH1_GPIO_FUNC;
  178. ald_gpio_init(ES_GP16C4T0_CH1_GPIO_PORT, ES_GP16C4T0_CH1_GPIO_PIN, &gpio_initstructure);
  179. #endif
  180. #if defined(ES_GP16C4T0_CH2_GPIO_FUNC)&&defined(ES_GP16C4T0_CH2_GPIO_PORT)&&defined(ES_GP16C4T0_CH2_GPIO_PIN)
  181. gpio_initstructure.func = ES_GP16C4T0_CH2_GPIO_FUNC;
  182. ald_gpio_init(ES_GP16C4T0_CH2_GPIO_PORT, ES_GP16C4T0_CH2_GPIO_PIN, &gpio_initstructure);
  183. #endif
  184. #if defined(ES_GP16C4T0_CH3_GPIO_FUNC)&&defined(ES_GP16C4T0_CH3_GPIO_PORT)&&defined(ES_GP16C4T0_CH3_GPIO_PIN)
  185. gpio_initstructure.func = ES_GP16C4T0_CH3_GPIO_FUNC;
  186. ald_gpio_init(ES_GP16C4T0_CH3_GPIO_PORT, ES_GP16C4T0_CH3_GPIO_PIN, &gpio_initstructure);
  187. #endif
  188. #if defined(ES_GP16C4T0_CH4_GPIO_FUNC)&&defined(ES_GP16C4T0_CH4_GPIO_PORT)&&defined(ES_GP16C4T0_CH4_GPIO_PIN)
  189. gpio_initstructure.func = ES_GP16C4T0_CH4_GPIO_FUNC;
  190. ald_gpio_init(ES_GP16C4T0_CH4_GPIO_PORT, ES_GP16C4T0_CH4_GPIO_PIN, &gpio_initstructure);
  191. #endif
  192. ret = rt_device_pwm_register(&gp16c4t0_pwm_dev, ES_DEVICE_NAME_GP16C4T0_PWM, &es32f3_pwm_ops,
  193. &gp16c4t0_timer_initstruct);
  194. #endif
  195. #ifdef BSP_USING_GP16C4T1_PWM /* 4 channels */
  196. static struct rt_device_pwm gp16c4t1_pwm_dev;
  197. static ald_timer_handle_t gp16c4t1_timer_initstruct;
  198. gp16c4t1_timer_initstruct.perh = GP16C4T1;
  199. ald_timer_pwm_init(&gp16c4t1_timer_initstruct);
  200. /* gpio initialization */
  201. #if defined(ES_GP16C4T1_CH1_GPIO_FUNC)&&defined(ES_GP16C4T1_CH1_GPIO_PORT)&&defined(ES_GP16C4T1_CH1_GPIO_PIN)
  202. gpio_initstructure.func = ES_GP16C4T1_CH1_GPIO_FUNC;
  203. ald_gpio_init(ES_GP16C4T1_CH1_GPIO_PORT, ES_GP16C4T1_CH1_GPIO_PIN, &gpio_initstructure);
  204. #endif
  205. #if defined(ES_GP16C4T1_CH2_GPIO_FUNC)&&defined(ES_GP16C4T1_CH2_GPIO_PORT)&&defined(ES_GP16C4T1_CH2_GPIO_PIN)
  206. gpio_initstructure.func = ES_GP16C4T1_CH2_GPIO_FUNC;
  207. ald_gpio_init(ES_GP16C4T1_CH2_GPIO_PORT, ES_GP16C4T1_CH2_GPIO_PIN, &gpio_initstructure);
  208. #endif
  209. #if defined(ES_GP16C4T1_CH3_GPIO_FUNC)&&defined(ES_GP16C4T1_CH3_GPIO_PORT)&&defined(ES_GP16C4T1_CH3_GPIO_PIN)
  210. gpio_initstructure.func = ES_GP16C4T1_CH3_GPIO_FUNC;
  211. ald_gpio_init(ES_GP16C4T1_CH3_GPIO_PORT, ES_GP16C4T1_CH3_GPIO_PIN, &gpio_initstructure);
  212. #endif
  213. #if defined(ES_GP16C4T1_CH4_GPIO_FUNC)&&defined(ES_GP16C4T1_CH4_GPIO_PORT)&&defined(ES_GP16C4T1_CH4_GPIO_PIN)
  214. gpio_initstructure.func = ES_GP16C4T1_CH4_GPIO_FUNC;
  215. ald_gpio_init(ES_GP16C4T1_CH4_GPIO_PORT, ES_GP16C4T1_CH4_GPIO_PIN, &gpio_initstructure);
  216. #endif
  217. ret = rt_device_pwm_register(&gp16c4t1_pwm_dev, ES_DEVICE_NAME_GP16C4T1_PWM, &es32f3_pwm_ops,
  218. &gp16c4t1_timer_initstruct);
  219. #endif
  220. #ifdef BSP_USING_GP16C4T2_PWM /* 4 channels */
  221. static struct rt_device_pwm gp16c4t2_pwm_dev;
  222. static ald_timer_handle_t gp16c4t2_timer_initstruct;
  223. gp16c4t2_timer_initstruct.perh = GP16C4T2;
  224. ald_timer_pwm_init(&gp16c4t2_timer_initstruct);
  225. /* gpio initialization */
  226. #if defined(ES_GP16C4T2_CH1_GPIO_FUNC)&&defined(ES_GP16C4T2_CH1_GPIO_PORT)&&defined(ES_GP16C4T2_CH1_GPIO_PIN)
  227. gpio_initstructure.func = ES_GP16C4T2_CH1_GPIO_FUNC;
  228. ald_gpio_init(ES_GP16C4T2_CH1_GPIO_PORT, ES_GP16C4T2_CH1_GPIO_PIN, &gpio_initstructure);
  229. #endif
  230. #if defined(ES_GP16C4T2_CH2_GPIO_FUNC)&&defined(ES_GP16C4T2_CH2_GPIO_PORT)&&defined(ES_GP16C4T2_CH2_GPIO_PIN)
  231. gpio_initstructure.func = ES_GP16C4T2_CH2_GPIO_FUNC;
  232. ald_gpio_init(ES_GP16C4T2_CH2_GPIO_PORT, ES_GP16C4T2_CH2_GPIO_PIN, &gpio_initstructure);
  233. #endif
  234. #if defined(ES_GP16C4T2_CH3_GPIO_FUNC)&&defined(ES_GP16C4T2_CH3_GPIO_PORT)&&defined(ES_GP16C4T2_CH3_GPIO_PIN)
  235. gpio_initstructure.func = ES_GP16C4T2_CH3_GPIO_FUNC;
  236. ald_gpio_init(ES_GP16C4T2_CH3_GPIO_PORT, ES_GP16C4T2_CH3_GPIO_PIN, &gpio_initstructure);
  237. #endif
  238. #if defined(ES_GP16C4T2_CH4_GPIO_FUNC)&&defined(ES_GP16C4T2_CH4_GPIO_PORT)&&defined(ES_GP16C4T2_CH4_GPIO_PIN)
  239. gpio_initstructure.func = ES_GP16C4T2_CH4_GPIO_FUNC;
  240. ald_gpio_init(ES_GP16C4T2_CH4_GPIO_PORT, ES_GP16C4T2_CH4_GPIO_PIN, &gpio_initstructure);
  241. #endif
  242. ret = rt_device_pwm_register(&gp16c4t2_pwm_dev, ES_DEVICE_NAME_GP16C4T2_PWM, &es32f3_pwm_ops,
  243. &gp16c4t2_timer_initstruct);
  244. #endif
  245. return ret;
  246. }
  247. INIT_DEVICE_EXPORT(rt_hw_pwm_init);
  248. #endif