📄 iis.c
字号:
//=================================
//File name: iis.c
//Discription:provide some iis test method use by the main.c file
//Author: Decell.Zhou
//version
//=================================
#include "iis.h"
//this variable indicate the 1341 mute state,"0" stands for unmute,"1" stands for mute
static char gMute;
//the boolean variable indicate the if DMA transfer has been complete
static char gbDMAStop;
//the variable indicate the current completed tranfer size
static unsigned long gCompletedSize;
//the file size we need to transfer via DMA
static unsigned long gDMACounter;
//===================================================================
void iisWriteL3Addr(unsigned char data)
{
char i,j;
//Uart_Printf("Addr: 0x%x\n",data);
//then generate the set-up signal,for address transmit mode
rGPBDAT =rGPBDAT & (~(L3M|L3C|L3D)) | (L3C) ;//L3CLK=1,L3DAT=0,L3MOD =0
for(j=0;j<4;j++);//tsu(L3)A = 190ns,1/135M = 7ns, 7cycles for one loop,
//49ns/loop,4 loops for 190ns
//in L3 bus we send bit 0 first
for(i=0;i<8;i++)
{
if(data & 0x1)//if the bit is 1
{
rGPBDAT &= ~(L3C);//the first half of the cycle
rGPBDAT |= L3D;//L3D=H,because the bit is 1
for(j=0;j<4;j++);//tcy(L3)/2 = 250ns
rGPBDAT |= L3C;//the second half of the cycle
rGPBDAT |= L3D;//L3D=H,because the bit is 1
for(j=0;j<4;j++);//tcy(L3)/2 = 250ns
}
else//if the bit is 0
{
rGPBDAT &= ~L3C;//the first half of the cycle
rGPBDAT &= ~L3D;//L3D=L,because the bit is 0
for(j=0;j<4;j++);//tcy(L3)/2 = 250ns
rGPBDAT |= L3C;//the second half of the cycle
rGPBDAT &= ~L3D;//L3D=L,because the bit is 0
for(j=0;j<4;j++);//tcy(L3)/2 = 250ns
}
data >>= 1;
}
rGPBDAT = rGPBDAT & (~(L3D|L3M|L3C)) | (L3C|L3M);//stop the address mode
}
//===================================================================
void iisWriteL3Data(unsigned char data)
{
char i,j;
//Uart_Printf("Addr: 0x%x\n",data);
//first generate the halt signal,that is tstp(L3)
rGPBDAT = rGPBDAT & (~(L3D | L3M | L3C)) | (L3C);//L3M and L3D is "0" while L3C is "1"
for(j=0;j<4;j++);//tstp(L3)=190ns
//then generate the set-up signal,for data transmit mode
rGPBDAT = rGPBDAT & (~(L3D | L3M | L3C)) | (L3C | L3M);//L3M and L3C is "1" while L3D is "0"
for(j=0;j<4;j++);//tsu(L3)D=190ns
//we send bit0 first in L3 bus
for(i=0;i<8;i++)
{
if(data & 0x1)//if the data bit is "1"
{
//the first half of the cycle
rGPBDAT &= ~L3C;//L3C="0"
rGPBDAT |= L3D;//L3D="1"
for(j=0;j<4;j++);//tcy(L3)/2=250ns
//the second half of the cycle
rGPBDAT |= L3C;//L3C="1"
rGPBDAT |= L3D;//L3D="1"
for(j=0;j<4;j++);//tcy(L3)/2=250ns
}
else//if the data bit is "0"
{
//the first half of the cycle
rGPBDAT &= ~L3C;//L3C="0"
rGPBDAT &= ~L3D;//L3D="0"
for(j=0;j<4;j++);//tcy(L3)/2=250ns
//the first half of the cycle
rGPBDAT |= L3C;//L3C="1"
rGPBDAT &= ~L3D;//L3D="0"
for(j=0;j<4;j++);//tcy(L3)/2=250ns
}
data >>= 1;
}
rGPBDAT = rGPBDAT & ~(L3D | L3M | L3C) | (L3C | L3M);//stop the data mode
}
//===========[iisEint0ISR]==========
//Discription:the isr for the eint0,make the 1341 into mute state
//Author:Decell.Zhou
//Version:
//parm:
//return:
//==================================
static void __irq iisEint0ISR(void){
rSRCPND = (0x1 << 0);//clear pending bit
rINTPND = (0x1 << 0);
rINTMSK |= (0x1 << 0);//disable the interrupt
if(gMute == 0){//we are going to mute state
gMute =1;
iisWriteL3Addr(0x14);//enter data0 mode
iisWriteL3Data(0xa4);//10,1,00,1,00:data0, direct addressing ,after the tone features,no de-emphasis,mute,flat
Uart_SendString("----mute----\n");
}else{
gMute = 0;
iisWriteL3Addr(0x14);//enter data0 mode
iisWriteL3Data(0xa0);//10,1,00,0,00:data0, direct addressing ,after the tone features,no de-emphasis, no mute,flat
Uart_SendString("---- no mute----\n");
}
for(;(rSRCPND & (0x1 << 0)) == 1;){
rSRCPND = (0x1 << 0);//clear pending bit
}
rINTMSK &= ~(0x1 << 0);//enable the interrupt
}
//===========[iisDMA2PlayISR]========
//Dicription:the isr for DMA when 1341 is in the play mode
//Author:Decell.Zhou
//Version:
//parm:none
//return:none
//===================================
static void __irq iisDMA2PlayISR(void){
rINTMSK |= (0x1 << 19);
rSRCPND |= (0x1 << 19);
rINTPND |= (0x1 << 19);
gCompletedSize += 0x10000;
if(gCompletedSize >= gDMACounter){
gbDMAStop = 1;//stop the DMA transfer
gCompletedSize = 0;
}else{
rINTMSK &= ~(0x1 << 19);
}
Uart_Printf("DMA count: 0x%x\n",rDSTAT2 & 0xfffff);
Uart_SendString("DMA INT!...\n");
rGPFDAT |= ((0x1 << 7)|(0x1 << 4));//set the out put the high, turn off the LED
}
//===========[iisDMA2RecordISR]========
//Dicription:the isr for DMA when 1341 is in the record mode
//Author:Decell.Zhou
//Version:
//parm:none
//return:none
//===================================
static void __irq iisDMA2RecordISR(void){}
//===========[iisInitPort]==========
//Discription: initial the GPE for the iis function,GPB for L3,GPF for eint
//Author:Decell.Zhou
//Version:
//parm:none
//return:none
//==================================
void iisInitPort(void){
//set GPB2,GPB3,GPB4 as L3MOD,L3DATA,L3CLOCK
rGPBCON |=((0x2 << 8)|(0x2 << 6)|(0x2 << 4));
//disable GPB2-GPB3 pull-up resistor
rGPBUP |= (0x1 << 4)|(0x1 << 3)|(0x1 << 2);
//Start condition : L3M=H, L3C=H
rGPBDAT = rGPBDAT & ~(L3M|L3C|L3D) |(L3M|L3C);
//set GPE0,GPE1,GPE2,GPE3,GPE4 as I2SLRCLK,I2SSCLK,I2SCDCLK,I2SSDI,I2SSDO
rGPECON |=((0x2 << 0)|(0x2 << 2)|(0x2 << 4)|(0x2 << 6)|(0x2 << 8));
//disable the pull-up resistor for the GPE0-GPE4
rGPEUP |= (0x1 << 0)|(0x1 << 1)|(0x1 << 2)|(0x1 << 3)|(0x1 << 4);
//set GPF0 as EINT0
rGPFCON |= (0x2 << 0);
//disable the pull-up resistor for GPF0
rGPFUP |= (0x1 << 0);
}
//===========[iis1341SetMode]===========
//Discription: set up the UDA1341 work mode accroding to the argument
//Author:Decell.Zhou
//Version:
//parm:
// 0|char|play mode
// 1|char|record mode
//return:none
//======================================
void iis1341SetMode(char argMode){
char i;
int j;
rGPBDAT = rGPBDAT & ~(L3M|L3C|L3D) |(L3M|L3C); //Start condition : L3M=H, L3C=H
rGPBUP = rGPBUP & ~(0x7<<2) |(0x7<<2); //The pull up function is disabled GPB[4:2] 1 1100
rGPBCON = rGPBCON & ~(0x3f<<4) |(0x15<<4); //GPB[4:2]=Output(L3CLOCK):Output(L3DATA):Output(L3MODE)
//Start condition : L3M=H, L3C=H
//rGPBDAT = rGPBDAT & ~(L3M|L3C|L3D) |(L3M|L3C);
//for(j = 0;j<1000;j++);
if(argMode == PLAY_MODE){//for play mode
iisWriteL3Addr(0x16);// enter STATUS mode (00010100+10)
iisWriteL3Data(0x60);//0,1,10,000,0:Reset,256fs,no DCfilter,iis format
iisWriteL3Addr(0x16);//enter STATUS (00010100+10)
iisWriteL3Data(0x20);//0,0,10,000,0:No reset,256fs,no DCfilter,iis format
iisWriteL3Addr(0x16);//enter STATUS (00010100+10)
iisWriteL3Data(0x81);//1,0,0,0,0,0,01:OGS=0,IGS=0,ADC_NI,DAC_NI,single speed,ADC off,DAC on
Uart_SendString("1341 init play...!\n");
}else{//for recoed mode
iisWriteL3Addr(0x16);// enter STATUS mode (00010100+10)
iisWriteL3Data(0x60);//0,1,10,000,0:Reset,256fs,no DCfilter,iis format
iisWriteL3Addr(0x16);//enter STATUS (00010100+10)
iisWriteL3Data(0x20);//0,0,10,000,0:No reset,256fs,no DCfilter,iis format
iisWriteL3Addr(0x16);//enter STATUS (00010100+10)
iisWriteL3Data(0xa2);//1,0,0,0,0,0,10:OGS=0,IGS=1,ADC_NI,DAC_NI,single speed,ADC on,DAC off
iisWriteL3Addr(0x14);//enter DATA0 (00010100+00)
iisWriteL3Data(0xc2); //11000,010: DATA0, Extended addressing,extended address=010
iisWriteL3Data(0xed);//111,011,01 : DATA0,Extended data, Mic Sensitivity =9dB, Ch1=on Ch2=off,
Uart_SendString("1341 init record...!\n");
}
}
//===========[iisInitDMA]============
//Discription:init the DMA setting,then start the DMA operation
//Author:Decell.Zhou
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -