#define GLOBALS_API_TPM
#include "../App_Headers/includes.h"


/*-------------------------------------------------------------------------
* Function Name  : Api_Timer0_Init
* Description    : 定时器0初始化
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_Timer0_Init(INT8U TPM_DIV)
{
    
    TIM0_TSCR1_TSWAI = Enable;   //timer 0 在MCU等待模式下不能运行
    TIM0_TSCR1_TSWAI = WaitRun;
    TIM0_TSCR1_TSFRZ = Enable;   //timer 0 计数器在冻结模式下禁止运行
    TIM0_TSCR1_TFFCA = Disable;  //正常清除标志位
    TIM0_TSCR1_PRNT  = Disable;  // 
    TIM0_TSCR2_PR    = TPM_DIV;  //Bus Clock / 4
    TIM0_TSCR2_TCRE  = Disable;
    TIM0_TTOV        = Disable;

    TIM0_TCTL1 = Disable;        //禁止输出比较功能
    TIM0_TCTL2 = Disable;
    TIM0_TCTL3 = Disable;        //禁止输出比较功能
    TIM0_TCTL4 = Disable; 
        
    TIM0_TFLG2_TOF   = Enable;
    TIM0_TSCR1_TEN   = Enable;   //使能timer 0 正常功能
    TIM0_TSCR2_TOI   = Enable;   //溢出中断使能
    
}
/*-------------------------------------------------------------------------
* Function Name  : Api_Timer1_Init
* Description    : 定时器1初始化
* Input          : 
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Api_Timer1_Init(INT8U TPM_DIV)
{

    TIM1_TSCR1_TSWAI = Enable;   //timer 0 在MCU等待模式下不能运行
    TIM1_TSCR1_TSFRZ = Enable;   //timer 0 计数器在冻结模式下禁止运行
    TIM1_TSCR1_TFFCA = Disable;  //正常清除标志位
    TIM1_TSCR1_PRNT  = Disable;  // 
    TIM1_TSCR2_PR    = TPM_DIV; //Bus Clock / 4
    TIM1_TSCR2_TCRE  = Disable;
    TIM1_TTOV        = Disable;

    TIM1_TCTL1 = Disable;        //禁止输出比较功能
    TIM1_TCTL2 = Disable;
    TIM1_TCTL3 = Disable;        //禁止输出比较功能
    TIM1_TCTL4 = Disable; 
    
    TIM1_TFLG2_TOF   = Enable;
    TIM1_TSCR1_TEN   = Enable;   //使能timer 0 正常功能
    TIM1_TSCR2_TOI   = Enable;   //溢出中断使能
    

}

//-----------------------------------------------------------------------------
// 函数名称   :    Api_Motor_Init
// 函数功能   :    初始化电机驱动
// 输入       :    
//                 *Motor    ->   电机结构指针
// 输出       :    无
// 返回值     :    运行状态
// 其他       :    无
//-----------------------------------------------------------------------------
void  Api_Motor_Init(void)
{
    INT16U         *w;
    INT8U          Nub;

    MCCTL0_MCPRE        = 0;            //时钟为总线的8分频
    MCCTL0_MCSWAI       = Enable;          //停止模式停止工作 
    MCCTL0_FAST         = Disable;         //11bit分辨率
    MCCTL0_DITH         = Disable;         //PER-> P10-P0   0:包含P0
    MCCTL0_MCTOIF       = ClearFlag;       //清零中断标志
    MCCTL1_RECIRC       = True;            //低续流
    MCCTL1_MCTOIE       = Enable;          //使能中断
    MCPER               = 1454;       //PWM周期   中断频率 22000
    //
    MCCC0_MCOM          = clear;            //双全H桥模式
    MCCC0_MCAM          = clear;         //左对齐
    MCCC0_CD            = clear;         //禁止延时
    MCCC1_MCOM          = clear;            //全H桥模式
    MCCC1_MCAM          = clear;         //左对齐
    MCCC1_CD            = clear;         //禁止延时
    //
    MCCC2_MCOM          = clear;            //双全H桥模式
    MCCC2_MCAM          = clear;         //左对齐
    MCCC2_CD            = clear;         //禁止延时
    MCCC3_MCOM          = clear;            //全H桥模式
    MCCC3_MCAM          = clear;         //左对齐
    MCCC3_CD            = clear;         //禁止延时

    
    MCCC4_MCOM          = clear;         
    MCCC4_MCAM          = clear;         
    MCCC4_CD            = clear;         
    MCCC5_MCOM          = clear;         
    MCCC5_MCAM          = clear;       
    MCCC5_CD            = clear;       
    
    
    MCCC6_MCOM          = clear;            //双全H桥模式
    MCCC6_MCAM          = clear;         //左对齐
    MCCC6_CD            = clear;         //禁止延时
    MCCC7_MCOM          = clear;            //全H桥模式
    MCCC7_MCAM          = clear;         //左对齐
    MCCC7_CD            = clear;         //禁止延时
    
    w = &MCDC0;                            //初始化MCOC 0-7
         
    for(Nub = 0;Nub < 8 ;Nub++)
    {
         *(w + Nub) = 0;         
    }
}
//-----------------------------------------------------------------------------
// 函数名称   :    Api_Motor_isr
// 函数功能   :    PWM溢出中断
// 输入       :    无
// 输出       :    无
// 返回值     :    无
// 其他       :    无
//-----------------------------------------------------------------------------
interrupt void Api_Motor_isr(void)
{


    MCCTL0_MCTOIF       = ClearFlag;       //清零中断标志
}
/*-------------------------------------------------------------------------
* Function Name  : Api_Timer0_isr
* Description    : 定时器中断
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
interrupt void Api_Timer0_isr(void)
{
    if((CANRFLG_RSTAT == 0x03)||(CANRFLG_TSTAT == 0x03))
    {
        if(BusOffTime == null) 
        {                       
            BusOffTime = TT_200ms;
            CANMISC_BOHOLD = ClearFlag;                                                 
        }  
        if(BusOffTime > null)
        {
             BusOffTime--;                 
        }         
    }    
    TIM0_TFLG2_TOF = ClearFlag;  //清除中断标志位
}
/*-------------------------------------------------------------------------
* Function Name  : API_Timer1_isr
* Description    : 定时器中断
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
interrupt void API_Timer1_isr(void)
{
  
    TIM1_TFLG2_TOF = ClearFlag;  //清除中断标志位
}
