annotate l476rg-hal-test/src/main.cpp @ 5:0d3eea2dd7ea

working on devices and timers
author cin
date Fri, 20 Jan 2017 03:40:30 +0300
parents ca4f5b55b391
children ca42336826bd
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
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
48 template<typename TDev, typename TCounter = uint32_t> class TTimerBase {
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
49 public:
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
50 typedef TCounter counter_t;
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
51
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
52 static void Init() {
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
53 TDev::instance()->EGR = TIM_EGR_UG;
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
54 }
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
55
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
56 static void period(counter_t value) {
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
57 TDev::instance()->ARR = value;
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
58 }
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
59
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
60 static counter_t period() {
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
61 return (counter_t) TDev::instance()->ARR;
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
62 }
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
63
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
64 static void prescaler(counter_t value) {
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
65 TDev::instance()->PSC = value;
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
66 }
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
67
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
68 static counter_t prescaler() {
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
69 return TDev::instance()->PSC;
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
70 }
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
71
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
72 static uint32_t resolution() {
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
73 return TDev::frequency() / prescaler();
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
74 }
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
75 };
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
76
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
77 template<typename TDev, typename TCounter = uint32_t> class TTimerChannel1 {
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
78 public:
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
79 typedef TCounter counter_t;
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
80 static void duty(counter_t value) {
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
81 TDev::instance()->CCR1 = (uint32_t)value;
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
82 }
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
83
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
84 static counter_t duty() {
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
85 return (counter_t)TDev::instance()->CCR1;
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
86 }
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
87 };
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
88
3
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
89 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed {
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
90 TLed();
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
91 explicit TLed(const TLed&);
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
92 public:
3
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
93 static void Init() {
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
94 TTraits::SetPinMode(PIN, GpioModeOutput);
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
95 TTraits::SetPinOutputType(PIN, GpioPushPull);
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
96 TTraits::SetPinPullMode(PIN, GpioNoPull);
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
97 }
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
98
3
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
99 static void Set() {
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
100 TTraits::WritePin(PIN, 1);
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
101 }
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 Reset() {
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
104 TTraits::WritePin(PIN, 0);
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
105 }
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
106
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
107 static void Toogle() {
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
108 TTraits::TooglePin(PIN);
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
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
112 class AHB2Bus {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
113 public:
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
114 static void Enable(unsigned int flag) {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
115 RCC->AHB2ENR |= flag;
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
116 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
117 };
0
32a3b1785697 a rough draft of Hardware Abstraction Layer for C++
cin
parents:
diff changeset
118
3
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
119 class APB1Bus {
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
120 public:
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
121 static void Enable(unsigned int flag) {
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
122 RCC->APB1ENR1 |= flag;
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
123 }
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
124 };
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
125
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
126 class APB1Bus2 {
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
127 static void Enable(uint32_t flag) {
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
128 RCC->APB1ENR2 |= flag;
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
129 }
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
130 };
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
131
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
132 template<typename TRegs, typename TBus, uint32_t BASE, uint32_t ENF> class TDevice {
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
133 public:
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
134 static TRegs* instance() {
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
135 return (TRegs*) BASE;
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
136 }
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
137
2
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
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
142 static uint32_t frequency() {
4
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:
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
149 typedef TDevice<GPIO_TypeDef, GPIOA_BASE, AHB2Bus, RCC_AHB2ENR_GPIOAEN> GpioA;
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
150 typedef TDevice<TIM_TypeDef, TIM3_BASE, APB1Bus, RCC_APB1ENR1_TIM3EN> Tim3;
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
151
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
152 typedef TLed<GpioA, 5> GreenLed;
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
153 typedef TLed<GpioA, 6> Led2;
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
154 typedef TPwmOut<GpioA, 7, Tim3, 2> PwmLed3;
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
155 };
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
156
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
157 int main(void) {
0
32a3b1785697 a rough draft of Hardware Abstraction Layer for C++
cin
parents:
diff changeset
158
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
159 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
160 * IMPORTANT NOTE!
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
161 * The symbol VECT_TAB_SRAM needs to be defined when building the project
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
162 * if code has been located to RAM and interrupts are used.
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
163 * Otherwise the interrupt table located in flash will be used.
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
164 * See also the <system_*.c> file and how the SystemInit() function updates
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
165 * SCB->VTOR register.
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
166 * E.g. SCB->VTOR = 0x20000000;
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
167 */
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
168
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
169 /* TODO - Add your application code here */
0
32a3b1785697 a rough draft of Hardware Abstraction Layer for C++
cin
parents:
diff changeset
170
3
3d9705e842f8 working on rcc and bus
cin
parents: 2
diff changeset
171 Hardware::GpioA::Enable();
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
172 Hardware::Tim3::Enable();
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
173 Hardware::GreenLed::Init();
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
174 Hardware::Led2::Init();
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
175 Hardware::PwmLed3::Init(1000);
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
176
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
177 int ii = 0;
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
178 /* Infinite loop */
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
179 while (1) {
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
180 Hardware::GreenLed::Toogle();
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
181 Hardware::Led2::Toogle();
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
182
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
183 Hardware::PwmLed3::SetDutyFactor(ii / 10.f);
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
184
5
0d3eea2dd7ea working on devices and timers
cin
parents: 4
diff changeset
185 ii = (ii + 1) % 11; // 0 .. 10
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
186
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
187 for (int i = 0; i < 1000000; i++) {
4
ca4f5b55b391 working on pwm
cin
parents: 3
diff changeset
188
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
189 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
190 }
0
32a3b1785697 a rough draft of Hardware Abstraction Layer for C++
cin
parents:
diff changeset
191 }