Combining nrfx gpiote driver with legacy hal\nrf_gpio

Setup : 

NRF Connect SDK v1.8.0

Zephyr 2.7.0

nRF52833

Hi, in my application I am using mostly the nrfx_gpiote driver, but for one of the 1-wire communication protocol, I am forced to use the hal nrf_gpio implementation because I have to switch between input and output on a specific pin very quickly. 

It is not achievable by using nrfx_gpiote init functions because they simply last too long. I mean nrfx_gpiote_in_init() and nrfx_gpiote_out_init(). 

To overcome that I am using nrf_gpio_pin_configure(). 

Is combining those two approaches legal?  It is possible that it will affect the behavior of other pins which use the nrfx_gpiote? 

How to add the interrupt handler by using the "hal approach" knowing that the interrupts are controlled by the nrfx_gpiote? Here I mean working directly on CONFIG, INTENSET, INTENCLR  registers.

Thanks in advance, 

  • Hi, Have you got any ideas?  Should I be more specific in that question? 

  • Hello,

    Is combining those two approaches legal?  It is possible that it will affect the behavior of other pins which use the nrfx_gpiote? 

    I see no reason why that wouldn't work.

    How to add the interrupt handler by using the "hal approach" knowing that the interrupts are controlled by the nrfx_gpiote? Here I mean working directly on CONFIG, INTENSET, INTENCLR  registers.

    To me it doesn't look like there is a way to use the gpiote HAL for this, so I think you need to use the driver directly.

  • Hi, sorry for late reply. I figured it out and I would like to share that, maybe it will be helpful for someone. Let me know if you notice any problems in my solution. I was testing that pretty long and I didn't notice any problems. 

    Ok, so in my scenario I have to switch between GPIO input and output on the same pin very quickly and when being configured as input be able to call interrupt handler. After some measurements I noticed that I can't use nrfx init, and uninit functions when I want to switch because they last to long.

    So to deal with that I implemented sth like this. First of all init function.

    static uint8_t GpioteSigChannel;
    void hal_gpio_sig_init() {
    /* Init GPIOTE module handling for TEST_SIG pin */
        nrfx_gpiote_in_config_t const in_config = {
            .sense = NRF_GPIOTE_POLARITY_LOTOHI,
            .pull = NRF_GPIO_PIN_NOPULL,
            .is_watcher = false,
            .hi_accuracy = true,
            .skip_gpio_setup = false,
        };
    
        /* Allocate gpiote channel externally during init, to be able to work
         * directly on nrf registers */
        err = nrfx_gpiote_channel_alloc(&GpioteSigChannel);
        if (err != NRFX_SUCCESS) {
            while (1)
                ;
        }
        /* Events only from Low to High transitions */
        err = nrfx_gpiote_in_prealloc_init(TEST_SIG_PIN,
                                           &in_config, GpioteSigChannel,
                                           gpio_sig_in_handler);
        if (err != NRFX_SUCCESS) {
            while (1)
                ;
        }
    
        gpio_sig_irq_enable();
    }


    The goal of the init function is to let the nrfx gpiote decide which channel should be used for that pin (nrfx_gpiote_channel_alloc()). The returned channel number is just a value from 0-7, which points to specific registers of nrf52833 gpiote module. Nest step is simple gpio as input init with given config, channel and handler for interrupt. The returned channel number is basically all we need to work directly on registers in next steps.

    Code below represents enabling the interrupts for that pin using hal nrf approach.

    static void gpio_sig_irq_enable(void) {
        nrf_gpiote_event_configure(NRF_GPIOTE, (uint32_t)GpioteSigChannel,
                                   TEST_SIG_PIN,
                                   NRF_GPIOTE_POLARITY_LOTOHI);
        nrf_gpiote_event_enable(NRF_GPIOTE, (uint32_t)GpioteSigChannel);
        nrf_gpiote_int_enable(NRF_GPIOTE, 0x1UL << (uint32_t)GpioteSigChannel);
    }

    So right now the pin is configured as input with interrupts enabled which will be handled by gpio_sig_in_handler(), cause nrfx gpiote saved the address of that function in its module.

    When there is a moment for switch to output the following functions are being called

    static void gpio_sig_set_as_output(void) {
        /* Set the PIN.CONF register before disabling GPIOTE module for SIG_TEST_PIN
         * pin */
        nrf_gpio_pin_clear(SIG_TEST_PIN);
        nrf_gpio_cfg(SIG_TEST_PIN, NRF_GPIO_PIN_DIR_OUTPUT,
                     NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL,
                     NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE);
        /* Disable GPIOTE for SIG_TEST_PIN pin, take config from PIN.CONF */
        gpio_sig_irq_disable();
    }


    static void gpio_sig_irq_disable(void) {
        /* Clear CONFIG register -> Pin takes his config from PIN.CNF register */
        nrf_gpiote_te_default(NRF_GPIOTE, (uint32_t)GpioteSigChannel);
        nrf_gpiote_int_disable(NRF_GPIOTE, 0x1UL << (uint32_t)GpioteSigChannel);
    }

    Here it might look not intuitive at all, but before we disable the gpiote module (cause we do not need any events and tasks handling when the pin is set to output) we call the nrf_gpio_clear() which sets the nrf52833 PIN.CONF registers in GPIO module and also sets the pin to be in low state at the beginning, so when the GPIOTE will be disabled,that pin will take its configuration from PIN.CONF rather the GPIOTE CONFIG register.

    The last thing to consider is going back from output to input.


    static void gpio_sig_set_as_input(void) {
        gpio_sig_irq_enable();
    }

    Remember that we have that pin already initialized as input in the gpiote module so there is no need to reconfig it, we have to only enable interrupts from that pin directly in the gpiote registers based on the preallocated channel.

    Above implementation reduces the time of switching between input and output from I guess 23us to 3-4us . Let me know if I cheated somewhere and if it might cause any problems  Slight smile

  • Pawel(embeddedsolutions.pl) said:
    Let me know if I cheated somewhere and if it might cause any problems

    I don't see any problems. I think it would be best to just run it for a while and see if you run into some issue.

Related