www.pudn.com > DSP5502_audio_loopback.rar > DEC5502_CODEC.c


 
 
/******************************************************************************/ 
/*  Copyright 2004 by SEED Electronic Technology LTD.                         */ 
/*  All rights reserved. SEED Electronic Technology LTD.                      */ 
/*  Restricted rights to use, duplicate or disclose this code are             */ 
/*  granted through contract.                                                 */ 
/*                                                                            */ 
/*                                                                            */ 
/******************************************************************************/ 
/* DESCRIPTION:  															  */ 
/*   																		  */ 
/* This is an audio signal sample and play example for CODEC of C5502		  */ 
/*----------------------------------------------------------------------------*/ 
/* MODULE NAME... IIC and Mcbsp												  */ 
/* FILENAME...... 5502_CODEC.c   											  */ 
/* DATE CREATED.. Mon 6/20/2004	    										  */ 
/* COMPONENT..... 															  */ 
/* PREREQUISITS.. 															  */ 
/*----------------------------------------------------------------------------*/ 
/* DESCRIPTION:																  */   
/*   																		  */ 
/* This example is that the codec is controled by the IIC and Mcbsp 		  */ 
/*----------------------------------------------------------------------------*/ 
 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
 
#include "E2PROM_Function.h" 
#include "CODEC.h" 
 
#undef  CODEC_ADDR 
#define CODEC_ADDR 0x1A 
 
/*------------------------------------------------------------------------------------*/ 
// 
// Config McBSP:  Use McBSP to send and receive the data between DSP and AIC23B 
// 
/*------------------------------------------------------------------------------------*/ 
MCBSP_Config Mcbsp1Config = { 
  MCBSP_SPCR1_RMK(     
    MCBSP_SPCR1_DLB_OFF,  			// DLB    = 0  
    MCBSP_SPCR1_RJUST_LZF,          // RJUST  = 0,right justify the data and zero fill the MSBs 
    MCBSP_SPCR1_CLKSTP_DISABLE,     // CLKSTP = 0  
    MCBSP_SPCR1_DXENA_ON,           // DXENA  = 1,DX delay enabler on  
    0,             				   	// Reserved   = 0  
    MCBSP_SPCR1_RINTM_RRDY,         // RINTM  = 0  
    MCBSP_SPCR1_RSYNCERR_NO,        // RSYNCER = 0  
    MCBSP_SPCR1_RFULL_NO,           // RFULL = 0   
    MCBSP_SPCR1_RRDY_NO,            // RRDY = 0   
    MCBSP_SPCR1_RRST_DISABLE 		// RRST   = 0; Disable receiver  
   ), 
  MCBSP_SPCR2_RMK(   
    MCBSP_SPCR2_FREE_NO,            // FREE   = 0  
    MCBSP_SPCR2_SOFT_NO,            // SOFT   = 0  
    MCBSP_SPCR2_FRST_FSG,         	// FRST   = 1 ; Enable the frame-sync logic 
    MCBSP_SPCR2_GRST_CLKG,         	// GRST   = 1 ; The sample rate generator is take out of its reset state  
    MCBSP_SPCR2_XINTM_XRDY,         // XINTM  = 0  
    MCBSP_SPCR2_XSYNCERR_NO,        // XSYNCER =0  
    MCBSP_SPCR2_XEMPTY_NO,          // XEMPTY = 0  
    MCBSP_SPCR2_XRDY_NO,            // XRDY   = 0              
    MCBSP_SPCR2_XRST_DISABLE 	    // XRST   = 0 Disable transimitter  
   ), 
   // 单数据相,接受数据长度为16位,每相2个数据 
  MCBSP_RCR1_RMK(  
  	MCBSP_RCR1_RFRLEN1_OF(1),       // RFRLEN1 = 1  
  	MCBSP_RCR1_RWDLEN1_16BIT        // RWDLEN1 = 2  
  ), 
  MCBSP_RCR2_RMK(     
    MCBSP_RCR2_RPHASE_SINGLE,       // RPHASE  = 0  
    MCBSP_RCR2_RFRLEN2_OF(0),       // RFRLEN2 = 0  
    MCBSP_RCR2_RWDLEN2_8BIT,       	// RWDLEN2 = 0  
    MCBSP_RCR2_RCOMPAND_MSB,        // RCOMPAND = 0 No companding,any size data, MSB received first  
    MCBSP_RCR2_RFIG_YES,  		    // RFIG    = 1 Frame-sync ignore  
    MCBSP_RCR2_RDATDLY_1BIT  		// RDATDLY = 1 1-bit data delay  
    ),   
  MCBSP_XCR1_RMK(     
    MCBSP_XCR1_XFRLEN1_OF(1),       // XFRLEN1 = 1   
    MCBSP_XCR1_XWDLEN1_16BIT        // XWDLEN1 = 2    
 ),    
 MCBSP_XCR2_RMK(    
    MCBSP_XCR2_XPHASE_SINGLE,       // XPHASE  = 0  
    MCBSP_XCR2_XFRLEN2_OF(1),       // XFRLEN2 = 0  
    MCBSP_XCR2_XWDLEN2_8BIT,       	// XWDLEN2 = 0  
    MCBSP_XCR2_XCOMPAND_MSB,        // XCOMPAND = 0  
    MCBSP_XCR2_XFIG_YES,            // XFIG    = 1 Unexpected Frame-sync ignore  
    MCBSP_XCR2_XDATDLY_1BIT         // XDATDLY = 1 1-bit data delay  
  ),             
 MCBSP_SRGR1_DEFAULT, 
 MCBSP_SRGR2_DEFAULT,				  
 MCBSP_MCR1_DEFAULT, 
 MCBSP_MCR2_DEFAULT,  
 MCBSP_PCR_RMK( 
   MCBSP_PCR_IDLEEN_RESET,          // IDLEEN   = 0    
   MCBSP_PCR_XIOEN_SP,              // XIOEN    = 0    
   MCBSP_PCR_RIOEN_SP,              // RIOEN    = 0    
   MCBSP_PCR_FSXM_EXTERNAL,  		// FSXM     = 0 Tranmit frame-syn is provided by AIC23B  
   MCBSP_PCR_FSRM_EXTERNAL,         // FSRM     = 0 Receive frame-syn is provided by AIC23B  
   MCBSP_PCR_CLKXM_INPUT,   		// CLKR is input  
   MCBSP_PCR_CLKRM_INPUT,           // CLKX is input  
   MCBSP_PCR_SCLKME_NO,             // SCLKME=0 CLKG is taken from the McBSP internal input clock   
   MCBSP_PCR_CLKSSTAT_0,            // The signal on the CLKS pin is low    
   MCBSP_PCR_DXSTAT_0,              // Drive the signal on the DX pin low    
   MCBSP_PCR_DRSTAT_0,              // The signal on the DR pin is low    
   MCBSP_PCR_FSXP_ACTIVEHIGH,  		// FSXP     = 1 Because a falling edge on LRCIN or LRCOUT starts data transfer   
   MCBSP_PCR_FSRP_ACTIVELOW,        // FSRP     = 1    
   MCBSP_PCR_CLKXP_FALLING,         // CLKXP    = 1   The falling edge of BCLK starts data transfer  
   MCBSP_PCR_CLKRP_RISING           // CLKRP    = 1    
 ), 
 MCBSP_RCERA_DEFAULT,  
 MCBSP_RCERB_DEFAULT,  
 MCBSP_RCERC_DEFAULT,  
 MCBSP_RCERD_DEFAULT,  
 MCBSP_RCERE_DEFAULT,  
 MCBSP_RCERF_DEFAULT,  
 MCBSP_RCERG_DEFAULT,  
 MCBSP_RCERH_DEFAULT,  
 MCBSP_XCERA_DEFAULT, 
 MCBSP_XCERB_DEFAULT, 
 MCBSP_XCERC_DEFAULT, 
 MCBSP_XCERD_DEFAULT,   
 MCBSP_XCERE_DEFAULT, 
 MCBSP_XCERF_DEFAULT,   
 MCBSP_XCERG_DEFAULT, 
 MCBSP_XCERH_DEFAULT 
 };  
// 定义McBSP的句柄 
MCBSP_Handle hMcbsp; 
 
/*------------------------------------------------------------------------------------*/ 
// 
// Config I2C: Use I2C to interface CODEC control interface 
// 
/*------------------------------------------------------------------------------------*/ 
I2C_Setup Setup = { 
					0, 		// 7 bit address mode  
					0x0000, // own address  
					60, 	// clkout value (Mhz)  
					400, 	// a number between 10 and 400 
					0, 		// 8 bits/byte to be received or transmitted  
					0, 		// DLB mode off  
					0 		// FREE mode off  
				  }; 
				   
/*------------------------------------------------------------------------------------*/ 
// 
// Config CODEC control registers 
// 
/*------------------------------------------------------------------------------------*/ 
 
	// 数字音频接口格式设置 
	// AIC23为主模式,数据为DSP模式,数据长度16位    
 Uint16 Digital_Audio_Inteface_Format[2]={ 
 	Codec_DAIF_REV, 
 	DAIF_MS(1)+DAIF_LRSWAP(0)+DAIF_LRP(1)+DAIF_IWL(0)+DAIF_FOR(3)}; 
 
	// AIC23的波特率设置,采样率为48k,CLKIN=CLKOUT=MCLK 
	// 时钟模式设为普通模式,基过采样率为250Fs 
 Uint16 Sample_Rate_Control[2] = { 
 	Codec_SRC_REV, 
 	SRC_CLKIN(0)+SRC_CLKOUT(0)+SRC_SR(6)+SRC_BOSR(0)+SRC_USB(0)}; 
 
	// AIC23寄存器复位 
 Uint16 Reset[2] ={ 
 	Codec_RST_REV, 
 	RST_RES}; 
 
	// AIC23节电方式设置,所有部分均处于工作状态 
 Uint16 Power_Down_Control[2] ={ 
 	Codec_PDC_REV, 
 	PDC_DEFAULT}; 
 
	// AIC23模拟音频的控制:关掉侧音 
	// DAC使能,ADC输入选择为音频输入 
 Uint16 Analog_Aduio_Path_Control[2] = { 
  	Codec_AAPC_STA2(0), 
  	AAPC_STA10(0)+AAPC_STE(0)+AAPC_DAC(1)+AAPC_BYP(0)+AAPC_INSEL(0)+AAPC_MICM(0)+AAPC_MICB(0)}; 
 
	// AIC23数字音频通路的控制 
	// 使能ADC高通滤波 
 Uint16 Digital_Audio_Path_Control[2] ={ 
  	Codec_DAPC_REV, 
  	DAPC_DACM(0)+DAPC_DEEMP(0)+DAPC_ADCHP(1)}; 
 
	// AIC23数字接口的使能 
 Uint16 Digital_Interface_Activation[2] ={ 
 	Codec_DIA_REV, 
 	DIA_ACT(1)}; 
 
	// AIC23左通路音频调节	 
 Uint16 Left_Line_Input_Volume_Control[2] ={ 
  	Codec_LLIVC_LPS(1), 
  	LLIVC_LIM(0)+LLIVC_LIV(23)}; 
 
	// AIC23右通路音频调节 
 Uint16 Right_Line_Input_Volume_Control[2] = { 
 	Codec_RLIVC_RLS(1), 
 	RLIVC_RIM(0)+RLIVC_RIV(23)}; 
 
	// AIC23耳机左通路音频调节 
 Uint16 Left_Headphone_Volume_Control[2] = { 
 	Codec_LHPVC_LRS(1), 
 	LHPVC_LZC(1)+LHPVC_LHV(127)}; 
 
	// AIC23耳机右通路音频调节 
 Uint16 Right_Headphone_Volume_Control[2] = { 
 	Codec_RHPVC_RLS(1), 
 	LHPVC_RZC(1)+LHPVC_RHV(127)}; 
 
/*------------------------------------------------------------------------------------*/ 
// 
// FUNCTION: MAIN 
// 
/*------------------------------------------------------------------------------------*/ 
                   
void main(void) 
{ 
    Uint16  DataTempLeft = 0;		// 暂存采样数据 
    Uint16  DataTempRight = 0; 
     
// Initialize CSL library - This is REQUIRED !!!  
    CSL_init(); 
	 
// The main frequency of system is 240MHz 
// 该频率是为了设置IIC模块的需要设置的,为了使用I2C_setup函数 
    PLL_setFreq(1, 0xC, 0, 1, 3, 3, 0); 
   
// Initialize I2C, Using parameters in I2C_Setup structure 
	MyI2C_setup(&Setup); 
 
// Open McBSP port 1 and get a McBSP type handle 
	hMcbsp = MCBSP_open(MCBSP_PORT1,MCBSP_OPEN_RESET); 
 
// Config McBSP	port 1 by use previously defined structure 
	MCBSP_config(hMcbsp,&Mcbsp1Config); 
 
// Start McBSP1  
	MCBSP_start(hMcbsp,  
                MCBSP_RCV_START | MCBSP_XMIT_START,  
                0); 
     
    /*设置AIC23各部分均工作*/ 
    MyI2C_Write( Power_Down_Control,//pointer to data array 
	   	       2,				//length of data to be transmitted 
	    	   1,				//master or slaver 
	    	   CODEC_ADDR,	    //slave address to transmit to 
	    	   1,				//transfer mode of operation 
	    	   30000			//time out for bus busy 
	    	   );   
           
	/*设置AIC23的数字接口*/ 
	MyI2C_Write( Digital_Audio_Inteface_Format,//pointer to data array 
	    	   2,				//length of data to be transmitted 
	    	   1,				//master or slaver 
	    	   CODEC_ADDR,	    //slave address to transmit to 
	    	   1,				//transfer mode of operation 
	    	   30000			//time out for bus busy 
	    	   ); 
 
	 /*设置AIC23模拟通路*/ 
	MyI2C_Write( Analog_Aduio_Path_Control,//pointer to data array 
	    	   2,				//length of data to be transmitted 
	    	   1,				//master or slaver 
	    	   CODEC_ADDR,	    //slave address to transmit to 
	    	   1,				//transfer mode of operation 
	    	   30000			//time out for bus busy 
	    	   ); 
 
	/*设置数字通路*/ 
	MyI2C_Write( Digital_Audio_Path_Control,//pointer to data array 
	    	   2,				//length of data to be transmitted 
	    	   1,				//master or slaver 
	    	   CODEC_ADDR,	    //slave address to transmit to 
	    	   1,				//transfer mode of operation 
	    	   30000			//time out for bus busy 
	    	   );     
		  
	/*设置AIC23的采样率*/ 
	MyI2C_Write( Sample_Rate_Control,//pointer to data array 
	    	   2,				//length of data to be transmitted 
	    	   1,				//master or slaver 
	    	   CODEC_ADDR,	    //slave address to transmit to 
	    	   1,				//transfer mode of operation 
	    	   30000			//time out for bus busy 
	    	   ); 
 
	/*设置耳机音量*/ 
	MyI2C_Write( Left_Headphone_Volume_Control,//pointer to data array 
	    	   2,				//length of data to be transmitted 
	    	   1,				//master or slaver 
	    	   CODEC_ADDR,	    //slave address to transmit to 
	    	   1,				//transfer mode of operation 
	    	   30000			//time out for bus busy 
	    	   ); 
 
	/*设置Line输入的音量*/ 
	MyI2C_Write( Left_Line_Input_Volume_Control,//pointer to data array 
	    	   2,				//length of data to be transmitted 
	    	   1,				//master or slaver 
	    	   CODEC_ADDR,	    //slave address to transmit to 
	    	   1,				//transfer mode of operation 
	    	   30000			//time out for bus busy 
	    	   ); 
 
	/*启动AIC23*/ 
	MyI2C_Write( Digital_Interface_Activation,//pointer to data array 
	    	   2,				//length of data to be transmitted 
	    	   1,				//master or slaver 
	    	   CODEC_ADDR,	    //slave address to transmit to 
	    	   1,				//transfer mode of operation 
	    	   30000			//time out for bus busy 
	    	   ); 
	    	    
/*------------------------------------------------------------------------------------*/	    	        	    
// Receive the ADC output data of CODEC   
// Then output the received data to DAC of CODEC  
/*------------------------------------------------------------------------------------*/ 
 
while(TRUE) 
	{ 
    /* 左通路数据 */ 
	while(!MCBSP_rrdy(hMcbsp)){}; 
	DataTempLeft = MCBSP_read16(hMcbsp); 
     
    /* 右通路数据 */ 
    while(!MCBSP_rrdy(hMcbsp)){}; 
    DataTempRight = MCBSP_read16(hMcbsp); 
    	 
	/* 左声道耳机输出 */ 
	while(!MCBSP_xrdy(hMcbsp)) {}; 
	MCBSP_write16(hMcbsp,DataTempLeft); 
	 
	/* 右声道耳机输出 */ 
	while(!MCBSP_xrdy(hMcbsp)) {}; 
	MCBSP_write16(hMcbsp,DataTempRight);  
	};     	   	    	       	         	   	    	    
} 
 
/******************************************************************************/ 
//	No more 
/******************************************************************************/