2022-12-08 05:21:01 +01:00
|
|
|
/*
|
|
|
|
* © 2021, Peter Cole. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The IO_EX-IOExpander.h device driver integrates with one or more EX-IOExpander devices.
|
|
|
|
* This device driver will configure the device and all I/O ports on startup, along with
|
|
|
|
* interacting with the device for all input/output duties.
|
2022-12-09 23:32:15 +01:00
|
|
|
*
|
|
|
|
* To create EX-IOExpander devices, these are defined in myHal.cpp:
|
|
|
|
*
|
|
|
|
* #include "IO_EX-IOExpander.h"
|
|
|
|
*
|
|
|
|
* void halSetup() {
|
2022-12-10 10:14:32 +01:00
|
|
|
* // EXIOExpander::create(vpin, num_vpins, i2c_address);
|
|
|
|
* EXIOExpander::create(800, 18, 0x90);
|
2022-12-09 23:32:15 +01:00
|
|
|
}
|
2022-12-08 05:21:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef IO_EX_IOEXPANDER_H
|
|
|
|
#define IO_EX_IOEXPANDER_H
|
|
|
|
|
|
|
|
#include "IO_GPIOBase.h"
|
|
|
|
#include "FSH.h"
|
2022-12-11 01:22:48 +01:00
|
|
|
#include "EX-IOExpanderPins.h"
|
2022-12-08 05:21:01 +01:00
|
|
|
|
2022-12-09 23:23:46 +01:00
|
|
|
// Include user defined pin maps in myEX-IOExpander if defined
|
|
|
|
#if __has_include ("myEX-IOExpander.h")
|
|
|
|
#include "myEX-IOExpander.h"
|
2022-12-08 05:21:01 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/*
|
|
|
|
* IODevice subclass for EX-IOExpander.
|
|
|
|
*/
|
|
|
|
class EXIOExpander : public IODevice {
|
|
|
|
public:
|
2022-12-11 01:22:48 +01:00
|
|
|
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);
|
2022-12-08 05:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Constructor
|
2022-12-11 01:22:48 +01:00
|
|
|
EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress, uint8_t numDigitalPins, uint8_t numAnaloguePins) {
|
2022-12-08 05:21:01 +01:00
|
|
|
_firstVpin = firstVpin;
|
|
|
|
_nPins = nPins;
|
|
|
|
_i2cAddress = i2cAddress;
|
2022-12-11 01:22:48 +01:00
|
|
|
_numDigitalPins = numDigitalPins;
|
|
|
|
_numAnaloguePins = numAnaloguePins;
|
2022-12-08 05:21:01 +01:00
|
|
|
addDevice(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _begin() {
|
|
|
|
// Initialise EX-IOExander device
|
|
|
|
if (I2CManager.exists(_i2cAddress)) {
|
|
|
|
#ifdef DIAG_IO
|
|
|
|
_display();
|
|
|
|
#endif
|
2022-12-11 01:22:48 +01:00
|
|
|
_setupDevice();
|
2022-12-08 05:21:01 +01:00
|
|
|
} else {
|
|
|
|
DIAG(F("EX-IOExpander device not found, I2C:%x"), _i2cAddress);
|
|
|
|
_deviceState = DEVSTATE_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 01:22:48 +01:00
|
|
|
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];
|
2022-12-11 10:44:42 +01:00
|
|
|
for (uint8_t byte = 0; byte < _digitalPinBytes; byte++) {
|
|
|
|
enableDigitalPins[byte] = 0;
|
|
|
|
}
|
2022-12-11 01:22:48 +01:00
|
|
|
for (uint8_t pin = 0; pin < _numDigitalPins; pin++) {
|
2022-12-11 10:44:42 +01:00
|
|
|
int pinByte = pin / 8;
|
|
|
|
bitSet(enableDigitalPins[pinByte], pin - pinByte * 8);
|
2022-12-11 01:22:48 +01:00
|
|
|
}
|
2022-12-11 10:44:42 +01:00
|
|
|
I2CManager.write(_i2cAddress, _digitalPinBytes + 1, REG_EXIODPIN, *enableDigitalPins);
|
2022-12-11 01:22:48 +01:00
|
|
|
// Enable analogue ports
|
|
|
|
_analoguePinBytes = (_numAnaloguePins + 7) / 8;
|
|
|
|
uint8_t enableAnaloguePins[_analoguePinBytes];
|
2022-12-11 10:44:42 +01:00
|
|
|
for (uint8_t byte = 0; byte < _analoguePinBytes; byte++) {
|
|
|
|
enableAnaloguePins[byte] = 0;
|
|
|
|
}
|
2022-12-11 01:22:48 +01:00
|
|
|
for (uint8_t pin = 0; pin < _numAnaloguePins; pin++) {
|
2022-12-11 10:44:42 +01:00
|
|
|
int pinByte = pin / 8;
|
|
|
|
bitSet(enableAnaloguePins[pinByte], pin - pinByte * 8);
|
2022-12-11 01:22:48 +01:00
|
|
|
}
|
2022-12-11 10:44:42 +01:00
|
|
|
I2CManager.write(_i2cAddress, _analoguePinBytes + 1, REG_EXIOAPIN, *enableAnaloguePins);
|
2022-12-11 01:22:48 +01:00
|
|
|
}
|
|
|
|
|
2022-12-08 05:21:01 +01:00
|
|
|
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;
|
2022-12-11 01:22:48 +01:00
|
|
|
uint8_t _numDigitalPins;
|
|
|
|
uint8_t _numAnaloguePins;
|
|
|
|
int _digitalPinBytes;
|
|
|
|
int _analoguePinBytes;
|
2022-12-09 05:41:48 +01:00
|
|
|
|
|
|
|
enum {
|
2022-12-11 10:44:42 +01:00
|
|
|
REG_EXIOINIT = 0xE0, // Flag to initialise setup procedure
|
|
|
|
REG_EXIODPIN = 0xE1, // Flag we're sending digital pin assignments
|
|
|
|
REG_EXIOAPIN = 0xE2, // Flag we're sending analogue pin assignments
|
|
|
|
REG_EXIORDY = 0xE3, // Flag we have completed setup procedure, also for EX-IO to ACK setup
|
|
|
|
REG_EXIODDIR = 0xE4, // Flag we're sending digital pin direction configuration
|
|
|
|
REG_EXIODPUP = 0xE5, // Flag we're sending digital pin pullup configuration
|
2022-12-09 05:41:48 +01:00
|
|
|
};
|
2022-12-08 05:21:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|