diff --git a/stm-firmware/button.c b/stm-firmware/button.c index 0e402b3..6f0db95 100644 --- a/stm-firmware/button.c +++ b/stm-firmware/button.c @@ -20,10 +20,14 @@ #include #include +#include #include #include +#include +#include static volatile uint64_t to_active_timestamp; +static volatile enum button_state int_state; void button_init() { @@ -32,10 +36,33 @@ void button_init() BUTTON_PORT->PUPDR &= PUPDR_DELETE(BUTTON_PIN); BUTTON_PORT->PUPDR |= PULLUP(BUTTON_PIN); + to_active_timestamp = 0ULL; + int_state = BUTTON_IDLE; + SYSCFG->EXTICR[1] |= 0x3; + EXTI->IMR |= (1U<<4); + EXTI->FTSR |= (1U<<4); + EXTI->FTSR |= (1U<<4); + NVIC_EnableIRQ(EXTI4_IRQn); } -enum button_state button_read_event(); +enum button_state button_read_event() +{ + uint64_t time_delta; + + if (BUTTON_PORT->IDR & (1U<= BUTTON_LONG_ON_TIME_MS) + return BUTTON_LONG; + else if (time_delta >= BUTTON_SHORT_ON_TIME_MS) + return BUTTON_SHORT; + else + return BUTTON_IDLE; + } +} void button_deinit() { @@ -43,3 +70,23 @@ void button_deinit() BUTTON_PORT->PUPDR &= PUPDR_DELETE(BUTTON_PIN); rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(BUTTON_RCC_MASK)); } + +void EXTI4_IRQHandler(void) +{ + uint64_t time_delta; + + /* Clear interrupts */ + EXTI->PR = EXTI->PR; + __DSB(); + + if (BUTTON_PORT->IDR & (1U<= BUTTON_SHORT_ON_TIME_MS && time_delta < BUTTON_LONG_ON_TIME_MS) { + int_state = BUTTON_SHORT_RELEASED; + } else if (time_delta >= BUTTON_LONG_ON_TIME_MS) { + int_state = BUTTON_LONG_RELEASED; + } + } else { + to_active_timestamp = systick_get_global_tick(); + } +} diff --git a/stm-firmware/include/reflow-controller/button.h b/stm-firmware/include/reflow-controller/button.h index e5a72b7..46d648d 100644 --- a/stm-firmware/include/reflow-controller/button.h +++ b/stm-firmware/include/reflow-controller/button.h @@ -21,7 +21,6 @@ #ifndef __BUTTON_H__ #define __BUTTON_H__ -#define BUTTON_ACTIVE_LEVEL 0 #define BUTTON_PORT GPIOD #define BUTTON_RCC_MASK RCC_AHB1ENR_GPIODEN #define BUTTON_PIN 4 @@ -29,7 +28,7 @@ #define BUTTON_SHORT_ON_TIME_MS 50 #define BUTTON_LONG_ON_TIME_MS 800 -enum button_state {BUTTON_IDLE = 0, BUTTON_SHORT, BUTTON_LONG}; +enum button_state {BUTTON_IDLE = 0, BUTTON_SHORT_RELEASED, BUTTON_LONG_RELEASED, BUTTON_SHORT, BUTTON_LONG}; void button_init();