<?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>Good way to measure distance between two falling edges on one input pin?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/80654/good-way-to-measure-distance-between-two-falling-edges-on-one-input-pin</link><description>I understand I&amp;#39;d be using 16MHz timer for this and it worked on Arduino nano. 
 I&amp;#39;m trying to measure the distance above with nRF52840 dongle. 
 Those two edges come in every 10ms(100Hz) 
 The timer value would have to be captured and subtracted to between</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 03 Dec 2021 01:25:58 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/80654/good-way-to-measure-distance-between-two-falling-edges-on-one-input-pin" /><item><title>RE: Good way to measure distance between two falling edges on one input pin?</title><link>https://devzone.nordicsemi.com/thread/341762?ContentTypeID=1</link><pubDate>Fri, 03 Dec 2021 01:25:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9387111b-a1a3-436b-9c09-54b22dd9d9d2</guid><dc:creator>Senchoi</dc:creator><description>&lt;p&gt;I&amp;#39;ve eventually merged the code above(ppi) and ble_app_uart successfully.&lt;/p&gt;
&lt;p&gt;strangely using timer2 instead of timer0 worked for me.&lt;/p&gt;
&lt;p&gt;This is the main.c file &lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/7433.main.c"&gt;devzone.nordicsemi.com/.../7433.main.c&lt;/a&gt;&amp;nbsp;for others to look at, but sdk_config would have to be adjusted accordingly.&lt;/p&gt;
&lt;p&gt;So this is the final answer to my question from the beginning.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Good way to measure distance between two falling edges on one input pin?</title><link>https://devzone.nordicsemi.com/thread/341643?ContentTypeID=1</link><pubDate>Thu, 02 Dec 2021 08:32:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c948355f-6169-4fb0-b37e-0b9278519592</guid><dc:creator>Senchoi</dc:creator><description>&lt;p&gt;I&amp;#39;ve figured out how to use ppi for this purposes in a smaller scale ppi peripheral example.&lt;/p&gt;
&lt;p&gt;The following code works.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA &amp;quot;AS IS&amp;quot; AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/** @file
*
* @defgroup ppi_example_main main.c
* @{
* @ingroup ppi_example
* @brief PPI Example Application main file.
*
* This file contains the source code for a sample application using PPI to communicate between timers.
*
*/
#include &amp;lt;stdint.h&amp;gt;

#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;

#include &amp;quot;nrf_drv_ppi.h&amp;quot;
#include &amp;quot;nrf_drv_timer.h&amp;quot;

#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#include &amp;quot;nrf_drv_gpiote.h&amp;quot;
#include &amp;quot;nrfx_gpiote.h&amp;quot;
#include &amp;lt;math.h&amp;gt;

#define PIN_IN  29
static const nrf_drv_timer_t m_timer_capture = NRFX_TIMER_INSTANCE(1);
static const nrf_drv_timer_t m_timer_compare = NRFX_TIMER_INSTANCE(0);

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){

    int distance_1;
    uint8_t data_high;
    distance_1 = nrfx_timer_capture(&amp;amp;m_timer_capture, NRF_TIMER_CC_CHANNEL0);
    nrfx_timer_clear(&amp;amp;m_timer_capture);
    //NRF_LOG_INFO(&amp;quot;%d&amp;quot;, distance_1);
    if (distance_1 &amp;lt; 1200 &amp;amp;&amp;amp; distance_1 &amp;gt; 70){
        data_high = fmin( fmax(round((distance_1 - 16.0 - 52.0 - 16.0 + 4.0) / 4.0), 0.0), 255.0);
        NRF_LOG_INFO(&amp;quot;%d&amp;quot;, data_high);
    }

}

void timer_handler_read(nrf_timer_event_t event_type, void * p_context)
{}

void timer_handler_compare(nrf_timer_event_t event_type, void * p_context)
{
    int distance_1;
    uint8_t data_high;
    distance_1 = nrfx_timer_capture_get(&amp;amp;m_timer_capture, NRF_TIMER_CC_CHANNEL0);
    //nrfx_timer_clear(&amp;amp;m_timer_capture);
    //NRF_LOG_INFO(&amp;quot;%d&amp;quot;, distance_1);
    //if(true){
    if (distance_1 &amp;lt; 1200 &amp;amp;&amp;amp; distance_1 &amp;gt; 70){
        //data_high = fmin( fmax(round((distance_1 - 16.0 - 52.0 - 16.0 + 4.0) / 4.0), 0.0), 255.0);
        NRF_LOG_INFO(&amp;quot;%d&amp;quot;, distance_1);
    }
}

/**
 * @brief Function for configuring: PIN_IN pin for input sensing
 */
static void gpiote_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    in_config.pull = NRF_GPIO_PIN_PULLDOWN;

    err_code = nrf_drv_gpiote_in_init(PIN_IN, &amp;amp;in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);
    
    nrf_drv_gpiote_in_event_enable(PIN_IN, false);
}

/**@brief Function for initializing the timer module.
 */
static void timers_init(void)
{
    ret_code_t err_code;
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    
    err_code = nrfx_timer_init(&amp;amp;m_timer_capture, &amp;amp;timer_cfg, timer_handler_read);
    APP_ERROR_CHECK(err_code);
    err_code = nrfx_timer_init(&amp;amp;m_timer_compare, &amp;amp;timer_cfg, timer_handler_compare);
    APP_ERROR_CHECK(err_code);

    uint32_t ticks = nrf_drv_timer_ms_to_ticks(&amp;amp;m_timer_compare, 10);
    nrf_drv_timer_extended_compare(&amp;amp;m_timer_compare,
                                   NRF_TIMER_CC_CHANNEL0,
                                   ticks,
                                   NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
                                   true);

    nrf_drv_timer_enable(&amp;amp;m_timer_capture);
    nrf_drv_timer_enable(&amp;amp;m_timer_compare);
    
}
nrf_ppi_channel_t ppi_channel_1, ppi_channel_2;
void ppi_init()
{
    ret_code_t err_code;

    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);
    
    err_code = nrf_drv_ppi_channel_alloc(&amp;amp;ppi_channel_1);
    APP_ERROR_CHECK(err_code);
    
    err_code = nrf_drv_ppi_channel_alloc(&amp;amp;ppi_channel_2);
    APP_ERROR_CHECK(err_code);
    
    uint32_t gpiote_evt_addr_1              = nrf_drv_gpiote_in_event_addr_get(PIN_IN);
    
    uint32_t timer_capture_task_addr  = nrf_drv_timer_task_address_get(&amp;amp;m_timer_capture, NRF_TIMER_TASK_CAPTURE0);
    uint32_t timer_clear_task_addr    = nrf_drv_timer_task_address_get(&amp;amp;m_timer_capture, NRF_TIMER_TASK_CLEAR);
    
    uint32_t timer_read_compare_event_addr  = nrf_drv_timer_event_address_get(&amp;amp;m_timer_capture, NRF_TIMER_EVENT_COMPARE0);
    
    
    err_code = nrf_drv_ppi_channel_assign(ppi_channel_1, gpiote_evt_addr_1, timer_capture_task_addr);
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_ppi_channel_fork_assign(ppi_channel_1, timer_clear_task_addr);
    APP_ERROR_CHECK(err_code);
    
    err_code = nrf_drv_ppi_channel_enable(ppi_channel_1);
    APP_ERROR_CHECK(err_code);
    //err_code = nrf_drv_ppi_channel_enable(ppi_channel_2);
    APP_ERROR_CHECK(err_code);
}
/**
 * @brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;

    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();

    gpiote_init();
    timers_init();
    ppi_init();

    NRF_LOG_INFO(&amp;quot;PPI example started.&amp;quot;);

    while (true)
    {
        if (NRF_LOG_PROCESS() == false)
        {
            __WFE();
            __SEV();
            __WFE();
        }
    }
}

/** @} */
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;But now the problem is how I can send this data over ble.&lt;/p&gt;
&lt;p&gt;The data is distance_1 in timer_handler_compare(), which data will be scaled down to 8 bit data.&lt;/p&gt;
&lt;p&gt;I was trying to move the code above to ble_app_uart but I&amp;#39;m getting a fatal error. error code is 0x00001001&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Good way to measure distance between two falling edges on one input pin?</title><link>https://devzone.nordicsemi.com/thread/340634?ContentTypeID=1</link><pubDate>Thu, 25 Nov 2021 05:44:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:43f4c5b5-c89d-43b8-98ee-b90659d4e75c</guid><dc:creator>Senchoi</dc:creator><description>&lt;p&gt;I thought it was working okay without measuring current. But it had problems when two chips had small gap.&lt;/p&gt;
&lt;p&gt;the test device is running gradually increasing gap between two chips. But the data shows consecutive and same small number at the start. So I guess I have to change the code but this time I&amp;#39;m trying to use ppi channels for accurate measurement.&lt;/p&gt;
&lt;p&gt;But it is too complicated to include ppi in this purpose. What can I do?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Good way to measure distance between two falling edges on one input pin?</title><link>https://devzone.nordicsemi.com/thread/334967?ContentTypeID=1</link><pubDate>Wed, 20 Oct 2021 03:22:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:78c91fcb-517b-4ef9-9916-94fc99a95694</guid><dc:creator>Senchoi</dc:creator><description>&lt;p&gt;It didn&amp;#39;t help but I found a strange phenomenon.&lt;/p&gt;
&lt;p&gt;It was when I measure the current with ppk, it misses a lot of data.&lt;/p&gt;
&lt;p&gt;If I don&amp;#39;t measure it with nrf connect app It receives and sends data just fine.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Good way to measure distance between two falling edges on one input pin?</title><link>https://devzone.nordicsemi.com/thread/334599?ContentTypeID=1</link><pubDate>Mon, 18 Oct 2021 12:09:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b327ee1-9370-46bc-a56e-bbdc6ec89062</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;What is the priority level of your GPIOTE interrupt handler? Can you for instance try to set&amp;nbsp;NRFX_GPIOTE_CONFIG_IRQ_PRIORITY 2 and see if that helps?&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Good way to measure distance between two falling edges on one input pin?</title><link>https://devzone.nordicsemi.com/thread/334259?ContentTypeID=1</link><pubDate>Fri, 15 Oct 2021 06:09:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8dcaaab1-497e-4849-a7e5-c1806ff3db90</guid><dc:creator>Senchoi</dc:creator><description>&lt;p&gt;I&amp;#39;m not sure how spi would help. The incoming data is the form of consecutive 2 chips. it has to be measured with gpiote.&lt;/p&gt;
&lt;p&gt;And the sending module doesn&amp;#39;t have pins for spi&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Also there was a little bit of misunderstanding when interpreting J-link RTT Viewer. There is a period of sending correct data (1079 approximately) constantly and the other period constantly calculate distance in ridiculously big amount.&lt;/p&gt;
&lt;p&gt;So the problem is less worse(?) than I thought. Any ideas?&lt;/p&gt;
&lt;p&gt;Can you see this video? if you can&amp;#39;t see it go to&amp;nbsp;&lt;a href="https://youtu.be/C_S65sW2kFI"&gt;https://youtu.be/C_S65sW2kFI&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/6082.KakaoTalk_5F00_20211015_5F00_160929749.mp4"&gt;devzone.nordicsemi.com/.../6082.KakaoTalk_5F00_20211015_5F00_160929749.mp4&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Good way to measure distance between two falling edges on one input pin?</title><link>https://devzone.nordicsemi.com/thread/334189?ContentTypeID=1</link><pubDate>Thu, 14 Oct 2021 13:37:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d701a86f-e621-4020-abc3-8c6f1b4fb5b6</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;I can&amp;#39;t really think of a good solution here.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Maybe you can use SPI for this, e.g. that you prepare an SPI read of for instance 10bytes @ 1Mbps (this will take ~80us)&amp;nbsp;such that on negative edge you connect a GPIOTE channel using PPI to the start task of SPI. Then if the input is connected to both GPIOTE and MISO pin, then on SPI complete event you can read the 10bytes to find the first transition in the rx buffer, each bit in the buffer will represent 1us. Though this would require 4 pins (1 GPIOTE input + 3 SPI (miso, mosi and clock)).&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>