DSP

学习DSP外设之I2C的配置和应用

2019-07-13 09:41发布

一、I2C的协议这里就不赘述了,随便搜一下就可以了解。

二、I2C的初始化

1.Enable I2C clock

configure the PSC register(if it is driven by PSC)PSC相关说明可以查看手册

2. Configure the PinMultiplexing registers

3. Place I2C in reset

set IRS =0 in ICMDR register

4.Configure the peripheral clock operation frequency(ICPSC)

NOTE:the frequency should be between 6.7 and 13.3MHz

5.Configure i2c master clock frequency:

configure ICCLKL , ICCLKH

7.Make sure the interrupt status register(ICSTR) is cleared:

Read ICSTR and write it back (write 1 to clear) ICSTR = ICSTR Read ICIVR until it is 0. 

8.Take i2c controller out of reset 

set IRS=1 in ICMDR

三、I2C作为master发送数据的配置

1. Wait until bus bit is cleared (BB =0 in ICSTR)

2.Configure ICMDR register

  • configure I2C as Master ( MST = 1 );
  • indicate the i2c configuration to be used(if TRX = 0, device configured as Data Receiver);
  • configure serial data formats( eg.  XA = 0, 7-bit addressing format );
  • set RM = 0 to disable repeat mode;
  • set DLB = 0 to disable loopback mode;
  • set FDF = 0 to disable free data format;
  • optional:disable start byte mode if addressing a fully fledged i2c device (set STB = 0 );
  • set number of bits to transfer to be 8 bits (BC = 0) ;
  • 设置仿真器断点无响应,bus继续运行, ICMDR中 FREE=1

3.设置发送数据长度(字节)ICCNT

4.Configure Slave Address( ICSAR = 7bit address)

5.Generate a START event ,followed by Slave Address,etc.(set STT =1 in ICMDR)

6.循环发送数据,数据写入ICDXR,等待ICSTR中ICXRDY=1,表明数据搬运完成。并设置超时重新初始化init(我这设置的是while循环不超过500000)

7. End transfer/release bus when transfer is done. Generate a STOP event (set STP = 1 in ICMDR). 

四、I2C作为master读取数据

1. Wait until bus bit is cleared (BB =0 in ICSTR)

2.Configure ICMDR register

  • configure I2C as Master ( MST = 1 );
  • indicate the i2c configuration to be used(if TRX = 0, device configured as Data Receiver);
  • configure serial data formats( eg.  XA = 0, 7-bit addressing format );
  • set RM = 0 to disable repeat mode;
  • set DLB = 0 to disable loopback mode;
  • set FDF = 0 to disable free data format;
  • optional:disable start byte mode if addressing a fully fledged i2c device (set STB = 0 );
  • set number of bits to transfer to be 8 bits (BC = 0) ;
  • 设置仿真器断点无响应,bus继续运行, ICMDR中 FREE=1

3.设置发送数据长度(字节)ICCNT

4.Configure Slave Address( ICSAR = 7bit address)

5.Generate a START event ,followed by Slave Address,etc.(set STT =1 in ICMDR)

6.循环接收数据

等待ICSTR中ICRRDY=1,为1时表明数据可以读取,并设置超时重新初始化init(我这设置的是while循环不超过500000),读取ICDRR中数据。当数据为最后一个字节时发送NACK(ICMDR中NACKMOD=1)

7. End transfer/release bus when transfer is done. Generate a STOP event (set STP = 1 in ICMDR).