⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wdt_ac97.c

📁 2440超级bios(ads1.2 rvds2.2)通用
💻 C
📖 第 1 页 / 共 3 页
字号:

		UartPrintf(SHL_PRT,"\nPress any key to exit.\n");
		UartGetByte(SHL_PRT);
	}
	Codec_Ready =1;
	AC97_Port_Return();
}

/* Functional Sub-Routines */
void AC97_Port_Init(void)
{
	//Push AC97 GPIO port configuration
	save_AC97_rGPEDAT=rGPEDAT;
	save_AC97_rGPECON=rGPECON;
	save_AC97_rGPEUP=rGPEUP;

	//---------------------------------------------------------------------
	//   PORT E GROUP
	//Ports  :  GPE4    		        GPE3                 GPE2            GPE1              GPE0
	//Signal :  AC_SDATA_OUT   AC_SDATA_IN   AC_nRESET   AC_BIT_CLK   AC_SYNC
	//Binary :   11,                     11,                   11,                11,                 11
	//---------------------------------------------------------------------
	rGPECON = rGPECON & ~(0x3ff) | 0x3ff;   //GPE[4:0]=AC_SDATA_OUT   AC_SDATA_IN   AC_nRESET   AC_BIT_CLK   AC_SYNC
	rGPEUP  = rGPEUP  & ~(0x1f)  | 0x1f;    //The pull up function is disabled GPE[4:0] 1 1111

	//For EINT0 Push Button
   	rGPFUP   = ((rGPFUP   & ~(1<<0)) | (1<<0));     //GPF0
   	rGPFCON  = ((rGPFCON  & ~(3<<0)) | (1<<1));     //GPF0=EINT0
   	rEXTINT0 = ((rEXTINT0 & ~(7<<0)) | (2<<0));     //EINT0=falling edge triggered

	//gpg11 for lm4863en
	rGPGCON &= ~(3<<22);
	rGPGCON |=  (1<<22);		// output
	rGPGUP  &= ~(1<<11);			// pullup disable
	rGPGDAT |=  (1<<11);			// output high
}
void AC97_Port_Return(void)
{
	//Pop AC97 GPIO port configuration
	rGPECON=save_AC97_rGPECON;
	rGPEDAT=save_AC97_rGPEDAT;
	rGPEUP=save_AC97_rGPEUP;
}
void AC97_Init(void)
{
	int i = 0;
	U08 ch;

	UartPrintf(SHL_PRT,"\nAC97 Initialization...\n");
 	// Cold Reset
	rAC_GLBCTRL = 0x1;	// controller and codec cold reset
	Delay(1000);		// delay for controller safety reset
	rAC_GLBCTRL = 0x0;	// controller and codec normal mode
	Delay(1000);
	rAC_GLBCTRL = 0x1;
	Delay(1000);
	rAC_GLBCTRL = 0x0;
	Delay(1000);
	//AC-link On
	rAC_GLBCTRL = (1<<2);
	Delay(1000);
	AC97_Controller_State();
	//Transfer data enable using AC-link
	rAC_GLBCTRL |= (1<<3);	// AC97 Data transfer active
	Delay(1000);
	AC97_Controller_State();
	UartPrintf(SHL_PRT,"\nAC97-Link On...\n");
    //Codec Ready Check using Codec Ready Interrupt
    Codec_Ready_Irq =0;
	pISR_WDT_AC97= (unsigned)AC97_Codec_Ready;
	ClearPending(BIT_WDT_AC97);
	rSUBSRCPND=(BIT_SUB_AC97);
   	rINTMSK=~(BIT_WDT_AC97);
   	rINTSUBMSK=~(BIT_SUB_AC97);
   	rAC_GLBCTRL |= 0x400000;
	while(!Codec_Ready_Irq)
	{
	 	UartPrintf(SHL_PRT,".");
       	Delay(3000);
       	i++;
       	if(i==20)
			break;
	}
	UartPrintf(SHL_PRT,"\n");
	if(i==20)
	{
		UartPrintf(SHL_PRT,"\nAC97 codec is not ready.");
		UartPrintf(SHL_PRT,"\nCheck on connection between 2440A and AC97 Codec.\n");
		//UartPrintf(SHL_PRT,"\nRN1, RN3 and R280 instead of RN2, RN4 and R281 must be used for AC97 link on SMDK2440 Base Board (Rev 0.18).\n");
		UartPrintf(SHL_PRT,"\nBye. ");
		Codec_Ready = 0;
	}
}
void AC97_CodecInit_PD(void)
{
	UartPrintf(SHL_PRT,"\nAC97 Codec Soft Reset\n");
	AC97_Codec_Cmd(0,0x00,0x683F);		//Codec Soft Reset : 16bit In/Out  (stac9766/67)
	UartPrintf(SHL_PRT,"AC97 Codec 0x26 Reg.: 0x%x\n\n", AC97_Codec_Cmd(1,0x26,0x0000));
}

void AC97_CodecInit_PCMIn( U16 AC97_fs)
{
	AC97_Codec_Cmd(0,0x00,0x683F);		//codec soft reset
	AC97_Codec_Cmd(0,0x2A,0x0001);		//variable rate enable
	UartPrintf(SHL_PRT,"VRA Enable(1)/Disable(0): 0x%x\n",(0x1&AC97_Codec_Cmd(1,0x2A,0x0001)));
	if(AC97_fs==48000)
		AC97_Codec_Cmd(0,0x32,0xbb80);	//ADC Sampling frequency 48kHz
	else if(AC97_fs==44100)
		AC97_Codec_Cmd(0,0x32,0xac44);	//ADC Sampling frequency 44.1kHz
	else if(AC97_fs==22050)
		AC97_Codec_Cmd(0,0x32,0x5622);	//ADC Sampling frequency 22.05kHz

	AC97_Codec_Cmd(0,0x26,(1<<9));		//all power on except DAC Block
	UartPrintf(SHL_PRT,"\nAC97 Codec 0x26 Reg.: 0x%x\n\n", AC97_Codec_Cmd(1,0x26,0x0000));
	AC97_Codec_Cmd(0,0x10,0x1010);		//line in volume on
	AC97_Codec_Cmd(0,0x6e,0x0000);		//All Analog Mode, ADC Input select => left slot3, right slot4
	AC97_Codec_Cmd(0,0x1a,0x0505);		//record source select => Stereo Mix
	AC97_Codec_Cmd(0,0x1c,0x0909);		//record gain is initial
	AC97_Codec_Cmd(0,0x78,0x0001);		//ADC HPF Bypass
	AC97_Codec_Cmd(0,0x20,0x0000);		//General Reg.
	Input_Volume = 	AC97_Codec_Cmd(1,0x10,0x0000);	      //Line In volume
}
void AC97_CodecInit_PCMOut( U16 AC97_fs)
{

	AC97_Codec_Cmd(0,0x00,0x683F);		//codec soft reset
	AC97_Codec_Cmd(0,0x2A,0x0001);		//variable rate enable
	//UartPrintf(SHL_PRT,"\nVRA Enable(1)/Disable(0): 0x%x\n", (0x1&AC97_Codec_Cmd(1,0x2A,0x0001)));
	if(AC97_fs==48000)	//DAC Sampling frequency 48kHz
		AC97_Codec_Cmd(0,0x2C,0xbb80);
	else if(AC97_fs==44100)	//DAC Sampling frequency 44.1kHz
		AC97_Codec_Cmd(0,0x2C,0xac44);
	else if(AC97_fs==22050)	//DAC Sampling frequency 22.05kHz
		AC97_Codec_Cmd(0,0x2C,0x5622);
	AC97_Codec_Cmd(0,0x26, 0x00);		// all power on except ADC blcok
//	AC97_Codec_Cmd(0,0x26, (1<<8));		// all power on except ADC blcok
	UartPrintf(SHL_PRT,"AC97 Codec 0x26 Reg.: 0x%x\n\n", AC97_Codec_Cmd(1,0x26,0x0000));
	AC97_Codec_Cmd(0,0x18,0x6000);		// PCM out volume on
//AC97_Codec_Cmd(0,0x5c,0x0080);		// PCM out volume on
AC97_Codec_Cmd(0,0x5c,0x0000);		// PCM out volume on
AC97_Codec_Cmd(0,0x20,0x2000);		// general purpose
AC97_Codec_Cmd(0,0x22,0x0f);		// general purpose
//	AC97_Codec_Cmd(0,0x20,0x0000);		// general purpose
	AC97_Codec_Cmd(0,0x04,0x0000);		// Aux out(HP out) volume on
	Output_Volume = AC97_Codec_Cmd(1,0x04,0x00000);	//HP out volume
}
void AC97_CodecInit_MICIn(U16 AC97_fs)
{
	AC97_Codec_Cmd(0,0x00,0x683F);		//codec soft reset
	AC97_Codec_Cmd(0,0x2A,0x0001);		//variable rate enable
	UartPrintf(SHL_PRT,"VRA Enable(1)/Disable(0): 0x%x\n",(0x1&AC97_Codec_Cmd(1,0x2A,0x0001)));
	if(AC97_fs==48000)	//ADC Sampling frequency 48kHz
		AC97_Codec_Cmd(0,0x32,0xbb80);
	else if(AC97_fs==44100)	//ADC Sampling frequency 44.1kHz
		AC97_Codec_Cmd(0,0x32,0xac44);
	else if(AC97_fs==22050)	//ADC Sampling frequency 22.05kHz
		AC97_Codec_Cmd(0,0x32,0x5622);
	AC97_Codec_Cmd(0,0x26,(1<<9));		//all power on except DAC Block
	UartPrintf(SHL_PRT,"\nAC97 Codec 0x26 Reg.: 0x%x\n\n", AC97_Codec_Cmd(1,0x26,0x0000));
	AC97_Codec_Cmd(0,0x20,0x0000);		//MIC1 Selected
	AC97_Codec_Cmd(0,0x6e,0x0024);		//ADC Input Slot => left slot6, right slot9, MIC GAIN VAL =1
	AC97_Codec_Cmd(0,0x0e,0x0040);		//BOOSTEN =1
	AC97_Codec_Cmd(0,0x1a,0x0000);		//Left, Right => MIC
	AC97_Codec_Cmd(0,0x1c,0xff);
	AC97_Codec_Cmd(0,0x78,0x0001);		//ADC HPF Bypass
	Input_Volume = 	AC97_Codec_Cmd(1,0x1c,0x0000);	      //Record Volume
}
U16 AC97_Codec_Cmd(U08 CMD_Read, U08 CMD_Offset, U16 CMD_Data)
{
	U16 Codec_Stat;

	if(CMD_Read == 0)
	{
		rAC_CODEC_CMD = (0<<23)|(CMD_Offset<<16)|(CMD_Data<<0);
		Delay_After_CommandWrite(1);	//30us delay.
    	rAC_CODEC_CMD |= (1<<23);	//To receive SLOTREQ bits when VRA is '1'.
	}
	else if (CMD_Read ==1)
	{
		rAC_CODEC_CMD = (1<<23)|(CMD_Offset<<16)|(CMD_Data<<0);
		Delay(1000);

    	Codec_Stat = (U16)(rAC_CODEC_STAT & 0xFFFF);
    	Delay(1000);
    	return Codec_Stat;
    }
    else
    	return 0;
}
void PCM_In_Volume( U08 Up_Down_Volume)
{
	if( ( Up_Down_Volume == 'u') | (Up_Down_Volume == 'U') )
	{
		if (Input_Volume == 0x0000)
			UartPrintf(SHL_PRT,"Limit Volume Range!\n");
		else
		{
			Input_Volume -= 0x0101;
			AC97_Codec_Cmd(0,0x10, Input_Volume);	// PCM In Volume Up
			UartPrintf(SHL_PRT,"PCM In Volume Level : 0x%x\n", Input_Volume);
		}
	}
	if ( ( Up_Down_Volume == 'd') | (Up_Down_Volume == 'D') )
	{
		if (Input_Volume == 0x1F1F)
			UartPrintf(SHL_PRT,"Limit Volume Range!\n");
		else
		{
			Input_Volume += 0x0101;
			AC97_Codec_Cmd(0,0x10, Input_Volume);	// PCM In Volume Down
			UartPrintf(SHL_PRT,"PCM In Volume Level : 0x%4x\n", Input_Volume);
		}
	}
}
void PCM_Out_Volume(U08 Up_Down_Volume)
{
	if( ( Up_Down_Volume == 'u') | (Up_Down_Volume == 'U') )
	{
		if (Output_Volume < 0x0101)
		{
			Output_Volume = 0x0000;
			UartPrintf(SHL_PRT,"\nLimit Volume Range!");
		}
		else
		{
			Output_Volume -= 0x0101;
			AC97_Codec_Cmd(0,0x04, Output_Volume);		// PCM out Volume Up
			UartPrintf(SHL_PRT,"\nHeadphone Volume Level (In AC97 Codec 04h Reg.): 0x%x", Output_Volume);
		}
	}
	if ( ( Up_Down_Volume == 'd') | (Up_Down_Volume == 'D') )
	{
		if (Output_Volume > 0x1F1F)
		{
			Output_Volume = 0x1F1F;
			UartPrintf(SHL_PRT,"\nLimit Volume Range!");
		}
		else
		{
			Output_Volume += 0x0101;
			AC97_Codec_Cmd(0,0x04, Output_Volume);		// PCM out Volume Down
			UartPrintf(SHL_PRT,"\nHeadphone Volume Level (In AC97 Codec 04h Reg.): 0x%x", Output_Volume);
		}
	}
}
void Download_PCM_File(void)
{
    pISR_UART1 = (unsigned)RxInt;

    rINTMSK    = ~( BIT_UART1);
    rINTSUBMSK = ~(BIT_SUB_RXD1);
    AC97_BUF   = (U08 *)DOWN_BUF;
   	AC97_temp = AC97_BUF;

	UartPrintf(SHL_PRT,"\nDownload the PCM(no ADPCM) file via Serial Port Transmit in DNW (With header & CS)!\n");
	UartPrintf(SHL_PRT,"Max of PCM Size: 4M bytes\n");
	while(((unsigned int)AC97_temp - (unsigned int)AC97_BUF) < 4)
       	Delay(3000);
   	AC97_size = *(AC97_BUF) | *(AC97_BUF + 1)<<8 | *(AC97_BUF + 2)<<16 | *(AC97_BUF + 3)<<24;
   	UartPrintf(SHL_PRT,"\nNow, Downloading... [ File Size : %7d(      0)]",AC97_size);
   	while(((unsigned int)AC97_temp - (unsigned int)AC97_BUF) < AC97_size)
    UartPrintf(SHL_PRT,"\b\b\b\b\b\b\b\b%7d)",(unsigned int)AC97_temp - (unsigned int)AC97_BUF);
  	UartPrintf(SHL_PRT,"\b\b\b\b\b\b\b\b%7d)]\n",(unsigned int)AC97_temp - (unsigned int)AC97_BUF);
   	rINTSUBMSK |= BIT_SUB_RXD1;
   	AC97_size = *(AC97_BUF + 0x2c) | *(AC97_BUF + 0x2d)<<8 | *(AC97_BUF + 0x2e)<<16 | *(AC97_BUF + 0x2f)<<24;
   	AC97_size = (AC97_size>>1)<<1;
    AC97_fs   = *(AC97_BUF + 0x1c) | *(AC97_BUF + 0x1d)<<8 | *(AC97_BUF + 0x1e)<<16 | *(AC97_BUF + 0x1f)<<24;
   	Play_AC97_BUF = (U32 *)(AC97_BUF + 0x30);	//DNW Header + PCM File Header => OffSet: 0x30
 	UartPrintf(SHL_PRT,"Sample PCM Data Size = %d\n", AC97_size);
	UartPrintf(SHL_PRT,"Sampling Frequency = %d Hz\n", AC97_fs);
}
void AC97_PCMout_DMA1(U32 PCM_Size)
{
	pISR_EINT0= (unsigned)Muting;
	pISR_DMA1= (unsigned)DMA1_Play_Done;
	rINTMSK = ~(BIT_DMA1|BIT_EINT0);

	Play_AC97_BUF=(U32 *)(DOWN_BUF +0x30);
	//DMA Ch1 for PCMInitialize
	rDISRC1  = (int)Play_AC97_BUF;
	rDISRCC1 = (0<<1) + (0<<0);		//The source is in the system bus(AHB), Increment
	rDIDST1  = ((U32)0x5b000018);  	//PCM Out Data Fifo
	rDIDSTC1 = (1<<1) + (1<<0);           //The destination is in the peripheral bus(APB), Fixed
	rDCON1   = (1<<31)+(0<<30)+(1<<29)+(0<<28)+(0<<27)+(5<<24)+(1<<23)+(0<<22)+(2<<20)+(PCM_Size/4);
	rDMASKTRIG1 = (0<<2) + (1<<1) + (0<<0);          //No-stop[2], DMA1 channel On[1], No-sw trigger[0]
	//AC97  Initialize
	UartPrintf(SHL_PRT,"\nConnect Head Phone Line In to CON21 (Head Phone Out) on SMDK2440 and Press Key.\n");
	UartGetByte(SHL_PRT);
	UartPrintf(SHL_PRT,"\nNow Play...\n");
	UartPrintf(SHL_PRT,"To Volume Up, Press the 'u' key.\n");
	UartPrintf(SHL_PRT,"To Volume Down, Press the 'd' key.\n");
	UartPrintf(SHL_PRT,"To Mute On/Off, Press EINT0 Button.\n");
	UartPrintf(SHL_PRT,"\nIf you want to exit, Press the 'x' key.\n");
	UartPrintf(SHL_PRT,"Headphone Volume Register = 0x%x\n", Output_Volume);
	//Transfer data enable  using AC-Link
	rAC_GLBCTRL = 0x200C;
	//UartPrintf(SHL_PRT,"\nWrite Value to AC_GLBCTRL Reg. =>0x200C\n");
	//UartPrintf(SHL_PRT,"Read Value from AC_GLBCTRL Reg. =>0x%x\n", rAC_GLBCTRL);

	Delay(1000);
  	while(1)
   	{
		//UartPrintf(SHL_PRT,"STAT3: 0x%x		CURR_TC: 0x%x		DCDST3: 0x%x\n", rDSTAT3&0x300000, rDSTAT3&0xfffff, rDCDST3);
   		Up_Down_Volume=UartGetByte(SHL_PRT);
		if( (Up_Down_Volume == 'x') | (Up_Down_Volume == 'X'))
			break;
   		PCM_Out_Volume(Up_Down_Volume);
   	}
	ClearPending(BIT_DMA1);
	rDMASKTRIG1  = (1<<2);       //DMA1 stop
	rINTMSK|= (BIT_DMA1 | BIT_EINT0);
	 //AC97 PCM In Channel Finish
	 rAC_GLBCTRL &= ~(1<<13); //PCM Out Transfer Mode Off
	 UartPrintf(SHL_PRT,"\nEnd of Play!\n");
}
void AC97_PCMout_INT(U32 PCM_Size)
{
	//Record AC97_BUF initialize
    Rec_AC97_BUF   = (U32 *)(DOWN_BUF);
	Play_AC97_BUF = (Rec_AC97_BUF + 0x30);
	End_AC97_BUF = (Rec_AC97_BUF + 0x30 + PCM_Size/4);

	//IRQ Initialization
	pISR_WDT_AC97= (unsigned)Irq_AC97_PCMout;

	UartPrintf(SHL_PRT,"\nConnect Head Phone Line In to CON21 on SMDK2440 and Press any key.\n");
	UartGetByte(SHL_PRT);
	UartPrintf(SHL_PRT,"\nNow Play...\n");
	UartPrintf(SHL_PRT,"Headphone Volume Register = 0x%x\n", Output_Volume);

	ClearPending(BIT_WDT_AC97);
	rSUBSRCPND=(BIT_SUB_AC97);

   	rINTMSK=~(BIT_WDT_AC97);
   	rINTSUBMSK=~(BIT_SUB_AC97);

	rAC_GLBCTRL = 0x4100C;	//PCM Out channel threshold INT enable, PIO Mode On

	while(1)
	{
		if(PCM_Out_INT_Exit == 1)
			break;
	}

	rAC_GLBCTRL &= ~(1<<12); //PCM Out Transfer PIO Mode Off

	rINTSUBMSK|=(BIT_SUB_WDT|BIT_SUB_AC97);
	rINTMSK|=(BIT_WDT_AC97);

	UartPrintf(SHL_PRT,"\nEnd of Play!\n");
}

void AC97_PCMin_DMA2(U32 PCM_Size)
{
	Rec_AC97_BUF   = (U32 *)(DOWN_BUF);

	//IRQ Initialize
	pISR_DMA2  = (unsigned)DMA2_Rec_Done;
	rINTMSK = ~(BIT_DMA2);

       //DMA2 Initialize
	rDISRC2  = ((U32)0x5B000018);         //PCM Input Data FIFO
	rDISRCC2 = (1<<1) + (1<<0);          //APB, Fix
       rDIDST2  = (int)Rec_AC97_BUF;   //Record AC97_BUF initializ
       rDIDSTC2 = (0<<1) + (0<<0);          //AHB, Increment
       rDCON2   = (1<<31)+(0<<30)+(1<<29)+(0<<28)+(0<<27)+(5<<24)+(1<<23)+(1<<22)+(2<<20)+(PCM_Size/4);

	UartPrintf(SHL_PRT,"Connect Sound Line Out to CON25(Line In) on SMDK2440\n");
       UartPrintf(SHL_PRT,"Press any key to start record.\n");
    	UartGetByte(SHL_PRT);
   	UartPrintf(SHL_PRT,"Recording...\n");

       rDMASKTRIG2 = (0<<2) + (1<<1) + 0;    //No-stop, DMA2 channel on, No-sw trigger

	// Transfer data enable  using AC-Link
       rAC_GLBCTRL = 0x80C;	// Transfer data enable  using AC-Link

       //UartPrintf(SHL_PRT,"\nWrite Value to AC_GLBCTRL Reg. =>0x80C\n");
	//UartPrintf(SHL_PRT,"Read Value from AC_GLBCTRL Reg. =>0x%x\n", rAC_GLBCTRL);

       while(AC97_Rec_Done ==0)
       {
        	UartPrintf(SHL_PRT,".");
         	Delay(3000);
         	//UartPrintf(SHL_PRT,"STAT2: 0x%x		CURR_TC: 0x%x		DCDST2: 0x%x\n", rDSTAT2&0x300000, rDSTAT2&0xfffff, rDCDST2);
       }
       AC97_Rec_Done = 0;
       rDMASKTRIG2 = (1<<2);     //DMA2 stop
       rINTMSK |= BIT_DMA2;
       //AC97 PCM In Channel Finish
       rAC_GLBCTRL &= ~(1<<11); //PCM In Transfer Mode Off
  	Delay(1000);
  	UartPrintf(SHL_PRT,"\nEnd of Record!\n");
}
void AC97_PCMin_INT(U32 PCM_Size)
{
	//Record AC97_BUF initialize
    Rec_AC97_BUF   = (U32 *)(DOWN_BUF);
	Play_AC97_BUF = Rec_AC97_BUF + 0x30;
	End_AC97_BUF = (Rec_AC97_BUF + 0x30 + PCM_Size/4);

	//IRQ Initialization
	pISR_WDT_AC97= (unsigned)Irq_AC97_PCMin;
	UartPrintf(SHL_PRT,"Connect Sound Line Out to CON25(Line In) on SMDK2440\n");
    UartPrintf(SHL_PRT,"Press any key to start record.\n");
  	UartGetByte(SHL_PRT);
   	UartPrintf(SHL_PRT,"Recording...\n");

	ClearPending(BIT_WDT_AC97);
	rSUBSRCPND=(BIT_SUB_AC97);
   	rINTMSK=~(BIT_WDT_AC97);
   	rINTSUBMSK=~(BIT_SUB_AC97);
	rAC_GLBCTRL = 0x2040C;	//PCM In channel threshold INT enable, PIO Mode On
	while(1)
	{
       if(PCM_In_INT_Exit == 1)
		break;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -