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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
|
/**************************************************************************//**
* @file pwm.c
* @brief N9H30 series PWM driver source file
*
* @note
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "N9H30.h"
#include "nu_sys.h"
#include "nu_pwm.h"
/** @addtogroup N9H30_Device_Driver N9H30 Device Driver
@{
*/
/** @addtogroup N9H30_PWM_Driver PWM Driver
@{
*/
/** @addtogroup N9H30_PWM_EXPORTED_FUNCTIONS PWM Exported Functions
@{
*/
//Internal function definition
/// @cond HIDDEN_SYMBOLS
void pwmISR(PVOID pvParam);
static INT pwmInitGPIO(const INT nTimerIdentity, const INT nValue);
static INT pwmInitTimer(const INT nTimerIdentity);
static INT pwmStartTimer(const INT nTimerIdentity);
static INT pwmStopTimer(const INT nTimerIdentity, const INT nMethod);
// Register operation
static INT pwmSetCP(const INT nTimerIdentity, const INT nValue);
static INT pwmSetDZI(const INT nTimerIdentity, const INT nValue);
static INT pwmSetCSR(const INT nTimerIdentity, const INT nValue);
static INT pwmSetDZGenerator(const INT nTimerIdentity, const INT nStatus);
static INT pwmSetTimerState(const INT nTimerIdentity, const INT nStatus);
static INT pwmSetInverter(const INT nTimerIdentity, const INT nStatus);
static INT pwmSetMode(const INT nTimerIdentity, const INT nStatus);
static INT pwmSetCNR(const INT nTimerIdentity, const INT nValue);
static INT pwmSetCMR(const INT nTimerIdentity, const INT nValue);
static UINT pwmGetPDR(const INT nTimerIdentity);
static INT pwmSetPIER(const INT nTimerIdentity, const INT value);
static INT pwmCleanPIIR(const INT nTimerIdentity);
//Global variable
static BOOL bPWMIRQFlag = FALSE; //IRQ enable flag, set after PWM IRQ enable
static BOOL bPWMTimerOpenStatus[PWM_TIMER_NUM]; //timer flag which set after open(for disable IRQ decision)
static BOOL bPWMTimerStartStatus[PWM_TIMER_NUM]; //timer flag which set after Start count(to avoid incorrectly stop procedure)
static BOOL bPWMTimerMode[PWM_TIMER_NUM]; //PWM timer toggle/one shot mode
static BOOL volatile bPWMIntFlag[PWM_TIMER_NUM]; //interrupt flag which set by ISR
/// @endcond /* HIDDEN_SYMBOLS */
/**
* @brief The init function of PWM device driver
*/
INT pwmInit(void)
{
UINT temp;
// Enable PWM clock
temp = inpw(REG_CLK_PCLKEN1);
temp = temp | 0x8000000;
outpw(REG_CLK_PCLKEN1, temp);
sysInstallISR(IRQ_LEVEL_1, PWM_IRQn, (PVOID)pwmISR);
sysSetLocalInterrupt(ENABLE_IRQ); // Enable CPSR I bit
return 0;
}
/**
* @brief The exit function of PWM device driver
*/
INT pwmExit(void)
{
return 0;
}
/**
* @brief The open function of PWM device driver
* @param[in] nTimerIdentity PWM Timer channel identity
* @retval Successful PWM successfully opened
* @retval pwmTimerBusy PWM timer already open
* @retval pwmInvalidTimerChannel PWM Timer channel number error
*/
INT pwmOpen(const INT nTimerIdentity)
{
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// nTimerIdentity value error
}
if (bPWMTimerOpenStatus[nTimerIdentity] == TRUE)
{
return pwmTimerBusy;
}
if (bPWMIRQFlag == FALSE)
{
sysEnableInterrupt(PWM_IRQn);
bPWMIRQFlag = TRUE;
}
bPWMTimerOpenStatus[nTimerIdentity] = TRUE;
// Set PWM timer default value(CSR->PPR->PCR->CMR->CNR)
pwmInitTimer(nTimerIdentity);
//Enable PIER
pwmSetPIER(nTimerIdentity, PWM_ENABLE);
//Reset PIIR
pwmCleanPIIR(nTimerIdentity);
//Reset PWM timer start count flag
bPWMTimerStartStatus[nTimerIdentity] = FALSE;
return Successful;
}
/**
* @brief The close function of PWM device driver
* @param[in] nTimerIdentity PWM Timer channel identity
* @retval Successful PWM successfully closed
* @retval pwmTimerNotOpen PWM timer not open
* @retval pwmInvalidTimerChannel PWM Timer channel number error
*/
INT pwmClose(const INT nTimerIdentity)
{
INT nLoop;
BOOL uAllTimerClose = TRUE;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// nTimerIdentity value error
}
if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
{
return pwmTimerNotOpen;
}
bPWMTimerOpenStatus[nTimerIdentity] = FALSE;
//Check if all timer stop, IRQ can be disable
for (nLoop = PWM_TIMER_MIN; nLoop < PWM_TIMER_NUM; nLoop++)
{
if (bPWMTimerOpenStatus[nLoop] == TRUE)
{
uAllTimerClose = FALSE;
}
}
//All timer stop, disable IRQs
if (uAllTimerClose == TRUE)
{
sysDisableInterrupt(PWM_IRQn);
bPWMIRQFlag = FALSE;
}
pwmSetPIER(nTimerIdentity, PWM_DISABLE);
pwmCleanPIIR(nTimerIdentity);
return Successful;
}
/**
* @brief The read function of PWM device driver
* @param[in] nTimerIdentity PWM Timer channel identity
* @param[out] pucStatusValue The point of typePWMSTATUS
* @param[in] uLength The length of typePWMSTATUS
* @retval Successful Read PWM value successfully
* @retval pwmTimerNotOpen PWM timer not open
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidStructLength Struct length error(struct type error)
*/
INT pwmRead(const INT nTimerIdentity, PUCHAR pucStatusValue, const UINT uLength)
{
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// nTimerIdentity value error
}
if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
{
return pwmTimerNotOpen;
}
if (uLength != sizeof(typePWMSTATUS))
{
return pwmInvalidStructLength;// Struct length error(struct type error)
}
if (sizeof(*((typePWMSTATUS *)pucStatusValue)) != sizeof(typePWMSTATUS))
{
return pwmInvalidStructLength;// Struct length error(struct type error)
}
((typePWMSTATUS *)pucStatusValue)->PDR = pwmGetPDR(nTimerIdentity);
if (bPWMIntFlag[nTimerIdentity] == TRUE)
{
bPWMIntFlag[nTimerIdentity] = FALSE;
((typePWMSTATUS *)pucStatusValue)->InterruptFlag = TRUE;
}
else
{
((typePWMSTATUS *)pucStatusValue)->InterruptFlag = FALSE;
}
return Successful;
}
/**
* @brief The write function of PWM device driver
* @param[in] nTimerIdentity PWM Timer channel identity
* @param[in] pucCNRCMRValue The value of CNR and CMR
* @param[in] uLength For future usage
* @retval Successful Write PWM setting successfully
* @retval pwmTimerNotOpen PWM timer not open
* @retval pwmInvalidTimerChannel PWM Timer channel number error
*/
INT pwmWrite(const INT nTimerIdentity, PUCHAR pucCNRCMRValue, const UINT uLength)
{
typePWMVALUE pwmvalue;
INT nStatus;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// nTimerIdentity value error
}
if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
{
return pwmTimerNotOpen;
}
if (uLength != sizeof(typePWMVALUE))
{
return pwmInvalidStructLength;// Struct length error(struct type error)
}
pwmvalue.value = ((typePWMVALUE *)pucCNRCMRValue)->value;
nStatus = pwmSetCNR(nTimerIdentity, pwmvalue.field.cnr);
if (nStatus != Successful)
{
return nStatus;
}
nStatus = pwmSetCMR(nTimerIdentity, pwmvalue.field.cmr);
if (nStatus != Successful)
{
return nStatus;
}
return Successful;
}
/**
* @brief The ioctl function of PWM device driver
* @param[in] nTimerIdentity PWM Timer channel identity
* @param[in] uCommand Ioctl command which indicates different operation
* @param[in] uIndication Not use in PWM
* @param[in] uValue The value which use with uCommand
* @retval Successful PWM ioctl execute successfully
* @retval pwmTimerNotOpen PWM timer not open
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidIoctlCommand Ioctl command error
* @retval Others Error according to different uCommand
*/
INT pwmIoctl(const INT nTimerIdentity, const UINT uCommand, const UINT uIndication, UINT uValue)
{
INT nStatus;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// nTimerIdentity value error
}
if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
{
return pwmTimerNotOpen;
}
switch (uCommand)
{
case START_PWMTIMER:
{
nStatus = pwmStartTimer(nTimerIdentity);
break;
}
case STOP_PWMTIMER:
{
// default stop method is 2
nStatus = pwmStopTimer(nTimerIdentity, PWM_STOP_METHOD2);
break;
}
case SET_CSR:
{
nStatus = pwmSetCSR(nTimerIdentity, uValue);
break;
}
case SET_CP:
{
nStatus = pwmSetCP(nTimerIdentity, uValue);
break;
}
case SET_DZI:
{
nStatus = pwmSetDZI(nTimerIdentity, uValue);
break;
}
case SET_INVERTER:
{
nStatus = pwmSetInverter(nTimerIdentity, uValue);
break;
}
case SET_MODE:
{
nStatus = pwmSetMode(nTimerIdentity, uValue);
break;
}
case ENABLE_DZ_GENERATOR:
{
nStatus = pwmSetDZGenerator(nTimerIdentity, PWM_ENABLE);
break;
}
case DISABLE_DZ_GENERATOR:
{
nStatus = pwmSetDZGenerator(nTimerIdentity, PWM_DISABLE);
break;
}
case ENABLE_PWMGPIOOUTPUT:
{
nStatus = pwmInitGPIO(nTimerIdentity, uValue);
break;
}
default:
{
return pwmInvalidIoctlCommand;
}
}
return nStatus;
}
/// @cond HIDDEN_SYMBOLS
/**
* @brief The interrupt service routines of PWM
* @param[in] pvParam IRQ Parameter(not use in PWM)
*/
VOID pwmISR(PVOID pvParam)
{
INT i;
UINT32 uRegisterValue = 0;
uRegisterValue = inpw(REG_PWM_PIIR);// Get PIIR value
for (i = 0; i < PWM_TIMER_NUM ; i++)
{
if (uRegisterValue & (1 << i))
{
bPWMIntFlag[i] = 1;
outpw(REG_PWM_PIIR, (1 << i));
}
}
}
/**
* @brief This function set corresponding GPIO as PWM function according to the
* parameter nTimerIdentity
* @param[in] nTimerIdentity Timer channel number
* @retval Successful PWM init GPIO successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidPin PWM output pin setting error
*/
static INT pwmInitGPIO(const INT nTimerIdentity, const INT nValue)
{
UINT temp = 0;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nTimerIdentity == PWM_TIMER0)
{
if (nValue == PWM0_GPA12)
{
temp = inpw(REG_SYS_GPA_MFPH);
temp = (temp & ~0x000F0000) | 0xD0000;
outpw(REG_SYS_GPA_MFPH, temp);
}
else if (nValue == PWM0_GPB2)
{
temp = inpw(REG_SYS_GPB_MFPL);
temp = (temp & ~0xF00) | 0xD00;
outpw(REG_SYS_GPB_MFPL, temp);
}
else
return pwmInvalidPin;
}
else if (nTimerIdentity == PWM_TIMER1)
{
if (nValue == PWM1_GPA13)
{
temp = inpw(REG_SYS_GPA_MFPH);
temp = (temp & ~0x00F00000) | 0xD00000;
outpw(REG_SYS_GPA_MFPH, temp);
}
else if (nValue == PWM1_GPB3)
{
temp = inpw(REG_SYS_GPB_MFPL);
temp = (temp & ~0xF000) | 0xD000;
outpw(REG_SYS_GPB_MFPL, temp);
}
else
return pwmInvalidPin;
}
else if (nTimerIdentity == PWM_TIMER2)
{
if (nValue == PWM2_GPA14)
{
temp = inpw(REG_SYS_GPA_MFPH);
temp = (temp & ~0x0F000000) | 0xD000000;
outpw(REG_SYS_GPA_MFPH, temp);
}
else if (nValue == PWM2_GPH2)
{
temp = inpw(REG_SYS_GPH_MFPL);
temp = (temp & ~0xF00) | 0xD00;
outpw(REG_SYS_GPH_MFPL, temp);
}
else
return pwmInvalidPin;
}
else
{
if (nValue == PWM3_GPA15)
{
temp = inpw(REG_SYS_GPA_MFPH);
temp = (temp & ~0xF0000000) | 0xD0000000;
outpw(REG_SYS_GPA_MFPH, temp);
}
else if (nValue == PWM3_GPH3)
{
temp = inpw(REG_SYS_GPH_MFPL);
temp = (temp & ~0xF000) | 0xD000;
outpw(REG_SYS_GPH_MFPL, temp);
}
else
return pwmInvalidPin;
}
return Successful;
}
/**
* @brief This function initiates PWM timer n and set the default setting to CSR,
* PPR, PCR, CNR, CMR
* @param[in] nTimerIdentity Timer channel number
* @retval Successful PWM init timer successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
*/
static INT pwmInitTimer(const INT nTimerIdentity)
{
typePPR PWMPPR;
INT nStatus;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// nTimerIdentity value error
}
//Set CSR
nStatus = pwmSetCSR(nTimerIdentity, DEFAULT_CSR);
if (nStatus != Successful)
{
return nStatus;
}
//Set PPR
PWMPPR.value = (UINT)inpw(REG_PWM_PPR);
switch (nTimerIdentity)
{
case PWM_TIMER0:
{
if (PWMPPR.field.cp0 == 0)
{
pwmSetCP(nTimerIdentity, DEFAULT_CP);
}
break;
}
case PWM_TIMER1:
{
if (PWMPPR.field.cp0 == 0)
{
pwmSetCP(nTimerIdentity, DEFAULT_CP);
}
break;
}
case PWM_TIMER2:
{
if (PWMPPR.field.cp1 == 0)
{
pwmSetCP(nTimerIdentity, DEFAULT_CP);
}
break;
}
case PWM_TIMER3:
{
if (PWMPPR.field.cp1 == 0)
{
pwmSetCP(nTimerIdentity, DEFAULT_CP);
}
break;
}
}
//Set PCR
nStatus = pwmSetMode(nTimerIdentity, DEFAULT_MODE);
if (nStatus != Successful)
{
return nStatus;
}
bPWMTimerMode[nTimerIdentity] = DEFAULT_MODE;
//Set CMR
nStatus = pwmSetCMR(nTimerIdentity, DEFAULT_CMR);
if (nStatus != Successful)
{
return nStatus;
}
//Set CNR
nStatus = pwmSetCNR(nTimerIdentity, DEFAULT_CNR);
if (nStatus != Successful)
{
return nStatus;
}
return Successful;
}
/**
* @brief This function starts PWM timer according to the parameter
* @param[in] nTimerIdentity Timer channel number
* @retval Successful PWM start timer successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
*/
static INT pwmStartTimer(const INT nTimerIdentity)
{
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
pwmSetTimerState(nTimerIdentity, PWM_ENABLE);
if (bPWMTimerMode[nTimerIdentity] == PWM_TOGGLE)
{
bPWMTimerStartStatus[nTimerIdentity] = TRUE;
}
return Successful;
}
/**
* @brief This function stops PWM timer n using method 1, 2, or 3 according to the
* parameter nTimerIdentity and nStatus
* @param[in] nTimerIdentity Timer channel number
* @param[in] nMethod Stop PWM timer method
* @retval Successful PWM stop timer successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidStopMethod Stop method error
*/
static INT pwmStopTimer(const INT nTimerIdentity, INT nMethod)
{
typeCNR PWMCNR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
// Timer_num value error
return pwmInvalidTimerChannel;
}
//Can't stop before open PWM timer
if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
{
return Successful;
}
// one shot mode didn't need stop procedure
if (bPWMTimerMode[nTimerIdentity] == PWM_ONESHOT)
{
return Successful;
}
// Timer stop already, no need to stop again
if (bPWMTimerStartStatus[nTimerIdentity] == FALSE)
{
return Successful;
}
// Set CNR as 0
PWMCNR.field.cnr = 0;
outpw(REG_PWM_CNR0 + (PWM_OFFSET * nTimerIdentity), PWMCNR.value);
switch (nMethod)
{
case PWM_STOP_METHOD1:
{
while (1)
{
if (pwmGetPDR(nTimerIdentity) == 0) // Wait PDR reach to 0
{
pwmSetTimerState(nTimerIdentity, PWM_DISABLE);// Disable pwm timer
bPWMIntFlag[nTimerIdentity] = FALSE;
bPWMTimerStartStatus[nTimerIdentity] = FALSE;
break;
}
}
break;
}
case PWM_STOP_METHOD2:
{
while (1)
{
if (bPWMIntFlag[nTimerIdentity] == TRUE) // Wait interrupt happen
{
pwmSetTimerState(nTimerIdentity, PWM_DISABLE);// Disable pwm timer
bPWMIntFlag[nTimerIdentity] = FALSE;
bPWMTimerStartStatus[nTimerIdentity] = FALSE;
break;
}
}
break;
}
/*case PWM_STOP_METHOD3:
{
pwmSetPCRState(nTimerIdentity, PWM_DISABLE);// Disable pwm timer
bPWMIntFlag[nTimerIdentity] = FALSE;
bPWMTimerStartStatus[nTimerIdentity] = FALSE;
break;
}*/
default:
{
return pwmInvalidStopMethod;// Stop method value error
}
}
return Successful;
}
/**
* @brief This function set CPn value according to the parameter nTimerIdentity and nValue
* @param[in] nTimerIdentity Timer channel number
* @param[in] nValue The value which want to set in CSRn
* @retval Successful Set CPn successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidCPValue PWM_PPR CPn value out of range
*/
static INT pwmSetCP(const INT nTimerIdentity, const INT nValue)
{
typePPR PWMPPR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nValue < CP_MIN || nValue > CP_MAX)
{
return pwmInvalidCPValue;// CP value error
}
PWMPPR.value = (UINT)inpw(REG_PWM_PPR);
switch (nTimerIdentity)
{
case PWM_TIMER0:
{
PWMPPR.field.cp0 = nValue;
break;
}
case PWM_TIMER1:
{
PWMPPR.field.cp0 = nValue;
break;
}
case PWM_TIMER2:
{
PWMPPR.field.cp1 = nValue;
break;
}
case PWM_TIMER3:
{
PWMPPR.field.cp1 = nValue;
break;
}
}
outpw(REG_PWM_PPR, PWMPPR.value);
return Successful;
}
/**
* @brief This function set DZIn value according to the parameter nTimerIdentity and nValue
* @param[in] nTimerIdentity Timer channel number
* @param[in] nValue The value which want to set in DZIn
* @retval Successful Set DZIn successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidDZIValue PWM_PPR DZIn value out of range
*/
static INT pwmSetDZI(const INT nTimerIdentity, const INT nValue)
{
typePPR PWMPPR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nValue < DZI_MIN || nValue > DZI_MAX)
{
return pwmInvalidDZIValue;// CSR value error
}
PWMPPR.value = (UINT)inpw(REG_PWM_PPR);
switch (nTimerIdentity)
{
case PWM_TIMER0:
{
PWMPPR.field.dzi0 = nValue;
break;
}
case PWM_TIMER1:
{
PWMPPR.field.dzi0 = nValue;
break;
}
case PWM_TIMER2:
{
PWMPPR.field.dzi1 = nValue;
break;
}
case PWM_TIMER3:
{
PWMPPR.field.dzi1 = nValue;
break;
}
}
outpw(REG_PWM_PPR, PWMPPR.value);
return Successful;
}
/**
* @brief This function set CSRn value according to the parameter nTimerIdentity and nValue
* @param[in] nTimerIdentity Timer channel number
* @param[in] nValue The value which want to set in CSRn
* @retval Successful Set CSRn successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
*/
static INT pwmSetCSR(const INT nTimerIdentity, const INT nValue)
{
typeCSR PWMCSR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nValue < CSR_MIN || nValue > CSR_MAX)
{
return pwmInvalidCSRValue;// CSR value error
}
PWMCSR.value = (UINT)inpw(REG_PWM_CSR);
switch (nTimerIdentity)
{
case PWM_TIMER0:
{
PWMCSR.field.csr0 = nValue;
break;
}
case PWM_TIMER1:
{
PWMCSR.field.csr1 = nValue;
break;
}
case PWM_TIMER2:
{
PWMCSR.field.csr2 = nValue;
break;
}
case PWM_TIMER3:
{
PWMCSR.field.csr3 = nValue;
break;
}
}
outpw(REG_PWM_CSR, PWMCSR.value);
return Successful;
}
/**
* @brief This function enable/disable PWM channel n dead zone function according to the
* parameter nTimerIdentity and nStatus
* @param[in] nTimerIdentity Timer channel number
* @param[in] nStatus PWMDZG_ENABLE/PWMDZG_DISABLE
* @retval Successful Set dead zone successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidDZGStatus PWM Dead-Zone Generator enable/disable status error
*/
static INT pwmSetDZGenerator(const INT nTimerIdentity, INT nStatus)
{
typePCR PWMPCR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nStatus != PWMDZG_ENABLE && nStatus != PWMDZG_DISABLE)
{
return pwmInvalidDZGStatus;// PCR inverter value error
}
PWMPCR.value = (UINT)inpw(REG_PWM_PCR);
switch (nTimerIdentity)
{
case PWM_TIMER0:
{
PWMPCR.field.grpup0_dzen = nStatus;
break;
}
case PWM_TIMER1:
{
PWMPCR.field.grpup0_dzen = nStatus;
break;
}
case PWM_TIMER2:
{
PWMPCR.field.grpup1_dzen = nStatus;
break;
}
case PWM_TIMER3:
{
PWMPCR.field.grpup1_dzen = nStatus;
break;
}
}
outpw(REG_PWM_PCR, PWMPCR.value);
return Successful;
}
/**
* @brief This function set PWM channel n enable/disable according to the
* parameter nTimerIdentity and nStatus
* @param[in] nTimerIdentity Timer channel number
* @param[in] nStatus PWM_ENABLE/PWMDISABLE
* @retval Successful Set channel enable/disable successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
*/
static INT pwmSetTimerState(const INT nTimerIdentity, INT nStatus)
{
typePCR PWMPCR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nStatus != PWM_ENABLE && nStatus != PWM_DISABLE)
{
return pwmInvalidTimerStatus;
}
PWMPCR.value = (UINT)inpw(REG_PWM_PCR);
switch (nTimerIdentity)
{
case PWM_TIMER0:
{
PWMPCR.field.ch0_en = nStatus;
break;
}
case PWM_TIMER1:
{
PWMPCR.field.ch1_en = nStatus;
break;
}
case PWM_TIMER2:
{
PWMPCR.field.ch2_en = nStatus;
break;
}
case PWM_TIMER3:
{
PWMPCR.field.ch3_en = nStatus;
break;
}
}
outpw(REG_PWM_PCR, PWMPCR.value);
return Successful;
}
/**
* @brief This function set PWM channel n inverter on/off according to the
* parameter nTimerIdentity and nStatus
* @param[in] nTimerIdentity Timer channel number
* @param[in] nStatus PWM_ENABLE/PWM_DISABLE
* @retval Successful Set inverter successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidInverterValue Inverter value error
*/
static INT pwmSetInverter(const INT nTimerIdentity, INT nStatus)
{
typePCR PWMPCR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nStatus != PWM_INVON && nStatus != PWM_INVOFF)
{
return pwmInvalidInverterValue;// PCR inverter value error
}
PWMPCR.value = (UINT)inpw(REG_PWM_PCR);
switch (nTimerIdentity)
{
case PWM_TIMER0:
{
PWMPCR.field.ch0_inverter = nStatus;
break;
}
case PWM_TIMER1:
{
PWMPCR.field.ch1_inverter = nStatus;
break;
}
case PWM_TIMER2:
{
PWMPCR.field.ch2_inverter = nStatus;
break;
}
case PWM_TIMER3:
{
PWMPCR.field.ch3_inverter = nStatus;
break;
}
}
outpw(REG_PWM_PCR, PWMPCR.value);
return Successful;
}
/**
* @brief This function set PWM channel n toggle/one shot mode according to the
* parameter nTimerIdentity and nStatus
* @param[in] nTimerIdentity Timer channel number
* @param[in] nStatus PWM_TOGGLE/PWM_ONESHOT
* @retval Successful Set operation mode successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidModeStatus Operating mode error
*/
static INT pwmSetMode(const INT nTimerIdentity, INT nStatus)
{
typePCR PWMPCR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nStatus != PWM_TOGGLE && nStatus != PWM_ONESHOT)
{
return pwmInvalidModeStatus;// PCR inverter value error
}
PWMPCR.value = (UINT)inpw(REG_PWM_PCR);
switch (nTimerIdentity)
{
case PWM_TIMER0:
{
PWMPCR.field.ch0_mode = nStatus;
break;
}
case PWM_TIMER1:
{
PWMPCR.field.ch1_mode = nStatus;
break;
}
case PWM_TIMER2:
{
PWMPCR.field.ch2_mode = nStatus;
break;
}
case PWM_TIMER3:
{
PWMPCR.field.ch3_mode = nStatus;
break;
}
}
outpw(REG_PWM_PCR, PWMPCR.value);
bPWMTimerMode[nTimerIdentity] = nStatus;
return Successful;
}
/**
* @brief This function set PWM_CNRn value according to the parameter nTimerIdentity and nValue
* @param[in] nTimerIdentity Timer channel number
* @param[in] nValue CNR value
* @retval Successful Set CNR successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidCNRValue Invalid CNR value
*/
static INT pwmSetCNR(const INT nTimerIdentity, INT nValue)
{
typeCNR PWMCNR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nValue < CNR_MIN || nValue > CNR_MAX)
{
return pwmInvalidCNRValue;// PCR inverter value error
}
PWMCNR.field.cnr = nValue;
outpw(REG_PWM_CNR0 + (PWM_OFFSET * nTimerIdentity), PWMCNR.value);
return Successful;
}
/**
* @brief This function set PWM_CMRn value according to the parameter nTimerIdentity and nValue
* @param[in] nTimerIdentity Timer channel number
* @param[in] nValue CMR value
* @retval Successful Set CMR successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval pwmInvalidCMRValue Invalid CMR value
*/
static INT pwmSetCMR(const INT nTimerIdentity, INT nValue)
{
typeCMR PWMCMR;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
if (nValue < CMR_MIN || nValue > CMR_MAX)
{
return pwmInvalidCMRValue;// CMR value error
}
PWMCMR.field.cmr = nValue;
outpw(REG_PWM_CMR0 + (PWM_OFFSET * nTimerIdentity), PWMCMR.value);
return Successful;
}
/**
* @brief This function return the PDR value of PWM timer n
* @param[in] nTimerIdentity Timer channel number
* @retval pwmInvalidTimerChannel PWM Timer channel number error
* @retval Others Current PDR value
*/
static UINT pwmGetPDR(const INT nTimerIdentity)
{
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
else
{
return (UINT)inpw(REG_PWM_PDR0 + (PWM_OFFSET * nTimerIdentity)); // Return PDR value
}
}
/**
* @brief This function set the PIERn bit of PWM timer n as 1 or 0 according to the
* parameter nTimerIdentity and nValue
* @param[in] nTimerIdentity Timer channel number
* @param[in] nValue PWM_ENABLE/PWM_DISABLE
* @retval Successful Set PIER successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
*/
static INT pwmSetPIER(const INT nTimerIdentity, INT nValue)
{
UINT uRegisterValue = 0;;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// Timer_num value error
}
else
{
uRegisterValue = (UINT)inpw(REG_PWM_PIER);
if (nValue == PWM_ENABLE)
{
uRegisterValue = uRegisterValue | (1 << nTimerIdentity); // Set PIER
}
else
{
uRegisterValue = uRegisterValue & (0 << nTimerIdentity); // Clear PIER
}
outpw(REG_PWM_PIER, uRegisterValue);// Write value to PIER
return Successful;
}
}
/**
* @brief This function clear PIIRn bit according to the parameter nTimerIdentity
* @param[in] nTimerIdentity Timer channel number
* @retval Successful Clear PIIR successfully
* @retval pwmInvalidTimerChannel PWM Timer channel number error
*/
static INT pwmCleanPIIR(const INT nTimerIdentity)
{
UINT uRegisterValue = 0;
if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
{
return pwmInvalidTimerChannel;// nTimerIdentity value error
}
uRegisterValue = (UINT)inpw(REG_PWM_PIIR);
uRegisterValue = uRegisterValue & ~(1 << nTimerIdentity);
outpw(REG_PWM_PIIR, uRegisterValue);
return Successful;
}
/// @endcond /* HIDDEN_SYMBOLS */
/*@}*/ /* end of group N9H30_PWM_EXPORTED_FUNCTIONS */
/*@}*/ /* end of group N9H30_PWM_Driver */
/*@}*/ /* end of group N9H30_Device_Driver */
/*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/
|