summaryrefslogtreecommitdiff
path: root/hal/pin.h
blob: e2b0a62c5016721753a827a1e41edb22767f0c9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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,
		};
		
		enum Pull {
			PullNone,
			PullUp,
			PullDown,
		};
		
		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_pull(Pull p) {
			g.PUPDR = (g.PUPDR & ~(3 << (n * 2))) | p << (n * 2);
		}
		
		void set_af(int af) {
			if(n < 8) {
				g.AFRL = (g.AFRL & ~(0xf << (n * 4))) | af << (n * 4);
			} else {
				g.AFRH = (g.AFRH & ~(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 PA0(GPIOA, 0);
static Pin PA1(GPIOA, 1);
static Pin PA2(GPIOA, 2);
static Pin PA3(GPIOA, 3);
static Pin PA4(GPIOA, 4);
static Pin PA5(GPIOA, 5);
static Pin PA6(GPIOA, 6);
static Pin PA7(GPIOA, 7);
static Pin PA8(GPIOA, 8);
static Pin PA9(GPIOA, 9);
static Pin PA10(GPIOA, 10);
static Pin PA11(GPIOA, 11);
static Pin PA12(GPIOA, 12);
static Pin PA13(GPIOA, 13);
static Pin PA14(GPIOA, 14);
static Pin PA15(GPIOA, 15);

static Pin PB0(GPIOB, 0);
static Pin PB1(GPIOB, 1);
static Pin PB2(GPIOB, 2);
static Pin PB3(GPIOB, 3);
static Pin PB4(GPIOB, 4);
static Pin PB5(GPIOB, 5);
static Pin PB6(GPIOB, 6);
static Pin PB7(GPIOB, 7);
static Pin PB8(GPIOB, 8);
static Pin PB9(GPIOB, 9);

static Pin PD12(GPIOD, 12);
static Pin PD13(GPIOD, 13);
static Pin PD14(GPIOD, 14);
static Pin PD15(GPIOD, 15);

#endif