66/* !
77 @file m5_unit_unified_wiring.hpp
88 @brief Opt-in, header-only board-aware connection helpers for M5UnitUnified examples
9+
10+ @details
11+ This header exists solely to keep the example code in M5UnitUnified and M5Unit-* repositories
12+ short and readable. Each helper resolves board-specific pins / backends through M5Unified,
13+ opens the bus, and calls UnitUnified::add().
14+
15+ Users SHOULD treat these helpers as reference implementations, not as the only supported way
16+ to wire a unit. In real applications, prefer to call UnitUnified::add() directly with a bus
17+ or handle that fits the application's lifecycle (shared bus management, custom pins,
18+ alternative backends, etc.).
19+
920 @note REQUIRES <M5Unified.h> (or <M5Unified.hpp>) to be included BEFORE this header.
1021 Pin-acquisition helpers (i2cPins / gpioPins / uartPins / spiPins / hatI2CPins / hatGPIOPins /
1122 hatUARTPins) work on both Arduino and ESP-IDF (any platform where M5Unified is available).
@@ -332,14 +343,18 @@ inline HardwareSerial& defaultUartSerial()
332343}
333344
334345/* !
335- @brief Add a unit on UART, preferring PortC and falling back to PortA
336- @note On NanoC6 / NanoH2 fallback, M5.Ex_I2C.begin() + release() is called to flip the m5gfx
346+ @brief Add a unit on the board's default UART, preferring PortC and falling back to PortA
347+ @param baud Baud rate (default 115200)
348+ @param config UART config (0 = SERIAL_8N1, otherwise passed through)
349+ @note Selects HardwareSerial via defaultUartSerial() based on board.
350+ On NanoC6 / NanoH2 fallback, M5.Ex_I2C.begin() + release() is called to flip the m5gfx
337351 I2C `initialized` flag so release() actually detaches the bus + restores pins.
338352*/
339- inline bool addUART (UnitUnified& units, Component& unit, HardwareSerial& serial, const uint32_t baud = 115200 ,
340- const uint32_t config = SERIAL_8N1 )
353+ inline bool addUART (UnitUnified& units, Component& unit, const uint32_t baud = 115200 , const uint32_t config = 0 )
341354{
342- const auto p = uartPins ();
355+ HardwareSerial& serial = defaultUartSerial ();
356+ const uint32_t cfg = (config == 0 ) ? static_cast <uint32_t >(SERIAL_8N1 ) : config;
357+ const auto p = uartPins ();
343358 if (p.fallback_a ) {
344359 const auto b = M5 .getBoard ();
345360 if (b == m5::board_t ::board_M5NanoC6 || b == m5::board_t ::board_M5NanoH2) {
@@ -350,10 +365,10 @@ inline bool addUART(UnitUnified& units, Component& unit, HardwareSerial& serial,
350365 M5 .Ex_I2C .release ();
351366 }
352367 }
353- M5_LIB_LOGI (" wiring: UART rx=%d tx=%d baud=%lu fallback_a=%d" , (int )p.rx , (int )p.tx , ( unsigned long )baud ,
354- (int )p.fallback_a );
368+ M5_LIB_LOGI (" wiring: UART rx=%d tx=%d baud=%lu config=0x%lx fallback_a=%d" , (int )p.rx , (int )p.tx ,
369+ (unsigned long )baud, ( unsigned long )cfg, ( int )p.fallback_a );
355370 serial.end ();
356- serial.begin (baud, config , p.rx , p.tx );
371+ serial.begin (baud, cfg , p.rx , p.tx );
357372 return units.add (unit, serial);
358373}
359374
@@ -366,12 +381,18 @@ inline bool spiBus(UnitUnified& units, Component& unit, SPIClass& spi, const SPI
366381
367382/* !
368383 @brief Add a unit on the board's shared SD/SPI bus, beginning it on demand
384+ @param clock_hz SPI clock in Hz
385+ @param mode SPI mode 0/1/2/3 (default 0)
386+ @param bit_order Bit order: 0 = MSBFIRST (default), 1 = LSBFIRST
369387 @note Resolves the sd_spi_* pins. SPI.begin() is called only if the bus is not already begun.
370388*/
371- inline bool addSPI (UnitUnified& units, Component& unit, const SPISettings& settings)
389+ inline bool addSPI (UnitUnified& units, Component& unit, const uint32_t clock_hz, const uint8_t mode = 0 ,
390+ const uint8_t bit_order = 0 )
372391{
392+ SPISettings settings{clock_hz, static_cast <uint8_t >((bit_order == 0 ) ? MSBFIRST : LSBFIRST ), mode};
373393 const auto p = spiPins ();
374- M5_LIB_LOGI (" wiring: addSPI sclk=%d miso=%d mosi=%d" , (int )p.sclk , (int )p.miso , (int )p.mosi );
394+ M5_LIB_LOGI (" wiring: addSPI sclk=%d miso=%d mosi=%d clock=%lu mode=%u bit_order=%u" , (int )p.sclk , (int )p.miso ,
395+ (int )p.mosi , (unsigned long )clock_hz, (unsigned )mode, (unsigned )bit_order);
375396 if (!SPI .bus ()) {
376397 SPI .begin (p.sclk , p.miso , p.mosi );
377398 }
@@ -427,19 +448,23 @@ inline bool addHatGPIO(UnitUnified& units, Component& unit, const GpioRole role
427448
428449/* !
429450 @brief Add a unit on the board's Hat UART header
451+ @param baud Baud rate (default 115200)
452+ @param config UART config (0 = SERIAL_8N1, otherwise passed through)
453+ @note Selects HardwareSerial via defaultUartSerial() based on board.
430454*/
431- inline bool addHatUART (UnitUnified& units, Component& unit, HardwareSerial& serial, const uint32_t baud = 115200 ,
432- const uint32_t config = SERIAL_8N1 )
455+ inline bool addHatUART (UnitUnified& units, Component& unit, const uint32_t baud = 115200 , const uint32_t config = 0 )
433456{
434- const auto p = hatUARTPins ();
457+ HardwareSerial& serial = defaultUartSerial ();
458+ const uint32_t cfg = (config == 0 ) ? static_cast <uint32_t >(SERIAL_8N1 ) : config;
459+ const auto p = hatUARTPins ();
435460 if (p.rx < 0 || p.tx < 0 ) {
436461 M5_LIB_LOGE (" wiring: Hat UART unsupported board=0x%02x" , (int )M5 .getBoard ());
437462 return false ;
438463 }
439- M5_LIB_LOGI (" wiring: addHatUART board=0x%02x rx=%d tx=%d baud=%lu" , (int )M5 .getBoard (), (int )p.rx , ( int )p. tx ,
440- (unsigned long )baud);
464+ M5_LIB_LOGI (" wiring: addHatUART board=0x%02x rx=%d tx=%d baud=%lu config=0x%lx " , (int )M5 .getBoard (), (int )p.rx ,
465+ (int )p. tx , ( unsigned long )baud, ( unsigned long )cfg );
441466 serial.end ();
442- serial.begin (baud, config , p.rx , p.tx );
467+ serial.begin (baud, cfg , p.rx , p.tx );
443468 return units.add (unit, serial);
444469}
445470#endif // ARDUINO
0 commit comments