drv_pwm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 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. 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. #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. gpio_init_t gpio_initstructure;
  139. gpio_initstructure.mode = GPIO_MODE_OUTPUT;
  140. gpio_initstructure.odos = GPIO_PUSH_PULL;
  141. gpio_initstructure.pupd = GPIO_PUSH_UP;
  142. gpio_initstructure.podrv = GPIO_OUT_DRIVE_6;
  143. gpio_initstructure.nodrv = GPIO_OUT_DRIVE_6;
  144. gpio_initstructure.flt = GPIO_FILTER_DISABLE;
  145. gpio_initstructure.type = GPIO_TYPE_TTL;
  146. #ifdef BSP_USING_AD16C4T0_PWM /* 4 channels */
  147. static struct rt_device_pwm ad16c4t0_pwm_dev;
  148. static timer_handle_t ad16c4t0_timer_initstruct;
  149. ad16c4t0_timer_initstruct.perh = AD16C4T0;
  150. ald_timer_pwm_init(&ad16c4t0_timer_initstruct);
  151. /* gpio initialization */
  152. #if defined(ES_AD16C4T0_CH1_GPIO_FUNC)&&defined(ES_AD16C4T0_CH1_GPIO_PORT)&&defined(ES_AD16C4T0_CH1_GPIO_PIN)
  153. gpio_initstructure.func = ES_AD16C4T0_CH1_GPIO_FUNC;
  154. ald_gpio_init(ES_AD16C4T0_CH1_GPIO_PORT, ES_AD16C4T0_CH1_GPIO_PIN, &gpio_initstructure);
  155. #endif
  156. #if defined(ES_AD16C4T0_CH2_GPIO_FUNC)&&defined(ES_AD16C4T0_CH2_GPIO_PORT)&&defined(ES_AD16C4T0_CH2_GPIO_PIN)
  157. gpio_initstructure.func = ES_AD16C4T0_CH2_GPIO_FUNC;
  158. ald_gpio_init(ES_AD16C4T0_CH2_GPIO_PORT, ES_AD16C4T0_CH2_GPIO_PIN, &gpio_initstructure);
  159. #endif
  160. #if defined(ES_AD16C4T0_CH3_GPIO_FUNC)&&defined(ES_AD16C4T0_CH3_GPIO_PORT)&&defined(ES_AD16C4T0_CH3_GPIO_FUNC)
  161. gpio_initstructure.func = ES_AD16C4T0_CH3_GPIO_FUNC;
  162. ald_gpio_init(ES_AD16C4T0_CH3_GPIO_PORT, ES_AD16C4T0_CH3_GPIO_PIN, &gpio_initstructure);
  163. #endif
  164. #if defined(ES_AD16C4T0_CH4_GPIO_FUNC)&&defined(ES_AD16C4T0_CH4_GPIO_PORT)&&defined(ES_AD16C4T0_CH4_GPIO_PIN)
  165. gpio_initstructure.func = ES_AD16C4T0_CH4_GPIO_FUNC;
  166. ald_gpio_init(ES_AD16C4T0_CH4_GPIO_PORT, ES_AD16C4T0_CH4_GPIO_PIN, &gpio_initstructure);
  167. #endif
  168. ret = rt_device_pwm_register(&ad16c4t0_pwm_dev, ES_DEVICE_NAME_AD16C4T0_PWM, &es32f3_pwm_ops,
  169. &ad16c4t0_timer_initstruct);
  170. #endif
  171. #ifdef BSP_USING_AD16C4T1_PWM /* 4 channels */
  172. static struct rt_device_pwm ad16c4t1_pwm_dev;
  173. static timer_handle_t ad16c4t1_timer_initstruct;
  174. ad16c4t1_timer_initstruct.perh = AD16C4T1;
  175. ald_timer_pwm_init(&ad16c4t1_timer_initstruct);
  176. /* gpio initialization */
  177. #if defined(ES_AD16C4T1_CH1_GPIO_FUNC)&&defined(ES_AD16C4T1_CH1_GPIO_PORT)&&defined(ES_AD16C4T1_CH1_GPIO_PIN)
  178. gpio_initstructure.func = ES_AD16C4T1_CH1_GPIO_FUNC;
  179. ald_gpio_init(ES_AD16C4T1_CH1_GPIO_PORT, ES_AD16C4T1_CH1_GPIO_PIN, &gpio_initstructure);
  180. #endif
  181. #if defined(ES_AD16C4T1_CH2_GPIO_FUNC)&&defined(ES_AD16C4T1_CH2_GPIO_PORT)&&defined(ES_AD16C4T1_CH2_GPIO_PIN)
  182. gpio_initstructure.func = ES_AD16C4T1_CH2_GPIO_FUNC;
  183. ald_gpio_init(ES_AD16C4T1_CH2_GPIO_PORT, ES_AD16C4T1_CH2_GPIO_PIN, &gpio_initstructure);
  184. #endif
  185. #if defined(ES_AD16C4T1_CH3_GPIO_FUNC)&&defined(ES_AD16C4T1_CH3_GPIO_PORT)&&defined(ES_AD16C4T1_CH3_GPIO_PIN)
  186. gpio_initstructure.func = ES_AD16C4T1_CH3_GPIO_FUNC;
  187. ald_gpio_init(ES_AD16C4T1_CH3_GPIO_PORT, ES_AD16C4T1_CH3_GPIO_PIN, &gpio_initstructure);
  188. #endif
  189. #if defined(ES_AD16C4T1_CH4_GPIO_FUNC)&&defined(ES_AD16C4T1_CH4_GPIO_PORT)&&defined(ES_AD16C4T1_CH4_GPIO_PIN)
  190. gpio_initstructure.func = ES_AD16C4T1_CH4_GPIO_FUNC;
  191. ald_gpio_init(ES_AD16C4T1_CH4_GPIO_PORT, ES_AD16C4T1_CH4_GPIO_PIN, &gpio_initstructure);
  192. #endif
  193. ret = rt_device_pwm_register(&ad16c4t1_pwm_dev, ES_DEVICE_NAME_AD16C4T1_PWM, &es32f3_pwm_ops,
  194. &ad16c4t1_timer_initstruct);
  195. #endif
  196. #ifdef BSP_USING_GP32C4T0_PWM /* 4 channels */
  197. static struct rt_device_pwm gp32c4t0_pwm_dev;
  198. static timer_handle_t gp32c4t0_timer_initstruct;
  199. gp32c4t0_timer_initstruct.perh = GP32C4T0;
  200. ald_timer_pwm_init(&gp32c4t0_timer_initstruct);
  201. /* gpio initialization */
  202. #if defined(ES_GP32C4T0_CH1_GPIO_FUNC)&&defined(ES_GP32C4T0_CH1_GPIO_PORT)&&defined(ES_GP32C4T0_CH1_GPIO_PIN)
  203. gpio_initstructure.func = ES_GP32C4T0_CH1_GPIO_FUNC;
  204. ald_gpio_init(ES_GP32C4T0_CH1_GPIO_PORT, ES_GP32C4T0_CH1_GPIO_PIN, &gpio_initstructure);
  205. #endif
  206. #if defined(ES_GP32C4T0_CH2_GPIO_FUNC)&&defined(ES_GP32C4T0_CH2_GPIO_PORT)&&defined(ES_GP32C4T0_CH2_GPIO_PIN)
  207. gpio_initstructure.func = ES_GP32C4T0_CH2_GPIO_FUNC;
  208. ald_gpio_init(ES_GP32C4T0_CH2_GPIO_PORT, ES_GP32C4T0_CH2_GPIO_PIN, &gpio_initstructure);
  209. #endif
  210. #if defined(ES_GP32C4T0_CH3_GPIO_FUNC)&&defined(ES_GP32C4T0_CH3_GPIO_PORT)&&defined(ES_GP32C4T0_CH3_GPIO_PIN)
  211. gpio_initstructure.func = ES_GP32C4T0_CH3_GPIO_FUNC;
  212. ald_gpio_init(ES_GP32C4T0_CH3_GPIO_PORT, ES_GP32C4T0_CH3_GPIO_PIN, &gpio_initstructure);
  213. #endif
  214. #if defined(ES_GP32C4T0_CH4_GPIO_FUNC)&&defined(ES_GP32C4T0_CH4_GPIO_PORT)&&defined(ES_GP32C4T0_CH4_GPIO_PIN)
  215. gpio_initstructure.func = ES_GP32C4T0_CH4_GPIO_FUNC;
  216. ald_gpio_init(ES_GP32C4T0_CH4_GPIO_PORT, ES_GP32C4T0_CH4_GPIO_PIN, &gpio_initstructure);
  217. #endif
  218. ret = rt_device_pwm_register(&gp32c4t0_pwm_dev, ES_DEVICE_NAME_GP32C4T0_PWM, &es32f3_pwm_ops,
  219. &gp32c4t0_timer_initstruct);
  220. #endif
  221. #ifdef BSP_USING_GP32C4T1_PWM /* 4 channels */
  222. static struct rt_device_pwm gp32c4t1_pwm_dev;
  223. static timer_handle_t gp32c4t1_timer_initstruct;
  224. gp32c4t1_timer_initstruct.perh = GP32C4T1;
  225. ald_timer_pwm_init(&gp32c4t1_timer_initstruct);
  226. /* gpio initialization */
  227. #if defined(ES_GP32C4T1_CH1_GPIO_FUNC)&&defined(ES_GP32C4T1_CH1_GPIO_PORT)&&defined(ES_GP32C4T1_CH1_GPIO_PIN)
  228. gpio_initstructure.func = ES_GP32C4T1_CH1_GPIO_FUNC;
  229. ald_gpio_init(ES_GP32C4T1_CH1_GPIO_PORT, ES_GP32C4T1_CH1_GPIO_PIN, &gpio_initstructure);
  230. #endif
  231. #if defined(ES_GP32C4T1_CH2_GPIO_FUNC)&&defined(ES_GP32C4T1_CH2_GPIO_PORT)&&defined(ES_GP32C4T1_CH2_GPIO_PIN)
  232. gpio_initstructure.func = ES_GP32C4T1_CH2_GPIO_FUNC;
  233. ald_gpio_init(ES_GP32C4T1_CH2_GPIO_PORT, ES_GP32C4T1_CH2_GPIO_PIN, &gpio_initstructure);
  234. #endif
  235. #if defined(ES_GP32C4T1_CH3_GPIO_FUNC)&&defined(ES_GP32C4T1_CH3_GPIO_PORT)&&defined(ES_GP32C4T1_CH3_GPIO_PIN)
  236. gpio_initstructure.func = ES_GP32C4T1_CH3_GPIO_FUNC;
  237. ald_gpio_init(ES_GP32C4T1_CH3_GPIO_PORT, ES_GP32C4T1_CH3_GPIO_PIN, &gpio_initstructure);
  238. #endif
  239. #if defined(ES_GP32C4T1_CH4_GPIO_FUNC)&&defined(ES_GP32C4T1_CH4_GPIO_PORT)&&defined(ES_GP32C4T1_CH4_GPIO_PIN)
  240. gpio_initstructure.func = ES_GP32C4T1_CH4_GPIO_FUNC;
  241. ald_gpio_init(ES_GP32C4T1_CH4_GPIO_PORT, ES_GP32C4T1_CH4_GPIO_PIN, &gpio_initstructure);
  242. #endif
  243. ret = rt_device_pwm_register(&gp32c4t1_pwm_dev, ES_DEVICE_NAME_GP32C4T1_PWM, &es32f3_pwm_ops,
  244. &gp32c4t1_timer_initstruct);
  245. #endif
  246. #ifdef BSP_USING_GP16C4T0_PWM /* 4 channels */
  247. static struct rt_device_pwm gp16c4t0_pwm_dev;
  248. static timer_handle_t gp16c4t0_timer_initstruct;
  249. gp16c4t0_timer_initstruct.perh = GP16C4T0;
  250. ald_timer_pwm_init(&gp16c4t0_timer_initstruct);
  251. /* gpio initialization */
  252. #if defined(ES_GP16C4T0_CH1_GPIO_FUNC)&&defined(ES_GP16C4T0_CH1_GPIO_PORT)&&defined(ES_GP16C4T0_CH1_GPIO_PIN)
  253. gpio_initstructure.func = ES_GP16C4T0_CH1_GPIO_FUNC;
  254. ald_gpio_init(ES_GP16C4T0_CH1_GPIO_PORT, ES_GP16C4T0_CH1_GPIO_PIN, &gpio_initstructure);
  255. #endif
  256. #if defined(ES_GP16C4T0_CH2_GPIO_FUNC)&&defined(ES_GP16C4T0_CH2_GPIO_PORT)&&defined(ES_GP16C4T0_CH2_GPIO_PIN)
  257. gpio_initstructure.func = ES_GP16C4T0_CH2_GPIO_FUNC;
  258. ald_gpio_init(ES_GP16C4T0_CH2_GPIO_PORT, ES_GP16C4T0_CH2_GPIO_PIN, &gpio_initstructure);
  259. #endif
  260. #if defined(ES_GP16C4T0_CH3_GPIO_FUNC)&&defined(ES_GP16C4T0_CH3_GPIO_PORT)&&defined(ES_GP16C4T0_CH3_GPIO_PIN)
  261. gpio_initstructure.func = ES_GP16C4T0_CH3_GPIO_FUNC;
  262. ald_gpio_init(ES_GP16C4T0_CH3_GPIO_PORT, ES_GP16C4T0_CH3_GPIO_PIN, &gpio_initstructure);
  263. #endif
  264. #if defined(ES_GP16C4T0_CH4_GPIO_FUNC)&&defined(ES_GP16C4T0_CH4_GPIO_PORT)&&defined(ES_GP16C4T0_CH4_GPIO_PIN)
  265. gpio_initstructure.func = ES_GP16C4T0_CH4_GPIO_FUNC;
  266. ald_gpio_init(ES_GP16C4T0_CH4_GPIO_PORT, ES_GP16C4T0_CH4_GPIO_PIN, &gpio_initstructure);
  267. #endif
  268. ret = rt_device_pwm_register(&gp16c4t0_pwm_dev, ES_DEVICE_NAME_GP16C4T0_PWM, &es32f3_pwm_ops,
  269. &gp16c4t0_timer_initstruct);
  270. #endif
  271. #ifdef BSP_USING_GP16C4T1_PWM /* 4 channels */
  272. static struct rt_device_pwm gp16c4t1_pwm_dev;
  273. static timer_handle_t gp16c4t1_timer_initstruct;
  274. gp16c4t1_timer_initstruct.perh = GP16C4T1;
  275. ald_timer_pwm_init(&gp16c4t1_timer_initstruct);
  276. /* gpio initialization */
  277. #if defined(ES_GP16C4T1_CH1_GPIO_FUNC)&&defined(ES_GP16C4T1_CH1_GPIO_PORT)&&defined(ES_GP16C4T1_CH1_GPIO_PIN)
  278. gpio_initstructure.func = ES_GP16C4T1_CH1_GPIO_FUNC;
  279. ald_gpio_init(ES_GP16C4T1_CH1_GPIO_PORT, ES_GP16C4T1_CH1_GPIO_PIN, &gpio_initstructure);
  280. #endif
  281. #if defined(ES_GP16C4T1_CH2_GPIO_FUNC)&&defined(ES_GP16C4T1_CH2_GPIO_PORT)&&defined(ES_GP16C4T1_CH2_GPIO_PIN)
  282. gpio_initstructure.func = ES_GP16C4T1_CH2_GPIO_FUNC;
  283. ald_gpio_init(ES_GP16C4T1_CH2_GPIO_PORT, ES_GP16C4T1_CH2_GPIO_PIN, &gpio_initstructure);
  284. #endif
  285. #if defined(ES_GP16C4T1_CH3_GPIO_FUNC)&&defined(ES_GP16C4T1_CH3_GPIO_PORT)&&defined(ES_GP16C4T1_CH3_GPIO_PIN)
  286. gpio_initstructure.func = ES_GP16C4T1_CH3_GPIO_FUNC;
  287. ald_gpio_init(ES_GP16C4T1_CH3_GPIO_PORT, ES_GP16C4T1_CH3_GPIO_PIN, &gpio_initstructure);
  288. #endif
  289. #if defined(ES_GP16C4T1_CH4_GPIO_FUNC)&&defined(ES_GP16C4T1_CH4_GPIO_PORT)&&defined(ES_GP16C4T1_CH4_GPIO_PIN)
  290. gpio_initstructure.func = ES_GP16C4T1_CH4_GPIO_FUNC;
  291. ald_gpio_init(ES_GP16C4T1_CH4_GPIO_PORT, ES_GP16C4T1_CH4_GPIO_PIN, &gpio_initstructure);
  292. #endif
  293. ret = rt_device_pwm_register(&gp16c4t1_pwm_dev, ES_DEVICE_NAME_GP16C4T1_PWM, &es32f3_pwm_ops,
  294. &gp16c4t1_timer_initstruct);
  295. #endif
  296. return ret;
  297. }
  298. INIT_DEVICE_EXPORT(rt_hw_pwm_init);
  299. #endif