DM8168裸机调试-I2C(MCP79410)

来源:互联网 发布:unity3d 自学网 编辑:程序博客网 时间:2024/06/10 10:13

一、I2C寄存器配置


#define I2C0_BASE                    0x48028000
#if 0
#define I2C0_STAT                    *( volatile Uint32* )( I2C0_BASE + 0x24 )//0x0180
#define I2C0_SYSC                    *( volatile Uint32* )( I2C0_BASE + 0x10 )//0x01
#define I2C0_IE                      *( volatile Uint32* )( I2C0_BASE + 0x2C )//0x
#endif
/*why I2C0_STAT、 I2C0_SYSC、 I2C0_IE地址值 同手册不一致??  */
#define I2C0_STAT                    *( volatile Uint32* )( I2C0_BASE + 0x88 ) //0x0180 
#define I2C0_SYSC                    *( volatile Uint32* )( I2C0_BASE + 0xA0 )//0x00
#define I2C0_IE                      *( volatile Uint32* )( I2C0_BASE + 0x84 )//0x00

#define I2C0_CNT                     *( volatile Uint32* )( I2C0_BASE + 0x98 )//0x07
#define I2C0_DATA                    *( volatile Uint32* )( I2C0_BASE + 0x9C )//0x3A
#define I2C0_CON                     *( volatile Uint32* )( I2C0_BASE + 0xA4 )//0x8000
#define I2C0_OA                      *( volatile Uint32* )( I2C0_BASE + 0xA8 )//0xCC
#define I2C0_SA                      *( volatile Uint32* )( I2C0_BASE + 0xAC )//0x6F
#define I2C0_PSC                     *( volatile Uint32* )( I2C0_BASE + 0xB0 )//0xFF
#define I2C0_SCLL                    *( volatile Uint32* )( I2C0_BASE + 0xB4 )//0x09
#define I2C0_SCLH                    *( volatile Uint32* )( I2C0_BASE + 0xB8 )//0x09

二、时序配置


概念:1、SCL时钟同I2C工作时钟一回事吗? 不是一回事。
当配置为主机master模式,比特率发生器控制时钟信号SCL的周期。当配置为从机slave模式,不需要配置SCL的周期。
 2、SCLK和ICLK定义?
   1、 SCLK定义Functional Clock,由DDRPLL的SYSCLK10 Functional Clock
    确认SYSCLK10输出(手册推荐48 MHz,实际计算gel配置结果也是48 MHz)
    N=59     P=1   M=30, FREQ=8+0xD99999 = 8.85     B= 1
    SYSCLK10 = DDRPLLclcok2/B =  [((N*K)/(FREQ*M*P))*Fr]/B = 48 MHz
   2、ICLK --I2C逻辑时钟内部,由SCLK经过I2C _PSC分频器获得的。
      ICLK = SCLK /(PSC+1)
 3、SCLL和SCLH说明
  SCLL--I2C总线传输1bit时,SCL低电平时间配置值 tLOW = (SCLL + 7) * ICLK time period
  SCLH--I2C总线传输1bit时,SCL高电平时间配置值 tHIGH = (SCLH + 5 + floor[t_rise/ICLK Period]) * ICLK time period
 4、实测值
  tLOW   = 42.8us
  tHIGH  = 37.2us
  I2C速率 = 12.5 KHz 80us
  ICLK time period = 1/ (SCLK /(PSC+1))=128/48 =2.66us
  反推:SCLL = 42.8/2.66  -7 = 9   同evm816x_i2c.c中SCLL配置值9 一致
  反推:SCLH = 37.2/2.66 -5 - floor[t_rise/ICLK Period]= 9-floor[t_rise/ICLK Period] 而evm816x_i2c.c中SCLH配置为9,
  则floor[t_rise/ICLK Period]=0
  
 5、验证evm816x_i2c.c中400K
  ICLK time period =1/ (SCLK /(PSC+1)) =256/12=21.3 us
  tLOW = (SCLL + 7) * ICLK time period= 16*21.3 =340.8us
  tHIGH = (SCLH + 5 + floor[t_rise/ICLK Period]) * ICLK time period =14*21.3 = 298.2 us
  T= tLOW + tHIGH = 639 us
  
  波特率400K对应周期 T=1/400 = 2.5 us  验证结果:不正确
  
 6、自己配置一个100K通讯速率(将SCLL和SCLH配置相同值)
    需要配置值有 PSC、SCLH、SCLL。
    100K对应周期时间 T= 1/100K =10us  = tLOW + tHIGH =(SCLL + 7+SCLH + 5)*ICLK time period(SCLL和SCLH配置相同值)  
                                                     =(12 +2*SCLH)*ICLK time period(SCLH = 4)
              =20*ICLK time period
     反推:ICLK time period =0.5us
  
  PSC+1 =SCLK*ICLK time period= 48*0.5=24  ,则PSC=23 。
  通过示波器验证,tLOW =5.5us,tHIGH=4.5us,则T=10us,符合100K通讯速率。
  推到计算公式:需要设置通讯速率Fa
  Ta=tLOW + tHIGH =(SCLL + 7+SCLH + 5)*ICLK time period
                  =(12 +2*SCLH)*ICLK time period
      =(12 +2*SCLH)*(PSC+1)/SCLK
  即Fa =SCLK/((12 +2*SCLH)*(PSC+1))  则 SCLK/Fa =(12 +2*SCLH)*(PSC+1)
  若通讯速率50 KHz,假设SCLH=SCLL=4,则PSC=47
  
  (注:该公式忽略floor[t_rise/ICLK Period],通过计算反推出来,该项对结果影响较小。具体原因待查)
  
  
 总结:1、SCL和ICLK之前理解误区:之前认为I2C通信传输过程中,SCL时钟频率等同 I2C模式的ICLK。
  
手册中定义:  
 11.2.8 Prescaler (SCLK/ICLK)
 The I2C module is operated with a functional clock (SCLK) frequency that can be in a range of 12-100 MHz,
according to I2C mode that must be used (an internal ~24 MHz clock (ICLK) is recommended in case of F/S operation mode).
Note that the frequency of the functional clock influences directly the I2C bus performance and timings.

 The internal clock used for I2C logic - ICLK - is generated via the I2C prescaler block. The prescaler
consists of a 4-bit register - I2C _PSC, and is used to divide the system clock (SCLK) to obtain the internal
required clock for the I2C module.

三、从机地址、模式等配置


 模式:主机,地址:0xCC
 从机:         地址
   1、MCP794100(RTC)    0x57(EEPROM_Address)和0x6F(RTCRAM_Address)
   2、SGTL5000(音频)    0n01010详见手册P23 where n is determined by CTRL_ADR0_CS and R/W is the read/write bit from the I2C protocol.
   3、TCA9539(外扩IO)    0x77
   4、MB85RC64A(EEPROM)   0x56
   5、CH7034(显示芯片)   0x75
   6、VG1(PCA9507DP)    0x50
 
 I2C0_CNT:访问I2C设备中内容长度 。当I2C0_CNT接近于0时,产生停止信号(前提使能停止信号触发I2C_CON.STP = 1)),
 同时状态寄存器I2C_IRQSTATUS_RAW.ARDY置1,表示通知I2C寄存器 本次访问结束,准备下一次访问。
 
小结: 1、I2C_CON     配置I2C
  2、I2C_IRQSTATUS_RAW 查询I2C总线状态

四、程序


4.1、初始化

4.2、读操作

4.3、写操作

0 0