2020-02-23 21:06:42 +01:00
|
|
|
/* Reflow Oven Controller
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*
|
|
|
|
* This file is part of the Reflow Oven Controller Project.
|
|
|
|
*
|
|
|
|
* The reflow oven controller is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with the reflow oven controller project.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-02-23 21:48:52 +01:00
|
|
|
#include <stm32/stm32f4xx.h>
|
2020-02-23 21:06:42 +01:00
|
|
|
#include <reflow-controller/button.h>
|
2020-02-25 21:01:34 +01:00
|
|
|
#include <stm-periph/stm32-gpio-macros.h>
|
2020-12-01 21:00:23 +01:00
|
|
|
#include <stm-periph/rcc-manager.h>
|
2020-02-23 21:48:52 +01:00
|
|
|
#include <stdint.h>
|
2020-08-18 19:57:13 +02:00
|
|
|
#include <helper-macros/helper-macros.h>
|
2020-02-25 21:01:34 +01:00
|
|
|
#include <cmsis/core_cm4.h>
|
|
|
|
#include <reflow-controller/systick.h>
|
2020-02-23 21:48:52 +01:00
|
|
|
|
2020-08-21 23:25:03 +02:00
|
|
|
static volatile uint64_t IN_SECTION(.ccm.bss) to_active_timestamp;
|
|
|
|
static volatile enum button_state IN_SECTION(.ccm.bss) int_state;
|
2020-08-30 18:20:58 +02:00
|
|
|
static volatile enum button_state IN_SECTION(.ccm.bss) override_state;
|
2020-02-23 21:48:52 +01:00
|
|
|
|
|
|
|
void button_init()
|
|
|
|
{
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(BUTTON_RCC_MASK));
|
2020-04-27 20:18:12 +02:00
|
|
|
rcc_manager_enable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(RCC_APB2ENR_SYSCFGEN));
|
2020-02-23 21:48:52 +01:00
|
|
|
BUTTON_PORT->MODER &= MODER_DELETE(BUTTON_PIN);
|
|
|
|
BUTTON_PORT->PUPDR &= PUPDR_DELETE(BUTTON_PIN);
|
|
|
|
BUTTON_PORT->PUPDR |= PULLUP(BUTTON_PIN);
|
|
|
|
|
2020-02-25 21:01:34 +01:00
|
|
|
to_active_timestamp = 0ULL;
|
|
|
|
int_state = BUTTON_IDLE;
|
2020-08-30 18:20:58 +02:00
|
|
|
override_state = BUTTON_IDLE;
|
2020-02-23 21:48:52 +01:00
|
|
|
|
2020-02-25 21:01:34 +01:00
|
|
|
SYSCFG->EXTICR[1] |= 0x3;
|
|
|
|
EXTI->IMR |= (1U<<4);
|
2020-04-27 20:18:12 +02:00
|
|
|
EXTI->RTSR |= (1U<<4);
|
2020-02-25 21:01:34 +01:00
|
|
|
EXTI->FTSR |= (1U<<4);
|
|
|
|
NVIC_EnableIRQ(EXTI4_IRQn);
|
2020-02-23 21:48:52 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 21:01:34 +01:00
|
|
|
enum button_state button_read_event()
|
|
|
|
{
|
|
|
|
uint64_t time_delta;
|
2020-04-27 20:18:12 +02:00
|
|
|
enum button_state temp_state;
|
2020-02-25 21:01:34 +01:00
|
|
|
|
2020-08-30 18:20:58 +02:00
|
|
|
if (override_state != BUTTON_IDLE) {
|
|
|
|
temp_state = override_state;
|
|
|
|
override_state = BUTTON_IDLE;
|
|
|
|
return temp_state;
|
|
|
|
}
|
|
|
|
|
2020-02-25 21:01:34 +01:00
|
|
|
if (BUTTON_PORT->IDR & (1U<<BUTTON_PIN)) {
|
2020-04-27 20:18:12 +02:00
|
|
|
temp_state = int_state;
|
2020-02-25 21:01:34 +01:00
|
|
|
int_state = BUTTON_IDLE;
|
2020-04-27 20:18:12 +02:00
|
|
|
return temp_state;
|
2020-02-25 21:01:34 +01:00
|
|
|
} else {
|
|
|
|
time_delta = systick_get_global_tick() - to_active_timestamp;
|
|
|
|
if (time_delta >= BUTTON_LONG_ON_TIME_MS)
|
|
|
|
return BUTTON_LONG;
|
|
|
|
else if (time_delta >= BUTTON_SHORT_ON_TIME_MS)
|
|
|
|
return BUTTON_SHORT;
|
|
|
|
else
|
|
|
|
return BUTTON_IDLE;
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 21:48:52 +01:00
|
|
|
|
|
|
|
void button_deinit()
|
|
|
|
{
|
|
|
|
BUTTON_PORT->MODER &= MODER_DELETE(BUTTON_PIN);
|
|
|
|
BUTTON_PORT->PUPDR &= PUPDR_DELETE(BUTTON_PIN);
|
|
|
|
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(BUTTON_RCC_MASK));
|
2020-04-27 20:18:12 +02:00
|
|
|
EXTI->IMR &= ~(1U<<4);
|
|
|
|
rcc_manager_disable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(RCC_APB2ENR_SYSCFGEN));
|
2020-02-23 21:48:52 +01:00
|
|
|
}
|
2020-02-25 21:01:34 +01:00
|
|
|
|
|
|
|
void EXTI4_IRQHandler(void)
|
|
|
|
{
|
|
|
|
uint64_t time_delta;
|
|
|
|
|
|
|
|
/* Clear interrupts */
|
|
|
|
EXTI->PR = EXTI->PR;
|
|
|
|
__DSB();
|
|
|
|
|
|
|
|
if (BUTTON_PORT->IDR & (1U<<BUTTON_PIN)) {
|
|
|
|
time_delta = systick_get_global_tick() - to_active_timestamp;
|
|
|
|
if (time_delta >= 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();
|
|
|
|
}
|
|
|
|
}
|
2020-08-30 18:20:58 +02:00
|
|
|
|
|
|
|
void button_override_event(enum button_state state)
|
|
|
|
{
|
|
|
|
override_state = state;
|
|
|
|
}
|