comparison 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
comparison
equal deleted inserted replaced
2:0c59e7a7782a 3:3d9705e842f8
43 **=========================================================================== 43 **===========================================================================
44 */ 44 */
45 45
46 using namespace halpp; 46 using namespace halpp;
47 47
48 template<typename TDev, unsigned short PIN> class TLed { 48 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed {
49 TDev& m_dev; 49 TLed();
50 explicit TLed(const TLed&);
50 public: 51 public:
51 TLed(TDev& dev) : 52 static void Init() {
52 m_dev(dev) { 53 TTraits::SetPinMode(PIN, GpioModeOutput);
53 54 TTraits::SetPinOutputType(PIN, GpioPushPull);
55 TTraits::SetPinPullMode(PIN, GpioNoPull);
54 } 56 }
55 57
58 static void Set() {
59 TTraits::WritePin(PIN, 1);
60 }
61
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 }
56 void Init() const { 78 void Init() const {
57 m_dev.SetPinMode(5, halpp::GpioModeOutput); 79 TTraits::SetPinMode(m_ledNo, GpioModeOutput);
58 m_dev.SetPinOutputType(5, halpp::GpioOutputType::GpioPushPull); 80 TTraits::SetPinOutputType(m_ledNo, GpioPushPull);
59 m_dev.SetPinPullMode(5, halpp::GpioPullMode::GpioNoPull); 81 TTraits::SetPinPullMode(m_ledNo, GpioNoPull);
60 } 82 }
61 83
62 void Set() const { 84 void Set() const {
63 m_dev.WritePin(PIN, 1); 85 TTraits::WritePin(m_ledNo, 1);
64 } 86 }
65 87
66 void Reset() const { 88 void Reset() const {
67 m_dev.WritePin(PIN, 0); 89 TTraits::WritePin(m_ledNo, 0);
68 } 90 }
69 91
70 void Toogle() const { 92 void Toogle() const {
71 m_dev.TooglePin(PIN); 93 TTraits::TooglePin(m_ledNo);
72 } 94 }
73 }; 95 };
74 96
75 class AHB2Bus { 97 class AHB2Bus {
76 public: 98 public:
77 static void Enable(unsigned int flag) { 99 static void Enable(unsigned int flag) {
78 RCC->AHB2ENR |= flag; 100 RCC->AHB2ENR |= flag;
79 } 101 }
80 }; 102 };
81 103
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
82 template<typename TBus, unsigned short ENF> class TDevice { 117 template<typename TBus, unsigned short ENF> class TDevice {
83 public: 118 public:
84 static void Enable() { 119 static void Enable() {
85 TBus::Enable(ENF); 120 TBus::Enable(ENF);
86 } 121 }
87 }; 122 };
88 123
89 class GpioA : public TGpio<GPIO_TypeDef>, public TDevice<AHB2Bus, RCC_AHB2ENR_GPIOAEN> { 124 class Hardware {
90 static GpioA m_instance;
91 typedef TGpio<GPIO_TypeDef> GpioBase;
92 public: 125 public:
126 class GpioA: public TDevice<AHB2Bus, RCC_AHB2ENR_GPIOAEN> {
127 public:
128 static GPIO_TypeDef& instance() {
129 return *GPIOA;
130 }
131 };
93 132
94 GpioA(GPIO_TypeDef& dev) : GpioBase(dev) { 133 class Tim3: public TDevice<AHB1Bus, RCC_APB1ENR1_TIM3EN> {
134 public:
135 static TIM_TypeDef& instance() {
136 return *TIM3;
137 }
138 };
95 139
96 } 140 static const TLedInst<GpioA> greenLed;
141 static const TLedInst<GpioA> led2;
97 142
98 static GpioA& instance() {
99 return m_instance;
100 }
101 }; 143 };
102 144
103 GpioA GpioA::m_instance(*GPIOA); 145 const TLedInst<Hardware::GpioA> Hardware::greenLed(5);
146 const TLedInst<Hardware::GpioA> Hardware::led2(5);
104 147
105 int main(void) { 148 int main(void) {
106 149
107 /** 150 /**
108 * IMPORTANT NOTE! 151 * IMPORTANT NOTE!
114 * E.g. SCB->VTOR = 0x20000000; 157 * E.g. SCB->VTOR = 0x20000000;
115 */ 158 */
116 159
117 /* TODO - Add your application code here */ 160 /* TODO - Add your application code here */
118 161
119 GpioA::Enable(); 162 Hardware::GpioA::Enable();
120 163 Hardware::greenLed.Init();
121 TLed<halpp::TGpio<GPIO_TypeDef>, 5> led1(GpioA::instance()); 164 Hardware::led2.Init();
122 led1.Init();
123 165
124 /* Infinite loop */ 166 /* Infinite loop */
125 while (1) { 167 while (1) {
126 led1.Toogle(); 168 Hardware::greenLed.Toogle();
169 Hardware::led2.Toogle();
127 170
128 for (int i = 0; i < 1000000; i++) { 171 for (int i = 0; i < 1000000; i++) {
129 } 172 }
130 } 173 }
131 } 174 }