Skip to content

Commit 79fd4d3

Browse files
committed
[bsp/es32f0654] fixed pwm timing inaccuracy when period is too small.
1 parent fc5cfe0 commit 79fd4d3

File tree

13 files changed

+104
-62
lines changed

13 files changed

+104
-62
lines changed

bsp/es32f0654/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ ES-PDS-ES32F0654 是东软载波微电子官方推出的一款基于 ARM Cortex-
1313

1414
开发板外观如下图所示:
1515

16-
ES-PDS-ES32F0654-V1.0
16+
ES-PDS-ES32F0654-V1.1
1717

18-
![ES32F0654](figures/ES-PDS-ES32F0654-V1.0.jpg)
18+
![ES32F0654](figures/ES-PDS-ES32F0654-V1.1.jpg)
1919

2020
该开发板常用 **板载资源** 如下:
2121

@@ -34,7 +34,7 @@ ES-PDS-ES32F0654-V1.0
3434

3535
| **板载外设** | **支持情况** | **备注** |
3636
| :---------------- | :----------: | :------------------------------------|
37-
| SPI FLASH | 支持 | |
37+
| SPI FLASH | 支持 | SPI0 |
3838

3939
| **片上外设** | **支持情况** | **备注** |
4040
| :---------------- | :----------: | :------------------------------------|
@@ -56,7 +56,7 @@ ES-PDS-ES32F0654-V1.0
5656

5757
使用ESlinkⅡ(mini)连接开发板如下图所示:
5858

59-
ESLinkⅡ(mini) + ES-PDS-ES32F0654-V1.0
59+
ESLinkⅡ(mini) + ES-PDS-ES32F0654-V1.1
6060

6161
![ESLinkII](figures/ESLinkII-mini.jpg)
6262

bsp/es32f0654/drivers/Kconfig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ menu "Hardware Drivers Config"
7979
menu "Onboard Peripheral Drivers"
8080

8181
config BSP_USING_SPI_FLASH
82-
bool "Enable SPI FLASH (W25Q64 spi0)"
83-
select BSP_USING_SPI
84-
select BSP_USING_SPI0
85-
select RT_USING_SFUD
86-
select RT_SFUD_USING_SFDP
87-
default n
82+
bool "Enable SPI FLASH (W25Q64 spi0)"
83+
select BSP_USING_SPI
84+
select BSP_USING_SPI0
85+
select RT_USING_SFUD
86+
select RT_SFUD_USING_SFDP
87+
default n
8888

8989
endmenu
9090

bsp/es32f0654/drivers/board.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ void rt_hw_us_delay(rt_uint32_t us)
114114
start = SysTick->VAL;
115115
reload = SysTick->LOAD;
116116
us_tick = cmu_get_sys_clock() / 1000000UL;
117-
do{
117+
do
118+
{
118119
now = SysTick->VAL;
119120
delta = start > now ? start - now : reload + start - now;
120-
} while(delta < us_tick * us);
121+
}
122+
while (delta < us_tick * us);
121123
}

bsp/es32f0654/drivers/drv_gpio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ rt_err_t es32f0_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
257257
{
258258
return RT_ENOSYS;
259259
}
260-
/**pin no. convert to dec no.**/
260+
/* pin no. convert to dec no. */
261261
for (irqindex = 0; irqindex < 16; irqindex++)
262262
{
263263
if ((0x01 << irqindex) == index->pin)
@@ -327,7 +327,7 @@ rt_err_t es32f0_pin_irq_enable(struct rt_device *device, rt_base_t pin,
327327
const struct pin_irq_map *irqmap;
328328
rt_base_t level;
329329
rt_int32_t irqindex = -1;
330-
/*Configure GPIO_InitStructure & EXTI_InitStructure*/
330+
/* Configure GPIO_InitStructure & EXTI_InitStructure */
331331
gpio_init_t gpio_initstruct;
332332
exti_init_t exti_initstruct;
333333
exti_initstruct.filter = DISABLE;
@@ -341,7 +341,7 @@ rt_err_t es32f0_pin_irq_enable(struct rt_device *device, rt_base_t pin,
341341
}
342342
if (enabled == PIN_IRQ_ENABLE)
343343
{
344-
/**pin no. convert to dec no.**/
344+
/* pin no. convert to dec no. */
345345
for (irqindex = 0; irqindex < 16; irqindex++)
346346
{
347347
if ((0x01 << irqindex) == index->pin)
@@ -421,7 +421,7 @@ INIT_BOARD_EXPORT(rt_hw_pin_init);
421421
rt_inline void pin_irq_hdr(uint16_t GPIO_Pin)
422422
{
423423
uint16_t irqno;
424-
/**pin no. convert to dec no.**/
424+
/* pin no. convert to dec no. */
425425
for (irqno = 0; irqno < 16; irqno++)
426426
{
427427
if ((0x01 << irqno) == GPIO_Pin)

bsp/es32f0654/drivers/drv_i2c.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@
1919
#ifdef RT_USING_I2C
2020

2121
#define TIMEOUT 0x0FFF
22-
23-
/*define i2c Instance*/
24-
struct rt_i2c_bus_device _i2c_device0;
25-
struct rt_i2c_bus_device _i2c_device1;
26-
i2c_handle_t _h_i2c0, _h_i2c1;
22+
/* I2C struct definition */
23+
static i2c_handle_t _h_i2c0, _h_i2c1;
2724

2825
static void _i2c_init(void)
2926
{
30-
gpio_init_t gpio_instruct; //i2c function init
27+
gpio_init_t gpio_instruct;
3128

32-
/* Initialize I2C Pin*/
29+
/* Initialize I2C Pin */
3330
gpio_instruct.mode = GPIO_MODE_OUTPUT;
3431
gpio_instruct.odos = GPIO_PUSH_PULL;
3532
gpio_instruct.pupd = GPIO_PUSH_UP;
@@ -52,7 +49,7 @@ static void _i2c_init(void)
5249
i2c_init(&_h_i2c0);
5350
/* I2C0_SCL->PB8, I2C0_SDA->PB9 */
5451
gpio_init(GPIOB, GPIO_PIN_8 | GPIO_PIN_9, &gpio_instruct);
55-
#endif/*BSP_USING_I2C0*/
52+
#endif
5653

5754
#ifdef BSP_USING_I2C1
5855
/* Initialize i2c function */
@@ -68,7 +65,7 @@ static void _i2c_init(void)
6865
i2c_init(&_h_i2c1);
6966
/* I2C1_SCL->PB10, I2C1_SDA->PB11 */
7067
gpio_init(GPIOB, GPIO_PIN_10 | GPIO_PIN_11, &gpio_instruct);
71-
#endif/*BSP_USING_I2C1*/
68+
#endif
7269
}
7370

7471
static rt_size_t es32f0_master_xfer(struct rt_i2c_bus_device *bus,
@@ -120,13 +117,17 @@ int rt_hw_i2c_init(void)
120117
_i2c_init();
121118

122119
#ifdef BSP_USING_I2C0
120+
/* define i2c Instance */
121+
static struct rt_i2c_bus_device _i2c_device0;
123122
rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device));
124123
_i2c_device0.ops = &es32f0_i2c_ops;
125124
_i2c_device0.priv = &_h_i2c0;
126125
rt_i2c_bus_device_register(&_i2c_device0, "i2c0");
127126
#endif
128127

129128
#ifdef BSP_USING_I2C1
129+
/* define i2c Instance */
130+
static struct rt_i2c_bus_device _i2c_device1;
130131
rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device));
131132
_i2c_device1.ops = &es32f0_i2c_ops;
132133
_i2c_device1.priv = &_h_i2c1;
@@ -136,5 +137,5 @@ int rt_hw_i2c_init(void)
136137
return RT_EOK;
137138
}
138139
INIT_DEVICE_EXPORT(rt_hw_i2c_init);
139-
/* end of i2c driver */
140+
140141
#endif

bsp/es32f0654/drivers/drv_pwm.c

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,42 @@
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+
1847
static 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

Comments
 (0)