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 
25 #include <rthw.h>
26 #include <rtthread.h>
27 #include <rtdevice.h>
28 #include <board.h>
29 #include "es_conf_info_pwm.h"
30 
31 
32 #ifdef RT_USING_PWM
33 
pwm_set_duty(timer_handle_t * timer_initstruct,timer_channel_t ch,uint32_t ns)34 static void pwm_set_duty(timer_handle_t *timer_initstruct, timer_channel_t ch, uint32_t ns)
35 {
36     uint64_t tmp = (uint64_t)ald_cmu_get_pclk1_clock() * ns / 1000000000 /
37                    (timer_initstruct->init.prescaler + 1);
38 
39     if (ch == TIMER_CHANNEL_1)
40         WRITE_REG(timer_initstruct->perh->CCVAL1, (uint32_t)tmp);
41     else if (ch == TIMER_CHANNEL_2)
42         WRITE_REG(timer_initstruct->perh->CCVAL2, (uint32_t)tmp);
43     else if (ch == TIMER_CHANNEL_3)
44         WRITE_REG(timer_initstruct->perh->CCVAL3, (uint32_t)tmp);
45     else if (ch == TIMER_CHANNEL_4)
46         WRITE_REG(timer_initstruct->perh->CCVAL4, (uint32_t)tmp);
47 }
48 
es32f3_pwm_control(struct rt_device_pwm * device,int cmd,void * arg)49 static rt_err_t es32f3_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
50 {
51     rt_err_t ret = RT_EOK;
52     uint64_t _arr,bus_speed,tmp;
53     uint32_t _maxcnt,_ccep_ch_en = 0U;
54     timer_channel_t pwm_channel;
55     timer_oc_init_t tim_ocinit;
56     timer_handle_t *timer_initstruct = (timer_handle_t *)device->parent.user_data;
57     struct rt_pwm_configuration *cfg = (struct rt_pwm_configuration *)arg;
58 
59     RT_ASSERT(timer_initstruct != RT_NULL);
60 
61 
62     /* select pwm output channel */
63     if (1 == cfg->channel)
64     {
65         pwm_channel = TIMER_CHANNEL_1;
66         _ccep_ch_en = timer_initstruct->perh->CCEP & TIMER_CCEP_CC1EN_MSK;
67     }
68     else if (2 == cfg->channel)
69     {
70         pwm_channel = TIMER_CHANNEL_2;
71         _ccep_ch_en = timer_initstruct->perh->CCEP & TIMER_CCEP_CC2EN_MSK;
72     }
73     else if (3 == cfg->channel)
74     {
75         pwm_channel = TIMER_CHANNEL_3;
76         _ccep_ch_en = timer_initstruct->perh->CCEP & TIMER_CCEP_CC3EN_MSK;
77     }
78     else if (4 == cfg->channel)
79     {
80         pwm_channel = TIMER_CHANNEL_4;
81         _ccep_ch_en = timer_initstruct->perh->CCEP & TIMER_CCEP_CC4EN_MSK;
82     }
83     else
84         return -RT_EINVAL;
85 
86     switch (cmd)
87     {
88     case PWM_CMD_ENABLE:
89         ald_timer_pwm_start(timer_initstruct, pwm_channel);
90         break;
91 
92     case PWM_CMD_DISABLE:
93         ald_timer_pwm_stop(timer_initstruct, pwm_channel);
94         break;
95 
96     case PWM_CMD_SET:
97 
98           /*当通道没开的时候:关通道,设置输出模式和极性,初始化通道*/
99         if(!_ccep_ch_en)
100         {
101          tim_ocinit.oc_mode      = ES_PWM_OC_MODE;
102          tim_ocinit.oc_polarity  = ES_PWM_OC_POLARITY;
103          tim_ocinit.oc_fast_en   = DISABLE;
104          tim_ocinit.ocn_polarity = TIMER_OCN_POLARITY_HIGH;
105          tim_ocinit.ocn_idle     = TIMER_OCN_IDLE_RESET;
106          tim_ocinit.oc_idle      = TIMER_OC_IDLE_RESET;
107 
108             ald_timer_oc_config_channel(timer_initstruct, &tim_ocinit, pwm_channel);
109         }
110 
111         bus_speed = (uint64_t)ald_cmu_get_pclk1_clock();
112 
113         /*判断外设的计数器最大值*/
114 #ifdef ES32F36xx
115         if((timer_initstruct->perh == GP32C4T0)||(timer_initstruct->perh == GP32C4T1))
116         {
117             _maxcnt = 0xFFFFFFFF;
118         }
119         else _maxcnt = 0xFFFF;
120 #else
121         _maxcnt = 0xFFFF;
122 #endif
123 
124         /*当最大分频 <= _maxcnt时:估计大概的分频,加快速度 */
125         tmp = bus_speed * (cfg->period)/1000000000/_maxcnt;
126         timer_initstruct->init.prescaler = (tmp > 2U) ? (tmp - 2U) : 0U ;    /*bus_speed < 500000000*/
127 
128         /* count registers max , auto adjust prescaler */
129         do
130         {
131           _arr = bus_speed * (cfg->period) / 1000000000 /(++timer_initstruct->init.prescaler);
132 
133         }
134         while (_arr > _maxcnt);
135 
136         WRITE_REG(timer_initstruct->perh->AR, (uint32_t)_arr);
137         timer_initstruct->init.period   = (uint32_t)_arr;
138 
139         /* update prescaler */
140         WRITE_REG(timer_initstruct->perh->PRES, --timer_initstruct->init.prescaler);
141 
142         pwm_set_duty(timer_initstruct, pwm_channel, cfg->pulse);
143 
144         break;
145 
146     case PWM_CMD_GET:
147         cfg->pulse = ald_timer_read_capture_value(timer_initstruct, pwm_channel) * 100 /
148                      READ_REG(timer_initstruct->perh->AR);
149         break;
150 
151     default:
152         break;
153     }
154     return ret;
155 }
156 
157 const static struct rt_pwm_ops es32f3_pwm_ops =
158 {
159     es32f3_pwm_control
160 };
161 
rt_hw_pwm_init(void)162 int rt_hw_pwm_init(void)
163 {
164     rt_err_t ret = RT_EOK;
165     gpio_init_t gpio_initstructure;
166 
167     gpio_initstructure.mode = GPIO_MODE_OUTPUT;
168     gpio_initstructure.odos = GPIO_PUSH_PULL;
169     gpio_initstructure.pupd = GPIO_PUSH_UP;
170     gpio_initstructure.podrv = GPIO_OUT_DRIVE_6;
171     gpio_initstructure.nodrv = GPIO_OUT_DRIVE_6;
172     gpio_initstructure.flt  = GPIO_FILTER_DISABLE;
173     gpio_initstructure.type = GPIO_TYPE_TTL;
174 
175 #ifdef BSP_USING_AD16C4T0_PWM /* 4 channels */
176     static struct rt_device_pwm ad16c4t0_pwm_dev;
177     static timer_handle_t ad16c4t0_timer_initstruct;
178 
179     ad16c4t0_timer_initstruct.perh = AD16C4T0;
180     ald_timer_pwm_init(&ad16c4t0_timer_initstruct);
181 
182     /* gpio initialization */
183 
184 #if  defined(ES_AD16C4T0_CH1_GPIO_FUNC)&&defined(ES_AD16C4T0_CH1_GPIO_PORT)&&defined(ES_AD16C4T0_CH1_GPIO_PIN)
185     gpio_initstructure.func = ES_AD16C4T0_CH1_GPIO_FUNC;
186     ald_gpio_init(ES_AD16C4T0_CH1_GPIO_PORT, ES_AD16C4T0_CH1_GPIO_PIN, &gpio_initstructure);
187 #endif
188 
189 #if  defined(ES_AD16C4T0_CH2_GPIO_FUNC)&&defined(ES_AD16C4T0_CH2_GPIO_PORT)&&defined(ES_AD16C4T0_CH2_GPIO_PIN)
190     gpio_initstructure.func = ES_AD16C4T0_CH2_GPIO_FUNC;
191     ald_gpio_init(ES_AD16C4T0_CH2_GPIO_PORT, ES_AD16C4T0_CH2_GPIO_PIN, &gpio_initstructure);
192 #endif
193 
194 #if  defined(ES_AD16C4T0_CH3_GPIO_FUNC)&&defined(ES_AD16C4T0_CH3_GPIO_PORT)&&defined(ES_AD16C4T0_CH3_GPIO_FUNC)
195     gpio_initstructure.func = ES_AD16C4T0_CH3_GPIO_FUNC;
196     ald_gpio_init(ES_AD16C4T0_CH3_GPIO_PORT, ES_AD16C4T0_CH3_GPIO_PIN, &gpio_initstructure);
197 #endif
198 
199 #if  defined(ES_AD16C4T0_CH4_GPIO_FUNC)&&defined(ES_AD16C4T0_CH4_GPIO_PORT)&&defined(ES_AD16C4T0_CH4_GPIO_PIN)
200     gpio_initstructure.func = ES_AD16C4T0_CH4_GPIO_FUNC;
201     ald_gpio_init(ES_AD16C4T0_CH4_GPIO_PORT, ES_AD16C4T0_CH4_GPIO_PIN, &gpio_initstructure);
202 #endif
203 
204     ret = rt_device_pwm_register(&ad16c4t0_pwm_dev, ES_DEVICE_NAME_AD16C4T0_PWM, &es32f3_pwm_ops,
205                                  &ad16c4t0_timer_initstruct);
206 #endif
207 
208 #ifdef BSP_USING_AD16C4T1_PWM /* 4 channels */
209     static struct rt_device_pwm ad16c4t1_pwm_dev;
210     static timer_handle_t ad16c4t1_timer_initstruct;
211 
212     ad16c4t1_timer_initstruct.perh = AD16C4T1;
213     ald_timer_pwm_init(&ad16c4t1_timer_initstruct);
214 
215     /* gpio initialization */
216 
217 #if  defined(ES_AD16C4T1_CH1_GPIO_FUNC)&&defined(ES_AD16C4T1_CH1_GPIO_PORT)&&defined(ES_AD16C4T1_CH1_GPIO_PIN)
218     gpio_initstructure.func = ES_AD16C4T1_CH1_GPIO_FUNC;
219     ald_gpio_init(ES_AD16C4T1_CH1_GPIO_PORT, ES_AD16C4T1_CH1_GPIO_PIN, &gpio_initstructure);
220 #endif
221 
222 #if  defined(ES_AD16C4T1_CH2_GPIO_FUNC)&&defined(ES_AD16C4T1_CH2_GPIO_PORT)&&defined(ES_AD16C4T1_CH2_GPIO_PIN)
223     gpio_initstructure.func = ES_AD16C4T1_CH2_GPIO_FUNC;
224     ald_gpio_init(ES_AD16C4T1_CH2_GPIO_PORT, ES_AD16C4T1_CH2_GPIO_PIN, &gpio_initstructure);
225 #endif
226 
227 #if  defined(ES_AD16C4T1_CH3_GPIO_FUNC)&&defined(ES_AD16C4T1_CH3_GPIO_PORT)&&defined(ES_AD16C4T1_CH3_GPIO_PIN)
228     gpio_initstructure.func = ES_AD16C4T1_CH3_GPIO_FUNC;
229     ald_gpio_init(ES_AD16C4T1_CH3_GPIO_PORT, ES_AD16C4T1_CH3_GPIO_PIN, &gpio_initstructure);
230 #endif
231 
232 #if  defined(ES_AD16C4T1_CH4_GPIO_FUNC)&&defined(ES_AD16C4T1_CH4_GPIO_PORT)&&defined(ES_AD16C4T1_CH4_GPIO_PIN)
233     gpio_initstructure.func = ES_AD16C4T1_CH4_GPIO_FUNC;
234     ald_gpio_init(ES_AD16C4T1_CH4_GPIO_PORT, ES_AD16C4T1_CH4_GPIO_PIN, &gpio_initstructure);
235 #endif
236 
237     ret = rt_device_pwm_register(&ad16c4t1_pwm_dev, ES_DEVICE_NAME_AD16C4T1_PWM, &es32f3_pwm_ops,
238                                  &ad16c4t1_timer_initstruct);
239 #endif
240 
241 
242 #ifdef BSP_USING_GP32C4T0_PWM /* 4 channels */
243     static struct rt_device_pwm gp32c4t0_pwm_dev;
244     static timer_handle_t gp32c4t0_timer_initstruct;
245 
246     gp32c4t0_timer_initstruct.perh = GP32C4T0;
247     ald_timer_pwm_init(&gp32c4t0_timer_initstruct);
248 
249     /* gpio initialization */
250 
251 #if  defined(ES_GP32C4T0_CH1_GPIO_FUNC)&&defined(ES_GP32C4T0_CH1_GPIO_PORT)&&defined(ES_GP32C4T0_CH1_GPIO_PIN)
252     gpio_initstructure.func = ES_GP32C4T0_CH1_GPIO_FUNC;
253     ald_gpio_init(ES_GP32C4T0_CH1_GPIO_PORT, ES_GP32C4T0_CH1_GPIO_PIN, &gpio_initstructure);
254 #endif
255 
256 #if  defined(ES_GP32C4T0_CH2_GPIO_FUNC)&&defined(ES_GP32C4T0_CH2_GPIO_PORT)&&defined(ES_GP32C4T0_CH2_GPIO_PIN)
257     gpio_initstructure.func = ES_GP32C4T0_CH2_GPIO_FUNC;
258     ald_gpio_init(ES_GP32C4T0_CH2_GPIO_PORT, ES_GP32C4T0_CH2_GPIO_PIN, &gpio_initstructure);
259 #endif
260 
261 #if  defined(ES_GP32C4T0_CH3_GPIO_FUNC)&&defined(ES_GP32C4T0_CH3_GPIO_PORT)&&defined(ES_GP32C4T0_CH3_GPIO_PIN)
262     gpio_initstructure.func = ES_GP32C4T0_CH3_GPIO_FUNC;
263     ald_gpio_init(ES_GP32C4T0_CH3_GPIO_PORT, ES_GP32C4T0_CH3_GPIO_PIN, &gpio_initstructure);
264 #endif
265 
266 #if  defined(ES_GP32C4T0_CH4_GPIO_FUNC)&&defined(ES_GP32C4T0_CH4_GPIO_PORT)&&defined(ES_GP32C4T0_CH4_GPIO_PIN)
267     gpio_initstructure.func = ES_GP32C4T0_CH4_GPIO_FUNC;
268     ald_gpio_init(ES_GP32C4T0_CH4_GPIO_PORT, ES_GP32C4T0_CH4_GPIO_PIN, &gpio_initstructure);
269 #endif
270 
271     ret = rt_device_pwm_register(&gp32c4t0_pwm_dev, ES_DEVICE_NAME_GP32C4T0_PWM, &es32f3_pwm_ops,
272                                  &gp32c4t0_timer_initstruct);
273 #endif
274 
275 
276 #ifdef BSP_USING_GP32C4T1_PWM /* 4 channels */
277     static struct rt_device_pwm gp32c4t1_pwm_dev;
278     static timer_handle_t gp32c4t1_timer_initstruct;
279 
280     gp32c4t1_timer_initstruct.perh = GP32C4T1;
281     ald_timer_pwm_init(&gp32c4t1_timer_initstruct);
282 
283     /* gpio initialization */
284 
285 #if  defined(ES_GP32C4T1_CH1_GPIO_FUNC)&&defined(ES_GP32C4T1_CH1_GPIO_PORT)&&defined(ES_GP32C4T1_CH1_GPIO_PIN)
286     gpio_initstructure.func = ES_GP32C4T1_CH1_GPIO_FUNC;
287     ald_gpio_init(ES_GP32C4T1_CH1_GPIO_PORT, ES_GP32C4T1_CH1_GPIO_PIN, &gpio_initstructure);
288 #endif
289 
290 #if  defined(ES_GP32C4T1_CH2_GPIO_FUNC)&&defined(ES_GP32C4T1_CH2_GPIO_PORT)&&defined(ES_GP32C4T1_CH2_GPIO_PIN)
291     gpio_initstructure.func = ES_GP32C4T1_CH2_GPIO_FUNC;
292     ald_gpio_init(ES_GP32C4T1_CH2_GPIO_PORT, ES_GP32C4T1_CH2_GPIO_PIN, &gpio_initstructure);
293 #endif
294 
295 #if  defined(ES_GP32C4T1_CH3_GPIO_FUNC)&&defined(ES_GP32C4T1_CH3_GPIO_PORT)&&defined(ES_GP32C4T1_CH3_GPIO_PIN)
296     gpio_initstructure.func = ES_GP32C4T1_CH3_GPIO_FUNC;
297     ald_gpio_init(ES_GP32C4T1_CH3_GPIO_PORT, ES_GP32C4T1_CH3_GPIO_PIN, &gpio_initstructure);
298 #endif
299 
300 #if  defined(ES_GP32C4T1_CH4_GPIO_FUNC)&&defined(ES_GP32C4T1_CH4_GPIO_PORT)&&defined(ES_GP32C4T1_CH4_GPIO_PIN)
301     gpio_initstructure.func = ES_GP32C4T1_CH4_GPIO_FUNC;
302     ald_gpio_init(ES_GP32C4T1_CH4_GPIO_PORT, ES_GP32C4T1_CH4_GPIO_PIN, &gpio_initstructure);
303 #endif
304 
305     ret = rt_device_pwm_register(&gp32c4t1_pwm_dev, ES_DEVICE_NAME_GP32C4T1_PWM, &es32f3_pwm_ops,
306                                  &gp32c4t1_timer_initstruct);
307 #endif
308 
309 
310 #ifdef BSP_USING_GP16C4T0_PWM /* 4 channels */
311     static struct rt_device_pwm gp16c4t0_pwm_dev;
312     static timer_handle_t gp16c4t0_timer_initstruct;
313 
314     gp16c4t0_timer_initstruct.perh = GP16C4T0;
315     ald_timer_pwm_init(&gp16c4t0_timer_initstruct);
316 
317     /* gpio initialization */
318 
319 #if  defined(ES_GP16C4T0_CH1_GPIO_FUNC)&&defined(ES_GP16C4T0_CH1_GPIO_PORT)&&defined(ES_GP16C4T0_CH1_GPIO_PIN)
320     gpio_initstructure.func = ES_GP16C4T0_CH1_GPIO_FUNC;
321     ald_gpio_init(ES_GP16C4T0_CH1_GPIO_PORT, ES_GP16C4T0_CH1_GPIO_PIN, &gpio_initstructure);
322 #endif
323 
324 #if  defined(ES_GP16C4T0_CH2_GPIO_FUNC)&&defined(ES_GP16C4T0_CH2_GPIO_PORT)&&defined(ES_GP16C4T0_CH2_GPIO_PIN)
325     gpio_initstructure.func = ES_GP16C4T0_CH2_GPIO_FUNC;
326     ald_gpio_init(ES_GP16C4T0_CH2_GPIO_PORT, ES_GP16C4T0_CH2_GPIO_PIN, &gpio_initstructure);
327 #endif
328 
329 #if  defined(ES_GP16C4T0_CH3_GPIO_FUNC)&&defined(ES_GP16C4T0_CH3_GPIO_PORT)&&defined(ES_GP16C4T0_CH3_GPIO_PIN)
330     gpio_initstructure.func = ES_GP16C4T0_CH3_GPIO_FUNC;
331     ald_gpio_init(ES_GP16C4T0_CH3_GPIO_PORT, ES_GP16C4T0_CH3_GPIO_PIN, &gpio_initstructure);
332 #endif
333 
334 #if  defined(ES_GP16C4T0_CH4_GPIO_FUNC)&&defined(ES_GP16C4T0_CH4_GPIO_PORT)&&defined(ES_GP16C4T0_CH4_GPIO_PIN)
335     gpio_initstructure.func = ES_GP16C4T0_CH4_GPIO_FUNC;
336     ald_gpio_init(ES_GP16C4T0_CH4_GPIO_PORT, ES_GP16C4T0_CH4_GPIO_PIN, &gpio_initstructure);
337 #endif
338 
339     ret = rt_device_pwm_register(&gp16c4t0_pwm_dev, ES_DEVICE_NAME_GP16C4T0_PWM, &es32f3_pwm_ops,
340                                  &gp16c4t0_timer_initstruct);
341 #endif
342 
343 
344 #ifdef BSP_USING_GP16C4T1_PWM /* 4 channels */
345     static struct rt_device_pwm gp16c4t1_pwm_dev;
346     static timer_handle_t gp16c4t1_timer_initstruct;
347 
348     gp16c4t1_timer_initstruct.perh = GP16C4T1;
349     ald_timer_pwm_init(&gp16c4t1_timer_initstruct);
350 
351     /* gpio initialization */
352 
353 #if  defined(ES_GP16C4T1_CH1_GPIO_FUNC)&&defined(ES_GP16C4T1_CH1_GPIO_PORT)&&defined(ES_GP16C4T1_CH1_GPIO_PIN)
354     gpio_initstructure.func = ES_GP16C4T1_CH1_GPIO_FUNC;
355     ald_gpio_init(ES_GP16C4T1_CH1_GPIO_PORT, ES_GP16C4T1_CH1_GPIO_PIN, &gpio_initstructure);
356 #endif
357 
358 #if  defined(ES_GP16C4T1_CH2_GPIO_FUNC)&&defined(ES_GP16C4T1_CH2_GPIO_PORT)&&defined(ES_GP16C4T1_CH2_GPIO_PIN)
359     gpio_initstructure.func = ES_GP16C4T1_CH2_GPIO_FUNC;
360     ald_gpio_init(ES_GP16C4T1_CH2_GPIO_PORT, ES_GP16C4T1_CH2_GPIO_PIN, &gpio_initstructure);
361 #endif
362 
363 #if  defined(ES_GP16C4T1_CH3_GPIO_FUNC)&&defined(ES_GP16C4T1_CH3_GPIO_PORT)&&defined(ES_GP16C4T1_CH3_GPIO_PIN)
364     gpio_initstructure.func = ES_GP16C4T1_CH3_GPIO_FUNC;
365     ald_gpio_init(ES_GP16C4T1_CH3_GPIO_PORT, ES_GP16C4T1_CH3_GPIO_PIN, &gpio_initstructure);
366 #endif
367 
368 #if  defined(ES_GP16C4T1_CH4_GPIO_FUNC)&&defined(ES_GP16C4T1_CH4_GPIO_PORT)&&defined(ES_GP16C4T1_CH4_GPIO_PIN)
369     gpio_initstructure.func = ES_GP16C4T1_CH4_GPIO_FUNC;
370     ald_gpio_init(ES_GP16C4T1_CH4_GPIO_PORT, ES_GP16C4T1_CH4_GPIO_PIN, &gpio_initstructure);
371 #endif
372 
373     ret = rt_device_pwm_register(&gp16c4t1_pwm_dev, ES_DEVICE_NAME_GP16C4T1_PWM, &es32f3_pwm_ops,
374                                  &gp16c4t1_timer_initstruct);
375 #endif
376 
377     return ret;
378 }
379 INIT_DEVICE_EXPORT(rt_hw_pwm_init);
380 
381 #endif
382