Mcucpp

Форк
0
/
PinList.cpp 
323 строки · 8.8 Кб
1
#include <iostream>
2
#include <string>
3
#include <iopins.h>
4
#include <pinlist.h>
5
#include <gtest/gtest.h>
6

7
using namespace std;
8
using namespace Mcucpp;
9
using namespace IO;
10

11

12
template<class Pin>
13
void PinTest()
14
{
15
	typedef typename Pin::Port Port;
16
	typedef typename Pin::ConfigPort ConfigPort;
17
	typedef typename Port::DataT DataT;
18
	const DataT mask = 1 << Pin::Number;
19

20
	cout << __FUNCTION__ << "\tPort: " << (char)Port::Id << "\tPinNumber: " << Pin::Number;
21

22
	Port::Write(0);
23
	Port::template SetConfiguration<DataT(-1), Port::In>();
24
	EXPECT_EQ(0u, Port::OutReg);
25
	Pin::Set();
26
	EXPECT_EQ(mask, Port::OutReg);
27
	Pin::Clear();
28
	EXPECT_EQ(0u, Port::OutReg);
29
	Pin::Set(1);
30
	EXPECT_EQ(mask, Port::OutReg);
31
	Pin::Set(0);
32
	EXPECT_EQ(0u, Port::OutReg);
33
	Pin::Toggle();
34
	EXPECT_EQ(mask, Port::OutReg);
35
	Pin::Toggle();
36
	EXPECT_EQ(0u, Port::OutReg);
37

38
	Pin::SetDirRead();
39
	EXPECT_EQ(0u, Port::DirReg);
40
	Pin::SetDirWrite();
41
	EXPECT_EQ(mask, Port::DirReg);
42

43
	Pin::SetDir(0);
44
	EXPECT_EQ(0u, Port::DirReg);
45
	Pin::SetDir(1);
46
	EXPECT_EQ(mask, Port::DirReg);
47

48
	Pin::SetConfiguration(Port::In);
49
	EXPECT_EQ(0u, Port::DirReg);
50
	Pin::SetConfiguration(Port::Out);
51
	EXPECT_EQ(mask, Port::DirReg);
52

53
	Pin::template SetConfiguration<Port::In>();
54
	EXPECT_EQ(0u, Port::DirReg);
55
	Pin:: template SetConfiguration<Port::Out>();
56
	EXPECT_EQ(mask, Port::DirReg);
57

58
	Port::InReg = 0;
59
	EXPECT_FALSE(Pin::IsSet());
60

61
	Port::InReg = mask;
62
	EXPECT_TRUE(Pin::IsSet());
63

64
	cout << "\tOK" << endl;
65
}
66

67
template<class Pins>
68
struct PrintPinList
69
{
70
    template<class List, int index>
71
    struct Iterator
72
    {
73
        static void Print()
74
        {
75
            Iterator<List, index-1>::Print();
76
            typedef typename List:: template Pin<index-1> CurrentPin;
77
            if(index == List::Length)
78
                std::cout << (char)CurrentPin::Port::Id << CurrentPin::Number;
79
            else
80
                std::cout << (char)CurrentPin::Port::Id << CurrentPin::Number << ", ";
81
        }
82
    };
83

84
    template<class List>
85
    struct Iterator<List, 0>
86
    {
87
        static void Print()
88
        {}
89
    };
90

91
    static void Print()
92
    {
93
        std::cout << "PinList<";
94
        Iterator<Pins, Pins::Length>::Print();
95
        std::cout << ">";
96
    }
97
};
98

99

100
template<class Pins>
101
void TestOnePortPinList(unsigned listValue, unsigned portValue)
102
{
103
    typedef typename Pins::template Pin<0>::Port Port;
104
    typename Pins::DataType val;
105
    cout << __FUNCTION__ << "\t";
106
    PrintPinList<Pins>::Print();
107

108
    Port::Write(0);
109

110
    Pins::Write(listValue);
111
    EXPECT_EQ(Port::OutReg,  portValue);
112
    val = Pins::Read();
113
    EXPECT_EQ(listValue, val);
114

115
    Port::DirReg = 0;
116
    Pins::SetConfiguration(listValue, Pins::Out);
117
    EXPECT_EQ(portValue, Port::DirReg);
118

119
    Port::Write(0);
120
    Port::DirReg = 0;
121

122
    Port::InReg = portValue;
123
    val = Pins::PinRead();
124
    EXPECT_EQ(listValue, val);
125

126
    Port::InReg = 0;
127
    val = Pins::PinRead();
128
    EXPECT_EQ(0, val);
129

130
    Pins::Write(0);
131
    EXPECT_EQ(0u, Port::OutReg);
132

133
    Pins::Set(listValue);
134
    EXPECT_EQ(portValue, Port::OutReg);
135

136
    Pins::Clear(listValue);
137
    EXPECT_EQ(0u, Port::OutReg);
138

139
    Pins::SetConfiguration(Pins::In);
140
    EXPECT_EQ(0u, Port::DirReg);
141

142
    cout << "\tOK" << endl;
143
}
144

145
template<class Pins, unsigned listValue, unsigned portValue>
146
void TestOnePortConstIface()
147
{
148
    typedef typename Pins::template Pin<0>::Port Port;
149
    typename Pins::DataType val;
150
    cout << __FUNCTION__ << "\t";
151
    PrintPinList<Pins>::Print();
152

153
    Port::template Write<0>();
154

155
    Pins::template Write<listValue>();
156
    EXPECT_EQ(Port::OutReg,  portValue);
157
    val = Pins::Read();
158
    EXPECT_EQ(listValue, val);
159

160
    Port::DirReg = 0;
161
    Pins::template SetConfiguration<listValue, Pins::Out>();
162
    EXPECT_EQ(portValue, Port::DirReg);
163

164
    Port::template Write<0>();
165
    Port::DirReg = 0;
166

167
    Port::InReg = portValue;
168
    val = Pins::PinRead();
169
    EXPECT_EQ(listValue, val);
170

171
    Port::InReg = 0;
172
    val = Pins::PinRead();
173
    EXPECT_EQ(0, val);
174

175
    Port::template Write<0>();
176
    EXPECT_EQ(0u, Port::OutReg);
177

178
    Pins::template Set<listValue>();
179
    EXPECT_EQ(portValue, Port::OutReg);
180

181
    Pins::template Clear<listValue>();
182
    EXPECT_EQ(0u, Port::OutReg);
183

184
    Pins::template SetConfiguration<0xff, Pins::In>();
185
    EXPECT_EQ(0u, Port::DirReg);
186

187
    cout << "\tOK" << endl;
188
}
189

190
template<class Pins, class Port1, class Port2>
191
void Test2PortConfiguration(unsigned listValue, unsigned portValue, unsigned portValue2)
192
{
193
    typename Pins::DataType val;
194
    cout << __FUNCTION__ << "\t";
195

196
	Port1::OutReg = 0;
197
	Port2::OutReg = 0;
198
	Port1::DirReg = 0;
199
	Port2::DirReg = 0;
200

201
    PrintPinList<Pins>::Print();
202
    Pins::Write(listValue);
203
    EXPECT_EQ(portValue, Port1::OutReg);
204
    EXPECT_EQ(portValue2, Port2::OutReg);
205
    val = Pins::Read();
206
    EXPECT_EQ(listValue, val);
207

208
    Port1::OutReg = 0;
209
    Pins::Set(listValue);
210
    EXPECT_EQ(portValue, Port1::OutReg);
211
    EXPECT_EQ(portValue2, Port2::OutReg);
212
    val = Pins::Read();
213
    EXPECT_EQ(listValue, val);
214

215
    Pins::Clear(listValue);
216
    EXPECT_EQ(0u, Port1::OutReg);
217
    EXPECT_EQ(0u, Port2::OutReg);
218
    val = Pins::Read();
219
    EXPECT_EQ(0u, val);
220

221
    Pins::SetConfiguration(listValue, Pins::Out);
222
    EXPECT_EQ(portValue, Port1::DirReg);
223
    EXPECT_EQ(portValue2, Port2::DirReg);
224
    cout << "\tOK" << endl;
225
}
226

227
TEST(GPIO, PinsTests)
228
{
229
	PinTest<Pa0>();
230
	PinTest<Pa1>();
231
	PinTest<Pa2>();
232
	PinTest<Pa3>();
233
	PinTest<Pa4>();
234
	PinTest<Pa5>();
235
	PinTest<Pa6>();
236
	PinTest<Pa7>();
237
	PinTest<Pa8>();
238
	PinTest<Pa9>();
239
	PinTest<Pa10>();
240
	PinTest<Pa11>();
241
	PinTest<Pa12>();
242
	PinTest<Pa13>();
243
	PinTest<Pa14>();
244
	PinTest<Pa15>();
245
	PinTest<Pa16>();
246
	PinTest<Pa17>();
247
	PinTest<Pa18>();
248
	PinTest<Pa19>();
249
	PinTest<Pa20>();
250
	PinTest<Pa21>();
251
	PinTest<Pa22>();
252
	PinTest<Pa23>();
253
	PinTest<Pa24>();
254
	PinTest<Pa25>();
255
	PinTest<Pa26>();
256
	PinTest<Pa27>();
257
	PinTest<Pa28>();
258
	PinTest<Pa29>();
259
	PinTest<Pa30>();
260
	PinTest<Pa31>();
261
}
262

263
TEST(GPIO, PinListTest)
264
{
265
    for(int i=0; i< 16; i++)
266
    {
267
        cout << "Writing value: " << i << endl;
268
        TestOnePortPinList<PinList<Pa0, Pa1, Pa2, Pa3> >(i, i);
269
        TestOnePortPinList<PinList<Pa1, Pa2, Pa3, Pa4> >(i, i << 1);
270
        TestOnePortPinList<PinList<Pa2, Pa3, Pa4, Pa5> >(i, i << 2);
271
    }
272

273
    TestOnePortPinList<PinList<Pa1, Pa3, Pa2, Pa0> >(0x0f, 0x0f);
274
    TestOnePortPinList<PinList<Pa0, Pa2, Pa1, Pa3> >(0x0f, 0x0f);
275
    TestOnePortPinList<PinList<Pa2, Pa1, Pa3, Pa4, Pa6> >(0x1f, 0x5e);
276

277
    TestOnePortPinList<PinList<Pa5, Pa6, Pa7, Pa0, Pa1, Pa2, Pa3, Pa4> >(0xff, 0xff);
278

279
    TestOnePortPinList<PinList<Pa2, Pa1, Pa3, Pa4, Pa6, Pa8, Pa7, Pa0, Pa5> >(0x1ff, 0x1ff);
280

281
    TestOnePortPinList<PinList<Pa0, Pa1, Pa2, Pa3, Pa4, Pa5, Pa6, Pa7, Pa8>::Slice<5, 4> >(0x1e0, 0x1e0);
282
    cout << "Length = \t" <<PinList<Pa0, Pa1, Pa2, Pa3, Pa4, Pa5, Pa6, Pa7, Pa8>::Slice<5, 4>::Length << std::endl;
283
    TestOnePortPinList<PinList<Pa0, Pa1, Pa2, Pa3, Pa4, Pa5, Pa6, Pa7, Pa8>::Slice<0, 4> >(0x0f, 0x0f);
284

285
    TestOnePortPinList<PinList<Pa4, Pa1, Pa6, Pa3, Pa7, Pa5, Pa0 > >(0x7f, 0xfb);
286
    TestOnePortPinList<PinList<Pa4, Pa1, Pa6, Pa3, Pa2, Pa5, Pa0, Pa7 > >(0xaa, 0xaa);
287
    TestOnePortPinList<PinList<Pa4, Pa1, Pa6, Pa3, Pa2, Pa5, Pa0, Pa7 > >(0x55, 0x55);
288

289
    TestOnePortPinList<PinList<Pa1, Pa2, Pa14, Pa15> >(0x0f, (0x3 << 1) | (0x3 << 14));
290

291
    typedef PinList<Pa2, Pa1, Pa3, Pa4, Pa6> Pins1;
292
    typedef PinList<Pins1::Pin<0>, Pins1::Pin<1>, Pins1::Pin<2>, Pins1::Pin<3>, Pins1::Pin<4> > Pins1Clone;
293

294
    TestOnePortPinList<Pins1Clone >(0x1f, 0x5e);
295

296
    TestOnePortPinList<PinList<Pa1, Pa3, NullPin, Pa0> >(0x0b, 0x0b);
297

298
    TestOnePortConstIface<PinList<Pa1, Pa3, Pa2, Pa0>, 0x0f, 0x0f>();
299
    TestOnePortConstIface<PinList<Pa0, Pa2, Pa1, Pa3>, 0x0f, 0x0f>();
300
    TestOnePortConstIface<PinList<Pa2, Pa1, Pa3, Pa4, Pa6>, 0x1f, 0x5e>();
301
    TestOnePortConstIface<PinList<Pa5, Pa6, Pa7, Pa0, Pa1, Pa2, Pa3, Pa4>, 0xff, 0xff>();
302
    TestOnePortConstIface<PinList<Pa2, Pa1, Pa3, Pa4, Pa6, Pa8, Pa7, Pa0, Pa5>, 0x1ff, 0x1ff>();
303
    TestOnePortConstIface<PinList<Pa0, Pa1, Pa2, Pa3, Pa4, Pa5, Pa6, Pa7, Pa8>::Slice<5, 4>, 0x1e0, 0x1e0>();
304
    TestOnePortConstIface<PinList<Pa0, Pa1, Pa2, Pa3, Pa4, Pa5, Pa6, Pa7, Pa8>::Slice<0, 4>, 0x0f, 0x0f>();
305
    TestOnePortConstIface<Pins1Clone, 0x1f, 0x5e>();
306

307
    Test2PortConfiguration<PinList<Pa1, Pa3, Pa2, Pa0, Pb1, Pb3, Pb2, Pb0>, Porta, Portb>(0xff, 0x0f, 0x0f);
308
    Test2PortConfiguration<PinList<Pa1, Pa2, Pa3, Pa0, Pb0, Pb1, Pb2, Pb3>, Porta, Portb>(0xff, 0x0f, 0x0f);
309
}
310

311
TEST(GPIO, NullPin)
312
{
313
	NullPin::Set();
314
	NullPin::Set(false);
315
	NullPin::Set(true);
316
	NullPin::Clear();
317
	NullPin::Toggle();
318
	NullPin::SetConfiguration(NullPin::Port::Out);
319
	NullPin::SetConfiguration(Mcucpp::IO::NativePortBase::Out);
320
    NullPin::Port::Enable();
321
    NullPin::Port::Disable();
322
	EXPECT_FALSE(NullPin::IsSet());
323
}
324

325

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.