annotate l476rg-hal-test/src/main.cpp @ 2:0c59e7a7782a

Working on GPIO and RCC
author cin
date Mon, 16 Jan 2017 11:04:47 +0300
parents a0b14b11ad9f
children 3d9705e842f8
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
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
48 template<typename TDev, unsigned short PIN> class TLed {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
49 TDev& m_dev;
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
50 public:
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
51 TLed(TDev& dev) :
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
52 m_dev(dev) {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
53
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
54 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
55
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
56 void Init() const {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
57 m_dev.SetPinMode(5, halpp::GpioModeOutput);
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
58 m_dev.SetPinOutputType(5, halpp::GpioOutputType::GpioPushPull);
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
59 m_dev.SetPinPullMode(5, halpp::GpioPullMode::GpioNoPull);
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
60 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
61
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
62 void Set() const {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
63 m_dev.WritePin(PIN, 1);
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
64 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
65
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
66 void Reset() const {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
67 m_dev.WritePin(PIN, 0);
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
68 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
69
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
70 void Toogle() const {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
71 m_dev.TooglePin(PIN);
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
72 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
73 };
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
74
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
75 class AHB2Bus {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
76 public:
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
77 static void Enable(unsigned int flag) {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
78 RCC->AHB2ENR |= flag;
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
79 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
80 };
0
32a3b1785697 a rough draft of Hardware Abstraction Layer for C++
cin
parents:
diff changeset
81
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
82 template<typename TBus, unsigned short ENF> class TDevice {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
83 public:
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
84 static void Enable() {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
85 TBus::Enable(ENF);
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
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
89 class GpioA : public TGpio<GPIO_TypeDef>, public TDevice<AHB2Bus, RCC_AHB2ENR_GPIOAEN> {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
90 static GpioA m_instance;
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
91 typedef TGpio<GPIO_TypeDef> GpioBase;
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
92 public:
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
93
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
94 GpioA(GPIO_TypeDef& dev) : GpioBase(dev) {
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
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
98 static GpioA& instance() {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
99 return m_instance;
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
100 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
101 };
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
102
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
103 GpioA GpioA::m_instance(*GPIOA);
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
104
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
105 int main(void) {
0
32a3b1785697 a rough draft of Hardware Abstraction Layer for C++
cin
parents:
diff changeset
106
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
107 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
108 * IMPORTANT NOTE!
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
109 * The symbol VECT_TAB_SRAM needs to be defined when building the project
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
110 * if code has been located to RAM and interrupts are used.
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
111 * Otherwise the interrupt table located in flash will be used.
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
112 * See also the <system_*.c> file and how the SystemInit() function updates
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
113 * SCB->VTOR register.
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
114 * E.g. SCB->VTOR = 0x20000000;
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
115 */
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
116
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
117 /* TODO - Add your application code here */
0
32a3b1785697 a rough draft of Hardware Abstraction Layer for C++
cin
parents:
diff changeset
118
2
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
119 GpioA::Enable();
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
120
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
121 TLed<halpp::TGpio<GPIO_TypeDef>, 5> led1(GpioA::instance());
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
122 led1.Init();
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
123
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
124 /* Infinite loop */
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
125 while (1) {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
126 led1.Toogle();
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
127
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
128 for (int i = 0; i < 1000000; i++) {
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
129 }
0c59e7a7782a Working on GPIO and RCC
cin
parents: 1
diff changeset
130 }
0
32a3b1785697 a rough draft of Hardware Abstraction Layer for C++
cin
parents:
diff changeset
131 }