avr: Add support for gpio_x_reset()

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-08-19 19:10:36 -04:00
parent 34fe4cce14
commit 1ae67441c3
2 changed files with 26 additions and 12 deletions

View File

@ -52,16 +52,22 @@ gpio_out_setup(uint8_t pin, uint8_t val)
struct gpio_digital_regs *regs = GPIO2REGS(pin); struct gpio_digital_regs *regs = GPIO2REGS(pin);
if (! regs) if (! regs)
goto fail; goto fail;
uint8_t bit = GPIO2BIT(pin); struct gpio_out g = { .regs=regs, .bit=GPIO2BIT(pin) };
irqstatus_t flag = irq_save(); gpio_out_reset(g, val);
regs->out = val ? (regs->out | bit) : (regs->out & ~bit); return g;
regs->mode |= bit;
irq_restore(flag);
return (struct gpio_out){ .regs=regs, .bit=bit };
fail: fail:
shutdown("Not an output pin"); shutdown("Not an output pin");
} }
void
gpio_out_reset(struct gpio_out g, uint8_t val)
{
irqstatus_t flag = irq_save();
g.regs->out = val ? (g.regs->out | g.bit) : (g.regs->out & ~g.bit);
g.regs->mode |= g.bit;
irq_restore(flag);
}
void void
gpio_out_toggle_noirq(struct gpio_out g) gpio_out_toggle_noirq(struct gpio_out g)
{ {
@ -90,16 +96,22 @@ gpio_in_setup(uint8_t pin, int8_t pull_up)
struct gpio_digital_regs *regs = GPIO2REGS(pin); struct gpio_digital_regs *regs = GPIO2REGS(pin);
if (! regs) if (! regs)
goto fail; goto fail;
uint8_t bit = GPIO2BIT(pin); struct gpio_in g = { .regs=regs, .bit=GPIO2BIT(pin) };
irqstatus_t flag = irq_save(); gpio_in_reset(g, pull_up);
regs->out = pull_up > 0 ? (regs->out | bit) : (regs->out & ~bit); return g;
regs->mode &= ~bit;
irq_restore(flag);
return (struct gpio_in){ .regs=regs, .bit=bit };
fail: fail:
shutdown("Not an input pin"); shutdown("Not an input pin");
} }
void
gpio_in_reset(struct gpio_in g, int8_t pull_up)
{
irqstatus_t flag = irq_save();
g.regs->out = pull_up > 0 ? (g.regs->out | g.bit) : (g.regs->out & ~g.bit);
g.regs->mode &= ~g.bit;
irq_restore(flag);
}
uint8_t uint8_t
gpio_in_read(struct gpio_in g) gpio_in_read(struct gpio_in g)
{ {

View File

@ -9,6 +9,7 @@ struct gpio_out {
uint8_t bit : 8; uint8_t bit : 8;
}; };
struct gpio_out gpio_out_setup(uint8_t pin, uint8_t val); struct gpio_out gpio_out_setup(uint8_t pin, uint8_t val);
void gpio_out_reset(struct gpio_out g, uint8_t val);
void gpio_out_toggle_noirq(struct gpio_out g); void gpio_out_toggle_noirq(struct gpio_out g);
void gpio_out_toggle(struct gpio_out g); void gpio_out_toggle(struct gpio_out g);
void gpio_out_write(struct gpio_out g, uint8_t val); void gpio_out_write(struct gpio_out g, uint8_t val);
@ -18,6 +19,7 @@ struct gpio_in {
uint8_t bit; uint8_t bit;
}; };
struct gpio_in gpio_in_setup(uint8_t pin, int8_t pull_up); struct gpio_in gpio_in_setup(uint8_t pin, int8_t pull_up);
void gpio_in_reset(struct gpio_in g, int8_t pull_up);
uint8_t gpio_in_read(struct gpio_in g); uint8_t gpio_in_read(struct gpio_in g);
struct gpio_pwm { struct gpio_pwm {