lpc176x: 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-20 16:11:29 -04:00
parent 1ae67441c3
commit 5a993b743e
2 changed files with 46 additions and 17 deletions

View File

@ -4,6 +4,7 @@
// //
// This file may be distributed under the terms of the GNU GPLv3 license. // This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // ffs
#include "LPC17xx.h" // LPC_PINCON #include "LPC17xx.h" // LPC_PINCON
#include "autoconf.h" // CONFIG_CLOCK_FREQ #include "autoconf.h" // CONFIG_CLOCK_FREQ
#include "board/irq.h" // irq_save #include "board/irq.h" // irq_save
@ -50,6 +51,16 @@ gpio_peripheral(int bank, int pin, int func, int pullup)
irq_restore(flag); irq_restore(flag);
} }
// Convert a register and bit location back to an integer pin identifier
static int
regs_to_pin(LPC_GPIO_TypeDef *regs, uint32_t bit)
{
int i;
for (i=0; i<ARRAY_SIZE(digital_regs); i++)
if (digital_regs[i] == regs)
return GPIO(i, ffs(bit)-1);
return 0;
}
struct gpio_out struct gpio_out
gpio_out_setup(uint8_t pin, uint8_t val) gpio_out_setup(uint8_t pin, uint8_t val)
@ -57,19 +68,28 @@ gpio_out_setup(uint8_t pin, uint8_t val)
if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs)) if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs))
goto fail; goto fail;
LPC_GPIO_TypeDef *regs = digital_regs[GPIO2PORT(pin)]; LPC_GPIO_TypeDef *regs = digital_regs[GPIO2PORT(pin)];
uint32_t bit = GPIO2BIT(pin); struct gpio_out g = { .regs=regs, .bit=GPIO2BIT(pin) };
irqstatus_t flag = irq_save(); gpio_out_reset(g, val);
if (val) return g;
regs->FIOSET = bit;
else
regs->FIOCLR = bit;
regs->FIODIR |= 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)
{
LPC_GPIO_TypeDef *regs = g.regs;
int pin = regs_to_pin(regs, g.bit);
irqstatus_t flag = irq_save();
if (val)
regs->FIOSET = g.bit;
else
regs->FIOCLR = g.bit;
regs->FIODIR |= g.bit;
gpio_peripheral(GPIO2PORT(pin), pin % 32, 0, 0);
irq_restore(flag);
}
void void
gpio_out_toggle_noirq(struct gpio_out g) gpio_out_toggle_noirq(struct gpio_out g)
{ {
@ -101,18 +121,25 @@ gpio_in_setup(uint8_t pin, int8_t pull_up)
{ {
if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs)) if (GPIO2PORT(pin) >= ARRAY_SIZE(digital_regs))
goto fail; goto fail;
uint32_t port = GPIO2PORT(pin); LPC_GPIO_TypeDef *regs = digital_regs[GPIO2PORT(pin)];
LPC_GPIO_TypeDef *regs = digital_regs[port]; struct gpio_in g = { .regs=regs, .bit=GPIO2BIT(pin) };
uint32_t bit = GPIO2BIT(pin); gpio_in_reset(g, pull_up);
irqstatus_t flag = irq_save(); return g;
gpio_peripheral(port, pin % 32, 0, pull_up);
regs->FIODIR &= ~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)
{
LPC_GPIO_TypeDef *regs = g.regs;
int pin = regs_to_pin(regs, g.bit);
irqstatus_t flag = irq_save();
gpio_peripheral(GPIO2PORT(pin), pin % 32, 0, pull_up);
regs->FIODIR &= ~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

@ -8,6 +8,7 @@ struct gpio_out {
uint32_t bit; uint32_t bit;
}; };
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);
@ -17,6 +18,7 @@ struct gpio_in {
uint32_t bit; uint32_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_adc { struct gpio_adc {