From c3d8f5876d6c1016e60a6a1dd33272dc8950163f Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sun, 4 Dec 2011 02:04:14 +0100 Subject: Fixed Pin class. --- hal/pin.h | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 26 ++++++++++++++------ pin.h | 83 --------------------------------------------------------------- 3 files changed, 96 insertions(+), 91 deletions(-) create mode 100644 hal/pin.h delete mode 100644 pin.h 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 diff --git a/main.cpp b/main.cpp index fa4d1a8..e34d90e 100644 --- a/main.cpp +++ b/main.cpp @@ -13,6 +13,8 @@ #include "xbee.h" #include "gps.h" +#include "pin.h" + template 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); diff --git a/pin.h b/pin.h deleted file mode 100644 index cbace79..0000000 --- a/pin.h +++ /dev/null @@ -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 Pin : public Ts... { - public: - Pin(GPIO_t& gpio, int pin) : Ts(gpio, pin)... {} -}; -/* -template -struct U { - struct RX; - struct TX; -}; - -typedef U<1> U1; -typedef U<2> U2; -typedef U<3> U3; - -template -class AF { - public: - AF(GPIO_t& gpio, int pin) {} -}; - -template -class Remap : public AF { - public: - Remap(GPIO_t& gpio, int pin) : AF(gpio, pin) {} -}; - -class Kake { - public: - template - Kake(AF rxpin, AF txpin) {} - - template - Kake(const U&&, AF rxpin, AF txpin) {} -}; - -static Pin> PB6(GPIOB, 6); -static Pin> PB7(GPIOB, 7); - -static Pin, AF> PB8(GPIOB, 8); -static Pin, AF> PB9(GPIOB, 9); -*/ - -static Pin PD12(GPIOD, 12); -static Pin PD13(GPIOD, 13); -static Pin PD14(GPIOD, 14); -static Pin PD15(GPIOD, 15); -/* -Kake f1(PB6, PB7); -Kake f2(PB8, PB9); -*/ -#endif -- cgit v1.2.3