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) {