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 
Parents Reply Children
Related