aboutsummaryrefslogtreecommitdiffstats
path: root/src/stm32f4/gpio.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2019-07-23 23:51:31 -0400
committerKevin O'Connor <kevin@koconnor.net>2019-07-25 18:08:28 -0400
commitd501ca6b0b8d58d666cfbee87731e409b55dd902 (patch)
tree1ec67bf43d27fd64e7e8c801cb07961eadc6ee4a /src/stm32f4/gpio.c
parent5a02572001d1d903bdb36c13396efe73f3835a67 (diff)
downloadkutter-d501ca6b0b8d58d666cfbee87731e409b55dd902.tar.gz
kutter-d501ca6b0b8d58d666cfbee87731e409b55dd902.tar.xz
kutter-d501ca6b0b8d58d666cfbee87731e409b55dd902.zip
stm32f4: Add initial support for STM32F446 chip
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/stm32f4/gpio.c')
-rw-r--r--src/stm32f4/gpio.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/stm32f4/gpio.c b/src/stm32f4/gpio.c
new file mode 100644
index 00000000..370aba1f
--- /dev/null
+++ b/src/stm32f4/gpio.c
@@ -0,0 +1,27 @@
+// GPIO functions on stm32f4
+//
+// Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include "internal.h" // gpio_peripheral
+
+static GPIO_TypeDef * const digital_regs[] = {
+ GPIOA, GPIOB, GPIOC
+};
+
+// Set the mode and extended function of a pin
+void
+gpio_peripheral(uint32_t gpio, uint32_t mode, uint32_t func, int pullup)
+{
+ GPIO_TypeDef *regs = digital_regs[GPIO2PORT(gpio)];
+ uint32_t pup = pullup ? (pullup > 0 ? 1 : 2) : 0;
+ uint32_t pos = gpio % 16, af_reg = pos / 8;
+ uint32_t af_shift = (pos % 8) * 4, af_msk = 0x0f << af_shift;
+ uint32_t m_shift = pos * 2, m_msk = 0x03 << m_shift;
+
+ regs->AFR[af_reg] = (regs->AFR[af_reg] & ~af_msk) | (func << af_shift);
+ regs->MODER = (regs->MODER & ~m_msk) | (mode << m_shift);
+ regs->PUPDR = (regs->PUPDR & ~m_msk) | (pup << m_shift);
+ regs->OSPEEDR = (regs->OSPEEDR & ~m_msk) | (0x02 << m_shift);
+}