|
15 | 15 | #include <SPI.h> |
16 | 16 | #include <M5Unified.h> |
17 | 17 | #include <M5HAL.hpp> |
18 | | -#include <esp32-hal-i2c.h> |
| 18 | +#include <wiring/m5_unit_unified_wiring.hpp> // include last; uses M5Unified/M5HAL detected above |
19 | 19 |
|
20 | 20 | using namespace m5::unit; |
21 | 21 |
|
22 | 22 | constexpr uint32_t I2C_FREQ{400000U}; |
23 | 23 |
|
24 | | -// Board-aware I2C add: same 3-branch logic as I2CComponentTestBase |
| 24 | +// Board-aware connection helpers delegate to the m5::unit::wiring helper. Dogfooding it here also |
| 25 | +// compile-checks the header and exercises it on real hardware. |
25 | 26 | static bool add_with_i2c(UnitUnified& units, Component& u) |
26 | 27 | { |
27 | | - auto board = M5.getBoard(); |
28 | | - if (board == m5::board_t::board_ArduinoNessoN1) { |
29 | | - auto sda = M5.getPin(m5::pin_name_t::port_b_out); |
30 | | - auto scl = M5.getPin(m5::pin_name_t::port_b_in); |
31 | | - m5::hal::bus::I2CBusConfig i2c_cfg; |
32 | | - i2c_cfg.pin_sda = m5::hal::gpio::getPin(sda); |
33 | | - i2c_cfg.pin_scl = m5::hal::gpio::getPin(scl); |
34 | | - auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg); |
35 | | - return units.add(u, i2c_bus ? i2c_bus.value() : nullptr); |
36 | | - } |
37 | | - if (board == m5::board_t::board_M5NanoC6) { |
38 | | - return units.add(u, M5.Ex_I2C); |
39 | | - } |
40 | | - // Standard boards: Wire |
41 | | - auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); |
42 | | - auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); |
43 | | - if (i2cIsInit(0)) { |
44 | | - Wire.end(); |
45 | | - } |
46 | | - Wire.begin(pin_num_sda, pin_num_scl, I2C_FREQ); |
47 | | - return units.add(u, Wire); |
| 28 | + return m5::unit::wiring::addI2C(units, u, I2C_FREQ); // default PortB = SoftwareI2C on NessoN1 |
48 | 29 | } |
49 | 30 |
|
50 | | -// GPIO add helper |
51 | 31 | static bool add_with_gpio(UnitUnified& units, Component& u) |
52 | 32 | { |
53 | | - auto rx = M5.getPin(m5::pin_name_t::port_b_in); |
54 | | - auto tx = M5.getPin(m5::pin_name_t::port_b_out); |
55 | | - if (rx < 0 || tx < 0) { |
56 | | - // Fallback to port_a if port_b unavailable |
57 | | - rx = M5.getPin(m5::pin_name_t::port_a_pin1); |
58 | | - tx = M5.getPin(m5::pin_name_t::port_a_pin2); |
59 | | - } |
60 | | - return units.add(u, rx, tx); |
| 33 | + return m5::unit::wiring::addGPIO(units, u); |
61 | 34 | } |
62 | 35 |
|
63 | | -// UART add helper |
64 | 36 | static bool add_with_uart(UnitUnified& units, Component& u) |
65 | 37 | { |
66 | | -#if SOC_UART_NUM > 2 |
67 | | - auto& s = Serial2; |
68 | | -#elif SOC_UART_NUM > 1 |
69 | | - auto& s = Serial1; |
70 | | -#else |
71 | | -#error "Not enough Serial" |
72 | | -#endif |
73 | | - return units.add(u, s); |
| 38 | + return m5::unit::wiring::addUART(units, u, m5::unit::wiring::defaultUartSerial()); |
74 | 39 | } |
75 | 40 |
|
76 | | -// SPI add helper |
77 | 41 | static bool add_with_spi(UnitUnified& units, Component& u) |
78 | 42 | { |
79 | 43 | SPISettings settings{1000000, MSBFIRST, SPI_MODE0}; |
80 | | - return units.add(u, SPI, settings); |
| 44 | + return m5::unit::wiring::spiBus(units, u, SPI, settings); |
81 | 45 | } |
82 | 46 |
|
83 | 47 | // Test: I2C unit add/begin/update lifecycle (success path) |
|
0 commit comments