NFR52840 button P1.02 and P0.02 callback

How Can I store information about button port ? I have two buttons P1.02 and P0.02. I have 

static struct gpio_callback button_cb_data[2];

And I have one function to init button

void ButtonInit(const struct gpio_dt_spec* button, gpio_flags_t type, int position) {

    if (!inited) {
        k_timer_init(&firstButtonTimer, FirstButtonExpired, NULL);

        k_timer_init(&secondButtonTimer, SecondButtonExpired, NULL);
        
        k_timer_init(&resetTimer, ExpiryReset, NULL);
        inited = true;
    }

    int ret = 0;
    if (!device_is_ready(button->port)) {
        return;
    }

    ret = gpio_pin_configure(button->port, button->pin, GPIO_INPUT | GPIO_INT_DEBOUNCE | GPIO_PULL_UP);
    if (ret != 0) {
        return;
    }

    ret = gpio_pin_interrupt_configure(button->port, button->pin, type);
    if (ret != 0) {
        return;
    }

    gpio_init_callback(&button_cb_data[position], Button_pressed, BIT(32 * button->port + button->pin));

    gpio_add_callback(button->port, &button_cb_data[position]);

    printk("Set up BUTTON at %s pin %d\n", button->port->name, button->pin);
}

the most interesting is callback because I thought If I have pin bitmask I can use the same callback for gpio1 and gpio0. But somewhere information about port is missing.

void Button_pressed(const struct device* dev, struct gpio_callback* cb, uint32_t pins) {
    dev1 = dev;

    uint32_t bit_to_pin = 0;
	for (int i = 0; i < 32; i++) {
		if ((pins >> i) == 1) {
			bit_to_pin = i;
			break;
		}
	}

    HAPLogError(&kHAPLog_Default, "button pressed first. %d", bit_to_pin);
    // first button pressed 
    if (accessoryConfiguration.driver.firstRelayButton == bit_to_pin) {
        HAPLogError(&kHAPLog_Default, "button pressed first.");
        if (accessoryConfiguration.driver.firstButtonState != gpio_pin_get(dev, accessoryConfiguration.driver.firstRelayButton))
        {
            accessoryConfiguration.driver.firstButtonState = gpio_pin_get(dev, accessoryConfiguration.driver.firstRelayButton);
            if (accessoryConfiguration.driver.firstButtonState && !debounceTimerStarted)
            {
                k_timer_start(&firstButtonTimer, K_MSEC(INTERVAL), K_NO_WAIT);
                debounceTimerStarted = true;
            }
            else if(!accessoryConfiguration.driver.firstButtonState && !debounceTimerStarted && accessoryConfiguration.buttonType)
            {
                k_timer_start(&firstButtonTimer, K_MSEC(INTERVAL), K_NO_WAIT);
                debounceTimerStarted = true;
            }
        } 
    }
    // test button pressed
    else if (accessoryConfiguration.driver.testButtonPin == bit_to_pin) {

        if (accessoryConfiguration.driver.testButtonState != gpio_pin_get(dev, accessoryConfiguration.driver.testButtonPin)) 
        {
            accessoryConfiguration.driver.testButtonState = gpio_pin_get(dev, accessoryConfiguration.driver.testButtonPin);

            if (accessoryConfiguration.driver.testButtonState) 
            {
                SetGpio(&testLed, true);

                if (!accessoryConfiguration.resetTimerRegistered)
                {
                    k_timer_start(&resetTimer, K_MSEC(RESET_INTERVAL), K_NO_WAIT);
                    accessoryConfiguration.resetTimerRegistered = true;
                }
            } 
            else if (!accessoryConfiguration.driver.testButtonState)
            {
                SetGpio(&testLed, false);
                if (accessoryConfiguration.resetTimerRegistered)
                {
                    k_timer_stop(&resetTimer);
                    accessoryConfiguration.resetTimerRegistered = false;
                }
            }
        }
    }

My question is that 
How Can I add button to store information about port and how can I read it in callback.

I was thinking about 

gpio_init_callback(&button_cb_data[position], Button_pressed, BIT(32 * button->port + button->pin));
but button->port is not an integer. 
I can use two different callbacks for gpio1 and gpio0 but it's odd solution :P 
  • Hi

    If understand you write then

    #definу gpio_0 11
    #definу gpio_1 12
    #definу gpio_2 24
    #definу gpio_3 25
    
    static void GPIO_EVENT(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
            LOG_INF("screaming pin %d", pin);
            LOG_INF("low or high state %d", action);
    }
    
    main()
    {
    	    IRQ_CONNECT(DT_IRQN(DT_NODELABEL(gpiote)),
    		DT_IRQ(DT_NODELABEL(gpiote), priority),
    		nrfx_isr, nrfx_gpiote_irq_handler, 0);
            nrfx_gpiote_init(NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY);
            nrfx_gpiote_in_config_t cfg = NRFX_GPIOTE_CONFIG_IN_SENSE_LOTOHI(false);
            cfg.pull = NRF_GPIO_PIN_PULLDOWN;
            nrfx_gpiote_in_init(gpio_0, &cfg, GPIO_EVENT);
            nrfx_gpiote_in_init(gpio_1, &cfg, GPIO_EVENT);
            nrfx_gpiote_in_init(gpio_2, &cfg, GPIO_EVENT);
            nrfx_gpiote_in_init(gpio_3, &cfg, GPIO_EVENT);
            nrfx_gpiote_in_event_enable(gpio_0, true);
            nrfx_gpiote_in_event_enable(gpio_1, true);
            nrfx_gpiote_in_event_enable(gpio_2, true);
            nrfx_gpiote_in_event_enable(gpio_3, true);
    }

  • Hi Michael, 

    In the callback you can get the port number of the pin that trigger the callback by checking the dev argument. So in the Button_pressed() function if you add this: 

    printk("Port of the button: %s ",dev->name);
    You can find which port the pin (s) is on. 
  • Ok so I need to compare string something like that 
    accessoryConfiguration.driver.testButtonPin == bit_to_pin && dev->name == 'GPIO_1'

Related