comparison 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
comparison
equal deleted inserted replaced
3:3d9705e842f8 4:ca4f5b55b391
43 **=========================================================================== 43 **===========================================================================
44 */ 44 */
45 45
46 using namespace halpp; 46 using namespace halpp;
47 47
48 template <typename TDev> class TTimerTraits {
49 public:
50 static void SetPeriod(uint32_t period) {
51 }
52
53 static void SetPrescaler(uint32_t prescaler) {
54 }
55 };
56
57 template <typename TDev> class TGenTimerTraits : public TTimerTraits<TDev> {
58 public:
59 static void SetDuty(uint32_t duty) {
60 TDev::instance().CCR1 = duty;
61 }
62 };
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
48 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed { 93 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed {
49 TLed(); 94 TLed();
50 explicit TLed(const TLed&); 95 explicit TLed(const TLed&);
51 public: 96 public:
52 static void Init() { 97 static void Init() {
66 static void Toogle() { 111 static void Toogle() {
67 TTraits::TooglePin(PIN); 112 TTraits::TooglePin(PIN);
68 } 113 }
69 }; 114 };
70 115
71 template<typename TDev, typename TTraits = TGpioTraits<TDev> > class TLedInst {
72 const unsigned short m_ledNo;
73
74 explicit TLedInst(const TLedInst&);
75 public:
76 TLedInst(unsigned short led) : m_ledNo(led) {
77 }
78 void Init() const {
79 TTraits::SetPinMode(m_ledNo, GpioModeOutput);
80 TTraits::SetPinOutputType(m_ledNo, GpioPushPull);
81 TTraits::SetPinPullMode(m_ledNo, GpioNoPull);
82 }
83
84 void Set() const {
85 TTraits::WritePin(m_ledNo, 1);
86 }
87
88 void Reset() const {
89 TTraits::WritePin(m_ledNo, 0);
90 }
91
92 void Toogle() const {
93 TTraits::TooglePin(m_ledNo);
94 }
95 };
96
97 class AHB2Bus { 116 class AHB2Bus {
98 public: 117 public:
99 static void Enable(unsigned int flag) { 118 static void Enable(unsigned int flag) {
100 RCC->AHB2ENR |= flag; 119 RCC->AHB2ENR |= flag;
101 } 120 }
116 135
117 template<typename TBus, unsigned short ENF> class TDevice { 136 template<typename TBus, unsigned short ENF> class TDevice {
118 public: 137 public:
119 static void Enable() { 138 static void Enable() {
120 TBus::Enable(ENF); 139 TBus::Enable(ENF);
140 }
141
142 static uint32_t GetFreq() {
143 return 80000000;
121 } 144 }
122 }; 145 };
123 146
124 class Hardware { 147 class Hardware {
125 public: 148 public:
128 static GPIO_TypeDef& instance() { 151 static GPIO_TypeDef& instance() {
129 return *GPIOA; 152 return *GPIOA;
130 } 153 }
131 }; 154 };
132 155
133 class Tim3: public TDevice<AHB1Bus, RCC_APB1ENR1_TIM3EN> { 156 class Tim3: public TDevice<APB1Bus, RCC_APB1ENR1_TIM3EN> {
134 public: 157 public:
135 static TIM_TypeDef& instance() { 158 static TIM_TypeDef& instance() {
136 return *TIM3; 159 return *TIM3;
137 } 160 }
138 }; 161 };
139 162
140 static const TLedInst<GpioA> greenLed; 163 typedef TLed<GpioA, 5> GreenLed;
141 static const TLedInst<GpioA> led2; 164 typedef TLed<GpioA, 6> Led2;
142 165 typedef TPwmOut<GpioA, 7, Tim3, 2> PwmLed3;
143 }; 166 };
144
145 const TLedInst<Hardware::GpioA> Hardware::greenLed(5);
146 const TLedInst<Hardware::GpioA> Hardware::led2(5);
147 167
148 int main(void) { 168 int main(void) {
149 169
150 /** 170 /**
151 * IMPORTANT NOTE! 171 * IMPORTANT NOTE!
158 */ 178 */
159 179
160 /* TODO - Add your application code here */ 180 /* TODO - Add your application code here */
161 181
162 Hardware::GpioA::Enable(); 182 Hardware::GpioA::Enable();
163 Hardware::greenLed.Init(); 183 Hardware::Tim3::Enable();
164 Hardware::led2.Init(); 184 Hardware::GreenLed::Init();
165 185 Hardware::Led2::Init();
186 Hardware::PwmLed3::Init(1000);
187
188 int ii = 0;
166 /* Infinite loop */ 189 /* Infinite loop */
167 while (1) { 190 while (1) {
168 Hardware::greenLed.Toogle(); 191 Hardware::GreenLed::Toogle();
169 Hardware::led2.Toogle(); 192 Hardware::Led2::Toogle();
193
194 Hardware::PwmLed3::SetDutyFactor(ii / 10.f);
195
196 ii = (ii+1) % 11; // 0 .. 10
170 197
171 for (int i = 0; i < 1000000; i++) { 198 for (int i = 0; i < 1000000; i++) {
199
172 } 200 }
173 } 201 }
174 } 202 }