0
|
1 /*
|
2
|
2 ******************************************************************************
|
|
3 File: main.c
|
|
4 Info: Generated by Atollic TrueSTUDIO(R) 7.0.1 2017-01-12
|
0
|
5
|
2
|
6 The MIT License (MIT)
|
|
7 Copyright (c) 2009-2016 Atollic AB
|
0
|
8
|
2
|
9 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10 of this software and associated documentation files (the "Software"), to deal
|
|
11 in the Software without restriction, including without limitation the rights
|
|
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13 copies of the Software, and to permit persons to whom the Software is
|
|
14 furnished to do so, subject to the following conditions:
|
0
|
15
|
2
|
16 The above copyright notice and this permission notice shall be included in all
|
|
17 copies or substantial portions of the Software.
|
0
|
18
|
2
|
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25 SOFTWARE.
|
|
26 ******************************************************************************
|
|
27 */
|
0
|
28
|
|
29 /* Includes */
|
|
30 #include "stm32l4xx.h"
|
2
|
31 #include "gpio.h"
|
0
|
32
|
|
33 /* Private macro */
|
|
34 /* Private variables */
|
|
35 /* Private function prototypes */
|
|
36 /* Private functions */
|
|
37
|
|
38 /**
|
2
|
39 **===========================================================================
|
|
40 **
|
|
41 ** Abstract: main program
|
|
42 **
|
|
43 **===========================================================================
|
|
44 */
|
|
45
|
|
46 using namespace halpp;
|
|
47
|
4
|
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
|
3
|
93 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed {
|
|
94 TLed();
|
|
95 explicit TLed(const TLed&);
|
2
|
96 public:
|
3
|
97 static void Init() {
|
|
98 TTraits::SetPinMode(PIN, GpioModeOutput);
|
|
99 TTraits::SetPinOutputType(PIN, GpioPushPull);
|
|
100 TTraits::SetPinPullMode(PIN, GpioNoPull);
|
|
101 }
|
2
|
102
|
3
|
103 static void Set() {
|
|
104 TTraits::WritePin(PIN, 1);
|
2
|
105 }
|
|
106
|
3
|
107 static void Reset() {
|
|
108 TTraits::WritePin(PIN, 0);
|
|
109 }
|
|
110
|
|
111 static void Toogle() {
|
|
112 TTraits::TooglePin(PIN);
|
|
113 }
|
|
114 };
|
|
115
|
2
|
116 class AHB2Bus {
|
|
117 public:
|
|
118 static void Enable(unsigned int flag) {
|
|
119 RCC->AHB2ENR |= flag;
|
|
120 }
|
|
121 };
|
0
|
122
|
3
|
123 class APB1Bus {
|
|
124 public:
|
|
125 static void Enable(unsigned int flag) {
|
|
126 RCC->APB1ENR1 |= flag;
|
|
127 }
|
|
128 };
|
|
129
|
|
130 class APB1Bus2 {
|
|
131 static void Enable(uint32_t flag) {
|
|
132 RCC->APB1ENR2 |= flag;
|
|
133 }
|
|
134 };
|
|
135
|
2
|
136 template<typename TBus, unsigned short ENF> class TDevice {
|
|
137 public:
|
|
138 static void Enable() {
|
|
139 TBus::Enable(ENF);
|
|
140 }
|
4
|
141
|
|
142 static uint32_t GetFreq() {
|
|
143 return 80000000;
|
|
144 }
|
2
|
145 };
|
|
146
|
3
|
147 class Hardware {
|
2
|
148 public:
|
3
|
149 class GpioA: public TDevice<AHB2Bus, RCC_AHB2ENR_GPIOAEN> {
|
|
150 public:
|
|
151 static GPIO_TypeDef& instance() {
|
|
152 return *GPIOA;
|
|
153 }
|
|
154 };
|
2
|
155
|
4
|
156 class Tim3: public TDevice<APB1Bus, RCC_APB1ENR1_TIM3EN> {
|
3
|
157 public:
|
|
158 static TIM_TypeDef& instance() {
|
|
159 return *TIM3;
|
|
160 }
|
|
161 };
|
2
|
162
|
4
|
163 typedef TLed<GpioA, 5> GreenLed;
|
|
164 typedef TLed<GpioA, 6> Led2;
|
|
165 typedef TPwmOut<GpioA, 7, Tim3, 2> PwmLed3;
|
2
|
166 };
|
|
167
|
|
168 int main(void) {
|
0
|
169
|
2
|
170 /**
|
|
171 * IMPORTANT NOTE!
|
|
172 * The symbol VECT_TAB_SRAM needs to be defined when building the project
|
|
173 * if code has been located to RAM and interrupts are used.
|
|
174 * Otherwise the interrupt table located in flash will be used.
|
|
175 * See also the <system_*.c> file and how the SystemInit() function updates
|
|
176 * SCB->VTOR register.
|
|
177 * E.g. SCB->VTOR = 0x20000000;
|
|
178 */
|
|
179
|
|
180 /* TODO - Add your application code here */
|
0
|
181
|
3
|
182 Hardware::GpioA::Enable();
|
4
|
183 Hardware::Tim3::Enable();
|
|
184 Hardware::GreenLed::Init();
|
|
185 Hardware::Led2::Init();
|
|
186 Hardware::PwmLed3::Init(1000);
|
2
|
187
|
4
|
188 int ii = 0;
|
2
|
189 /* Infinite loop */
|
|
190 while (1) {
|
4
|
191 Hardware::GreenLed::Toogle();
|
|
192 Hardware::Led2::Toogle();
|
|
193
|
|
194 Hardware::PwmLed3::SetDutyFactor(ii / 10.f);
|
|
195
|
|
196 ii = (ii+1) % 11; // 0 .. 10
|
2
|
197
|
|
198 for (int i = 0; i < 1000000; i++) {
|
4
|
199
|
2
|
200 }
|
|
201 }
|
0
|
202 }
|