/* * Copyright (C) 2019 Max Regan * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "Application/ButtonManager.h" #include "Bsp/SystemTime.h" #include "Bsp/macros.h" #include "Mcu.h" namespace BSP { using BSP::ReturnCode; using BSP::Schedule::NextTime; ButtonManager *ButtonManager::m_instance = nullptr; ReturnCode ButtonManager::init() { for (auto &btn: m_buttons) { /** Enable pin for input (pulled up) */ uint32_t index = btn.m_pin.get_index(); #if defined(STM32L0XX) // Unmask this interrupt SET(EXTI->IMR, 1u << index); // Enable this interrupt SET(EXTI->EMR, 1u << index); // Enable interrupt for rising edge SET(EXTI->RTSR, 1u << index); // Enable interrupt for falling edge SET(EXTI->FTSR, 1u << index); #elif defined(STM32L4XX) // Unmask this interrupt SET(EXTI->IMR1, 1u << index); // Enable this interrupt SET(EXTI->EMR1, 1u << index); // Enable interrupt for rising edge SET(EXTI->RTSR1, 1u << index); // Enable interrupt for falling edge SET(EXTI->FTSR1, 1u << index); #else #error "Unsupported device type" #endif } #if defined (STM32L0XX) CLR(SYSCFG->EXTICR[0], SYSCFG_EXTICR1_EXTI3 | SYSCFG_EXTICR1_EXTI2 | SYSCFG_EXTICR1_EXTI1 | SYSCFG_EXTICR1_EXTI0); NVIC_EnableIRQ(EXTI0_1_IRQn); NVIC_EnableIRQ(EXTI2_3_IRQn); NVIC_EnableIRQ(EXTI4_15_IRQn); NVIC_SetPriority(EXTI0_1_IRQn, 1); NVIC_SetPriority(EXTI2_3_IRQn, 1); NVIC_SetPriority(EXTI4_15_IRQn, 1); #elif defined(STM32L4XX) CLR(SYSCFG->EXTICR[0], SYSCFG_EXTICR1_EXTI3 | SYSCFG_EXTICR1_EXTI2 | SYSCFG_EXTICR1_EXTI1 | SYSCFG_EXTICR1_EXTI0); NVIC_EnableIRQ(EXTI0_IRQn); NVIC_EnableIRQ(EXTI1_IRQn); NVIC_EnableIRQ(EXTI2_IRQn); NVIC_EnableIRQ(EXTI3_IRQn); NVIC_EnableIRQ(EXTI4_IRQn); NVIC_EnableIRQ(EXTI9_5_IRQn); NVIC_EnableIRQ(EXTI15_10_IRQn); NVIC_SetPriority(EXTI0_IRQn, 1); NVIC_SetPriority(EXTI1_IRQn, 1); NVIC_SetPriority(EXTI2_IRQn, 1); NVIC_SetPriority(EXTI3_IRQn, 1); NVIC_SetPriority(EXTI4_IRQn, 1); NVIC_SetPriority(EXTI9_5_IRQn, 1); NVIC_SetPriority(EXTI15_10_IRQn, 1); #else #error "Unsupported device type" #endif return ReturnCode::OK; } NextTime ButtonManager::execute() { BSP::time_t systime; BSP::time_t endtime = 0; BSP::SystemTimer::get_time(systime); for (auto &btn: m_buttons) { // Has the state changed? if (btn.m_prev_call_state != btn.m_state) { // Have we 'debounced' this state? if (btn.m_state_change_ts + btn.m_debounce_time < systime) { // It's tiiiiime if (btn.m_callback != nullptr) { btn.m_callback(btn.m_state); } btn.m_prev_call_state = btn.m_state; continue; } else { // It's not time yet. Use this to figure out the next time we should check if (endtime == 0 || btn.m_state_change_ts + btn.m_debounce_time < endtime) { endtime = btn.m_state_change_ts + btn.m_debounce_time; } } } } if (endtime == 0) { return NextTime::never(); } else { return NextTime::at(endtime); } } void ButtonManager::set_callback(Button btn, ChangeCallback callback) { if (btn == Button::Count) { return; } m_buttons[btn].m_callback = callback; } void ButtonManager::remove_callback(Button btn) { if (btn == Button::Count) { return; } m_buttons[btn].m_callback = nop_callback; } void ButtonManager::irq() { static BSP::time_t systime; // TODO: is this call too expensive for an interrupt handler? //BSP::SystemTimer::get_time(systime); if (!m_instance) { return; } for (auto &btn: m_instance->m_buttons) { // Buttons are pulled up. Maybe this will be configurable one day. bool is_pressed = !btn.m_pin.read(); // Check if the button state has changed, and timestamp it if (is_pressed && (btn.m_state == ButtonState::NOT_PRESSED)) { btn.m_state_change_ts = systime; } else if (!is_pressed && (btn.m_state == ButtonState::PRESSED)) { btn.m_state_change_ts = systime; } btn.m_state = is_pressed ? ButtonState::PRESSED : ButtonState::NOT_PRESSED; // Clear the event #if defined(STM32L0XX) SET(EXTI->PR, 1u << btn.m_pin.get_index()); #elif defined (STM32L4XX) SET(EXTI->PR1, 1u << btn.m_pin.get_index()); #else #error "Unsupported device type" #endif } if (m_instance != nullptr) { m_instance->m_scheduler.add_task(*m_instance, NextTime::asap()); } } extern "C" void EXTI_1_0_IRQHandler() { static uint32_t irq_count = 0; ButtonManager::irq(); irq_count++; } extern "C" void EXTI_3_2_IRQHandler() { static uint32_t irq_count = 0; ButtonManager::irq(); irq_count++; } extern "C" void EXTI_15_4_IRQHandler() { static uint32_t irq_count = 0; ButtonManager::irq(); irq_count++; } }