aboutsummaryrefslogtreecommitdiffstats
path: root/src/lpc176x/gpio.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2018-05-07 21:32:27 -0400
committerKevin O'Connor <kevin@koconnor.net>2018-05-25 11:52:13 -0400
commit970831ee0d3b91897196e92270d98b2a3067427f (patch)
tree441c2b329ea669ff5e996a617d7b6bc2c315fa0d /src/lpc176x/gpio.c
parent5ae22a5e51872fd3e6130a3acce68f598bee9ff0 (diff)
downloadkutter-970831ee0d3b91897196e92270d98b2a3067427f.tar.gz
kutter-970831ee0d3b91897196e92270d98b2a3067427f.tar.xz
kutter-970831ee0d3b91897196e92270d98b2a3067427f.zip
lpc176x: Add initial support for LPC176x processors
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/lpc176x/gpio.c')
-rw-r--r--src/lpc176x/gpio.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/lpc176x/gpio.c b/src/lpc176x/gpio.c
new file mode 100644
index 00000000..756b0e7b
--- /dev/null
+++ b/src/lpc176x/gpio.c
@@ -0,0 +1,28 @@
+// GPIO functions on lpc176x
+//
+// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include "LPC17xx.h" // LPC_PINCON
+#include "internal.h" // gpio_peripheral
+#include "board/irq.h" // irq_save
+
+// Set the mode and extended function of a pin
+void
+gpio_peripheral(int bank, int pin, int func, int pullup)
+{
+ uint32_t bank_pos = bank * 2, pin_pos = pin * 2;
+ if (pin_pos >= 32) {
+ pin_pos -= 32;
+ bank_pos++;
+ }
+ uint32_t sel_bits = (func & 0x03) << pin_pos, mask = ~(0x03 << pin_pos);
+ uint32_t mode_bits = (pullup ? 0x00 : 0x02) << pin_pos;
+ volatile uint32_t *pinsel = &LPC_PINCON->PINSEL0;
+ volatile uint32_t *pinmode = &LPC_PINCON->PINMODE0;
+ irqstatus_t flag = irq_save();
+ pinsel[bank_pos] = (pinsel[bank_pos] & mask) | sel_bits;
+ pinmode[bank_pos] = (pinmode[bank_pos] & mask) | mode_bits;
+ irq_restore(flag);
+}