diff options
-rw-r--r-- | hal/pin.h | 78 | ||||
-rw-r--r-- | main.cpp | 26 | ||||
-rw-r--r-- | pin.h | 83 |
3 files changed, 96 insertions, 91 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 @@ -13,6 +13,8 @@ #include "xbee.h" #include "gps.h" +#include "pin.h" + template<class T> inline void saturate(T& var, T absmax) { if(var > absmax) { @@ -80,24 +82,32 @@ uint32_t gps_stack[256]; Thread gps_thread(gps_stack, sizeof(gps_stack), gps_thread_main); +static Pin& led_green = PD12; +static Pin& led_yellow = PD13; +static Pin& led_red = PD14; +static Pin& led_blue = PD15; + int main() { // Initialize system timer. STK.LOAD = 168000000 / 8 / 1000; // 1000 Hz. STK.CTRL = 0x03; - RCC.enable(RCC.AFIO); - RCC.enable(RCC.IOPA); - RCC.enable(RCC.IOPB); + RCC.enable(RCC.GPIOA); + RCC.enable(RCC.GPIOB); + RCC.enable(RCC.GPIOD); - GPIOA.CRL = 0x4400bbbb; - GPIOA.CRH = 0x444444b4; + led_green.set_mode(Pin::Output); + led_yellow.set_mode(Pin::Output); + led_red.set_mode(Pin::Output); + led_blue.set_mode(Pin::Output); - GPIOB.CRL = 0xff444434; - GPIOB.CRH = 0x44444444; - GPIOB.ODR = 1 << 1; RCC.enable(RCC.DMA1); RCC.enable(RCC.ADC1); + PB6.set_af(7); + PB7.set_af(7); + PB6.set_mode(Pin::Output); + PB7.set_mode(Pin::Output); // Give all hardware enough time to initialize. Time::sleep(200); @@ -1,83 +0,0 @@ -#ifndef PIN_H -#define PIN_H - -#include "gpio.h" - -class GPIO { - private: - GPIO_t& g; - int n; - - public: - GPIO(GPIO_t& gpio, int pin) : g(gpio), n(pin) {} - - void on() { - g.BSRR = 1 << n; - } - - void off() { - g.BSRR = 1 << 16 << n; - } - - void toggle() { - if(g.ODR & (1 << n)) { - off(); - } else { - on(); - } - } -}; - -template <class... Ts> -class Pin : public Ts... { - public: - Pin(GPIO_t& gpio, int pin) : Ts(gpio, pin)... {} -}; -/* -template <int n> -struct U { - struct RX; - struct TX; -}; - -typedef U<1> U1; -typedef U<2> U2; -typedef U<3> U3; - -template <class P, class F> -class AF { - public: - AF(GPIO_t& gpio, int pin) {} -}; - -template <int AFn, class P, class F> -class Remap : public AF<P, F> { - public: - Remap(GPIO_t& gpio, int pin) : AF<P, F>(gpio, pin) {} -}; - -class Kake { - public: - template <class U> - Kake(AF<U, class U::RX> rxpin, AF<U, class U::TX> txpin) {} - - template <class U> - Kake(const U&&, AF<U, class U::RX> rxpin, AF<U, class U::TX> txpin) {} -}; - -static Pin<GPIO, Remap<7, U1, U1::RX>> PB6(GPIOB, 6); -static Pin<GPIO, Remap<7, U1, U1::TX>> PB7(GPIOB, 7); - -static Pin<GPIO, Remap<7, U2, U2::RX>, AF<U3, U3::RX>> PB8(GPIOB, 8); -static Pin<GPIO, Remap<7, U2, U2::TX>, AF<U3, U3::TX>> PB9(GPIOB, 9); -*/ - -static Pin<GPIO> PD12(GPIOD, 12); -static Pin<GPIO> PD13(GPIOD, 13); -static Pin<GPIO> PD14(GPIOD, 14); -static Pin<GPIO> PD15(GPIOD, 15); -/* -Kake f1(PB6, PB7); -Kake f2(PB8, PB9); -*/ -#endif |