# HG changeset patch # User cin # Date 1484872830 -10800 # Node ID 0d3eea2dd7ea1f2145bf2b924ebd3d19a46ce87d # Parent ca4f5b55b3911ef20b3e863ba43f8dc65da1fc88 working on devices and timers diff -r ca4f5b55b391 -r 0d3eea2dd7ea halpp/include/gpio.h --- a/halpp/include/gpio.h Wed Jan 18 03:27:00 2017 +0300 +++ b/halpp/include/gpio.h Fri Jan 20 03:40:30 2017 +0300 @@ -30,27 +30,27 @@ TGpioTraits(); public: static void SetPinSpeed(unsigned short pin, GpioSpeed speed) { - TDev::instance().OSPEEDR = (TDev::instance().OSPEED & (~(3U << (pin * 2)))) | (speed << (pin * 2)); + TDev::instance()->OSPEEDR = (TDev::instance().OSPEED & (~(3U << (pin * 2)))) | (speed << (pin * 2)); } static void SetPinMode(unsigned short pin, GpioMode mode) { - TDev::instance().MODER = (TDev::instance().MODER & (~(3U << (pin * 2)))) | (mode << (pin * 2)); + TDev::instance()->MODER = (TDev::instance().MODER & (~(3U << (pin * 2)))) | (mode << (pin * 2)); } static void SetPinPullMode(unsigned short pin, GpioPullMode mode) { - TDev::instance().PUPDR = (TDev::instance().PUPDR & (~(3U << (pin * 2)))) | (mode << (pin * 2)); + TDev::instance()->PUPDR = (TDev::instance().PUPDR & (~(3U << (pin * 2)))) | (mode << (pin * 2)); } static void SetPinOutputType(unsigned short pin, GpioOutputType type) { - TDev::instance().OTYPER = (TDev::instance().OTYPER & (~(1U << pin))) | (type << pin); + TDev::instance()->OTYPER = (TDev::instance().OTYPER & (~(1U << pin))) | (type << pin); } static void TooglePin(unsigned short pin) { - TDev::instance().ODR ^= 1U << pin; + TDev::instance()->ODR ^= 1U << pin; } static void WritePin(unsigned short pin, unsigned short value) { - TDev::instance().ODR = (TDev::instance().ODR & (~(1U << pin))) | ((value & 1U) << pin); + TDev::instance()->ODR = (TDev::instance().ODR & (~(1U << pin))) | ((value & 1U) << pin); } }; diff -r ca4f5b55b391 -r 0d3eea2dd7ea l476rg-hal-test/src/main.cpp --- a/l476rg-hal-test/src/main.cpp Wed Jan 18 03:27:00 2017 +0300 +++ b/l476rg-hal-test/src/main.cpp Fri Jan 20 03:40:30 2017 +0300 @@ -45,51 +45,47 @@ using namespace halpp; -template class TTimerTraits { +template class TTimerBase { public: - static void SetPeriod(uint32_t period) { + typedef TCounter counter_t; + + static void Init() { + TDev::instance()->EGR = TIM_EGR_UG; + } + + static void period(counter_t value) { + TDev::instance()->ARR = value; } - static void SetPrescaler(uint32_t prescaler) { + static counter_t period() { + return (counter_t) TDev::instance()->ARR; } -}; -template class TGenTimerTraits : public TTimerTraits { -public: - static void SetDuty(uint32_t duty) { - TDev::instance().CCR1 = duty; + static void prescaler(counter_t value) { + TDev::instance()->PSC = value; + } + + static counter_t prescaler() { + return TDev::instance()->PSC; + } + + static uint32_t resolution() { + return TDev::frequency() / prescaler(); } }; -template < - typename TGpioDev, - unsigned short Pin, - typename TTimerDev, - unsigned short Ch, - typename TGpioTraits = TGpioTraits, - typename TTimerTraits = TGenTimerTraits -> class TPwmOut { - static uint32_t pwmPeriod; +template class TTimerChannel1 { public: - static void Init(uint32_t freq) { - pwmPeriod = TTimerDev::GetFreq()/freq - 1; - TTimerTraits::SetPeriod(pwmPeriod); + typedef TCounter counter_t; + static void duty(counter_t value) { + TDev::instance()->CCR1 = (uint32_t)value; } - static void SetDutyFactor(float f) { - TTimerTraits::SetDuty(pwmPeriod*f); + static counter_t duty() { + return (counter_t)TDev::instance()->CCR1; } }; -template < - typename TGpioDev, - unsigned short Pin, - typename TTimerDev, - unsigned short Ch, - typename TGpioTraits, - typename TTimerTraits -> uint32_t TPwmOut::pwmPeriod = 0; - template > class TLed { TLed(); explicit TLed(const TLed&); @@ -133,32 +129,25 @@ } }; -template class TDevice { +template class TDevice { public: + static TRegs* instance() { + return (TRegs*) BASE; + } + static void Enable() { TBus::Enable(ENF); } - static uint32_t GetFreq() { + static uint32_t frequency() { return 80000000; } }; class Hardware { public: - class GpioA: public TDevice { - public: - static GPIO_TypeDef& instance() { - return *GPIOA; - } - }; - - class Tim3: public TDevice { - public: - static TIM_TypeDef& instance() { - return *TIM3; - } - }; + typedef TDevice GpioA; + typedef TDevice Tim3; typedef TLed GreenLed; typedef TLed Led2; @@ -193,7 +182,7 @@ Hardware::PwmLed3::SetDutyFactor(ii / 10.f); - ii = (ii+1) % 11; // 0 .. 10 + ii = (ii + 1) % 11; // 0 .. 10 for (int i = 0; i < 1000000; i++) {