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