Mercurial > pub > halpp
comparison l476rg-hal-test/src/main.cpp @ 5:0d3eea2dd7ea
working on devices and timers
author | cin |
---|---|
date | Fri, 20 Jan 2017 03:40:30 +0300 |
parents | ca4f5b55b391 |
children | ca42336826bd |
comparison
equal
deleted
inserted
replaced
4:ca4f5b55b391 | 5:0d3eea2dd7ea |
---|---|
43 **=========================================================================== | 43 **=========================================================================== |
44 */ | 44 */ |
45 | 45 |
46 using namespace halpp; | 46 using namespace halpp; |
47 | 47 |
48 template <typename TDev> class TTimerTraits { | 48 template<typename TDev, typename TCounter = uint32_t> class TTimerBase { |
49 public: | 49 public: |
50 static void SetPeriod(uint32_t period) { | 50 typedef TCounter counter_t; |
51 | |
52 static void Init() { | |
53 TDev::instance()->EGR = TIM_EGR_UG; | |
51 } | 54 } |
52 | 55 |
53 static void SetPrescaler(uint32_t prescaler) { | 56 static void period(counter_t value) { |
57 TDev::instance()->ARR = value; | |
58 } | |
59 | |
60 static counter_t period() { | |
61 return (counter_t) TDev::instance()->ARR; | |
62 } | |
63 | |
64 static void prescaler(counter_t value) { | |
65 TDev::instance()->PSC = value; | |
66 } | |
67 | |
68 static counter_t prescaler() { | |
69 return TDev::instance()->PSC; | |
70 } | |
71 | |
72 static uint32_t resolution() { | |
73 return TDev::frequency() / prescaler(); | |
54 } | 74 } |
55 }; | 75 }; |
56 | 76 |
57 template <typename TDev> class TGenTimerTraits : public TTimerTraits<TDev> { | 77 template<typename TDev, typename TCounter = uint32_t> class TTimerChannel1 { |
58 public: | 78 public: |
59 static void SetDuty(uint32_t duty) { | 79 typedef TCounter counter_t; |
60 TDev::instance().CCR1 = duty; | 80 static void duty(counter_t value) { |
81 TDev::instance()->CCR1 = (uint32_t)value; | |
82 } | |
83 | |
84 static counter_t duty() { | |
85 return (counter_t)TDev::instance()->CCR1; | |
61 } | 86 } |
62 }; | 87 }; |
63 | |
64 template < | |
65 typename TGpioDev, | |
66 unsigned short Pin, | |
67 typename TTimerDev, | |
68 unsigned short Ch, | |
69 typename TGpioTraits = TGpioTraits<TGpioDev>, | |
70 typename TTimerTraits = TGenTimerTraits<TTimerDev> | |
71 > class TPwmOut { | |
72 static uint32_t pwmPeriod; | |
73 public: | |
74 static void Init(uint32_t freq) { | |
75 pwmPeriod = TTimerDev::GetFreq()/freq - 1; | |
76 TTimerTraits::SetPeriod(pwmPeriod); | |
77 } | |
78 | |
79 static void SetDutyFactor(float f) { | |
80 TTimerTraits::SetDuty(pwmPeriod*f); | |
81 } | |
82 }; | |
83 | |
84 template < | |
85 typename TGpioDev, | |
86 unsigned short Pin, | |
87 typename TTimerDev, | |
88 unsigned short Ch, | |
89 typename TGpioTraits, | |
90 typename TTimerTraits | |
91 > uint32_t TPwmOut<TGpioDev, Pin, TTimerDev, Ch, TGpioTraits, TTimerTraits>::pwmPeriod = 0; | |
92 | 88 |
93 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed { | 89 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed { |
94 TLed(); | 90 TLed(); |
95 explicit TLed(const TLed&); | 91 explicit TLed(const TLed&); |
96 public: | 92 public: |
131 static void Enable(uint32_t flag) { | 127 static void Enable(uint32_t flag) { |
132 RCC->APB1ENR2 |= flag; | 128 RCC->APB1ENR2 |= flag; |
133 } | 129 } |
134 }; | 130 }; |
135 | 131 |
136 template<typename TBus, unsigned short ENF> class TDevice { | 132 template<typename TRegs, typename TBus, uint32_t BASE, uint32_t ENF> class TDevice { |
137 public: | 133 public: |
134 static TRegs* instance() { | |
135 return (TRegs*) BASE; | |
136 } | |
137 | |
138 static void Enable() { | 138 static void Enable() { |
139 TBus::Enable(ENF); | 139 TBus::Enable(ENF); |
140 } | 140 } |
141 | 141 |
142 static uint32_t GetFreq() { | 142 static uint32_t frequency() { |
143 return 80000000; | 143 return 80000000; |
144 } | 144 } |
145 }; | 145 }; |
146 | 146 |
147 class Hardware { | 147 class Hardware { |
148 public: | 148 public: |
149 class GpioA: public TDevice<AHB2Bus, RCC_AHB2ENR_GPIOAEN> { | 149 typedef TDevice<GPIO_TypeDef, GPIOA_BASE, AHB2Bus, RCC_AHB2ENR_GPIOAEN> GpioA; |
150 public: | 150 typedef TDevice<TIM_TypeDef, TIM3_BASE, APB1Bus, RCC_APB1ENR1_TIM3EN> Tim3; |
151 static GPIO_TypeDef& instance() { | |
152 return *GPIOA; | |
153 } | |
154 }; | |
155 | |
156 class Tim3: public TDevice<APB1Bus, RCC_APB1ENR1_TIM3EN> { | |
157 public: | |
158 static TIM_TypeDef& instance() { | |
159 return *TIM3; | |
160 } | |
161 }; | |
162 | 151 |
163 typedef TLed<GpioA, 5> GreenLed; | 152 typedef TLed<GpioA, 5> GreenLed; |
164 typedef TLed<GpioA, 6> Led2; | 153 typedef TLed<GpioA, 6> Led2; |
165 typedef TPwmOut<GpioA, 7, Tim3, 2> PwmLed3; | 154 typedef TPwmOut<GpioA, 7, Tim3, 2> PwmLed3; |
166 }; | 155 }; |
191 Hardware::GreenLed::Toogle(); | 180 Hardware::GreenLed::Toogle(); |
192 Hardware::Led2::Toogle(); | 181 Hardware::Led2::Toogle(); |
193 | 182 |
194 Hardware::PwmLed3::SetDutyFactor(ii / 10.f); | 183 Hardware::PwmLed3::SetDutyFactor(ii / 10.f); |
195 | 184 |
196 ii = (ii+1) % 11; // 0 .. 10 | 185 ii = (ii + 1) % 11; // 0 .. 10 |
197 | 186 |
198 for (int i = 0; i < 1000000; i++) { | 187 for (int i = 0; i < 1000000; i++) { |
199 | 188 |
200 } | 189 } |
201 } | 190 } |