Cypress技术william hill官网
直播中

李悠冉

7年用户 252经验值
私信 关注
[问答]

如何读取包含16位子地址的I2C

嗨,伙计们,
我有一个奇怪的问题,这使我发疯了!
我已经创建了一个函数,它能够读取包含16位子地址的I2C。
在我的情况,我需要回读4个字节的子地址,第四字节的值将一个值0,1或2。
如果(睡眠计数器和GT;50){
睡眠计数器=0;
readdata = i2c_1_dsp_master_read(device_addr_ic_1,mod_audio_detect_detect_reg_readbackalgsigma2001_addr,4);
如果(Read DATA=2){
[做某事]
否则,(Read DATA==1){
[做某事]
否则,(Read DATA==0){
做某事
}
}
其中读取函数如下:
i2c_1_dsp_master_read卡片(卡片slavechip,uint16 regaddress,uint8 CNT){
UIT1616MSB =(ReaveAdt & Gt;Gt;8);
UIT1616LSB=RealAdvices;
UIT8I;
挥发性UtiT8状态;
UITN8 RDATA〔4〕;
状态= dsp_i2cmastersendstart(slavechip,dsp_i2c_write_xfer_mode);
如果(状态= = dsp_i2c_mstr_no_error)/ *检查是否转移无误*结束
{
发送寄存器地址
状态=DSPIII2CMASTEMWRITE字节(MSB);
(状态)= DSPII2CYMSTRYNNORY错误);/ /中断;
Stase= DSP.I2CMistWrrestEnter字节(LSB);
(状态)= DSPIII2CYMSTRYNONOX错误;
}
状态= dsp_i2cmastersendrestart(slavechip,dsp_i2c_read_xfer_mode);
If(Stase= DSPGI2CYMSTRIGNONEAR错误){
/*读取的CNT字节数组*/
对于(i=0;i<CNT;i++)
{
如果(我& lt;(CNT))rddata [我] = dsp_i2cmasterreadbyte(dsp_i2c_ack_data);
否则RDATABAS[I]=DSPTI2CMASTATEDRADIONTELL(DSPIII2CY-NAKYDATA);
}
}
DSPGI2cMistSeNestStor(;)/*发送停止*/
DSP2I2CMASTReleRead BuffFor();
返回rdDATA〔3〕;
}
每次我看回我得到预期的4字节的登记,使用逻辑分析仪验证。然而,有两件奇怪的事情发生了。
1)即使数组的第四个字节是一样的,因此返回rddata [ 3 ]应始终返回相同的值,这是没有的情况下。第一次i2c_1_dsp_master_read()称为正确的返回值,每次经过它返回0
2)第一次i2c_1_dsp_master_read叫逻辑分析仪显示四字节返回应答,另外一个5字节0x00和NACK看到。每一次,只有4个字节返回,但4字节是现在伴随着一个NACK。请参阅附件。(请注意,即使我提高CNT 5,4字节总是承认,返回值是错误的第一次阅读后。
任何帮助都将非常感谢!
最好的,
卡特琳
屏幕截图2014-01-13在110.7.40.PNG
106.6 K

以上来自于百度翻译


     以下为原文
   Hi guys,
                        I have a peculiar problem, which is driving me insane!      

    I have created a function that is able to read back I2C which includes a 16 bit subaddress.      

    In my case I need to read back 4 bytes of the subaddress where the value of the 4th byte will be a value of 0, 1 or 2.      

     
    if(sleep_counter > 50){      

    sleep_counter = 0;      

          readData = I2C_1_DSP_MASTER_READ(DEVICE_ADDR_IC_1, MOD_AUDIO_DETECT_DETECT_REG_READBACKALGSIGMA2001_ADDR, 4);      

                                        

          if(readData == 2 ){      

          //Do something    }      

          else if(readData == 1 ){      

          //Do something    }      

          else if(readData == 0){      

          //Do something         

    }      

    }      

    Where the read function is as follows:      

    uint8 I2C_1_DSP_MASTER_READ(uint8 slavechip, uint16 regAddress, uint8 cnt ){      

    uint16 m*** = (regAddress >> 8);      

    uint16 l*** = regAddress;      
               

    uint8 i;      

    volatile uint8 status;      

    uint8 rdData[4];
          

    status = DSP_I2CMasterSendStart(slavechip, DSP_I2C_WRITE_XFER_MODE);      

    if(status == DSP_I2C_MSTR_NO_ERROR) /* Check if transfer completed without errors */      

    {      

          // Send register address      

          status = DSP_I2CMasterWriteByte(m***);      

          while(status != DSP_I2C_MSTR_NO_ERROR);// break;      
                        

          status = DSP_I2CMasterWriteByte(l***);      

          while(status != DSP_I2C_MSTR_NO_ERROR);                     

    }      

    status = DSP_I2CMasterSendRestart(slavechip, DSP_I2C_READ_XFER_MODE);      

    if(status == DSP_I2C_MSTR_NO_ERROR) {      

    /* Read array of cnt bytes */      

          for(i=0; i
          {      

          if(i < (cnt))rdData=DSP_I2CMasterReadByte(DSP_I2C_ACK_DATA);      

          else rdData = DSP_I2CMasterReadByte(DSP_I2C_NAK_DATA);         

          }      

    }      

    DSP_I2CMasterSendStop(); /* Send Stop */      

    DSP_I2CMasterClearReadBuf();      
         

    return rdData[3];      

    }      

     
    Every time I read back from the register I get the expected four bytes, verified by using a logic analyzer.  However, there are two strange things that happen.      

     
    1) Even when the 4th byte of the array stays the same, hence return rdData[3] should always return the same value, this is not the case. First time I2C_1_DSP_MASTER_READ()is called the correct value is returned, every time after that it returns 0      

     
    2) The first time I2C_1_DSP_MASTER_READ  is called the logic analyzer shows the four bytes returned with ACK, in addition a 5 byte of 0x00 and NACK is seen. Everytime after that 4 bytes only are returned, but the 4 byte is now accompanied by a NACK. Please see attachment. (please note that even if I increase cnt to 5 so that the four bytes are always acknowledged, the return value is still wrong after the first read.      

     
    Any help would be very much appreciated!      

     
    All the Best,      

    Katrine      

     
     


回帖(4)

杨军

2019-7-16 11:55:11
你解决了你的问题吗?

以上来自于百度翻译


     以下为原文
  Did you resolve your issue ?
举报

李悠冉

2019-7-16 12:01:51
是的,在前一个消息中稍加改动就解决了我所看到的问题。

以上来自于百度翻译


     以下为原文
   Yes, applying the slight change in the previous message solved the issue I was seeing.
举报

王琳

2019-7-16 12:13:57
我经常使用
/*读取的CNT字节数组*/
对于(i=0;i&lt;CNT;i++)
{
rdDAT=dSP2I2cMistRead字节(i&lt;(CNT-1))?DSPGI I2CYACKYDATA:DSPI2I2CNAKYA数据;
}
虽然我不喜欢“?”接线员,但这是我最喜欢的。
希望福尔姆斯不会破坏线路
鲍勃

以上来自于百度翻译


     以下为原文
  I frequently use
    /* Read array of cnt bytes */
    for(i=0; i
    {
    rdData = DSP_I2CMasterReadByte(i < (cnt-1))?DSP_I2C_ACK_DATA:DSP_I2C_NAK_DATA;
    }
     
    although I do not like the "?" operator, but here it is my favorite.
     
     
    Hoping that forumSW doesn't clobber the lines
    Bob
举报

李悠冉

2019-7-16 12:27:57
谢谢大家分享:

以上来自于百度翻译


     以下为原文
   Thank you for sharing Bob :)
举报

更多回帖

×
20
完善资料,
赚取积分