#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.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 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