Compare commits

..

1 Commits

Author SHA1 Message Date
Thomas Klaehn
aa7e9fb3a4 Add gpio event driver 2020-04-25 10:11:29 +02:00
3 changed files with 23 additions and 33 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; drive = GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos;
sense = GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos; sense = GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos;
gpiote.instance().register_handler(this); gpiote.instance().register_handler(Gpiote::GpioteChannel::CHANNEL_0, *this);
this->blocking = blocking_read; this->blocking = blocking_read;

View File

@ -16,11 +16,7 @@ using namespace pinetime::platform::nrf52;
Gpiote::Gpiote() Gpiote::Gpiote()
: InterruptHandler(InterruptGuardian::Nrf52IrqN::GPIOTE_IRQ) : 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() void Gpiote::enable()
@ -34,34 +30,17 @@ void Gpiote::disable()
NVIC_DisableIRQ(GPIOTE_IRQn); NVIC_DisableIRQ(GPIOTE_IRQn);
} }
void Gpiote::register_handler(pinetime::interfaces::GpioInterface *gpio) void Gpiote::register_handler(GpioteChannel channel, pinetime::interfaces::GpioInterface & gpio)
{ {
if(num_registered == GPIOTE_CHANNELS) { unsigned int idx = static_cast<unsigned int>(channel);
//FIXME: Error notification this->gpiote_channels[idx] = &gpio;
return;
} 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_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos);
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[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() void Gpiote::handle()

View File

@ -26,7 +26,18 @@ public:
void enable() override; void enable() override;
void disable() override; void disable() override;
void register_handler(pinetime::interfaces::GpioInterface *); 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 &);
private: private:
void handle() override; void handle() override;
@ -36,7 +47,7 @@ private:
}; };
std::array<pinetime::interfaces::GpioInterface *, GPIOTE_CHANNELS> gpiote_channels; std::array<pinetime::interfaces::GpioInterface *, GPIOTE_CHANNELS> gpiote_channels;
uint32_t num_registered;
}; };
} }