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

More diags

This commit is contained in:
peteGSX 2024-01-17 08:09:45 +10:00
parent c2bd012164
commit 5579a9aa49
2 changed files with 22 additions and 2 deletions

View File

@ -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,
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 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()) {
@ -178,10 +186,14 @@ 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);
while (Wire.available() && nBytes < readSize)
readBuffer[nBytes++] = Wire.read();
DIAG(F("nBytes=%d,readBuffer[nBytes]=%d"),nBytes-1,readBuffer[nBytes-1]);
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
DIAG(F("status=%d"),status);
#endif
}
}
@ -215,7 +227,7 @@ void I2CManagerClass::queueRequest(I2CRB *req) {
DIAG(F("NOOOOOOOOOOO"));
return;
}
req->dump();
// req->dump();
switch (req->operation & OPERATION_MASK) {
case OPERATION_READ:
read(req->i2cAddress, req->readBuffer, req->readLen, NULL, 0, req);

View File

@ -80,19 +80,24 @@ 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) {
@ -208,6 +213,7 @@ private:
// Main loop, collect both digital and analogue pin states continuously (faster sensor/input reads)
void _loop(unsigned long currentMicros) override {
DIAG(F("EXIO _loop()"));
if (_deviceState == DEVSTATE_FAILED) return; // If device failed, return
// Request block is used for analogue and digital reads from the IOExpander, which are performed
@ -245,6 +251,7 @@ 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;
@ -252,6 +259,7 @@ 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);
_lastAnalogueRead = currentMicros;