Skip to content

Commit 60d0e11

Browse files
committed
feat(bsp/gd32): 添加PWM和更多硬件定时器支持
- 在Kconfig中添加BSP_USING_PWM和BSP_USING_HWTIMER配置项 - 修正PWM时钟获取逻辑,使其能正确得到APB1/APB2上的定时器时钟源频率,添加在pwm结构体通道引脚资源配置项 - 修改硬件定时器初始化逻辑,修正定时器中断处理函数,适配GD32F4xx系列 - 在main.c中添加hwtimer_test和pwm_test测试函数 - 修复PWM和硬件定时器的依赖关系配置
1 parent be5ceca commit 60d0e11

8 files changed

Lines changed: 1007 additions & 389 deletions

File tree

bsp/gd32/arm/gd32405rg/.config

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ CONFIG_RT_USING_I2C_BITOPS=y
254254
# CONFIG_RT_USING_NULL is not set
255255
# CONFIG_RT_USING_ZERO is not set
256256
# CONFIG_RT_USING_RANDOM is not set
257-
# CONFIG_RT_USING_PWM is not set
257+
CONFIG_RT_USING_PWM=y
258258
# CONFIG_RT_USING_PULSE_ENCODER is not set
259259
# CONFIG_RT_USING_INPUT_CAPTURE is not set
260260
# CONFIG_RT_USING_MTD_NOR is not set
@@ -274,7 +274,7 @@ CONFIG_RT_USING_I2C_BITOPS=y
274274
# CONFIG_RT_USING_VIRTIO is not set
275275
CONFIG_RT_USING_PIN=y
276276
# CONFIG_RT_USING_KTIME is not set
277-
# CONFIG_RT_USING_HWTIMER is not set
277+
CONFIG_RT_USING_HWTIMER=y
278278
# CONFIG_RT_USING_CHERRYUSB is not set
279279
# end of Device Drivers
280280

@@ -1442,7 +1442,20 @@ CONFIG_BSP_USING_UART0=y
14421442
# CONFIG_BSP_USING_UART5 is not set
14431443
# CONFIG_BSP_USING_SPI is not set
14441444
# CONFIG_BSP_USING_ADC is not set
1445-
# CONFIG_BSP_USING_TIM is not set
1445+
# CONFIG_BSP_USING_HWTIMER is not set
1446+
CONFIG_BSP_USING_PWM=y
1447+
CONFIG_BSP_USING_PWM0=y
1448+
CONFIG_BSP_USING_PWM1=y
1449+
CONFIG_BSP_USING_PWM2=y
1450+
CONFIG_BSP_USING_PWM3=y
1451+
CONFIG_BSP_USING_PWM4=y
1452+
CONFIG_BSP_USING_PWM7=y
1453+
CONFIG_BSP_USING_PWM8=y
1454+
CONFIG_BSP_USING_PWM9=y
1455+
CONFIG_BSP_USING_PWM10=y
1456+
CONFIG_BSP_USING_PWM11=y
1457+
CONFIG_BSP_USING_PWM12=y
1458+
CONFIG_BSP_USING_PWM13=y
14461459
# CONFIG_BSP_USING_ONCHIP_RTC is not set
14471460
# CONFIG_BSP_USING_WDT is not set
14481461
# CONFIG_BSP_USING_SDIO is not set

bsp/gd32/arm/gd32405rg/applications/main.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,105 @@ int main(void)
3333

3434
return RT_EOK;
3535
}
36+
37+
#ifdef BSP_USING_HWTIMER
38+
/* hwtimer callback function */
39+
static rt_err_t hwtimer_test_cb(rt_device_t dev, rt_size_t size)
40+
{
41+
rt_kprintf("this is hwtimer timeout callback fucntion!\n");
42+
rt_kprintf("tick is :%d !\n", rt_tick_get());
43+
44+
return 0;
45+
}
46+
47+
#define HWTIMER_DEV_NAME "timer13"
48+
49+
/* hwtimer test */
50+
static void hwtimer_test(void)
51+
{
52+
rt_err_t ret = RT_EOK;
53+
rt_hwtimerval_t timeout_s; /* 定时器超时值 */
54+
rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
55+
rt_hwtimer_mode_t mode; /* 定时器模式 */
56+
rt_uint32_t freq = 10000; /* 计数频率 */
57+
58+
/* 查找定时器设备 */
59+
hw_dev = rt_device_find(HWTIMER_DEV_NAME);
60+
if (hw_dev == RT_NULL)
61+
{
62+
rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
63+
return;
64+
}
65+
66+
/* 以读写方式打开设备 */
67+
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
68+
if (ret != RT_EOK)
69+
{
70+
rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
71+
return;
72+
}
73+
74+
/* 设置超时回调函数 */
75+
rt_device_set_rx_indicate(hw_dev, hwtimer_test_cb);
76+
77+
/* 设置计数频率(若未设置该项,默认为1Mhz 或 支持的最小计数频率) */
78+
rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
79+
/* 设置模式为周期性定时器(若未设置,默认是HWTIMER_MODE_ONESHOT)*/
80+
mode = HWTIMER_MODE_PERIOD;
81+
ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
82+
if (ret != RT_EOK)
83+
{
84+
rt_kprintf("set mode failed! ret is :%d\n", ret);
85+
return;
86+
}
87+
88+
/* 设置定时器超时值为5s并启动定时器 */
89+
timeout_s.sec = 5; /* 秒 */
90+
timeout_s.usec = 0; /* 微秒 */
91+
if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
92+
{
93+
rt_kprintf("set timeout value failed\n");
94+
return;
95+
}
96+
}
97+
MSH_CMD_EXPORT(hwtimer_test, hwtimer test);
98+
#endif //BSP_USING_HWTIMER
99+
100+
#ifdef BSP_USING_PWM
101+
102+
/* pwm test */
103+
static void pwm_test(void)
104+
{
105+
rt_err_t ret = RT_EOK;
106+
struct rt_device_pwm *pwm2 = RT_NULL, *pwm8 = RT_NULL, *pwm1 = RT_NULL, *pwm4 = RT_NULL;
107+
rt_uint32_t period = 1000; /* 周期 */
108+
rt_uint32_t pulse = 500; /* 占空比 */
109+
110+
/* 查找PWM设备 */
111+
pwm2 = (struct rt_device_pwm *)rt_device_find("pwm2");
112+
if (pwm2 == RT_NULL)
113+
{
114+
rt_kprintf("pwm sample run failed! can't find %s device!\n", "pwm2");
115+
return;
116+
}
117+
rt_pwm_set(pwm2, 3, period, pulse);
118+
rt_pwm_enable(pwm2, 3);
119+
rt_pwm_set(pwm2, 4, period, pulse);
120+
rt_pwm_enable(pwm2, 4);
121+
122+
pwm8 = (struct rt_device_pwm *)rt_device_find("pwm8");
123+
rt_pwm_set(pwm8, 2, period, pulse);
124+
rt_pwm_enable(pwm8, 2);
125+
126+
pwm1 = (struct rt_device_pwm *)rt_device_find("pwm1");
127+
rt_pwm_set(pwm1, 3, period, pulse);
128+
rt_pwm_enable(pwm1, 3);
129+
130+
pwm4 = (struct rt_device_pwm *)rt_device_find("pwm4");
131+
rt_pwm_set(pwm4, 1, period, pulse);
132+
rt_pwm_enable(pwm4, 1);
133+
rt_pwm_set(pwm4, 2, period, pulse);
134+
rt_pwm_enable(pwm4, 2);
135+
}
136+
MSH_CMD_EXPORT(pwm_test, pwm test);
137+
#endif // BSP_USING_PWM

bsp/gd32/arm/gd32405rg/board/Kconfig

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,24 +271,134 @@ menu "On-chip Peripheral Drivers"
271271
default n
272272
endif
273273

274-
menuconfig BSP_USING_TIM
274+
menuconfig BSP_USING_HWTIMER
275275
bool "Enable timer"
276276
default n
277277
select RT_USING_HWTIMER
278-
if BSP_USING_TIM
279-
config BSP_USING_TIM10
278+
if BSP_USING_HWTIMER
279+
config BSP_USING_HWTIMER0
280+
bool "Enable TIM0"
281+
default n
282+
283+
config BSP_USING_HWTIMER1
284+
bool "Enable TIM1"
285+
default n
286+
287+
config BSP_USING_HWTIMER2
288+
bool "Enable TIM2"
289+
default n
290+
291+
config BSP_USING_HWTIMER3
292+
bool "Enable TIM3"
293+
default n
294+
295+
config BSP_USING_HWTIMER4
296+
bool "Enable TIM4"
297+
default n
298+
299+
config BSP_USING_HWTIMER5
300+
bool "Enable TIM5"
301+
default n
302+
303+
config BSP_USING_HWTIMER6
304+
bool "Enable TIM6"
305+
default y
306+
307+
config BSP_USING_HWTIMER7
308+
bool "Enable TIM7"
309+
default n
310+
311+
config BSP_USING_HWTIMER8
312+
bool "Enable TIM8"
313+
default n
314+
315+
config BSP_USING_HWTIMER9
316+
bool "Enable TIM9"
317+
default n
318+
319+
config BSP_USING_HWTIMER10
280320
bool "Enable TIM10"
281321
default n
282322

283-
config BSP_USING_TIM11
323+
config BSP_USING_HWTIMER11
284324
bool "Enable TIM11"
285325
default n
286326

287-
config BSP_USING_TIM12
327+
config BSP_USING_HWTIMER12
328+
bool "Enable TIM12"
329+
default n
330+
331+
config BSP_USING_HWTIMER13
288332
bool "Enable TIM13"
289333
default n
290334
endif
291335

336+
menuconfig BSP_USING_PWM
337+
bool "Enable pwm"
338+
select RT_USING_PWM
339+
default n
340+
if BSP_USING_PWM
341+
config BSP_USING_PWM0
342+
bool "Enable PWM0"
343+
depends on !BSP_USING_HWTIMER0
344+
default n
345+
346+
config BSP_USING_PWM1
347+
bool "Enable PWM1"
348+
depends on !BSP_USING_HWTIMER1
349+
default n
350+
351+
config BSP_USING_PWM2
352+
bool "Enable PWM2"
353+
depends on !BSP_USING_HWTIMER2
354+
default n
355+
356+
config BSP_USING_PWM3
357+
bool "Enable PWM3"
358+
depends on !BSP_USING_HWTIMER3
359+
default n
360+
361+
config BSP_USING_PWM4
362+
bool "Enable PWM4"
363+
depends on !BSP_USING_HWTIMER4
364+
default n
365+
366+
config BSP_USING_PWM7
367+
bool "Enable PWM7"
368+
depends on !BSP_USING_HWTIMER7
369+
default n
370+
371+
config BSP_USING_PWM8
372+
bool "Enable PWM8"
373+
depends on !BSP_USING_HWTIMER8
374+
default n
375+
376+
config BSP_USING_PWM9
377+
bool "Enable PWM9"
378+
depends on !BSP_USING_HWTIMER9
379+
default n
380+
381+
config BSP_USING_PWM10
382+
bool "Enable PWM10"
383+
depends on !BSP_USING_HWTIMER10
384+
default n
385+
386+
config BSP_USING_PWM11
387+
bool "Enable PWM11"
388+
depends on !BSP_USING_HWTIMER11
389+
default n
390+
391+
config BSP_USING_PWM12
392+
bool "Enable PWM12"
393+
depends on !BSP_USING_HWTIMER12
394+
default n
395+
396+
config BSP_USING_PWM13
397+
bool "Enable PWM13"
398+
depends on !BSP_USING_HWTIMER13
399+
default n
400+
endif
401+
292402
menuconfig BSP_USING_ONCHIP_RTC
293403
bool "Enable RTC"
294404
select RT_USING_RTC

0 commit comments

Comments
 (0)