From 173676287c2ddaccf93f433a658443f42a763707 Mon Sep 17 00:00:00 2001 From: Neil McKechnie Date: Sat, 18 Feb 2023 09:32:38 +0000 Subject: [PATCH] Allow extended I2C addresses to be specified in non-extended configuration If an extended I2C address is specified (including mux and/or subbus) then these parameters are ignored, but a warning output to the diagnostic console. --- I2CManager.cpp | 8 +++++++- I2CManager.h | 22 +++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/I2CManager.cpp b/I2CManager.cpp index 05feb28..14e84b7 100644 --- a/I2CManager.cpp +++ b/I2CManager.cpp @@ -350,4 +350,10 @@ void I2CAddress::toHex(const uint8_t value, char *buffer) { *ptr++ = bits > 9 ? bits-10+'a' : bits+'0'; bits = value & 0xf; *ptr++ = bits > 9 ? bits-10+'a' : bits+'0'; -} \ No newline at end of file +} + +#if !defined(I2C_EXTENDED_ADDRESS) + +/* static */ bool I2CAddress::_addressWarningDone = false; + +#endif \ No newline at end of file diff --git a/I2CManager.h b/I2CManager.h index 542c92f..016874a 100644 --- a/I2CManager.h +++ b/I2CManager.h @@ -24,6 +24,7 @@ #include #include "FSH.h" #include "defines.h" +#include "DIAG.h" /* * Manager for I2C communications. For portability, it allows use @@ -144,9 +145,6 @@ //#define I2C_EXTENDED_ADDRESS -// Type to hold I2C address -#if defined(I2C_EXTENDED_ADDRESS) - ///////////////////////////////////////////////////////////////////////////////////// // Extended I2C Address type to facilitate extended I2C addresses including // I2C multiplexer support. @@ -189,6 +187,9 @@ enum I2CSubBus : uint8_t { SubBus_All = 255, // Enable all sub-buses }; +// Type to hold I2C address +#if defined(I2C_EXTENDED_ADDRESS) + // First MUX address (they range between 0x70-0x77). #define I2C_MUX_BASE_ADDRESS 0x70 @@ -315,6 +316,14 @@ public: I2CAddress(const uint8_t deviceAddress) { _deviceAddress = deviceAddress; } + I2CAddress(I2CMux, I2CSubBus, const uint8_t deviceAddress) { + addressWarning(); + _deviceAddress = deviceAddress; + } + I2CAddress(I2CSubBus, const uint8_t deviceAddress) { + addressWarning(); + _deviceAddress = deviceAddress; + } // Basic constructor I2CAddress() : I2CAddress(0) {} @@ -344,6 +353,13 @@ public: private: // Helper function for converting byte to four-character hex string (e.g. 0x23). void toHex(const uint8_t value, char *buffer); + void addressWarning() { + if (!_addressWarningDone) { + DIAG(F("WARNIING: Extended I2C address used but not supported in this configuration")); + _addressWarningDone = true; + } + } + static bool _addressWarningDone; }; #endif // I2C_EXTENDED_ADDRESS