<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/48300/issues-reading-virtual-registers-via-i2c</link><description>Hello, 
 
 I have an AMS AS7261 light sensor that I&amp;#39;m using with my NRF51422 DK to take a light reading, pull it using nrf_drv_twi_tx/rx, and send it over BLE. However, when I try to read from the virtual registers of interest, nearly each of the registers</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 25 Jun 2019 06:54:10 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/48300/issues-reading-virtual-registers-via-i2c" /><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/194505?ContentTypeID=1</link><pubDate>Tue, 25 Jun 2019 06:54:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fc3af8cf-1909-473b-97e0-39f700c407ac</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Bennett&lt;/p&gt;
&lt;p&gt;You welcome!&lt;/p&gt;
&lt;p&gt;I&amp;#39;m glad you were able to solve the issue &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/194158?ContentTypeID=1</link><pubDate>Fri, 21 Jun 2019 18:44:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ce245e39-39cb-47eb-a58d-a6ded1863733</guid><dc:creator>bsas</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;Torbj&amp;oslash;rn,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;So I&amp;#39;m not sure exactly why, but this issue seems to have solved itself when I tried it again yesterday morning. I don&amp;#39;t know what change fixed it, I&amp;#39;m thinking it may have just been resetting the DK/rebuilding the project, though I did try that initially as well.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you for all your help,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Bennett&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/194028?ContentTypeID=1</link><pubDate>Fri, 21 Jun 2019 08:54:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c615ecf-9da6-410f-a1b2-30da6d6c2eda</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Bennett&lt;/p&gt;
&lt;p&gt;I am having a hard time understanding why this happens by looking at the code alone.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Are you able to provide another scope plot, similar to the one you provided before?&lt;/p&gt;
&lt;p&gt;Ideally one showing the entire communication sequence (assuming the code doesn&amp;#39;t have to check the status register over and over).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/193759?ContentTypeID=1</link><pubDate>Wed, 19 Jun 2019 23:53:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9494ffc3-59ac-4727-9665-2a852cc28c76</guid><dc:creator>bsas</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;Torbj&amp;oslash;rn,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you again for your help. I have resolved this issue by adding some extra read/writes. It looks like the main fix was changing the p_data argument in nrf_drv_twi_tx from just the virtual register address to an array holding both the HW write register and the virtual address.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;However, I&amp;#39;m running into a new issue now. When I read all the values from these registers, the registers seem to be &amp;quot;offset&amp;quot; by 1. For example, the value I should get for R0 is instead returned for R1, the value I should get for R1 is returned for R2, etc. My code is below, do you know why this may be happening?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;void read_sensor(uint16_t values[], const uint8_t registers[]) {
  uint8_t reg = 0;
  uint8_t read_addy = I2C_AS72XX_SLAVE_READ_REG;
  uint8_t stat_addy = I2C_AS72XX_SLAVE_STATUS_REG;
  uint8_t reg_addr[2] = {I2C_AS72XX_SLAVE_WRITE_REG, registers[0]}; //first virtual register to read
  uint8_t end_reg = registers[1];
  uint8_t buf;
  ret_code_t status;
  int counter = 0;
  FILE *fp;
  fp = fopen(&amp;quot;./test.txt&amp;quot;, &amp;quot;w&amp;quot;);

  while (reg_addr[1] &amp;lt;= end_reg) {
    buf = 0;

    //Check status
    while (true) {
      nrf_drv_twi_tx(&amp;amp;m_twi, AS72XX_ADDR, &amp;amp;stat_addy, sizeof(stat_addy), true);
      status = nrf_drv_twi_rx(&amp;amp;m_twi, AS72XX_ADDR, &amp;amp;buf, sizeof(buf));
      if ((buf &amp;amp; I2C_AS72XX_SLAVE_TX_VALID) == 0)
        break;
    }
    buf = 0;

    //Write virtual address
    while(true) {
      status = nrf_drv_twi_tx(&amp;amp;m_twi, AS72XX_ADDR, reg_addr, sizeof(reg_addr), true);
      if (status == NRF_SUCCESS)
        break;
    }

    //Check status
    while(true) {
      nrf_drv_twi_tx(&amp;amp;m_twi, AS72XX_ADDR, &amp;amp;stat_addy, sizeof(stat_addy), true);
      status = nrf_drv_twi_rx(&amp;amp;m_twi, AS72XX_ADDR, &amp;amp;buf, sizeof(buf));
      if ((buf &amp;amp; I2C_AS72XX_SLAVE_RX_VALID) != 0)
        break;
    }
    buf = 0;

    //Read virtual register
    while(true) {
      status = nrf_drv_twi_tx(&amp;amp;m_twi, AS72XX_ADDR, &amp;amp;read_addy, 1, true);
      if (status == NRF_SUCCESS)
        break;
    }

    while(true) {
      status = nrf_drv_twi_rx(&amp;amp;m_twi, AS72XX_ADDR, &amp;amp;buf, sizeof(buf));
      if (status == NRF_SUCCESS)
        break;
    }

    printf(&amp;quot;Value at register %#x: %#x\r\n&amp;quot;, reg_addr[1], buf);
    fprintf(fp, &amp;quot;Value at register %#x: %#x\r\n&amp;quot;, reg_addr[1], buf);
    reg_addr[1]++;
  }
  fclose(fp);
}

...

int main() {
...
    uint16_t values[6];
    const uint8_t regs[2] = {0x00, 0x13};
    read_sensor(values, regs);
...
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you again for your help,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Bennett Sasaki&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/192675?ContentTypeID=1</link><pubDate>Thu, 13 Jun 2019 12:12:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6805b239-f1ae-4671-9af3-72ac8351ec23</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Bennett&lt;/p&gt;
&lt;p&gt;Thanks for sharing the data sheet.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Reading through the sample code for reading and writing the virtual registers this seems considerably more complicated than similar sensors I have worked with, and I am not sure the code you sent me is equivalent to the code they provided.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For one thing their virtual register read example starts with a read loop, to wait for the read buffer to be ready, while your code starts with a write.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Secondly, it is important to note that the&amp;nbsp;nrf_drv_twi_rx function returns any error code returned by the TWI driver, it doesn&amp;#39;t return the data read. The data read is stored in the buffer that you pass into the function.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In other words you have to implement the while(1) loops in the virtual register write/read procedures a bit different from what they do in the data sheet in order to properly check the status data returned from the sensor.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/192478?ContentTypeID=1</link><pubDate>Wed, 12 Jun 2019 16:53:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:71300be1-9d7c-40b2-9ff1-6da3ac8c1567</guid><dc:creator>bsas</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;Torbj&amp;oslash;rn,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you for your detailed respone. The data sheet is available &lt;a href="https://ams.com/documents/20143/36005/AS7261_DS000493_1-00.pdf/0e7d1f12-702e-d061-b07a-67b0b6316800"&gt;here&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Bennett&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/192414?ContentTypeID=1</link><pubDate>Wed, 12 Jun 2019 13:43:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9f45c5a6-355d-4236-82b1-304478a057e1</guid><dc:creator>awneil</dc:creator><description>[quote userid="2116" url="~/f/nordic-q-a/48300/issues-reading-virtual-registers-via-i2c/192410"]&amp;nbsp;I also think it&amp;#39;s normal for the sensor to send a NACK after the last byte of information has been sent[/quote]
&lt;p&gt;Yes - it is.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/192410?ContentTypeID=1</link><pubDate>Wed, 12 Jun 2019 13:35:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7301cb09-3bc4-41a2-b9ed-0d87030bf6d7</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Bennett&lt;/p&gt;
&lt;p&gt;Thanks for the traces.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To me they look pretty normal. I tried running some comparable code on a nRF52DK which reads an LM75BD temperature sensor and an MMA7660 accelerometer over I2C, and the results are comparable.&lt;/p&gt;
&lt;p&gt;Granted it&amp;#39;s running on a nRF52 device and not a nRF51, but the driver is the same, and I also see the &amp;#39;missing&amp;#39; gap in the RX calls which is not there when sending data.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;I also think it&amp;#39;s normal for the sensor to send a NACK after the last byte of information has been sent, to signal that no more data is coming. I see similar behavior from the LM75BD and MMA7660 devices.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Do you have a data sheet for the sensor that I could have a look at?&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/192181?ContentTypeID=1</link><pubDate>Tue, 11 Jun 2019 23:48:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b816c77-b8e3-42fd-a509-3b3647cc95e4</guid><dc:creator>bsas</dc:creator><description>&lt;p&gt;I&amp;#39;m using SDK 12.03.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m now noticing a few oddities when I use the scope. After doing a write (with nrf_drv_twi_tx), it mostly looks fine but SCL does not return to high at the end of the call. When I call the subsequent read, it looks like there&amp;#39;s a NACK, but nrf_drv_twi_rx returns NRF_SUCCESS. I&amp;#39;ll attach the scope captures below:&lt;/p&gt;
&lt;p&gt;Write:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/sensor_5F00_write.JPG" /&gt;&lt;/p&gt;
&lt;p&gt;Read:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/sensor_5F00_read.JPG" /&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m kind of new to this stuff so I&amp;#39;m not sure where the issue may lie, I&amp;#39;m thinking it&amp;#39;s in the sensor, I&amp;#39;ve tried contacting the manufacturer. I also noticed that on the read, there isn&amp;#39;t the &amp;quot;break&amp;quot; in the clock line between the end of the address and the start of the data like there is in the write screenshot, not sure if that&amp;#39;s an issue or not.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you for your help,&lt;/p&gt;
&lt;p&gt;Bennett&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Issues reading virtual registers via I2C</title><link>https://devzone.nordicsemi.com/thread/191915?ContentTypeID=1</link><pubDate>Tue, 11 Jun 2019 06:45:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:649c6347-6389-48ea-bd0e-3523eec2fc21</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Bennett&lt;/p&gt;
&lt;p&gt;Could there be some init/configuration sequence that you have to run before accessing these registers?&lt;/p&gt;
&lt;p&gt;Are there any registers in the sensor that hold a fixed value, that you could use to debug the communication?&lt;br /&gt;Many sensors have some kind of device ID register that will never change.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Have you tried to scope the lines to see what happens on the bus?&lt;/p&gt;
&lt;p&gt;Assuming the&amp;nbsp;nrf_drv_twi_tx/rx calls are blocking I can&amp;#39;t see any issues with the code. Can you let me know which SDK version you are using?&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>