Compare commits

..

1 Commits

Author SHA1 Message Date
Thomas Klaehn
2e3d0c6f80 Add gpio event driver 2020-04-26 08:34:37 +02:00
3 changed files with 33 additions and 23 deletions

View File

@ -46,7 +46,7 @@ void Gpio::set_direction(direction dir, bool blocking_read)
drive = GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos;
sense = GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos;
gpiote.instance().register_handler(Gpiote::GpioteChannel::CHANNEL_0, *this);
gpiote.instance().register_handler(this);
this->blocking = blocking_read;

View File

@ -16,7 +16,11 @@ using namespace pinetime::platform::nrf52;
Gpiote::Gpiote()
: InterruptHandler(InterruptGuardian::Nrf52IrqN::GPIOTE_IRQ)
, num_registered(0)
{
for(auto it = this->gpiote_channels.begin(); it != this->gpiote_channels.end(); ++it) {
*it = nullptr;
}
}
void Gpiote::enable()
@ -30,17 +34,34 @@ void Gpiote::disable()
NVIC_DisableIRQ(GPIOTE_IRQn);
}
void Gpiote::register_handler(GpioteChannel channel, pinetime::interfaces::GpioInterface & gpio)
void Gpiote::register_handler(pinetime::interfaces::GpioInterface *gpio)
{
unsigned int idx = static_cast<unsigned int>(channel);
this->gpiote_channels[idx] = &gpio;
if(num_registered == GPIOTE_CHANNELS) {
//FIXME: Error notification
return;
}
if(num_registered == 0) {
this->enable();
} else {
//check if gpio already registered
for(auto it = this->gpiote_channels.begin(); it != this->gpiote_channels.end(); ++it) {
if(*it == gpio) {
return;
}
}
}
uint32_t i = 0;
for(auto it = this->gpiote_channels.begin(); it != this->gpiote_channels.end(); ++it) {
if(*it == nullptr) {
*it = gpio;
GPIOTE_REGS->INTENSET = GPIOTE_INTENSET_IN0_Msk;
GPIOTE_REGS->CONFIG[idx] = (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos)
| ((gpio.pin_number()) << GPIOTE_CONFIG_PSEL_Pos)
GPIOTE_REGS->CONFIG[i] = (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos)
| ((gpio->pin_number()) << GPIOTE_CONFIG_PSEL_Pos)
| (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos);
}
i++;
}
}
void Gpiote::handle()

View File

@ -26,18 +26,7 @@ public:
void enable() override;
void disable() override;
enum class GpioteChannel {
CHANNEL_0 = 0,
CHANNEL_1,
CHANNEL_2,
CHANNEL_3,
CHANNEL_4,
CHANNEL_5,
CHANNEL_6,
CHANNEL_7,
};
void register_handler(GpioteChannel, pinetime::interfaces::GpioInterface &);
void register_handler(pinetime::interfaces::GpioInterface *);
private:
void handle() override;
@ -47,7 +36,7 @@ private:
};
std::array<pinetime::interfaces::GpioInterface *, GPIOTE_CHANNELS> gpiote_channels;
uint32_t num_registered;
};
}