mboard_eeprom_t usrp1_impl::get_mb_eeprom(uhd::i2c_iface::sptr iface) { mboard_eeprom_t mb_eeprom; //extract the serial mb_eeprom["serial"] = uhd::bytes_to_string(iface->read_eeprom( USRP1_EEPROM_ADDR, offsetof(usrp1_eeprom_map, serial), USRP1_SERIAL_LEN )); //extract the name mb_eeprom["name"] = uhd::bytes_to_string(iface->read_eeprom( USRP1_EEPROM_ADDR, offsetof(usrp1_eeprom_map, name), NAME_MAX_LEN )); //extract master clock rate as a 32-bit uint in Hz uint32_t master_clock_rate; const byte_vector_t rate_bytes = iface->read_eeprom( USRP1_EEPROM_ADDR, offsetof(usrp1_eeprom_map, mcr), sizeof(master_clock_rate) ); std::copy( rate_bytes.begin(), rate_bytes.end(), //input reinterpret_cast<uint8_t *>(&master_clock_rate) //output ); master_clock_rate = ntohl(master_clock_rate); if (master_clock_rate > 1e6 and master_clock_rate < 1e9){ mb_eeprom["mcr"] = std::to_string(master_clock_rate); } else mb_eeprom["mcr"] = ""; return mb_eeprom; }
static void load_b000(mboard_eeprom_t &mb_eeprom, i2c_iface &iface){ //extract the serial mb_eeprom["serial"] = bytes_to_string(iface.read_eeprom( B000_EEPROM_ADDR, offsetof(b000_eeprom_map, serial), B000_SERIAL_LEN )); //extract the name mb_eeprom["name"] = bytes_to_string(iface.read_eeprom( B000_EEPROM_ADDR, offsetof(b000_eeprom_map, name), NAME_MAX_LEN )); //extract master clock rate as a 32-bit uint in Hz boost::uint32_t master_clock_rate; const byte_vector_t rate_bytes = iface.read_eeprom( B000_EEPROM_ADDR, offsetof(b000_eeprom_map, mcr), sizeof(master_clock_rate) ); std::copy( rate_bytes.begin(), rate_bytes.end(), //input reinterpret_cast<boost::uint8_t *>(&master_clock_rate) //output ); master_clock_rate = ntohl(master_clock_rate); if (master_clock_rate > 1e6 and master_clock_rate < 1e9){ mb_eeprom["mcr"] = boost::lexical_cast<std::string>(master_clock_rate); } else mb_eeprom["mcr"] = ""; }
//negative sum of bytes excluding checksum byte static uint8_t checksum(const byte_vector_t &bytes){ int sum = 0; for (size_t i = 0; i < std::min(bytes.size(), size_t(DB_EEPROM_CHKSUM)); i++){ sum -= int(bytes.at(i)); } UHD_LOG_TRACE("DB_EEPROM", boost::format("byte sum: 0x%02x") % sum) return uint8_t(sum); }
//negative sum of bytes excluding checksum byte static boost::uint8_t checksum(const byte_vector_t &bytes){ int sum = 0; for (size_t i = 0; i < std::min(bytes.size(), size_t(DB_EEPROM_CHKSUM)); i++){ sum -= int(bytes.at(i)); } UHD_LOGV(often) << boost::format("sum: 0x%02x") % sum << std::endl; return boost::uint8_t(sum); }
void write_i2c(boost::uint8_t addr, const byte_vector_t &bytes){ //allocate some memory for this transaction UHD_ASSERT_THROW(bytes.size() <= max_i2c_data_bytes); boost::uint8_t mem[sizeof(usrp_e_i2c) + max_i2c_data_bytes]; //load the data struct usrp_e_i2c *data = reinterpret_cast<usrp_e_i2c*>(mem); data->addr = addr; data->len = bytes.size(); std::copy(bytes.begin(), bytes.end(), data->data); //call the spi ioctl this->ioctl(USRP_E_I2C_WRITE, data); }
void write_i2c(boost::uint8_t addr, const byte_vector_t &bytes){ byte_vector_t rw_bytes(bytes); //setup the message i2c_msg msg; msg.addr = addr; msg.flags = 0; msg.len = bytes.size(); msg.buf = &rw_bytes.front(); //setup the data i2c_rdwr_ioctl_data data; data.msgs = &msg; data.nmsgs = 1; //call the ioctl UHD_ASSERT_THROW(::ioctl(_node_fd, I2C_RDWR, &data) >= 0); }
//! convert a byte vector read from eeprom to a string static std::string uint16_bytes_to_string(const byte_vector_t &bytes){ const boost::uint16_t num = (boost::uint16_t(bytes.at(0)) << 0) | (boost::uint16_t(bytes.at(1)) << 8); return (num == 0 or num == 0xffff)? "" : boost::lexical_cast<std::string>(num); }