2022-12-08 05:21:01 +01:00
|
|
|
/*
|
2023-01-29 10:26:33 +01:00
|
|
|
* © 2022, Peter Cole. All rights reserved.
|
2022-12-08 05:21:01 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2022-12-28 20:10:37 +01:00
|
|
|
* The IO_EXIOExpander.h device driver integrates with one or more EX-IOExpander devices.
|
|
|
|
* This device driver will configure the device on startup, along with
|
2022-12-08 05:21:01 +01:00
|
|
|
* 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:
|
2022-12-28 23:44:08 +01:00
|
|
|
* (Note the device driver is included by default)
|
2022-12-09 23:32:15 +01:00
|
|
|
*
|
|
|
|
* void halSetup() {
|
2023-01-29 10:26:33 +01:00
|
|
|
* // EXIOExpander::create(vpin, num_vpins, i2c_address);
|
|
|
|
* EXIOExpander::create(800, 18, 0x65);
|
2022-12-15 06:10:53 +01:00
|
|
|
* }
|
|
|
|
*
|
2023-01-29 10:26:33 +01:00
|
|
|
* All pins on an EX-IOExpander device are allocated according to the pin map for the specific
|
|
|
|
* device in use. There is no way for the device driver to sanity check pins are used for the
|
|
|
|
* correct purpose, however the EX-IOExpander device's pin map will prevent pins being used
|
|
|
|
* incorrectly (eg. A6/7 on Nano cannot be used for digital input/output).
|
2022-12-08 05:21:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef IO_EX_IOEXPANDER_H
|
|
|
|
#define IO_EX_IOEXPANDER_H
|
|
|
|
|
2022-12-12 10:54:20 +01:00
|
|
|
#include "I2CManager.h"
|
|
|
|
#include "DIAG.h"
|
2022-12-08 05:21:01 +01:00
|
|
|
#include "FSH.h"
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/*
|
|
|
|
* IODevice subclass for EX-IOExpander.
|
|
|
|
*/
|
|
|
|
class EXIOExpander : public IODevice {
|
|
|
|
public:
|
2023-01-29 01:06:01 +01:00
|
|
|
static void create(VPIN vpin, int nPins, uint8_t i2cAddress) {
|
|
|
|
if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress);
|
2022-12-08 05:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Constructor
|
2023-01-29 01:06:01 +01:00
|
|
|
EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress) {
|
2022-12-08 05:21:01 +01:00
|
|
|
_firstVpin = firstVpin;
|
|
|
|
_nPins = nPins;
|
|
|
|
_i2cAddress = i2cAddress;
|
|
|
|
addDevice(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _begin() {
|
|
|
|
// Initialise EX-IOExander device
|
2023-01-09 23:16:42 +01:00
|
|
|
I2CManager.begin();
|
2022-12-08 05:21:01 +01:00
|
|
|
if (I2CManager.exists(_i2cAddress)) {
|
2023-02-01 10:46:08 +01:00
|
|
|
_command4Buffer[0] = EXIOINIT;
|
|
|
|
_command4Buffer[1] = _nPins;
|
|
|
|
_command4Buffer[2] = _firstVpin & 0xFF;
|
|
|
|
_command4Buffer[3] = _firstVpin >> 8;
|
2023-02-01 05:53:46 +01:00
|
|
|
// Send config, if EXIOPINS returned, we're good, setup pin buffers, otherwise go offline
|
2023-02-01 10:46:08 +01:00
|
|
|
I2CManager.read(_i2cAddress, _receive3Buffer, 3, _command4Buffer, 4);
|
2023-02-01 05:53:46 +01:00
|
|
|
if (_receive3Buffer[0] == EXIOPINS) {
|
|
|
|
_numDigitalPins = _receive3Buffer[1];
|
|
|
|
_numAnaloguePins = _receive3Buffer[2];
|
|
|
|
_digitalPinBytes = (_numDigitalPins + 7)/8;
|
|
|
|
_digitalInputStates=(byte*) calloc(_digitalPinBytes,1);
|
2023-01-29 01:06:01 +01:00
|
|
|
_analoguePinBytes = _numAnaloguePins * 2;
|
|
|
|
_analogueInputStates = (byte*) calloc(_analoguePinBytes, 1);
|
|
|
|
_analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1);
|
2023-01-29 01:06:01 +01:00
|
|
|
} else {
|
2022-12-20 10:41:32 +01:00
|
|
|
DIAG(F("ERROR configuring EX-IOExpander device, I2C:x%x"), _i2cAddress);
|
|
|
|
_deviceState = DEVSTATE_FAILED;
|
|
|
|
return;
|
|
|
|
}
|
2023-01-29 01:06:01 +01:00
|
|
|
// We now need to retrieve the analogue pin map
|
|
|
|
_command1Buffer[0] = EXIOINITA;
|
|
|
|
I2CManager.read(_i2cAddress, _analoguePinMap, _numAnaloguePins, _command1Buffer, 1);
|
2022-12-25 21:44:15 +01:00
|
|
|
// Attempt to get version, if we don't get it, we don't care, don't go offline
|
2023-01-29 01:06:01 +01:00
|
|
|
_command1Buffer[0] = EXIOVER;
|
|
|
|
I2CManager.read(_i2cAddress, _versionBuffer, 3, _command1Buffer, 1);
|
2023-01-29 01:06:01 +01:00
|
|
|
_command1Buffer[0] = EXIOVER;
|
|
|
|
I2CManager.read(_i2cAddress, _versionBuffer, 3, _command1Buffer, 1);
|
2022-12-25 22:36:12 +01:00
|
|
|
_majorVer = _versionBuffer[0];
|
|
|
|
_minorVer = _versionBuffer[1];
|
|
|
|
_patchVer = _versionBuffer[2];
|
2023-01-11 22:27:42 +01:00
|
|
|
DIAG(F("EX-IOExpander device found, I2C:x%x, Version v%d.%d.%d"),
|
|
|
|
_i2cAddress, _versionBuffer[0], _versionBuffer[1], _versionBuffer[2]);
|
2022-12-08 05:21:01 +01:00
|
|
|
#ifdef DIAG_IO
|
|
|
|
_display();
|
|
|
|
#endif
|
|
|
|
} else {
|
2022-12-13 22:49:09 +01:00
|
|
|
DIAG(F("EX-IOExpander device not found, I2C:x%x"), _i2cAddress);
|
2022-12-08 05:21:01 +01:00
|
|
|
_deviceState = DEVSTATE_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 01:06:01 +01:00
|
|
|
// Digital input pin configuration, used to enable on EX-IOExpander device and set pullups if in use
|
2022-12-19 05:24:49 +01:00
|
|
|
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override {
|
|
|
|
if (paramCount != 1) return false;
|
|
|
|
int pin = vpin - _firstVpin;
|
2023-01-29 10:26:33 +01:00
|
|
|
if (configType == CONFIGURE_INPUT) {
|
|
|
|
bool pullup = params[0];
|
|
|
|
_digitalOutBuffer[0] = EXIODPUP;
|
|
|
|
_digitalOutBuffer[1] = pin;
|
|
|
|
_digitalOutBuffer[2] = pullup;
|
2023-02-06 22:32:16 +01:00
|
|
|
I2CManager.read(_i2cAddress, _command1Buffer, 1, _digitalOutBuffer, 3);
|
|
|
|
if (_command1Buffer[0] == EXIORDY) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
DIAG(F("Vpin %d cannot be used as a digital input pin"), (int)vpin);
|
|
|
|
return false;
|
|
|
|
}
|
2023-01-29 10:26:33 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-19 05:24:49 +01:00
|
|
|
}
|
|
|
|
|
2023-01-29 01:06:01 +01:00
|
|
|
// Analogue input pin configuration, used to enable on EX-IOExpander device
|
2023-01-11 23:10:41 +01:00
|
|
|
int _configureAnalogIn(VPIN vpin) override {
|
2023-01-23 07:26:07 +01:00
|
|
|
int pin = vpin - _firstVpin;
|
2023-01-29 01:06:01 +01:00
|
|
|
_command2Buffer[0] = EXIOENAN;
|
|
|
|
_command2Buffer[1] = pin;
|
2023-02-06 22:32:16 +01:00
|
|
|
I2CManager.read(_i2cAddress, _command1Buffer, 1, _command2Buffer, 2);
|
|
|
|
if (_command1Buffer[0] == EXIORDY) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
DIAG(F("Vpin %d cannot be used as an analogue input pin"), (int)vpin);
|
|
|
|
return false;
|
|
|
|
}
|
2023-01-23 07:26:07 +01:00
|
|
|
return true;
|
2023-01-11 23:10:41 +01:00
|
|
|
}
|
|
|
|
|
2023-01-29 01:06:01 +01:00
|
|
|
// Main loop, collect both digital and analogue pin states continuously (faster sensor/input reads)
|
2023-01-23 02:49:23 +01:00
|
|
|
void _loop(unsigned long currentMicros) override {
|
2023-01-27 19:42:55 +01:00
|
|
|
(void)currentMicros; // remove warning
|
2023-01-29 01:06:01 +01:00
|
|
|
_command1Buffer[0] = EXIORDD;
|
|
|
|
I2CManager.read(_i2cAddress, _digitalInputStates, _digitalPinBytes, _command1Buffer, 1);
|
|
|
|
_command1Buffer[0] = EXIORDAN;
|
|
|
|
I2CManager.read(_i2cAddress, _analogueInputStates, _analoguePinBytes, _command1Buffer, 1);
|
2023-01-23 02:49:23 +01:00
|
|
|
}
|
|
|
|
|
2023-01-29 01:06:01 +01:00
|
|
|
// Obtain the correct analogue input value
|
2023-01-11 23:10:41 +01:00
|
|
|
int _readAnalogue(VPIN vpin) override {
|
2023-01-29 01:06:01 +01:00
|
|
|
int pin = vpin - _firstVpin;
|
|
|
|
uint8_t _pinLSBByte;
|
|
|
|
for (uint8_t aPin = 0; aPin < _numAnaloguePins; aPin++) {
|
|
|
|
if (_analoguePinMap[aPin] == pin) {
|
|
|
|
_pinLSBByte = aPin * 2;
|
|
|
|
}
|
|
|
|
}
|
2023-01-23 07:26:07 +01:00
|
|
|
uint8_t _pinMSBByte = _pinLSBByte + 1;
|
2023-01-23 11:12:28 +01:00
|
|
|
return (_analogueInputStates[_pinMSBByte] << 8) + _analogueInputStates[_pinLSBByte];
|
2022-12-11 01:22:48 +01:00
|
|
|
}
|
|
|
|
|
2023-01-29 01:06:01 +01:00
|
|
|
// Obtain the correct digital input value
|
2022-12-19 05:24:49 +01:00
|
|
|
int _read(VPIN vpin) override {
|
|
|
|
int pin = vpin - _firstVpin;
|
2023-01-23 02:49:23 +01:00
|
|
|
uint8_t pinByte = pin / 8;
|
2023-02-04 00:19:32 +01:00
|
|
|
bool value = bitRead(_digitalInputStates[pinByte], pin - pinByte * 8);
|
2023-01-23 02:49:23 +01:00
|
|
|
return value;
|
2022-12-19 05:24:49 +01:00
|
|
|
}
|
|
|
|
|
2022-12-18 09:59:16 +01:00
|
|
|
void _write(VPIN vpin, int value) override {
|
|
|
|
int pin = vpin - _firstVpin;
|
2022-12-19 05:24:49 +01:00
|
|
|
_digitalOutBuffer[0] = EXIOWRD;
|
|
|
|
_digitalOutBuffer[1] = pin;
|
|
|
|
_digitalOutBuffer[2] = value;
|
2023-02-06 22:32:16 +01:00
|
|
|
I2CManager.read(_i2cAddress, _command1Buffer, 1, _digitalOutBuffer, 3);
|
|
|
|
if (_command1Buffer[0] != EXIORDY) {
|
|
|
|
DIAG(F("Vpin %d cannot be used as a digital output pin"), (int)vpin);
|
|
|
|
}
|
2022-12-18 09:59:16 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 05:53:46 +01:00
|
|
|
void _writeAnalogue(VPIN vpin, int value, uint8_t param1, uint16_t param2) override {
|
|
|
|
int pin = vpin - _firstVpin;
|
2023-02-01 10:46:08 +01:00
|
|
|
_command4Buffer[0] = EXIOWRAN;
|
|
|
|
_command4Buffer[1] = pin;
|
|
|
|
_command4Buffer[2] = value & 0xFF;
|
|
|
|
_command4Buffer[3] = value >> 8;
|
|
|
|
I2CManager.write(_i2cAddress, _command4Buffer, 4);
|
2023-02-01 05:53:46 +01:00
|
|
|
}
|
|
|
|
|
2022-12-08 05:21:01 +01:00
|
|
|
void _display() override {
|
2023-01-29 01:06:01 +01:00
|
|
|
DIAG(F("EX-IOExpander I2C:x%x v%d.%d.%d Vpins %d-%d %S"),
|
2022-12-25 22:36:12 +01:00
|
|
|
_i2cAddress, _majorVer, _minorVer, _patchVer,
|
2023-01-29 01:06:01 +01:00
|
|
|
(int)_firstVpin, (int)_firstVpin+_nPins-1,
|
2022-12-25 21:44:15 +01:00
|
|
|
_deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F(""));
|
2022-12-08 05:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t _i2cAddress;
|
2023-02-01 05:53:46 +01:00
|
|
|
uint8_t _numDigitalPins = 0;
|
2023-01-29 10:26:33 +01:00
|
|
|
uint8_t _numAnaloguePins = 0;
|
2022-12-19 05:24:49 +01:00
|
|
|
byte _digitalOutBuffer[3];
|
2022-12-25 22:36:12 +01:00
|
|
|
uint8_t _versionBuffer[3];
|
2022-12-25 21:44:15 +01:00
|
|
|
uint8_t _majorVer = 0;
|
|
|
|
uint8_t _minorVer = 0;
|
|
|
|
uint8_t _patchVer = 0;
|
2023-01-23 07:26:07 +01:00
|
|
|
byte* _digitalInputStates;
|
|
|
|
byte* _analogueInputStates;
|
|
|
|
uint8_t _digitalPinBytes = 0;
|
|
|
|
uint8_t _analoguePinBytes = 0;
|
2023-01-29 01:06:01 +01:00
|
|
|
byte _command1Buffer[1];
|
|
|
|
byte _command2Buffer[2];
|
2023-02-01 10:46:08 +01:00
|
|
|
byte _command4Buffer[4];
|
2023-02-01 05:53:46 +01:00
|
|
|
byte _receive3Buffer[3];
|
2023-01-29 01:06:01 +01:00
|
|
|
uint8_t* _analoguePinMap;
|
2022-12-09 05:41:48 +01:00
|
|
|
|
|
|
|
enum {
|
2022-12-13 10:51:41 +01:00
|
|
|
EXIOINIT = 0xE0, // Flag to initialise setup procedure
|
2022-12-15 06:10:53 +01:00
|
|
|
EXIORDY = 0xE1, // Flag we have completed setup procedure, also for EX-IO to ACK setup
|
2022-12-19 05:24:49 +01:00
|
|
|
EXIODPUP = 0xE2, // Flag we're sending digital pin pullup configuration
|
2022-12-25 21:44:15 +01:00
|
|
|
EXIOVER = 0xE3, // Flag to get version
|
2022-12-19 05:24:49 +01:00
|
|
|
EXIORDAN = 0xE4, // Flag to read an analogue input
|
|
|
|
EXIOWRD = 0xE5, // Flag for digital write
|
|
|
|
EXIORDD = 0xE6, // Flag to read digital input
|
2023-02-01 10:46:08 +01:00
|
|
|
EXIOENAN = 0xE7, // Flag to enable an analogue pin
|
2023-02-01 05:53:46 +01:00
|
|
|
EXIOINITA = 0xE8, // Flag we're receiving analogue pin mappings
|
|
|
|
EXIOPINS = 0xE9, // Flag we're receiving pin counts for buffers
|
2023-02-01 10:46:08 +01:00
|
|
|
EXIOWRAN = 0xEA, // Flag we're sending an analogue write (PWM)
|
|
|
|
EXIOERR = 0xEF, // Flag we've received an error
|
2022-12-09 05:41:48 +01:00
|
|
|
};
|
2022-12-08 05:21:01 +01:00
|
|
|
};
|
|
|
|
|
2023-01-27 19:42:55 +01:00
|
|
|
#endif
|