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,
|
||||
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,22 @@ 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);
|
||||
while (Wire.available() && nBytes < readSize)
|
||||
readBuffer[nBytes++] = Wire.read();
|
||||
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]);
|
||||
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
|
||||
DIAG(F("status=%d"),status);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -211,6 +231,7 @@ 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;
|
||||
|
|
|
@ -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) {
|
||||
|
@ -129,12 +134,14 @@ 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];
|
||||
|
@ -245,6 +252,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,8 +260,9 @@ private:
|
|||
} else if (currentMicros - _lastAnalogueRead > _analogueRefresh && _numAnaloguePins>0) { // Delay for analogue read refresh
|
||||
// Issue new read for analogue input states
|
||||
_readCommandBuffer[0] = EXIORDAN;
|
||||
I2CManager.read(_I2CAddress, _analogueInputBuffer,
|
||||
_numAnaloguePins * 2, _readCommandBuffer, 1, &_i2crb);
|
||||
// DIAG(F("EXIORDAN address=%x, aBuffer=%d, bytes=%d"),_I2CAddress,_analogueInputBuffer,_numAnaloguePins*2);
|
||||
// I2CManager.read(_I2CAddress, _analogueInputBuffer,
|
||||
// _numAnaloguePins * 2, _readCommandBuffer, 1, &_i2crb);
|
||||
_lastAnalogueRead = currentMicros;
|
||||
_readState = RDS_ANALOGUE;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user