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
|
|
52 static void Init() {
|
|
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:
|
|
114 static void Enable(unsigned int flag) {
|
|
115 RCC->AHB2ENR |= flag;
|
|
116 }
|
|
117 };
|
0
|
118
|
3
|
119 class APB1Bus {
|
|
120 public:
|
|
121 static void Enable(unsigned int flag) {
|
|
122 RCC->APB1ENR1 |= flag;
|
|
123 }
|
|
124 };
|
|
125
|
|
126 class APB1Bus2 {
|
|
127 static void Enable(uint32_t flag) {
|
|
128 RCC->APB1ENR2 |= flag;
|
|
129 }
|
|
130 };
|
|
131
|
5
|
132 template<typename TRegs, typename TBus, uint32_t BASE, uint32_t ENF> class TDevice {
|
2
|
133 public:
|
5
|
134 static TRegs* instance() {
|
|
135 return (TRegs*) BASE;
|
|
136 }
|
|
137
|
2
|
138 static void Enable() {
|
|
139 TBus::Enable(ENF);
|
|
140 }
|
4
|
141
|
5
|
142 static uint32_t frequency() {
|
4
|
143 return 80000000;
|
|
144 }
|
2
|
145 };
|
|
146
|
3
|
147 class Hardware {
|
2
|
148 public:
|
5
|
149 typedef TDevice<GPIO_TypeDef, GPIOA_BASE, AHB2Bus, RCC_AHB2ENR_GPIOAEN> GpioA;
|
|
150 typedef TDevice<TIM_TypeDef, TIM3_BASE, APB1Bus, RCC_APB1ENR1_TIM3EN> Tim3;
|
2
|
151
|
4
|
152 typedef TLed<GpioA, 5> GreenLed;
|
|
153 typedef TLed<GpioA, 6> Led2;
|
|
154 typedef TPwmOut<GpioA, 7, Tim3, 2> PwmLed3;
|
2
|
155 };
|
|
156
|
|
157 int main(void) {
|
0
|
158
|
2
|
159 /**
|
|
160 * IMPORTANT NOTE!
|
|
161 * The symbol VECT_TAB_SRAM needs to be defined when building the project
|
|
162 * if code has been located to RAM and interrupts are used.
|
|
163 * Otherwise the interrupt table located in flash will be used.
|
|
164 * See also the <system_*.c> file and how the SystemInit() function updates
|
|
165 * SCB->VTOR register.
|
|
166 * E.g. SCB->VTOR = 0x20000000;
|
|
167 */
|
|
168
|
|
169 /* TODO - Add your application code here */
|
0
|
170
|
3
|
171 Hardware::GpioA::Enable();
|
4
|
172 Hardware::Tim3::Enable();
|
|
173 Hardware::GreenLed::Init();
|
|
174 Hardware::Led2::Init();
|
|
175 Hardware::PwmLed3::Init(1000);
|
2
|
176
|
4
|
177 int ii = 0;
|
2
|
178 /* Infinite loop */
|
|
179 while (1) {
|
4
|
180 Hardware::GreenLed::Toogle();
|
|
181 Hardware::Led2::Toogle();
|
|
182
|
|
183 Hardware::PwmLed3::SetDutyFactor(ii / 10.f);
|
|
184
|
5
|
185 ii = (ii + 1) % 11; // 0 .. 10
|
2
|
186
|
|
187 for (int i = 0; i < 1000000; i++) {
|
4
|
188
|
2
|
189 }
|
|
190 }
|
0
|
191 }
|