最近一段时间忙着考试,没留意试用报告分享时间是到20号~先说声抱歉了
先把程序放上来供大家参考,等考完试我再把相关寄存器配置说明补上~~~再说声抱歉
这部分介绍I2C接口配置,这个接口是串行接口,数据率不高,但就是简单,相比同样串行的总线SPI来说,它总共两根线,一根时钟线,一根数据线,而SPI有三根线,输入输出是分开的,我们有用到这个接口用于读取CODEC芯片中的寄存器。
一、I2C接口简介
(稍后再补)
二、I2C接口寄存器介绍
(稍后再补)
三、基于CSLr的I2C数据收发例程
1、I2C初始化函数
extern void setup_I2C(void)
{
CSL_FINST(i2cRegs->ICMDR,I2C_ICMDR_IRS,DISABLE);//I2C复位状态
i2cRegs->ICMDR=(
(CSL_I2C_ICMDR_FREE_TRUE<
|(CSL_I2C_ICMDR_MST_MASTER_MODE<
|(CSL_I2C_ICMDR_TRX_TX_MODE<
|(CSL_I2C_ICMDR_XA_7BIT<
|(CSL_I2C_ICMDR_RM_ENABLE<
|(CSL_I2C_ICMDR_DLB_ENABLE<
|(CSL_I2C_ICMDR_FDF_DISABLE<
|(CSL_I2C_ICMDR_BC_8BIT<
);
i2cRegs->ICOAR=(0x00<
//i2cRegs->ICSAR=(0x01<
i2cRegs->ICPSC=(1<
//I2C总线时钟为12MHz/((6+ICCH)+(6+ICCL))=1MHz
i2cRegs->ICCLKH=(10<
i2cRegs->ICCLKL=(10<
i2cRegs->ICSTR=(
(CSL_I2C_ICSTR_SDIR_CLEAR<
|(CSL_I2C_ICSTR_NACKSNT_CLEAR<
|(CSL_I2C_ICSTR_BB_CLEAR<
|(CSL_I2C_ICSTR_SCD_CLEAR<
|(CSL_I2C_ICSTR_ICXRDY_CLEAR<
|(CSL_I2C_ICSTR_ICRRDY_CLEAR<
|(CSL_I2C_ICSTR_ARDY_CLEAR<
|(CSL_I2C_ICSTR_NACK_CLEAR<
|(CSL_I2C_ICSTR_AL_CLEAR<
);
while(CSL_FEXT(i2cRegs->ICIVR,I2C_ICIVR_INTCODE));//等待中断清除
CSL_FINST(i2cRegs->ICMDR,I2C_ICMDR_IRS,ENABLE);//I2C使能
}
2、I2C发送数据函数
extern void i2c_Transfer(int *i2c_src,int i2c_src_length)
{
int jj;
while(CSL_FEXT(i2cRegs->ICSTR,I2C_ICSTR_BB));//等待总线释放
CSL_FINST(i2cRegs->ICMDR,I2C_ICMDR_STT,SET);//开始位
for(jj=0;jj
{
while(CSL_FEXT(i2cRegs->ICSTR,I2C_ICSTR_ICXRDY)==0);
CSL_FINS(i2cRegs->ICDXR,I2C_ICDXR_D,*i2c_src++);
}
CSL_FINST(i2cRegs->ICMDR,I2C_ICMDR_STP,SET);//停止位
}
3、I2C接收函数函数
extern void i2c_Reciver(int *i2c_dst,int i2c_dst_length)
{
int jj;
for(jj=0;jj
{
while(CSL_FEXT(i2cRegs->ICSTR,I2C_ICSTR_ICRRDY)==0);
(*i2c_dst++)=CSL_FEXT(i2cRegs->ICDRR,I2C_ICDRR_D);
}
CSL_FINST(i2cRegs->ICMDR,I2C_ICMDR_STP,SET);//停止位
}
4、I2C回环模式,相当于自检
extern void i2c_Lookback(char *i2c_src,char *i2c_dst,int i2c_length)
{
int jj;
while(CSL_FEXT(i2cRegs->ICSTR,I2C_ICSTR_BB));//等待总线释放
CSL_FINST(i2cRegs->ICMDR,I2C_ICMDR_STT,SET);//开始位
for(jj=0;jj
{
while(CSL_FEXT(i2cRegs->ICSTR,I2C_ICSTR_ICXRDY)==0);
CSL_FINS(i2cRegs->ICDXR,I2C_ICDXR_D,*i2c_src++);
while(CSL_FEXT(i2cRegs->ICSTR,I2C_ICSTR_ICRRDY)==0);
(*i2c_dst++)=CSL_FEXT(i2cRegs->ICDRR,I2C_ICDRR_D);
}
CSL_FINST(i2cRegs->ICMDR,I2C_ICMDR_STP,SET);//停止位
}
5、主函数
char SRC_AA[32] = "abcdefghijklmnopqrstuvwxyz123456";
char DST_AA[32];
void main(void)
{
setup_I2C();
asm("nop");
i2c_Lookback(&SRC_AA[0],&DST_AA[0],32);
while(1);
}
|