Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/pflib/lpGBT.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions src/pflib/Bias.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ MAX5825::MAX5825(std::shared_ptr<I2C> i2c, uint8_t addr)
std::vector<uint8_t> MAX5825::get(uint8_t channel) {
uint8_t cmd = (uint8_t)(MAX5825::CODEn | (channel & 0x07));

i2c_->set_bus_speed(100);
std::vector<uint8_t> retval = i2c_->general_write_read(our_addr_, {cmd}, 2);

return retval;
Expand All @@ -33,6 +34,7 @@ std::vector<uint8_t> 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<uint8_t> retval =
i2c_->general_write_read(our_addr_,
{cmd, static_cast<uint8_t>((code << 4) >> 8),
Expand All @@ -58,18 +60,21 @@ Bias::Bias(std::shared_ptr<I2C> i2c_bias, std::shared_ptr<I2C> 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
Expand All @@ -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<uint8_t> ret = i2c_board_->general_write_read(0x4A, {}, 2);
Expand Down
9 changes: 9 additions & 0 deletions src/pflib/lpGBT.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/pflib/lpgbt/I2C.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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_; }
Expand Down