1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
/*
* Copyright (c) 2006-2023
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-06-05 zengjianwei first version
* 2025-06-23 Yucai Liu Support for non-complementary PWM output with
* advanced timers
* 2025-12-26 shihongchao Optimize the timer clock frequency acquisition
* method; optimize the gd32_pwm structure to make
* it easier to configure; optimize the RCU enable
* logic; optimize GPIO configuration to maintain
* floating input mode when channels are disabled,
* reducing power consumption.
*/
#include <board.h>
#include <rtdevice.h>
#include <rtthread.h>
#ifdef BSP_USING_PWM
/* #define DRV_DEBUG */
#define LOG_TAG "drv.pwm"
#include <rtdbg.h>
#define MAX_PERIOD 65535
#define MIN_PERIOD 3
#define MIN_PULSE 2
typedef struct{
uint32_t gpio_port;
uint32_t gpio_af;
uint16_t gpio_pin;
}channel_type;
struct gd32_pwm
{
struct rt_device_pwm pwm_device; /* Inherit PWM device */
char *name; /* Device name */
uint32_t timerx; /* Hardware timer dependent on PWM */
rcu_clock_freq_enum apb_of; /* APB bus to which TIMER belongs */
channel_type channels[4]; /* PWM channels */
channel_type nchannels[3]; /* PWM complementary channels, only supported by advanced timers */
};
static struct gd32_pwm gd32_pwm_obj[] = {
#ifdef BSP_USING_PWM0
{
.name = "pwm0",
.timerx = TIMER0,
.apb_of = CK_APB2,
.channels = {
{GPIOC, GPIO_AF_1, GPIO_PIN_0},
{GPIOA, GPIO_AF_1, GPIO_PIN_1},
{GPIOA, GPIO_AF_1, GPIO_PIN_2},
{GPIOA, GPIO_AF_1, GPIO_PIN_3},
},
.nchannels = {
{GPIOB, GPIO_AF_1, GPIO_PIN_13},
{GPIOB, GPIO_AF_1, GPIO_PIN_14},
{GPIOB, GPIO_AF_1, GPIO_PIN_15},
}
},
#endif
#ifdef BSP_USING_PWM1
{
.name = "pwm1",
.timerx = TIMER1,
.apb_of = CK_APB1,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_0},
{GPIOA, GPIO_AF_1, GPIO_PIN_1},
{GPIOA, GPIO_AF_1, GPIO_PIN_2},
{GPIOB, GPIO_AF_1, GPIO_PIN_2},
},
},
#endif
#ifdef BSP_USING_PWM2
{
.name = "pwm2",
.timerx = TIMER2,
.apb_of = CK_APB1,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_6},
{GPIOA, GPIO_AF_1, GPIO_PIN_7},
{GPIOB, GPIO_AF_2, GPIO_PIN_0},
{GPIOB, GPIO_AF_2, GPIO_PIN_1},
},
},
#endif
#ifdef BSP_USING_PWM3
{
.name = "pwm3",
.timerx = TIMER3,
.apb_of = CK_APB1,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_0},
{GPIOA, GPIO_AF_1, GPIO_PIN_1},
{GPIOA, GPIO_AF_1, GPIO_PIN_2},
{GPIOA, GPIO_AF_1, GPIO_PIN_3},
},
},
#endif
#ifdef BSP_USING_PWM4
{
.name = "pwm4",
.timerx = TIMER4,
.apb_of = CK_APB1,
.channels = {
{GPIOA, GPIO_AF_2, GPIO_PIN_0},
{GPIOA, GPIO_AF_2, GPIO_PIN_1},
{GPIOA, GPIO_AF_1, GPIO_PIN_2},
{GPIOA, GPIO_AF_1, GPIO_PIN_3},
},
},
#endif
#ifdef BSP_USING_PWM7
{
.name = "pwm7",
.timerx = TIMER7,
.apb_of = CK_APB2,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_0},
{GPIOA, GPIO_AF_1, GPIO_PIN_1},
{GPIOC, GPIO_AF_1, GPIO_PIN_8},
{GPIOA, GPIO_AF_1, GPIO_PIN_3},
},
.nchannels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_5},
{GPIOB, GPIO_AF_1, GPIO_PIN_0},
{GPIOB, GPIO_AF_1, GPIO_PIN_1},
}
},
#endif
#ifdef BSP_USING_PWM8
{
.name = "pwm8",
.timerx = TIMER8,
.apb_of = CK_APB2,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_2},
{GPIOA, GPIO_AF_3, GPIO_PIN_3},
}, // L1 general-purpose timer is a two-channel timer
},
#endif
#ifdef BSP_USING_PWM9
{
.name = "pwm9",
.timerx = TIMER9,
.apb_of = CK_APB2,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_0},
}, // L2 general-purpose timer is a single-channel timer
},
#endif
#ifdef BSP_USING_PWM10
{
.name = "pwm10",
.timerx = TIMER10,
.apb_of = CK_APB2,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_0},
}, // L2 general-purpose timer is a single-channel timer
},
#endif
#ifdef BSP_USING_PWM11
{
.name = "pwm11",
.timerx = TIMER11,
.apb_of = CK_APB1,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_0},
{GPIOA, GPIO_AF_1, GPIO_PIN_1},
}, // L1 general-purpose timer is a two-channel timer
},
#endif
#ifdef BSP_USING_PWM12
{
.name = "pwm12",
.timerx = TIMER12,
.apb_of = CK_APB1,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_0},
}, // L2 general-purpose timer is a single-channel timer
},
#endif
#ifdef BSP_USING_PWM13
{
.name = "pwm13",
.timerx = TIMER13,
.apb_of = CK_APB1,
.channels = {
{GPIOA, GPIO_AF_1, GPIO_PIN_7},
}, // L2 general-purpose timer is a single-channel timer
},
#endif
};
static void gpio_clock_enable(rt_uint32_t Port)
{
switch (Port)
{
case GPIOA:
rcu_periph_clock_enable(RCU_GPIOA);
break;
case GPIOB:
rcu_periph_clock_enable(RCU_GPIOB);
break;
case GPIOC:
rcu_periph_clock_enable(RCU_GPIOC);
break;
case GPIOD:
rcu_periph_clock_enable(RCU_GPIOD);
break;
case GPIOE:
rcu_periph_clock_enable(RCU_GPIOE);
break;
case GPIOF:
rcu_periph_clock_enable(RCU_GPIOF);
break;
case GPIOG:
rcu_periph_clock_enable(RCU_GPIOG);
break;
default:
LOG_E("Unsport gpio port!\n");
}
}
static void timer_clock_enable(uint32_t timer)
{
switch (timer)
{
case TIMER0:
rcu_periph_clock_enable(RCU_TIMER0);
break;
case TIMER1:
rcu_periph_clock_enable(RCU_TIMER1);
break;
case TIMER2:
rcu_periph_clock_enable(RCU_TIMER2);
break;
case TIMER3:
rcu_periph_clock_enable(RCU_TIMER3);
break;
case TIMER4:
rcu_periph_clock_enable(RCU_TIMER4);
break;
case TIMER5:
rcu_periph_clock_enable(RCU_TIMER5);
break;
case TIMER6:
rcu_periph_clock_enable(RCU_TIMER6);
break;
case TIMER7:
rcu_periph_clock_enable(RCU_TIMER7);
break;
case TIMER8:
rcu_periph_clock_enable(RCU_TIMER8);
break;
case TIMER9:
rcu_periph_clock_enable(RCU_TIMER9);
break;
case TIMER10:
rcu_periph_clock_enable(RCU_TIMER10);
break;
case TIMER11:
rcu_periph_clock_enable(RCU_TIMER11);
break;
case TIMER12:
rcu_periph_clock_enable(RCU_TIMER12);
break;
case TIMER13:
rcu_periph_clock_enable(RCU_TIMER13);
break;
default:
LOG_E("Unsport timer periph!\n");
}
}
static void rcu_config(void)
{
rt_int16_t i;
for (i = 0; i < sizeof(gd32_pwm_obj) / sizeof(gd32_pwm_obj[0]); ++i)
{
/* enable GPIO clock */
switch (gd32_pwm_obj[i].timerx)
{
/* Advanced timer */
case TIMER0:
case TIMER7:
gpio_clock_enable(gd32_pwm_obj[i].nchannels[0].gpio_port);
gpio_clock_enable(gd32_pwm_obj[i].nchannels[1].gpio_port);
gpio_clock_enable(gd32_pwm_obj[i].nchannels[2].gpio_port);
/* L0 general-purpose timer */
case TIMER1:
case TIMER2:
case TIMER3:
case TIMER4:
gpio_clock_enable(gd32_pwm_obj[i].channels[2].gpio_port);
gpio_clock_enable(gd32_pwm_obj[i].channels[3].gpio_port);
/* L1 general-purpose timer */
case TIMER8:
case TIMER11:
gpio_clock_enable(gd32_pwm_obj[i].channels[1].gpio_port);
/* L2 general-purpose timer */
case TIMER9:
case TIMER10:
case TIMER12:
case TIMER13:
gpio_clock_enable(gd32_pwm_obj[i].channels[0].gpio_port);
break;
default:
LOG_E("Unsport timer periph at rcu_config!\n");
break;
}
}
for (i = 0; i < sizeof(gd32_pwm_obj) / sizeof(gd32_pwm_obj[0]); ++i)
{
/* enable timer clock */
timer_clock_enable(gd32_pwm_obj[i].timerx);
timer_deinit(gd32_pwm_obj[i].timerx);
}
}
/**
* @brief Configure PWM output pin to PWM output mode
* @param pwm PWM object
* @param configuration Configuration information passed by PWM driver framework
*/
static void gpio_config_pwmout(const struct gd32_pwm *pwm,
const struct rt_pwm_configuration *configuration)
{
channel_type channel;
uint8_t channel_num = configuration->channel;
if(channel_num == 0)
{
LOG_E("PWM channel starts from 1!\n");
return;
}
if(configuration->complementary)
{
if(channel_num > 3)
{
LOG_E("GD32 PWM complementary channel max 3!\n");
return;
};
channel = pwm->nchannels[channel_num-1];
}
else
{
if(channel_num > 4)
{
LOG_E("GD32 PWM complementary channel max 4!\n");
return;
};
channel = pwm->channels[channel_num-1];
}
gpio_mode_set(channel.gpio_port, GPIO_MODE_AF, GPIO_PUPD_NONE, channel.gpio_pin);
gpio_output_options_set(channel.gpio_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, channel.gpio_pin);
gpio_af_set(channel.gpio_port, channel.gpio_af, channel.gpio_pin);
}
/**
* @brief Configure PWM output pin to PWM floating input mode
* @param pwm PWM object
* @param configuration Configuration information passed by PWM driver framework
*/
static void gpio_config_input(const struct gd32_pwm *pwm,
const struct rt_pwm_configuration *configuration)
{
channel_type channel = {0};
uint8_t channel_num = configuration->channel;
if(channel_num == 0)
{
return;
}
if(configuration->complementary)
{
if(channel_num > 3)
{
LOG_E("GD32 PWM complementary channel max 3!\n");
return;
};
channel = pwm->nchannels[channel_num-1];
}
else
{
if(channel_num > 4)
{
LOG_E("GD32 PWM complementary channel max 4!\n");
return;
};
channel = pwm->channels[channel_num-1];
}
gpio_mode_set(channel.gpio_port, GPIO_MODE_INPUT, GPIO_PUPD_NONE, channel.gpio_pin);
}
static void channel_output_config(rt_uint32_t timer_periph, timer_oc_parameter_struct *ocpara)
{
switch (timer_periph)
{
/* Advanced timer */
case TIMER0:
case TIMER7:
timer_primary_output_config(timer_periph, ENABLE);
/* L0 general-purpose timer */
case TIMER1:
case TIMER2:
case TIMER3:
case TIMER4:
timer_channel_output_config(timer_periph, TIMER_CH_2, ocpara);
timer_channel_output_pulse_value_config(timer_periph, TIMER_CH_2, 7999);
timer_channel_output_mode_config(timer_periph, TIMER_CH_2, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(timer_periph, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload shadow reg enable */
/* timer_auto_reload_shadow_enable(timer_periph); */
timer_channel_output_state_config(timer_periph, TIMER_CH_2, TIMER_CCX_DISABLE);
timer_channel_complementary_output_state_config(timer_periph, TIMER_CH_2, TIMER_CCXN_DISABLE);
timer_channel_output_config(timer_periph, TIMER_CH_3, ocpara);
timer_channel_output_pulse_value_config(timer_periph, TIMER_CH_3, 7999);
timer_channel_output_mode_config(timer_periph, TIMER_CH_3, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(timer_periph, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload shadow reg enable */
/* timer_auto_reload_shadow_enable(timer_periph); */
timer_channel_output_state_config(timer_periph, TIMER_CH_3, TIMER_CCX_DISABLE);
timer_channel_complementary_output_state_config(timer_periph, TIMER_CH_3, TIMER_CCXN_DISABLE);
/* L1 general-purpose timer */
case TIMER8:
case TIMER11:
timer_channel_output_config(timer_periph, TIMER_CH_1, ocpara);
timer_channel_output_pulse_value_config(timer_periph, TIMER_CH_1, 7999);
timer_channel_output_mode_config(timer_periph, TIMER_CH_1, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(timer_periph, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload shadow reg enable */
/* timer_auto_reload_shadow_enable(timer_periph); */
timer_channel_output_state_config(timer_periph, TIMER_CH_1, TIMER_CCX_DISABLE);
timer_channel_complementary_output_state_config(timer_periph, TIMER_CH_1, TIMER_CCXN_DISABLE);
/* L2 general-purpose timer */
case TIMER9:
case TIMER10:
case TIMER12:
case TIMER13:
timer_channel_output_config(timer_periph, TIMER_CH_0, ocpara);
timer_channel_output_pulse_value_config(timer_periph, TIMER_CH_0, 7999);
timer_channel_output_mode_config(timer_periph, TIMER_CH_0, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(timer_periph, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload shadow reg enable */
/* timer_auto_reload_shadow_enable(timer_periph); */
timer_channel_output_state_config(timer_periph, TIMER_CH_0, TIMER_CCX_DISABLE);
timer_channel_complementary_output_state_config(timer_periph, TIMER_CH_0, TIMER_CCXN_DISABLE);
break;
default:
LOG_E("Unsport timer periph at channel_output_config!\n");
break;
}
timer_enable(timer_periph);
}
static void timer_config(void)
{
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
/* TIMER configuration */
timer_initpara.prescaler = 199;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 15999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
for (size_t i = 0; i < sizeof(gd32_pwm_obj) / sizeof(gd32_pwm_obj[0]); ++i)
{
timer_init(gd32_pwm_obj[i].timerx, &timer_initpara);
}
/* CHX configuration in PWM mode */
timer_ocintpara.outputstate = TIMER_CCX_DISABLE;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
/* config the channel config */
for (size_t i = 0; i < sizeof(gd32_pwm_obj) / sizeof(gd32_pwm_obj[0]); ++i)
{
channel_output_config(gd32_pwm_obj[i].timerx, &timer_ocintpara);
}
}
static rt_err_t drv_pwm_enable(struct gd32_pwm *pwm, const struct rt_pwm_configuration *configuration,
rt_bool_t enable)
{
uint8_t channel_num = configuration->channel;
if(channel_num == 0)
{
LOG_E("GD32 PWM channel starts from 1!\n");
return -RT_EINVAL;
}
if(configuration->complementary)
{
if(channel_num > 3)
{
LOG_E("GD32 PWM complementary channel max 3!\n");
return -RT_EINVAL;
};
}
else
{
if(channel_num > 4)
{
LOG_E("GD32 PWM complementary channel max 4!\n");
return -RT_EINVAL;
};
}
if (!enable)
{
gpio_config_input(pwm, configuration);
if (configuration->complementary == RT_TRUE)
{
timer_channel_complementary_output_state_config(pwm->timerx, configuration->channel-1,
TIMER_CCXN_DISABLE);
}
else
{
timer_channel_output_state_config(pwm->timerx, configuration->channel-1,
TIMER_CCX_DISABLE);
}
}
else
{
gpio_config_pwmout(pwm, configuration);
if (configuration->complementary == RT_TRUE)
{
timer_channel_complementary_output_state_config(pwm->timerx, configuration->channel-1,
TIMER_CCXN_ENABLE);
}
else
{
timer_channel_output_state_config(pwm->timerx, configuration->channel-1,
TIMER_CCX_ENABLE);
}
}
return RT_EOK;
}
static rt_err_t drv_pwm_get(const struct gd32_pwm *pwm, struct rt_pwm_configuration *configuration)
{
rt_uint64_t tim_clock;
rt_uint16_t psc;
rt_uint32_t chxcv;
if(configuration->channel == 0)
{
return -RT_EINVAL;
}
rt_uint8_t coef = (RCU_CFG1&RCU_CFG1_TIMERSEL)?4:2;
tim_clock = rcu_clock_freq_get(pwm->apb_of)*coef;
psc = timer_prescaler_read(pwm->timerx);
if (psc == TIMER_CKDIV_DIV2)
{
tim_clock = tim_clock / 2;
}
else if (psc == TIMER_CKDIV_DIV4)
{
tim_clock = tim_clock / 4;
}
chxcv = timer_channel_capture_value_register_read(pwm->timerx, configuration->channel-1);
/* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
tim_clock /= 1000000UL;
configuration->period = (TIMER_CAR(pwm->timerx) + 1) * (psc + 1) * 1000UL / tim_clock;
configuration->pulse = (chxcv + 1) * (psc + 1) * 1000UL / tim_clock;
return RT_EOK;
}
static rt_err_t drv_pwm_set(struct gd32_pwm *pwm, struct rt_pwm_configuration *configuration)
{
rt_uint32_t period, pulse;
rt_uint64_t tim_clock, psc;
rt_uint8_t coef = (RCU_CFG1&RCU_CFG1_TIMERSEL)?4:2;
tim_clock = rcu_clock_freq_get(pwm->apb_of)*coef;
if(configuration->channel == 0)
{
return -RT_EINVAL;
}
/* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
tim_clock /= 1000000UL;
period = (unsigned long long)configuration->period * tim_clock / 1000ULL;
psc = period / MAX_PERIOD + 1;
period = period / psc;
timer_prescaler_config(pwm->timerx, psc - 1, TIMER_PSC_RELOAD_NOW);
if (period < MIN_PERIOD)
{
period = MIN_PERIOD;
}
timer_autoreload_value_config(pwm->timerx, period - 1);
pulse = (unsigned long long)configuration->pulse * tim_clock / psc / 1000ULL;
if (pulse < MIN_PULSE)
{
pulse = MIN_PULSE;
}
else if (pulse > period)
{
pulse = period;
}
timer_channel_output_pulse_value_config(pwm->timerx, configuration->channel-1, pulse);
timer_counter_value_config(pwm->timerx, 0);
/* Update frequency value */
timer_event_software_generate(pwm->timerx, TIMER_EVENT_SRC_UPG);
return RT_EOK;
}
static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
{
struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
struct gd32_pwm *pwm = rt_container_of(device, struct gd32_pwm, pwm_device);
switch (cmd)
{
case PWM_CMD_ENABLE:
return drv_pwm_enable(pwm, configuration, RT_TRUE);
case PWM_CMD_DISABLE:
return drv_pwm_enable(pwm, configuration, RT_FALSE);
case PWM_CMD_SET:
return drv_pwm_set(pwm, configuration);
case PWM_CMD_GET:
return drv_pwm_get(pwm, configuration);
default:
return -RT_EINVAL;
}
}
static struct rt_pwm_ops drv_ops = {drv_pwm_control};
static rt_err_t gd32_hw_pwm_init(void)
{
rcu_config();
timer_config();
/*
* GPIO is not configured here. When PWM channel is enabled, it will be configured as PWM output.
* When disabled, it will be configured as floating input.
* GPIO defaults to floating input.
*/
return RT_EOK;
}
static int rt_hw_pwm_init(void)
{
int i = 0;
int result = RT_EOK;
/* pwm init */
if (gd32_hw_pwm_init() != RT_EOK)
{
LOG_E("PWM init failed");
result = -RT_ERROR;
goto __exit;
}
LOG_D("PWM init success");
for (i = 0; i < sizeof(gd32_pwm_obj) / sizeof(gd32_pwm_obj[0]); i++)
{
/* register pwm device */
if (rt_device_pwm_register(&gd32_pwm_obj[i].pwm_device, gd32_pwm_obj[i].name, &drv_ops,
RT_NULL)== RT_EOK )
{
LOG_D("%s register success", gd32_pwm_obj[i].name);
}
else
{
LOG_E("%s register failed", gd32_pwm_obj[i].name);
result = -RT_ERROR;
}
}
__exit:
return result;
}
INIT_DEVICE_EXPORT(rt_hw_pwm_init);
#endif /* BSP_USING_PWM */
|