diff l476rg-hal-test/src/main.cpp @ 4:ca4f5b55b391

working on pwm
author cin
date Wed, 18 Jan 2017 03:27:00 +0300
parents 3d9705e842f8
children 0d3eea2dd7ea
line wrap: on
line diff
--- a/l476rg-hal-test/src/main.cpp	Wed Jan 18 01:07:59 2017 +0300
+++ b/l476rg-hal-test/src/main.cpp	Wed Jan 18 03:27:00 2017 +0300
@@ -45,6 +45,51 @@
 
 using namespace halpp;
 
+template <typename TDev> class TTimerTraits {
+public:
+	static void SetPeriod(uint32_t period) {
+	}
+
+	static void SetPrescaler(uint32_t prescaler) {
+	}
+};
+
+template <typename TDev> class TGenTimerTraits : public TTimerTraits<TDev> {
+public:
+	static void SetDuty(uint32_t duty) {
+		TDev::instance().CCR1 = duty;
+	}
+};
+
+template <
+	typename TGpioDev,
+	unsigned short Pin,
+	typename TTimerDev,
+	unsigned short Ch,
+	typename TGpioTraits = TGpioTraits<TGpioDev>,
+	typename TTimerTraits = TGenTimerTraits<TTimerDev>
+> class TPwmOut {
+	static uint32_t pwmPeriod;
+public:
+	static void Init(uint32_t freq) {
+		pwmPeriod = TTimerDev::GetFreq()/freq - 1;
+		TTimerTraits::SetPeriod(pwmPeriod);
+	}
+
+	static void SetDutyFactor(float f) {
+		TTimerTraits::SetDuty(pwmPeriod*f);
+	}
+};
+
+template <
+	typename TGpioDev,
+	unsigned short Pin,
+	typename TTimerDev,
+	unsigned short Ch,
+	typename TGpioTraits,
+	typename TTimerTraits
+> uint32_t TPwmOut<TGpioDev, Pin, TTimerDev, Ch, TGpioTraits, TTimerTraits>::pwmPeriod = 0;
+
 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed {
 	TLed();
 	explicit TLed(const TLed&);
@@ -68,32 +113,6 @@
 	}
 };
 
-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 {
-		TTraits::SetPinMode(m_ledNo, GpioModeOutput);
-		TTraits::SetPinOutputType(m_ledNo, GpioPushPull);
-		TTraits::SetPinPullMode(m_ledNo, GpioNoPull);
-	}
-
-	void Set() const {
-		TTraits::WritePin(m_ledNo, 1);
-	}
-
-	void Reset() const {
-		TTraits::WritePin(m_ledNo, 0);
-	}
-
-	void Toogle() const {
-		TTraits::TooglePin(m_ledNo);
-	}
-};
-
 class AHB2Bus {
 public:
 	static void Enable(unsigned int flag) {
@@ -119,6 +138,10 @@
 	static void Enable() {
 		TBus::Enable(ENF);
 	}
+
+	static uint32_t GetFreq() {
+		return 80000000;
+	}
 };
 
 class Hardware {
@@ -130,21 +153,18 @@
 		}
 	};
 
-	class Tim3: public TDevice<AHB1Bus, RCC_APB1ENR1_TIM3EN> {
+	class Tim3: public TDevice<APB1Bus, RCC_APB1ENR1_TIM3EN> {
 	public:
 		static TIM_TypeDef& instance() {
 			return *TIM3;
 		}
 	};
 
-	static const TLedInst<GpioA> greenLed;
-	static const TLedInst<GpioA> led2;
-
+	typedef TLed<GpioA, 5> GreenLed;
+	typedef TLed<GpioA, 6> Led2;
+	typedef TPwmOut<GpioA, 7, Tim3, 2> PwmLed3;
 };
 
-const TLedInst<Hardware::GpioA> Hardware::greenLed(5);
-const TLedInst<Hardware::GpioA> Hardware::led2(5);
-
 int main(void) {
 
 	/**
@@ -160,15 +180,23 @@
 	/* TODO - Add your application code here */
 
 	Hardware::GpioA::Enable();
-	Hardware::greenLed.Init();
-	Hardware::led2.Init();
+	Hardware::Tim3::Enable();
+	Hardware::GreenLed::Init();
+	Hardware::Led2::Init();
+	Hardware::PwmLed3::Init(1000);
 
+	int ii = 0;
 	/* Infinite loop */
 	while (1) {
-		Hardware::greenLed.Toogle();
-		Hardware::led2.Toogle();
+		Hardware::GreenLed::Toogle();
+		Hardware::Led2::Toogle();
+
+		Hardware::PwmLed3::SetDutyFactor(ii / 10.f);
+
+		ii = (ii+1) % 11; // 0 .. 10
 
 		for (int i = 0; i < 1000000; i++) {
+
 		}
 	}
 }