From 47224cbc84f5736b3357a7607f9df9c5b8f2a23b Mon Sep 17 00:00:00 2001
From: Florian Kargl <florian.kargl@student.tugraz.at>
Date: Wed, 28 Aug 2019 22:46:02 +0200
Subject: [PATCH] Unmask portexpander button interrupts by default + prevent
 redundant interrupts between polling

---
 lib/card10/portexpander.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lib/card10/portexpander.c b/lib/card10/portexpander.c
index c1406ea43..e84c259f7 100644
--- a/lib/card10/portexpander.c
+++ b/lib/card10/portexpander.c
@@ -123,6 +123,12 @@ int portexpander_init(void)
 	GPIO_IntEnable(&pe_int_pin);
 	NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(pe_int_pin.port));
 
+	// Unmask portexpander interrupts
+	portexpander_int_enable(PE_INPUT_MASK);
+
+	// Clear existing interrupts
+	portexpander_in_get(0);
+
 	return E_SUCCESS;
 }
 
@@ -304,6 +310,8 @@ int portexpander_register_callback(
 /* ************************************************************************** */
 __attribute__((weak)) void portexpander_interrupt_callback(void *_)
 {
+	GPIO_IntDisable(&pe_int_pin);
+	GPIO_IntClr(&pe_int_pin);
 	interrupt_pending = true;
 }
 
@@ -317,6 +325,8 @@ void portexpander_poll()
 		// Port read resets interrupts
 		uint8_t port_levels = portexpander_in_get(0xFF);
 
+		GPIO_IntEnable(&pe_int_pin);
+
 		for (uint8_t pin = 0; pin < 8; ++pin) {
 			if ((caused_by & (1 << pin)) && callbacks[pin]) {
 				gpio_int_pol_t edge_type =
-- 
GitLab