mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2025-04-21 12:31:19 +02:00
now it is first prototype - forgot a few things
This commit is contained in:
parent
523f2bd7ea
commit
cd1afaf04d
@ -21,51 +21,6 @@
|
|||||||
#include "IO_Modbus.h"
|
#include "IO_Modbus.h"
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
uint8_t MBRB::wait() {
|
|
||||||
while (status==MB_STATUS_PENDING) {
|
|
||||||
// may as well whistle or something
|
|
||||||
};
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
bool MBRB::isBusy() {
|
|
||||||
if (status==MB_STATUS_PENDING) {
|
|
||||||
return true;
|
|
||||||
} else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
void MBRB::setReadParams(int nodeID, uint8_t *readBuffer, uint8_t readLen) {
|
|
||||||
this->nodeID = nodeID;
|
|
||||||
this->writeLen = 0;
|
|
||||||
this->readBuffer = readBuffer;
|
|
||||||
this->readLen = readLen;
|
|
||||||
this->operation = OPERATION_READ;
|
|
||||||
this->status = MB_STATUS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MBRB::setRequestParams(int nodeID, uint8_t *readBuffer, uint8_t readLen,
|
|
||||||
const uint8_t *writeBuffer, uint8_t writeLen) {
|
|
||||||
this->nodeID = nodeID;
|
|
||||||
this->writeBuffer = writeBuffer;
|
|
||||||
this->writeLen = writeLen;
|
|
||||||
this->readBuffer = readBuffer;
|
|
||||||
this->readLen = readLen;
|
|
||||||
this->operation = OPERATION_REQUEST;
|
|
||||||
this->status = MB_STATUS_OK;
|
|
||||||
}
|
|
||||||
void MBRB::setWriteParams(int nodeID, const uint8_t *writeBuffer, uint8_t writeLen) {
|
|
||||||
this->nodeID = nodeID;
|
|
||||||
this->writeBuffer = writeBuffer;
|
|
||||||
this->writeLen = writeLen;
|
|
||||||
this->readLen = 0;
|
|
||||||
this->operation = OPERATION_SEND;
|
|
||||||
this->status = MB_STATUS_OK;
|
|
||||||
}
|
|
||||||
void MBRB::suppressRetries(bool suppress) {
|
|
||||||
if (suppress)
|
|
||||||
this->operation |= OPERATION_NORETRY;
|
|
||||||
else
|
|
||||||
this->operation &= ~OPERATION_NORETRY;
|
|
||||||
}
|
|
||||||
void Modbus::setTransactionId(uint16_t transactionId) {
|
void Modbus::setTransactionId(uint16_t transactionId) {
|
||||||
_setRegister(tcp, 0, transactionId);
|
_setRegister(tcp, 0, transactionId);
|
||||||
}
|
}
|
||||||
@ -424,32 +379,8 @@ if (taskCnt > 0) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// receive states
|
|
||||||
if (_readState != RDS_IDLE) {
|
|
||||||
if (_mbrb.isBusy()) return; // If I2C operation still in progress, return
|
|
||||||
|
|
||||||
uint8_t status = _mbrb.status;
|
|
||||||
if (status == I2C_STATUS_OK) { // If device request ok, read input data
|
|
||||||
|
|
||||||
// First check if we need to process received data
|
|
||||||
if (_readState == RDS_ANALOGUE) {
|
|
||||||
// Read of analogue values was in progress, so process received values
|
|
||||||
// Here we need to copy the values from input buffer to the analogue value array. We need to
|
|
||||||
// do this to avoid tearing of the values (i.e. one byte of a two-byte value being changed
|
|
||||||
// while the value is being read).
|
|
||||||
memcpy(_currentNode->_analogueInputStates, _currentNode->_analogueInputBuffer, _currentNode->_analoguePinBytes); // Copy I2C input buffer to states
|
memcpy(_currentNode->_analogueInputStates, _currentNode->_analogueInputBuffer, _currentNode->_analoguePinBytes); // Copy I2C input buffer to states
|
||||||
|
|
||||||
} else if (_readState == RDS_DIGITAL) {
|
|
||||||
// Read of digital states was in progress, so process received values
|
|
||||||
// The received digital states are placed directly into the digital buffer on receipt,
|
|
||||||
// so don't need any further processing at this point (unless we want to check for
|
|
||||||
// changes and notify them to subscribers, to avoid the need for polling - see IO_GPIOBase.h).
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
reportError(status, false); // report eror but don't go offline.
|
|
||||||
|
|
||||||
_readState = RDS_IDLE;
|
|
||||||
}
|
|
||||||
if (_currentNode->_numDigitalPins>0 && currentMicros - _lastDigitalRead > _digitalRefresh) { // Delay for digital read refresh
|
if (_currentNode->_numDigitalPins>0 && currentMicros - _lastDigitalRead > _digitalRefresh) { // Delay for digital read refresh
|
||||||
// Issue new read request for digital states. As the request is non-blocking, the buffer has to
|
// Issue new read request for digital states. As the request is non-blocking, the buffer has to
|
||||||
// be allocated from heap (object state).
|
// be allocated from heap (object state).
|
||||||
|
122
IO_Modbus.h
122
IO_Modbus.h
@ -55,46 +55,6 @@
|
|||||||
#include "IODevice.h"
|
#include "IODevice.h"
|
||||||
uint16_t div8RndUp(uint16_t value);
|
uint16_t div8RndUp(uint16_t value);
|
||||||
|
|
||||||
// Class defining a request context for an I2C operation.
|
|
||||||
class MBRB {
|
|
||||||
public:
|
|
||||||
volatile uint8_t status; // Completion status, or pending flag (updated from IRC)
|
|
||||||
volatile uint8_t nBytes; // Number of bytes read (updated from IRC)
|
|
||||||
|
|
||||||
inline MBRB() { status = I2C_STATUS_OK; };
|
|
||||||
uint8_t wait();
|
|
||||||
bool isBusy();
|
|
||||||
|
|
||||||
void setReadParams(int nodeID, uint8_t *readBuffer, uint8_t readLen);
|
|
||||||
void setRequestParams(int nodeID, uint8_t *readBuffer, uint8_t readLen, const uint8_t *writeBuffer, uint8_t writeLen);
|
|
||||||
void setWriteParams(int nodeID, const uint8_t *writeBuffer, uint8_t writeLen);
|
|
||||||
void suppressRetries(bool suppress);
|
|
||||||
|
|
||||||
uint8_t writeLen;
|
|
||||||
uint8_t readLen;
|
|
||||||
uint8_t operation;
|
|
||||||
int nodeID;
|
|
||||||
uint8_t *readBuffer;
|
|
||||||
const uint8_t *writeBuffer;
|
|
||||||
MBRB *nextRequest;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum : uint8_t {
|
|
||||||
// Codes used by Wire and by native drivers
|
|
||||||
MB_STATUS_OK=0,
|
|
||||||
MB_STATUS_TRUNCATED=1,
|
|
||||||
MB_STATUS_NEGATIVE_ACKNOWLEDGE=2,
|
|
||||||
MB_STATUS_TRANSMIT_ERROR=3,
|
|
||||||
MB_STATUS_TIMEOUT=5,
|
|
||||||
// Code used by Wire only
|
|
||||||
MB_STATUS_OTHER_TWI_ERROR=4, // catch-all error
|
|
||||||
// Codes used by native drivers only
|
|
||||||
MB_STATUS_ARBITRATION_LOST=6,
|
|
||||||
MB_STATUS_BUS_ERROR=7,
|
|
||||||
MB_STATUS_UNEXPECTED_ERROR=8,
|
|
||||||
MB_STATUS_PENDING=253,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**********************************************************************
|
/**********************************************************************
|
||||||
* Modbusnode class
|
* Modbusnode class
|
||||||
*
|
*
|
||||||
@ -210,7 +170,7 @@ public:
|
|||||||
uint8_t _digitalPinBytes = 0; // Size of allocated memory buffer (may be longer than needed)
|
uint8_t _digitalPinBytes = 0; // Size of allocated memory buffer (may be longer than needed)
|
||||||
uint8_t _analoguePinBytes = 0; // Size of allocated memory buffer (may be longer than needed)
|
uint8_t _analoguePinBytes = 0; // Size of allocated memory buffer (may be longer than needed)
|
||||||
uint8_t* _analoguePinMap = NULL;
|
uint8_t* _analoguePinMap = NULL;
|
||||||
I2CRB _i2crb;
|
|
||||||
static void create(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID) {
|
static void create(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID) {
|
||||||
if (checkNoOverlap(firstVpin, nPins)) new Modbusnode(firstVpin, nPins, busNo, nodeID);
|
if (checkNoOverlap(firstVpin, nPins)) new Modbusnode(firstVpin, nPins, busNo, nodeID);
|
||||||
}
|
}
|
||||||
@ -302,28 +262,18 @@ public:
|
|||||||
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override {
|
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override {
|
||||||
if (paramCount != 1) return false;
|
if (paramCount != 1) return false;
|
||||||
int pin = vpin - _firstVpin;
|
int pin = vpin - _firstVpin;
|
||||||
if (configType == CONFIGURE_INPUT) {
|
int pin = vpin - _firstVpin;
|
||||||
Modbus* mb = Modbus::findBus(0);
|
Modbus* mb = Modbus::findBus(0);
|
||||||
mb->_CommMode = 2;
|
int* param[] = {(int*)pin, (int*)configType, (int*)paramCount, (int*)params[0]};
|
||||||
mb->_pullup = params[0];
|
mb->addTask(_nodeID, 3, 4, param);
|
||||||
mb->_pin = pin;
|
|
||||||
mb->_opperation = 1;
|
|
||||||
} else if (configType == CONFIGURE_ANALOGINPUT) {
|
|
||||||
// TODO: Consider moving code from _configureAnalogIn() to here and remove _configureAnalogIn
|
|
||||||
// from IODevice class definition. Not urgent, but each virtual function defined
|
|
||||||
// means increasing the RAM requirement of every HAL device driver, whether it's relevant
|
|
||||||
// to the driver or not.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int _configureAnalogIn(VPIN vpin) override {
|
int _configureAnalogIn(VPIN vpin) override {
|
||||||
int pin = vpin - _firstVpin;
|
int pin = vpin - _firstVpin;
|
||||||
Modbus* mb = Modbus::findBus(0);
|
Modbus* mb = Modbus::findBus(0);
|
||||||
mb->_CommMode = 2;
|
int* params[] = {(int*)pin};
|
||||||
mb->_pin = pin;
|
mb->addTask(_nodeID, 3, 1, params);
|
||||||
mb ->_opperation = 2;
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -468,40 +418,44 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
int _read(VPIN vpin) override {
|
int _read(VPIN vpin) override {
|
||||||
// Return current state from this device
|
if (_deviceState == DEVSTATE_FAILED) return 0;
|
||||||
uint16_t pin = vpin - _firstVpin;
|
int pin = vpin - _firstVpin;
|
||||||
int PinNum = pin / 16;
|
uint8_t pinByte = pin / 8;
|
||||||
int PinBit = pin % 16;
|
bool value = bitRead(_digitalInputStates[pinByte], pin - pinByte * 8);
|
||||||
if (bitRead(configAPinsI[PinNum],PinBit) == true) return bitRead(dataBI[PinNum],PinBit)? 1:0;
|
return value;
|
||||||
else return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void _write(VPIN vpin, int value) override {
|
void _write(VPIN vpin, int value) override {
|
||||||
// Update current state for this device, in preparation the bus transmission
|
if (_deviceState == DEVSTATE_FAILED) return;
|
||||||
uint16_t pin = vpin - _firstVpin;
|
int pin = vpin - _firstVpin;
|
||||||
int PinNum = pin / 16;
|
Modbus* mb = Modbus::findBus(0);
|
||||||
int PinBit = pin % 16;
|
int* params[] = {(int*)pin, (int*)value};
|
||||||
if (bitRead(configAPinsO[PinNum], PinBit) == true) {
|
mb->addTask(_nodeID, 3, 2, params);
|
||||||
if (value == 1) bitSet(dataBO[PinNum], PinBit);
|
|
||||||
else bitClear(dataBO[PinNum], PinBit);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int _readAnalogue(VPIN vpin) {
|
int _readAnalogue(VPIN vpin) override {
|
||||||
// Return acquired data value, e.g.
|
if (_deviceState == DEVSTATE_FAILED) return 0;
|
||||||
uint16_t pin = vpin - _firstVpin;
|
int pin = vpin - _firstVpin;
|
||||||
int PinNum = pin / 16;
|
for (uint8_t aPin = 0; aPin < _numAnaloguePins; aPin++) {
|
||||||
int PinBit = pin % 16;
|
if (_analoguePinMap[aPin] == pin) {
|
||||||
if (bitRead(configAPinsI[PinNum],PinBit) == true) return dataAI[pin];
|
uint8_t _pinLSBByte = aPin * 2;
|
||||||
else return 0;
|
uint8_t _pinMSBByte = _pinLSBByte + 1;
|
||||||
|
return (_analogueInputStates[_pinMSBByte] << 8) + _analogueInputStates[_pinLSBByte];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1; // pin not found in table
|
||||||
}
|
}
|
||||||
|
|
||||||
void _writeAnalogue(VPIN vpin, int value) {
|
void _writeAnalogue(VPIN vpin, int value, uint8_t profile, uint16_t duration) override {
|
||||||
uint16_t pin = vpin - _firstVpin;
|
uint8_t servoBuffer[7];
|
||||||
int PinNum = pin / 16;
|
uint8_t responseBuffer[1];
|
||||||
int PinBit = pin % 16;
|
|
||||||
if (bitRead(configAPinsI[PinNum],PinBit) == true) dataAO[pin] = value;
|
if (_deviceState == DEVSTATE_FAILED) return;
|
||||||
|
int pin = vpin - _firstVpin;
|
||||||
|
Modbus* mb = Modbus::findBus(0);
|
||||||
|
int* params[] = {(int*)pin, (int*)value, (int*)profile, (int*)duration};
|
||||||
|
mb->addTask(_nodeID, 3, 4, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t getBusNumber() {
|
uint8_t getBusNumber() {
|
||||||
@ -611,7 +565,7 @@ private:
|
|||||||
unsigned long _lastAnalogueRead = 0;
|
unsigned long _lastAnalogueRead = 0;
|
||||||
const unsigned long _digitalRefresh = 10000UL; // Delay refreshing digital inputs for 10ms
|
const unsigned long _digitalRefresh = 10000UL; // Delay refreshing digital inputs for 10ms
|
||||||
const unsigned long _analogueRefresh = 50000UL; // Delay refreshing analogue inputs for 50ms
|
const unsigned long _analogueRefresh = 50000UL; // Delay refreshing analogue inputs for 50ms
|
||||||
MBRB _mbrb;
|
|
||||||
|
|
||||||
// EX-IOExpander protocol flags
|
// EX-IOExpander protocol flags
|
||||||
enum {
|
enum {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user