Skip to content

Commit 23e84e7

Browse files
committed
Merge branch 'develop'
2 parents 75e6e5f + d0fdf81 commit 23e84e7

File tree

7 files changed

+99
-7
lines changed

7 files changed

+99
-7
lines changed

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"m5stack/M5Utility": "*",
1515
"m5stack/M5HAL": "*"
1616
},
17-
"version": "0.1.4",
17+
"version": "0.1.5",
1818
"frameworks": [
1919
"arduino"
2020
],

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=M5UnitUnified
2-
version=0.1.4
2+
version=0.1.5
33
author=M5Stack
44
maintainer=M5Stack
55
sentence=M5UnitUnified is a library for unified handling of various M5 units products. (Alpha version)

src/googletest/test_template.hpp

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

src/m5_unit_component/adapter_gpio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::read_analog(uint16_t& value,
462462
}
463463

464464
m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::pulse_in(uint32_t& duration, const gpio_num_t pin, const int state,
465-
const uint32_t timeout_us = 30000)
465+
const uint32_t timeout_us)
466466
{
467467
duration = 0;
468468
auto start = esp_timer_get_time();

src/m5_unit_component/adapter_gpio.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class AdapterGPIOBase : public Adapter {
8787
}
8888

8989
inline virtual m5::hal::error::error_t pulseInRX(uint32_t& duration, const int state,
90-
const uint32_t timeout_us) override
90+
const uint32_t timeout_us = 30000) override
9191
{
9292
return pulse_in(duration, rx_pin(), state, timeout_us);
9393
}
@@ -114,7 +114,7 @@ class AdapterGPIOBase : public Adapter {
114114
return read_analog(v, tx_pin());
115115
}
116116
inline virtual m5::hal::error::error_t pulseInTX(uint32_t& duration, const int state,
117-
const uint32_t timeout_us) override
117+
const uint32_t timeout_us = 30000) override
118118
{
119119
return pulse_in(duration, tx_pin(), state, timeout_us);
120120
}

src/m5_unit_component/adapter_gpio_v1.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ bool declrare_use_rmt_channel(const int ch)
3838
return false;
3939
}
4040

41+
void clear_use_rmt_channel(const int ch)
42+
{
43+
if (ch >= 0 && ch < RMT_CHANNEL_MAX) {
44+
using_rmt_channel_bits &= ~(1U << ch);
45+
}
46+
}
47+
4148
rmt_config_t to_rmt_config_tx(const adapter_config_t& cfg, const uint32_t apb_freq_hz)
4249
{
4350
rmt_config_t out{};
@@ -78,6 +85,16 @@ class GPIOImplV1 : public AdapterGPIOBase::GPIOImpl {
7885
GPIOImplV1(const int8_t rx_pin, const int8_t tx_pin) : AdapterGPIOBase::GPIOImpl(rx_pin, tx_pin)
7986
{
8087
}
88+
virtual ~GPIOImplV1()
89+
{
90+
rmt_tx_stop(_tx_config.channel);
91+
rmt_driver_uninstall(_tx_config.channel);
92+
clear_use_rmt_channel(_tx_config.channel);
93+
94+
rmt_rx_stop(_rx_config.channel);
95+
rmt_driver_uninstall(_rx_config.channel);
96+
clear_use_rmt_channel(_rx_config.channel);
97+
}
8198

8299
virtual bool begin(const gpio::adapter_config_t& cfg) override
83100
{
@@ -137,8 +154,8 @@ class GPIOImplV1 : public AdapterGPIOBase::GPIOImpl {
137154
}
138155
return err == ESP_OK ? m5::hal::error::error_t::OK : m5::hal::error::error_t::UNKNOWN_ERROR;
139156
}
140-
M5_LIB_LOGE("Failed invalid config");
141157

158+
M5_LIB_LOGE("Failed invalid config");
142159
return m5::hal::error::error_t::UNKNOWN_ERROR;
143160
}
144161

src/m5_unit_component/adapter_gpio_v2.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ class GPIOImplV2 : public AdapterGPIO::GPIOImpl {
4848
GPIOImplV2(const int8_t rx_pin, const int8_t tx_pin) : AdapterGPIOBase::GPIOImpl(rx_pin, tx_pin)
4949
{
5050
}
51+
virtual ~GPIOImplV2()
52+
{
53+
if (_tx_handle) {
54+
rmt_disable(_tx_handle);
55+
rmt_del_channel(_tx_handle);
56+
}
57+
if (_rx_handle) {
58+
rmt_disable(_rx_handle);
59+
rmt_del_channel(_rx_handle);
60+
}
61+
}
5162

5263
bool begin(const gpio::adapter_config_t& cfg)
5364
{
@@ -88,6 +99,9 @@ class GPIOImplV2 : public AdapterGPIO::GPIOImpl {
8899
rmt_transmit_config_t tx_config = {};
89100
auto err = rmt_transmit(_tx_handle, copy_encoder, (gpio::m5_rmt_item_t*)data, len * sizeof(gpio::m5_rmt_item_t),
90101
&tx_config);
102+
if (err != ESP_OK) {
103+
M5_LIB_LOGE("Failed to transmit %d:%s", err, esp_err_to_name(err));
104+
}
91105
if (err == ESP_OK && waitMs) {
92106
err = rmt_tx_wait_all_done(_tx_handle, waitMs);
93107
if (err != ESP_OK) {

0 commit comments

Comments
 (0)