<?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>How to get the time difference between each TWI sensor measurement (microsecond-level)</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/77903/how-to-get-the-time-difference-between-each-twi-sensor-measurement-microsecond-level</link><description>Hi, I&amp;#39;m using an nRF52805 TWI sensor (400kHz, 1 complete received data is 9 bytes = 72 bits). The ideal time difference between each TWI sensor measurement is 72bits/400kHz = 18us. 
 Because my code contains TWI (low priority), SAADC and BLE, so TWI will</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 28 Jul 2021 12:15:53 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/77903/how-to-get-the-time-difference-between-each-twi-sensor-measurement-microsecond-level" /><item><title>RE: How to get the time difference between each TWI sensor measurement (microsecond-level)</title><link>https://devzone.nordicsemi.com/thread/322229?ContentTypeID=1</link><pubDate>Wed, 28 Jul 2021 12:15:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:74a68a22-c0e9-459c-8056-fc200c4e98ad</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;In the&amp;nbsp;saadc_callback() can you also print out the event type?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I suggest to get a logic analyzer or oscilloscope so you can measure the actual timing here.&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to get the time difference between each TWI sensor measurement (microsecond-level)</title><link>https://devzone.nordicsemi.com/thread/322022?ContentTypeID=1</link><pubDate>Tue, 27 Jul 2021 11:38:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3fc646f9-5607-459a-a71f-9146c8c1b507</guid><dc:creator>StevenW807</dc:creator><description>&lt;p&gt;Thank you very much, Kenneth. Nordic is so helpful and professional!&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As shown in the code, I have included&amp;nbsp;&lt;span&gt;uint32_t static t2, and I tried to print the timer1 value right after saadc_callback (interrupt every 50ms by PPI). &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_t static t;

...
...


void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    t = nrf_drv_timer_capture(&amp;amp;m_timer,NRF_TIMER_CC_CHANNEL0);
    printf(&amp;quot;%d\r\n&amp;quot;,t);

    if (p_event-&amp;gt;type == NRF_DRV_SAADC_EVT_DONE)
    {
      
      ...
      ...
    }
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Q1) &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;In theory, every time when the 1MHz timer1 reaches 50ms, timer1 gets cleared (i.e. timer1 value = 0). &lt;strong&gt;But why on PuTTY, the printed value is NOT always 0? &lt;/strong&gt;Here are some printed timer1 values on PuTTY:&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;17
0
371
17
0
372
17
0
401
17
0
395
17
0
378
17
0
405
17
0
417
17
0
370
17
0
397
17
0
372
17
0
368
17
0
371
17
0
372
17
0
366
17
0
372
29
17
0
372
17
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10
0
10&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;Q2) &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Also, I would like to check that the timer1 PPI interruption&amp;nbsp;is 50ms. &lt;strong&gt;How could I print the time1 value right before the interruption, please?&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Q3)&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;My timer1 is 1MHz, 32-bit. Does that mean 1 tick = 1us, the max tick value is 2^32 =&amp;nbsp;4294967296?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;If I change the timer1 to 125kHz and 8-bit, so 1 tick will be 1/125kHz = 8us, and the max tick value = 2^8 = 256 ticks = 2048 us, right?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks again, Kenneth!&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to get the time difference between each TWI sensor measurement (microsecond-level)</title><link>https://devzone.nordicsemi.com/thread/321994?ContentTypeID=1</link><pubDate>Tue, 27 Jul 2021 09:23:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:51a93b9f-6008-49c5-963c-df06163d6965</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;1. The clock frequency is set by the FREQUENCY register (it&amp;#39;s derived from the HFCLK internally, so you only need to set the register):&lt;br /&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf52805/twim.html#register.FREQUENCY"&gt;https://infocenter.nordicsemi.com/topic/ps_nrf52805/twim.html#register.FREQUENCY&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;2. I assume the timer is setup to clear on 50ms, I don&amp;#39;t see an easy way for the same timer to generate both 1ms and 50ms. Unless you update the timer every 1ms.&lt;/p&gt;
&lt;p&gt;3. Try to make the variables static, e.g. uint32_t static t2 (else t2 will likely be random).&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to get the time difference between each TWI sensor measurement (microsecond-level)</title><link>https://devzone.nordicsemi.com/thread/321961?ContentTypeID=1</link><pubDate>Tue, 27 Jul 2021 07:32:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b361aa4d-3584-4c87-8651-b86de3d41167</guid><dc:creator>StevenW807</dc:creator><description>&lt;p&gt;Thank&amp;nbsp;you very much, Kenneth,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I have a few more questions for nRF52805:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;1) On my custom board, I don&amp;#39;t have external clock. So my nRF52805 only has a 64MHZ system clock and 32.768kHz LFCLK. My TWI sensor operates at 400kHz, so it must use 64MHz HFCLK. However, if you look at&amp;nbsp;twi_sensor SDK example, there is no code to define the timer/clock for TWI. &lt;strong&gt;So how could the chip know TWI should use 64MHz? I mean, which code defines the clock for TWI?&amp;nbsp;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;2) I already used a 1MHz timer1 for SAADC PPI interrupt every 50ms. &lt;strong&gt;Could I also use this timer1 for TWI PPI interruption every 1ms?&amp;nbsp;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;3) I want to use timer1 to check that my SAADC PPI interrupt is 50ms correctly. However, on PuTTY, the printed time duration value is about 860, or 17, or just 0 (see screenshot). Sometimes, even negative values. My timer1 is 1MHz, timer mode, 32-bit, priority 6. &lt;strong&gt;According to my code, the duration value is in the unit of millisecond already, right?&amp;nbsp;Are the wrong values caused by overflow? How to solve it, please?&lt;/strong&gt; Here is a part of my code:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void saadc_sampling_event_init(void)
{
    ret_code_t err_code;
    // Initial PPI
    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);
    
    // Configure and initial timer 1
    // Open sdk_config.h to modify the NRF_DRV_TIMER_DEFAULT_CONFIG
    // Settings: 1MHz clock, timer mode, 32-bit, priority 6
    nrf_drv_timer_config_t timer_config = NRF_DRV_TIMER_DEFAULT_CONFIG;
    timer_config.frequency = NRF_TIMER_FREQ_1MHz;

    err_code = nrf_drv_timer_init(&amp;amp;m_timer, &amp;amp;timer_config, timer_handler);
    APP_ERROR_CHECK(err_code);

    /* setup m_timer for compare event */
    // Convert timer value to the # of ticks
    // Compare timer(i.e. # of ticks) with user-defined time interval
    uint32_t ticks = nrf_drv_timer_ms_to_ticks(&amp;amp;m_timer,SAADC_SAMPLE_RATE);
    // Clear the timer eveytime when the counter reaches the user-defined time interval
    nrf_drv_timer_extended_compare(&amp;amp;m_timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);
    nrf_drv_timer_enable(&amp;amp;m_timer);
    
    // Get timer event and SAADC event address for PPI 
    uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&amp;amp;m_timer, NRF_TIMER_CC_CHANNEL0);
    uint32_t saadc_sample_event_addr = nrf_drv_saadc_sample_task_get();

    /* setup ppi channel so that timer compare event is triggering sample task in SAADC */
    err_code = nrf_drv_ppi_channel_alloc(&amp;amp;m_ppi_channel);
    APP_ERROR_CHECK(err_code);
    
    // Time-based interrupt: Timer Event --&amp;gt; PPI --&amp;gt; SAADC Task
    err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, saadc_sample_event_addr);
    APP_ERROR_CHECK(err_code);
}



void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event-&amp;gt;type == NRF_DRV_SAADC_EVT_DONE)
    {
        
        uint32_t t1 = t2;
        uint32_t t2 = nrf_drv_timer_capture(&amp;amp;m_timer,NRF_TIMER_CC_CHANNEL0);
        uint32_t duration = t2-t1;
        printf(&amp;quot;Duration = %d\r\n&amp;quot;,duration);
        
        ...
        ...
    }
}

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1627370953993v2.png" /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1627370953975v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;Thanks!&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to get the time difference between each TWI sensor measurement (microsecond-level)</title><link>https://devzone.nordicsemi.com/thread/321953?ContentTypeID=1</link><pubDate>Tue, 27 Jul 2021 07:05:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:db5160b7-6107-4b34-9a4b-48de335390c8</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;If you have a timer running in parallell, then the simplest may be to create a ppi channel between the twi stopped event and the timer capture task. &lt;span&gt;Every time the CAPTURE[n] task is triggered, the Counter value is copied to the CC[n] register. Y&lt;/span&gt;ou can then read out the counter value&amp;nbsp;from the CC register of the timer between twi transfers.&lt;/p&gt;
&lt;p&gt;For more accurate timing of transfers you may consider to do the opposite, e.g. you can setup a timer to trigger twi transfers, such that on every timer compare event you can have a ppi channel that start the twi transfer, this will ensure you always transfer twi every 18us. You can find that the&amp;nbsp;\peripheral\saadc example in the nRF5 SDK show this, but instead of starting SAADC you will need to start TWI.&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>