diff l476rg-hal-test/src/main.cpp @ 3:3d9705e842f8

working on rcc and bus
author cin
date Wed, 18 Jan 2017 01:07:59 +0300
parents 0c59e7a7782a
children ca4f5b55b391
line wrap: on
line diff
--- a/l476rg-hal-test/src/main.cpp	Mon Jan 16 11:04:47 2017 +0300
+++ b/l476rg-hal-test/src/main.cpp	Wed Jan 18 01:07:59 2017 +0300
@@ -45,30 +45,52 @@
 
 using namespace halpp;
 
-template<typename TDev, unsigned short PIN> class TLed {
-	TDev& m_dev;
+template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed {
+	TLed();
+	explicit TLed(const TLed&);
 public:
-	TLed(TDev& dev) :
-			m_dev(dev) {
+	static void Init() {
+		TTraits::SetPinMode(PIN, GpioModeOutput);
+		TTraits::SetPinOutputType(PIN, GpioPushPull);
+		TTraits::SetPinPullMode(PIN, GpioNoPull);
+	}
 
+	static void Set() {
+		TTraits::WritePin(PIN, 1);
 	}
 
+	static void Reset() {
+		TTraits::WritePin(PIN, 0);
+	}
+
+	static void Toogle() {
+		TTraits::TooglePin(PIN);
+	}
+};
+
+template<typename TDev, typename TTraits = TGpioTraits<TDev> > class TLedInst {
+	const unsigned short m_ledNo;
+
+	explicit TLedInst(const TLedInst&);
+public:
+	TLedInst(unsigned short led) : m_ledNo(led) {
+	}
 	void Init() const {
-		m_dev.SetPinMode(5, halpp::GpioModeOutput);
-		m_dev.SetPinOutputType(5, halpp::GpioOutputType::GpioPushPull);
-		m_dev.SetPinPullMode(5, halpp::GpioPullMode::GpioNoPull);
+		TTraits::SetPinMode(m_ledNo, GpioModeOutput);
+		TTraits::SetPinOutputType(m_ledNo, GpioPushPull);
+		TTraits::SetPinPullMode(m_ledNo, GpioNoPull);
 	}
 
 	void Set() const {
-		m_dev.WritePin(PIN, 1);
+		TTraits::WritePin(m_ledNo, 1);
 	}
 
 	void Reset() const {
-		m_dev.WritePin(PIN, 0);
+		TTraits::WritePin(m_ledNo, 0);
 	}
 
 	void Toogle() const {
-		m_dev.TooglePin(PIN);
+		TTraits::TooglePin(m_ledNo);
 	}
 };
 
@@ -79,6 +101,19 @@
 	}
 };
 
+class APB1Bus {
+public:
+	static void Enable(unsigned int flag) {
+		RCC->APB1ENR1 |= flag;
+	}
+};
+
+class APB1Bus2 {
+	static void Enable(uint32_t flag) {
+		RCC->APB1ENR2 |= flag;
+	}
+};
+
 template<typename TBus, unsigned short ENF> class TDevice {
 public:
 	static void Enable() {
@@ -86,21 +121,29 @@
 	}
 };
 
-class GpioA : public TGpio<GPIO_TypeDef>, public TDevice<AHB2Bus, RCC_AHB2ENR_GPIOAEN> {
-	static GpioA m_instance;
-	typedef TGpio<GPIO_TypeDef> GpioBase;
+class Hardware {
 public:
+	class GpioA: public TDevice<AHB2Bus, RCC_AHB2ENR_GPIOAEN> {
+	public:
+		static GPIO_TypeDef& instance() {
+			return *GPIOA;
+		}
+	};
 
-	GpioA(GPIO_TypeDef& dev) : GpioBase(dev) {
-
-	}
+	class Tim3: public TDevice<AHB1Bus, RCC_APB1ENR1_TIM3EN> {
+	public:
+		static TIM_TypeDef& instance() {
+			return *TIM3;
+		}
+	};
 
-	static GpioA& instance() {
-		return m_instance;
-	}
+	static const TLedInst<GpioA> greenLed;
+	static const TLedInst<GpioA> led2;
+
 };
 
-GpioA GpioA::m_instance(*GPIOA);
+const TLedInst<Hardware::GpioA> Hardware::greenLed(5);
+const TLedInst<Hardware::GpioA> Hardware::led2(5);
 
 int main(void) {
 
@@ -116,14 +159,14 @@
 
 	/* TODO - Add your application code here */
 
-	GpioA::Enable();
-
-	TLed<halpp::TGpio<GPIO_TypeDef>, 5> led1(GpioA::instance());
-	led1.Init();
+	Hardware::GpioA::Enable();
+	Hardware::greenLed.Init();
+	Hardware::led2.Init();
 
 	/* Infinite loop */
 	while (1) {
-		led1.Toogle();
+		Hardware::greenLed.Toogle();
+		Hardware::led2.Toogle();
 
 		for (int i = 0; i < 1000000; i++) {
 		}