Hack in support for other boards/microcontrollers, add GpioDriver

There's definitely plenty of cleanup work to be done (see:
"ifdefs").
This commit is contained in:
2020-04-11 11:03:17 -07:00
parent 385402e7aa
commit 99317eb99b
17 changed files with 1156 additions and 99 deletions

View File

@@ -23,7 +23,8 @@
#include "Bsp/SystemTime.h"
#include "Bsp/macros.h"
#include "stm32l0xx.h"
#include "Mcu.h"
namespace BSP {
@@ -36,20 +37,33 @@ ReturnCode ButtonManager::init()
{
for (auto &btn: m_buttons) {
/** Enable pin for input (pulled up) */
CLR(GPIOA->MODER, 3u << (2 * btn.m_gpio_idx));
CLR(GPIOA->OTYPER, 1u << btn.m_gpio_idx);
SET_TO(GPIOA->PUPDR, 3u << (2 * btn.m_gpio_idx), 1u << (2 * btn.m_gpio_idx));
uint32_t index = btn.m_pin.get_index();
#if defined(STM32L0XX)
// Unmask this interrupt
SET(EXTI->IMR, 1u << btn.m_gpio_idx);
SET(EXTI->IMR, 1u << index);
// Enable this interrupt
SET(EXTI->EMR, 1u << btn.m_gpio_idx);
SET(EXTI->EMR, 1u << index);
// Enable interrupt for rising edge
SET(EXTI->RTSR, 1u << btn.m_gpio_idx);
SET(EXTI->RTSR, 1u << index);
// Enable interrupt for falling edge
SET(EXTI->FTSR, 1u << btn.m_gpio_idx);
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);
@@ -61,6 +75,31 @@ ReturnCode ButtonManager::init()
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;
}
@@ -71,7 +110,6 @@ NextTime ButtonManager::execute()
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) {
@@ -120,30 +158,36 @@ void ButtonManager::remove_callback(Button btn)
void ButtonManager::irq()
{
uint32_t idr = GPIOA->IDR;
static BSP::time_t systime;
// TODO: is this call too expensive for an interrupt handler?
BSP::SystemTimer::get_time(systime);
//BSP::SystemTimer::get_time(systime);
if (!m_instance) {
return;
}
for (auto &btn: m_instance->m_buttons) {
bool is_pressed = !(idr & (1 << btn.m_gpio_idx));
// 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 = ButtonState::PRESSED;
btn.m_state_change_ts = systime;
} else if (!is_pressed && (btn.m_state == ButtonState::PRESSED)) {
btn.m_state = ButtonState::NOT_PRESSED;
btn.m_state_change_ts = systime;
}
btn.m_state = is_pressed ? ButtonState::PRESSED : ButtonState::NOT_PRESSED;
// Clear the event
SET(EXTI->PR, 1u << btn.m_gpio_idx);
#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) {

View File

@@ -26,22 +26,23 @@
#include "Bsp/TaskScheduler.h"
#include "Bsp/Task.h"
#include "Bsp/ReturnCode.h"
#include "Bsp/Drivers/GpioDriver.h"
namespace BSP {
class ButtonManager : public BSP::Schedule::Task {
public:
ButtonManager(BSP::Schedule::TaskScheduler &scheduler,
uint8_t up_gpio_idx,
uint8_t mid_gpio_idx,
uint8_t down_gpio_idx,
GpioPin &btn_down,
GpioPin &btn_mid,
GpioPin &btn_up,
BSP::time_t debounce_time)
: m_scheduler(scheduler)
, m_buttons
{
button_state(up_gpio_idx, debounce_time, ButtonManager::nop_callback),
button_state(mid_gpio_idx, debounce_time, ButtonManager::nop_callback),
button_state(down_gpio_idx, debounce_time, ButtonManager::nop_callback),
button_state(btn_down, debounce_time, ButtonManager::nop_callback),
button_state(btn_mid, debounce_time, ButtonManager::nop_callback),
button_state(btn_up, debounce_time, ButtonManager::nop_callback),
}
{
m_instance = this;
@@ -78,16 +79,16 @@ private:
struct button_state {
button_state(uint8_t gpio_index,
button_state(GpioPin &pin,
BSP::time_t debounce_time,
ChangeCallback callback)
: m_gpio_idx(gpio_index)
: m_pin(pin)
, m_debounce_time(debounce_time)
, m_state(ButtonState::NOT_PRESSED)
, m_state_change_ts(0)
, m_callback(callback)
{}
uint8_t const m_gpio_idx;
GpioPin &m_pin;
BSP::time_t const m_debounce_time;
ButtonState m_prev_call_state; /*<! The last state the button was in when it the callback was called */