📄 dac_api.c
字号:
#include ".\Peripheral\DAC\DAC_API.h"
U32 DAC_Get_PCM_Free_Bytes(void);
U16 DAC_TempPCM[DAC_PCM_BUFFER_SIZE];
U32 DAC_temp_rp=0;
U32 DAC_temp_wp=0;
U16 DAC_FIFOArray[4096];
U32 DAC_FIFO_Offset=0;
U8 DAC_FIFOCounter=0;
U8 DAC_Output_Type;
U8 DAC_IRQ_Flag;
#ifdef FOR_DAC_DEBUG
U32 DAC_SampleCount=0;
#endif
void DAC_CheckFIFOEnough(void);
void DAC_FillSoftFIFO(void)
{
unsigned short TempValue=0;
unsigned short TempCounter=0;
unsigned int free_space;
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;
}
void DAC_Write_PCM_Data(U16 *PCMPtr,U32 PCMSizes) //PCMSizes unit: bytes
{
U32 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;
}
}
}
U32 DAC_Get_PCM_Free_Bytes(void)
{
U32 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);
}
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 channel
}
void DAC_CheckFIFOEnough(void)
{
DAC_IRQ_Flag=0;
while(DAC_IRQ_Flag==0);
}
void DAC_DisableSoftCh(void)
{
*P_DAC_INT_STATUS = 0; //Stop DAC software channel
*P_DAC_MODE_CTRL2 = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -