@@ -55,7 +55,7 @@ class GlobalFixture : public ::testing::Environment {
5555
5656/* !
5757 @class ComponentTestBase
58- @brief UnitComponent Derived class for testing
58+ @brief UnitComponent Derived class for testing (I2C)
5959 @tparam U m5::unit::Component-derived classes to be tested
6060 @tparam TP parameter type for testing. see also INSTANTIATE_TEST_SUITE_P
6161 */
@@ -109,6 +109,67 @@ class ComponentTestBase : public ::testing::TestWithParam<TP> {
109109 m5::unit::UnitUnified Units;
110110};
111111
112+ /* !
113+ @class GPIOComponentTestBase
114+ @brief UnitComponent Derived class for testing (GPIO)
115+ @tparam U m5::unit::Component-derived classes to be tested
116+ @tparam TP parameter type for testing. see also INSTANTIATE_TEST_SUITE_P
117+ */
118+ template <typename U, typename TP>
119+ class GPIOComponentTestBase : public ::testing::TestWithParam<TP> {
120+ static_assert (std::is_base_of<m5::unit::Component, U>::value, " U must be derived from Component" );
121+
122+ protected:
123+ virtual void SetUp () override
124+ {
125+ unit.reset (get_instance ());
126+ if (!unit) {
127+ FAIL () << " Failed to get_instance" ;
128+ GTEST_SKIP ();
129+ return ;
130+ }
131+ ustr = m5::utility::formatString (" %s:%s" , unit->deviceName (), is_using_hal () ? " HAL" : " GPIO" );
132+ if (!begin ()) {
133+ FAIL () << " Failed to begin " << ustr;
134+ GTEST_SKIP ();
135+ }
136+ }
137+
138+ virtual void TearDown () override
139+ {
140+ }
141+
142+ virtual bool begin ()
143+ {
144+ auto pin_num_gpio_in = M5.getPin (m5::pin_name_t ::port_b_in);
145+ auto pin_num_gpio_out = M5.getPin (m5::pin_name_t ::port_b_out);
146+ if (pin_num_gpio_in < 0 || pin_num_gpio_out < 0 ) {
147+ M5_LOGW (" PortB is not available" );
148+ Wire.end ();
149+ pin_num_gpio_in = M5.getPin (m5::pin_name_t ::port_a_pin1);
150+ pin_num_gpio_out = M5.getPin (m5::pin_name_t ::port_a_pin2);
151+ }
152+ M5_LOGI (" getPin: %d,%d" , pin_num_gpio_in, pin_num_gpio_out);
153+
154+ if (is_using_hal ()) {
155+ // Using M5HAL
156+ // TODO Not yet
157+ return false ;
158+ }
159+ // Using TwoWire
160+ return Units.add (*unit, pin_num_gpio_in, pin_num_gpio_out) && Units.begin ();
161+ }
162+
163+ // !@brief Function returning true if M5HAL is used (decision based on TP)
164+ virtual bool is_using_hal () const = 0;
165+ // ! @brief return m5::unit::Component-derived class instance (decision based on TP)
166+ virtual U* get_instance () = 0;
167+
168+ std::string ustr{};
169+ std::unique_ptr<U> unit{};
170+ m5::unit::UnitUnified Units;
171+ };
172+
112173} // namespace googletest
113174} // namespace unit
114175} // namespace m5
0 commit comments