#include "44b.h"
#include "44blib.h"
#include "def.h"
#include "iis.h"
void _WrL3Addr(U8 data);
void _WrL3Data(U8 data,int halt);
void __irq BDMA0_PLAY(void);
void __irq BDMA0_Done(void);
void Init1341(char);
#define L3D (0x200)
#define L3M (0x10)
#define L3C (0x20)
#define TESTSIZE 700000//1000//100
//for test
//static short int Buf[TESTSIZE];
//for down
unsigned char *Buf,*_temp;
unsigned char *rec_buf;
#define PLAY 0
#define RECORD 1
#define REC_LEN 0xF0000
volatile unsigned int size=0;
volatile unsigned int leng,ssize;
volatile char Rec_Done=0;
volatile char mute=1;
void Test_Iis(void)
{
U32 i;
ChangePllValue(0x49,0x7,0x1); //MCLK=45.1584MHz <-- 5.6448MHz*8
Uart_Init(45000000,115200);
Uart_Printf("S3C44B0 IIS Test\n");
rPCONE=(rPCONE&0xffff)+(2<<16); //PE:CODECLK
pISR_BDMA0=(unsigned)BDMA0_Done;
//for down
rINTMSK&=~(BIT_GLOBAL|BIT_BDMA0);
Buf=(unsigned char *)0xc400000;
_temp=(unsigned char *)0x14b000;
for(i=0XC400000;i<(0XC400000+0XF0000);i+=4)
{
*((volatile unsigned *)i)=0x0;
}
//Uart_Printf("Download the PCM(no ADPCM) wave file by wkcom2(with header)!!\n");
while ((unsigned int)Buf<(0XC400000+0XF0000))
{
(*Buf)=(*_temp);
Buf++;
_temp++;
}
Buf=(unsigned char *)0xc400000;
size=*(Buf) | *(Buf+1)<<8 | *(Buf+2)<<16 | *(Buf+3)<<24;
leng=0xf0000;
// size=*(Buf+0x2c) | *(Buf+0x2d)<<8 | *(Buf+0x2e)<<16 | *(Buf+0x2f)<<24;
size=(size>>1)<<1;
//Uart_Printf("sample size=0x%x\n",size/2);
Uart_Printf("\nNow play the wave file\n");
Init1341(PLAY);
/****** BDMA0 Initialize ******/
//for down
rBDISRC0=(1<<30)+(1<<28)+(int)(Buf+0x30); //Half word,inc,Buf
rBDIDES0=(1<<30)+(3<<28)+((int)rIISFIF); //M2IO,fix,IISFIF
rBDICNT0=(1<<30)+(1<<26)+(3<<22)+(1<<21)+(1<<20)+size;
rBDCON0 = 0x0<<2;
/****** IIS Initialize ******/
rIISCON=0x22; //Tx DMA enable,Rx idle,prescaler enable
rIISMOD=0x89; //Master,Tx,L-ch=low,iis,16bit ch.,codeclk=256fs,lrck=32fs
rIISPSR=0x33; //Prescaler_A/B enable, value=3
rIISFCON=0xa00; //Tx/Rx DMA,Tx/Rx FIFO --> start piling....
Uart_Printf("Push any key to exit!!!\n");
/****** IIS Tx Start ******/
rIISCON |=0x1;
Uart_Getch();
/****** IIS Tx Stop ******/
rIISCON=0x0; //IIS stop
rBDICNT0=0x0; //BDMA stop
//for down
free(Buf);
Cache_Flush();
rNCACHBE0=0x0;
size=0;
rINTMSK|=BIT_BDMA0;
ChangePllValue(PLL_M,PLL_P,PLL_S); //MCLK=40MHz
Uart_Init(0,115200);
}
void Init1341(char mode)
{
/****** Port Initialize ******/
rPCONA = 0x1ff; //PA9(out):L3D
rPCONB = 0x7CF; //PB4:L3M, PB5:L3C
rPDATB = L3M|L3C; //L3M=H(start condition)
//L3C=H(start condition)
/****** L3 Interface ******/
_WrL3Addr(0x14+2); //status (000101xx+10)
#ifdef FS441KHZ
_WrL3Data(0x60,0); //0,1,10,000,0 reset,256fs,no DCfilter,iis
#else
_WrL3Data(0x40,0); //0,1,00,000,0 reset,512fs,no DCfilter,iis
#endif
_WrL3Addr(0x14+2); //status (000101xx+10)
#ifdef FS441KHZ
_WrL3Data(0x20,0); //0,0,10,000,0 no reset,256fs,no DCfilter,iis
#else
_WrL3Data(0x00,0); //0,0,00,000,0 no reset,512fs,no DCfilter,iis
#endif
_WrL3Addr(0x14+2); //status (000101xx+10)
_WrL3Data(0x81,0);
// //1,0,0,0,0,0,11 OGS=0,IGS=0,ADC_NI,DAC_NI,sngl speed,AonDon
_WrL3Addr(0x14+0); //DATA0 (000101xx+00)
_WrL3Data(0x00,0);
//record
if(mode)
{
_WrL3Addr(0x14+2); //STATUS (000101xx+10)
_WrL3Data(0xa2,0); ////1,0,1,0,0,0,10 : OGS=0,IGS=1,ADC_NI,DAC_NI,sngl speed,AonDoff
_WrL3Addr(0x14+0); //DATA0 (000101xx+00)
_WrL3Data(0xc2,0); //11000,010 : DATA0, Extended addr(010)
_WrL3Data(0x45,0); //010,001,01 : DATA0, MS=0dB, Ch1=on Ch2=off,
}
//record
}
void _WrL3Addr(U8 data)
{
S32 i,j;
rPDATB &= (~L3M); //L3M=L
rPDATB |= L3C; //L3C=H
for(j=0;j<4;j++); //tsu(L3) > 190ns
//PA9:L3D PB4:L3M PB5:L3C
for(i=0;i<8;i++)
{
if(data&0x1)//if data bit is 'H'
{
rPDATB &= (~L3C); //L3C=L
rPDATA |= L3D; //L3D=H
for(j=0;j<4;j++); //tcy(L3) > 500ns
rPDATB |= L3C; //L3C=H
rPDATA |= L3D; //L3D=H
for(j=0;j<4;j++); //tcy(L3) > 500ns
}
else //if data bit is 'L'
{
rPDATB&=(~L3C); //L3C=L
rPDATA&=(~L3D); //L3D=L
for(j=0;j<4;j++); //tcy(L3) > 500ns
rPDATB|=L3C; //L3C=H
rPDATA&=(~L3D); //L3D=L
for(j=0;j<4;j++); //tcy(L3) > 500ns
}
data >>=1;
}
rPDATB |=L3C|L3M; //L3M=H,L3C=H
}
void _WrL3Data(U8 data,int halt)
{
U32 vPdata = 0x0; //L3D=L
U32 vPdatb = 0x0; //L3M/L3C=L
S32 i,j;
if(halt)
{
rPDATB|=L3C; //L3C=H(while tstp, L3 interface halt condition)
for(j=0;j<4;j++); //tstp(L3) > 190ns
}
rPDATB|=L3C|L3M; //L3M=H(in data transfer mode)
for(j=0;j<4;j++); //tsu(L3)D > 190ns
//PA9:L3MODE PG6:L3DATA PG7:L3CLOCK
for(i=0;i<8;i++)
{
if(data&0x1) //if data bit is 'H'
{
// rPDATB=L3M; //L3C=L
rPDATB&=(~L3C);
rPDATA|=L3D; //L3D=H
for(j=0;j<4;j++); //tcy(L3) > 500ns
rPDATB=L3C|L3M; //L3C=H,L3D=H
rPDATA=L3D;
for(j=0;j<4;j++); //tcy(L3) > 500ns
}
else //if data bit is 'L'
{
rPDATB&=(~L3C); //L3C=L
rPDATA&=(~L3D); //L3D=L
for(j=0;j<4;j++); //tcy(L3) > 500ns
rPDATB|=L3C|L3M; //L3C=H
rPDATA&=(~L3D); //L3D=L
for(j=0;j<4;j++); //tcy(L3) > 500ns
}
data>>=1;
}
rPDATB|=L3C|L3M; //L3M=H,L3C=H
}
void __irq BDMA0_Done(void)
{
rI_ISPC=BIT_BDMA0; //clear pending bit
WrUTXH0('&');
rIISCON|=0x1;
}