Skip to content

Commit

Permalink
Implement GPIOTE tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Oct 30, 2024
1 parent baf55d1 commit b8f8b73
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
12 changes: 12 additions & 0 deletions include/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ typedef enum
PIN_PULLUP = 4,
} pindir_t;

typedef enum
{
PINOWNER_NONE = 0,
PINOWNER_GPIOTE = 1 << 0,

PINOWNER_ANY = ~0,
} pinowner_t;

#define pins_is_input(pins, pin) ((pins_get_dir(pins, pin) & 1) == 0)
#define pins_set_input(pins, pin) pins_set_dir(pins, pin, pins_get_dir(pins, pin) & ~PIN_OUTPUT)
#define pins_set_output(pins, pin) pins_set_dir(pins, pin, pins_get_dir(pins, pin) | PIN_OUTPUT)
Expand All @@ -46,3 +54,7 @@ void pins_set_latch(pins_t *, uint32_t latch);

bool pins_is_set(pins_t *, int pin);
uint32_t pins_read_all(pins_t *);

bool pins_acquire(pins_t *, int pin, pinowner_t owner);
bool pins_release(pins_t *, int pin, pinowner_t owner);
bool pins_is_owned(pins_t *, int pin, pinowner_t owner);
48 changes: 46 additions & 2 deletions src/peripherals/nrf52832/gpiote.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,26 @@ enum
EVENTS_PORT = 0x17C, // Event generated from multiple input GPIO pins with SENSE mechanism enabled
};

typedef enum
{
MODE_DISABLED = 0,
MODE_EVENT = 1,
MODE_TASK = 3,
} mode_t;

typedef enum
{
POLARITY_NONE = 0,
POLARITY_LOTOHI = 1,
POLARITY_HITOLO = 2,
POLARITY_TOGGLE = 3,
} polarity_t;

typedef union
{
struct
{
unsigned int MODE : 2;
mode_t MODE : 2;
unsigned int : 6;
unsigned int PSEL : 5;
unsigned int : 3;
Expand Down Expand Up @@ -88,7 +103,7 @@ struct GPIOTE_inst_t

OPERATION(gpiote)
{
GPIOTE_t *gpiote = (GPIOTE_t *)userdata;
GPIOTE_t *gpiote = userdata;

if (op == OP_RESET)
{
Expand Down Expand Up @@ -152,6 +167,35 @@ OPERATION(gpiote)

PPI_TASK_HANDLER(gpiote_task_handler)
{
GPIOTE_t *gpiote = userdata;

if (task <= TASK_ID(TASKS_OUT7))
{
uint32_t pin = gpiote->config[task - TASK_ID(TASKS_OUT0)].PSEL;

switch (gpiote->config[task - TASK_ID(TASKS_OUT0)].POLARITY)
{
case POLARITY_LOTOHI:
pins_set(gpiote->pins, pin);
break;

case POLARITY_HITOLO:
pins_clear(gpiote->pins, pin);
break;

case POLARITY_TOGGLE:
pins_toggle(gpiote->pins, pin);
break;
}
}
else if (task >= TASK_ID(TASKS_SET0) && task <= TASK_ID(TASKS_SET7))
{
pins_set(gpiote->pins, gpiote->config[task - TASK_ID(TASKS_SET0)].PSEL);
}
else if (task >= TASK_ID(TASKS_CLR0) && task <= TASK_ID(TASKS_CLR7))
{
pins_clear(gpiote->pins, gpiote->config[task - TASK_ID(TASKS_CLR0)].PSEL);
}
}

NRF52_PERIPHERAL_CONSTRUCTOR(GPIOTE, gpiote)
Expand Down
32 changes: 32 additions & 0 deletions src/pins.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef struct
{
pindir_t dir;
pinsense_t sense;
pinowner_t owner;
} pin_t;

struct pins_t
Expand Down Expand Up @@ -116,3 +117,34 @@ void pins_set_latch(pins_t *pins, uint32_t latch)
{
pins->latch = latch;
}

bool pins_acquire(pins_t *pins, int pin, pinowner_t owner)
{
assert(pin >= 0 && pin < PINS_COUNT);

if (pins->pins[pin].owner != PINOWNER_NONE && pins->pins[pin].owner != owner)
return false;

pins->pins[pin].owner = owner;
return true;
}

bool pins_release(pins_t *pins, int pin, pinowner_t owner)
{
assert(pin >= 0 && pin < PINS_COUNT);

if (pins->pins[pin].owner == owner)
{
pins->pins[pin].owner = PINOWNER_NONE;
return true;
}

return false;
}

bool pins_is_owned(pins_t *pins, int pin, pinowner_t owner)
{
assert(pin >= 0 && pin < PINS_COUNT);

return pins->pins[pin].owner == owner;
}

0 comments on commit b8f8b73

Please sign in to comment.