diff --git a/EX-IOExpanderPins.h b/EX-IOExpanderPins.h new file mode 100644 index 0000000..fa32873 --- /dev/null +++ b/EX-IOExpanderPins.h @@ -0,0 +1,46 @@ +/* + * © 2022 Peter Cole + * + * This file is part of EX-CommandStation + * + * This is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * It is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CommandStation. If not, see . + */ + +/* +* This file defines default pin maps for the various architectures supported +* by default by EX-IOExpander. +* +* Custom user defined pinmaps should be defined in myEX-IOExpander.h. +* +* Any modifications to this file will be overwritten by future software updates. +*/ + +// #define DEFAULT_NANO_DIGITAL_PINMAP 2,3,4,5,6,7,8,9,10,11,12,13 +// #define DEFAULT_NANO_ANALOGUE_PINMAP A0,A1,A2,A3,A6,A7 + +#define EXIO_UNO_DIGITAL_PINS 12 +#define EXIO_UNO_ANALOGUE_PINS 4 + +#define EXIO_NANO_DIGITAL_PINS 12 +#define EXIO_NANO_ANALOGUE_PINS 6 + +#define EXIO_MEGA_DIGITAL_PINS 46 +#define EXIO_MEGA_ANALOGUE_PINS 16 + +// #define EXIO_UNO_DIGITAL_PINMAP 2,3,4,5,6,7,8,9,10,11,12,13 +// #define EXIO_UNO_ANALOGUE_PINMAP A0,A1,A2,A3 + +// #define DEFAULT_MEGA_DIGITAL_PINMAP 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,22,23,24,25,26,27,28,29, \ +// 30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49 +// #define DEFAULT_MEGA_ANALOGUE_PINMAP A0,A1,A2,A3,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15 diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h index 0521ac7..efd09cd 100644 --- a/IO_EXIOExpander.h +++ b/IO_EXIOExpander.h @@ -37,7 +37,7 @@ #include "IO_GPIOBase.h" #include "FSH.h" -#include "EX-IOExpanderPinMaps.h" +#include "EX-IOExpanderPins.h" // Include user defined pin maps in myEX-IOExpander if defined #if __has_include ("myEX-IOExpander.h") @@ -50,16 +50,18 @@ */ class EXIOExpander : public IODevice { public: - static void create(VPIN vpin, int nPins, uint8_t i2cAddress) { - if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress); + static void create(VPIN vpin, int nPins, uint8_t i2cAddress, uint8_t numDigitalPins, uint8_t numAnaloguePins) { + if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress, numDigitalPins, numAnaloguePins); } private: // Constructor - EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress) { + EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress, uint8_t numDigitalPins, uint8_t numAnaloguePins) { _firstVpin = firstVpin; _nPins = nPins; _i2cAddress = i2cAddress; + _numDigitalPins = numDigitalPins; + _numAnaloguePins = numAnaloguePins; addDevice(this); } @@ -69,18 +71,44 @@ private: #ifdef DIAG_IO _display(); #endif + _setupDevice(); } else { DIAG(F("EX-IOExpander device not found, I2C:%x"), _i2cAddress); _deviceState = DEVSTATE_FAILED; } } + void _setupDevice() { + // Send digital and analogue pin counts + I2CManager.write(_i2cAddress, 3, REG_EXIOINIT, _numDigitalPins, _numAnaloguePins); + // Enable digital ports + _digitalPinBytes = (_numDigitalPins + 7) / 8; + uint8_t enableDigitalPins[_digitalPinBytes]; + for (uint8_t pin = 0; pin < _numDigitalPins; pin++) { + int pinByte = ((pin + 7) / 8); + bitSet(enableDigitalPins[pinByte], (pin - (pinByte * 8))); + } + I2CManager.write(_i2cAddress, _digitalPinBytes + 1, REG_EXIODPIN, enableDigitalPins); + // Enable analogue ports + _analoguePinBytes = (_numAnaloguePins + 7) / 8; + uint8_t enableAnaloguePins[_analoguePinBytes]; + for (uint8_t pin = 0; pin < _numAnaloguePins; pin++) { + int pinByte = ((pin + 7) / 8); + bitSet(enableAnaloguePins[pinByte], (pin - (pinByte * 8))); + } + I2CManager.write(_i2cAddress, _analoguePinBytes + 1, REG_EXIOAPIN, enableAnaloguePins); + } + void _display() override { DIAG(F("EX-IOExpander I2C:x%x Configured on Vpins:%d-%d %S"), _i2cAddress, _firstVpin, _firstVpin+_nPins-1, _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F("")); } uint8_t _i2cAddress; + uint8_t _numDigitalPins; + uint8_t _numAnaloguePins; + int _digitalPinBytes; + int _analoguePinBytes; enum { REG_EXIOINIT = 0x00, // Flag to initialise setup procedure