mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-24 08:36:14 +01:00
adjusted to allow user to decide which I2C to use
This commit is contained in:
parent
13bb1175f3
commit
22323ea065
|
@ -35,13 +35,21 @@
|
||||||
#define WIRE_HAS_TIMEOUT
|
#define WIRE_HAS_TIMEOUT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(GIGA_I2C_1)
|
||||||
|
#define DCCEX_WIRE Wire1
|
||||||
|
#else
|
||||||
|
#define DCCEX_WIRE Wire
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/***************************************************************************
|
/***************************************************************************
|
||||||
* Initialise I2C interface software
|
* Initialise I2C interface software
|
||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
void I2CManagerClass::_initialise() {
|
void I2CManagerClass::_initialise() {
|
||||||
Wire.begin();
|
DCCEX_WIRE.begin();
|
||||||
#if defined(WIRE_HAS_TIMEOUT)
|
#if defined(WIRE_HAS_TIMEOUT)
|
||||||
Wire.setWireTimeout(_timeout, true);
|
DCCEX_WIRE.setWireTimeout(_timeout, true);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +58,7 @@ void I2CManagerClass::_initialise() {
|
||||||
* on Arduino. Mega4809 supports 1000000 (Fast+) too.
|
* on Arduino. Mega4809 supports 1000000 (Fast+) too.
|
||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
void I2CManagerClass::_setClock(unsigned long i2cClockSpeed) {
|
void I2CManagerClass::_setClock(unsigned long i2cClockSpeed) {
|
||||||
Wire.setClock(i2cClockSpeed);
|
DCCEX_WIRE.setClock(i2cClockSpeed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/***************************************************************************
|
/***************************************************************************
|
||||||
|
@ -61,7 +69,7 @@ void I2CManagerClass::_setClock(unsigned long i2cClockSpeed) {
|
||||||
void I2CManagerClass::setTimeout(unsigned long value) {
|
void I2CManagerClass::setTimeout(unsigned long value) {
|
||||||
_timeout = value;
|
_timeout = value;
|
||||||
#if defined(WIRE_HAS_TIMEOUT)
|
#if defined(WIRE_HAS_TIMEOUT)
|
||||||
Wire.setWireTimeout(value, true);
|
DCCEX_WIRE.setWireTimeout(value, true);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +82,7 @@ static uint8_t muxSelect(I2CAddress address) {
|
||||||
I2CMux muxNo = address.muxNumber();
|
I2CMux muxNo = address.muxNumber();
|
||||||
I2CSubBus subBus = address.subBus();
|
I2CSubBus subBus = address.subBus();
|
||||||
if (muxNo != I2CMux_None) {
|
if (muxNo != I2CMux_None) {
|
||||||
Wire.beginTransmission(I2C_MUX_BASE_ADDRESS+muxNo);
|
DCCEX_WIRE.beginTransmission(I2C_MUX_BASE_ADDRESS+muxNo);
|
||||||
uint8_t data = (subBus == SubBus_All) ? 0xff :
|
uint8_t data = (subBus == SubBus_All) ? 0xff :
|
||||||
(subBus == SubBus_None) ? 0x00 :
|
(subBus == SubBus_None) ? 0x00 :
|
||||||
#if defined(I2CMUX_PCA9547)
|
#if defined(I2CMUX_PCA9547)
|
||||||
|
@ -86,8 +94,8 @@ static uint8_t muxSelect(I2CAddress address) {
|
||||||
// with a bit set for the subBus to be enabled
|
// with a bit set for the subBus to be enabled
|
||||||
1 << subBus;
|
1 << subBus;
|
||||||
#endif
|
#endif
|
||||||
Wire.write(&data, 1);
|
DCCEX_WIRE.write(&data, 1);
|
||||||
return Wire.endTransmission(true); // have to release I2C bus for it to work
|
return DCCEX_WIRE.endTransmission(true); // have to release I2C bus for it to work
|
||||||
}
|
}
|
||||||
return I2C_STATUS_OK;
|
return I2C_STATUS_OK;
|
||||||
}
|
}
|
||||||
|
@ -110,9 +118,9 @@ uint8_t I2CManagerClass::write(I2CAddress address, const uint8_t buffer[], uint8
|
||||||
#endif
|
#endif
|
||||||
// Only send new transaction if address is non-zero.
|
// Only send new transaction if address is non-zero.
|
||||||
if (muxStatus == I2C_STATUS_OK && address != 0) {
|
if (muxStatus == I2C_STATUS_OK && address != 0) {
|
||||||
Wire.beginTransmission(address);
|
DCCEX_WIRE.beginTransmission(address);
|
||||||
if (size > 0) Wire.write(buffer, size);
|
if (size > 0) DCCEX_WIRE.write(buffer, size);
|
||||||
status = Wire.endTransmission();
|
status = DCCEX_WIRE.endTransmission();
|
||||||
}
|
}
|
||||||
#ifdef I2C_EXTENDED_ADDRESS
|
#ifdef I2C_EXTENDED_ADDRESS
|
||||||
// Deselect MUX if there's more than one MUX present, to avoid having multiple ones selected
|
// Deselect MUX if there's more than one MUX present, to avoid having multiple ones selected
|
||||||
|
@ -161,25 +169,25 @@ uint8_t I2CManagerClass::read(I2CAddress address, uint8_t readBuffer[], uint8_t
|
||||||
// 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) {
|
||||||
if (writeSize > 0) {
|
if (writeSize > 0) {
|
||||||
Wire.beginTransmission(address);
|
DCCEX_WIRE.beginTransmission(address);
|
||||||
Wire.write(writeBuffer, writeSize);
|
DCCEX_WIRE.write(writeBuffer, writeSize);
|
||||||
status = Wire.endTransmission(false); // Don't free bus yet
|
status = DCCEX_WIRE.endTransmission(false); // Don't free bus yet
|
||||||
}
|
}
|
||||||
if (status == I2C_STATUS_OK) {
|
if (status == I2C_STATUS_OK) {
|
||||||
#ifdef WIRE_HAS_TIMEOUT
|
#ifdef WIRE_HAS_TIMEOUT
|
||||||
Wire.clearWireTimeoutFlag();
|
DCCEX_WIRE.clearWireTimeoutFlag();
|
||||||
Wire.requestFrom(address, (size_t)readSize);
|
DCCEX_WIRE.requestFrom(address, (size_t)readSize);
|
||||||
if (!Wire.getWireTimeoutFlag()) {
|
if (!DCCEX_WIRE.getWireTimeoutFlag()) {
|
||||||
while (Wire.available() && nBytes < readSize)
|
while (DCCEX_WIRE.available() && nBytes < readSize)
|
||||||
readBuffer[nBytes++] = Wire.read();
|
readBuffer[nBytes++] = DCCEX_WIRE.read();
|
||||||
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
|
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
|
||||||
} else {
|
} else {
|
||||||
status = I2C_STATUS_TIMEOUT;
|
status = I2C_STATUS_TIMEOUT;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
Wire.requestFrom(address, (size_t)readSize);
|
DCCEX_WIRE.requestFrom(address, (size_t)readSize);
|
||||||
while (Wire.available() && nBytes < readSize)
|
while (DCCEX_WIRE.available() && nBytes < readSize)
|
||||||
readBuffer[nBytes++] = Wire.read();
|
readBuffer[nBytes++] = DCCEX_WIRE.read();
|
||||||
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
|
if (nBytes < readSize) status = I2C_STATUS_TRUNCATED;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user