00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00031
00032
00033
00034
00035 #ifndef EPWM_H
00036 #define EPWM_H
00037
00038
00039
00040
00041 #include "TIMotorLIB.h"
00042 #include GENERATE_FLEX_INC(Device.h)
00043 #include "SFO_V5.h"
00044 #include GENERATE_FLEX_INC(ePwm_defines.h)
00045 #include "IQConstants.h"
00046 #include "Utils.h"
00047
00048
00049
00050
00051
00052 #define EPWM1_INT_ACK (0x1);
00053 #define EPWM2_INT_ACK (0x2);
00054 #define EPWM3_INT_ACK (0x4);
00055 #define EPWM4_INT_ACK (0x8);
00056 #define EPWM5_INT_ACK (0x10);
00057 #define EPWM6_INT_ACK (0x20);
00058
00060 #define PWM_OUTPUTS_SIZE (12)
00061
00062
00063
00064
00065
00067 typedef enum {
00068 EPWMA,
00069 EPWMB
00070 } EPWMX;
00071
00073 typedef enum {
00074 MASTERPWM,
00075 SLAVEPWM
00076 } PHSEN;
00077
00079 typedef enum {
00080 SYNC_IN=0,
00081 SYNC_CTR_ZERO=1,
00082 SYNC_CTR_CMPB=2,
00083 SYNC_DISABLE=3
00084 } SYNCOSEL;
00085
00087 typedef enum {
00088 PWM1=0,
00089 PWM2=1,
00090 PWM3=2,
00091 PWM4=3,
00092 PWM5=4,
00093 PWM6=5
00094 } ePWMenum;
00095
00097 typedef enum {
00098 NORMAL_MODE,
00099 HRPWM_MODE
00100 } PWMMODE;
00101
00103 typedef enum {
00104 SAWTOOTH=TB_COUNT_UP,
00105 TRIANGLE=TB_COUNT_UPDOWN
00106 } PWMCARRIER;
00107
00109 typedef enum {
00110 SYMMETRICAL=CC_CTR_ZERO,
00111 ASYMMETRICAL=CC_CTR_ZERO_PRD
00112 } PWMMODULATION;
00113
00115 typedef enum {
00116 CTR_ZERO=ET_CTR_ZERO,
00117 CTR_PRD=ET_CTR_PRD
00118 } INTSEL;
00119
00121 typedef enum {
00122 SOCNULL=0,
00123 SOCA=1,
00124 SOCB=2
00125 } ePWMSOC;
00126
00127
00129 typedef enum {
00130 EvTBeqZERO=1,
00131 EvTBeqPRD=2,
00132 EvTBCNTeqCMPAI=4,
00133 EvTBCNTeqCMPAD=5,
00134 EvTBCNTeqCMPBI=6,
00135 EvTBCNTeqCMPBD=7
00136 } SOCTrigger;
00137
00139 typedef enum {
00140 xA=0,
00141 xB=6
00142 } sideEnum;
00143
00145 typedef enum {
00146 NEG_LOGIC=1,
00147 POS_LOGIC=2
00148 } SWITCHLOGIC;
00149
00150
00151
00152
00153
00154
00155
00157 struct PWM3phConfiguration {
00158 Uint16 t_pwm;
00159 Uint16 OFFSETPWM;
00160 _iq OFFSETPWMDIVVBUS;
00161 _iq vBus;
00162 EPWMX epwmX;
00163 volatile Uint16* cmp1;
00164 volatile Uint16* cmp2;
00165 volatile Uint16* cmp3;
00166 volatile Uint16* enable_input;
00167 Uint16 enabled;
00168 };
00169
00171 extern volatile struct PWM3phConfiguration pwmConf;
00172 extern volatile struct PWM3phConfiguration pwmAConf, pwmBConf;
00173
00175 extern volatile struct EPWM_REGS *ePWM[];
00177 extern volatile Uint16* cmpPtr[PWM_OUTPUTS_SIZE];
00178 extern int MEP_ScaleFactor[PWM_CH];
00179 extern int MEP_SF1, MEP_SF2, MEP_SF3, MEP_SF4, MEP_SF5, MEP_SF6;
00180
00181
00182
00183
00184
00185
00186 extern void BindPWMOutputs(void);
00187
00188
00190
00191 extern void InitPWMPeripheralClocks(void);
00192
00193
00195
00196 extern void InitPWMAPeripheralClocks(void);
00197
00198
00200
00201 extern void InitPWMBPeripheralClocks(void);
00202
00203
00216
00217 extern void InitEPWMGpio(ePWMenum PWM);
00218
00219
00232
00233 extern void InitTzGpio(void);
00234
00235
00245
00246 extern void InitPWMSide(ePWMenum PWM, sideEnum side, PWMMODULATION modulation,
00247 SWITCHLOGIC switchLogic);
00248
00249
00266
00267 extern void InitSinglePWMOutput(void(*interruptFCN)(void), ePWMenum PWM, sideEnum side,
00268 Uint16 tpwm_us, PHSEN phSen, SYNCOSEL syncMode, PWMMODE pwmMode, PWMCARRIER pwmCarrier,
00269 PWMMODULATION modulation, INTSEL intSel, SWITCHLOGIC switchLogic);
00270
00271
00287
00288 extern void InitComplementaryPWMOutput(void(*interruptFCN)(void), ePWMenum PWM, Uint16 tpwm_us,
00289 Uint16 db_us, PHSEN phSen, PWMMODE pwmMode, PWMCARRIER pwmCarrier, PWMMODULATION modulation,
00290 INTSEL intSel, SWITCHLOGIC switchLogic);
00291
00292
00293
00311
00312 extern void Init3phPWM(void(*interruptFCN)(void), EPWMX epwmX, Uint16 tpwm_us,
00313 Uint16 db_us, PWMCARRIER pwmCarrier, PWMMODULATION modulation, INTSEL intSel,
00314 _iq vBus, ePWMSOC soc, SOCTrigger trigger, SWITCHLOGIC switchLogic);
00315
00316
00328
00329 extern void ConfigurePWMLeg(volatile struct EPWM_REGS* pwmPtr, PHSEN master,
00330 Uint16 tpwm_us, Uint16 db_us, PWMCARRIER pwmCarrier, PWMMODULATION modulation,
00331 unsigned int polSel);
00332
00333
00344
00345 extern void AddEventTrigger(volatile struct EPWM_REGS* pwmPtr,
00346 ePWMSOC soc, SOCTrigger trigger);
00347
00348
00349 static inline void SetEnablePWMInput(Uint16* input);
00350 static inline void UpdateEPWM(ePWMenum PWM, sideEnum side, _iq cmp);
00351 static inline void UpdateEPWMDuty(ePWMenum PWM, sideEnum side, _iq duty);
00352 static inline void UpdateHRPWM(ePWMenum PWM, _iq cmp);
00354 static inline void UpdatePWM3ph(_iq vas,_iq vbs,_iq vcs, char saturate);
00355 static inline void UpdatePWMA3ph(_iq vas,_iq vbs,_iq vcs, char saturate);
00356 static inline void UpdatePWMB3ph(_iq vas,_iq vbs,_iq vcs, char saturate);
00358 static inline void UpdatePWMDuty3ph(_iq vad, _iq vbd, _iq vcd);
00359 static inline void UpdatePWMADuty3ph(_iq vad, _iq vbd, _iq vcd);
00360 static inline void UpdatePWMBDuty3ph(_iq vad, _iq vbd, _iq vcd);
00361 static inline void ForceSingleEPWM(ePWMenum PWM, sideEnum side, Uint16 level);
00362 static inline void _ForceSingleEPWM(volatile struct EPWM_REGS * pwmPtr, sideEnum side,
00363 Uint16 level);
00364 static inline void ForceEPWM(ePWMenum PWM, sideEnum side, Uint16 level);
00365 static inline void EnablePWM(Uint16 enable);
00366 static inline void EnableEPWMModuleTBCLK(void);
00367 static inline void EpwmPIEAck(volatile struct EPWM_REGS* epwmRegs);
00368 static inline void EpwmAPIEAck(void);
00369 static inline void EpwmBPIEAck(void);
00370
00371
00372
00373
00374
00375 static inline void SetEnablePWMInput(Uint16* input)
00376 {
00377 pwmConf.enable_input = input;
00378 }
00379
00380
00381
00382 static inline void UpdateEPWM(ePWMenum PWM, sideEnum side, _iq cmp)
00383 {
00384 *(cmpPtr[PWM+side]) = (Uint16)(_IQtoF(cmp));
00385 }
00386
00387
00388
00389 static inline void UpdateEPWMDuty(ePWMenum PWM, sideEnum side, _iq duty)
00390 {
00391 *(cmpPtr[PWM+side]) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(duty),_IQ16(ePWM[PWM]->TBPRD))));
00392 }
00393
00394
00395
00396 static inline void UpdateHRPWM(ePWMenum PWM, _iq cmp)
00397 {
00398 ePWM[PWM]->CMPA.all = (_IQint(cmp*ePWM[PWM]->TBPRD) << 16) +
00399 (((Uint32)(_IQfrac(cmp*ePWM[PWM]->TBPRD*MEP_ScaleFactor[0])) << 8) + 0x180);
00400 }
00401
00402
00403
00404
00406
00407 static inline void UpdatePWM3ph(_iq vas, _iq vbs, _iq vcs, char saturate)
00408 {
00409
00410
00411
00412
00414
00417 if(saturate){
00418 LIM(vas,(long)(pwmConf.vBus)>>1);
00419 LIM(vbs,(long)(pwmConf.vBus)>>1);
00420 LIM(vcs,(long)(pwmConf.vBus)>>1);
00421 }
00422 *(pwmConf.cmp1) = (Uint16)(pwmConf.OFFSETPWM + _IQtoF(_IQmpy(vas,pwmConf.OFFSETPWMDIVVBUS)));
00423 *(pwmConf.cmp2) = (Uint16)(pwmConf.OFFSETPWM + _IQtoF(_IQmpy(vbs,pwmConf.OFFSETPWMDIVVBUS)));
00424 *(pwmConf.cmp3) = (Uint16)(pwmConf.OFFSETPWM + _IQtoF(_IQmpy(vcs,pwmConf.OFFSETPWMDIVVBUS)));
00429
00430
00431 }
00432
00433
00434
00435
00441
00442 static inline void UpdatePWMA3ph(_iq vas, _iq vbs, _iq vcs, char saturate)
00443 {
00445 if(saturate){
00446 LIM(vas,(long)(pwmAConf.vBus)>>1);
00447 LIM(vbs,(long)(pwmAConf.vBus)>>1);
00448 LIM(vcs,(long)(pwmAConf.vBus)>>1);
00449 }
00450 *(pwmAConf.cmp1) = (Uint16)(pwmAConf.OFFSETPWM + _IQtoF(_IQmpy(vas,pwmAConf.OFFSETPWMDIVVBUS)));
00451 *(pwmAConf.cmp2) = (Uint16)(pwmAConf.OFFSETPWM + _IQtoF(_IQmpy(vbs,pwmAConf.OFFSETPWMDIVVBUS)));
00452 *(pwmAConf.cmp3) = (Uint16)(pwmAConf.OFFSETPWM + _IQtoF(_IQmpy(vcs,pwmAConf.OFFSETPWMDIVVBUS)));
00456 }
00457
00458
00459
00460
00466
00467 static inline void UpdatePWMB3ph(_iq vas, _iq vbs, _iq vcs, char saturate)
00468 {
00470 if(saturate){
00471 LIM(vas,(long)(pwmBConf.vBus)>>1);
00472 LIM(vbs,(long)(pwmBConf.vBus)>>1);
00473 LIM(vcs,(long)(pwmBConf.vBus)>>1);
00474 }
00475 *(pwmBConf.cmp1) = (Uint16)(pwmBConf.OFFSETPWM + _IQtoF(_IQmpy(vas,pwmBConf.OFFSETPWMDIVVBUS)));
00476 *(pwmBConf.cmp2) = (Uint16)(pwmBConf.OFFSETPWM + _IQtoF(_IQmpy(vbs,pwmBConf.OFFSETPWMDIVVBUS)));
00477 *(pwmBConf.cmp3) = (Uint16)(pwmBConf.OFFSETPWM + _IQtoF(_IQmpy(vcs,pwmBConf.OFFSETPWMDIVVBUS)));
00481 }
00482
00483
00484
00485
00487
00488 static inline void UpdatePWMDuty3ph(_iq vad, _iq vbd, _iq vcd)
00489 {
00490
00491
00492
00493
00494
00495
00496
00497
00498 *(pwmConf.cmp1) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(vad),(_IQ16(pwmConf.OFFSETPWM << 1)))));
00499 *(pwmConf.cmp2) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(vbd),(_IQ16(pwmConf.OFFSETPWM << 1)))));
00500 *(pwmConf.cmp3) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(vcd),(_IQ16(pwmConf.OFFSETPWM << 1)))));
00501 }
00502
00503
00504
00505
00512
00513 static inline void UpdatePWMADuty3ph(_iq vad, _iq vbd, _iq vcd)
00514 {
00522 *(pwmAConf.cmp1) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(vad),(_IQ16(pwmAConf.OFFSETPWM << 1)))));
00523 *(pwmAConf.cmp2) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(vbd),(_IQ16(pwmAConf.OFFSETPWM << 1)))));
00524 *(pwmAConf.cmp3) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(vcd),(_IQ16(pwmAConf.OFFSETPWM << 1)))));
00525 }
00526
00527
00528
00529
00536
00537 static inline void UpdatePWMBDuty3ph(_iq vad, _iq vbd, _iq vcd)
00538 {
00539
00540
00541
00542
00543
00544
00545
00546
00547 *(pwmBConf.cmp1) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(vad),(_IQ16(pwmBConf.OFFSETPWM << 1)))));
00548 *(pwmBConf.cmp2) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(vbd),(_IQ16(pwmBConf.OFFSETPWM << 1)))));
00549 *(pwmBConf.cmp3) = (Uint16)(_IQ16toF(_IQ16mpy(_IQtoIQ16(vcd),(_IQ16(pwmBConf.OFFSETPWM << 1)))));
00550 }
00551
00552
00553
00554
00556
00557 static inline void ForceSingleEPWM(ePWMenum PWM, sideEnum side, Uint16 level)
00558 {
00559 _ForceSingleEPWM(ePWM[PWM], side, level);
00560 }
00561
00562
00563
00564
00569
00570 static inline void _ForceSingleEPWM(volatile struct EPWM_REGS * pwmPtr,
00571 sideEnum side, Uint16 level)
00572 {
00573 pwmPtr->AQSFRC.bit.RLDCSF = 3;
00574 if(side == xA){
00575 pwmPtr->AQSFRC.bit.ACTSFA = level+1;
00576 pwmPtr->AQSFRC.bit.OTSFA=1;
00577 }
00578 else{
00579 pwmPtr->AQSFRC.bit.ACTSFB = level+1;
00580 pwmPtr->AQSFRC.bit.OTSFB=1;
00581 }
00582 }
00583
00584
00585
00586
00591
00592 static inline void ForceEPWM(ePWMenum PWM, sideEnum side, Uint16 level)
00593 {
00594 if(side == xA)
00595 ePWM[PWM]->AQCSFRC.bit.CSFA = level+1;
00596 else
00597 ePWM[PWM]->AQCSFRC.bit.CSFB = level+1;
00598
00599 }
00600
00601
00602
00603
00605
00606 static inline void EnablePWM(Uint16 enable)
00607 {
00608
00609 pwmConf.enabled = enable;
00610 }
00611
00612
00613
00614
00616
00617 static inline void EnableEPWMModuleTBCLK(void)
00618 {
00619
00620 EALLOW;
00621 SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
00622 EDIS;
00623 }
00624
00625
00626
00627
00631
00632 static inline void EpwmPIEAck(volatile struct EPWM_REGS* epwmRegs)
00633 {
00634
00635 PieCtrlRegs.PIEACK.all |= PIEACK_GROUP3;
00636
00637 epwmRegs->ETCLR.bit.INT = 1;
00638 }
00639
00640
00641
00642
00645
00646 static inline void EpwmAPIEAck(void)
00647 {
00648 EpwmPIEAck(&EPwm1Regs);
00649 }
00650
00651
00652
00653
00656
00657 static inline void EpwmBPIEAck(void)
00658 {
00659 EpwmPIEAck(&EPwm4Regs);
00660 }
00661
00662
00663
00664 #endif
00665
00666
00667
00668