📄 codec.c
字号:
int i;
if(Channel_sel>3)return 0;
for(i=0;i<4;i++)
{
tp = readreg(Channel_sel,R5B,1);
if(((tp &0x80) ==0x00 )&& ((tp &0x40) ==0x40 ))
break;
}
#ifdef CODEC_DEBUG
printf("fun:HW_Get_DAC_Gain get reg R5B ori val=0x%x\n",tp);
#endif
if(tp == 0x5F) tp = 0xff;
else if ( tp == 0x5e ) tp = 54;
else if (tp == 0x5d) tp = 48;
else tp = (tp&0xbF)*1.5;
#ifdef CODEC_DEBUG
printf("fun:HW_Get_DAC_Gain gain=0x%d\n",tp);
#endif
return tp;
}
/*************************************************************
*The routine set ADC gain level
*Input:Codec_Sel. 0 means CODEC 1, 1 means CODEC 2.
Gain_level.it ranges from 0~54. 0 means 0db.54 means -54db.0 is the largest
*Output:void
*
*
***************************************************************/
void HW_Set_ADC_Gain(unsigned char Channel_sel,unsigned char Gain_level)
{
unsigned char temp[2];
unsigned char channel_adr;
if(Channel_sel>3)return;
temp[0] = R5A;
channel_adr = 0x80+(3-Channel_sel)*2;/*cal codec channel address*/
if(Gain_level > 0x54)
temp[1] = 0x1f;
else if((Gain_level>48) && (Gain_level <=54))
temp[1] = 0x1E;
else if((Gain_level>42) && (Gain_level <=48 ))
temp[1] = 0x1D;
else
temp[1] = Gain_level /1.5;
temp[1] &=0x3f;
#ifdef CODEC_DEBUG
printf("fun:HW_Set_ADC_Gain set reg R5A val=0x%x\n",temp[1]);
#endif
I2CWrite( channel_adr, temp, 2);
}
/*************************************************************
*The routine set DAC gain level
*Input:Codec_Sel. 0 means CODEC 1, 1 means CODEC 2.
Gain_level. it ranges from 0~54. 0 means 0db.54 means -54db.0 is the largest
*Output:void
*
*
***************************************************************/
void HW_Set_DAC_Gain(unsigned char Channel_sel,unsigned char Gain_level)
{
unsigned char temp[2];
unsigned char channel_adr;
if(Channel_sel>3)return;
temp[0] = R5B;
channel_adr = 0x80+(3-Channel_sel)*2;/*cal codec channel address*/
if(Gain_level > 0x54)
temp[1] = 0x5f;
else if((Gain_level>48) && (Gain_level <=54))
temp[1] = 0x5E;
else if((Gain_level>42) && (Gain_level <=48 ))
temp[1] = 0x5D;
else
temp[1] = Gain_level /1.5;
temp[1] |=0x40;
#ifdef CODEC_DEBUG
printf("fun:HW_Set_DAC_Gain set reg R5B val=0x%x\n",temp[1]);
#endif
I2CWrite( channel_adr, temp, 2);
}
/*************************************************************
*The routine set analog side tone control gain level
*Input:Codec_Sel. 0 means CODEC 1, 1 means CODEC 2.
Gain_level.Gain level is eight grade, so it's range is 0~7.
0 is the largest. 0 is -9db.
1 is -12db.
(Then decreasing in turn -3db.)
*Output:void
*
*
*set R5C = 10xx-xuuu
***************************************************************/
void HW_Set_Analog_Sidetone(unsigned char Channel_sel,unsigned char Gain_level )
{
unsigned char temp[2];
unsigned char tp = 0;
unsigned char channel_adr;
int i;
if(Channel_sel>1)return;
if(Gain_level>7)return;
temp[0] = R5C;
channel_adr = 0x80+(3-Channel_sel)*2;/*cal codec channel address*/
for(i=0;i<4;i++)
{
tp = readreg(Channel_sel,R5C,1);
if(((tp &0x80 )==0x80 )&& ((tp &0x40) ==0x00 ))/*test tp= 10uu-uuuu*/
break;
}
tp &=0x87;/*set R5C(d5,d4,d3)<---0*/
temp[1] = (Gain_level<<3)|tp;
#ifdef CODEC_DEBUG
printf("fun:HW_Set_Analog_Sidetone set reg R5C val=0x%x\n",temp[1]);
#endif
I2CWrite(channel_adr,temp,2);
}
/*************************************************************
*The routine set digital side tone control gain level
*Input:Codec_Sel. 0 means CODEC 1, 1 means CODEC 2.
Gain_level.Gain level is eight grade, so it's range is 0~7.
0 is the largest. 0 is -9db.
1 is -12db.
(Then decreasing in turn -3db.)
*Output:void
*
*
* set R5C = 10uu-uxxx
***************************************************************/
void HW_Set_Digital_Sidetone(unsigned char Channel_sel,unsigned char Gain_level)
{
unsigned char temp[2];
unsigned char tp = 0;
unsigned char channel_adr;
int i;
if(Channel_sel>3)return;
if(Gain_level>7)return;
temp[0] = R5C;
channel_adr = 0x80+(3-Channel_sel)*2;/*cal codec channel address*/
for(i=0;i<4;i++)
{
tp = readreg(Channel_sel,R5C,1);
if(((tp &0x80) ==0x80 )&& ((tp &0x40) ==0x00 ))/*test tp= 10uu-uuuu*/
break;
}
tp &=0xF8;/*set R5C(d2,d1,d0)<---0*/
temp[1] = Gain_level|tp;
#ifdef CODEC_DEBUG
printf("fun:HW_Set_Digital_Sidetone set reg R5C val=0x%x\n",temp[1]);
#endif
I2CWrite(channel_adr,temp,2);
}
/*************************************************************
*The routine perform Codec's different channel connection
*Input:Channel_sel. 0 means CODEC 1 channel 1, 1 means CODEC 1 channel2.
2 means CODEC 2 channel 1, 3 means CODEC 2 channel2.
Connect_type. range 1~11.
1:The headset input is connected to the headset output. R6a.d6<--1
2:The handset input is connected to the handset output. R6a.d5<--1
3:The caller ID input is connected to ADC channel. (not use currently) R6a.d4<--1
4:The line driver input is connected to ADC channel. (not use currently)R6a.d3<--1
5:The microphone input is connected to ADC channel.R6a.d2<--1
6:The handset input is connected to ADC channel .R6a.d1<--1
7:The headset input is connected to ADC channel. R6a.d0<--1
8:The speaker driver output is connected to DAC channel.R6b.d3<--1
9:The line driver output is connected to DAC channel. (not use currently) R6b.d2<--1
10:The handset driver output is connected to DAC channel.R6b.d1<--1
11:The headset driver output is connected to DAC channel.R6b.d0<--1
//////////////////////////////////////////////////////////////////////////////
codec0/1: microphone/speaker <---------->handset
------------> 5:The handset input is connected to ADC channel.R6a.d2<--1
6:The microphone input is connected to ADC channel .R6a.d1<--1
8:The handset driver output is connected to DAC channel.R6b.d3<--1
10:The speaker driver output is connected to DAC channel.R6b.d1<--1
ie . 8/5 --->handset
10/6---->microphone/speaker
5<--->6
8<--->10
codec1 ------->microphone/speaker
------->handset
------->headset
codec2 -------->handset
-------->headset
-------->recorder
*Output:void
*
*
***************************************************************/
void HW_Set_Channel_Connect(unsigned char Channel_sel,unsigned char Connect_type )
{
unsigned char tp;
int i;
if(Channel_sel>3)return;
if((Connect_type<1) || (Connect_type >11)) return;
if(Connect_type<=7)
{
tp = 1<<(7-Connect_type);
tp &=0x7F;/*R6A(d7)<---0*/
}
else
{
tp = (1<<(11-Connect_type));
tp |=0x80;/*R6B(d7)<---1*/
}
if(Connect_type<=7)
{
#ifdef CODEC_DEBUG
printf("fun:HW_Set_Channel_Connect set reg R6A val=0x%x\n",tp);
#endif
}
else
{
#ifdef CODEC_DEBUG
printf("fun:HW_Set_Channel_Connect set reg R6B val=0x%x\n",tp);
#endif
}
if(Connect_type<=7)
Codec_Write_Reg(Channel_sel,R6A ,0x00);/*disconnet all ad*/
else
Codec_Write_Reg(Channel_sel,R6B ,0x80);/*disconnet all da*/
for(i=0;i<500;i++);
Codec_Write_Reg(Channel_sel,R6A ,tp);
for(i=0;i<500;i++);
}
/*************************************************************
*The routine get Codec's different channel connection
*Input:Codec_Sel. 0 means CODEC 1, 1 means CODEC 2.
Connect_type.
*ret: 0:headset
* 1:handset
* 2:unused
* 3:speaker
***************************************************************/
unsigned char HW_Get_Channel_Connect(unsigned char Channel_sel,unsigned char * Connect_type )
{
unsigned char channel_adr;
unsigned char tp =0;
int i;
if(Channel_sel>3)return 0;
channel_adr = 0x80+(3-Channel_sel)*2;/*cal codec channel address*/
for(i=0;i<2;i++)
{
tp = readreg(Channel_sel,R6B,1);
if(((tp &0x80 )==0x80 ))/*test tp= 1uuu-uuuu*/
break;
}
#ifdef CODEC_DEBUG
printf("fun:HW_Set_Channel_Connect get reg R6B val=0x%x\n",tp);
#endif
if(tp == 0x88)
*Connect_type = 1; /*handset*/
else if(tp == 0x84)
*Connect_type = 2; /*line drive*/
else if(tp == 0x82)
*Connect_type = 3/*0*/; /*speaker*/
else if(tp == 0x80)
*Connect_type = 0/*1*/; /*headset*/
#ifdef CODEC_DEBUG
printf("fun:HW_Set_Channel_Connect ret val=%d\n",*Connect_type);
#endif
return *Connect_type;
}
/*
Channel_sel: 0 means CODEC 1 channel 1, 1 means CODEC 1 channel2.
2 means CODEC 2 channel 1, 3 means CODEC 2 channel2.
*/
void Codec_didital_loopback_en(unsigned char Channel_sel)
{
Codec_Write_Reg(Channel_sel,R1,0x6b);
}
void Codec_didital_loopback_dis(unsigned char Channel_sel)
{
Codec_Write_Reg(Channel_sel,R1,0x69);
}
void Codec_AD_Disconnect(unsigned char Channel_sel)
{
Codec_Write_Reg(Channel_sel,R6A,0x0);
}
void Codec_DA_Disconnect(unsigned char Channel_sel)
{
Codec_Write_Reg(Channel_sel,R6B,0x80);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -