#include "dac.h"

#include "pit.h"
#include "data.h"
#include "bsp.h"
#include "pwm.h"
#include "pit32_drv.h"
#include "dac_drv.h"


UINT16 etARRValue(UINT16 sample)
{
	UINT16 arrValue;
	/* 更新OCA值以符合.WAV文件采样率 */
	switch (sample)
	{
	case 8000 :
	    arrValue = (uint16_t)(125000000/8000);
	    break; /* 8KHz = 125MHz / 9375 */
	case 11025:
	    arrValue = (uint16_t)(125000000/11025);
	    break; /* 11.025KHz = 125MHz / 6802 */
	case 16000:
	     arrValue = (uint16_t)(125000000/16000);
	    break; /* 16KHz = 125MHz / 4687 */
	case 22050:
	    arrValue = (uint16_t)(125000000/22050);
	    break; /* 22.05KHz = 125MHz / 3401 */
	case 44100:
	    arrValue = (uint16_t)(125000000/44100);
	    break; /* 44.1KHz = 125MHz / 1700 */
	case 48000:
	    arrValue = (uint16_t)(125000000/48000);
	    break; /* 48KHz = 125MHz / 1562 */
	default:
	    arrValue = 0;
	    break;
	}
	return arrValue - 1;
}

void DAC_Write_Data(UINT16 data)
{
	DAC->DAC_DR = data;
}

void DAC_Wait_Load_Done(void)
{
	while(0 == (DAC->DAC_FSR & DAC_LD_DONE));
}

void DAC_SetData(UINT16 data)
{
	DAC_Write_Data(data);
	DAC->DAC_SWTR |= DAC_SW_TRIG;
	DAC_Wait_Load_Done();
}

void WAV_amplifier_EN(void)
{
	EPORT->EPDDR |= (1<<3);
	EPORT->EPDR |= (1<<3);	
}

void WAV_amplifier_DIS(void)
{	
	EPORT->EPDDR |= (1<<3);
	EPORT->EPDR  &= ~(1<<3);
}

void close_wav(void)
{
	bufPos = 0;
	gWavAddrFull_Flag = 0;
	gWavFinish_Flag = 0;
	PIT2->PCSR &=~PIT_PCSR_EN;
	gWavState = 0;

	gWavFlag = 0;
	
	WAV_amplifier_DIS();
}

UINT8 LT_PlayWav(void)
{
	if(gWavState == 1)
	{
//		printf("start\r\n");
		gWavState = 0;
		gTempWavFlashAddr = gWavFlashAddr;
//		LT768_SPI_Init(1,1);
		
		LT_ReadFlash(wavBuf0,gTempWavFlashAddr,2048);																//读取数据到 Buf0									//2019
		gTempWavFlashAddr += 2048;
		wbuf0_Stat = 1;																																//Buf0 数据准备好

		LT_ReadFlash(wavBuf1,gTempWavFlashAddr,2048);																//读取数据到 Buf1									//2019
		gTempWavFlashAddr += 2048;
		wbuf1_Stat = 1;																																//Buf1 数据准备好
		wbuf_select = 1;																															//选择 Buf0

		WAV_amplifier_EN();		//每次播放前使能功放IC
		gWavOpen = 1;			//因为IC使能后需要一段时间才能正常工作，这里若加延时会造成其他动效有停顿一下的情况，所以延时的工作交给定时器，这里负责打开这个延时的标志位
	}
	
	if(wbuf0_Stat == 0)
	{
//		LT768_SPI_Init(1,1);
		LT_ReadFlash(wavBuf0,gTempWavFlashAddr,2048);																																	//2019
		gTempWavFlashAddr += 2048;
		wbuf0_Stat = 1; 

		if(gTempWavFlashAddr>=gWavLen)																								//读取数据地址超出 WAV 文件长度， 地址归零
		{
			gTempWavFlashAddr = gWavFlashAddr;
			
			gWavAddrFull_Flag=1;
		}
//		printf("111:%d\r\n",gWavDMA_Len_buf0);
	}
	if(wbuf1_Stat == 0)
	{

		LT_ReadFlash(wavBuf1,gTempWavFlashAddr,2048);
		gTempWavFlashAddr+=2048;
		wbuf1_Stat = 1;

		if(gTempWavFlashAddr>=gWavLen)																								//读取数据地址超出 WAV 文件长度， 地址归零
		{
			gTempWavFlashAddr = gWavFlashAddr;
			gWavAddrFull_Flag=1;
		}
//		printf("222:%d\r\n",gWavDMA_Len_buf1);
	}
	
	if(gWavAddrFull_Flag&&gWavFinish_Flag&&gWavType == 0)
	{
//		printf("end\r\n");
		close_wav();	//判断DMA读的所有数据都通过DAC转换完，同时标志位为单曲循环，才关闭
	}
	//return OK;
	return 1;	
}
