Mercurial > pub > halpp
changeset 5:0d3eea2dd7ea
working on devices and timers
author | cin |
---|---|
date | Fri, 20 Jan 2017 03:40:30 +0300 |
parents | ca4f5b55b391 |
children | ca42336826bd |
files | halpp/include/gpio.h l476rg-hal-test/src/main.cpp |
diffstat | 2 files changed, 42 insertions(+), 53 deletions(-) [+] |
line wrap: on
line diff
--- 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); } };
--- 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 <typename TDev> class TTimerTraits { +template<typename TDev, typename TCounter = uint32_t> 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 <typename TDev> class TGenTimerTraits : public TTimerTraits<TDev> { -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<TGpioDev>, - typename TTimerTraits = TGenTimerTraits<TTimerDev> -> class TPwmOut { - static uint32_t pwmPeriod; +template<typename TDev, typename TCounter = uint32_t> 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<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&); @@ -133,32 +129,25 @@ } }; -template<typename TBus, unsigned short ENF> class TDevice { +template<typename TRegs, typename TBus, uint32_t BASE, uint32_t ENF> 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<AHB2Bus, RCC_AHB2ENR_GPIOAEN> { - public: - static GPIO_TypeDef& instance() { - return *GPIOA; - } - }; - - class Tim3: public TDevice<APB1Bus, RCC_APB1ENR1_TIM3EN> { - public: - static TIM_TypeDef& instance() { - return *TIM3; - } - }; + typedef TDevice<GPIO_TypeDef, GPIOA_BASE, AHB2Bus, RCC_AHB2ENR_GPIOAEN> GpioA; + typedef TDevice<TIM_TypeDef, TIM3_BASE, APB1Bus, RCC_APB1ENR1_TIM3EN> Tim3; typedef TLed<GpioA, 5> GreenLed; typedef TLed<GpioA, 6> 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++) {