<?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>TWI write to IC registers fails when triggered by an incoming a BLE command [NRF Internal Error 3]</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/85426/twi-write-to-ic-registers-fails-when-triggered-by-an-incoming-a-ble-command-nrf-internal-error-3</link><description>System details: 
 
 Board: Custom 
 Chip: nRF52832 
 PCA Number: PCA10040 
 SoftDevice: S132 
 SDK: Version 16.0.0 
 
 Until recently, I have had no issues using the TWI module to read/write form all of our various sensor ICs that we have on our custom</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 05 Mar 2022 23:23:01 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/85426/twi-write-to-ic-registers-fails-when-triggered-by-an-incoming-a-ble-command-nrf-internal-error-3" /><item><title>RE: TWI write to IC registers fails when triggered by an incoming a BLE command [NRF Internal Error 3]</title><link>https://devzone.nordicsemi.com/thread/356435?ContentTypeID=1</link><pubDate>Sat, 05 Mar 2022 23:23:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96274c8d-50c0-405e-a5e5-bd881097131f</guid><dc:creator>cor10</dc:creator><description>&lt;p&gt;In the code I posted, specifically for the register write function, execution makes it all the way until the first APP_ERROR_CHECK() function catches the NRF INTERNAL ERROR 3. It does call memcpy right before interfacing with the TWI library, but there havent been any failures&amp;nbsp;from memcpy to my knowledge. As Jared (from the other comment) mentioned, it is looking like a hardware and timing issue with the TWI driver, and thankfully it looks like I figured it out. I needed to gracefully handle the expected TWI NACK from forcing the communications window with my sensor...it appears the NACK followed by an immediate write or read would cause issues with the timing? Not really sure, but it seems to have helped. And I added a few delays in between operations to give a little more slack in regards to timing.&lt;/p&gt;
&lt;p&gt;However, checking for a non-zero length of incoming data is a good check to have in place, so thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI write to IC registers fails when triggered by an incoming a BLE command [NRF Internal Error 3]</title><link>https://devzone.nordicsemi.com/thread/356433?ContentTypeID=1</link><pubDate>Sat, 05 Mar 2022 19:11:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96365ab0-3c36-45be-a0d0-a603a921beff</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;You are correct, I re-read the code more closely and see that I&amp;#39;d missed that.&lt;/p&gt;
&lt;p&gt;The TWI always had an issue (MISRA and CERT violation) with using an unchecked length to create an array which is subsequently used by an unchecked block copy/move. if length is (say) 0 bad things happen .. edit ignore that I see there is a +2 on reading again. Is pdata range-checked? memcpy of a zero-length value is also bad .. and the error shown seems to be in memcpy ..&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI write to IC registers fails when triggered by an incoming a BLE command [NRF Internal Error 3]</title><link>https://devzone.nordicsemi.com/thread/356417?ContentTypeID=1</link><pubDate>Sat, 05 Mar 2022 01:19:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:335d5548-d245-422d-83be-c01462cecb1f</guid><dc:creator>cor10</dc:creator><description>&lt;p&gt;Hi Jared,&lt;/p&gt;
&lt;p&gt;I&amp;nbsp;spoke with the EE who designed the board and YES there are external pullups in place (2.2k&amp;Omega;). And, yes the error is coming from the TWI driver, specifically from this function:&lt;/p&gt;
&lt;pre&gt;uint32_t twiRegWrite16(uint8_t slaveAddr, uint16_t regAddr, uint8_t const * pdata, uint16_t length)
{
    uint32_t ret;
    uint8_t tx_buff[length + 2];
    uint8_t upperAddr = (regAddr &amp;gt;&amp;gt; 8) &amp;amp; 0x00FF;
    uint8_t lowerAddr = regAddr &amp;amp; 0x00FF;
    tx_buff[0] = upperAddr;
    tx_buff[1] = lowerAddr;

    memcpy(tx_buff + 2, pdata, length);
    nrf_drv_twi_xfer_desc_t xfer = NRF_DRV_TWI_XFER_DESC_TX(slaveAddr, tx_buff, length + 2);

    ret = nrf_drv_twi_xfer(&amp;amp;m_twi, &amp;amp;xfer, 0);

    if (NRF_SUCCESS != ret)
    {
        return ret;
    }

    // Wait for response for 5 ms
    for(uint32_t k = 0; k &amp;lt;= 5000; k++)
    {
        if(m_xfer_status != 0)
        {
            nrf_delay_ms(1);
            break;
        }
        nrf_delay_us(1);
    }

    if(m_xfer_status == TWI_XFER_STATUS_SUCCESS)
    {
        return NRF_SUCCESS;
    }
    else
    {
        return NRF_ERROR_INTERNAL;
    }
}&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;First, I tried disabling the internal pullups first, but that didnt seem to work right, because the error occurred again when just relying on the external Pullups.&lt;/p&gt;
&lt;p&gt;Next, we removed the external pullups, which you can see in the schematic that I am attaching for reference. However, now we can no longer communicate with ANY of our sensor ICs (I double checked that the internal PullUps are enabled in &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;nrf_drv_twi.c&lt;/span&gt; in the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;SCL/SDA_PIN_INIT_CONF&lt;/span&gt; defines). Any ideas as to why this would happen when just using the internal PullUps?&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/960x720/__key/communityserver-discussions-components-files/4/shcematic_5F00_pullups.png" /&gt;&lt;/p&gt;
&lt;p&gt;Also, I am curious as to why don&amp;#39;t I ever see the &lt;span style="font-family:&amp;#39;courier new&amp;#39;, courier;"&gt;NRF INTERNAL ERROR 3&lt;/span&gt; thrown during the initialization stage of the application. When I initialize all the sensors, I perform many register write operations in a row, and never once has it thrown an error. The only other register writes that occur with this project are from the Desktop app that I am working on here, and they are buggy for some reason. The only other running code in this application is&amp;nbsp;the 50ms repeated timer in, which performs&amp;nbsp;many register reads and never fails....I suppose those repeated register reads&amp;nbsp;could be conflicting with these asynchronous register writes (but oddly not with asynch register reads,&amp;nbsp;which always works). And we dont see this failure during configuration because the timer hasnt been started yet...&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI write to IC registers fails when triggered by an incoming a BLE command [NRF Internal Error 3]</title><link>https://devzone.nordicsemi.com/thread/356416?ContentTypeID=1</link><pubDate>Sat, 05 Mar 2022 01:14:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4705f20b-0fe0-4d0c-aba2-7fb6ee37ea6b</guid><dc:creator>cor10</dc:creator><description>&lt;p&gt;Correct me if I am wrong here, but I&amp;nbsp;am specifically using&amp;nbsp;the app Scheduler to deal with this issue of multiple threads. My understanding of the Scheduler could be incorrect, but from the main() function in my project, the application is in an infinite loop&amp;nbsp;that solely waits for events and calls app_sched_execute() to run any tasks that have been queued via app_sched_event_put().&lt;/p&gt;
&lt;p&gt;By only scheduling (not running) the register write task in the ble_nus_handler(), I thought that I was effectively handing off the execution of each regWrite task to the main() loop. The only other&amp;nbsp;reads/writes made to the sensors are also done via the same app_sched_event_put() function, so my hope was that the Scheduler would be smart enough to sequentially process these tasks without conflict or fear of asynchronous interrupts.&lt;/p&gt;
&lt;p&gt;Is this logic incorrect?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI write to IC registers fails when triggered by an incoming a BLE command [NRF Internal Error 3]</title><link>https://devzone.nordicsemi.com/thread/356383?ContentTypeID=1</link><pubDate>Fri, 04 Mar 2022 16:12:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d8725205-6ac3-4e1f-89a1-5b6d2af52b75</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;This may be due to not considering multiple threads, which is an issue I have seen on other projects.&amp;nbsp;&lt;span&gt;&lt;em&gt;regWriteHandlerHelper()&lt;/em&gt; is (probably) not re-entrant, so an error is generated when it is used by two threads simultaneously. Why would that happen? Consider a read or write in progress to the sensor from some function invoked from main or other part of the code when a BLE packet is received asynchronously&amp;nbsp;which invokes&amp;nbsp;&lt;em&gt;nus_data_handler()&lt;/em&gt;. The problem is that this BLE request is within an interrupt context, and so initiates a sensor transfer interrupting the transfer already in progress with no handling to detect this.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;A simple test to see if this is the issue is instead of &lt;em&gt;nus_data_handler()&lt;/em&gt; invoking&amp;nbsp;&lt;em&gt;regWriteHandlerHelper()&lt;/em&gt;&amp;nbsp;directly instead set a volatile bool and invoke&amp;nbsp;&lt;em&gt;regWriteHandlerHelper()&lt;/em&gt;&amp;nbsp; from main().&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;If you have already considered this please ignore, but looking at the code presented in the original post it looks like this is the issue.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: TWI write to IC registers fails when triggered by an incoming a BLE command [NRF Internal Error 3]</title><link>https://devzone.nordicsemi.com/thread/356335?ContentTypeID=1</link><pubDate>Fri, 04 Mar 2022 14:02:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a6acbea7-0265-41da-ac32-0cca24b3da02</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;What function throws the NRF_ERROR_INTERNAL? If it&amp;#39;s the TWI driver then it&amp;#39;s a hardware error. This can happen for example if the timing requirements are not respected. Do you have external pullups connected to the SDA or SCL?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>