/* ==================================================================================
File name:        F281XPWM.H                     
                    
Originator:	Digital Control Systems Group
			Texas Instruments
Description:  
Header file containing data type and object definitions and 
initializers. Also contains prototypes for the functions in F281XPWM.C.

Target: TMS320F281x family

=====================================================================================
 History:
-------------------------------------------------------------------------------------
 04-15-2005	Version 3.20: Using DSP281x v. 1.00 or higher 
---------------------------------------------------------------------------------- */

#ifndef __F281X_PWM_H__
#define __F281X_PWM_H__

#include <f281xbmsk.h>
//#include "DSP281x_Device.h"
/*----------------------------------------------------------------------------
Initialization constant for the F281X Timer TxCON for PWM Generation. 
Sets up the timer to run free upon emulation suspend, continuous up-down mode
prescaler 1, timer enabled.
----------------------------------------------------------------------------*/  //FREE_RUN_FLAG
#define PWM_INIT_STATE  (FREE_RUN_FLAG + \
						TIMER_CONT_UPDN + \
                        TIMER_CLK_PRESCALE_X_1 + \
                        TIMER_ENABLE_BY_OWN    + \
                        TIMER_ENABLE)

#define PWM_INIT_STATE_DOUBLE_UPADTE  (FREE_RUN_FLAG + \
						TIMER_COMPARE_LD_ON_ZERO_OR_PRD + \
                        TIMER_CONT_UPDN + \
                        TIMER_CLK_PRESCALE_X_1 + \
                        TIMER_ENABLE_BY_OWN    + \
                        TIMER_ENABLE)

/*----------------------------------------------------------------------------
Initialization constant for the F281X ACTRx register for PWM Generation. 
Sets up PWM polarities.
----------------------------------------------------------------------------*/
#define ACTR_INIT_STATE ( COMPARE1_FH + \
                          COMPARE2_FH + \
                          COMPARE3_FH + \
                          COMPARE4_FH + \
                          COMPARE5_FH + \
                          COMPARE6_FH )

#define ACTR_ON_STATE ( COMPARE1_AL + \
                          COMPARE2_AH + \
                          COMPARE3_AL + \
                          COMPARE4_AH + \
                          COMPARE5_AL + \
                          COMPARE6_AH )

/*----------------------------------------------------------------------------
Initialization constant for the F281X DBTCONx register for PWM Generation. 
Sets up the dead band for PWM and sets up dead band values.
----------------------------------------------------------------------------*/
#define DBTCON_INIT_STATE ( DBT_VAL_10 +  \
                            EDBT3_EN   +  \
                            EDBT2_EN   +  \
                            EDBT1_EN   +  \
                            DBTPS_X32 )


/*-----------------------------------------------------------------------------
Define the structure of the PWM Driver Object 
-----------------------------------------------------------------------------*/
typedef struct {   
        Uint16 PeriodMax;     // Parameter: PWM Half-Period in CPU clock cycles (Q0)
        int16 MfuncPeriod;    // Input: Period scaler (Q15) 
        int16 MfuncC1;        // Input: PWM 1&2 Duty cycle ratio (Q15)
        int16 MfuncC2;        // Input: PWM 3&4 Duty cycle ratio (Q15) 
        int16 MfuncC3;        // Input: PWM 5&6 Duty cycle ratio (Q15)
        void (*init)();       // Pointer to the init function 
        void (*update)();     // Pointer to the update function 
        } PWMGEN ;    


typedef struct {   
        Uint16 PeriodMax;     // Parameter: PWM Half-Period in CPU clock cycles (Q0)
        Uint16 PeriodMin;     // Parameter: PWM Half-Period in CPU clock cycles (Q0)
        int16 MfuncPeriod;    // Input: Period scaler (Q15) 
        int16 MfuncC1;        // Input: PWM 1&2 Duty cycle ratio (Q15)
        int16 MfuncC2;        // Input: PWM 3&4 Duty cycle ratio (Q15) 
        int16 MfuncC3;        // Input: PWM 5&6 Duty cycle ratio (Q15)
        int16 MfuncC4;        // Input: PWM 1&2 Duty cycle ratio (Q15)
        int16 MfuncC5;        // Input: PWM 3&4 Duty cycle ratio (Q15) 
        int16 MfuncC6;        // Input: PWM 5&6 Duty cycle ratio (Q15)
        Uint16 ShiftPhaseA;     // Parameter: PWM Half-Period in CPU clock cycles (Q0)
        Uint16 ShiftPhaseB;     // Parameter: PWM Half-Period in CPU clock cycles (Q0)
        void (*init)();       // Pointer to the init function 
        void (*update)();     // Pointer to the update function 
        } PWMGEND ;    

/*-----------------------------------------------------------------------------
Define a PWMGEN_handle
-----------------------------------------------------------------------------*/
typedef PWMGEN *PWMGEN_handle;
typedef PWMGEND *PWMGEND_handle;

/*------------------------------------------------------------------------------
Default Initializers for the F281X PWMGEN Object 
------------------------------------------------------------------------------*/
#define F281X_EV1_FC_PWM_GEN {1000,   \
                              0x7FFF, \
                              0x4000, \
                              0x4000, \
                              0x4000, \
                             (void (*)(Uint32))F281X_EV1_PWM_Init,  \
                             (void (*)(Uint32))F281X_EV1_PWM_Update \
                             }

#define F281X_EV2_FC_PWM_GEN {1000,   \
                              0x7FFF, \
                              0x4000, \
                              0x4000, \
                              0x4000, \
                             (void (*)(Uint32))F281X_EV2_PWM_Init,  \
                             (void (*)(Uint32))F281X_EV2_PWM_Update \
                             }


#define F281X_EVD_FC_PWM_GEN {1000,   \
							  0,   \
                              0x7FFF, \
                              0x4000, \
                              0x4000, \
                              0x4000, \
                              0x4000, \
                              0x4000, \
                              0x4000, \
                              0x000, \
                              0x000, \
                             (void (*)(Uint32))F281X_EVD_PWM_Init,  \
                             (void (*)(Uint32))F281X_EVD_PWM_Update \
                             }

#define PWMGEN1_DEFAULTS 	F281X_EV1_FC_PWM_GEN
#define PWMGEN2_DEFAULTS 	F281X_EV2_FC_PWM_GEN
#define PWMGEND_DEFAULTS 	F281X_EVD_FC_PWM_GEN

/*------------------------------------------------------------------------------
 Prototypes for the functions in F281XPWM.C
------------------------------------------------------------------------------*/
void F281X_EV1_PWM_Init(PWMGEN_handle);
void F281X_EV1_PWM_Update(PWMGEN_handle);
void F281X_EV2_PWM_Init(PWMGEN_handle);
void F281X_EV2_PWM_Update(PWMGEN_handle);

void F281X_EVD_PWM_Init(PWMGEND_handle);
void F281X_EVD_PWM_Update(PWMGEND_handle);

int16 set_predel_dshim_max(int16 dshim,int16 dmin,int16 dpwm);
int16 set_predel_dshim(int16 dshim,int16 dmin,int16 dpwm);


#endif  // __F281X_PWM_H__

