1212#include <rtthread.h>
1313#include <rtdevice.h>
1414#include <board.h>
15+ #include <ald_cmu.h>
1516#include <ald_timer.h>
1617#include <ald_gpio.h>
1718
19+ static void pwm_set_freq (timer_handle_t * timer_initstruct , uint32_t ns )
20+ {
21+ uint64_t _arr = (uint64_t )cmu_get_pclk1_clock () * ns / 1000000000 /
22+ (timer_initstruct -> init .prescaler + 1 ) - 1 ;
23+
24+ WRITE_REG (timer_initstruct -> perh -> AR , (uint32_t )_arr );
25+ timer_initstruct -> init .period = (uint32_t )_arr ;
26+ }
27+
28+ static void pwm_set_duty (timer_handle_t * timer_initstruct , timer_channel_t ch , uint32_t ns )
29+ {
30+ uint64_t tmp = (uint64_t )cmu_get_pclk1_clock () * ns / 1000000000 /
31+ (timer_initstruct -> init .prescaler + 1 ) - 1 ;
32+
33+ if (ch == TIMER_CHANNEL_1 )
34+ WRITE_REG (timer_initstruct -> perh -> CCVAL1 , (uint32_t )tmp );
35+ else if (ch == TIMER_CHANNEL_2 )
36+ WRITE_REG (timer_initstruct -> perh -> CCVAL2 , (uint32_t )tmp );
37+ else if (ch == TIMER_CHANNEL_3 )
38+ WRITE_REG (timer_initstruct -> perh -> CCVAL3 , (uint32_t )tmp );
39+ else if (ch == TIMER_CHANNEL_4 )
40+ WRITE_REG (timer_initstruct -> perh -> CCVAL4 , (uint32_t )tmp );
41+ else
42+ {
43+ ;/* do nothing */
44+ }
45+ }
46+
1847static rt_err_t es32f0_pwm_control (struct rt_device_pwm * device , int cmd , void * arg )
1948{
2049 rt_err_t ret = RT_EOK ;
50+ uint32_t _ccep ;
2151 timer_channel_t pwm_channel ;
2252 timer_oc_init_t tim_ocinit ;
2353 timer_handle_t * timer_initstruct = (timer_handle_t * )device -> parent .user_data ;
@@ -33,21 +63,21 @@ static rt_err_t es32f0_pwm_control(struct rt_device_pwm *device, int cmd, void *
3363 tim_ocinit .oc_idle = TIMER_OC_IDLE_RESET ;
3464
3565 /* select pwm output channel */
36- if (0 == cfg -> channel )
66+ if (1 == cfg -> channel )
3767 {
3868 pwm_channel = TIMER_CHANNEL_1 ;
3969 }
40- else if (1 == cfg -> channel )
70+ else if (2 == cfg -> channel )
4171 {
4272 pwm_channel = TIMER_CHANNEL_2 ;
4373 }
44- else if (2 == cfg -> channel )
74+ else if (3 == cfg -> channel )
4575 {
4676 if (timer_initstruct -> perh == GP16C2T0 || timer_initstruct -> perh == GP16C2T1 )
4777 return RT_EINVAL ;
4878 pwm_channel = TIMER_CHANNEL_3 ;
4979 }
50- else if (3 == cfg -> channel )
80+ else if (4 == cfg -> channel )
5181 {
5282 if (timer_initstruct -> perh == GP16C2T0 || timer_initstruct -> perh == GP16C2T1 )
5383 return RT_EINVAL ;
@@ -69,21 +99,24 @@ static rt_err_t es32f0_pwm_control(struct rt_device_pwm *device, int cmd, void *
6999 break ;
70100
71101 case PWM_CMD_SET :
72- /* count registers max 0xFFFF, auto adjust prescaler*/
102+ _ccep = timer_initstruct -> perh -> CCEP ;
103+ /* count registers max 0xFFFF, auto adjust prescaler */
73104 do
74105 {
75- timer_pwm_set_freq (timer_initstruct , 1000000000 / cfg -> period );
106+ pwm_set_freq (timer_initstruct , cfg -> period );
76107 timer_initstruct -> init .prescaler ++ ;
77108 }
78109 while (timer_initstruct -> init .period > 0xFFFF );
79110 /* update prescaler */
80- WRITE_REG (timer_initstruct -> perh -> PRES , -- timer_initstruct -> init .prescaler );
111+ WRITE_REG (timer_initstruct -> perh -> PRES , -- timer_initstruct -> init .prescaler );
81112 timer_oc_config_channel (timer_initstruct , & tim_ocinit , pwm_channel );
82- timer_pwm_set_duty (timer_initstruct , pwm_channel , cfg -> pulse * 100 / cfg -> period );
113+ pwm_set_duty (timer_initstruct , pwm_channel , cfg -> pulse );
114+ timer_initstruct -> perh -> CCEP = _ccep ;
83115 break ;
84116
85117 case PWM_CMD_GET :
86- cfg -> pulse = timer_read_capture_value (timer_initstruct , pwm_channel ) * 100 / READ_REG (timer_initstruct -> perh -> AR );
118+ cfg -> pulse = timer_read_capture_value (timer_initstruct , pwm_channel ) * 100 /
119+ READ_REG (timer_initstruct -> perh -> AR );
87120 break ;
88121
89122 default :
@@ -123,7 +156,8 @@ int rt_hw_pwm_init(void)
123156 gpio_init (GPIOA , GPIO_PIN_10 , & gpio_initstructure );
124157 gpio_init (GPIOA , GPIO_PIN_11 , & gpio_initstructure );
125158
126- ret = rt_device_pwm_register (& pwm_dev0 , "pwm0" , & es32f0_pwm_ops , & timer_initstruct0 );
159+ ret = rt_device_pwm_register (& pwm_dev0 , "pwm0" , & es32f0_pwm_ops ,
160+ & timer_initstruct0 );
127161#endif
128162
129163#ifdef BSP_USING_PWM1 /* 4 channels */
@@ -140,7 +174,8 @@ int rt_hw_pwm_init(void)
140174 gpio_init (GPIOB , GPIO_PIN_8 , & gpio_initstructure );
141175 gpio_init (GPIOB , GPIO_PIN_9 , & gpio_initstructure );
142176
143- ret = rt_device_pwm_register (& pwm_dev1 , "pwm1" , & es32f0_pwm_ops , & timer_initstruct1 );
177+ ret = rt_device_pwm_register (& pwm_dev1 , "pwm1" , & es32f0_pwm_ops ,
178+ & timer_initstruct1 );
144179#endif
145180
146181#ifdef BSP_USING_PWM2 /* 2 channels */
@@ -155,7 +190,8 @@ int rt_hw_pwm_init(void)
155190 gpio_init (GPIOA , GPIO_PIN_0 , & gpio_initstructure );
156191 gpio_init (GPIOA , GPIO_PIN_1 , & gpio_initstructure );
157192
158- ret = rt_device_pwm_register (& pwm_dev2 , "pwm2" , & es32f0_pwm_ops , & timer_initstruct2 );
193+ ret = rt_device_pwm_register (& pwm_dev2 , "pwm2" , & es32f0_pwm_ops ,
194+ & timer_initstruct2 );
159195#endif
160196
161197#ifdef BSP_USING_PWM3 /* 2 channels */
@@ -170,7 +206,8 @@ int rt_hw_pwm_init(void)
170206 gpio_init (GPIOC , GPIO_PIN_6 , & gpio_initstructure );
171207 gpio_init (GPIOC , GPIO_PIN_7 , & gpio_initstructure );
172208
173- ret = rt_device_pwm_register (& pwm_dev3 , "pwm3" , & es32f0_pwm_ops , & timer_initstruct3 );
209+ ret = rt_device_pwm_register (& pwm_dev3 , "pwm3" , & es32f0_pwm_ops ,
210+ & timer_initstruct3 );
174211#endif
175212
176213 return ret ;
0 commit comments