diff --git a/Makefile b/Makefile index 8de653d..769ed7d 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,8 @@ TARGET_HEX = $(patsubst %.elf,%.hex,$(TARGET)) TARGET_PACKAGE = $(patsubst %.hex,%.zip,$(TARGET_HEX)) THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST)) +INCLUDES += src/ +INCLUDES += interfaces/ INCLUDES += include/ INCLUDES += include/application/$(APPLICATION) diff --git a/interfaces/gpio_interface.h b/interfaces/gpio_interface.h new file mode 100644 index 0000000..097482e --- /dev/null +++ b/interfaces/gpio_interface.h @@ -0,0 +1,21 @@ +#ifndef __INTERFACES_GPIO_INTERFACE_H__ +#define __INTERFACES_GPIO_INTERFACE_H__ + +#include + +namespace interfaces { + +class GpioInterface +{ + public: + enum class direction {IN, OUT}; + + virtual void set_direction(direction) = 0; + virtual uint32_t get() = 0; + virtual void set() = 0; + virtual void clear() = 0; + virtual void toggle() = 0; +}; + +} +#endif diff --git a/src/application/blinky/main.cc b/src/application/blinky/main.cc index 913f43d..e54235b 100644 --- a/src/application/blinky/main.cc +++ b/src/application/blinky/main.cc @@ -1,24 +1,14 @@ -extern "C" { - #include - #include +#include "delay.h" +#include "platform/gpio.h" - #include "delay.h" - #include "board.h" -} +using namespace hal; int main(void) { - unsigned int cnt[4] = {0, 0, 0, 0}; - const struct driver *leds[4] = {&led_1, &led_2, &led_3, &led_4}; - for(unsigned int i = 0; i < 4; i++) { - drv_open(leds[i]); - } + Gpio led_1(17); while(true) { - for(unsigned int i = 0; i < 4; i++) { - char c = (cnt[i]++ % 2) + 0x30; - drv_write(leds[i], &c, 1); - delay_ms(500); - } + delay_ms(200); + led_1.toggle(); } return 0; } diff --git a/src/platform/gpio.h b/src/platform/gpio.h new file mode 100644 index 0000000..f8da63f --- /dev/null +++ b/src/platform/gpio.h @@ -0,0 +1,10 @@ +#ifndef __PLATFORM_GPIO_H__ +#define __PLATFORM_GPIO_H__ + +#if defined(PLATFORM_nrf52) + #include "platform/nrf52/gpio.h" + + namespace hal = platform::nrf52; +#endif + +#endif diff --git a/src/platform/nrf52/gpio.c b/src/platform/nrf52/gpio.c deleted file mode 100644 index 328a58e..0000000 --- a/src/platform/nrf52/gpio.c +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include -#include - -#include "gpio.h" - -#include "nrf_gpio.h" - -int gpio_open(const struct driver *drv) -{ - assert(NULL != drv); - - struct gpio *this = (struct gpio *)(drv->dev); - - if(this->dir == OUT) { - nrf_gpio_cfg_output((uint32_t)(this->pin)); - nrf_gpio_pin_clear(this->pin); - } - // FIXME: implement input configuration - - return 0; -} - -int gpio_close(const struct driver *drv) -{ - assert(NULL != drv); - - struct gpio *this = (struct gpio *)(drv->dev); - nrf_gpio_pin_clear(this->pin); - - return 0; -} - -int gpio_read(const struct driver *drv, char *buffer, unsigned int len) -{ - assert(NULL != drv); - - if(len == 0) { - return 0; - } - - struct gpio *this = (struct gpio *)(drv->dev); - - uint32_t value = nrf_gpio_pin_read(this->pin); - buffer[0] = value + 0x30; // ascii convert - - return 1; -} - -int gpio_write(const struct driver *drv, const char *buffer, unsigned int len) -{ - assert((NULL != drv) && (NULL != buffer)); - - if(len == 0) { - return 0; - } - - struct gpio *this = (struct gpio *)(drv->dev); - - nrf_gpio_pin_write(this->pin, buffer[0] - 0x30); - - return 1; -} diff --git a/src/platform/nrf52/gpio.cc b/src/platform/nrf52/gpio.cc new file mode 100644 index 0000000..f321b87 --- /dev/null +++ b/src/platform/nrf52/gpio.cc @@ -0,0 +1,62 @@ +#include "platform/nrf52/gpio.h" + +extern "C" { + #include "nrf.h" + + NRF_GPIO_Type *const GPIO_REGS = reinterpret_cast(NRF_P0_BASE); +} + +using namespace platform::nrf52; + +Gpio::Gpio(uint32_t pin) + : pin_number(pin) +{ + this->set_direction(direction::OUT); + this->clear(); +} + +void Gpio::set_direction(direction dir) +{ + uint32_t direct = GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos; + uint32_t input = GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos; + uint32_t pull = GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos; + uint32_t drive = GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos; + uint32_t sense = GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos; + if(dir == direction::IN) { + direct = GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos; + input = GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos; + + // FIXME: Make this configurable + // GPIO_PIN_CNF_PULL_Disabled, ///< Pin pull-up resistor disabled. + // GPIO_PIN_CNF_PULL_Pulldown, ///< Pin pull-down resistor enabled. + // GPIO_PIN_CNF_PULL_Pullup, ///< Pin pull-up resistor enabled. + pull = GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos; + drive = GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos; + sense = GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos; + } + GPIO_REGS->PIN_CNF[pin_number] = direct | input | pull | drive | sense; +} + +uint32_t Gpio::get() +{ + uint32_t res = (GPIO_REGS->IN >> pin_number) & 1UL; + return res; +} + +void Gpio::set() +{ + GPIO_REGS->OUTSET = 1UL << (this->pin_number); +} + +void Gpio::clear() +{ + GPIO_REGS->OUTCLR = 1UL << (this->pin_number); +} + +void Gpio::toggle() +{ + uint32_t state = GPIO_REGS->OUT; + + GPIO_REGS->OUTSET = (~state & (1UL << (this->pin_number))); + GPIO_REGS->OUTCLR = (state & (1UL << (this->pin_number))); +} \ No newline at end of file diff --git a/src/platform/nrf52/gpio.h b/src/platform/nrf52/gpio.h new file mode 100644 index 0000000..f92bdce --- /dev/null +++ b/src/platform/nrf52/gpio.h @@ -0,0 +1,24 @@ +#ifndef __PLATFORM_NRF52_GPIO_H__ +#define __PLATFORM_NRF52_GPIO_H__ + +#include "gpio_interface.h" + +namespace platform::nrf52 { + +class Gpio : public interfaces::GpioInterface +{ + public: + explicit Gpio(uint32_t); + void set_direction(direction) override; + uint32_t get() override; + void set() override; + void clear() override; + void toggle() override; + + private: + uint32_t pin_number; +}; + +} + +#endif