dev/c++ #7

Merged
tkl merged 7 commits from dev/c++ into master 2020-03-30 09:57:32 +00:00
7 changed files with 125 additions and 79 deletions
Showing only changes of commit 7f1721a536 - Show all commits

View File

@ -34,6 +34,8 @@ TARGET_HEX = $(patsubst %.elf,%.hex,$(TARGET))
TARGET_PACKAGE = $(patsubst %.hex,%.zip,$(TARGET_HEX)) TARGET_PACKAGE = $(patsubst %.hex,%.zip,$(TARGET_HEX))
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST)) THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
INCLUDES += src/
INCLUDES += interfaces/
INCLUDES += include/ INCLUDES += include/
INCLUDES += include/application/$(APPLICATION) INCLUDES += include/application/$(APPLICATION)

View File

@ -0,0 +1,21 @@
#ifndef __INTERFACES_GPIO_INTERFACE_H__
#define __INTERFACES_GPIO_INTERFACE_H__
#include <cstdint>
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

View File

@ -1,24 +1,14 @@
extern "C" {
#include <stdbool.h>
#include <stdint.h>
#include "delay.h" #include "delay.h"
#include "board.h" #include "platform/gpio.h"
}
using namespace hal;
int main(void) int main(void)
{ {
unsigned int cnt[4] = {0, 0, 0, 0}; Gpio led_1(17);
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]);
}
while(true) { while(true) {
for(unsigned int i = 0; i < 4; i++) { delay_ms(200);
char c = (cnt[i]++ % 2) + 0x30; led_1.toggle();
drv_write(leds[i], &c, 1);
delay_ms(500);
}
} }
return 0; return 0;
} }

10
src/platform/gpio.h Normal file
View File

@ -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

View File

@ -1,63 +0,0 @@
#include <assert.h>
#include <stdint.h>
#include <stddef.h>
#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;
}

View File

@ -0,0 +1,62 @@
#include "platform/nrf52/gpio.h"
extern "C" {
#include "nrf.h"
NRF_GPIO_Type *const GPIO_REGS = reinterpret_cast<NRF_GPIO_Type *>(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)));
}

24
src/platform/nrf52/gpio.h Normal file
View File

@ -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