mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 16:16:13 +01:00
Compare commits
4 Commits
c2bd012164
...
964364e51c
Author | SHA1 | Date | |
---|---|---|---|
|
964364e51c | ||
|
3af9cee9ad | ||
|
41befc99b1 | ||
|
5579a9aa49 |
|
@ -146,28 +146,36 @@ uint8_t I2CManagerClass::write_P(I2CAddress address, const uint8_t buffer[], uin
|
||||||
uint8_t I2CManagerClass::read(I2CAddress address, uint8_t readBuffer[], uint8_t readSize,
|
uint8_t I2CManagerClass::read(I2CAddress address, uint8_t readBuffer[], uint8_t readSize,
|
||||||
const uint8_t writeBuffer[], uint8_t writeSize, I2CRB *rb)
|
const uint8_t writeBuffer[], uint8_t writeSize, I2CRB *rb)
|
||||||
{
|
{
|
||||||
DIAG(F("I2CManagerClass::read()"));
|
// DIAG(F("Read addr=%x rdBuf=%x, rdLen=%d, wrBuf=%x, wrLen=%d"), address, readBuffer, readSize, writeBuffer, writeSize);
|
||||||
|
DIAG(F("I2CManagerClass::read() hit"));
|
||||||
uint8_t status, muxStatus;
|
uint8_t status, muxStatus;
|
||||||
uint8_t nBytes = 0;
|
uint8_t nBytes = 0;
|
||||||
uint8_t retryCount = 0;
|
uint8_t retryCount = 0;
|
||||||
// If request fails, retry up to the defined limit, unless the NORETRY flag is set
|
// If request fails, retry up to the defined limit, unless the NORETRY flag is set
|
||||||
// in the request block.
|
// in the request block.
|
||||||
do {
|
do {
|
||||||
|
DIAG(F("do hit"));
|
||||||
status = muxStatus = I2C_STATUS_OK;
|
status = muxStatus = I2C_STATUS_OK;
|
||||||
|
DIAG(F("status=%d"),status);
|
||||||
#ifdef I2C_EXTENDED_ADDRESS
|
#ifdef I2C_EXTENDED_ADDRESS
|
||||||
|
DIAG(F("Extended address hit"));
|
||||||
if (address.muxNumber() != I2CMux_None) {
|
if (address.muxNumber() != I2CMux_None) {
|
||||||
muxStatus = muxSelect(address);
|
muxStatus = muxSelect(address);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// Only start new transaction if address is non-zero.
|
// Only start new transaction if address is non-zero.
|
||||||
if (muxStatus == I2C_STATUS_OK && address != 0) {
|
if (muxStatus == I2C_STATUS_OK && address != 0) {
|
||||||
|
DIAG(F("muxStatus=%d"),muxStatus);
|
||||||
if (writeSize > 0) {
|
if (writeSize > 0) {
|
||||||
|
DIAG(F("wrBuf=%x, wrSize=%d"),writeBuffer,writeSize);
|
||||||
Wire.beginTransmission(address);
|
Wire.beginTransmission(address);
|
||||||
Wire.write(writeBuffer, writeSize);
|
Wire.write(writeBuffer, writeSize);
|
||||||
status = Wire.endTransmission(false); // Don't free bus yet
|
status = Wire.endTransmission(false); // Don't free bus yet
|
||||||
|
DIAG(F("status=%d"),status);
|
||||||
}
|
}
|
||||||
if (status == I2C_STATUS_OK) {
|
if (status == I2C_STATUS_OK) {
|
||||||
#ifdef WIRE_HAS_TIMEOUT
|
#ifdef WIRE_HAS_TIMEOUT
|
||||||
|
DIAG(F("WIRE_HAS_TIMEOUT"));
|
||||||
Wire.clearWireTimeoutFlag();
|
Wire.clearWireTimeoutFlag();
|
||||||
Wire.requestFrom(address, (size_t)readSize);
|
Wire.requestFrom(address, (size_t)readSize);
|
||||||
if (!Wire.getWireTimeoutFlag()) {
|
if (!Wire.getWireTimeoutFlag()) {
|
||||||
|
@ -178,10 +186,22 @@ uint8_t I2CManagerClass::read(I2CAddress address, uint8_t readBuffer[], uint8_t
|
||||||
status = I2C_STATUS_TIMEOUT;
|
status = I2C_STATUS_TIMEOUT;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
DIAG(F("NO timeout"));
|
||||||
Wire.requestFrom(address, (size_t)readSize);
|
Wire.requestFrom(address, (size_t)readSize);
|
||||||
while (Wire.available() && nBytes < readSize)
|
DIAG(F("address=%x, nBytes=%d, readSize=%d"),address,nBytes,readSize);
|
||||||
readBuffer[nBytes++] = Wire.read();
|
DIAG(F("Wire.available()=%d"),Wire.available());
|
||||||
|
// while (Wire.available() && nBytes < readSize)
|
||||||
|
while (nBytes < readSize)
|
||||||
|
if (Wire.available()) {
|
||||||
|
uint8_t temp=nBytes;
|
||||||
|
readBuffer[nBytes++] = Wire.read();
|
||||||
|
DIAG(F("nBytes=%d, readBuffer[nBytes]=%d"), temp, readBuffer[temp]);
|
||||||
|
} else {
|
||||||
|
delay(1);
|
||||||
|
}
|
||||||
|
// DIAG(F("nBytes=%d,readBuffer[nBytes]=%d"),nBytes-1,readBuffer[nBytes-1]);
|
||||||
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
|
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
|
||||||
|
DIAG(F("status=%d"),status);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -211,6 +231,7 @@ uint8_t I2CManagerClass::read(I2CAddress address, uint8_t readBuffer[], uint8_t
|
||||||
* the non-blocking version.
|
* the non-blocking version.
|
||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
void I2CManagerClass::queueRequest(I2CRB *req) {
|
void I2CManagerClass::queueRequest(I2CRB *req) {
|
||||||
|
DIAG(F("I2CManagerClass::queueRequest() hit"));
|
||||||
if (req==NULL) {
|
if (req==NULL) {
|
||||||
DIAG(F("NOOOOOOOOOOO"));
|
DIAG(F("NOOOOOOOOOOO"));
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -80,19 +80,24 @@ private:
|
||||||
void _begin() {
|
void _begin() {
|
||||||
uint8_t status;
|
uint8_t status;
|
||||||
// Initialise EX-IOExander device
|
// Initialise EX-IOExander device
|
||||||
|
DIAG(F("EXIO begin()"));
|
||||||
I2CManager.begin();
|
I2CManager.begin();
|
||||||
if (I2CManager.exists(_I2CAddress)) {
|
if (I2CManager.exists(_I2CAddress)) {
|
||||||
|
DIAG(F("EXIO address found %x"),_I2CAddress);
|
||||||
// Send config, if EXIOPINS returned, we're good, setup pin buffers, otherwise go offline
|
// Send config, if EXIOPINS returned, we're good, setup pin buffers, otherwise go offline
|
||||||
// NB The I2C calls here are done as blocking calls, as they're not time-critical
|
// NB The I2C calls here are done as blocking calls, as they're not time-critical
|
||||||
// during initialisation and the reads require waiting for a response anyway.
|
// during initialisation and the reads require waiting for a response anyway.
|
||||||
// Hence we can allocate I/O buffers from the stack.
|
// Hence we can allocate I/O buffers from the stack.
|
||||||
uint8_t receiveBuffer[3];
|
uint8_t receiveBuffer[3];
|
||||||
uint8_t commandBuffer[4] = {EXIOINIT, (uint8_t)_nPins, (uint8_t)(_firstVpin & 0xFF), (uint8_t)(_firstVpin >> 8)};
|
uint8_t commandBuffer[4] = {EXIOINIT, (uint8_t)_nPins, (uint8_t)(_firstVpin & 0xFF), (uint8_t)(_firstVpin >> 8)};
|
||||||
|
DIAG(F("EXIOINIT, _nPins=%d, _firstVpin=%d"),_nPins,_firstVpin);
|
||||||
status = I2CManager.read(_I2CAddress, receiveBuffer, sizeof(receiveBuffer), commandBuffer, sizeof(commandBuffer));
|
status = I2CManager.read(_I2CAddress, receiveBuffer, sizeof(receiveBuffer), commandBuffer, sizeof(commandBuffer));
|
||||||
|
DIAG(F("EXIO status=%d"),status);
|
||||||
if (status == I2C_STATUS_OK) {
|
if (status == I2C_STATUS_OK) {
|
||||||
if (receiveBuffer[0] == EXIOPINS) {
|
if (receiveBuffer[0] == EXIOPINS) {
|
||||||
_numDigitalPins = receiveBuffer[1];
|
_numDigitalPins = receiveBuffer[1];
|
||||||
_numAnaloguePins = receiveBuffer[2];
|
_numAnaloguePins = receiveBuffer[2];
|
||||||
|
DIAG(F("EXIO dPins=%d, aPins=%d"),_numDigitalPins,_numAnaloguePins);
|
||||||
|
|
||||||
// See if we already have suitable buffers assigned
|
// See if we already have suitable buffers assigned
|
||||||
if (_numDigitalPins>0) {
|
if (_numDigitalPins>0) {
|
||||||
|
@ -129,12 +134,14 @@ private:
|
||||||
// We now need to retrieve the analogue pin map if there are analogue pins
|
// We now need to retrieve the analogue pin map if there are analogue pins
|
||||||
if (status == I2C_STATUS_OK && _numAnaloguePins>0) {
|
if (status == I2C_STATUS_OK && _numAnaloguePins>0) {
|
||||||
commandBuffer[0] = EXIOINITA;
|
commandBuffer[0] = EXIOINITA;
|
||||||
|
DIAG(F("EXIOINITA"));
|
||||||
status = I2CManager.read(_I2CAddress, _analoguePinMap, _numAnaloguePins, commandBuffer, 1);
|
status = I2CManager.read(_I2CAddress, _analoguePinMap, _numAnaloguePins, commandBuffer, 1);
|
||||||
}
|
}
|
||||||
if (status == I2C_STATUS_OK) {
|
if (status == I2C_STATUS_OK) {
|
||||||
// Attempt to get version, if we don't get it, we don't care, don't go offline
|
// Attempt to get version, if we don't get it, we don't care, don't go offline
|
||||||
uint8_t versionBuffer[3];
|
uint8_t versionBuffer[3];
|
||||||
commandBuffer[0] = EXIOVER;
|
commandBuffer[0] = EXIOVER;
|
||||||
|
DIAG(F("EXIOVER"));
|
||||||
if (I2CManager.read(_I2CAddress, versionBuffer, sizeof(versionBuffer), commandBuffer, 1) == I2C_STATUS_OK) {
|
if (I2CManager.read(_I2CAddress, versionBuffer, sizeof(versionBuffer), commandBuffer, 1) == I2C_STATUS_OK) {
|
||||||
_majorVer = versionBuffer[0];
|
_majorVer = versionBuffer[0];
|
||||||
_minorVer = versionBuffer[1];
|
_minorVer = versionBuffer[1];
|
||||||
|
@ -245,6 +252,7 @@ private:
|
||||||
// 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).
|
||||||
_readCommandBuffer[0] = EXIORDD;
|
_readCommandBuffer[0] = EXIORDD;
|
||||||
|
DIAG(F("EXIORDD address=%x, states=%d, bytes=%d"),_I2CAddress,_digitalInputStates,(_numDigitalPins+7)/8);
|
||||||
I2CManager.read(_I2CAddress, _digitalInputStates, (_numDigitalPins+7)/8, _readCommandBuffer, 1, &_i2crb);
|
I2CManager.read(_I2CAddress, _digitalInputStates, (_numDigitalPins+7)/8, _readCommandBuffer, 1, &_i2crb);
|
||||||
// non-blocking read
|
// non-blocking read
|
||||||
_lastDigitalRead = currentMicros;
|
_lastDigitalRead = currentMicros;
|
||||||
|
@ -252,8 +260,9 @@ private:
|
||||||
} else if (currentMicros - _lastAnalogueRead > _analogueRefresh && _numAnaloguePins>0) { // Delay for analogue read refresh
|
} else if (currentMicros - _lastAnalogueRead > _analogueRefresh && _numAnaloguePins>0) { // Delay for analogue read refresh
|
||||||
// Issue new read for analogue input states
|
// Issue new read for analogue input states
|
||||||
_readCommandBuffer[0] = EXIORDAN;
|
_readCommandBuffer[0] = EXIORDAN;
|
||||||
I2CManager.read(_I2CAddress, _analogueInputBuffer,
|
// DIAG(F("EXIORDAN address=%x, aBuffer=%d, bytes=%d"),_I2CAddress,_analogueInputBuffer,_numAnaloguePins*2);
|
||||||
_numAnaloguePins * 2, _readCommandBuffer, 1, &_i2crb);
|
// I2CManager.read(_I2CAddress, _analogueInputBuffer,
|
||||||
|
// _numAnaloguePins * 2, _readCommandBuffer, 1, &_i2crb);
|
||||||
_lastAnalogueRead = currentMicros;
|
_lastAnalogueRead = currentMicros;
|
||||||
_readState = RDS_ANALOGUE;
|
_readState = RDS_ANALOGUE;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user