This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Good way to measure distance between two falling edges on one input pin?

I understand I'd be using 16MHz timer for this and it worked on Arduino nano.

I'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 1 and 1100 approximately.

This distance value would have to be converted in to 8bit data and when the dongle receives 10 of these, it should send them over bluetooth(BLE).

I'm struggling to get collect the data correctly. Here's my work done so far.

example of ble_app_uart i added this.

uint32_t time1=0;
uint32_t time2=0;
uint32_t distance_1=0;
uint8_t data_high=0;
uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
uint8_t index1 = 0;
bool time_flag = true;
void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    uint32_t       err_code;
    if (time_flag){
        time1 = nrfx_timer_capture(&m_timer_capture, NRF_TIMER_CC_CHANNEL0);
    }else{
        time2 = nrfx_timer_capture(&m_timer_capture, NRF_TIMER_CC_CHANNEL0);;
    }
    time_flag = !time_flag;
    NRF_LOG_INFO("time1 = %d, time2 = %d", time1, time2);

    distance_1 = app_timer_cnt_diff_compute(time2,time1);
    NRF_LOG_INFO("%d", distance_1);
    if (distance_1 < 1200){
        data_high = fmin( fmax(round((distance_1 - 16.0 - 52.0 - 16.0 + 4.0) / 4.0), 0.0), 255.0);
        data_array[index1]=data_high;
        index1++;
        
        if (index1 >= 10)
        {
            if (index1 >= 0)
            {
                do
                {
                    NRF_LOG_INFO("H");
                    uint16_t length = 10;
                    err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                    if ((err_code != NRF_ERROR_INVALID_STATE) &&
                        (err_code != NRF_ERROR_RESOURCES) &&
                        (err_code != NRF_ERROR_NOT_FOUND))
                    {
                        APP_ERROR_CHECK(err_code);
                    }
                } while (err_code == NRF_ERROR_RESOURCES);
            }
            index1 = 0;
        }
    }
    

}

/**
 * @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, &in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);
    
    nrf_drv_gpiote_in_event_enable(PIN_IN, true);
}

/**@brief Function for initializing the timer module.
 */
static void timers_init(void)
{
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
    
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrfx_timer_init(&m_timer_capture, &timer_cfg, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_timer_enable(&m_timer_capture);
    
}

Arduino uno runs this.

The log prints out like this as you can see in_pin_handler is not getting called fast enough and it's missing a lot of data. Too big values don't get sent.

but still once in a while the collected data reached 10 so it sends over bluetooth(BLE) and this is what I've got.

But this python data is running very slow because of all the missing data. 10data in approximately 2 minutes every time.

it seems it isn't impossible to measure such a short distance like 1078. Why is it happening so rarely?

What's the best way?

Parents
  • I can't really think of a good solution here. 

    Maybe you can use SPI for this, e.g. that you prepare an SPI read of for instance 10bytes @ 1Mbps (this will take ~80us) 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)).

    Kenneth

Reply
  • I can't really think of a good solution here. 

    Maybe you can use SPI for this, e.g. that you prepare an SPI read of for instance 10bytes @ 1Mbps (this will take ~80us) 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)).

    Kenneth

Children
  • I'm not sure how spi would help. The incoming data is the form of consecutive 2 chips. it has to be measured with gpiote.

    And the sending module doesn't have pins for spi

    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.

    So the problem is less worse(?) than I thought. Any ideas?

    Can you see this video? if you can't see it go to https://youtu.be/C_S65sW2kFI

  • What is the priority level of your GPIOTE interrupt handler? Can you for instance try to set NRFX_GPIOTE_CONFIG_IRQ_PRIORITY 2 and see if that helps?

    Kenneth

  • It didn't help but I found a strange phenomenon.

    It was when I measure the current with ppk, it misses a lot of data.

    If I don't measure it with nrf connect app It receives and sends data just fine.

  • I thought it was working okay without measuring current. But it had problems when two chips had small gap.

    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'm trying to use ppi channels for accurate measurement.

    But it is too complicated to include ppi in this purpose. What can I do?

  • I've figured out how to use ppi for this purposes in a smaller scale ppi peripheral example.

    The following code works.

    /**
     * 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 "AS IS" 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 <stdint.h>
    
    #include "nrf_delay.h"
    #include "app_error.h"
    
    #include "nrf_drv_ppi.h"
    #include "nrf_drv_timer.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #include "nrf_drv_gpiote.h"
    #include "nrfx_gpiote.h"
    #include <math.h>
    
    #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(&m_timer_capture, NRF_TIMER_CC_CHANNEL0);
        nrfx_timer_clear(&m_timer_capture);
        //NRF_LOG_INFO("%d", distance_1);
        if (distance_1 < 1200 && distance_1 > 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("%d", 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(&m_timer_capture, NRF_TIMER_CC_CHANNEL0);
        //nrfx_timer_clear(&m_timer_capture);
        //NRF_LOG_INFO("%d", distance_1);
        //if(true){
        if (distance_1 < 1200 && distance_1 > 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("%d", 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, &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(&m_timer_capture, &timer_cfg, timer_handler_read);
        APP_ERROR_CHECK(err_code);
        err_code = nrfx_timer_init(&m_timer_compare, &timer_cfg, timer_handler_compare);
        APP_ERROR_CHECK(err_code);
    
        uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer_compare, 10);
        nrf_drv_timer_extended_compare(&m_timer_compare,
                                       NRF_TIMER_CC_CHANNEL0,
                                       ticks,
                                       NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
                                       true);
    
        nrf_drv_timer_enable(&m_timer_capture);
        nrf_drv_timer_enable(&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(&ppi_channel_1);
        APP_ERROR_CHECK(err_code);
        
        err_code = nrf_drv_ppi_channel_alloc(&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(&m_timer_capture, NRF_TIMER_TASK_CAPTURE0);
        uint32_t timer_clear_task_addr    = nrf_drv_timer_task_address_get(&m_timer_capture, NRF_TIMER_TASK_CLEAR);
        
        uint32_t timer_read_compare_event_addr  = nrf_drv_timer_event_address_get(&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("PPI example started.");
    
        while (true)
        {
            if (NRF_LOG_PROCESS() == false)
            {
                __WFE();
                __SEV();
                __WFE();
            }
        }
    }
    
    /** @} */
    

    But now the problem is how I can send this data over ble.

    The data is distance_1 in timer_handler_compare(), which data will be scaled down to 8 bit data.

    I was trying to move the code above to ble_app_uart but I'm getting a fatal error. error code is 0x00001001

Related