lib/usrp/common/fifo_ctrl_excelsior.cpp
/******************************************************************* * Peek and poke 32 bit implementation ******************************************************************/ void poke32(wb_addr_type addr, boost::uint32_t data){ boost::mutex::scoped_lock lock(_mutex); this->send_pkt(addr, data, POKE32_CMD);//调用send_pkt,打包成VRT context包 this->wait_for_ack(_seq_out-MAX_SEQS_OUT); } boost::uint32_t peek32(wb_addr_type addr){ boost::mutex::scoped_lock lock(_mutex); this->send_pkt(addr, 0, PEEK32_CMD); return this->wait_for_ack(_seq_out); }
/******************************************************************* * FIFO controlled SPI implementation ******************************************************************/ void init_spi(void){ boost::mutex::scoped_lock lock(_mutex); this->send_pkt(SPI_DIV, SPI_DIVIDER, POKE32_CMD); this->wait_for_ack(_seq_out-MAX_SEQS_OUT); _ctrl_word_cache = 0; // force update first time around } boost::uint32_t transact_spi( int which_slave, const spi_config_t &config, boost::uint32_t data, size_t num_bits, bool readback ){ boost::mutex::scoped_lock lock(_mutex); //load control word boost::uint32_t ctrl_word = 0; ctrl_word |= ((which_slave & 0xffffff) << 0); ctrl_word |= ((num_bits & 0x3ff) << 24); if (config.mosi_edge == spi_config_t::EDGE_FALL) ctrl_word |= (1 << 31); if (config.miso_edge == spi_config_t::EDGE_RISE) ctrl_word |= (1 << 30); //load data word (must be in upper bits) const boost::uint32_t data_out = data << (32 - num_bits); //conditionally send control word if (_ctrl_word_cache != ctrl_word){ this->send_pkt(SPI_CTRL, ctrl_word, POKE32_CMD); this->wait_for_ack(_seq_out-MAX_SEQS_OUT); _ctrl_word_cache = ctrl_word; } //send data word this->send_pkt(SPI_DATA, data_out, POKE32_CMD); this->wait_for_ack(_seq_out-MAX_SEQS_OUT); //conditional readback if (readback){ this->send_pkt(_config.spi_rb, 0, PEEK32_CMD); return this->wait_for_ack(_seq_out); } return 0; }
/******************************************************************* * Primary control and interaction private methods ******************************************************************/ UHD_INLINE void send_pkt(wb_addr_type addr, boost::uint32_t data, int cmd){ managed_send_buffer::sptr buff = _xport->get_send_buff(); if (not buff){ throw uhd::runtime_error("fifo ctrl timed out getting a send buffer"); } boost::uint32_t *pkt = buff->cast<boost::uint32_t *>(); //load packet info,打包成VRT协议包 vrt::if_packet_info_t packet_info; packet_info.packet_type = vrt::if_packet_info_t::PACKET_TYPE_CONTEXT; packet_info.num_payload_words32 = 2; packet_info.num_payload_bytes = packet_info.num_payload_words32*sizeof(boost::uint32_t); packet_info.packet_count = ++_seq_out; packet_info.tsf = _time.to_ticks(_tick_rate); packet_info.sob = false; packet_info.eob = false; packet_info.has_sid = false; packet_info.has_cid = false; packet_info.has_tsi = false; packet_info.has_tsf = _use_time; packet_info.has_tlr = false; //load header vrt::if_hdr_pack_le(pkt, packet_info); //load payload const boost::uint32_t ctrl_word = (addr/4 & 0xff) | cmd | (_seq_out << 16); pkt[packet_info.num_header_words32+0] = uhd::htowx(ctrl_word); pkt[packet_info.num_header_words32+1] = uhd::htowx(data); //send the buffer over the interface buff->commit(sizeof(boost::uint32_t)*(packet_info.num_packet_words32)); }
usrp2_iface.cpp:
/*********************************************************************** * Peek and Poke **********************************************************************/ void poke32(wb_addr_type addr, boost::uint32_t data){ this->get_reg<boost::uint32_t, USRP2_REG_ACTION_FPGA_POKE32>(addr, data); } boost::uint32_t peek32(wb_addr_type addr){ return this->get_reg<boost::uint32_t, USRP2_REG_ACTION_FPGA_PEEK32>(addr); } void poke16(wb_addr_type addr, boost::uint16_t data){ this->get_reg<boost::uint16_t, USRP2_REG_ACTION_FPGA_POKE16>(addr, data); } boost::uint16_t peek16(wb_addr_type addr){ return this->get_reg<boost::uint16_t, USRP2_REG_ACTION_FPGA_PEEK16>(addr); } void pokefw(wb_addr_type addr, boost::uint32_t data) { this->get_reg<boost::uint32_t, USRP2_REG_ACTION_FW_POKE32>(addr, data); } boost::uint32_t peekfw(wb_addr_type addr) { return this->get_reg<boost::uint32_t, USRP2_REG_ACTION_FW_PEEK32>(addr); } template <class T, usrp2_reg_action_t action> T get_reg(wb_addr_type addr, T data = 0){ //setup the out data usrp2_ctrl_data_t out_data = usrp2_ctrl_data_t(); out_data.id = htonl(USRP2_CTRL_ID_GET_THIS_REGISTER_FOR_ME_BRO); out_data.data.reg_args.addr = htonl(addr); out_data.data.reg_args.data = htonl(boost::uint32_t(data)); out_data.data.reg_args.action = action; //send and recv usrp2_ctrl_data_t in_data = this->ctrl_send_and_recv(out_data, MIN_PROTO_COMPAT_REG); UHD_ASSERT_THROW(ntohl(in_data.id) == USRP2_CTRL_ID_OMG_GOT_REGISTER_SO_BAD_DUDE); return T(ntohl(in_data.data.reg_args.data)); } /*********************************************************************** * SPI **********************************************************************/ boost::uint32_t transact_spi( int which_slave, const spi_config_t &config, boost::uint32_t data, size_t num_bits, bool readback ){ static const uhd::dict<spi_config_t::edge_t, int> spi_edge_to_otw = boost::assign::map_list_of (spi_config_t::EDGE_RISE, USRP2_CLK_EDGE_RISE) (spi_config_t::EDGE_FALL, USRP2_CLK_EDGE_FALL) ; //setup the out data usrp2_ctrl_data_t out_data = usrp2_ctrl_data_t(); out_data.id = htonl(USRP2_CTRL_ID_TRANSACT_ME_SOME_SPI_BRO); out_data.data.spi_args.dev = htonl(which_slave); out_data.data.spi_args.miso_edge = spi_edge_to_otw[config.miso_edge]; out_data.data.spi_args.mosi_edge = spi_edge_to_otw[config.mosi_edge]; out_data.data.spi_args.readback = (readback)? 1 : 0; out_data.data.spi_args.num_bits = num_bits; out_data.data.spi_args.data = htonl(data); //send and recv usrp2_ctrl_data_t in_data = this->ctrl_send_and_recv(out_data, MIN_PROTO_COMPAT_SPI); UHD_ASSERT_THROW(ntohl(in_data.id) == USRP2_CTRL_ID_OMG_TRANSACTED_SPI_DUDE); return ntohl(in_data.data.spi_args.data); } /*********************************************************************** * I2C **********************************************************************/ void write_i2c(boost::uint16_t addr, const byte_vector_t &buf){ //setup the out data usrp2_ctrl_data_t out_data = usrp2_ctrl_data_t(); out_data.id = htonl(USRP2_CTRL_ID_WRITE_THESE_I2C_VALUES_BRO); out_data.data.i2c_args.addr = addr; out_data.data.i2c_args.bytes = buf.size(); //limitation of i2c transaction size UHD_ASSERT_THROW(buf.size() <= sizeof(out_data.data.i2c_args.data)); //copy in the data std::copy(buf.begin(), buf.end(), out_data.data.i2c_args.data); //send and recv usrp2_ctrl_data_t in_data = this->ctrl_send_and_recv(out_data, MIN_PROTO_COMPAT_I2C); UHD_ASSERT_THROW(ntohl(in_data.id) == USRP2_CTRL_ID_COOL_IM_DONE_I2C_WRITE_DUDE); } byte_vector_t read_i2c(boost::uint16_t addr, size_t num_bytes){ //setup the out data usrp2_ctrl_data_t out_data = usrp2_ctrl_data_t(); out_data.id = htonl(USRP2_CTRL_ID_DO_AN_I2C_READ_FOR_ME_BRO); out_data.data.i2c_args.addr = addr; out_data.data.i2c_args.bytes = num_bytes; //limitation of i2c transaction size UHD_ASSERT_THROW(num_bytes <= sizeof(out_data.data.i2c_args.data)); //send and recv usrp2_ctrl_data_t in_data = this->ctrl_send_and_recv(out_data, MIN_PROTO_COMPAT_I2C); UHD_ASSERT_THROW(ntohl(in_data.id) == USRP2_CTRL_ID_HERES_THE_I2C_DATA_DUDE); UHD_ASSERT_THROW(in_data.data.i2c_args.addr = num_bytes); //copy out the data byte_vector_t result(num_bytes); std::copy(in_data.data.i2c_args.data, in_data.data.i2c_args.data + num_bytes, result.begin()); return result; }
时间: 2024-10-31 10:21:38