www.pudn.com > s3c2410_iis.rar > iis_test.c


/********************************************************************************************* 
* File£º	iis.c 
* Author:	embest	 
* Desc£º	IIS sound circuit code 
* History:	 
*********************************************************************************************/ 
 
/*------------------------------------------------------------------------------------------*/ 
/*	 								include files						 				    */ 
/*------------------------------------------------------------------------------------------*/ 
#include "2410lib.h" 
#include "iis_test.h" 
 
/*------------------------------------------------------------------------------------------*/ 
/*	 								constant define						 				    */ 
/*------------------------------------------------------------------------------------------*/ 
#define PLAY 		0 
#define RECORD 		1 
#define REC_LEN		0xf0000 
#ifdef BOARDTEST_EXH 
#undef BOARDTEST 
#endif	 
 
/*------------------------------------------------------------------------------------------*/ 
/*	 								extern variables						 			    */ 
/*------------------------------------------------------------------------------------------*/ 
extern const UINT8T g_ucWave[155760]; 
 
/*------------------------------------------------------------------------------------------*/ 
/*	 								global variables						 			    */ 
/*------------------------------------------------------------------------------------------*/ 
int   f_nDMADone_1; 
int   f_nDMADone_2; 
/********************************************************************************************* 
* name:		iis_test 
* func:		Test IIS circuit 
* para:		none 
* ret:		none 
* modify: 
* comment:		 
*********************************************************************************************/ 
void iis_test(void) 
{ 
	UINT8T	ucInput; 
	int nSoundLen=155956; 
	 
	uart_printf("\n IIS test example\n"); 
    iis_init();							// initialize IIS 
 
    uart_printf(" Menu(press digital to select):\n"); 
    uart_printf(" 1: play wave file \n"); 
    uart_printf(" 2: record and play\n"); 
     
	g_nKeyPress = 1;					// only for board test to select and exit 
	while((ucInput != '1') & (ucInput != '2')) 
	{ 
   		ucInput = uart_getkey(); 
   		if(g_nKeyPress!=1)				// SB1202/SB1203 to exit board test 
   		{ 
   			ucInput='1';				// any select "Play wav" 
   			break; 
   		} 
	}; 
	 
	uart_printf(" %c\n",ucInput); 
	if(ucInput == 0x31) 
	{ 
#ifndef BOARDTEST 
		memcpy((void *)0x30200000, g_ucWave, nSoundLen);	 
#endif 
		iis_play_wave(1,(UINT8T *)0x30200000,nSoundLen);//		nSoundLen = 155956;  
	} 
	if(ucInput == 0x32) 
		iis_record(); 
 
	uart_printf(" end.\n"); 
    iis_close();						// close IIS 
} 
 
/********************************************************************************************* 
* name:		iis_init 
* func:		Initialize IIS circuit 
* para:		none 
* ret:		none 
* modify: 
* comment:		 
*********************************************************************************************/ 
void iis_init(void) 
{ 
	//---------------------------------------------------------- 
	//   PORT B GROUP 
	//Ports  :   GPB4    GPB3   GPB2   
	//Signal :  L3CLOCK L3DATA L3MODE 
	//Setting:   OUTPUT OUTPUT OUTPUT  
	//           [9:8]   [7:6}  [5:4] 
	//Binary :     01  ,   01    01  
	//----------------------------------------------------------     
    rGPBUP  = rGPBUP  & ~(0x7<<2) | (0x7<<2);	//The pull up function is disabled GPB[4:2] 1 1100     
    rGPBCON = rGPBCON & ~((1<<9)|(1<<7)|(1<<5)) | (1<<8)|(1<<6)|(1<<4); //GPB[4:2]=Output(L3CLOCK):Output(L3DATA):Output(L3MODE) 
 
	//---------------------------------------------------------- 
	//   PORT E GROUP 
	//Ports  :  GPE4    GPE3   GPE2  GPE1    GPE0  
	//Signal : I2SSDO  I2SSDI CDCLK I2SSCLK I2SLRCK  
	//Binary :   10  ,   10     10 ,  10      10     
	//---------------------------------------------------------- 
    rGPEUP  = rGPEUP | 0x1f;    //The pull up function is disabled GPE[4:0] 1 1111 
    rGPECON = rGPECON & ~((1<<8)|(1<<6)|(1<<4)|(1<<2)|(1<<0)) | (1<<9)|(1<<7)|(1<<5)|(1<<3)|(1<<1); 
    //GPE[4:0]=I2SSDO:I2SSDI:CDCLK:I2SSCLK:I2SLRCK 
	 
	f_nDMADone_1= 0; 
	f_nDMADone_2= 0; 
	init_1341(PLAY);							// initialize philips UDA1341 chip 
} 
 
/********************************************************************************************* 
* name:		iis_close 
* func:		Close IIS circuit 
* para:		none 
* ret:		none 
* modify: 
* comment:		 
*********************************************************************************************/ 
void iis_close() 
{ 
    rIISCON  = 0x0;								// IIS stop 
    rIISFCON = 0x0;							// For FIFO flush 
	rINTMSK |= BIT_DMA2;						// Mask interrupt 
	rINTMSK |= BIT_DMA1; 
} 
 
/********************************************************************************************* 
* name:		iis_play_wave 
* func:		play wave data 
* para:		nTimes 	--	input, play times 
* ret:		none 
* modify: 
* comment:		 
*********************************************************************************************/ 
void iis_play_wave(int nTimes,UINT8T *pWavFile, int nSoundLen) 
{ 
	int	i,j;	 
 
	ClearPending(BIT_DMA2);						 
	rINTMOD = 0x0; 
	 
	// initialize philips UDA1341 chip 
	init_1341(PLAY);	 
 
    // set BDMA interrupt 
	pISR_DMA2 = (unsigned)dma2_done; 
	rINTMSK  &= ~(BIT_DMA2);  //19λÖÃ0 
  
 	for(i=nTimes; i!=0; i--) 
	{ 
		// initialize variables 
		f_nDMADone_2 = 0; 
		 
		//DMA2 Initialize 
		rDISRCC2 = (0<<1) + (0<<0);             //AHB, Increment 
		rDISRC2  = ((INT32T)(pWavFile));                     
		rDIDSTC2 = (1<<1) + (1<<0);             //APB, Fixed 
		rDIDST2  = ((INT32T)IISFIFO);           //IISFIFO 
		//rDCON1   = (1<<31)+(0<<30)+(1<<29)+(0<<28)+(0<<27)+(2<<24)+(1<<23)+(1<<22)+(1<<20)+REC_LEN/2; 
		rDCON2   = (1<<31)+(0<<30)+(1<<29)+(0<<28)+(0<<27)+(0<<24)+(1<<23)+(0<<22)+(1<<20)+nSoundLen/2; 
 
		//Handshake, sync PCLK, TC int, single tx, single service, I2SSDO, I2S request,  
		//Auto-reload, half-word, size/2 
		rDMASKTRIG2 = (0<<2)+(1<<1)+0;    		//No-stop, DMA2 channel on, No-sw trigger  
 
		//IIS Initialize 
		//Master,Tx,L-ch=low,iis,16bit ch.,CDCLK=384fs,IISCLK=32fs 
		rIISCON = (1<<5)+(0<<4)+(0<<3)+(1<<2)+(1<<1); 
		//output mode, transmit mode, low for left channel, IIS compatible format, 16-BIt, 384fs, 32fs 
		rIISMOD = (0<<8) + (2<<6) + (0<<5) + (0<<4) + (1<<3) + (1<<2) + (1<<0);       
		rIISPSR = (2<<5) + 2;            		//Prescaler_A/B=3  
	 
		//Tx DMA enable,Rx DMA disable,Tx not idle,Rx idle,prescaler enable,stop 
		rIISFCON = (1<<15) + (1<<13);       	//Tx DMA,Tx FIFO --> start piling.... 
	 
	    rIISCON |= 0x1;             			// enable IIS    
	    while( f_nDMADone_2 == 0)      			// DMA end 
	    { 
	    j=rDSTAT2&0xfffff;	   
        uart_printf(" rDSTAT2=%d\n",j);   
		    	if(uart_getkey()){ 
			delay(10); 
    		f_nDMADone_1 = 1; 
    		//break; 
    		} 
        } 
 
	    rINTMSK |= BIT_DMA2;	     
	    rIISCON = 0x0;    						// IIS stop 
	} 
} 
 
/********************************************************************************************* 
* name:		iis_record 
* func:		record and play wave file 
* para:		nTimes 	--	input, play times 
* ret:		none 
* modify: 
* comment:		 
*********************************************************************************************/ 
void iis_record(void) 
{ 
 
	UINT8T * pRecBuf,ucInput; 
	int nSoundLen; 
    int i; 
     
	// enable interrupt 
	ClearPending(BIT_DMA1);						 
	rINTMOD=0x0; 
 
	//----------------------------------------------------------------// 
	//						record									  // 
	//----------------------------------------------------------------// 
    uart_printf(" Start recording....\n");   
	 
    pRecBuf = (unsigned char *)0x30200000; 		// for download 
    for(i= (UINT32T)pRecBuf; i<((UINT32T)pRecBuf+REC_LEN+0x20000); i+=4) 
    { 
    	*((volatile unsigned int*)i)=0x0;     
	} 
 
    init_1341(RECORD); 
 
    // set BDMA interrupt 
	f_nDMADone_1= 0; 
    pISR_DMA1 = (unsigned)dma1_done; 
    rINTMSK  &= ~(BIT_DMA1); 
 
	//--- DMA1 Initialize 
	rDISRCC1 = (1<<1) + (1<<0);            //IISFIFO     //APB, Fix   
	rDISRC1  = ((UINT32T)IISFIFO);              //IISFIFO 
	rDIDSTC1 = (0<<1) + (0<<0);                 //AHB, Increment 
	rDIDST1  = ((int)pRecBuf);                   
	//Handshake, sync APB, TC int, single tx, single service, I2SSDI, I2S Rx request,  
	//Off-reload, half-word, 0x50000 half word. 
	rDCON1   = (1<<31)+(0<<30)+(1<<29)+(0<<28)+(0<<27)+(2<<24)+(1<<23)+(1<<22)+(1<<20)+REC_LEN/2; 
	//No-stop, DMA1 channel on, No-sw trigger 
	rDMASKTRIG1 = (0<<2) + (1<<1) + 0;     
	 
	//IIS Initialize 
	//Master,Rx,L-ch=low,IIS,16bit ch,CDCLK=384fs,IISCLK=32fs 
	rIISCON = (0<<5) + (1<<4) + (1<<3) + (0<<2) + (1<<1); 
	//master mode, receive mode, Low left, IIS compatible format, 16 bit, 384fs, 32fs 
	rIISMOD = (0<<8) + (1<<6) + (0<<5) + (0<<4) + (1<<3) + (1<<2) + (1<<0); 
	rIISPSR = (2<<5) + 2; 	 
	//Tx DMA disable,Rx DMA enable,Tx idle,Rx not idle,prescaler enable,stop 
	rIISFCON = (1<<14) + (1<<12);   			//Rx DMA,Rx FIFO --> start piling.... 
	rIISCON |= 0x1;             				// enable IIS    
    uart_printf(" Press any key to end recording\n");   
 
    while(f_nDMADone_1== 0) 
    { 
    	if(uart_getkey()){ 
			//delay(10); 
    		f_nDMADone_1 = 1; 
    		//break; 
    		} 
	}     
    rINTMSK |= BIT_DMA1; 
    rIISCON = 0x0;								// IIS stop 
    delay(10); 
 
    uart_printf(" End of record!!!\n"); 
    uart_printf(" Press any key to play record data!!!\n"); 
    while(!uart_getkey()); 
 
	//----------------------------------------------------------------// 
	//						play									  // 
	//----------------------------------------------------------------// 
	iis_play_wave(1,pRecBuf, REC_LEN); 
    	     
    rINTMSK |= BIT_DMA1; 
    rIISCON = 0x0;    							// IIS stop 
    uart_printf(" Play end!!!\n"); 
} 
 
/********************************************************************************************* 
* name:		bdma0_done 
* func:		BDMA0 interrupt handler 
* para:		none 
* ret:		none 
* modify: 
* comment:		 
*********************************************************************************************/ 
void dma1_done(void) 
{ 
int j; 
j=rINTPND; 
uart_printf(" interrupt accur!!!\n"); 
       uart_printf(" rINTPND=%d\n",j);   
  
	    rINTMSK |= BIT_DMA1;	 
	ClearPending(BIT_DMA1);						// clear pending bit 
    f_nDMADone_1 = 1; 
} 
 
void dma2_done(void) 
{ 
int j; 
j=rINTPND; 
uart_printf(" interrupt accur!!!\n"); 
       uart_printf(" rINTPND=%d\n",j);   
  
	    rINTMSK |= BIT_DMA2;	 
	ClearPending(BIT_DMA2);						// clear pending bit 
    f_nDMADone_2 = 1; 
}