drv_pwm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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(timer_handle_t *timer_initstruct, timer_channel_t ch, uint32_t ns)
  31. {
  32. uint64_t tmp = (uint64_t)ald_cmu_get_pclk1_clock() * ns / 1000000000 /
  33. (timer_initstruct->init.prescaler + 1);
  34. if (ch == TIMER_CHANNEL_1)
  35. WRITE_REG(timer_initstruct->perh->CCVAL1, (uint32_t)tmp);
  36. else if (ch == TIMER_CHANNEL_2)
  37. WRITE_REG(timer_initstruct->perh->CCVAL2, (uint32_t)tmp);
  38. else if (ch == TIMER_CHANNEL_3)
  39. WRITE_REG(timer_initstruct->perh->CCVAL3, (uint32_t)tmp);
  40. else if (ch == TIMER_CHANNEL_4)
  41. WRITE_REG(timer_initstruct->perh->CCVAL4, (uint32_t)tmp);
  42. }
  43. static rt_err_t es32f0_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. timer_channel_t pwm_channel;
  49. timer_oc_init_t tim_ocinit;
  50. timer_handle_t *timer_initstruct = (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 = 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 = 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 = 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 = 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 = TIMER_OCN_POLARITY_HIGH;
  92. tim_ocinit.ocn_idle = TIMER_OCN_IDLE_RESET;
  93. tim_ocinit.oc_idle = 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_pclk1_clock();
  97. /*外设的计数器最大值*/
  98. _maxcnt = 0xFFFF;
  99. /*当最大分频 <= _maxcnt时:估计大概的分频,加快速度 */
  100. tmp = bus_speed * (cfg->period)/1000000000/_maxcnt;
  101. timer_initstruct->init.prescaler = (tmp > 2U) ? (tmp - 2U) : 0U ; /*bus_speed < 500000000*/
  102. /* count registers max , auto adjust prescaler */
  103. do
  104. {
  105. _arr = bus_speed * (cfg->period) / 1000000000 /(++timer_initstruct->init.prescaler);
  106. }
  107. while (_arr > _maxcnt);
  108. WRITE_REG(timer_initstruct->perh->AR, (uint32_t)_arr);
  109. timer_initstruct->init.period = (uint32_t)_arr;
  110. /* update prescaler */
  111. WRITE_REG(timer_initstruct->perh->PRES, --timer_initstruct->init.prescaler);
  112. pwm_set_duty(timer_initstruct, pwm_channel, cfg->pulse);
  113. break;
  114. case PWM_CMD_GET:
  115. cfg->pulse = ald_timer_read_capture_value(timer_initstruct, pwm_channel) * 100 /
  116. READ_REG(timer_initstruct->perh->AR);
  117. break;
  118. default:
  119. break;
  120. }
  121. return ret;
  122. }
  123. const static struct rt_pwm_ops es32f0_pwm_ops =
  124. {
  125. es32f0_pwm_control
  126. };
  127. int rt_hw_pwm_init(void)
  128. {
  129. rt_err_t ret = RT_EOK;
  130. gpio_init_t gpio_initstructure;
  131. gpio_initstructure.mode = GPIO_MODE_OUTPUT;
  132. gpio_initstructure.odos = GPIO_PUSH_PULL;
  133. gpio_initstructure.pupd = GPIO_PUSH_UP;
  134. gpio_initstructure.odrv = GPIO_OUT_DRIVE_NORMAL;
  135. gpio_initstructure.flt = GPIO_FILTER_DISABLE;
  136. gpio_initstructure.type = GPIO_TYPE_TTL;
  137. #ifdef BSP_USING_AD16C4T0_PWM /* 4 channels */
  138. static struct rt_device_pwm ad16c4t0_pwm_dev;
  139. static timer_handle_t ad16c4t0_timer_initstruct;
  140. ad16c4t0_timer_initstruct.perh = AD16C4T0;
  141. ald_timer_pwm_init(&ad16c4t0_timer_initstruct);
  142. /* gpio initialization */
  143. #if defined(ES_AD16C4T0_CH1_GPIO_FUNC)&&defined(ES_AD16C4T0_CH1_GPIO_PORT)&&defined(ES_AD16C4T0_CH1_GPIO_PIN)
  144. gpio_initstructure.func = ES_AD16C4T0_CH1_GPIO_FUNC;
  145. ald_gpio_init(ES_AD16C4T0_CH1_GPIO_PORT, ES_AD16C4T0_CH1_GPIO_PIN, &gpio_initstructure);
  146. #endif
  147. #if defined(ES_AD16C4T0_CH2_GPIO_FUNC)&&defined(ES_AD16C4T0_CH2_GPIO_PORT)&&defined(ES_AD16C4T0_CH2_GPIO_PIN)
  148. gpio_initstructure.func = ES_AD16C4T0_CH2_GPIO_FUNC;
  149. ald_gpio_init(ES_AD16C4T0_CH2_GPIO_PORT, ES_AD16C4T0_CH2_GPIO_PIN, &gpio_initstructure);
  150. #endif
  151. #if defined(ES_AD16C4T0_CH3_GPIO_FUNC)&&defined(ES_AD16C4T0_CH3_GPIO_PORT)&&defined(ES_AD16C4T0_CH3_GPIO_FUNC)
  152. gpio_initstructure.func = ES_AD16C4T0_CH3_GPIO_FUNC;
  153. ald_gpio_init(ES_AD16C4T0_CH3_GPIO_PORT, ES_AD16C4T0_CH3_GPIO_PIN, &gpio_initstructure);
  154. #endif
  155. #if defined(ES_AD16C4T0_CH4_GPIO_FUNC)&&defined(ES_AD16C4T0_CH4_GPIO_PORT)&&defined(ES_AD16C4T0_CH4_GPIO_PIN)
  156. gpio_initstructure.func = ES_AD16C4T0_CH4_GPIO_FUNC;
  157. ald_gpio_init(ES_AD16C4T0_CH4_GPIO_PORT, ES_AD16C4T0_CH4_GPIO_PIN, &gpio_initstructure);
  158. #endif
  159. ret = rt_device_pwm_register(&ad16c4t0_pwm_dev, ES_DEVICE_NAME_AD16C4T0_PWM, &es32f0_pwm_ops,
  160. &ad16c4t0_timer_initstruct);
  161. #endif
  162. #ifdef BSP_USING_GP16C4T0_PWM /* 4 channels */
  163. static struct rt_device_pwm gp16c4t0_pwm_dev;
  164. static timer_handle_t gp16c4t0_timer_initstruct;
  165. gp16c4t0_timer_initstruct.perh = GP16C4T0;
  166. ald_timer_pwm_init(&gp16c4t0_timer_initstruct);
  167. /* gpio initialization */
  168. #if defined(ES_GP16C4T0_CH1_GPIO_FUNC)&&defined(ES_GP16C4T0_CH1_GPIO_PORT)&&defined(ES_GP16C4T0_CH1_GPIO_PIN)
  169. gpio_initstructure.func = ES_GP16C4T0_CH1_GPIO_FUNC;
  170. ald_gpio_init(ES_GP16C4T0_CH1_GPIO_PORT, ES_GP16C4T0_CH1_GPIO_PIN, &gpio_initstructure);
  171. #endif
  172. #if defined(ES_GP16C4T0_CH2_GPIO_FUNC)&&defined(ES_GP16C4T0_CH2_GPIO_PORT)&&defined(ES_GP16C4T0_CH2_GPIO_PIN)
  173. gpio_initstructure.func = ES_GP16C4T0_CH2_GPIO_FUNC;
  174. ald_gpio_init(ES_GP16C4T0_CH2_GPIO_PORT, ES_GP16C4T0_CH2_GPIO_PIN, &gpio_initstructure);
  175. #endif
  176. #if defined(ES_GP16C4T0_CH3_GPIO_FUNC)&&defined(ES_GP16C4T0_CH3_GPIO_PORT)&&defined(ES_GP16C4T0_CH3_GPIO_PIN)
  177. gpio_initstructure.func = ES_GP16C4T0_CH3_GPIO_FUNC;
  178. ald_gpio_init(ES_GP16C4T0_CH3_GPIO_PORT, ES_GP16C4T0_CH3_GPIO_PIN, &gpio_initstructure);
  179. #endif
  180. #if defined(ES_GP16C4T0_CH4_GPIO_FUNC)&&defined(ES_GP16C4T0_CH4_GPIO_PORT)&&defined(ES_GP16C4T0_CH4_GPIO_PIN)
  181. gpio_initstructure.func = ES_GP16C4T0_CH4_GPIO_FUNC;
  182. ald_gpio_init(ES_GP16C4T0_CH4_GPIO_PORT, ES_GP16C4T0_CH4_GPIO_PIN, &gpio_initstructure);
  183. #endif
  184. ret = rt_device_pwm_register(&gp16c4t0_pwm_dev, ES_DEVICE_NAME_GP16C4T0_PWM, &es32f0_pwm_ops,
  185. &gp16c4t0_timer_initstruct);
  186. #endif
  187. #ifdef BSP_USING_GP16C2T0_PWM /* 2 channels */
  188. static struct rt_device_pwm gp16c2t0_pwm_dev;
  189. static timer_handle_t gp16c2t0_timer_initstruct;
  190. gp16c2t0_timer_initstruct.perh = GP16C2T0;
  191. ald_timer_pwm_init(&gp16c2t0_timer_initstruct);
  192. /* gpio initialization */
  193. #if defined(ES_GP16C2T0_CH1_GPIO_FUNC)&&defined(ES_GP16C2T0_CH1_GPIO_PORT)&&defined(ES_GP16C2T0_CH1_GPIO_PIN)
  194. gpio_initstructure.func = ES_GP16C2T0_CH1_GPIO_FUNC;
  195. ald_gpio_init(ES_GP16C2T0_CH1_GPIO_PORT, ES_GP16C2T0_CH1_GPIO_PIN, &gpio_initstructure);
  196. #endif
  197. #if defined(ES_GP16C2T0_CH2_GPIO_FUNC)&&defined(ES_GP16C2T0_CH2_GPIO_PORT)&&defined(ES_GP16C2T0_CH2_GPIO_PIN)
  198. gpio_initstructure.func = ES_GP16C2T0_CH2_GPIO_FUNC;
  199. ald_gpio_init(ES_GP16C2T0_CH2_GPIO_PORT, ES_GP16C2T0_CH2_GPIO_PIN, &gpio_initstructure);
  200. #endif
  201. ret = rt_device_pwm_register(&gp16c2t0_pwm_dev, ES_DEVICE_NAME_GP16C2T0_PWM, &es32f0_pwm_ops,
  202. &gp16c2t0_timer_initstruct);
  203. #endif
  204. #ifdef BSP_USING_GP16C2T1_PWM /* 2 channels */
  205. static struct rt_device_pwm gp16c2t1_pwm_dev;
  206. static timer_handle_t gp16c2t1_timer_initstruct;
  207. gp16c2t1_timer_initstruct.perh = GP16C2T1;
  208. ald_timer_pwm_init(&gp16c2t1_timer_initstruct);
  209. /* gpio initialization */
  210. #if defined(ES_GP16C2T1_CH1_GPIO_FUNC)&&defined(ES_GP16C2T1_CH1_GPIO_PORT)&&defined(ES_GP16C2T1_CH1_GPIO_PIN)
  211. gpio_initstructure.func = ES_GP16C2T1_CH1_GPIO_FUNC;
  212. ald_gpio_init(ES_GP16C2T1_CH1_GPIO_PORT, ES_GP16C2T1_CH1_GPIO_PIN, &gpio_initstructure);
  213. #endif
  214. #if defined(ES_GP16C2T1_CH2_GPIO_FUNC)&&defined(ES_GP16C2T1_CH2_GPIO_PORT)&&defined(ES_GP16C2T1_CH2_GPIO_PIN)
  215. gpio_initstructure.func = ES_GP16C2T1_CH2_GPIO_FUNC;
  216. ald_gpio_init(ES_GP16C2T1_CH2_GPIO_PORT, ES_GP16C2T1_CH2_GPIO_PIN, &gpio_initstructure);
  217. #endif
  218. ret = rt_device_pwm_register(&gp16c2t1_pwm_dev, ES_DEVICE_NAME_GP16C2T1_PWM, &es32f0_pwm_ops,
  219. &gp16c2t1_timer_initstruct);
  220. #endif
  221. return ret;
  222. }
  223. INIT_DEVICE_EXPORT(rt_hw_pwm_init);
  224. #endif