<?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>Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/51476/sending-actual-twi-sensor-values-through-ble</link><description>Hello! I am trying to modify the ble_app_hrs_freertos example to send actual heart rate sensor value to the nRF Toolbox app over BLE. I am able to read values from my sensor through TWI, and I am also able to run the ble_app_hrs_freertos example and send</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 06 Nov 2019 13:17:01 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/51476/sending-actual-twi-sensor-values-through-ble" /><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/218836?ContentTypeID=1</link><pubDate>Wed, 06 Nov 2019 13:17:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e57e3793-34ee-49fb-81a4-7a0ef40d41cb</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You can use this method. How you obtain data via WTI is&amp;nbsp;independent from how you use it later (such as transferring over BLE).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/218646?ContentTypeID=1</link><pubDate>Tue, 05 Nov 2019 15:41:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0a97a06c-1e61-4a4c-a516-7b7987f33265</guid><dc:creator>KCY</dc:creator><description>&lt;p&gt;Thank you for sending this link. I will go through it to debug. Meanwhile, I have a quick question.&lt;/p&gt;
&lt;p&gt;In the &lt;a href="https://github.com/particle-iot/nrf5_sdk/blob/master/examples/peripheral/twi_sensor/main.c"&gt;twi sensor example&lt;/a&gt;, the&lt;span&gt;&amp;nbsp;twi transaction &amp;quot;&lt;/span&gt;&lt;span class="pl-c1"&gt;read_sensor_data&lt;/span&gt;&lt;span&gt;()&amp;quot; is called in the main function, instead of in the data_handler, and I believe that the interrupt is controlled by the state of &lt;em&gt;m_xfer_done&lt;/em&gt;. Is that an approach I can try too? Or does it not work in my case because I have to send the data through BLE?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks!&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/218427?ContentTypeID=1</link><pubDate>Tue, 05 Nov 2019 07:54:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:165d796e-ae98-4a91-b09d-051790a486b8</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;It could be a timing issue or could be something else. The key is that you need to debug to understand what the connection is dropped. Without proper debugging you will just have to guess, and that is not a good way of approaching this. We have already seen from the screenshot of your call stack in a previous post that the nRF is in an error handler. So you need to debug to see what error occurred. Have you done that? You may find &lt;a href="https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/an-introduction-to-error-handling-in-nrf5-projects"&gt;An introduction to error handling in nRF5 projects&lt;/a&gt;&amp;nbsp;interesting, though it is not fully up to date.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/218272?ContentTypeID=1</link><pubDate>Mon, 04 Nov 2019 14:34:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:73c94f17-9fd5-4970-a65f-a1165096a673</guid><dc:creator>KCY</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Does the debugging mode shows error during the BLE transaction? I don&amp;#39;t seem to be getting any error in the debugging mode. When I give dummy heart rate values in the code, the BLE transactions works fine, but once I make it read from the sensor, the&amp;nbsp;BLE connection crashes. Could that be an issue with the timing? But shouldn&amp;#39;t the timing issue be handled automatically, since it is in non-blocking mode?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/217983?ContentTypeID=1</link><pubDate>Fri, 01 Nov 2019 14:03:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:11a8f105-d41e-433f-a69f-36a63a2872f9</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you are using Segger Embedded Studio, you simply select the debug build configuration (usually by a drop-down in the upper left corner of the screen). If you are using another toolchain, just make sure you have a global define &amp;quot;DEBUG&amp;quot; and build without optimization.&lt;/p&gt;
&lt;p&gt;Then enable debug logging in sdk_config.h if it is not already enabled (UART logging is enabled by default in most examples). If you are using an example without logging, you could just use the debugger to inspect the info struct in the error handler to see the same information, but it is a bit more hassle.&lt;/p&gt;
&lt;p&gt;Unfortunately, there is no unified list of error codes in the SDK, but there is a hierarchy with &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.api.v7.0.1/group___n_r_f___e_r_r_o_r_s___b_a_s_e.html"&gt;error bases&lt;/a&gt; etc. If you use logging the error code will most of the time be spelled out with the name of the define, so you won&amp;#39;t have to piece it together.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/217963?ContentTypeID=1</link><pubDate>Fri, 01 Nov 2019 12:59:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fd1eb20d-80a9-4dda-9221-2d59c16b00b0</guid><dc:creator>KCY</dc:creator><description>&lt;p&gt;Does that mean to click on the &amp;ldquo;build and debug&amp;rdquo;? Also, Is there a link to the list of error codes? I don&amp;rsquo;t seem to be able to find it. Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/217884?ContentTypeID=1</link><pubDate>Fri, 01 Nov 2019 07:36:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4c4b3811-47cd-4c6a-b27d-6cb6ea6123f0</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Looking at the call stack, you can see that it is in an error handler. So the first thing you should do is build with DEBUG enabled, and then either just enable logging or inspect the info struct in the error handler. This will tell you in which file and at which line you get which error code. As mentioned in my previous post, this is the by far best starting point when you see errors like this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/217808?ContentTypeID=1</link><pubDate>Thu, 31 Oct 2019 15:22:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2c61ea35-00eb-4f8c-b585-39d30d3fab78</guid><dc:creator>KCY</dc:creator><description>&lt;p&gt;Hi, Sorry for the delay. To clear things up. I decided to restart the project again and now I am back here at this position.&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I&amp;nbsp;configured the TWI driver to be non-blocking and use DMA&amp;nbsp;- passed an event handler when initializing the driver.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    ret_code_t err_code;
    uint16_t heart_rate;
    static sample_t m_sample;
    switch (p_event-&amp;gt;type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if (p_event-&amp;gt;xfer_desc.type == NRF_DRV_TWI_XFER_RX)
            {
                
                // call ble_hrs_heart_rate_measurement_send in twi_handler
                err_code = ble_hrs_heart_rate_measurement_send(&amp;amp;m_hrs, heart_rate);
                if ((err_code != NRF_SUCCESS) &amp;amp;&amp;amp;
                    (err_code != NRF_ERROR_INVALID_STATE) &amp;amp;&amp;amp;
                    (err_code != NRF_ERROR_RESOURCES) &amp;amp;&amp;amp;
                    (err_code != NRF_ERROR_BUSY) &amp;amp;&amp;amp;
                    (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
                   )
                {
                    APP_ERROR_HANDLER(err_code);
                }
            }
            m_xfer_done = true;
            break;
        default:
            break;
    }
}


void twi_init (void)
{
    ret_code_t err_code;
    
    const nrf_drv_twi_config_t twi_nrf52_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };
    
    // Non-blocking, passing twi_handler when initializing driver
    err_code = nrf_drv_twi_init(&amp;amp;m_twi_nrf52, &amp;amp;twi_nrf52_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&amp;amp;m_twi_nrf52);
}&lt;/pre&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Initiate the TWI transaction in&amp;nbsp;heart_rate_meas_timeout_handler()&lt;/li&gt;
&lt;li&gt;Call&amp;nbsp;ble_hrs_heart_rate_measurement_send() from the TWI event handler to send the new heard rate measurement data. (shown above)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void heart_rate_meas_timeout_handler(TimerHandle_t xTimer)
{
    static uint32_t cnt = 0;
    ret_code_t      err_code;
    uint8_t        heart_rate;

    UNUSED_PARAMETER(xTimer);

    // Initiate TWI transaction here
    heart_rate = readHRS();
    
    cnt++;

}


void readHRS(uint8_t * read_long_data){
    ret_code_t err_code;
    
    err_code = nrf_drv_twi_tx(&amp;amp;m_twi_nrf52, HRS_ADDR, (uint8_t *) HRS_FIFO_DATA, sizeof(HRS_FIFO_DATA), true);

    if (NRF_SUCCESS == err_code){
	    err_code = nrf_drv_twi_rx(&amp;amp;m_twi_nrf52, HRS_ADDR, (uint8_t *) read_long_data, 6);
    }
    APP_ERROR_CHECK(err_code);

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The BLE can be connected, but automatically disconnects after one second.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I get these error codes when I run debug.&lt;/p&gt;
&lt;p&gt;&lt;img height="75" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1572535644458v1.png" width="421" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Please advise on what I have done wrong here.&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: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/207075?ContentTypeID=1</link><pubDate>Fri, 30 Aug 2019 09:07:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:34a9e7ac-a9e5-4969-906a-eb2fec0e029a</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you do as &lt;a href="https://devzone.nordicsemi.com/members/awneil"&gt;awneil&lt;/a&gt; suggests, you will see that 8 is&amp;nbsp;NRF_ERROR_INVALID_STATE. And that will be returned from&amp;nbsp;nrf_drv_twi_init() if it is already initialized. Looking at your code that will be the case, since you initialize it every time&amp;nbsp;heart_rate_meas_timeout_handler() is run, without uninitializing it in between. This is not legal. Either just initialize it once, or uninitialized it once you are done, and initialize it again before the next time. Also, you do not actually do a transaction in this code snippet, (only initialize the driver), so you will not get any sensor data with this code.&lt;/p&gt;
&lt;p&gt;This is a good example of why this kind of debugging is very useful. By looking at the error code and API documentation (sometimes together with a small peak into the function returned the error) you can most of the time pinpoint the problem or at least narrow it down significantly.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/206940?ContentTypeID=1</link><pubDate>Thu, 29 Aug 2019 16:05:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:17ae4386-0f36-4712-8187-e061b40d1cd9</guid><dc:creator>awneil</dc:creator><description>[quote userid="71618" url="~/f/nordic-q-a/51476/sending-actual-twi-sensor-values-through-ble/206924"]It seems like it is an error code 8 in this line[/quote]
&lt;p&gt;Indeed.&lt;/p&gt;
&lt;p&gt;So look up what error code &lt;strong&gt;8&lt;/strong&gt; actually means.&lt;/p&gt;
&lt;p&gt;The documentation for&amp;nbsp;&lt;span&gt;&lt;strong&gt;nrf_drv_twi_init&lt;/strong&gt; will list the possible error&amp;nbsp; codes it can return ...&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/206924?ContentTypeID=1</link><pubDate>Thu, 29 Aug 2019 14:33:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2fce8251-0b0f-45fb-a2c6-0440bdec0492</guid><dc:creator>KCY</dc:creator><description>&lt;p&gt;Hi Einar,&lt;/p&gt;
&lt;p&gt;Thank you for your response. I&lt;span&gt;&amp;nbsp;called the twi_init() function in heart_rate_meas_timeout_handler(). And then in twi_init(), the&amp;nbsp;TWI event handler is used to ensure non-blocking mode. Then the readHRS() and ble_hrs_heart_rate_measurement_send() functions are called from the TWI event handler as shown in the code below. With these fixes, I am able to connect the device to the app through BLE, but it disconnects&amp;nbsp; automatically after a second.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Did I misunderstand anything from your suggestion?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    ret_code_t err_code;
    uint16_t heart_rate;
    static sample_t m_sample;
    switch (p_event-&amp;gt;type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if (p_event-&amp;gt;xfer_desc.type == NRF_DRV_TWI_XFER_RX)
            {
                heart_rate = readHRS();
                
                err_code = ble_hrs_heart_rate_measurement_send(&amp;amp;m_hrs, heart_rate);
                if ((err_code != NRF_SUCCESS) &amp;amp;&amp;amp;
                    (err_code != NRF_ERROR_INVALID_STATE) &amp;amp;&amp;amp;
                    (err_code != NRF_ERROR_RESOURCES) &amp;amp;&amp;amp;
                    (err_code != NRF_ERROR_BUSY) &amp;amp;&amp;amp;
                    (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
                   )
                {
                    APP_ERROR_HANDLER(err_code);
                }
            }
            m_xfer_done = true;
            break;
        default:
            break;
    }
}


void twi_init (void)
{
    ret_code_t err_code;
    
    const nrf_drv_twi_config_t twi_nrf52_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };
    
    err_code = nrf_drv_twi_init(&amp;amp;m_twi_nrf52, &amp;amp;twi_nrf52_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&amp;amp;m_twi_nrf52);
}


void heart_rate_meas_timeout_handler(TimerHandle_t xTimer)
{
    static uint32_t cnt = 0;
    ret_code_t      err_code;
    uint16_t        heart_rate;

    UNUSED_PARAMETER(xTimer);
    
    twi_init();
      
    cnt++;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;With the debugging tool, I received these errors.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1567088958716v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;It seems like it is an error code 8 in this line in the twi_init() function.&lt;/p&gt;
&lt;p&gt;err_code = nrf_drv_twi_init(&amp;amp;m_twi_nrf52, &amp;amp;twi_nrf52_config, twi_handler, NULL);&lt;/p&gt;
&lt;p&gt;What might be causing this issue?&lt;/p&gt;
&lt;p&gt;&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: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/206858?ContentTypeID=1</link><pubDate>Thu, 29 Aug 2019 12:06:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:775617a0-4a73-4277-bfef-7c506c359c11</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="chkan"]Yes, so I tried commenting out lines of code that are used in the readHRS() function and I was only able to connect to the device through BLE when I commented out &lt;em&gt;nrf_drv_twi_tx()&lt;/em&gt;&amp;nbsp;and&amp;nbsp;&lt;em&gt;nrf_drv_twi_rx()&lt;/em&gt;.So I thought that was the issue.[/quote]
&lt;p&gt;I see. What I meant by &amp;quot;debugging&amp;quot; in this context was more like using a debugging tool to see what is actually going on. What prevents BLE from working when you use TWI to communicate with the sensor? Is an error returned somewhere? Something along &lt;a href="https://www.youtube.com/watch?v=uP8RYgYGRvI"&gt;these lines.&lt;/a&gt;&amp;nbsp;That said, the combination of your code and that TWI transactions seem to cause the problem gives us a good clue, as described in the previous post.&lt;/p&gt;
[quote user="chkan"]Can you elaborate more on how I can do this? Right now I am&amp;nbsp;calling the&amp;nbsp;&lt;em&gt;ble_hrs_heart_rate_measurement_send&lt;/em&gt;() in the&amp;nbsp;&lt;em&gt;heart_rate_meas_timeout_handler&lt;/em&gt;() and&amp;nbsp;using&amp;nbsp;the&amp;nbsp;&lt;em&gt;heart_rate_meas_timeout_handler&lt;/em&gt;() in &lt;em&gt;timer_init&lt;/em&gt;()[/quote]
&lt;p&gt;Yes. I will repeat the suggestion in my previous post in more detail:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The idea is that you should use asynchronous (non-blocking) TWI transactions. To do so:&lt;/li&gt;
&lt;li&gt;Make sure you configure the TWI driver to be non-blocking and use DMA. That means that you have to pass an event handler when you initialize the driver.&lt;/li&gt;
&lt;li&gt;Initiate the TWI transaction in&amp;nbsp;heart_rate_meas_timeout_handler(). Since this is now a non-blocking call, it will not cause problems for BLE or anything else.&lt;/li&gt;
&lt;li&gt;The TWI transaction is now handled by HW, so the CPU can do whatever it needs, including BLE stuff.&lt;/li&gt;
&lt;li&gt;When the TWI transaction is finished, your TWI event handler will be called.&lt;/li&gt;
&lt;li&gt;Call&amp;nbsp;ble_hrs_heart_rate_measurement_send() from the TWI event handler to send the new heard rate measurement data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The important difference here compared to your implementation is that instead of spending a lot of time just busy waiting in&amp;nbsp;heart_rate_meas_timeout_handler(), the CPU will be idle, available for handling other events. I do not &lt;em&gt;know&lt;/em&gt; if this is a solution since you have not tracked down &lt;em&gt;why&lt;/em&gt; your calls to&amp;nbsp;nrf_drv_twi_tx() and nrf_drv_twi_rx(), but it seems very likely.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/206700?ContentTypeID=1</link><pubDate>Wed, 28 Aug 2019 15:40:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7d9a978f-34c5-4534-966b-e2831a4fd60e</guid><dc:creator>KCY</dc:creator><description>&lt;p&gt;&lt;strong&gt;What do you mean by &amp;quot;when I do this, I am not able to the device anymore.&amp;quot;?&amp;nbsp;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Sorry, I meant that I wasn&amp;#39;t able to connect to the device anymore when tried to modify the&amp;nbsp;&lt;em&gt;heart_rate_meas_timeout_handler&lt;/em&gt;&amp;nbsp;function of the code to take value from the sensor instead of&amp;nbsp;&lt;em&gt;sensorsim_measure().&amp;nbsp;&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Have you done any debugging to find out what actually happens?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Yes, so I tried commenting out lines of code that are used in the readHRS() function and I was only able to connect to the device through BLE when I commented out &lt;em&gt;nrf_drv_twi_tx()&lt;/em&gt;&amp;nbsp;and&amp;nbsp;&lt;em&gt;nrf_drv_twi_rx()&lt;/em&gt;.So I thought that was the issue.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Then you can call&amp;nbsp;ble_hrs_heart_rate_measurement_send() from the TWI event handler.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Can you elaborate more on how I can do this? Right now I am&amp;nbsp;calling the&amp;nbsp;&lt;em&gt;ble_hrs_heart_rate_measurement_send&lt;/em&gt;() in the&amp;nbsp;&lt;em&gt;heart_rate_meas_timeout_handler&lt;/em&gt;() and&amp;nbsp;using&amp;nbsp;the&amp;nbsp;&lt;em&gt;heart_rate_meas_timeout_handler&lt;/em&gt;() in &lt;em&gt;timer_init&lt;/em&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void timers_init(void)
{
    // Initialize timer module.
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    // Create timers.
    m_battery_timer = xTimerCreate(&amp;quot;BATT&amp;quot;,
                                   BATTERY_LEVEL_MEAS_INTERVAL,
                                   pdTRUE,
                                   NULL,
                                   battery_level_meas_timeout_handler);
    m_heart_rate_timer = xTimerCreate(&amp;quot;HRT&amp;quot;,
                                      HEART_RATE_MEAS_INTERVAL,
                                      pdTRUE,
                                      NULL,
                                      heart_rate_meas_timeout_handler);
    m_rr_interval_timer = xTimerCreate(&amp;quot;RRT&amp;quot;,
                                       RR_INTERVAL_INTERVAL,
                                       pdTRUE,
                                       NULL,
                                       rr_interval_timeout_handler);
    m_sensor_contact_timer = xTimerCreate(&amp;quot;SCT&amp;quot;,
                                          SENSOR_CONTACT_DETECTED_INTERVAL,
                                          pdTRUE,
                                          NULL,
                                          sensor_contact_detected_timeout_handler);

    /* Error checking */
    if ( (NULL == m_battery_timer)
         || (NULL == m_heart_rate_timer)
         || (NULL == m_rr_interval_timer)
         || (NULL == m_sensor_contact_timer) )
    {
        APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    }
}


static void heart_rate_meas_timeout_handler(TimerHandle_t xTimer)
{
    static uint32_t cnt = 0;
    ret_code_t      err_code;
    uint8_t        heart_rate;

    UNUSED_PARAMETER(xTimer);

    //heart_rate = (uint16_t)sensorsim_measure(&amp;amp;m_heart_rate_sim_state, &amp;amp;m_heart_rate_sim_cfg);
    heart_rate = readHRS();
    
    cnt++;
    err_code = ble_hrs_heart_rate_measurement_send(&amp;amp;m_hrs, heart_rate);
    if ((err_code != NRF_SUCCESS) &amp;amp;&amp;amp;
        (err_code != NRF_ERROR_INVALID_STATE) &amp;amp;&amp;amp;
        (err_code != NRF_ERROR_RESOURCES) &amp;amp;&amp;amp;
        (err_code != NRF_ERROR_BUSY) &amp;amp;&amp;amp;
        (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
       )
    {
        APP_ERROR_HANDLER(err_code);
    }

    // Disable RR Interval recording every third heart rate measurement.
    // NOTE: An application will normally not do this. It is done here just for testing generation
    // of messages without RR Interval measurements.
    //m_rr_interval_enabled = ((cnt % 3) != 0);
}


void readHRS(uint8_t * read_long_data){
    ret_code_t err_code;
    
    err_code = nrf_drv_twi_tx(&amp;amp;m_twi_nrf52, HRS_ADDR, (uint8_t *) HRS_FIFO_DATA, sizeof(HRS_FIFO_DATA), true);

    if (NRF_SUCCESS == err_code){
	    err_code = nrf_drv_twi_rx(&amp;amp;m_twi_nrf52, HRS_ADDR, (uint8_t *) read_long_data, 6);
    }
    APP_ERROR_CHECK(err_code);

}&lt;/pre&gt;&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;em&gt;&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;em&gt;&amp;nbsp;&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending actual TWI sensor values through BLE</title><link>https://devzone.nordicsemi.com/thread/206667?ContentTypeID=1</link><pubDate>Wed, 28 Aug 2019 14:05:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:da41c4f0-bf25-44f8-ab51-7fd89294dee0</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The&amp;nbsp;heart_rate_meas_timeout_handler() is just an app_timer timeout handler function, so it should not be any problems starting a TWI transaction here.&lt;/p&gt;
&lt;p&gt;What do you mean by &amp;quot;when I do this, I am not able to the device anymore.&amp;quot;? Have you done any debugging to find out what actually happens?&lt;/p&gt;
&lt;p&gt;Without knowing more, the one thing that stands out is your call to nrf_delay_ms(). Note that this is in an RTC interrupt routine (app_timer), so any same or lower interrupt priorities will be blocked. You should not use nrf_delay_ms or other forms of bussy waiting. Instead, it would make sense to start the TWI transfer and then wait for an event when the TWI transfer has completed. Then you can call&amp;nbsp;ble_hrs_heart_rate_measurement_send() from the TWI event handler.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>