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
|
5
|
48 template<typename TDev, typename TCounter = uint32_t> class TTimerBase {
|
4
|
49 public:
|
5
|
50 typedef TCounter counter_t;
|
|
51
|
6
|
52 static void init() {
|
5
|
53 TDev::instance()->EGR = TIM_EGR_UG;
|
|
54 }
|
|
55
|
|
56 static void period(counter_t value) {
|
|
57 TDev::instance()->ARR = value;
|
4
|
58 }
|
|
59
|
5
|
60 static counter_t period() {
|
|
61 return (counter_t) TDev::instance()->ARR;
|
4
|
62 }
|
|
63
|
5
|
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();
|
4
|
74 }
|
|
75 };
|
|
76
|
5
|
77 template<typename TDev, typename TCounter = uint32_t> class TTimerChannel1 {
|
4
|
78 public:
|
5
|
79 typedef TCounter counter_t;
|
|
80 static void duty(counter_t value) {
|
|
81 TDev::instance()->CCR1 = (uint32_t)value;
|
4
|
82 }
|
|
83
|
5
|
84 static counter_t duty() {
|
|
85 return (counter_t)TDev::instance()->CCR1;
|
4
|
86 }
|
|
87 };
|
|
88
|
3
|
89 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed {
|
|
90 TLed();
|
|
91 explicit TLed(const TLed&);
|
2
|
92 public:
|
3
|
93 static void Init() {
|
|
94 TTraits::SetPinMode(PIN, GpioModeOutput);
|
|
95 TTraits::SetPinOutputType(PIN, GpioPushPull);
|
|
96 TTraits::SetPinPullMode(PIN, GpioNoPull);
|
|
97 }
|
2
|
98
|
3
|
99 static void Set() {
|
|
100 TTraits::WritePin(PIN, 1);
|
2
|
101 }
|
|
102
|
3
|
103 static void Reset() {
|
|
104 TTraits::WritePin(PIN, 0);
|
|
105 }
|
|
106
|
|
107 static void Toogle() {
|
|
108 TTraits::TooglePin(PIN);
|
|
109 }
|
|
110 };
|
|
111
|
2
|
112 class AHB2Bus {
|
|
113 public:
|
6
|
114 static void enable(unsigned int flag) {
|
2
|
115 RCC->AHB2ENR |= flag;
|
|
116 }
|
6
|
117
|
|
118 static uint32_t clock() {
|
|
119
|
|
120 }
|
2
|
121 };
|
0
|
122
|
3
|
123 class APB1Bus {
|
|
124 public:
|
6
|
125 static void enable(unsigned int flag) {
|
3
|
126 RCC->APB1ENR1 |= flag;
|
|
127 }
|
|
128 };
|
|
129
|
|
130 class APB1Bus2 {
|
6
|
131 static void enable(uint32_t flag) {
|
3
|
132 RCC->APB1ENR2 |= flag;
|
|
133 }
|
|
134 };
|
|
135
|
6
|
136 template <typename TRegs, typename TBus, uint32_t BASE, uint32_t ENF> class TDevice {
|
2
|
137 public:
|
6
|
138 typedef TBus bus_t;
|
|
139
|
5
|
140 static TRegs* instance() {
|
|
141 return (TRegs*) BASE;
|
|
142 }
|
|
143
|
6
|
144 static void enable() {
|
|
145 TBus::enable(ENF);
|
2
|
146 }
|
4
|
147
|
5
|
148 static uint32_t frequency() {
|
4
|
149 return 80000000;
|
|
150 }
|
2
|
151 };
|
|
152
|
6
|
153 class OscMsi {
|
|
154 };
|
|
155
|
|
156 class OscHsi {
|
|
157 };
|
|
158
|
|
159 class OscHse {
|
|
160 };
|
|
161
|
|
162 class OscLsi {
|
|
163 };
|
|
164
|
|
165 class OscLse {
|
|
166 };
|
|
167
|
|
168 class Pll {
|
|
169 };
|
|
170
|
|
171 class Pclk1 {
|
|
172 public:
|
|
173 uint32_t clock() {
|
|
174 return APB1Bus::clock();
|
|
175 }
|
|
176 };
|
|
177
|
|
178 class Rcc {
|
|
179 public:
|
|
180 typedef enum {
|
|
181 PClk1,
|
|
182 Apb1TimerClk,
|
|
183 PClk2,
|
|
184 Apb2TimerClk,
|
|
185 HClk,
|
|
186 SysClk,
|
|
187 HsiClk,
|
|
188 HseClk,
|
|
189 LsiClk,
|
|
190 LseClk,
|
|
191 MsiClk,
|
|
192 PllClk,
|
|
193 PllQClk,
|
|
194 PllPClk,
|
|
195 PllSai1P,
|
|
196 PllSai1Q,
|
|
197 PllSai1R,
|
|
198 PllSai2P,
|
|
199 PllSai2R
|
|
200 } ClockSource;
|
|
201
|
|
202 template <ClockSource clk> unit32_t clock();
|
|
203 };
|
|
204
|
|
205 class SysClk {
|
|
206 public:
|
|
207 uint32_t clock() {
|
|
208 }
|
|
209 };
|
|
210
|
|
211 class UartClockMux {
|
|
212 public:
|
|
213 template <typename TClk> void select_clock_source();
|
|
214 };
|
|
215
|
|
216 template<> void UartClockMux::select_clock_source<Pclk1>() {
|
|
217 }
|
|
218
|
|
219 template<> void UartClockMux::select_clock_source<SysClk>() {
|
|
220 }
|
|
221
|
3
|
222 class Hardware {
|
2
|
223 public:
|
5
|
224 typedef TDevice<GPIO_TypeDef, GPIOA_BASE, AHB2Bus, RCC_AHB2ENR_GPIOAEN> GpioA;
|
|
225 typedef TDevice<TIM_TypeDef, TIM3_BASE, APB1Bus, RCC_APB1ENR1_TIM3EN> Tim3;
|
2
|
226
|
4
|
227 typedef TLed<GpioA, 5> GreenLed;
|
|
228 typedef TLed<GpioA, 6> Led2;
|
|
229 typedef TPwmOut<GpioA, 7, Tim3, 2> PwmLed3;
|
2
|
230 };
|
|
231
|
|
232 int main(void) {
|
0
|
233
|
2
|
234 /**
|
|
235 * IMPORTANT NOTE!
|
|
236 * The symbol VECT_TAB_SRAM needs to be defined when building the project
|
|
237 * if code has been located to RAM and interrupts are used.
|
|
238 * Otherwise the interrupt table located in flash will be used.
|
|
239 * See also the <system_*.c> file and how the SystemInit() function updates
|
|
240 * SCB->VTOR register.
|
|
241 * E.g. SCB->VTOR = 0x20000000;
|
|
242 */
|
|
243
|
|
244 /* TODO - Add your application code here */
|
0
|
245
|
3
|
246 Hardware::GpioA::Enable();
|
4
|
247 Hardware::Tim3::Enable();
|
|
248 Hardware::GreenLed::Init();
|
|
249 Hardware::Led2::Init();
|
|
250 Hardware::PwmLed3::Init(1000);
|
2
|
251
|
4
|
252 int ii = 0;
|
2
|
253 /* Infinite loop */
|
|
254 while (1) {
|
4
|
255 Hardware::GreenLed::Toogle();
|
|
256 Hardware::Led2::Toogle();
|
|
257
|
|
258 Hardware::PwmLed3::SetDutyFactor(ii / 10.f);
|
|
259
|
5
|
260 ii = (ii + 1) % 11; // 0 .. 10
|
2
|
261
|
|
262 for (int i = 0; i < 1000000; i++) {
|
4
|
263
|
2
|
264 }
|
|
265 }
|
0
|
266 }
|