#include "dac_wav.h"

#define DAC_TIMER ((PIT32_TypeDef *)PIT2_BASE_ADDR)
u8 finishflag = 0;
u8 last_select=0;
u8 dac_data_buff_1[1024];
u8 dac_data_buff_2[1024];
u16 bufflen=0;
u16 buffnum;//数据总包数
u16 bufflast;//最后一包的数据量
u16 numflag=0;//播放到第几包数据

#if !WAV_FLAG
void DMA2_IRQHandler(void)
{
	if (m_dma_control->STATTFR & 0x0f) //dma done
    {
		finishflag=1;
		numflag++;
    }
    m_dma_control->CLRTFR = m_dma_control->STATTFR;
    m_dma_control->CLRBLOCK = m_dma_control->STATBLOCK;
    m_dma_control->CLRSRC = m_dma_control->STATSRC;
    m_dma_control->CLRDST = m_dma_control->STATDST;
    m_dma_control->CLRERR = m_dma_control->ERR;
	
	if(wbuf_select==0)//传下一包数据
		DMA_Tran(dac_data_buff_1,bufflen);
	else if(wbuf_select==1)
		DMA_Tran(dac_data_buff_2,bufflen);
	
	if(numflag==(buffnum-1))//最后一包数据
	{
		bufflen=bufflast;
		last_select=1;
	}
	else if(numflag==buffnum)//传输完成
	{
		last_select=2;
		wbuf_select=2;
	}
}
#endif

static void DAC_Timer_Init(unsigned int rate)
{
	DAC_TIMER->PCSR &= (~PIT_PCSR_EN);

	DAC_TIMER->PCSR &= (~PIT_PCSR_EN);
	DAC_TIMER->PCSR = (0<<8)|PIT_PCSR_OVW|PIT_PCSR_RLD;

	DAC_TIMER->PMR = (g_ips_clk/1000000*(1000000/(float)rate));
//	PIT32->PCSR = (PIT32_CLK_DIV_1<<8)|PCSR_OVW|PCSR_RLD;  
}

static void DAC_Timer_Enable(void)
{
    DAC_TIMER->PCSR |= PIT_PCSR_EN;
}

static void DAC_Timer_Disable(void)
{
    DAC_TIMER->PCSR &= ~PIT_PCSR_EN;
}

void DMA_DAC_Tran(UINT8 channel, UINT32 src,UINT32 length)
{
	//m_dma_control->DMA_CONFIG = 1;
	m_dma_channel[channel]->SRCADDR = src;
	m_dma_channel[channel]->DSTADDR = 0x40021004;
	m_dma_channel[channel]->CTRL = SIEC|DNOCHG|M2P_DMA|DWIDTH_HW|SWIDTH_HW|DMA_IE;
	m_dma_channel[channel]->CTRL_HIGH = length;
	
	m_dma_channel[channel]->CONFIG = 0;
	m_dma_channel[channel]->CONFIG_HIGH = DST_PER_DAC;
	
	m_dma_control->MASKTFR = CHANNEL_UMASK(channel);
	m_dma_control->CHEN = CHANNEL_WRITE_ENABLE(channel)|CHANNEL_ENABLE(channel);
}

void DMA_dis(UINT8 n)
{
	m_dma_channel[n]->CONFIG|=1<<8;
	m_dma_control->CHEN |=CHANNEL_WRITE_ENABLE(n);
	m_dma_control->CHEN &=~CHANNEL_ENABLE(n);
	m_dma_channel[n]->CONFIG&=~(1<<8);
	m_dma_control->CONFIG=~1;	
}

/*******************************************************************************
* Function Name  : DMA_REG_Init
* Description    : DMA寄存器初始化
* Input          : - dmac_base_addr: DMA基地址
*
* Output         : None
* Return         : None
******************************************************************************/
void DMA_REG_Init_my(UINT32 dmac_base_addr)
{
	m_dma_control    = (DMAC_TypeDef*)(dmac_base_addr+0x2c0);
	m_dma_channel[0] = (DMAC_ChannelTypeDef*)(dmac_base_addr);
	m_dma_channel[1] = (DMAC_ChannelTypeDef*)(dmac_base_addr+0x58);
	m_dma_channel[2] = (DMAC_ChannelTypeDef*)(dmac_base_addr+0xB0);
	m_dma_channel[3] = (DMAC_ChannelTypeDef*)(dmac_base_addr+0x108);

	//m_dma_control->DMA_MASKBLOCK = 0xf0f;
	//interrupt_setup(0x24, isr_dma);
	m_dma_control->CONFIG = 0x01;
}

void DMA_Tran(uint8_t *data, uint32_t len)
{
	uint32_t tran_l = 0;
    uint8_t *wDataBufOut = data;
	tran_l = (len >> 1);
	len -= (tran_l << 1);
	DMA_DAC_Tran(0, (uint32_t)wDataBufOut, tran_l);
}
void close_wav_dac(void)
{
	WAV_amplifier_DIS();
	DAC_Timer_Disable();
	DMA_dis(0);
}

DAC_InitTypeDef hdac;
UINT8 LT_PlayWav_DAC(void)
{
	if(gWavState == 1)
	{
		gWavState = 0;
		//读取第一、二包数据
//		LT768_SPI_Init(1,1);
		LT_ReadFlash(dac_data_buff_1,gWavFlashAddr+numflag*bufflen,bufflen);
		numflag++;//numflag=1
		LT_ReadFlash(dac_data_buff_2,gWavFlashAddr+numflag*bufflen,bufflen);
		
		//初始化DMA和DAC功能
		hdac.DataFomat = RIGHTALIGNED_12BITS;
		hdac.TriggerMode = TRIGGER_PIT;
		hdac.ExtTriggerMode = DET_ON_RISING;
		DRV_DAC_Init(hdac);

		DMA_REG_Init_my(DMAC2_BASE_ADDR);
		NVIC_Init(2,0,DMA2_IRQn,2);
		DAC_Timer_Init(22050);
		DAC_Timer_Enable();
		
		WAV_amplifier_EN();
		
		DMA_Tran(dac_data_buff_1,bufflen);
		finishflag=0;
		wbuf_select=1;
	}
	if(wbuf_select == 0 && finishflag==1 && last_select==0)
	{
		wbuf_select=1;
		finishflag=0;
		LT_ReadFlash(dac_data_buff_2,gWavFlashAddr+numflag*bufflen,bufflen);
	}//DMA2_IRQHandler
	else if(wbuf_select == 1 && finishflag==1 && last_select==0)
	{
		wbuf_select=0;
		finishflag=0;
		LT_ReadFlash(dac_data_buff_1,gWavFlashAddr+numflag*bufflen,bufflen);
	}
	else if(last_select == 1 && finishflag==1)
	{
		finishflag=0;
		if(wbuf_select)
		{
			wbuf_select=0;
			LT_ReadFlash(dac_data_buff_1,gWavFlashAddr+numflag*bufflen,bufflen);
		}
		else
		{
			wbuf_select=1;
			LT_ReadFlash(dac_data_buff_2,gWavFlashAddr+numflag*bufflen,bufflen);
		}
	}
	if(wbuf_select==2 && finishflag==1)
	{
		gWavFlag=0;
		close_wav_dac();
	}
	
	//return OK;
	return 1;
}


