2 * This file is part of Betaflight.
4 * Betaflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Betaflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Betaflight. If not, see <http://www.gnu.org/licenses/>.
20 #include <drivers/timer.h>
28 #include "gtest/gtest.h"
30 #if !defined(USE_UNIFIED_TARGET)
32 extern const timerHardware_t timerHardware
[USABLE_TIMER_CHANNEL_COUNT
];
35 TEST(TimerDefinitionTest
, Test_counterMismatch
) {
36 for (const timerHardware_t
&t
: timerHardware
)
37 EXPECT_EQ(&t
- timerHardware
, t
.def_tim_counter
)
38 << "Counter mismatch in timerHardware (in target.c) at position "
39 << &t
- timerHardware
<< "; the array may have uninitialized "
40 << "trailing elements. This happens when USABLE_TIMER_CHANNEL_COUNT"
41 << " is not equal to the number of array initializers. Current "
42 << "value is " << USABLE_TIMER_CHANNEL_COUNT
<< ", last initialized"
43 << " array element appears to be " << &t
- timerHardware
- 1 << '.';
46 TEST(TimerDefinitionTest
, Test_duplicatePin
) {
47 std::set
<TestPinEnum
> usedPins
;
48 for (const timerHardware_t
&t
: timerHardware
)
49 EXPECT_TRUE(usedPins
.emplace(t
.pin
).second
)
50 << "Pin " << TEST_PIN_NAMES
[t
.pin
] << " is used more than once. "
51 << "This is a problem with the timerHardware array (in target.c). "
52 << "Check the array for typos. Then check the size of the array "
53 << "initializers; it must be USABLE_TIMER_CHANNEL_COUNT.";
54 EXPECT_EQ(USABLE_TIMER_CHANNEL_COUNT
, usedPins
.size());
57 #if !defined(USE_TIMER_MGMT)
59 std::string
writeUsedTimers(const std::bitset
<TEST_TIMER_SIZE
> &tset
) {
60 std::stringstream used_timers
;
62 unsigned int timer
{0};
63 for (; timer
< TEST_TIMER_SIZE
; ++timer
)
65 used_timers
<< "( TIM_N(" << timer
<< ')';
68 for (++timer
; timer
< TEST_TIMER_SIZE
; ++timer
)
69 if (tset
[timer
]) used_timers
<< " | TIM_N(" << timer
<< ')';
74 return used_timers
.str();
78 TEST(TimerDefinitionTest
, Test_usedTimers
)
80 std::bitset
<TEST_TIMER_SIZE
> expected
;
81 for (const timerHardware_t
&t
: timerHardware
)
82 expected
|= TIM_N(t
.timer
);
83 const std::bitset
<TEST_TIMER_SIZE
> actual
{USED_TIMERS
};
84 EXPECT_EQ(expected
, actual
)
85 << "Used timers mismatch. target.c says " << expected
<< ", but "
86 << "target.h says " << actual
<< ". This has two possible causes: "
87 << "(1) The USED_TIMERS bitmap (in target.h) is outdated and out of "
88 << "sync with timerHardware (in target.c). (2) There is an "
89 << "inconsistency between USABLE_TIMER_CHANNEL_COUNT and the length "
90 << "of timerHardware's initializer list.";
92 << "USED_TIMERS definition based on timerHardware:" << std::endl
93 << writeUsedTimers(expected
) << std::endl
;
100 void spiPinConfigure(int) {}
101 int spiPinConfig(int) { return 0; }
104 int i2cConfig(int) { return 0; }
105 void i2cHardwareConfigure(int) {}
110 #endif // USE_UNIFIED_TARGET