2021-09-11 14:35:11 +02:00
|
|
|
/*
|
|
|
|
* © 2021, Neil McKechnie. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is part of DCC++EX API
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef io_analogueinputs_h
|
|
|
|
#define io_analogueinputs_h
|
|
|
|
|
|
|
|
// Uncomment following line to slow the scan cycle down to 1second ADC samples, with
|
|
|
|
// diagnostic output of scanned values.
|
|
|
|
//#define IO_ANALOGUE_SLOW
|
|
|
|
|
|
|
|
#include "IODevice.h"
|
|
|
|
#include "I2CManager.h"
|
|
|
|
#include "DIAG.h"
|
|
|
|
#include "FSH.h"
|
|
|
|
|
|
|
|
/**********************************************************************************************
|
|
|
|
* ADS111x class for I2C-connected analogue input modules ADS1113, ADS1114 and ADS1115.
|
|
|
|
*
|
|
|
|
* ADS1113 and ADS1114 are restricted to 1 input. ADS1115 has a multiplexer which allows
|
|
|
|
* any of four input pins to be read by its ADC.
|
|
|
|
*
|
|
|
|
* The driver polls the device in accordance with the constant 'scanInterval' below. On first loop
|
|
|
|
* entry, the multiplexer is set to pin A0 and the ADC is triggered. On second and subsequent
|
|
|
|
* entries, the analogue value is read from the conversion register and then the multiplexer and
|
|
|
|
* ADC are set up to read the next pin.
|
|
|
|
*
|
|
|
|
* The ADS111x is set up as follows:
|
|
|
|
* Single-shot scan
|
2021-09-15 01:23:24 +02:00
|
|
|
* Data rate 128 samples/sec (7.8ms/sample, but scanned every 10ms)
|
2021-09-11 14:35:11 +02:00
|
|
|
* Comparator off
|
|
|
|
* Gain FSR=6.144V
|
|
|
|
* The gain means that the maximum input voltage of 5V (when Vss=5V) gives a reading
|
|
|
|
* of 32767*(5.0/6.144) = 26666.
|
|
|
|
*
|
2021-09-15 01:23:24 +02:00
|
|
|
* A device is configured by the following:
|
|
|
|
* ADS111x::create(firstVpin, nPins, i2cAddress);
|
|
|
|
* for example
|
|
|
|
* ADS111x::create(300, 1, 0x48); // single-input ADS1113
|
|
|
|
* ADS111x::create(300, 4, 0x48); // four-input ADS1115
|
|
|
|
*
|
2021-09-11 14:35:11 +02:00
|
|
|
* Note: The device is simple and does not need initial configuration, so it should recover from
|
|
|
|
* temporary loss of communications or power.
|
|
|
|
**********************************************************************************************/
|
|
|
|
class ADS111x: public IODevice {
|
|
|
|
public:
|
2022-06-14 16:23:27 +02:00
|
|
|
static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress) {
|
2022-06-14 18:21:11 +02:00
|
|
|
if (checkNoOverlap(firstVpin,nPins,i2cAddress)) new ADS111x(firstVpin, nPins, i2cAddress);
|
2022-06-14 16:23:27 +02:00
|
|
|
}
|
|
|
|
private:
|
2021-09-11 14:35:11 +02:00
|
|
|
ADS111x(VPIN firstVpin, int nPins, uint8_t i2cAddress) {
|
|
|
|
_firstVpin = firstVpin;
|
|
|
|
_nPins = min(nPins,4);
|
|
|
|
_i2cAddress = i2cAddress;
|
2021-10-05 13:48:45 +02:00
|
|
|
_currentPin = 0;
|
|
|
|
for (int8_t i=0; i<_nPins; i++)
|
|
|
|
_value[i] = -1;
|
2021-09-11 14:35:11 +02:00
|
|
|
addDevice(this);
|
|
|
|
}
|
|
|
|
void _begin() {
|
|
|
|
// Initialise ADS device
|
|
|
|
if (I2CManager.exists(_i2cAddress)) {
|
2021-10-05 13:48:45 +02:00
|
|
|
_nextState = STATE_STARTSCAN;
|
2021-09-11 14:35:11 +02:00
|
|
|
#ifdef DIAG_IO
|
|
|
|
_display();
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
DIAG(F("ADS111x device not found, I2C:%x"), _i2cAddress);
|
2021-09-21 12:02:23 +02:00
|
|
|
_deviceState = DEVSTATE_FAILED;
|
2021-09-11 14:35:11 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-15 11:44:43 +02:00
|
|
|
void _loop(unsigned long currentMicros) override {
|
2021-09-11 14:35:11 +02:00
|
|
|
|
2021-09-21 12:02:23 +02:00
|
|
|
// Check that previous non-blocking write has completed, if not then wait
|
2021-10-05 13:48:45 +02:00
|
|
|
uint8_t status = _i2crb.status;
|
|
|
|
if (status == I2C_STATUS_PENDING) return; // Busy, so don't do anything.
|
2021-09-21 12:02:23 +02:00
|
|
|
if (status == I2C_STATUS_OK) {
|
2021-10-05 13:48:45 +02:00
|
|
|
switch (_nextState) {
|
|
|
|
case STATE_STARTSCAN:
|
|
|
|
// Configure ADC and multiplexer for next scan. See ADS111x datasheet for details
|
|
|
|
// of configuration register settings.
|
|
|
|
_outBuffer[0] = 0x01; // Config register address
|
|
|
|
_outBuffer[1] = 0xC0 + (_currentPin << 4); // Trigger single-shot, channel n
|
|
|
|
_outBuffer[2] = 0xA3; // 250 samples/sec, comparator off
|
|
|
|
// Write command, without waiting for completion.
|
|
|
|
I2CManager.write(_i2cAddress, _outBuffer, 3, &_i2crb);
|
|
|
|
|
|
|
|
delayUntil(currentMicros + scanInterval);
|
|
|
|
_nextState = STATE_STARTREAD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_STARTREAD:
|
|
|
|
// Reading the pin value
|
|
|
|
_outBuffer[0] = 0x00; // Conversion register address
|
|
|
|
I2CManager.read(_i2cAddress, _inBuffer, 2, _outBuffer, 1, &_i2crb); // Read register
|
|
|
|
_nextState = STATE_GETVALUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_GETVALUE:
|
2021-09-21 12:02:23 +02:00
|
|
|
_value[_currentPin] = ((uint16_t)_inBuffer[0] << 8) + (uint16_t)_inBuffer[1];
|
|
|
|
#ifdef IO_ANALOGUE_SLOW
|
|
|
|
DIAG(F("ADS111x pin:%d value:%d"), _currentPin, _value[_currentPin]);
|
|
|
|
#endif
|
2021-10-05 13:48:45 +02:00
|
|
|
|
|
|
|
// Move to next pin
|
|
|
|
if (++_currentPin >= _nPins) _currentPin = 0;
|
|
|
|
_nextState = STATE_STARTSCAN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2021-09-11 14:35:11 +02:00
|
|
|
}
|
2021-10-05 13:48:45 +02:00
|
|
|
} else { // error status
|
2021-09-21 12:02:23 +02:00
|
|
|
DIAG(F("ADS111x I2C:x%d Error:%d %S"), _i2cAddress, status, I2CManager.getErrorMessage(status));
|
|
|
|
_deviceState = DEVSTATE_FAILED;
|
|
|
|
}
|
2021-09-11 14:35:11 +02:00
|
|
|
}
|
2021-09-21 12:02:23 +02:00
|
|
|
|
2021-09-15 11:44:43 +02:00
|
|
|
int _readAnalogue(VPIN vpin) override {
|
2021-09-11 14:35:11 +02:00
|
|
|
int pin = vpin - _firstVpin;
|
|
|
|
return _value[pin];
|
|
|
|
}
|
2021-09-21 12:02:23 +02:00
|
|
|
|
2021-09-15 11:44:43 +02:00
|
|
|
void _display() override {
|
2021-09-21 12:02:23 +02:00
|
|
|
DIAG(F("ADS111x I2C:x%x Configured on Vpins:%d-%d %S"), _i2cAddress, _firstVpin, _firstVpin+_nPins-1,
|
|
|
|
_deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F(""));
|
2021-09-11 14:35:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-15 11:44:43 +02:00
|
|
|
// ADC conversion rate is 250SPS, or 4ms per conversion. Set the period between updates to 10ms.
|
|
|
|
// This is enough to allow the conversion to reliably complete in time.
|
2021-09-11 14:35:11 +02:00
|
|
|
#ifndef IO_ANALOGUE_SLOW
|
|
|
|
const unsigned long scanInterval = 10000UL; // Period between successive ADC scans in microseconds.
|
|
|
|
#else
|
|
|
|
const unsigned long scanInterval = 1000000UL; // Period between successive ADC scans in microseconds.
|
|
|
|
#endif
|
2021-10-05 13:48:45 +02:00
|
|
|
enum : uint8_t {
|
|
|
|
STATE_STARTSCAN,
|
|
|
|
STATE_STARTREAD,
|
|
|
|
STATE_GETVALUE,
|
|
|
|
};
|
2021-09-11 14:35:11 +02:00
|
|
|
uint16_t _value[4];
|
|
|
|
uint8_t _i2cAddress;
|
|
|
|
uint8_t _outBuffer[3];
|
|
|
|
uint8_t _inBuffer[2];
|
|
|
|
uint8_t _currentPin; // ADC pin currently being scanned
|
|
|
|
I2CRB _i2crb;
|
2021-10-05 13:48:45 +02:00
|
|
|
uint8_t _nextState;
|
2021-09-11 14:35:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // io_analogueinputs_h
|