1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-24 00:26:13 +01:00

Compare commits

..

No commits in common. "964364e51ccde1403ccb2901a50449603aa5438f" and "c2bd0121647abf7210e8c01742d0e1a95665b84d" have entirely different histories.

2 changed files with 5 additions and 35 deletions

View File

@ -146,36 +146,28 @@ uint8_t I2CManagerClass::write_P(I2CAddress address, const uint8_t buffer[], uin
uint8_t I2CManagerClass::read(I2CAddress address, uint8_t readBuffer[], uint8_t readSize,
const uint8_t writeBuffer[], uint8_t writeSize, I2CRB *rb)
{
// DIAG(F("Read addr=%x rdBuf=%x, rdLen=%d, wrBuf=%x, wrLen=%d"), address, readBuffer, readSize, writeBuffer, writeSize);
DIAG(F("I2CManagerClass::read() hit"));
DIAG(F("I2CManagerClass::read()"));
uint8_t status, muxStatus;
uint8_t nBytes = 0;
uint8_t retryCount = 0;
// If request fails, retry up to the defined limit, unless the NORETRY flag is set
// in the request block.
do {
DIAG(F("do hit"));
status = muxStatus = I2C_STATUS_OK;
DIAG(F("status=%d"),status);
#ifdef I2C_EXTENDED_ADDRESS
DIAG(F("Extended address hit"));
if (address.muxNumber() != I2CMux_None) {
muxStatus = muxSelect(address);
}
#endif
// Only start new transaction if address is non-zero.
if (muxStatus == I2C_STATUS_OK && address != 0) {
DIAG(F("muxStatus=%d"),muxStatus);
if (writeSize > 0) {
DIAG(F("wrBuf=%x, wrSize=%d"),writeBuffer,writeSize);
Wire.beginTransmission(address);
Wire.write(writeBuffer, writeSize);
status = Wire.endTransmission(false); // Don't free bus yet
DIAG(F("status=%d"),status);
}
if (status == I2C_STATUS_OK) {
#ifdef WIRE_HAS_TIMEOUT
DIAG(F("WIRE_HAS_TIMEOUT"));
Wire.clearWireTimeoutFlag();
Wire.requestFrom(address, (size_t)readSize);
if (!Wire.getWireTimeoutFlag()) {
@ -186,22 +178,10 @@ uint8_t I2CManagerClass::read(I2CAddress address, uint8_t readBuffer[], uint8_t
status = I2C_STATUS_TIMEOUT;
}
#else
DIAG(F("NO timeout"));
Wire.requestFrom(address, (size_t)readSize);
DIAG(F("address=%x, nBytes=%d, readSize=%d"),address,nBytes,readSize);
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]);
while (Wire.available() && nBytes < readSize)
readBuffer[nBytes++] = Wire.read();
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
DIAG(F("status=%d"),status);
#endif
}
}
@ -231,7 +211,6 @@ uint8_t I2CManagerClass::read(I2CAddress address, uint8_t readBuffer[], uint8_t
* the non-blocking version.
***************************************************************************/
void I2CManagerClass::queueRequest(I2CRB *req) {
DIAG(F("I2CManagerClass::queueRequest() hit"));
if (req==NULL) {
DIAG(F("NOOOOOOOOOOO"));
return;

View File

@ -80,24 +80,19 @@ private:
void _begin() {
uint8_t status;
// Initialise EX-IOExander device
DIAG(F("EXIO begin()"));
I2CManager.begin();
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
// 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.
// Hence we can allocate I/O buffers from the stack.
uint8_t receiveBuffer[3];
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));
DIAG(F("EXIO status=%d"),status);
if (status == I2C_STATUS_OK) {
if (receiveBuffer[0] == EXIOPINS) {
_numDigitalPins = receiveBuffer[1];
_numAnaloguePins = receiveBuffer[2];
DIAG(F("EXIO dPins=%d, aPins=%d"),_numDigitalPins,_numAnaloguePins);
// See if we already have suitable buffers assigned
if (_numDigitalPins>0) {
@ -134,14 +129,12 @@ private:
// We now need to retrieve the analogue pin map if there are analogue pins
if (status == I2C_STATUS_OK && _numAnaloguePins>0) {
commandBuffer[0] = EXIOINITA;
DIAG(F("EXIOINITA"));
status = I2CManager.read(_I2CAddress, _analoguePinMap, _numAnaloguePins, commandBuffer, 1);
}
if (status == I2C_STATUS_OK) {
// Attempt to get version, if we don't get it, we don't care, don't go offline
uint8_t versionBuffer[3];
commandBuffer[0] = EXIOVER;
DIAG(F("EXIOVER"));
if (I2CManager.read(_I2CAddress, versionBuffer, sizeof(versionBuffer), commandBuffer, 1) == I2C_STATUS_OK) {
_majorVer = versionBuffer[0];
_minorVer = versionBuffer[1];
@ -252,7 +245,6 @@ private:
// Issue new read request for digital states. As the request is non-blocking, the buffer has to
// be allocated from heap (object state).
_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);
// non-blocking read
_lastDigitalRead = currentMicros;
@ -260,9 +252,8 @@ private:
} else if (currentMicros - _lastAnalogueRead > _analogueRefresh && _numAnaloguePins>0) { // Delay for analogue read refresh
// Issue new read for analogue input states
_readCommandBuffer[0] = EXIORDAN;
// DIAG(F("EXIORDAN address=%x, aBuffer=%d, bytes=%d"),_I2CAddress,_analogueInputBuffer,_numAnaloguePins*2);
// I2CManager.read(_I2CAddress, _analogueInputBuffer,
// _numAnaloguePins * 2, _readCommandBuffer, 1, &_i2crb);
I2CManager.read(_I2CAddress, _analogueInputBuffer,
_numAnaloguePins * 2, _readCommandBuffer, 1, &_i2crb);
_lastAnalogueRead = currentMicros;
_readState = RDS_ANALOGUE;
}