//===========================================================================
//文件名称：pwm.c
//功能概要：pwm底层驱动构件源文件
//版权所有：苏州大学飞思卡尔嵌入式中心(sumcu.suda.edu.cn)
//版    本：V3.0(WYH),20140909
//===========================================================================
#include "pwm.h"

//定时器模块0,1,2地址映射
static const TPM_MemMapPtr TPM_ARR[]={TPM0_BASE_PTR,TPM1_BASE_PTR,TPM2_BASE_PTR};

//===========================================================================
//函数名称：pwm_init                                                        
//功能概要：pwm模块初始化。                                                
//参数说明：tpmModule：模块号：TPM_0、TPM_1、TPM_2
//          channel：通道号:TPMCH0-TPMCH5
//          period：脉冲周期，单位微秒（例如1000为1ms）   
//          duty：占空比，取值：1-100                                                                   
//函数返回：无
//===========================================================================
void pwm_init(uint_8 tpmModule,uint_8 channel,uint_32 period,uint_8 duty)
{
    //参数检查
    if(tpmModule > TPM_2)
    {
        tpmModule = TPM_2;
    }
    switch(tpmModule)
    {
    case 0:
    	if(channel > TPMCH5)
    	    channel = TPMCH5;
		switch(channel)
		{
		case 0:
			PORTC_PCR1 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x4);
			break;
		case 1:
            #if (TPM0_CH1_PIN == 1)
			PORTC_PCR2 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x4);
            #endif

            #if (TPM0_CH1_PIN == 2)
			PORTA_PCR4 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3);
            #endif

			break;
		case 5:
			PORTD_PCR5 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x4);
			break;
		default:
			break;
		}
    	break;
    case 1:
    	if(channel > TPMCH1)
			channel = TPMCH1;
		switch(channel)
		{
		case 0:
			PORTB_PCR0 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3)| PORT_PCR_DSE_MASK;
			break;
		case 1:
			PORTB_PCR1 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3)| PORT_PCR_DSE_MASK;
			break;
		default:
			break;
		}
    	break;
    case 2:
    	if(channel > TPMCH1)
			channel = TPMCH1;
		switch(channel)
		{
		case 0:
            #if (TPM2_CH0_PIN == 1)
			PORTB_PCR2 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3);
            #endif

            #if (TPM2_CH0_PIN == 2)
			PORTA_PCR1 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3);
            #endif

			break;
		case 1:
			PORTA_PCR2 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3);
			break;
		default:
			break;
		}
    	break;
    default:
    	break;
    }

    //开启时钟默认选择用PLL/2时钟源48MHz
    SIM_SOPT2 |= SIM_SOPT2_TPMSRC(1);
    SIM_SOPT2 |= SIM_SOPT2_PLLFLLSEL_MASK;
    //开启SIM时钟门
    BSET(SIM_SCGC6_TPM0_SHIFT + tpmModule,SIM_SCGC6);
    TPM_ARR[tpmModule]->CNT = 0;
    //计算周期和占空比
    //与TPM的计算方法一致:PLL/2时钟24000Khz/1000*0.1(ms), 0.1ms中断一次
    period = 24000 * period / 1000;
    TPM_ARR[tpmModule]->MOD = period;
    TPM_ARR[tpmModule]->SC = TPM_SC_TOIE_MASK|TPM_SC_CMOD(1);
    //设置TPM为边沿对齐pwm
    TPM_CnSC_REG(TPM_ARR[tpmModule],channel) = TPM_CnSC_MSB_MASK|TPM_CnSC_ELSA_MASK;
    TPM_CnV_REG(TPM_ARR[tpmModule],channel) = period * duty / 100;

    //enable_irq(TPM0_IRQ_NO + tpmModule);
}

//=========================================================================
//函数名称：pwm_stop
//功能概要：关闭PWM模块
//参数说明：tpmModule(模块号)：TPM_0,TPM_1,TPM_2
//函数返回：无
//=========================================================================
void pwm_stop(uint_8 tpmModule)
{
	if(tpmModule<=TPM_2)
	    BCLR(SIM_SCGC6_TPM0_SHIFT + tpmModule,SIM_SCGC6);
}

//=========================================================================
//函数名称：pwm_enable_int
//功能概要：使能PWM中断
//参数说明：tpmModule(模块号)：TPM_0,TPM_1,TPM_2
//函数返回：无
//=========================================================================
void pwm_enable_int(uint_8 tpmModule)
{
	if(tpmModule<=TPM_2)
		enable_irq(TPM0_IRQ_NO + tpmModule);
}

//=========================================================================
//函数名称：pwm_disable_int
//功能概要：禁止PWM中断
//参数说明：tpmModule(模块号)：TPM_0,TPM_1,TPM_2
//函数返回：无
//=========================================================================
void pwm_disable_int(uint_8 tpmModule)
{
	if(tpmModule<=TPM_2)
		disable_irq(TPM0_IRQ_NO + tpmModule);
}
