#include "wav_dac.h"
#include "pit32_drv.h"
#include "dac_drv.h"
#include "dmac_drv.h"
#include "module_select.h"
#include "bsp.h"
#include "data.h"
#include "trans_port.h"
#include "sys.h" 


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


//DAC_DMA2[1]
static void WAV_DMA_DAC_Tran(UINT8 channel, UINT32 src,UINT32 length)
{	
	WAVdac_dma_control->DMA_CONFIG = 1;
	WAVdac_dma_channel[channel]->DMA_SADDR = (UINT32)&src;
	WAVdac_dma_channel[channel]->DMA_DADDR = (UINT32)&DAC->DAC_DR;//0x40021004;
	WAVdac_dma_channel[channel]->DMA_CTRL = SIEC|DNOCHG|M2P_DMA|DWIDTH_HW|SWIDTH_HW|INTEN;
	WAVdac_dma_channel[channel]->DMA_CTRL_HIGH = length;
	
	WAVdac_dma_channel[channel]->DMA_CFG = (HS_SEL_SRC_SOFT)|(HS_SEL_DST_HARD)|(CH_PRIOR3);
	WAVdac_dma_channel[channel]->DMA_CFG_HIGH = DST_PER_DAC;
	
	WAVdac_dma_control->DMA_MASKTFR |= CHANNEL_UMASK(channel);
	WAVdac_dma_control->DMA_CHEN |= CHANNEL_WRITE_ENABLE(channel)|CHANNEL_ENABLE(channel);
}

static void WAV_DMA_dis(UINT8 n)
{	
//	m_dma2_control->DMA_CONFIG =~1;	           //wav 和 picture 同一DMA,不禁用 

	WAVdac_dma_channel[n]->DMA_CFG |= (CH_SUSP);
	WAVdac_dma_control->DMA_CHEN |= CHANNEL_WRITE_ENABLE(n);
	WAVdac_dma_control->DMA_CHEN &=~CHANNEL_ENABLE(n);
	
	WAVdac_dma_channel[n]->DMA_CFG &=~(CH_SUSP);
	
//	m_dma2_control->DMA_CONFIG = 1;	
}

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

	DAC_TIMER->PCSR = (0<<8)|PCSR_OVW|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 |= PCSR_EN;
}

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

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);
		WAV_DMA_DAC_Tran(WAVdac_CHNUM, (uint32_t)wDataBufOut, tran_l);

}

void close_wav_dac(void)
{
	WAV_amplifier_DIS();
	DAC_Timer_Disable();
	
//	DMA_dis(0);
		WAV_DMA_dis(WAVdac_CHNUM);

}


#if !WAV_FLAG
void DMA2_IRQHandler(void)      
{
//	printf("DMA_STATTFR   =0x%x  \r\n",m_dma2_control->DMA_STATTFR);

	if ((m_dma2_control->DMA_STATTFR & 0x02)) //dma done   
    {
		finishflag=1;
		numflag++;
    }
	
	 m_dma2_control->DMA_CLRTFR   = m_dma2_control->DMA_STATTFR ;
//    m_dma2_control->DMA_CLRBLOCK = m_dma2_control->DMA_STATBLOCK;
//    m_dma2_control->DMA_CLRSRC = m_dma2_control->DMA_STATSRC;
//    m_dma2_control->DMA_CLRDST = m_dma2_control->DMA_STATDST;
//    m_dma2_control->DMA_CLRERR = m_dma2_control->DMA_STATERR;

	if( wbuf_select==0 )//传下一包数据
	{

		DMA_Tran(dac_data_buff_1,bufflen);

		//printf("Pb1\r\n");
	}
	else if(wbuf_select==1)
	{
		DMA_Tran(dac_data_buff_2,bufflen);
		//printf("Pb2\r\n");
	}

	
	if(numflag==(buffnum-1))//最后一包数据
	{
		bufflen=bufflast;
		last_select=1;
	}
	else if(numflag==buffnum)//传输完成
	{
		last_select=2;
		wbuf_select=2;
	}
	
	
}
#endif

UINT8 LT_PlayWav_DAC(void)
{
	if(gWavState == 1)
	{
		gWavState = 0;
		//读取第一、二包数据
		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功能
		DAC_Init(RIGHTALIGNED_12BITS, TRIGGER_PIT, DET_ON_RISING);       //PIT2
		//DAC_Init(LEFTALIGNED_12BITS, TRIGGER_PIT, DET_ON_RISING);
		//DMA_REG_Init(DMA2_BASE_ADDR);
		NVIC_Init(2,0,DMA2_IRQn,2);
		DAC_Timer_Init(22050);   //  11025  22050
		DAC_Timer_Enable();
		
		WAV_amplifier_EN();
		
		//printf("Wav S:\r\n");
		
		DMA_Tran(dac_data_buff_1,bufflen);
		finishflag=0;
		wbuf_select=1;
		
	}
	if(wbuf_select == 0 && finishflag==1 && last_select==0)
	{		
		//printf("rb2\r\n");
		LT_ReadFlash(dac_data_buff_2,gWavFlashAddr+numflag*bufflen,bufflen);
		wbuf_select=1;
		finishflag=0;	

	}//DMA2_IRQHandler
	else if(wbuf_select == 1 && finishflag==1 && last_select==0)
	{
		//printf("rb1\r\n");
		LT_ReadFlash(dac_data_buff_1,gWavFlashAddr+numflag*bufflen,bufflen);
		wbuf_select=0;
		finishflag=0;
	}
	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;
}


