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
|
3
|
48 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed {
|
|
49 TLed();
|
|
50 explicit TLed(const TLed&);
|
2
|
51 public:
|
3
|
52 static void Init() {
|
|
53 TTraits::SetPinMode(PIN, GpioModeOutput);
|
|
54 TTraits::SetPinOutputType(PIN, GpioPushPull);
|
|
55 TTraits::SetPinPullMode(PIN, GpioNoPull);
|
|
56 }
|
2
|
57
|
3
|
58 static void Set() {
|
|
59 TTraits::WritePin(PIN, 1);
|
2
|
60 }
|
|
61
|
3
|
62 static void Reset() {
|
|
63 TTraits::WritePin(PIN, 0);
|
|
64 }
|
|
65
|
|
66 static void Toogle() {
|
|
67 TTraits::TooglePin(PIN);
|
|
68 }
|
|
69 };
|
|
70
|
|
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 }
|
2
|
78 void Init() const {
|
3
|
79 TTraits::SetPinMode(m_ledNo, GpioModeOutput);
|
|
80 TTraits::SetPinOutputType(m_ledNo, GpioPushPull);
|
|
81 TTraits::SetPinPullMode(m_ledNo, GpioNoPull);
|
2
|
82 }
|
|
83
|
|
84 void Set() const {
|
3
|
85 TTraits::WritePin(m_ledNo, 1);
|
2
|
86 }
|
|
87
|
|
88 void Reset() const {
|
3
|
89 TTraits::WritePin(m_ledNo, 0);
|
2
|
90 }
|
|
91
|
|
92 void Toogle() const {
|
3
|
93 TTraits::TooglePin(m_ledNo);
|
2
|
94 }
|
|
95 };
|
|
96
|
|
97 class AHB2Bus {
|
|
98 public:
|
|
99 static void Enable(unsigned int flag) {
|
|
100 RCC->AHB2ENR |= flag;
|
|
101 }
|
|
102 };
|
0
|
103
|
3
|
104 class APB1Bus {
|
|
105 public:
|
|
106 static void Enable(unsigned int flag) {
|
|
107 RCC->APB1ENR1 |= flag;
|
|
108 }
|
|
109 };
|
|
110
|
|
111 class APB1Bus2 {
|
|
112 static void Enable(uint32_t flag) {
|
|
113 RCC->APB1ENR2 |= flag;
|
|
114 }
|
|
115 };
|
|
116
|
2
|
117 template<typename TBus, unsigned short ENF> class TDevice {
|
|
118 public:
|
|
119 static void Enable() {
|
|
120 TBus::Enable(ENF);
|
|
121 }
|
|
122 };
|
|
123
|
3
|
124 class Hardware {
|
2
|
125 public:
|
3
|
126 class GpioA: public TDevice<AHB2Bus, RCC_AHB2ENR_GPIOAEN> {
|
|
127 public:
|
|
128 static GPIO_TypeDef& instance() {
|
|
129 return *GPIOA;
|
|
130 }
|
|
131 };
|
2
|
132
|
3
|
133 class Tim3: public TDevice<AHB1Bus, RCC_APB1ENR1_TIM3EN> {
|
|
134 public:
|
|
135 static TIM_TypeDef& instance() {
|
|
136 return *TIM3;
|
|
137 }
|
|
138 };
|
2
|
139
|
3
|
140 static const TLedInst<GpioA> greenLed;
|
|
141 static const TLedInst<GpioA> led2;
|
|
142
|
2
|
143 };
|
|
144
|
3
|
145 const TLedInst<Hardware::GpioA> Hardware::greenLed(5);
|
|
146 const TLedInst<Hardware::GpioA> Hardware::led2(5);
|
2
|
147
|
|
148 int main(void) {
|
0
|
149
|
2
|
150 /**
|
|
151 * IMPORTANT NOTE!
|
|
152 * The symbol VECT_TAB_SRAM needs to be defined when building the project
|
|
153 * if code has been located to RAM and interrupts are used.
|
|
154 * Otherwise the interrupt table located in flash will be used.
|
|
155 * See also the <system_*.c> file and how the SystemInit() function updates
|
|
156 * SCB->VTOR register.
|
|
157 * E.g. SCB->VTOR = 0x20000000;
|
|
158 */
|
|
159
|
|
160 /* TODO - Add your application code here */
|
0
|
161
|
3
|
162 Hardware::GpioA::Enable();
|
|
163 Hardware::greenLed.Init();
|
|
164 Hardware::led2.Init();
|
2
|
165
|
|
166 /* Infinite loop */
|
|
167 while (1) {
|
3
|
168 Hardware::greenLed.Toogle();
|
|
169 Hardware::led2.Toogle();
|
2
|
170
|
|
171 for (int i = 0; i < 1000000; i++) {
|
|
172 }
|
|
173 }
|
0
|
174 }
|