Skip to content

Commit 531bc2a

Browse files
committed
Add UART(Serial) support
1 parent da0aa89 commit 531bc2a

13 files changed

Lines changed: 280 additions & 5 deletions

src/M5UnitComponent.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ bool Component::canAccessGPIO() const
5454
return attribute() & attribute::AccessGPIO;
5555
}
5656

57+
bool Component::canAccessUART() const
58+
{
59+
return attribute() & attribute::AccessUART;
60+
}
61+
5762
bool Component::add(Component& c, const int16_t ch)
5863
{
5964
if (childrenSize() >= _component_cfg.max_children) {
@@ -141,6 +146,14 @@ bool Component::assign(const int8_t rx_pin, const int8_t tx_pin)
141146
return static_cast<bool>(_adapter);
142147
}
143148

149+
bool Component::assign(HardwareSerial& serial)
150+
{
151+
if (canAccessUART()) {
152+
_adapter = std::make_shared<AdapterUART>(serial);
153+
}
154+
return static_cast<bool>(_adapter);
155+
}
156+
144157
bool Component::selectChannel(const uint8_t ch)
145158
{
146159
bool ret{true};

src/M5UnitComponent.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <memory>
2121

2222
class TwoWire;
23+
class HardwareSerial;
2324

2425
namespace m5 {
2526
namespace unit {
@@ -183,6 +184,7 @@ class Component {
183184
///@{
184185
bool canAccessI2C() const;
185186
bool canAccessGPIO() const;
187+
bool canAccessUART() const;
186188
///@}
187189

188190
///@name Periodic measurement
@@ -223,6 +225,8 @@ class Component {
223225
virtual bool assign(TwoWire& wire);
224226
/*! @brief Assgin GPIO */
225227
virtual bool assign(const int8_t rx_pin, const int8_t tx_pin);
228+
/*! @brief Assgin UART */
229+
virtual bool assign(HardwareSerial& serial);
226230
///@}
227231

228232
///@note For daisy-chaining units such as hubs

src/M5UnitUnified.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ bool UnitUnified::add(Component& u, const int8_t rx_pin, const int8_t tx_pin)
7676
return false;
7777
}
7878

79+
bool UnitUnified::add(Component& u, HardwareSerial& serial)
80+
{
81+
if (u.isRegistered()) {
82+
M5_LIB_LOGW("Already added");
83+
return false;
84+
}
85+
86+
M5_LIB_LOGD("Add [%s] addr:%02x children:%zu", u.deviceName(), u.address(), u.childrenSize());
87+
88+
u._manager = this;
89+
if (u.assign(serial)) {
90+
u._order = ++_registerCount;
91+
_units.emplace_back(&u);
92+
return add_children(u);
93+
}
94+
M5_LIB_LOGE("Failed to assign %s:%u", u.deviceName(), u.canAccessI2C());
95+
return false;
96+
}
97+
7998
// Add children if exists
8099
bool UnitUnified::add_children(Component& u)
81100
{

src/M5UnitUnified.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <string>
2727

2828
class TwoWire;
29+
class HardwareSerial;
2930

3031
/*!
3132
@namespace m5
@@ -80,6 +81,13 @@ class UnitUnified {
8081
@return True if successful
8182
*/
8283
bool add(Component& u, const int8_t rx_pin, const int8_t tx_pin);
84+
/*!
85+
@brief Adding unit to be managed (UART)
86+
@param u Unit Component
87+
@param serial HardwareSerial to be used
88+
@return True if successful
89+
*/
90+
bool add(Component& u, HardwareSerial& wire);
8391
/*!
8492
@brief Adding unit to be managed (M5HAL)
8593
@param u Unit Component

src/googletest/test_helper.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ uint32_t test_periodic_measurement(U* unit, const uint32_t times, const uint32_t
5050
std::this_thread::yield();
5151
}
5252

53+
M5_LOGI("AVG:%u avgCnt:%u", avg, avgCnt);
54+
5355
if (!skip_after_test) {
5456
EXPECT_EQ(cnt, 0U);
5557
EXPECT_EQ(avgCnt, times - 1);

src/googletest/test_template.hpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class GPIOComponentTestBase : public ::testing::TestWithParam<TP> {
156156
// TODO Not yet
157157
return false;
158158
}
159-
// Using TwoWire
159+
// Using GPIO
160160
return Units.add(*unit, pin_num_gpio_in, pin_num_gpio_out) && Units.begin();
161161
}
162162

@@ -170,6 +170,61 @@ class GPIOComponentTestBase : public ::testing::TestWithParam<TP> {
170170
m5::unit::UnitUnified Units;
171171
};
172172

173+
/*!
174+
@class UARTComponentTestBase
175+
@brief UnitComponent Derived class for testing (UART)
176+
@tparam U m5::unit::Component-derived classes to be tested
177+
@tparam TP parameter type for testing. see also INSTANTIATE_TEST_SUITE_P
178+
*/
179+
template <typename U, typename TP>
180+
class UARTComponentTestBase : public ::testing::TestWithParam<TP> {
181+
static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
182+
183+
protected:
184+
virtual void SetUp() override
185+
{
186+
unit.reset(get_instance());
187+
if (!unit) {
188+
FAIL() << "Failed to get_instance";
189+
GTEST_SKIP();
190+
return;
191+
}
192+
ustr = m5::utility::formatString("%s:%s", unit->deviceName(), is_using_hal() ? "HAL" : "UART");
193+
if (!begin()) {
194+
FAIL() << "Failed to begin " << ustr;
195+
GTEST_SKIP();
196+
}
197+
}
198+
199+
virtual void TearDown() override
200+
{
201+
}
202+
203+
virtual bool begin()
204+
{
205+
if (is_using_hal()) {
206+
// Using M5HAL
207+
// TODO Not yet
208+
return false;
209+
}
210+
// Using Serial
211+
serial = init_serial();
212+
return serial && Units.add(*unit, *serial) && Units.begin();
213+
}
214+
215+
//!@brief Function returning true if M5HAL is used (decision based on TP)
216+
virtual bool is_using_hal() const = 0;
217+
//! @brief return m5::unit::Component-derived class instance (decision based on TP)
218+
virtual U* get_instance() = 0;
219+
//!@brief Initialize the serial to be used
220+
virtual HardwareSerial* init_serial() = 0;
221+
222+
std::string ustr{};
223+
std::unique_ptr<U> unit{};
224+
m5::unit::UnitUnified Units;
225+
HardwareSerial* serial{};
226+
};
227+
173228
} // namespace googletest
174229
} // namespace unit
175230
} // namespace m5

src/m5_unit_component/adapter.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
#ifndef M5_UNIT_COMPONENT_ADAPTER_HPP
1212
#define M5_UNIT_COMPONENT_ADAPTER_HPP
13+
1314
#include "adapter_base.hpp"
1415
#include "adapter_i2c.hpp"
1516
#include "identify_functions.hpp"
@@ -18,4 +19,6 @@
1819
#else
1920
#include "adapter_gpio_v1.hpp"
2021
#endif
22+
#include "adapter_uart.hpp"
23+
2124
#endif

src/m5_unit_component/adapter_gpio_v1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
/*!
77
@file adapter_gpio_v1.cpp
8-
@brief Adapters to treat M5HAL and GPIO in the same way using RNT v1
8+
@brief Adapters to treat M5HAL and GPIO in the same way using RMT v1
99
@note Currently handles GPIO directly, but will handle via M5HAL in the future
1010
*/
1111
#include "adapter_gpio_v1.hpp"

src/m5_unit_component/adapter_gpio_v1.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
/*!
77
@file adapter_gpio_v1.hpp
8-
@brief Adapters to treat M5HAL and GPIO in the same way using RNT v1
8+
@brief Adapters to treat M5HAL and GPIO in the same way using RMT v1
99
@note Currently handles GPIO directly, but will handle via M5HAL in the future
1010
*/
1111
#ifndef M5_UNIT_COMPONENT_ADAPTER_GPIO_V1_HPP

src/m5_unit_component/adapter_i2c.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
@brief Adapter for I2C to treat M5HAL and TwoWire in the same way
99
@note Currently handles TwoWire directly, but will handle via M5HAL in the future
1010
*/
11-
#include "adapter.hpp"
12-
#include <cassert>
11+
#include "adapter_i2c.hpp"
12+
#if defined(ARDUINO)
1313
#include <Wire.h>
14+
#endif
1415
#include <M5HAL.hpp>
1516
#include <M5Utility.hpp>
1617
#include <soc/gpio_struct.h>
1718
#include <soc/gpio_sig_map.h>
19+
#include <cassert>
1820

1921
#if defined(ARDUINO)
2022

0 commit comments

Comments
 (0)