diff options
Diffstat (limited to 'usrp2/host')
-rw-r--r-- | usrp2/host/include/usrp2/usrp2.h | 13 | ||||
-rw-r--r-- | usrp2/host/lib/control.h | 7 | ||||
-rw-r--r-- | usrp2/host/lib/usrp2.cc | 6 | ||||
-rw-r--r-- | usrp2/host/lib/usrp2_impl.cc | 63 | ||||
-rw-r--r-- | usrp2/host/lib/usrp2_impl.h | 3 |
5 files changed, 92 insertions, 0 deletions
diff --git a/usrp2/host/include/usrp2/usrp2.h b/usrp2/host/include/usrp2/usrp2.h index ed114699c..82b1c6449 100644 --- a/usrp2/host/include/usrp2/usrp2.h +++ b/usrp2/host/include/usrp2/usrp2.h @@ -375,6 +375,19 @@ namespace usrp2 { */ std::vector<uint32_t> peek32(uint32_t addr, uint32_t words); + /*! + * Write memory to Wishbone bus as 32-bit words. Handles endian swapping if needed. + * + * \param addr 32-bit aligned address. Only the lower 16-bits are significant + * \param data Vector of 32-bit values to write. + * + * \returns true iff successful + * + * WARNING: Attempts to read memory from addresses that do not correspond to RAM or + * memory-mapped peripherals may cause the USRP2 to hang, requiring a power cycle. + * + */ + bool poke32(uint32_t addr, const std::vector<uint32_t> &data); #if 0 // not yet implemented /*! diff --git a/usrp2/host/lib/control.h b/usrp2/host/lib/control.h index 774ca85f6..2c042f0cf 100644 --- a/usrp2/host/lib/control.h +++ b/usrp2/host/lib/control.h @@ -86,6 +86,13 @@ namespace usrp2 { op_generic_t eop; }; + struct op_poke_cmd { + u2_eth_packet_t h; + op_poke_t op; + // words to write go here + // eop must be dynamically written here + }; + /*! * Control mechanism to allow API calls to block waiting for reply packets */ diff --git a/usrp2/host/lib/usrp2.cc b/usrp2/host/lib/usrp2.cc index 37d8aaa33..2a7fe5963 100644 --- a/usrp2/host/lib/usrp2.cc +++ b/usrp2/host/lib/usrp2.cc @@ -401,6 +401,12 @@ namespace usrp2 { return d_impl->peek32(addr, words); } + bool + usrp2::poke32(uint32_t addr, const std::vector<uint32_t> &data) + { + return d_impl->poke32(addr, data); + } + } // namespace usrp2 diff --git a/usrp2/host/lib/usrp2_impl.cc b/usrp2/host/lib/usrp2_impl.cc index c44c65981..04f50f480 100644 --- a/usrp2/host/lib/usrp2_impl.cc +++ b/usrp2/host/lib/usrp2_impl.cc @@ -1084,4 +1084,67 @@ namespace usrp2 { return result; } + bool + usrp2::impl::poke32(uint32_t addr, const std::vector<uint32_t> &data) + { + if (addr % 4 != 0) { + fprintf(stderr, "usrp2::poke32: addr (=%08X) must be 32-bit word aligned\n", addr); + return false; + } + + int plen = sizeof(op_poke_cmd); + int wlen = sizeof(uint32_t); + int max_words = (MAX_SUBPKT_LEN-plen)/wlen; + int words = data.size(); + + if (words > max_words) { + fprintf(stderr, "usrp2::poke32: write size (=%u) exceeds maximum of %u words\n", + words, max_words); + return false; + } + + //fprintf(stderr, "usrp2::poke32: addr=%08X words=%u\n", addr, words); + + if (words == 0) + return true; // NOP + + op_poke_cmd *cmd; + op_generic_t *eop; + + // Allocate, clear, and initialize command packet + int bytes = words*wlen; + int l = plen+bytes+sizeof(*eop); // op_poke_cmd+data+eop + cmd = (op_poke_cmd *)malloc(l); + //fprintf(stderr, "cmd=%p l=%i\n", cmd, l); + memset(cmd, 0, l); + init_etf_hdrs(&cmd->h, d_addr, 0, CONTROL_CHAN, -1); + cmd->op.opcode = OP_POKE; + cmd->op.len = sizeof(cmd->op)+bytes; + cmd->op.rid = d_next_rid++; + cmd->op.addr = htonl(addr); + + // Copy data from vector into packet space + uint32_t *dest = (uint32_t *)((uint8_t *)cmd+plen); + for (unsigned int i = 0; i < words; i++) { + //fprintf(stderr, "%03i@%p\n", i, dest); + *dest++ = htonl(data[i]); + } + + // Write end-of-packet subpacket + eop = (op_generic_t *)dest; + eop->opcode = OP_EOP; + eop->len = sizeof(*eop); + //fprintf(stderr, "eop=%p len=%i\n", eop, eop->len); + + // Send command to device and retrieve reply + bool ok = false; + op_generic_t reply; + pending_reply p(cmd->op.rid, &reply, sizeof(reply)); + if (transmit_cmd(cmd, l, &p, DEF_CMD_TIMEOUT)) + ok = (ntohx(reply.ok) == 1); + + free(cmd); + return ok; + } + } // namespace usrp2 diff --git a/usrp2/host/lib/usrp2_impl.h b/usrp2/host/lib/usrp2_impl.h index 0a6b97b86..0400a108b 100644 --- a/usrp2/host/lib/usrp2_impl.h +++ b/usrp2/host/lib/usrp2_impl.h @@ -27,6 +27,8 @@ #include "ring.h" #include <string> +#define MAX_SUBPKT_LEN 252 + namespace usrp2 { class eth_buffer; @@ -175,6 +177,7 @@ namespace usrp2 { bool burn_mac_addr(const std::string &new_addr); bool sync_to_pps(); std::vector<uint32_t> peek32(uint32_t addr, uint32_t words); + bool poke32(uint32_t addr, const std::vector<uint32_t> &data); }; } // namespace usrp2 |