1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00
CommandStation-EX/IO_EXIOExpander.h

213 lines
7.9 KiB
C
Raw Normal View History

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/>.
*/
/*
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() {
2022-12-13 22:49:09 +01:00
* // EXIOExpander::create(vpin, num_vpins, i2c_address, digitalPinCount, analoguePinCount);
2022-12-28 20:10:37 +01:00
* EXIOExpander::create(800, 18, 0x65, 12, 8);
* }
*
* Note when defining the number of digital and analogue pins, there is no way to sanity check
* this from the device driver, and it is up to the user to define the correct values here.
2022-12-28 20:10:37 +01:00
*
* All pins available on the EX-IOExpander device must be accounted for.
*
* Vpins are allocated to digital pins first, and then analogue pins, so digital pins will
* populate the first part of the specified vpin range, with the analogue pins populating the
* last part of the vpin range.
* Eg. for a default Nano, 800 - 811 are digital (D2 - D13), 812 to 817 are analogue (A0 - A3, A6/A7).
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:
static void create(VPIN vpin, int nPins, I2CAddress i2cAddress, int numDigitalPins, int numAnaloguePins) {
2022-12-11 01:22:48 +01:00
if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress, numDigitalPins, numAnaloguePins);
2022-12-08 05:21:01 +01:00
}
private:
// Constructor
EXIOExpander(VPIN firstVpin, int nPins, I2CAddress i2cAddress, int numDigitalPins, int 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;
2023-01-23 07:26:07 +01:00
_digitalPinBytes = (numDigitalPins+7)/8;
2023-01-23 11:12:28 +01:00
_analoguePinBytes = numAnaloguePins * 2;
2023-01-23 07:26:07 +01:00
_digitalInputStates=(byte*) calloc(_digitalPinBytes,1);
2023-01-23 11:12:28 +01:00
_analogueInputStates=(byte*) calloc(_analoguePinBytes,1);
2022-12-08 05:21:01 +01:00
addDevice(this);
}
void _begin() {
// Initialise EX-IOExander device
2023-01-09 23:16:42 +01:00
I2CManager.begin();
if (I2CManager.exists(_I2CAddress)) {
2022-12-20 10:41:32 +01:00
_digitalOutBuffer[0] = EXIOINIT;
_digitalOutBuffer[1] = _numDigitalPins;
_digitalOutBuffer[2] = _numAnaloguePins;
2022-12-25 21:44:15 +01:00
// Send config, if EXIORDY returned, we're good, otherwise go offline
I2CManager.read(_I2CAddress, _commandBuffer, 1, _digitalOutBuffer, 3);
2023-01-23 07:26:07 +01:00
if (_commandBuffer[0] != EXIORDY) {
DIAG(F("ERROR configuring EX-IOExpander device, I2C:%s"), _I2CAddress.toString());
2022-12-20 10:41:32 +01:00
_deviceState = DEVSTATE_FAILED;
return;
}
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
2022-12-25 22:36:12 +01:00
// Using digital in buffer in reverse to save RAM
2023-01-23 07:26:07 +01:00
_commandBuffer[0] = EXIOVER;
I2CManager.read(_I2CAddress, _versionBuffer, 3, _commandBuffer, 1);
2022-12-25 22:36:12 +01:00
_majorVer = _versionBuffer[0];
_minorVer = _versionBuffer[1];
_patchVer = _versionBuffer[2];
DIAG(F("EX-IOExpander device found, I2C:%s, Version v%d.%d.%d"),
_I2CAddress.toString(), _versionBuffer[0], _versionBuffer[1], _versionBuffer[2]);
2022-12-08 05:21:01 +01:00
#ifdef DIAG_IO
_display();
#endif
} else {
DIAG(F("EX-IOExpander device not found, I2C:%s"), _I2CAddress.toString());
2022-12-08 05:21:01 +01:00
_deviceState = DEVSTATE_FAILED;
}
}
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override {
if (configType != CONFIGURE_INPUT) return false;
if (paramCount != 1) return false;
2023-01-11 22:27:42 +01:00
if (vpin >= _firstVpin + _numDigitalPins) {
2023-01-11 23:10:41 +01:00
DIAG(F("EX-IOExpander ERROR: Vpin %d is an analogue pin, cannot use as a digital pin"), vpin);
2023-01-11 22:27:42 +01:00
return false;
}
bool pullup = params[0];
int pin = vpin - _firstVpin;
_digitalOutBuffer[0] = EXIODPUP;
_digitalOutBuffer[1] = pin;
_digitalOutBuffer[2] = pullup;
I2CManager.write(_I2CAddress, _digitalOutBuffer, 3);
return true;
}
2023-01-11 23:10:41 +01:00
// We only use this to detect incorrect use of analogue pins
int _configureAnalogIn(VPIN vpin) override {
2023-01-11 22:27:42 +01:00
if (vpin < _firstVpin + _numDigitalPins) {
2023-01-11 23:10:41 +01:00
DIAG(F("EX-IOExpander ERROR: Vpin %d is a digital pin, cannot use as an analogue pin"), vpin);
2023-01-23 07:26:07 +01:00
return false;
2023-01-11 22:27:42 +01:00
}
2023-01-23 07:26:07 +01:00
int pin = vpin - _firstVpin;
_analogueOutBuffer[0] = EXIOENAN;
_analogueOutBuffer[1] = pin;
I2CManager.write(_I2CAddress, _analogueOutBuffer, 2);
2023-01-23 07:26:07 +01:00
return true;
2023-01-11 23:10:41 +01:00
}
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-23 02:49:23 +01:00
_commandBuffer[0] = EXIORDD;
I2CManager.read(_I2CAddress, _digitalInputStates, _digitalPinBytes, _commandBuffer, 1);
2023-01-23 07:26:07 +01:00
_commandBuffer[0] = EXIORDAN;
I2CManager.read(_I2CAddress, _analogueInputStates, _analoguePinBytes, _commandBuffer, 1);
2023-01-11 23:10:41 +01:00
}
int _readAnalogue(VPIN vpin) override {
if (vpin < _firstVpin + _numDigitalPins) return false;
2023-01-23 12:35:22 +01:00
int pin = vpin - _firstVpin - _numDigitalPins;
2023-01-23 11:12:28 +01:00
uint8_t _pinLSBByte = pin * 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
}
int _read(VPIN vpin) override {
2023-01-11 23:10:41 +01:00
if (vpin >= _firstVpin + _numDigitalPins) return false;
int pin = vpin - _firstVpin;
2023-01-23 02:49:23 +01:00
uint8_t pinByte = pin / 8;
2023-01-23 07:26:07 +01:00
bool value = _digitalInputStates[pinByte] >> (pin - pinByte * 8);
2023-01-23 02:49:23 +01:00
return value;
}
2022-12-18 09:59:16 +01:00
void _write(VPIN vpin, int value) override {
2023-01-11 23:10:41 +01:00
if (vpin >= _firstVpin + _numDigitalPins) return;
2022-12-18 09:59:16 +01:00
int pin = vpin - _firstVpin;
_digitalOutBuffer[0] = EXIOWRD;
_digitalOutBuffer[1] = pin;
_digitalOutBuffer[2] = value;
I2CManager.write(_I2CAddress, _digitalOutBuffer, 3);
2022-12-18 09:59:16 +01:00
}
2022-12-08 05:21:01 +01:00
void _display() override {
2022-12-25 22:36:12 +01:00
int _firstAnalogue, _lastAnalogue;
if (_numAnaloguePins == 0) {
_firstAnalogue = 0;
_lastAnalogue = 0;
} else {
_firstAnalogue = _firstVpin + _numDigitalPins;
_lastAnalogue = _firstVpin + _nPins - 1;
}
DIAG(F("EX-IOExpander I2C:%s v%d.%d.%d: %d Digital Vpins %d-%d, %d Analogue Vpins %d-%d %S"),
_I2CAddress.toString(), _majorVer, _minorVer, _patchVer,
2022-12-25 22:36:12 +01:00
_numDigitalPins, _firstVpin, _firstVpin + _numDigitalPins - 1,
_numAnaloguePins, _firstAnalogue, _lastAnalogue,
2022-12-25 21:44:15 +01:00
_deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F(""));
2022-12-08 05:21:01 +01:00
}
2022-12-11 01:22:48 +01:00
uint8_t _numDigitalPins;
uint8_t _numAnaloguePins;
2022-12-18 00:43:11 +01:00
byte _analogueOutBuffer[2];
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-23 02:49:23 +01:00
byte _commandBuffer[1];
2022-12-09 05:41:48 +01:00
enum {
EXIOINIT = 0xE0, // Flag to initialise setup procedure
EXIORDY = 0xE1, // Flag we have completed setup procedure, also for EX-IO to ACK setup
EXIODPUP = 0xE2, // Flag we're sending digital pin pullup configuration
2022-12-25 21:44:15 +01:00
EXIOVER = 0xE3, // Flag to get version
EXIORDAN = 0xE4, // Flag to read an analogue input
EXIOWRD = 0xE5, // Flag for digital write
EXIORDD = 0xE6, // Flag to read digital input
2023-01-23 07:26:07 +01:00
EXIOENAN = 0xE7, // Flag eo enable an analogue pin
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