Skip to content

Commit

Permalink
stm32: Add a gpio_valid() helper function
Browse files Browse the repository at this point in the history
Add a function to validate that a gpio pin is valid on the chip.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
  • Loading branch information
KevinOConnor authored and Piezoid committed Sep 7, 2022
1 parent 0499db1 commit 35d9d4f
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/stm32/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,24 @@ regs_to_pin(GPIO_TypeDef *regs, uint32_t bit)
return 0;
}

// Verify that a gpio is a valid pin
static int
gpio_valid(uint32_t pin)
{
uint32_t port = GPIO2PORT(pin);
return port < ARRAY_SIZE(digital_regs) && digital_regs[port];
}

struct gpio_out
gpio_out_setup(uint32_t pin, uint32_t val)
{
if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs))
goto fail;
if (!gpio_valid(pin))
shutdown("Not an output pin");
GPIO_TypeDef *regs = digital_regs[GPIO2PORT(pin)];
if (! regs)
goto fail;
gpio_clock_enable(regs);
struct gpio_out g = { .regs=regs, .bit=GPIO2BIT(pin) };
gpio_out_reset(g, val);
return g;
fail:
shutdown("Not an output pin");
}

void
Expand Down Expand Up @@ -125,16 +129,12 @@ gpio_out_write(struct gpio_out g, uint32_t val)
struct gpio_in
gpio_in_setup(uint32_t pin, int32_t pull_up)
{
if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs))
goto fail;
if (!gpio_valid(pin))
shutdown("Not a valid input pin");
GPIO_TypeDef *regs = digital_regs[GPIO2PORT(pin)];
if (! regs)
goto fail;
struct gpio_in g = { .regs=regs, .bit=GPIO2BIT(pin) };
gpio_in_reset(g, pull_up);
return g;
fail:
shutdown("Not a valid input pin");
}

void
Expand Down

0 comments on commit 35d9d4f

Please sign in to comment.