3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
13
14#include <unity.h>
15
16#include "adc_chain.h" // Real header - pure C++
17
18void setUp(void) {}
19void tearDown(void) {}
20
22
24inline constexpr Channel chain[]{ Channel::V1, Channel::I1, Channel::V2, Channel::I2, Channel::V3, Channel::I3 };
25
26inline constexpr uint8_t MUX_MASK{ 0x0F };
27inline constexpr uint8_t REF_MASK{ (1U << 7) | (1U << 6) };
28inline constexpr uint8_t AVCC{ 1U << 6 };
29inline constexpr uint8_t INTERNAL_1V1{ (1U << 7) | (1U << 6) };
30inline constexpr uint8_t ADLAR_BIT{ 1U << 5 };
31
33static Channel channelOfAdmux(const uint8_t admux)
34{
35 const uint8_t pin{ static_cast< uint8_t >(admux & MUX_MASK) };
36 for (const Channel ch : chain)
37 {
38 if (AdcChain::pinOf(ch) == pin)
39 {
40 return ch;
41 }
42 }
43 TEST_FAIL_MESSAGE("ADMUX selects an input that is no channel of the chain");
44 return Channel::V1;
45}
46
48{
49 static_assert(AdcChain::NO_OF_CONVERSIONS == 6, "three phases, a V and an I each");
50
51 TEST_ASSERT_EQUAL_UINT8(0, static_cast< uint8_t >(Channel::V1));
52 TEST_ASSERT_EQUAL_UINT8(1, static_cast< uint8_t >(Channel::I1));
53 TEST_ASSERT_EQUAL_UINT8(2, static_cast< uint8_t >(Channel::V2));
54 TEST_ASSERT_EQUAL_UINT8(3, static_cast< uint8_t >(Channel::I2));
55 TEST_ASSERT_EQUAL_UINT8(4, static_cast< uint8_t >(Channel::V3));
56 TEST_ASSERT_EQUAL_UINT8(5, static_cast< uint8_t >(Channel::I3));
57}
58
59void test_pins(void)
60{
61 // V1 A0, I1 A1, V2 A2, I2 A3, V3 A4, I3 A5
62 TEST_ASSERT_EQUAL_UINT8(0, AdcChain::pinOf(Channel::V1));
63 TEST_ASSERT_EQUAL_UINT8(1, AdcChain::pinOf(Channel::I1));
64 TEST_ASSERT_EQUAL_UINT8(2, AdcChain::pinOf(Channel::V2));
65 TEST_ASSERT_EQUAL_UINT8(3, AdcChain::pinOf(Channel::I2));
66 TEST_ASSERT_EQUAL_UINT8(4, AdcChain::pinOf(Channel::V3));
67 TEST_ASSERT_EQUAL_UINT8(5, AdcChain::pinOf(Channel::I3));
68
69 static_assert(AdcChain::pinOf(Channel::I3) == 5, "pinOf() must be constexpr");
70}
71
73{
74 TEST_ASSERT_EQUAL_HEX8(AVCC, AdcChain::refBits(PcbVersion::OLD));
75 TEST_ASSERT_EQUAL_HEX8(INTERNAL_1V1, AdcChain::refBits(PcbVersion::NEW));
76
77 static_assert(AdcChain::refBits(PcbVersion::NEW) == INTERNAL_1V1, "refBits() must be constexpr");
78}
79
81{
82 for (const PcbVersion board : boards)
83 {
84 for (const Channel ch : chain)
85 {
86 TEST_ASSERT_EQUAL_HEX8(AdcChain::refBits(board) | ADLAR_BIT | AdcChain::pinOf(ch), AdcChain::admuxOf(board, ch));
87 }
88 }
89 TEST_ASSERT_EQUAL_HEX8(0x60, AdcChain::admuxOf(PcbVersion::OLD, Channel::V1));
90 TEST_ASSERT_EQUAL_HEX8(0xE5, AdcChain::admuxOf(PcbVersion::NEW, Channel::I3));
91}
92
94{
95 // the ADMUX written after a conversion applies to the conversion after next, wrap included
96 TEST_ASSERT_EQUAL_UINT8(static_cast< uint8_t >(Channel::V2), static_cast< uint8_t >(AdcChain::channelAfterNext(Channel::V1)));
97 TEST_ASSERT_EQUAL_UINT8(static_cast< uint8_t >(Channel::I2), static_cast< uint8_t >(AdcChain::channelAfterNext(Channel::I1)));
98 TEST_ASSERT_EQUAL_UINT8(static_cast< uint8_t >(Channel::V3), static_cast< uint8_t >(AdcChain::channelAfterNext(Channel::V2)));
99 TEST_ASSERT_EQUAL_UINT8(static_cast< uint8_t >(Channel::I3), static_cast< uint8_t >(AdcChain::channelAfterNext(Channel::I2)));
100 TEST_ASSERT_EQUAL_UINT8(static_cast< uint8_t >(Channel::V1), static_cast< uint8_t >(AdcChain::channelAfterNext(Channel::V3))); // wrap
101 TEST_ASSERT_EQUAL_UINT8(static_cast< uint8_t >(Channel::I1), static_cast< uint8_t >(AdcChain::channelAfterNext(Channel::I3))); // wrap
102
103 static_assert(AdcChain::channelAfterNext(Channel::I3) == Channel::I1, "channelAfterNext() must be constexpr");
104}
105
107{
108 for (const PcbVersion board : boards)
109 {
110 for (const Channel ch : chain)
111 {
112 TEST_ASSERT_EQUAL_HEX8(AdcChain::admuxOf(board, AdcChain::channelAfterNext(ch)), AdcChain::admuxWrittenAfter(board, ch));
113 }
114 }
115}
116
118{
119 for (uint8_t round = 0; round != 30; ++round)
120 {
121 TEST_ASSERT_EQUAL_UINT8(round % 6, static_cast< uint8_t >(AdcChain::channelConverted(round)));
122 }
123 static_assert(AdcChain::channelConverted(6) == Channel::V1, "channelConverted() must be constexpr");
124}
125
127{
128 constexpr uint8_t ROUNDS{ 60 };
129
130 for (const PcbVersion board : boards)
131 {
132 // admuxOf[k]: the ADMUX the conversion of round k runs with
133 uint8_t admuxOf[ROUNDS + 2]{};
134
135 // the chain primed with V1, then I1
136 admuxOf[0] = AdcChain::admuxOf(board, Channel::V1);
137 admuxOf[1] = AdcChain::admuxOf(board, Channel::I1);
138
139 for (uint8_t round = 0; round != ROUNDS; ++round)
140 {
141 // the ISR of round k believes it handles channelConverted(k)...
142 const Channel believed{ AdcChain::channelConverted(round) };
143
144 // ... and the ADC really converted the channel its ADMUX selected
145 TEST_ASSERT_EQUAL_UINT8(static_cast< uint8_t >(believed), static_cast< uint8_t >(channelOfAdmux(admuxOf[round])));
146 TEST_ASSERT_EQUAL_HEX8(AdcChain::refBits(board), admuxOf[round] & REF_MASK);
147 TEST_ASSERT_EQUAL_HEX8(ADLAR_BIT, admuxOf[round] & ADLAR_BIT);
148
149 // the conversion of round k+1 has already started: the write of round k applies to round k+2
150 admuxOf[round + 2] = AdcChain::admuxWrittenAfter(board, believed);
151 }
152 }
153}
154
156{
157 // the table processing.cpp held by hand: { next, index, admux }, ADMUX = reference | ADLAR | pin
158 struct Former
159 {
160 uint8_t index;
161 uint8_t setupPin;
162 };
163 constexpr Former former[6]{
164 { 0, 2 }, // V1 -> setup for V2
165 { 1, 3 }, // I1 -> setup for I2
166 { 2, 4 }, // V2 -> setup for V3
167 { 3, 5 }, // I2 -> setup for I3
168 { 4, 0 }, // V3 -> setup for V1
169 { 5, 1 } // I3 -> setup for I1
170 };
171
172 for (const PcbVersion board : boards)
173 {
174 const uint8_t ref{ board == PcbVersion::NEW ? static_cast< uint8_t >(INTERNAL_1V1) : AVCC };
175 for (const Channel ch : chain)
176 {
177 const Former &f{ former[static_cast< uint8_t >(ch)] };
178 TEST_ASSERT_EQUAL_UINT8(f.index, static_cast< uint8_t >(ch));
179 TEST_ASSERT_EQUAL_HEX8(ref | ADLAR_BIT | f.setupPin, AdcChain::admuxWrittenAfter(board, ch));
180 }
181 // the start-up ADMUX: reference, ADLAR, V1 (_ADMUX | sensorV[0])
182 TEST_ASSERT_EQUAL_HEX8(ref | ADLAR_BIT | 0, AdcChain::admuxOf(board, Channel::V1));
183 }
184}
185
186int main(int /* argc */, char ** /* argv */)
187{
188 UNITY_BEGIN();
189
190 RUN_TEST(test_order_of_the_chain);
191 RUN_TEST(test_pins);
192 RUN_TEST(test_reference_per_board);
197 RUN_TEST(test_pipeline_order);
199
200 return UNITY_END();
201}
The six ADC conversions of the 3-phase board, and the ADMUX pipeline of the free-running ADC.
PcbVersion
constexpr Channel channelAfterNext(const Channel ch)
The channel two steps after another one in the chain V1, I1, V2, I2, V3, I3.
Definition adc_chain.h:92
constexpr Channel channelConverted(const uint8_t round)
The channel round round converts, once the pipeline is full.
Definition adc_chain.h:118
constexpr uint8_t admuxOf(const PcbVersion board, const Channel ch)
The ADMUX value that converts a channel: reference, left-adjusted result, analog input.
Definition adc_chain.h:81
constexpr uint8_t refBits(const PcbVersion board)
The reference bits of ADMUX on a board.
Definition adc_chain.h:68
constexpr uint8_t admuxWrittenAfter(const PcbVersion board, const Channel converted)
The ADMUX value the interrupt writes after the conversion of a channel.
Definition adc_chain.h:107
constexpr uint8_t NO_OF_CONVERSIONS
Definition adc_chain.h:44
constexpr uint8_t pinOf(const Channel ch)
The analog input of a channel (A0 = 0).
Definition adc_chain.h:57
void test_reference_per_board(void)
Definition test_main.cpp:72
constexpr uint8_t REF_MASK
Definition test_main.cpp:27
constexpr uint8_t ADLAR_BIT
Definition test_main.cpp:30
void test_admux_of_is_reference_adlar_and_pin(void)
Definition test_main.cpp:80
void test_channel_converted_by_round(void)
void test_channel_table_matches_the_former_literals(void)
void test_after_next_of_every_position(void)
Definition test_main.cpp:93
constexpr uint8_t INTERNAL_1V1
Definition test_main.cpp:29
void setUp(void)
Definition test_main.cpp:18
void test_admux_written_after_selects_the_channel_after_next(void)
void tearDown(void)
Definition test_main.cpp:19
constexpr Channel chain[]
Definition test_main.cpp:24
void test_pins(void)
Definition test_main.cpp:59
constexpr PcbVersion boards[]
Definition test_main.cpp:23
constexpr uint8_t MUX_MASK
Definition test_main.cpp:26
void test_pipeline_order(void)
constexpr uint8_t AVCC
Definition test_main.cpp:28
void test_order_of_the_chain(void)
Definition test_main.cpp:47
static Channel channelOfAdmux(const uint8_t admux)
The channel an ADMUX value selects, from its analog input.
Definition test_main.cpp:33
int main(void)