aboutsummaryrefslogtreecommitdiffstats
path: root/src/samd21/gpio.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2018-07-31 15:49:37 -0400
committerKevin O'Connor <kevin@koconnor.net>2018-08-07 00:03:21 -0400
commitcf2393efc8697028ec9fc389ec992c6050fb5878 (patch)
tree8ac2e750a0a0c7589d975a0be05ea718b0431b7c /src/samd21/gpio.c
parent74cf4dc9e0fa129b315b398d8e70e4df20b26aea (diff)
downloadkutter-cf2393efc8697028ec9fc389ec992c6050fb5878.tar.gz
kutter-cf2393efc8697028ec9fc389ec992c6050fb5878.tar.xz
kutter-cf2393efc8697028ec9fc389ec992c6050fb5878.zip
samd21: Add support for gpio pins
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/samd21/gpio.c')
-rw-r--r--src/samd21/gpio.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/samd21/gpio.c b/src/samd21/gpio.c
index 3ae51f74..cf2d6618 100644
--- a/src/samd21/gpio.c
+++ b/src/samd21/gpio.c
@@ -4,8 +4,17 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.
+#include "board/irq.h" // irq_save
+#include "command.h" // shutdown
+#include "gpio.h" // gpio_out_setup
#include "internal.h" // gpio_peripheral
#include "samd21.h" // PORT
+#include "sched.h" // sched_shutdown
+
+
+/****************************************************************
+ * Pin multiplexing
+ ****************************************************************/
void
gpio_peripheral(char bank, uint32_t bit, char ptype, uint32_t pull_up)
@@ -20,3 +29,81 @@ gpio_peripheral(char bank, uint32_t bit, char ptype, uint32_t pull_up)
pg->PINCFG[bit].reg = ((ptype ? PORT_PINCFG_PMUXEN : 0)
| (pull_up ? PORT_PINCFG_PULLEN : 0));
}
+
+
+/****************************************************************
+ * General Purpose Input Output (GPIO) pins
+ ****************************************************************/
+
+#define GPIO(PORT, NUM) (((PORT)-'A') * 32 + (NUM))
+#define GPIO2PORT(PIN) ((PIN) / 32)
+#define GPIO2NUM(PIN) ((PIN) % 32)
+#define GPIO2BIT(PIN) (1<<((PIN) % 32))
+#define NUM_PORT 2
+
+struct gpio_out
+gpio_out_setup(uint8_t pin, uint8_t val)
+{
+ if (GPIO2PORT(pin) >= NUM_PORT)
+ goto fail;
+ PortGroup *pg = &PORT->Group[GPIO2PORT(pin)];
+ uint32_t bit = GPIO2BIT(pin);
+ irqstatus_t flag = irq_save();
+ if (val)
+ pg->OUTSET.reg = bit;
+ else
+ pg->OUTCLR.reg = bit;
+ pg->DIRSET.reg = bit;
+ pg->PINCFG[GPIO2NUM(pin)].reg = 0;
+ irq_restore(flag);
+ return (struct gpio_out){ .regs=pg, .bit=bit };
+fail:
+ shutdown("Not an output pin");
+}
+
+void
+gpio_out_toggle_noirq(struct gpio_out g)
+{
+ PortGroup *pg = g.regs;
+ pg->OUTTGL.reg = g.bit;
+}
+
+void
+gpio_out_toggle(struct gpio_out g)
+{
+ gpio_out_toggle_noirq(g);
+}
+
+void
+gpio_out_write(struct gpio_out g, uint8_t val)
+{
+ PortGroup *pg = g.regs;
+ if (val)
+ pg->OUTSET.reg = g.bit;
+ else
+ pg->OUTCLR.reg = g.bit;
+}
+
+
+struct gpio_in
+gpio_in_setup(uint8_t pin, int8_t pull_up)
+{
+ if (GPIO2PORT(pin) >= NUM_PORT)
+ goto fail;
+ PortGroup *pg = &PORT->Group[GPIO2PORT(pin)];
+ uint32_t bit = GPIO2BIT(pin);
+ irqstatus_t flag = irq_save();
+ pg->PINCFG[GPIO2NUM(pin)].reg = pull_up > 0 ? PORT_PINCFG_PULLEN : 0;
+ pg->DIRCLR.reg = bit;
+ irq_restore(flag);
+ return (struct gpio_in){ .regs=pg, .bit=bit };
+fail:
+ shutdown("Not an input pin");
+}
+
+uint8_t
+gpio_in_read(struct gpio_in g)
+{
+ PortGroup *pg = g.regs;
+ return !!(pg->IN.reg & g.bit);
+}