[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] r5004 - in gnuradio/branches/features/inband-usb/usrp/
From: |
gnychis |
Subject: |
[Commit-gnuradio] r5004 - in gnuradio/branches/features/inband-usb/usrp/host/lib: inband legacy |
Date: |
Sun, 15 Apr 2007 17:49:22 -0600 (MDT) |
Author: gnychis
Date: 2007-04-15 17:49:22 -0600 (Sun, 15 Apr 2007)
New Revision: 5004
Added:
gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_inband_usb_packet.h
Modified:
gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_server.cc
gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_server.h
gnuradio/branches/features/inband-usb/usrp/host/lib/legacy/usrp_bytesex.h
Log:
merging in the usrp server to the features branch from my developer's branch
code sucessfully builds and checks
Copied:
gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_inband_usb_packet.h
(from rev 4996,
gnuradio/branches/developers/gnychis/inband/usrp/host/lib/inband/usrp_inband_usb_packet.h)
===================================================================
---
gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_inband_usb_packet.h
(rev 0)
+++
gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_inband_usb_packet.h
2007-04-15 23:49:22 UTC (rev 5004)
@@ -0,0 +1,149 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef INCLUDED_USRP_INBAND_USB_PACKET_H_
+#define INCLUDED_USRP_INBAND_USB_PACKET_H_
+
+#include <usrp_bytesex.h>
+#include <mb_mblock.h>
+
+static const int USB_PKT_SIZE = 512; // bytes
+static const int MAX_PAYLOAD = USB_PKT_SIZE-2*sizeof(uint32_t);
+
+class usrp_inband_usb_packet {
+ //
+ // keep raw packet in USRP-endian order
+ //
+ uint32_t d_word0;
+ uint32_t d_timestamp;
+ unsigned char d_payload[MAX_PAYLOAD];
+
+public:
+
+ enum flags {
+ FL_OVERRUN = 0x80000000,
+ FL_UNDERRUN = 0x40000000,
+ FL_DROPPED = 0x20000000,
+ FL_END_OF_BURST = 0x10000000,
+ FL_START_OF_BURST = 0x08000000,
+
+ FL_ALL_FLAGS = 0xf8000000
+ };
+
+ static const int FL_OVERRUN_SHIFT = 31;
+ static const int FL_UNDERRUN_SHIFT = 30;
+ static const int FL_DROPPED_SHIFT = 29;
+ static const int FL_END_OF_BURST_SHIFT = 28;
+ static const int FL_START_OF_BURST_SHIFT = 27;
+
+ static const int RSSI_MASK = 0x3f;
+ static const int RSSI_SHIFT = 21;
+
+ static const int CHAN_MASK = 0x1f;
+ static const int CHAN_SHIFT = 16;
+
+ static const int TAG_MASK = 0xf;
+ static const int TAG_SHIFT = 9;
+
+ static const int PAYLOAD_LEN_MASK = 0x1ff;
+ static const int PAYLOAD_LEN_SHIFT = 0;
+
+public:
+
+ void set_timestamp(uint32_t timestamp){
+ d_timestamp = host_to_usrp_u32(timestamp);
+ }
+
+ void set_end_of_burst() {
+ uint32_t word0 = usrp_to_host_u32(d_word0);
+ word0 |= 1<<FL_END_OF_BURST_SHIFT;
+ d_word0 = host_to_usrp_u32(word0);
+ }
+
+ void set_header(int flags, int chan, int tag, int payload_len){
+ uint32_t word0 = ((flags & FL_ALL_FLAGS)
+ | ((chan & CHAN_MASK) << CHAN_SHIFT)
+ | ((tag & TAG_MASK) << TAG_SHIFT)
+ | ((payload_len & PAYLOAD_LEN_MASK) <<
PAYLOAD_LEN_SHIFT));
+ d_word0 = host_to_usrp_u32(word0);
+ }
+
+ uint32_t timestamp() const {
+ return usrp_to_host_u32(d_timestamp);
+ }
+
+ int rssi() const {
+ uint32_t word0 = usrp_to_host_u32(d_word0);
+ return (word0 >> RSSI_SHIFT) & RSSI_MASK;
+ }
+
+ int chan() const {
+ uint32_t word0 = usrp_to_host_u32(d_word0);
+ return (word0 >> CHAN_SHIFT) & CHAN_MASK;
+ }
+
+ int tag() const {
+ uint32_t word0 = usrp_to_host_u32(d_word0);
+ return (word0 >> TAG_SHIFT) & TAG_MASK;
+ }
+
+ int payload_len() const {
+ uint32_t word0 = usrp_to_host_u32(d_word0);
+ return (word0 >> PAYLOAD_LEN_SHIFT) & PAYLOAD_LEN_MASK;
+ }
+
+ int flags() const {
+ return usrp_to_host_u32(d_word0) & FL_ALL_FLAGS;
+ }
+
+ int overrun() const {
+ return (usrp_to_host_u32(d_word0) & FL_OVERRUN) >> FL_OVERRUN_SHIFT;
+ }
+
+
+ int underrun() const {
+ return (usrp_to_host_u32(d_word0) & FL_UNDERRUN) >> FL_UNDERRUN_SHIFT;
+ }
+
+
+ int start_of_burst() const {
+ return (usrp_to_host_u32(d_word0) & FL_START_OF_BURST) >>
FL_START_OF_BURST_SHIFT;
+ }
+
+ int end_of_burst() const {
+ return (usrp_to_host_u32(d_word0) & FL_END_OF_BURST) >>
FL_END_OF_BURST_SHIFT;
+ }
+
+ int dropped() const {
+ return (usrp_to_host_u32(d_word0) & FL_DROPPED) >> FL_DROPPED_SHIFT;
+ }
+
+ unsigned char *payload() {
+ return d_payload;
+ }
+
+ static int max_payload() {
+ return MAX_PAYLOAD;
+ }
+
+};
+
+#endif
Modified:
gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_server.cc
===================================================================
--- gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_server.cc
2007-04-15 19:50:09 UTC (rev 5003)
+++ gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_server.cc
2007-04-15 23:49:22 UTC (rev 5004)
@@ -24,7 +24,10 @@
#endif
#include <usrp_server.h>
#include <iostream>
+#include <usrp_inband_usb_packet.h>
+#include <vector>
+typedef usrp_inband_usb_packet transport_pkt; // makes conversion to gigabit
easy
// FIXME We should machine generate these by a simple preprocessor run over
this file
//
@@ -58,24 +61,29 @@
// control & status port
d_cs = define_port("cs", "usrp-server-cs", true, mb_port::EXTERNAL);
- // rx ports
+ // ports
//
// (if/when we do replicated ports, these will be replaced by a
// single replicated port)
- d_rx0 = define_port("rx0", "usrp-rx", true, mb_port::EXTERNAL);
- d_rx1 = define_port("rx1", "usrp-rx", true, mb_port::EXTERNAL);
- d_rx2 = define_port("rx2", "usrp-rx", true, mb_port::EXTERNAL);
- d_rx3 = define_port("rx3", "usrp-rx", true, mb_port::EXTERNAL);
-
- // tx ports
- //
- // (if/when we do replicated ports, these will be replaced by a
- // single replicated port)
- d_tx0 = define_port("tx0", "usrp-tx", true, mb_port::EXTERNAL);
- d_tx1 = define_port("tx1", "usrp-tx", true, mb_port::EXTERNAL);
- d_tx2 = define_port("tx2", "usrp-tx", true, mb_port::EXTERNAL);
- d_tx3 = define_port("tx3", "usrp-tx", true, mb_port::EXTERNAL);
-
+ for(int port=0; port < N_PORTS; port++) {
+ d_tx.push_back(define_port("rx"+port, "usrp-tx", true, mb_port::EXTERNAL));
+ d_rx.push_back(define_port("tx"+port, "usrp-rx", true, mb_port::EXTERNAL));
+ }
+
+ // FIXME ... initializing to 2 channels on each for now, eventually we should
+ // query the FPGA to get these values
+ d_ntx_chan = 2;
+ d_nrx_chan = 2;
+
+ // Initialize capacity on each channel to 0 and to no owner
+ for(int chan=0; chan < d_ntx_chan; chan++) {
+ d_chaninfo_tx[chan].assigned_capacity = 0;
+ d_chaninfo_tx[chan].owner = PMT_NIL;
+ }
+ for(int chan=0; chan < d_nrx_chan; chan++) {
+ d_chaninfo_rx[chan].assigned_capacity = 0;
+ d_chaninfo_rx[chan].owner = PMT_NIL;
+ }
}
usrp_server::~usrp_server()
@@ -133,14 +141,215 @@
}
if (pmt_eq(event, s_cmd_allocate_channel)){
- // blah blah blah, do something here
+ handle_cmd_allocate_channel(port_id, data);
return;
}
- if (pmt_eq(event, s_cmd_allocate_channel)){
- // ...
+
+ if (pmt_eq(event, s_cmd_deallocate_channel)) {
+ handle_cmd_deallocate_channel(port_id, data);
return;
}
+
+ if (pmt_eq(event, s_cmd_xmit_raw_frame)){
+ handle_cmd_xmit_raw_frame(data);
+ return;
+ }
unhandled:
std::cout << "unhandled msg: " << msg << std::endl;
}
+
+// Return -1 if it is not an RX port, or an index
+long usrp_server::tx_port_index(pmt_t port_id) {
+
+ std::vector<mb_port_sptr>::iterator tx;
+
+ for(int i=0; i < d_tx.size(); i++)
+ if(pmt_eq(tx[i]->port_symbol(), port_id))
+ return i;
+
+ return -1;
+}
+
+// Return -1 if it is not an RX port, or an index
+long usrp_server::rx_port_index(pmt_t port_id) {
+
+ std::vector<mb_port_sptr>::iterator rx;
+
+ for(int i=0; i < d_rx.size(); i++)
+ if(pmt_eq(rx[i]->port_symbol(), port_id))
+ return i;
+
+ return -1;
+}
+
+// Go through all TX and RX channels, sum up the assigned capacity
+// and return it
+long usrp_server::current_capacity_allocation() {
+ long capacity = 0;
+
+ for(int chan=0; chan < d_ntx_chan; chan++)
+ capacity += d_chaninfo_tx[chan].assigned_capacity;
+
+ for(int chan=0; chan < d_nrx_chan; chan++)
+ capacity += d_chaninfo_rx[chan].assigned_capacity;
+
+ return capacity;
+}
+
+void usrp_server::handle_cmd_allocate_channel(pmt_t port_id, pmt_t data) {
+
+ pmt_t invocation_handle = pmt_nth(0, data);
+ long rqstd_capacity = pmt_to_long(pmt_nth(1, data));
+ long chan, port;
+ pmt_t reply_data;
+
+ // If it's a TX port, allocate on a free channel, else check if it's a RX
port
+ // and allocate.
+ if((port = tx_port_index(port_id)) != -1) {
+
+ // Check capacity exists
+ if((D_USB_CAPACITY - current_capacity_allocation()) < rqstd_capacity) {
+ reply_data = pmt_list3(invocation_handle, PMT_F, PMT_NIL); // no
capacity available
+ d_tx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+
+ // Find a free channel, assign the capacity and respond
+ for(chan=0; chan < d_ntx_chan; chan++) {
+ if(d_chaninfo_tx[chan].owner == PMT_NIL) {
+ d_chaninfo_tx[chan].owner = port_id;
+ d_chaninfo_tx[chan].assigned_capacity = rqstd_capacity;
+ reply_data = pmt_list3(invocation_handle, PMT_T, pmt_from_long(chan));
+ d_tx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+ }
+
+ reply_data = pmt_list3(invocation_handle, PMT_F, PMT_NIL); // no free TX
chan found
+ d_tx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+
+ // Repeat the same process on the RX side if the port was not determined to
be TX
+ if((port = rx_port_index(port_id)) != -1) {
+
+ if((D_USB_CAPACITY - current_capacity_allocation()) < rqstd_capacity) {
+ reply_data = pmt_list3(invocation_handle, PMT_F, PMT_NIL); // no
capacity available
+ d_rx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+
+ for(chan=0; chan < d_ntx_chan; chan++) {
+ if(d_chaninfo_tx[chan].owner == PMT_NIL) {
+ d_chaninfo_tx[chan].owner = port_id;
+ d_chaninfo_tx[chan].assigned_capacity = rqstd_capacity;
+ reply_data = pmt_list3(invocation_handle, PMT_T, pmt_from_long(chan));
+ d_rx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+ }
+
+ reply_data = pmt_list3(invocation_handle, PMT_F, PMT_NIL); // no free RX
chan found
+ d_rx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+}
+
+// Check the port type and deallocate assigned capacity based on this, ensuring
+// that the owner of the method invocation is the owner of the port and that
+// the channel number is valid.
+void usrp_server::handle_cmd_deallocate_channel(pmt_t port_id, pmt_t data) {
+
+ pmt_t invocation_handle = pmt_nth(0, data);
+ long channel = pmt_to_long(pmt_nth(1, data));
+ long port;
+ pmt_t reply_data;
+
+ // Check that the channel number is valid, and that the calling port is the
owner
+ // of the channel, and if so remove the assigned capacity.
+ if((port = tx_port_index(port_id)) != -1) {
+
+ if(channel >= d_ntx_chan) {
+ reply_data = pmt_list2(invocation_handle, PMT_F); // not a legit
channel number
+ d_tx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+
+ if(d_chaninfo_tx[channel].owner != port_id) {
+ reply_data = pmt_list2(invocation_handle, PMT_F); // not the owner of
the port
+ d_tx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+
+ d_chaninfo_tx[channel].assigned_capacity = 0;
+ d_chaninfo_tx[channel].owner = PMT_NIL;
+
+ reply_data = pmt_list2(invocation_handle, PMT_T);
+ d_tx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+
+ // Repeated process on the RX side
+ if((port = rx_port_index(port_id)) != -1) {
+
+ if(channel >= d_nrx_chan) {
+ reply_data = pmt_list2(invocation_handle, PMT_F); // not a legit
channel number
+ d_rx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+
+ if(d_chaninfo_rx[channel].owner != port_id) {
+ reply_data = pmt_list2(invocation_handle, PMT_F); // not the owner of
the port
+ d_rx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+
+ d_chaninfo_rx[channel].assigned_capacity = 0;
+ d_chaninfo_rx[channel].owner = PMT_NIL;
+
+ reply_data = pmt_list2(invocation_handle, PMT_T);
+ d_rx[port]->send(invocation_handle, reply_data);
+ return;
+ }
+
+}
+
+void usrp_server::handle_cmd_xmit_raw_frame(pmt_t data) {
+
+ size_t n_bytes, psize;
+ long max_payload_len = transport_pkt::max_payload();
+
+ pmt_t invocation_handle = pmt_nth(0, data);
+ long channel = pmt_to_long(pmt_nth(1, data));
+ const void *samples = pmt_uniform_vector_elements(pmt_nth(2, data), n_bytes);
+ long timestamp = pmt_to_long(pmt_nth(3, data));
+
+ // Determine the number of packets to allocate contiguous memory for
bursting over the
+ // USB and get a pointer to the memory to be used in building the packets
+ long n_packets = std::ceil(n_bytes / (double)max_payload_len);
+ pmt_t v_packets = pmt_make_u8vector(sizeof(transport_pkt) * n_packets, 0);
+ transport_pkt *pkts = (transport_pkt *)
pmt_u8vector_writeable_elements(v_packets, psize);
+
+ for(int n=0; n < n_packets; n++) {
+
+ long payload_len = std::min((int)(n_bytes-(n*max_payload_len)),
(int)max_payload_len);
+
+ if(n == 0) { // first packet gets start of burst flag and timestamp
+ pkts[n].set_header(pkts[n].FL_START_OF_BURST, channel, 0, payload_len);
+ pkts[n].set_timestamp(timestamp);
+ } else {
+ pkts[n].set_header(0, channel, 0, payload_len);
+ pkts[n].set_timestamp(0xffffffff);
+ }
+
+ memcpy(pkts[n].payload(), (uint8_t *)samples+(max_payload_len * n),
payload_len);
+ }
+
+ pkts[n_packets-1].set_end_of_burst(); // set the last packet's end of burst
+
+ // interface with the USRP to send the USB packet, since the memory is
+ // contiguous, this should be a serious of memory copies to the bus, each
being
+ // USB_PKT_SIZE * MAX_PACKET_BURST bytes worth of data (given a full burst)
+}
+
Modified:
gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_server.h
===================================================================
--- gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_server.h
2007-04-15 19:50:09 UTC (rev 5003)
+++ gnuradio/branches/features/inband-usb/usrp/host/lib/inband/usrp_server.h
2007-04-15 23:49:22 UTC (rev 5004)
@@ -22,31 +22,51 @@
#define INCLUDED_USRP_SERVER_H
#include <mb_mblock.h>
+#include <vector>
/*!
* \brief Implements the lowest-level mblock interface to the USRP
*/
class usrp_server : public mb_mblock
{
+public:
+
// our ports
+ enum port_types {
+ RX_PORT = 0,
+ TX_PORT = 1
+ };
+ static const int N_PORTS = 4;
+ std::vector<mb_port_sptr> d_tx, d_rx;
mb_port_sptr d_cs;
- mb_port_sptr d_rx0;
- mb_port_sptr d_rx1;
- mb_port_sptr d_rx2;
- mb_port_sptr d_rx3;
- mb_port_sptr d_tx0;
- mb_port_sptr d_tx1;
- mb_port_sptr d_tx2;
- mb_port_sptr d_tx3;
- // add more stuff here...
+ static const int D_USB_CAPACITY = 32 * 1024 * 1024;
+ static const int D_MAX_CHANNELS = 16;
+ long d_ntx_chan;
+ long d_nrx_chan;
+ struct channel_info {
+ long assigned_capacity; // the capacity currently assignedby the channel
+ pmt_t owner; // port ID of the owner of the channel
+ };
+
+ struct channel_info d_chaninfo_tx[D_MAX_CHANNELS];
+ struct channel_info d_chaninfo_rx[D_MAX_CHANNELS];
+
public:
usrp_server(mb_runtime *rt, const std::string &instance_name, pmt_t
user_arg);
~usrp_server();
void initial_transition();
void handle_message(mb_message_sptr msg);
+
+private:
+ void handle_cmd_allocate_channel(pmt_t port_id, pmt_t data);
+ void handle_cmd_deallocate_channel(pmt_t port_id, pmt_t data);
+ void handle_cmd_xmit_raw_frame(pmt_t data);
+ long rx_port_index(pmt_t port_id);
+ long tx_port_index(pmt_t port_id);
+ long current_capacity_allocation();
};
#endif /* INCLUDED_USRP_SERVER_H */
Modified:
gnuradio/branches/features/inband-usb/usrp/host/lib/legacy/usrp_bytesex.h
===================================================================
--- gnuradio/branches/features/inband-usb/usrp/host/lib/legacy/usrp_bytesex.h
2007-04-15 19:50:09 UTC (rev 5003)
+++ gnuradio/branches/features/inband-usb/usrp/host/lib/legacy/usrp_bytesex.h
2007-04-15 23:49:22 UTC (rev 5004)
@@ -38,11 +38,30 @@
{
return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8));
}
+
+static inline unsigned int
+bswap32 (unsigned int x)
+{
+ return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) \
+ | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24));
+}
#endif
#ifdef WORDS_BIGENDIAN
+static inline unsigned int
+host_to_usrp_u32 (unsigned int x)
+{
+ return bswap_32(x);
+}
+
+static inline unsigned int
+usrp_to_host_u32 (unsigned int x)
+{
+ return bswap_32(x);
+}
+
static inline short int
host_to_usrp_short (short int x)
{
@@ -57,6 +76,18 @@
#else
+static inline unsigned int
+host_to_usrp_u32 (unsigned int x)
+{
+ return x;
+}
+
+static inline unsigned int
+usrp_to_host_u32 (unsigned int x)
+{
+ return x;
+}
+
static inline short int
host_to_usrp_short (short int x)
{
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Commit-gnuradio] r5004 - in gnuradio/branches/features/inband-usb/usrp/host/lib: inband legacy,
gnychis <=