Updated and Validated
[betaflight.git] / src / test / unit / timer_definition_unittest.cc
blob64d1e2540d079ab4fb7d5173c180f4c67d4b2030
1 /*
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/>.
18 extern "C" {
19 #include <target.h>
20 #include <drivers/timer.h>
23 #include <bitset>
24 #include <iostream>
25 #include <set>
26 #include <sstream>
27 #include <string>
28 #include "gtest/gtest.h"
30 #if !defined(USE_UNIFIED_TARGET)
31 extern "C" {
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)
58 namespace {
59 std::string writeUsedTimers(const std::bitset<TEST_TIMER_SIZE> &tset) {
60 std::stringstream used_timers;
61 if (tset.any()) {
62 unsigned int timer{0};
63 for (; timer < TEST_TIMER_SIZE; ++timer)
64 if (tset[timer]) {
65 used_timers << "( TIM_N(" << timer << ')';
66 break;
68 for (++timer; timer < TEST_TIMER_SIZE; ++timer)
69 if (tset[timer]) used_timers << " | TIM_N(" << timer << ')';
70 used_timers << " )";
71 } else {
72 used_timers << "(0)";
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.";
91 std::cerr
92 << "USED_TIMERS definition based on timerHardware:" << std::endl
93 << writeUsedTimers(expected) << std::endl;
95 #endif
97 // STUBS
99 extern "C" {
100 void spiPinConfigure(int) {}
101 int spiPinConfig(int) { return 0; }
102 void spiInit(int) {}
104 int i2cConfig(int) { return 0; }
105 void i2cHardwareConfigure(int) {}
106 void i2cInit(int) {}
108 void bstInit(int) {}
110 #endif // USE_UNIFIED_TARGET