This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Measurement of time between two GPIOs

Hello,

I'm trying to measure the time difference between two pulses using GPIOTE, PPI and a Timer. My platform is: nRF52832 SoftDevice S132 v2.0

My thinking is to start the timer count on the Low to High edge of one pin, then stop the count on the Low to High edge of a second pin and retrieve the count. Here is my code but I am not sure that it is correct.

A few questions:

  1. nrf_drv_gpiote_in_init requires a handler, but I don't want interrupts for the initial task/trigger. The second one has an interrupt that will set a flag once measurement is complete. Is NULL acceptable here?

  2. Any possible issues? I'm using TIMER1 (TIMER0 is used by SoftDevice)

    nrf_ppi_channel_t ppi_channel1, ppi_channel2; const nrf_drv_timer_t timer0 = NRF_DRV_TIMER_INSTANCE(1);

    void init_measurement(void) { ret_code_t err_code;

     stop_flag = 0;
     
     err_code = nrf_drv_ppi_init();
     APP_ERROR_CHECK(err_code);
    
     err_code = nrf_drv_gpiote_init();
     APP_ERROR_CHECK(err_code);
         
     nrf_drv_gpiote_in_config_t configStartPin = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
     nrf_drv_gpiote_in_config_t configStopPin = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
    
     err_code = nrf_drv_gpiote_in_init(START_PIN, 
                                   &configStartPin,
                                   NULL);
     APP_ERROR_CHECK(err_code);
     
     err_code = nrf_drv_gpiote_in_init(STOP_PIN, 
                                   &configStopPin,
                                   stop_handler);
     APP_ERROR_CHECK(err_code);        
         
     // Configure 1st available PPI channel to start TIMER1 counter on PPI
     err_code = nrf_drv_ppi_channel_alloc(&ppi_channel1);
     APP_ERROR_CHECK(err_code);
     err_code = nrf_drv_ppi_channel_assign(ppi_channel1,
                                           nrf_drv_gpiote_in_event_addr_get(START_PIN),
                                           nrf_drv_timer_task_address_get(&timer0, NRF_TIMER_TASK_START));
     APP_ERROR_CHECK(err_code);
     
     // Configure 2nd available PPI channel to stop TIMER1 counter on PPI
     err_code = nrf_drv_ppi_channel_alloc(&ppi_channel2);
     APP_ERROR_CHECK(err_code);
     err_code = nrf_drv_ppi_channel_assign(ppi_channel2,
                                           nrf_drv_gpiote_in_event_addr_get(STOP_PIN),
                                           nrf_drv_timer_task_address_get(&timer0, NRF_TIMER_TASK_STOP));
     APP_ERROR_CHECK(err_code);
     
     nrf_drv_timer_capture(&timer0, NRF_TIMER_CC_CHANNEL0);
     nrf_drv_ppi_channel_enable(ppi_channel1);
     nrf_drv_ppi_channel_enable(ppi_channel2);
    

    }

    void stop_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { stop_flag = 1; }

    uint8_t measure(void) { stop_flag = 0;

     init_measurement();    
             
     while(stop_flag == 0);
         
     // Read count
     nrf_drv_timer_capture_get(&timer0, NRF_TIMER_CC_CHANNEL0);
         
     return 0;    
    

    }

Parents Reply Children
No Data
Related