Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
*
* @param inputMultiplexer the input multiplexer
*/
public void setInputMultiplexer(int inputMultiplexer) {
if (inputMultiplexer < 0 || inputMultiplexer > 0b1110) {
throw new IllegalArgumentException(
"Invalid input multiplexer value: " + inputMultiplexer + " must be 0..14");
}
this.inputMultiplexer = (byte) (inputMultiplexer & 0xf);
setConfig0();
}
public GainConfig getGainConfig() {
return gainConfig;
}
public void setGainConfig(GainConfig gainConfig) {
this.gainConfig = gainConfig;
setConfig0();
}
public Pga getPga() {
return pga;
}
public void setPga(Pga pga) {
this.pga = pga;
setConfig0();
}
public DataRate getDataRate() {
return dataRate;
}
public void setDataRate(DataRate dataRate) {
this.dataRate = dataRate;
setConfig1();
}
public boolean isTurboModeEnabled() {
return operatingMode == OperatingMode.TURBO;
}
public void setTurboModeEnabled(boolean enabled) {
this.operatingMode = enabled ? OperatingMode.TURBO : OperatingMode.NORMAL;
setConfig1();
}
public int getDataRateFrequency() {
return dataRate.getDataRate() * operatingMode.getMultiplier();
}
public VRef getVRef() {
return vRef;
}
public void setVRef(VRef vRef) {
this.vRef = vRef;
setConfig1();
}
public boolean isTemperatureSensorModeEnabled() {
return tsMode.isEnabled();
}
public void setTemperatureSensorModeEnabled(boolean enabled) {
this.tsMode = enabled ? TemperatureSensorMode.ENABLED : TemperatureSensorMode.DISABLED;
setConfig1();
}
public boolean isDataCounterEnabled() {
return dataCounter.isEnabled();
}
public void setDataCounterEnabled(boolean enabled) {
this.dataCounter = enabled ? DataCounter.ENABLED : DataCounter.DISABLED;
setConfig2();
}
public CrcConfig getCrcConfig() {
return crcConfig;
}
public void setCrcConfig(CrcConfig crcConfig) {
this.crcConfig = crcConfig;
setConfig2();
}
public BurnoutCurrentSources getBurnoutCurrentSources() {
return burnoutCurrentSources;
}
public void setBurnoutCurrentSources(BurnoutCurrentSources burnoutCurrentSources) {
this.burnoutCurrentSources = burnoutCurrentSources;
setConfig2();
}
public IdacCurrent getIdacCurrent() {
return idacCurrent;
}
public void setIdacCurrent(IdacCurrent idacCurrent) {
this.idacCurrent = idacCurrent;
setConfig2();
}
public Idac1RoutingConfig getIdac1RoutingConfig() {
return idac1RoutingConfig;
}
public void setIdac1RoutingConfig(Idac1RoutingConfig idac1RoutingConfig) {
this.idac1RoutingConfig = idac1RoutingConfig;
setConfig3();
}
public Idac2RoutingConfig getIdac2RoutingConfig() {
return idac2RoutingConfig;
}
public void setIdac2RoutingConfig(Idac2RoutingConfig idac2RoutingConfig) {
this.idac2RoutingConfig = idac2RoutingConfig;
setConfig3();
}
public void setConfig0(GainConfig gainConfig, Pga pga) {
this.gainConfig = gainConfig;
this.pga = pga;
setConfig0();
}
public void setConfig1(DataRate dataRate, boolean turboModeEnabled, VRef vRef, boolean temperatureSensorEnabled) {
this.dataRate = dataRate;
this.operatingMode = turboModeEnabled ? OperatingMode.TURBO : OperatingMode.NORMAL;
this.vRef = vRef;
this.tsMode = temperatureSensorEnabled ? TemperatureSensorMode.ENABLED : TemperatureSensorMode.DISABLED;
setConfig1();
}
public void setConfig2(boolean dataCounterEnabled, CrcConfig crcConfig, BurnoutCurrentSources burnoutCurrentSources,
IdacCurrent idacCurrent) {
this.dataCounter = dataCounterEnabled ? DataCounter.ENABLED : DataCounter.DISABLED;
this.crcConfig = crcConfig;
this.burnoutCurrentSources = burnoutCurrentSources;
this.idacCurrent = idacCurrent;
setConfig2();
}
public void setConfig3(Idac1RoutingConfig idac1RoutingConfig, Idac2RoutingConfig idac2RoutingConfig) {
this.idac1RoutingConfig = idac1RoutingConfig;
this.idac2RoutingConfig = idac2RoutingConfig;
setConfig3();
}
public void setSingleShotMode() {
// System.out.println("getValueSingle");
conversionMode = ConversionMode.SINGLE_SHOT;
setConfig1();
// Start command must be issued each time the CM bit is changed
start();
}
public short getSingleShotReading(int adcNumber) {
if (adcNumber < 0 || adcNumber >= NUM_ADC_CHANNELS) {
throw new IllegalArgumentException("Invalid input channel number - " + adcNumber);
}
inputMultiplexer = (byte) ((0b1000 + adcNumber) << C0_MUX_BIT_START);
setConfig0();
// Must issue a start command to trigger a new reading
start();
return method1 ? getReadingOnDataReadyBit() : getReadingOnDataReadyBit2();
}
public void setContinuousMode(int adcNumber) {
inputMultiplexer = (byte) ((0b1000 + adcNumber) << C0_MUX_BIT_START);
setConfig0();
conversionMode = ConversionMode.CONTINUOUS;
setConfig1();
start();
}
/**
* Read data whenever the data read bit is set in Config Register #2
*
* @return the raw analog data reading in signed short format
*/
public short getReadingOnDataReadyBit() {
/*-
* DC enabled and CRC disabled is 3 bytes (1 DC, 2 data)
* DC enabled and CRC enabled is 5 bytes (1 DC, 2 data, 2 CRC).
* DC disabled and CRC enabled is 4 bytes (2 data, 2 CRC).
* DC enabled and CRC inverted is 6 bytes (1 DC, 2 data, 1 DC inv. and 2 data inv.).
* DC disabled and CRC inverted is 4 bytes (2 data, 2 data inv.).
*/
int bytes_to_read = 2;
if (dataCounter.isEnabled()) {
bytes_to_read++;
}
if (crcConfig != CrcConfig.DISABLED) {
bytes_to_read += 2;
if (dataCounter.isEnabled() && crcConfig == CrcConfig.INVERTED_DATA_OUTPUT) {
bytes_to_read++;
}
}
// Logger.debug("Waiting for data to be available...");
// Wait for the Data Ready bit to be set in config register #2
while (true) {
if ((readConfigRegister(ConfigRegister._2) & C2_DATA_RDY_MASK) != 0) {
break;
}
// 100 nS
SleepUtil.busySleep(100);
}
// Logger.debug("Data available");
// SleepUtil.sleepMillis(2);
/*-
byte[] buffer = device.readI2CBlockDataByteArray(COMMAND_RDATA, bytes_to_read);
Logger.debug("Read {} bytes:", Integer.valueOf(buffer.length));
*/
byte[] buffer = new byte[1 + bytes_to_read];
// device.readNoStop(COMMAND_RDATA, bytes_to_read, buffer, repeatedStart);
buffer[0] = COMMAND_RDATA;
I2CMessage messages[] = new I2CMessage[2];
messages[0] = new I2CMessage(I2CMessage.I2C_M_WR, 1);
messages[1] = new I2CMessage(I2CMessage.I2C_M_RD, bytes_to_read);
device.readWrite(messages, buffer);
if (Logger.isTraceEnabled()) {
Hex.dumpByteArray(buffer);
}
ByteBuffer bb = ByteBuffer.wrap(buffer, 1, bytes_to_read);
bb.order(ByteOrder.BIG_ENDIAN);
int counter = -1;
if (dataCounter.isEnabled()) {
counter = bb.get() & 0xff;
Logger.debug("Conversion counter: {}", Integer.valueOf(counter));
}
short value = bb.getShort();
if (crcConfig != CrcConfig.DISABLED) {
// Validate the CRC value
if (crcConfig == CrcConfig.INVERTED_DATA_OUTPUT) {
// A bitwise-inverted version of the data
if (dataCounter.isEnabled()) {
int counter_inverted = bb.get() & 0xff;
int calc_counter_inverted = ~counter & 0xff;
if (calc_counter_inverted != counter_inverted) {
Logger.warn("Inversion error for counter {}, calculated {}, got {}", Integer.valueOf(counter),
Integer.valueOf(calc_counter_inverted), Integer.valueOf(counter_inverted));
}
}
short value_inverted = bb.getShort();
short calc_val_inverted = (short) (~value);
if (calc_val_inverted != value_inverted) {
Logger.warn("Inversion error for data {}, calculated {}, got {}. DC Enabled: {}{}",
Short.valueOf(value), Short.valueOf(calc_val_inverted), Short.valueOf(value_inverted),
Boolean.valueOf(dataCounter.isEnabled()), dataCounter.isEnabled() ? " - " + counter : "");
}
} else if (crcConfig == CrcConfig.CRC16) {
int crc_val = bb.getShort() & 0xffff;
// In CRC mode, the checksum bytes are the 16-bit remainder of the bitwise
// exclusive-OR (XOR) of the data bytes with a CRC polynomial
/*-
* The CRC is "for the entire data being returned"
* i.e. includes the data counter if present
* https://e2e.ti.com/support/data-converters/f/73/t/758829
* From the datasheet:
* The optional data counter word that precedes conversion data is covered by both data
* integrity options.
*/
int calc_crc_val;
if (dataCounter.isEnabled()) {
calc_crc_val = Crc.crc16(CRC_PARAMS, (byte) counter, (byte) (value >> 8), (byte) value);
} else {
calc_crc_val = Crc.crc16Short(CRC_PARAMS, value);
}
if (calc_crc_val != crc_val) {
Logger.warn("CRC error for value {}, calculated {}, got {}. DC Enabled: {}{}", Short.valueOf(value),
Integer.valueOf((calc_crc_val)), Integer.valueOf(crc_val),
Boolean.valueOf(dataCounter.isEnabled()), dataCounter.isEnabled() ? " - " + counter : "");
}
}
}
return value;
}
/**
* Read data whenever the data read bit is set in Config Register #2
*
* @return the raw analog data reading in signed short format
*/
public short getReadingOnDataReadyBit2() {
/*-
* DC disabled and CRC disabled is 2 bytes (2 data)
* DC enabled and CRC disabled is 3 bytes (1 DC, 2 data)
* DC enabled and CRC enabled is 5 bytes (1 DC, 2 data, 2 CRC).
* DC disabled and CRC enabled is 4 bytes (2 data, 2 CRC).
* DC enabled and CRC inverted is 6 bytes (1 DC, 2 data, 1 DC inv. and 2 data inv.).
* DC disabled and CRC inverted is 4 bytes (2 data, 2 data inv.).
*/
int bytes_to_read = 2;
if (dataCounter.isEnabled()) {
// The data counter prefix
bytes_to_read++;
}
if (crcConfig != CrcConfig.DISABLED) {
// The data integrity check for data
bytes_to_read += 2;
if (dataCounter.isEnabled() && crcConfig == CrcConfig.INVERTED_DATA_OUTPUT) {
// The inverted data counter
bytes_to_read++;
}
}
I2CDeviceInterface.I2CMessage[] messages = { //
new I2CDeviceInterface.I2CMessage(I2CDeviceInterface.I2CMessage.I2C_M_WR, 1), // Write config register 2
new I2CDeviceInterface.I2CMessage(I2CDeviceInterface.I2CMessage.I2C_M_RD, 1), // Read the value
new I2CDeviceInterface.I2CMessage(I2CDeviceInterface.I2CMessage.I2C_M_WR, 1), // Write data register
new I2CDeviceInterface.I2CMessage(I2CDeviceInterface.I2CMessage.I2C_M_RD, bytes_to_read) // Read the
// value
};
// WRITE Config Reg 2 Addr, READ Config Reg 2 val, WRITE RDATA Addr, READ RDATA
// value
byte[] buffer = new byte[3 + bytes_to_read];
// Write Config Register #2
buffer[0] = (byte) (COMMAND_READ_REG | ConfigRegister._2.getMask());
// Placeholder for Config Register #2 data
buffer[1] = 0;
// Write Data Register
buffer[2] = COMMAND_RDATA;
// Buffer 3..end RDATA value
// Wait for the Data Ready bit to be set in config register #2
// Logger.debug("Waiting for data to be available...");
while (true) {
device.readWrite(messages, buffer);
if ((buffer[1] & C2_DATA_RDY_MASK) != 0) {
break;
}
// 100 nS before trying again
SleepUtil.busySleep(100);
}
// Logger.debug("Data available");
if (Logger.isTraceEnabled()) {
Hex.dumpByteArray(buffer);
}
ByteBuffer bb = ByteBuffer.wrap(buffer, 3, bytes_to_read);
bb.order(ByteOrder.BIG_ENDIAN);
int counter = -1;
if (dataCounter.isEnabled()) {
counter = bb.get() & 0xff;
Logger.debug("Conversion counter: {}", Integer.valueOf(counter));
}
short value = bb.getShort();
if (crcConfig != CrcConfig.DISABLED) {
// Validate the CRC value
if (crcConfig == CrcConfig.INVERTED_DATA_OUTPUT) {
// A bitwise-inverted version of the data
if (dataCounter.isEnabled()) {
int counter_inverted = bb.get() & 0xff;
int calc_counter_inverted = ~counter & 0xff;
if (calc_counter_inverted != counter_inverted) {
Logger.warn("Inversion error for counter {}, calculated {}, got {}", Integer.valueOf(counter),
Integer.valueOf(calc_counter_inverted), Integer.valueOf(counter_inverted));
}
}
short value_inverted = bb.getShort();
short calc_val_inverted = (short) (~value);
if (calc_val_inverted != value_inverted) {
Logger.warn("Inversion error for data {}, calculated {}, got {}. DC Enabled: {}{}",
Short.valueOf(value), Short.valueOf(calc_val_inverted), Short.valueOf(value_inverted),
Boolean.valueOf(dataCounter.isEnabled()), dataCounter.isEnabled() ? " - " + counter : "");
}
} else if (crcConfig == CrcConfig.CRC16) {
int crc_val = bb.getShort() & 0xffff;
// In CRC mode, the checksum bytes are the 16-bit remainder of the bitwise
// exclusive-OR (XOR) of the data bytes with a CRC polynomial
/*-
* The CRC is "for the entire data being returned"
* i.e. includes the data counter if present
* https://e2e.ti.com/support/data-converters/f/73/t/758829
* From the datasheet:
* The optional data counter word that precedes conversion data is covered by both data
* integrity options.
*/
int calc_crc_val;
if (dataCounter.isEnabled()) {
calc_crc_val = Crc.crc16(CRC_PARAMS, (byte) counter, (byte) (value >> 8), (byte) value);
} else {
calc_crc_val = Crc.crc16Short(CRC_PARAMS, value);
}
if (calc_crc_val != crc_val) {
Logger.warn("CRC error for value {}, calculated {}, got {}. DC Enabled: {}{}", Short.valueOf(value),
Integer.valueOf((calc_crc_val)), Integer.valueOf(crc_val),
Boolean.valueOf(dataCounter.isEnabled()), dataCounter.isEnabled() ? " - " + counter : "");
}
}
}
return value;
}
public short getReadingOnDataCounterChange() {
// Data counter must be available for this method to work
if (dataCounter == DataCounter.DISABLED) {
throw new IllegalArgumentException("Data counter must be enabled");
}
byte[] buffer;
switch (crcConfig) {
case CRC16:
buffer = new byte[5];
break;
case INVERTED_DATA_OUTPUT:
buffer = new byte[6];
break;
case DISABLED:
default:
buffer = new byte[3];
}
short value;
while (true) {
device.readI2CBlockData(COMMAND_RDATA, buffer);
int new_dc = buffer[0] & 0xff;
if (new_dc != lastDataCounter) {
if (lastDataCounter != -1 && (new_dc != lastDataCounter + 1)) {
Logger.info("Missed a reading - last DC: {}, new DC: {}", Integer.valueOf(lastDataCounter),
Integer.valueOf(new_dc));
}
lastDataCounter = new_dc;
// TODO If DI is set to inverted, buffer[1] is the inversion of the DC
value = (short) ((buffer[1] << 8) | (buffer[2] & 0xff));
// TODO Data Integrity validation
break;
}
SleepUtil.busySleep(100);
}
return value;
}
}