summaryrefslogtreecommitdiff
path: root/hal
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2011-12-04 02:04:14 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2011-12-04 02:04:14 +0100
commitc3d8f5876d6c1016e60a6a1dd33272dc8950163f (patch)
treed77c309ee50fb6ab14e63d2ccb1d419d6b0fad6e /hal
parenta3bce929687e476af5dc7e2e757617b8cb27c627 (diff)
Fixed Pin class.
Diffstat (limited to 'hal')
-rw-r--r--hal/pin.h78
1 files changed, 78 insertions, 0 deletions
diff --git a/hal/pin.h b/hal/pin.h
new file mode 100644
index 0000000..e45c178
--- /dev/null
+++ b/hal/pin.h
@@ -0,0 +1,78 @@
+#ifndef PIN_H
+#define PIN_H
+
+#include "gpio.h"
+
+class Pin {
+ private:
+ GPIO_t& g;
+ int n;
+
+ public:
+ Pin(GPIO_t& gpio, int pin) : g(gpio), n(pin) {}
+
+ enum Mode {
+ Input,
+ Output,
+ AF,
+ Analog,
+ };
+
+ enum Type {
+ PushPull,
+ OpenDrain,
+ };
+
+ void set_mode(Mode m) {
+ g.MODER = (g.MODER & ~(3 << (n * 2))) | m << (n * 2);
+ }
+
+ void set_type(Type t) {
+ if(t) {
+ g.OTYPER |= 1 << n;
+ } else {
+ g.OTYPER &= ~(1 << n);
+ }
+ }
+
+ void set_af(int af) {
+ if(n > 8) {
+ g.AFRL = (g.AFRL & ~(0xf << (n * 4))) | af << (n * 4);
+ } else {
+ g.AFRH = (g.AFRL & ~(0xf << (n * 4 - 32))) | af << (n * 4 - 32);
+ }
+ }
+
+ void on() {
+ g.BSRR = 1 << n;
+ }
+
+ void off() {
+ g.BSRR = 1 << 16 << n;
+ }
+
+ void toggle() {
+ if(g.ODR & (1 << n)) {
+ off();
+ } else {
+ on();
+ }
+ }
+};
+static Pin PB0(GPIOD, 0);
+static Pin PB1(GPIOD, 1);
+static Pin PB2(GPIOD, 2);
+static Pin PB3(GPIOD, 3);
+static Pin PB4(GPIOD, 4);
+static Pin PB5(GPIOD, 5);
+static Pin PB6(GPIOD, 6);
+static Pin PB7(GPIOD, 7);
+static Pin PB8(GPIOD, 8);
+static Pin PB9(GPIOD, 9);
+
+static Pin PD12(GPIOD, 12);
+static Pin PD13(GPIOD, 13);
+static Pin PD14(GPIOD, 14);
+static Pin PD15(GPIOD, 15);
+
+#endif