annotate l476rg-hal-test/src/main.cpp @ 3:3d9705e842f8

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