📄 dac_api.c
字号:
//====================================================================================
//File Name: DAC_API.c
//Description: DAC API
//Update: 2007.1.17 V0.1 by wangtao <wangtao@sunnorth.com.cn>
//====================================================================================
#include "SPCE3200_Register.h"
#include "SPCE3200_Constant.h"
#include "DAC_API.h"
unsigned int DAC_Get_PCM_Free_Bytes(void);
unsigned short DAC_TempPCM[DAC_PCM_BUFFER_SIZE];
unsigned int DAC_temp_rp=0;
unsigned int DAC_temp_wp=0;
unsigned short DAC_FIFOArray[4096];
unsigned int DAC_FIFO_Offset=0;
unsigned char DAC_FIFOCounter=0;
unsigned char DAC_Output_Type;
unsigned char DAC_IRQ_Flag;
#ifdef FOR_DAC_DEBUG
unsigned int DAC_SampleCount=0;
#endif
void DAC_CheckFIFOEnough(void);
//=============================================================
//Prototype: void DAC_FillSoftFIFO(void)
//Description: Fill software FIFO
//Arguments: None
//Return Value: None
//=============================================================
void DAC_FillSoftFIFO(void)
{
unsigned short TempValue=0;
unsigned short TempCounter=0;
if(DAC_Output_Type == Stereo)
{
for(TempCounter=0;TempCounter<2048;TempCounter++)
{
TempValue = DAC_TempPCM[DAC_temp_rp];
TempValue ^= 0x8000;
DAC_FIFOArray[TempCounter+DAC_FIFO_Offset] = TempValue;
DAC_temp_rp++;
if(DAC_temp_rp == DAC_PCM_BUFFER_SIZE)
DAC_temp_rp = 0;
}
}
else if(DAC_Output_Type == Left_Only)
{
for(TempCounter=0;TempCounter<1024;TempCounter++) //For stereo
{
TempValue = DAC_TempPCM[DAC_temp_rp];
TempValue ^= 0x8000;
DAC_FIFOArray[TempCounter*2+DAC_FIFO_Offset] = TempValue;
DAC_FIFOArray[TempCounter*2+1+DAC_FIFO_Offset] = TempValue;
DAC_temp_rp += 2;
if(DAC_temp_rp == DAC_PCM_BUFFER_SIZE)
DAC_temp_rp = 0;
}
}
else if(DAC_Output_Type == Right_Only)
{
for(TempCounter=0;TempCounter<1024;TempCounter++) //For stereo
{
TempValue = DAC_TempPCM[DAC_temp_rp+1];
TempValue ^= 0x8000;
DAC_FIFOArray[TempCounter*2+DAC_FIFO_Offset] = TempValue;
DAC_FIFOArray[TempCounter*2+1+DAC_FIFO_Offset] = TempValue;
DAC_temp_rp += 2;
if(DAC_temp_rp == DAC_PCM_BUFFER_SIZE)
DAC_temp_rp = 0;
}
}
else if(DAC_Output_Type == MonoToStereo)
{
for(TempCounter=0;TempCounter<1024;TempCounter++)
{
TempValue = DAC_TempPCM[DAC_temp_rp];
TempValue ^= 0x8000;
DAC_FIFOArray[TempCounter*2+DAC_FIFO_Offset] = TempValue;
DAC_FIFOArray[TempCounter*2+1+DAC_FIFO_Offset] = TempValue;
if(DAC_temp_rp!=DAC_temp_wp)
{
DAC_temp_rp++;
#ifdef FOR_DAC_DEBUG
DAC_SampleCount++;
#endif
}
else
{
DAC_temp_rp = DAC_temp_wp;
}
if(DAC_temp_rp == DAC_PCM_BUFFER_SIZE)
DAC_temp_rp = 0;
}
}
else
{
//
}
if(DAC_FIFOCounter != 0)
{
DAC_FIFO_Offset = 0;
DAC_FIFOCounter = 0;
}
else
{
DAC_FIFO_Offset += 2048;
DAC_FIFOCounter = 1;
}
return;
}
//=============================================================
//Prototype: void DAC_Write_PCM_Data(unsigned short *PCMPtr,unsigned int PCMSizes)
//Description: Fill PCM buffer
//Arguments: PCMPtr: Start address from where PCM data will be stored
// PCMSizes: Number of PCM data to be written
//Return Value: None
//=============================================================
void DAC_Write_PCM_Data(unsigned short *PCMPtr,unsigned int PCMSizes) //PCMSizes unit: bytes
{
unsigned int i;
for(i=0;i<(PCMSizes/2);i++)
{
DAC_TempPCM[DAC_temp_wp++] = PCMPtr[i];
if(DAC_temp_wp == DAC_PCM_BUFFER_SIZE)
{
DAC_temp_wp = 0;
}
}
}
//=============================================================
//Prototype: unsigned int DAC_Get_PCM_Free_Bytes(void)
//Description: Get PCM buffer size
//Arguments: None
//Return Value: Buffer size
//=============================================================
unsigned int DAC_Get_PCM_Free_Bytes(void)
{
unsigned int space;
space = DAC_temp_rp - DAC_temp_wp;
if(space < 0)
space += DAC_PCM_BUFFER_SIZE;
if(space == 0)
{
return ((DAC_PCM_BUFFER_SIZE*2)-2);
}
return ((space*2)-2);
}
//=============================================================
//Prototype: void DAC_EnableSoftCh(unsigned int Samplerate)
//Description: Enable software channel
//Arguments: Samplerate: Sample rate
//Return Value: None
//=============================================================
void DAC_EnableSoftCh(unsigned int Samplerate)
{
unsigned int TempValue;
DAC_Output_Type=MonoToStereo;
DAC_temp_rp=0;
DAC_temp_wp=0;
DAC_FIFO_Offset=0;
DAC_FIFOCounter=0;
*P_DAC_BUFFER_SA = 0;
*P_DAC_FIFOBA_LOW = ((unsigned int)DAC_FIFOArray); //Set start address of buffer for DAC soft channel
*P_DAC_FIFOBA_HIGH = ((unsigned int)DAC_FIFOArray) >> 16;
TempValue = 27000000/Samplerate - 1; //Set sampling rate of current PCM file
*P_DAC_SAMPLE_CLK = TempValue;
*P_DAC_INT_STATUS = 0x4000 | 0x0004 | 0x0003; //Enable software channel as stereo, 8Kbytes buffer
*P_DAC_MODE_CTRL2 = (0x0608 | D_VolSel_1 | 0x1003); //For both channels
}
//=============================================================
//Prototype: void DAC_CheckFIFOEnough(void)
//Description: Wait for FIFO interrupt
//Arguments: None
//Return Value: None
//=============================================================
void DAC_CheckFIFOEnough(void)
{
DAC_IRQ_Flag=0;
while(DAC_IRQ_Flag==0);
}
//=============================================================
//Prototype: void DAC_DisableSoftCh(void)
//Description: Disable software channel
//Arguments: None
//Return Value: None
//=============================================================
void DAC_DisableSoftCh(void)
{
*P_DAC_INT_STATUS = 0; //Disable DAC software channel
*P_DAC_MODE_CTRL2 = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -