diff --git a/include/pflib/lpGBT.h b/include/pflib/lpGBT.h index ec969789b..b4f378ce4 100644 --- a/include/pflib/lpGBT.h +++ b/include/pflib/lpGBT.h @@ -190,6 +190,14 @@ class lpGBT { bool strong_scl = true, bool strong_sda = true, bool pull_up_scl = false, bool pull_up_sda = false); + /** Setup i2c bus speed + \param ibus Which I2C bus (0-2) + \param speed_khz I2C speed (appropriate values are 100, 200, 400, 1000) + Note that this really just stores the information on what speed to use in + a temporary variable + */ + void setup_i2c_speed(int ibus, int speed_khz); + /** Start an I2C read */ void start_i2c_read(int ibus, uint8_t i2c_addr, int len = 1); diff --git a/src/pflib/Bias.cxx b/src/pflib/Bias.cxx index 45248a573..69d0346ee 100644 --- a/src/pflib/Bias.cxx +++ b/src/pflib/Bias.cxx @@ -25,6 +25,7 @@ MAX5825::MAX5825(std::shared_ptr i2c, uint8_t addr) std::vector MAX5825::get(uint8_t channel) { uint8_t cmd = (uint8_t)(MAX5825::CODEn | (channel & 0x07)); + i2c_->set_bus_speed(100); std::vector retval = i2c_->general_write_read(our_addr_, {cmd}, 2); return retval; @@ -33,6 +34,7 @@ std::vector MAX5825::get(uint8_t channel) { void MAX5825::set(uint8_t channel, uint16_t code) { uint8_t cmd = (uint8_t)(0xB0 | (channel & 0x07)); + i2c_->set_bus_speed(100); std::vector retval = i2c_->general_write_read(our_addr_, {cmd, static_cast((code << 4) >> 8), @@ -58,18 +60,21 @@ Bias::Bias(std::shared_ptr i2c_bias, std::shared_ptr i2c_board) { void Bias::initialize() { // Reset all DAC:s + i2c_bias_->set_bus_speed(100); i2c_bias_->general_write_read(0x10, {0x35, 0x96, 0x30}, 0); i2c_bias_->general_write_read(0x12, {0x35, 0x96, 0x30}, 0); i2c_bias_->general_write_read(0x14, {0x35, 0x96, 0x30}, 0); i2c_bias_->general_write_read(0x18, {0x35, 0x96, 0x30}, 0); // Set internal ref on DAC to 4.096 V + i2c_bias_->set_bus_speed(100); i2c_bias_->general_write_read(0x10, {0x27, 0x00, 0x00}, 0); i2c_bias_->general_write_read(0x12, {0x27, 0x00, 0x00}, 0); i2c_bias_->general_write_read(0x14, {0x27, 0x00, 0x00}, 0); i2c_bias_->general_write_read(0x18, {0x27, 0x00, 0x00}, 0); // Set up the GPIO device MCP23008 + i2c_board_->set_bus_speed(100); i2c_board_->general_write_read(0x20, {0x00, 0x70}, 0); // Turn on the status LED @@ -88,6 +93,7 @@ void Bias::initialize() { } double Bias::readTemp() { + i2c_board_->set_bus_speed(100); i2c_board_->general_write_read(0x4A, {0x00}, 0); usleep(250); // Response is a bit slow std::vector ret = i2c_board_->general_write_read(0x4A, {}, 2); diff --git a/src/pflib/lpGBT.cxx b/src/pflib/lpGBT.cxx index 7f401f0af..fe0806048 100644 --- a/src/pflib/lpGBT.cxx +++ b/src/pflib/lpGBT.cxx @@ -594,6 +594,15 @@ void lpGBT::setup_i2c(int ibus, int speed_khz, bool scl_drive, bool strong_scl, if (speed_khz > 500 && speed_khz < 2000) i2c_[ibus].ctl_reg |= 0x03; } +void lpGBT::setup_i2c_speed(int ibus, int speed_khz) { + if (ibus < 0 || ibus > 2) return; + + i2c_[ibus].ctl_reg &= 0x80; // keep the scl_drive + if (speed_khz > 125 && speed_khz < 300) i2c_[ibus].ctl_reg |= 0x01; + if (speed_khz > 300 && speed_khz < 500) i2c_[ibus].ctl_reg |= 0x02; + if (speed_khz > 500 && speed_khz < 2000) i2c_[ibus].ctl_reg |= 0x03; +} + static constexpr uint8_t CMD_I2C_WRITE_CR = 0; static constexpr uint8_t CMD_I2C_1BYTE_WRITE = 2; static constexpr uint8_t CMD_I2C_1BYTE_READ = 3; diff --git a/src/pflib/lpgbt/I2C.cxx b/src/pflib/lpgbt/I2C.cxx index 2aa7169a2..3590165c7 100644 --- a/src/pflib/lpgbt/I2C.cxx +++ b/src/pflib/lpgbt/I2C.cxx @@ -5,7 +5,7 @@ namespace lpgbt { void I2C::set_bus_speed(int speed) { ispeed_ = speed; - lpgbt_.setup_i2c(ibus_, speed); + lpgbt_.setup_i2c_speed(ibus_, speed); } int I2C::get_bus_speed() { return ispeed_; }