diff options
-rw-r--r-- | hal/gpio.h | 43 | ||||
-rw-r--r-- | hal/stm32.h | 39 | ||||
-rw-r--r-- | pin.h | 83 |
3 files changed, 126 insertions, 39 deletions
diff --git a/hal/gpio.h b/hal/gpio.h new file mode 100644 index 0000000..324950e --- /dev/null +++ b/hal/gpio.h @@ -0,0 +1,43 @@ +#ifndef GPIO_H +#define GPIO_H + +struct GPIO_t { + #if defined(STM32F1) + volatile uint32_t CRL; + volatile uint32_t CRH; + volatile uint32_t IDR; + volatile uint32_t ODR; + volatile uint32_t BSRR; + volatile uint32_t BRR; + volatile uint32_t LCKR; + #elif defined(STM32F4) + volatile uint32_t MODER; + volatile uint32_t OTYPER; + volatile uint32_t OSPEEDER; + volatile uint32_t PUPDR; + volatile uint32_t IDR; + volatile uint32_t ODR; + volatile uint32_t BSRR; + volatile uint32_t LCKR; + volatile uint32_t AFRL; + volatile uint32_t AFRH; + #endif +}; + +#if defined(STM32F1) +static GPIO_t& GPIOA = *(GPIO_t*)0x40010800; +static GPIO_t& GPIOB = *(GPIO_t*)0x40010c00; +static GPIO_t& GPIOC = *(GPIO_t*)0x40011000; +#elif defined(STM32F4) +static GPIO_t& GPIOA = *(GPIO_t*)0x40020000; +static GPIO_t& GPIOB = *(GPIO_t*)0x40020400; +static GPIO_t& GPIOC = *(GPIO_t*)0x40020800; +static GPIO_t& GPIOD = *(GPIO_t*)0x40020c00; +static GPIO_t& GPIOE = *(GPIO_t*)0x40021000; +static GPIO_t& GPIOF = *(GPIO_t*)0x40021400; +static GPIO_t& GPIOG = *(GPIO_t*)0x40021800; +static GPIO_t& GPIOH = *(GPIO_t*)0x40021c00; +static GPIO_t& GPIOI = *(GPIO_t*)0x40022000; +#endif + +#endif diff --git a/hal/stm32.h b/hal/stm32.h index 3f39bed..c487cd4 100644 --- a/hal/stm32.h +++ b/hal/stm32.h @@ -273,45 +273,6 @@ static FLASH_t& FLASH = *(FLASH_t*)0x40022000; static FLASH_t& FLASH = *(FLASH_t*)0x40023c00; #endif -struct GPIO_t { - #if defined(STM32F1) - volatile uint32_t CRL; - volatile uint32_t CRH; - volatile uint32_t IDR; - volatile uint32_t ODR; - volatile uint32_t BSRR; - volatile uint32_t BRR; - volatile uint32_t LCKR; - #elif defined(STM32F4) - volatile uint32_t MODER; - volatile uint32_t OTYPER; - volatile uint32_t OSPEEDER; - volatile uint32_t PUPDR; - volatile uint32_t IDR; - volatile uint32_t ODR; - volatile uint32_t BSRR; - volatile uint32_t LCKR; - volatile uint32_t AFRL; - volatile uint32_t AFRH; - #endif -}; - -#if defined(STM32F1) -static GPIO_t& GPIOA = *(GPIO_t*)0x40010800; -static GPIO_t& GPIOB = *(GPIO_t*)0x40010c00; -static GPIO_t& GPIOC = *(GPIO_t*)0x40011000; -#elif defined(STM32F4) -static GPIO_t& GPIOA = *(GPIO_t*)0x40020000; -static GPIO_t& GPIOB = *(GPIO_t*)0x40020400; -static GPIO_t& GPIOC = *(GPIO_t*)0x40020800; -static GPIO_t& GPIOD = *(GPIO_t*)0x40020c00; -static GPIO_t& GPIOE = *(GPIO_t*)0x40021000; -static GPIO_t& GPIOF = *(GPIO_t*)0x40021400; -static GPIO_t& GPIOG = *(GPIO_t*)0x40021800; -static GPIO_t& GPIOH = *(GPIO_t*)0x40021c00; -static GPIO_t& GPIOI = *(GPIO_t*)0x40022000; -#endif - struct I2C_t { volatile uint32_t CR1; volatile uint32_t CR2; @@ -0,0 +1,83 @@ +#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 |