From 0d365ff30324ea8684388051ac509e7df22929d0 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 19 Apr 2010 18:31:16 -0700 Subject: pulled aux dac and adc support from microblaze, its in the host now --- firmware/microblaze/apps/txrx.c | 39 -------------------- firmware/microblaze/lib/Makefile.am | 4 -- firmware/microblaze/lib/lsadc.c | 73 ------------------------------------- firmware/microblaze/lib/lsadc.h | 45 ----------------------- firmware/microblaze/lib/lsdac.c | 68 ---------------------------------- firmware/microblaze/lib/lsdac.h | 47 ------------------------ firmware/microblaze/lib/u2_init.c | 4 -- 7 files changed, 280 deletions(-) delete mode 100644 firmware/microblaze/lib/lsadc.c delete mode 100644 firmware/microblaze/lib/lsadc.h delete mode 100644 firmware/microblaze/lib/lsdac.c delete mode 100644 firmware/microblaze/lib/lsdac.h (limited to 'firmware') diff --git a/firmware/microblaze/apps/txrx.c b/firmware/microblaze/apps/txrx.c index 561f3148f..2e13a99d0 100644 --- a/firmware/microblaze/apps/txrx.c +++ b/firmware/microblaze/apps/txrx.c @@ -47,8 +47,6 @@ #include "usrp2/fw_common.h" #include #include -#include -#include #include #include @@ -296,43 +294,6 @@ void handle_udp_ctrl_packet( } break; - /******************************************************************* - * AUX DAC/ADC - ******************************************************************/ - case USRP2_CTRL_ID_WRITE_THIS_TO_THE_AUX_DAC_BRO: - if (ctrl_data_in->data.aux_args.dir == USRP2_DIR_RX){ - lsdac_write_rx( - ctrl_data_in->data.aux_args.which, - ctrl_data_in->data.aux_args.value - ); - } - - if (ctrl_data_in->data.aux_args.dir == USRP2_DIR_TX){ - lsdac_write_tx( - ctrl_data_in->data.aux_args.which, - ctrl_data_in->data.aux_args.value - ); - } - - ctrl_data_out.id = USRP2_CTRL_ID_DONE_WITH_THAT_AUX_DAC_DUDE; - break; - - case USRP2_CTRL_ID_READ_FROM_THIS_AUX_ADC_BRO: - if (ctrl_data_in->data.aux_args.dir == USRP2_DIR_RX){ - ctrl_data_out.data.aux_args.value = lsadc_read_rx( - ctrl_data_in->data.aux_args.which - ); - } - - if (ctrl_data_in->data.aux_args.dir == USRP2_DIR_TX){ - ctrl_data_out.data.aux_args.value = lsadc_read_tx( - ctrl_data_in->data.aux_args.which - ); - } - - ctrl_data_out.id = USRP2_CTRL_ID_DONE_WITH_THAT_AUX_ADC_DUDE; - break; - /******************************************************************* * Streaming ******************************************************************/ diff --git a/firmware/microblaze/lib/Makefile.am b/firmware/microblaze/lib/Makefile.am index 3d02cfe8b..bd8972f5c 100644 --- a/firmware/microblaze/lib/Makefile.am +++ b/firmware/microblaze/lib/Makefile.am @@ -38,8 +38,6 @@ libu2fw_a_SOURCES = \ hal_io.c \ hal_uart.c \ i2c.c \ - lsadc.c \ - lsdac.c \ mdelay.c \ memcpy_wa.c \ memset_wa.c \ @@ -71,8 +69,6 @@ noinst_HEADERS = \ hal_io.h \ hal_uart.h \ i2c.h \ - lsadc.h \ - lsdac.h \ mdelay.h \ memcpy_wa.h \ memory_map.h \ diff --git a/firmware/microblaze/lib/lsadc.c b/firmware/microblaze/lib/lsadc.c deleted file mode 100644 index 7983552e7..000000000 --- a/firmware/microblaze/lib/lsadc.c +++ /dev/null @@ -1,73 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2008 Free Software Foundation, Inc. - * - * This program 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 3 of the License, or - * (at your option) any later version. - * - * This program 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, see . - */ - -#include "lsadc.h" -#include "spi.h" -#include "memory_map.h" - - -// AD9712 or AD7922 1 MS/s, 10-/12-bit ADCs - -//#define SPI_SS_DEBUG SPI_SS_RX_DB -#define SPI_SS_DEBUG 0 - - -void -lsadc_init(void) -{ - // nop -} - -/* - * The ADC's are pipelined. That is, you have to tell them - * which of the two inputs you want one cycle ahead of time. - * We could optimize and keep track of which one we used last - * time, but for simplicity we'll always tell it which - * one we want. This takes 2 16-bit xfers, one to set the - * input and one to read the one we want. - */ - -int -_lsadc_read(int which_adc, int slave_select) -{ - uint32_t r; - int channel = which_adc & 0x1; - - // Set CHN and STY equal to channel number. We don't want "daisy chain mode" - uint16_t cmd = (channel << 13) | (channel << 12); - - spi_transact(SPI_TXONLY, slave_select | SPI_SS_DEBUG, - cmd, 16, SPIF_PUSH_RISE | SPIF_LATCH_RISE); - - r = spi_transact(SPI_TXRX, slave_select | SPI_SS_DEBUG, - cmd, 16, SPIF_PUSH_RISE | SPIF_LATCH_RISE); - - return r & 0x0fff; -} - -int -lsadc_read_rx(int which_adc) -{ - return _lsadc_read(which_adc, SPI_SS_RX_ADC); -} - -int -lsadc_read_tx(int which_adc) -{ - return _lsadc_read(which_adc, SPI_SS_TX_ADC); -} diff --git a/firmware/microblaze/lib/lsadc.h b/firmware/microblaze/lib/lsadc.h deleted file mode 100644 index 319f34d91..000000000 --- a/firmware/microblaze/lib/lsadc.h +++ /dev/null @@ -1,45 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2008 Free Software Foundation, Inc. - * - * This program 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 3 of the License, or - * (at your option) any later version. - * - * This program 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, see . - */ -#ifndef INCLUDED_LSADC_H -#define INCLUDED_LSADC_H - -#include "memory_map.h" - -/*! - * \brief One time call to initialize low-speed ADCs. - */ -void lsadc_init(void); - -/*! - * \brief Read one of the low-speed Rx daughterboard ADCs. - * \param which_adc in [0, 1] - * - * \returns 12-bit value in [0,4095] - */ -int lsadc_read_rx(int which_adc); - -/*! - * \brief Read one of the low-speed Tx daughterboard ADCs. - * \param which_adc in [0, 1] - * - * \returns 12-bit value in [0,4095] - */ -int lsadc_read_tx(int which_adc); - - -#endif /* INCLUDED_LSADC_H */ diff --git a/firmware/microblaze/lib/lsdac.c b/firmware/microblaze/lib/lsdac.c deleted file mode 100644 index 6bc2e5cb5..000000000 --- a/firmware/microblaze/lib/lsdac.c +++ /dev/null @@ -1,68 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2008 Free Software Foundation, Inc. - * - * This program 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 3 of the License, or - * (at your option) any later version. - * - * This program 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, see . - */ - -#include "lsdac.h" -#include "spi.h" -#include "memory_map.h" - -// AD5624, AD5623 - -#define CMD(x) ((x) << 19) -#define CMD_WR_INPUT_N CMD(0) // write input N -#define CMD_UP_DAC_N CMD(1) // update DAC N from input reg -#define CMD_WR_INPUT_N_LDAC CMD(2) // write input N, update all -#define CMD_WR_UP_DAC_N CMD(3) // write and update N -#define CMD_WR_PWR_CONFIG CMD(4) // write power up/down config reg -#define CMD_SW_RESET CMD(5) // force s/w reset -#define CMD_WR_LDAC_CFG CMD(6) // write LDAC config reg -#define CMD_WR_INT_REF_CFG CMD(7) // write internal ref cfg reg (AD5623R only) - - -//#define SPI_SS_DEBUG SPI_SS_TX_DB -#define SPI_SS_DEBUG 0 - -inline static void -_write_rx(uint32_t v) -{ - spi_transact(SPI_TXONLY, SPI_SS_RX_DAC | SPI_SS_DEBUG, v, 24, SPIF_PUSH_RISE); -} - -inline static void -_write_tx(uint32_t v) -{ - spi_transact(SPI_TXONLY, SPI_SS_TX_DAC | SPI_SS_DEBUG, v, 24, SPIF_PUSH_RISE); -} - -void -lsdac_init(void) -{ - _write_tx(CMD_SW_RESET | 0x1); // power-on reset - _write_rx(CMD_SW_RESET | 0x1); // power-on reset -} - -void -lsdac_write_rx(int which_dac, int value) -{ - _write_rx(CMD_WR_UP_DAC_N | ((which_dac & 0x7) << 16) | ((value << 4) & 0xffff)); -} - -void -lsdac_write_tx(int which_dac, int value) -{ - _write_tx(CMD_WR_UP_DAC_N | ((which_dac & 0x7) << 16) | ((value << 4) & 0xffff)); -} diff --git a/firmware/microblaze/lib/lsdac.h b/firmware/microblaze/lib/lsdac.h deleted file mode 100644 index 9cad917e3..000000000 --- a/firmware/microblaze/lib/lsdac.h +++ /dev/null @@ -1,47 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2008 Free Software Foundation, Inc. - * - * This program 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 3 of the License, or - * (at your option) any later version. - * - * This program 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, see . - */ -#ifndef INCLUDED_LSDAC_H -#define INCLUDED_LSDAC_H - -#include "memory_map.h" - -/*! - * \brief One time call to initialize low-speed DACs. - */ -void lsdac_init(void); - -/*! - * \brief Write one of the low-speed Rx daughterboard DACs. - * \param which_dac in [0, 3] - * \param unsigned 12-bit value in [0, 4095] - * - * value maps linearly to output voltage from 0 to 3.3V - */ -void lsdac_write_rx(int which_dac, int value); - -/*! - * \brief Write one of the low-speed Tx daughterboard DACs. - * \param which_dac in [0, 3] - * \param unsigned 12-bit value in [0, 4095] - * - * value maps linearly to output voltage from 0 to 3.3V - */ -void lsdac_write_tx(int which_dac, int value); - - -#endif /* INCLUDED_LSDAC_H */ diff --git a/firmware/microblaze/lib/u2_init.c b/firmware/microblaze/lib/u2_init.c index bd3302d95..76e83c660 100644 --- a/firmware/microblaze/lib/u2_init.c +++ b/firmware/microblaze/lib/u2_init.c @@ -20,8 +20,6 @@ #include "spi.h" #include "pic.h" #include "hal_io.h" -#include "lsadc.h" -#include "lsdac.h" #include "buffer_pool.h" #include "hal_uart.h" #include "i2c.h" @@ -84,8 +82,6 @@ u2_init(void) pic_init(); // progammable interrupt controller bp_init(); // buffer pool - lsadc_init(); // low-speed ADCs - lsdac_init(); // low-speed DACs hal_enable_ints(); -- cgit v1.2.3 From e5248746f66829c0bd899bcde5c15970fdda785d Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Wed, 21 Apr 2010 17:49:00 -0700 Subject: Added protocol version number to usrp2 common header and data struct. --- firmware/microblaze/apps/txrx.c | 34 +++++++++++++++++++++++++++++----- host/lib/usrp/usrp2/fw_common.h | 21 +++++++++++++++++---- host/lib/usrp/usrp2/usrp2_iface.cpp | 8 ++++++++ host/lib/usrp/usrp2/usrp2_impl.cpp | 1 + 4 files changed, 55 insertions(+), 9 deletions(-) (limited to 'firmware') diff --git a/firmware/microblaze/apps/txrx.c b/firmware/microblaze/apps/txrx.c index 2e13a99d0..d2497892b 100644 --- a/firmware/microblaze/apps/txrx.c +++ b/firmware/microblaze/apps/txrx.c @@ -201,20 +201,34 @@ void handle_udp_ctrl_packet( unsigned char *payload, int payload_len ){ //printf("Got ctrl packet #words: %d\n", (int)payload_len); + usrp2_ctrl_data_t *ctrl_data_in = (usrp2_ctrl_data_t *)payload; + uint32_t ctrl_data_in_id = ctrl_data_in->id; + + //ensure that the protocol versions match + if (payload_len >= sizeof(uint32_t) && ctrl_data_in->proto_ver != USRP2_PROTO_VERSION){ + printf("!Error in control packet handler: Expected protocol version %d, but got %d\n", + USRP2_PROTO_VERSION, ctrl_data_in->proto_ver + ); + ctrl_data_in_id = USRP2_CTRL_ID_GIVE_ME_YOUR_IP_ADDR_BRO; + } + + //ensure that this is not a short packet if (payload_len < sizeof(usrp2_ctrl_data_t)){ - //TODO send err packet - return; + printf("!Error in control packet handler: Expected payload length %d, but got %d\n", + (int)sizeof(usrp2_ctrl_data_t), payload_len + ); + ctrl_data_in_id = USRP2_CTRL_ID_HUH_WHAT; } - //setup the input and output data - usrp2_ctrl_data_t *ctrl_data_in = (usrp2_ctrl_data_t *)payload; + //setup the output data usrp2_ctrl_data_t ctrl_data_out = { + .proto_ver = USRP2_PROTO_VERSION, .id=USRP2_CTRL_ID_HUH_WHAT, .seq=ctrl_data_in->seq }; //handle the data based on the id - switch(ctrl_data_in->id){ + switch(ctrl_data_in_id){ /******************************************************************* * Addressing @@ -400,6 +414,15 @@ void handle_udp_ctrl_packet( ctrl_data_out.id = USRP2_CTRL_ID_WOAH_I_DEFINITELY_PEEKED_IT_DUDE; break; + /******************************************************************* + * Hardware Rev Numbers + ******************************************************************/ + case USRP2_CTRL_ID_WHATS_THE_HARDWARE_REV_NOS_BRO: + ctrl_data_out.data.hw_rev.major = u2_hw_rev_major; + ctrl_data_out.data.hw_rev.minor = u2_hw_rev_minor; + ctrl_data_out.id = USRP2_CTRL_ID_TAKE_THE_HARDWARE_REV_NOS_DUDE; + break; + default: ctrl_data_out.id = USRP2_CTRL_ID_HUH_WHAT; @@ -572,6 +595,7 @@ main(void) print_mac_addr(ethernet_mac_addr()->addr); newline(); print_ip_addr(get_ip_addr()); newline(); + printf("Control protocol version: %d\n", USRP2_PROTO_VERSION); ethernet_register_link_changed_callback(link_changed_callback); ethernet_init(); diff --git a/host/lib/usrp/usrp2/fw_common.h b/host/lib/usrp/usrp2/fw_common.h index b25316217..640b37ec6 100644 --- a/host/lib/usrp/usrp2/fw_common.h +++ b/host/lib/usrp/usrp2/fw_common.h @@ -32,6 +32,10 @@ extern "C" { #define _SINS_ #endif +//defines the protocol version in this shared header +//increment this value when the protocol is changed +#define USRP2_PROTO_VERSION 1 + //used to differentiate control packets over data port #define USRP2_INVALID_VRT_HEADER 0 @@ -78,21 +82,25 @@ typedef enum{ USRP2_CTRL_ID_PEEK_AT_THIS_REGISTER_FOR_ME_BRO = 'r', USRP2_CTRL_ID_WOAH_I_DEFINITELY_PEEKED_IT_DUDE = 'R', + USRP2_CTRL_ID_WHATS_THE_HARDWARE_REV_NOS_BRO = 'y', + USRP2_CTRL_ID_TAKE_THE_HARDWARE_REV_NOS_DUDE = 'Y', + USRP2_CTRL_ID_PEACE_OUT = '~' } usrp2_ctrl_id_t; typedef enum{ - USRP2_DIR_RX, - USRP2_DIR_TX + USRP2_DIR_RX = 'r', + USRP2_DIR_TX = 't' } usrp2_dir_which_t; typedef enum{ - USRP2_CLK_EDGE_RISE, - USRP2_CLK_EDGE_FALL + USRP2_CLK_EDGE_RISE = 'r', + USRP2_CLK_EDGE_FALL = 'f' } usrp2_clk_edge_t; typedef struct{ + _SINS_ uint32_t proto_ver; _SINS_ uint32_t id; _SINS_ uint32_t seq; union{ @@ -129,6 +137,11 @@ typedef struct{ _SINS_ uint32_t data; _SINS_ uint8_t num_bytes; //1, 2, 4 } poke_args; + struct { + _SINS_ uint8_t major; + _SINS_ uint8_t minor; + _SINS_ uint8_t _pad[2]; + } hw_rev; } data; } usrp2_ctrl_data_t; diff --git a/host/lib/usrp/usrp2/usrp2_iface.cpp b/host/lib/usrp/usrp2/usrp2_iface.cpp index 742c53a14..1b0dde1b4 100644 --- a/host/lib/usrp/usrp2/usrp2_iface.cpp +++ b/host/lib/usrp/usrp2/usrp2_iface.cpp @@ -97,6 +97,7 @@ public: //fill in the seq number and send usrp2_ctrl_data_t out_copy = out_data; + out_copy.proto_ver = htonl(USRP2_PROTO_VERSION); out_copy.seq = htonl(++_ctrl_seq_num); _ctrl_transport->send(boost::asio::buffer(&out_copy, sizeof(usrp2_ctrl_data_t))); @@ -104,6 +105,13 @@ public: while(true){ usrp2_ctrl_data_t in_data; size_t len = _ctrl_transport->recv(boost::asio::buffer(&in_data, sizeof(in_data))); + if(len >= sizeof(boost::uint32_t) and ntohl(in_data.proto_ver) != USRP2_PROTO_VERSION){ + throw std::runtime_error(str( + boost::format("Expected protocol version %d, but got %d\n" + "The firmware build does not match the host code build." + ) % int(USRP2_PROTO_VERSION) % ntohl(in_data.proto_ver) + )); + } if (len >= sizeof(usrp2_ctrl_data_t) and ntohl(in_data.seq) == _ctrl_seq_num){ return in_data; } diff --git a/host/lib/usrp/usrp2/usrp2_impl.cpp b/host/lib/usrp/usrp2/usrp2_impl.cpp index 3bdc5bd02..0fa56c339 100644 --- a/host/lib/usrp/usrp2/usrp2_impl.cpp +++ b/host/lib/usrp/usrp2/usrp2_impl.cpp @@ -70,6 +70,7 @@ uhd::device_addrs_t usrp2::find(const device_addr_t &hint){ //send a hello control packet usrp2_ctrl_data_t ctrl_data_out; + ctrl_data_out.proto_ver = htonl(USRP2_PROTO_VERSION); ctrl_data_out.id = htonl(USRP2_CTRL_ID_GIVE_ME_YOUR_IP_ADDR_BRO); udp_transport->send(boost::asio::buffer(&ctrl_data_out, sizeof(ctrl_data_out))); -- cgit v1.2.3 From 19a2f02768e03ced30a32dfc0a703068a8d99176 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Thu, 22 Apr 2010 13:24:21 -0700 Subject: toggle led a for continuous streaming --- firmware/microblaze/apps/txrx.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'firmware') diff --git a/firmware/microblaze/apps/txrx.c b/firmware/microblaze/apps/txrx.c index d2497892b..7a6fbd993 100644 --- a/firmware/microblaze/apps/txrx.c +++ b/firmware/microblaze/apps/txrx.c @@ -50,6 +50,8 @@ #include #include +#define LEDS_SW LED_A + /* * Full duplex Tx and Rx between ethernet and DSP pipelines * @@ -317,6 +319,7 @@ void handle_udp_ctrl_packet( if (ctrl_data_in->data.stream_cmd.continuous){ printf("Setting up continuous streaming...\n"); printf("items per frame: %d\n", (int)streaming_items_per_frame); + hal_set_leds(LED_A, LEDS_SW); auto_reload_command = true; streaming_frame_count = FRAMES_PER_CMD; @@ -341,6 +344,7 @@ void handle_udp_ctrl_packet( //issue regular stream commands (split commands if too large) else{ + hal_set_leds(0, LEDS_SW); auto_reload_command = false; size_t num_samps = ctrl_data_in->data.stream_cmd.num_samps; if (num_samps == 0) num_samps = 1; //FIXME hack, zero is used when stopping continuous streaming but it somehow makes it inifinite @@ -606,6 +610,8 @@ main(void) register_udp_listener(USRP2_UDP_CTRL_PORT, handle_udp_ctrl_packet); register_udp_listener(USRP2_UDP_DATA_PORT, handle_udp_data_packet); + hal_set_led_src(0, LEDS_SW); + #if 0 // make bit 15 of Tx gpio's be a s/w output hal_gpio_set_sel(GPIO_TX_BANK, 15, 's'); -- cgit v1.2.3 From 039eceb4b208b2ca5a3465d2f16c8d5a7c7276c7 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Fri, 23 Apr 2010 19:21:47 -0700 Subject: Moved reading the eeprom (dboard ids) onto the host. Created a eeprom parser for the format in the dboard. Removed the support from the microblaze code. --- firmware/microblaze/apps/txrx.c | 16 ------- firmware/microblaze/lib/Makefile.am | 2 - firmware/microblaze/lib/db.h | 31 ------------- firmware/microblaze/lib/db_init.c | 77 -------------------------------- firmware/microblaze/lib/u2_init.c | 1 - host/include/uhd/usrp/CMakeLists.txt | 1 + host/include/uhd/usrp/dboard_eeprom.hpp | 55 +++++++++++++++++++++++ host/lib/CMakeLists.txt | 1 + host/lib/usrp/dboard_eeprom.cpp | 78 +++++++++++++++++++++++++++++++++ host/lib/usrp/usrp2/dboard_iface.cpp | 36 +-------------- host/lib/usrp/usrp2/dboard_impl.cpp | 15 +++---- host/lib/usrp/usrp2/fw_common.h | 17 +------ host/lib/usrp/usrp2/usrp2_iface.cpp | 61 ++++++++++++++++++++++++++ host/lib/usrp/usrp2/usrp2_iface.hpp | 55 +++++++++++++++++++++++ 14 files changed, 259 insertions(+), 187 deletions(-) delete mode 100644 firmware/microblaze/lib/db.h delete mode 100644 firmware/microblaze/lib/db_init.c create mode 100644 host/include/uhd/usrp/dboard_eeprom.hpp create mode 100644 host/lib/usrp/dboard_eeprom.cpp (limited to 'firmware') diff --git a/firmware/microblaze/apps/txrx.c b/firmware/microblaze/apps/txrx.c index 7a6fbd993..8ff3b8c58 100644 --- a/firmware/microblaze/apps/txrx.c +++ b/firmware/microblaze/apps/txrx.c @@ -45,7 +45,6 @@ #include "clocks.h" #include #include "usrp2/fw_common.h" -#include #include #include #include @@ -257,12 +256,6 @@ void handle_udp_ctrl_packet( memcpy(&ctrl_data_out.data.mac_addr, ethernet_mac_addr(), sizeof(eth_mac_addr_t)); break; - case USRP2_CTRL_ID_GIVE_ME_YOUR_DBOARD_IDS_BRO: - ctrl_data_out.id = USRP2_CTRL_ID_THESE_ARE_MY_DBOARD_IDS_DUDE; - ctrl_data_out.data.dboard_ids.tx_id = read_dboard_eeprom(I2C_ADDR_TX_A); - ctrl_data_out.data.dboard_ids.rx_id = read_dboard_eeprom(I2C_ADDR_RX_A); - break; - /******************************************************************* * SPI ******************************************************************/ @@ -418,15 +411,6 @@ void handle_udp_ctrl_packet( ctrl_data_out.id = USRP2_CTRL_ID_WOAH_I_DEFINITELY_PEEKED_IT_DUDE; break; - /******************************************************************* - * Hardware Rev Numbers - ******************************************************************/ - case USRP2_CTRL_ID_WHATS_THE_HARDWARE_REV_NOS_BRO: - ctrl_data_out.data.hw_rev.major = u2_hw_rev_major; - ctrl_data_out.data.hw_rev.minor = u2_hw_rev_minor; - ctrl_data_out.id = USRP2_CTRL_ID_TAKE_THE_HARDWARE_REV_NOS_DUDE; - break; - default: ctrl_data_out.id = USRP2_CTRL_ID_HUH_WHAT; diff --git a/firmware/microblaze/lib/Makefile.am b/firmware/microblaze/lib/Makefile.am index bd8972f5c..783895850 100644 --- a/firmware/microblaze/lib/Makefile.am +++ b/firmware/microblaze/lib/Makefile.am @@ -28,7 +28,6 @@ libu2fw_a_SOURCES = \ bsm12.c \ buffer_pool.c \ clocks.c \ - db_init.c \ dbsm.c \ eeprom.c \ ethernet.c \ @@ -60,7 +59,6 @@ noinst_HEADERS = \ bsm12.h \ buffer_pool.h \ clocks.h \ - db.h \ dbsm.h \ eth_mac.h \ eth_mac_regs.h \ diff --git a/firmware/microblaze/lib/db.h b/firmware/microblaze/lib/db.h deleted file mode 100644 index 358cb222b..000000000 --- a/firmware/microblaze/lib/db.h +++ /dev/null @@ -1,31 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2008,2009 Free Software Foundation, Inc. - * - * This program 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 3 of the License, or - * (at your option) any later version. - * - * This program 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, see . - */ - -/* - * Interface to daughterboard code - */ - -#ifndef INCLUDED_DB_H -#define INCLUDED_DB_H - -#include -#include - -int read_dboard_eeprom(int i2c_addr); - -#endif /* INCLUDED_DB_H */ diff --git a/firmware/microblaze/lib/db_init.c b/firmware/microblaze/lib/db_init.c deleted file mode 100644 index 23805d9cd..000000000 --- a/firmware/microblaze/lib/db_init.c +++ /dev/null @@ -1,77 +0,0 @@ -// -// Copyright 2010 Ettus Research LLC -// -/* - * Copyright 2008,2009 Free Software Foundation, Inc. - * - * This program 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 3 of the License, or - * (at your option) any later version. - * - * This program 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, see . - */ - - -#include -#include -#include -#include -#include -#include -#include - - -typedef enum { UDBE_OK, UDBE_NO_EEPROM, UDBE_INVALID_EEPROM } usrp_dbeeprom_status_t; - -static usrp_dbeeprom_status_t -read_raw_dboard_eeprom (unsigned char *buf, int i2c_addr) -{ - if (!eeprom_read (i2c_addr, 0, buf, DB_EEPROM_CLEN)) - return UDBE_NO_EEPROM; - - if (buf[DB_EEPROM_MAGIC] != DB_EEPROM_MAGIC_VALUE) - return UDBE_INVALID_EEPROM; - - int sum = 0; - unsigned int i; - for (i = 0; i < DB_EEPROM_CLEN; i++) - sum += buf[i]; - - if ((sum & 0xff) != 0) - return UDBE_INVALID_EEPROM; - - return UDBE_OK; -} - - -/* - * Return DBID, -1 or -2 - */ -int -read_dboard_eeprom(int i2c_addr) -{ - unsigned char buf[DB_EEPROM_CLEN]; - - usrp_dbeeprom_status_t s = read_raw_dboard_eeprom (buf, i2c_addr); - - //printf("\nread_raw_dboard_eeprom: %d\n", s); - - switch (s){ - case UDBE_OK: - return (buf[DB_EEPROM_ID_MSB] << 8) | buf[DB_EEPROM_ID_LSB]; - - case UDBE_NO_EEPROM: - default: - return -1; - - case UDBE_INVALID_EEPROM: - return -2; - } -} diff --git a/firmware/microblaze/lib/u2_init.c b/firmware/microblaze/lib/u2_init.c index 76e83c660..399d834cb 100644 --- a/firmware/microblaze/lib/u2_init.c +++ b/firmware/microblaze/lib/u2_init.c @@ -25,7 +25,6 @@ #include "i2c.h" #include "mdelay.h" #include "clocks.h" -#include "db.h" #include "usrp2_i2c_addr.h" //#include "nonstdio.h" diff --git a/host/include/uhd/usrp/CMakeLists.txt b/host/include/uhd/usrp/CMakeLists.txt index 23758041c..bbd124ed8 100644 --- a/host/include/uhd/usrp/CMakeLists.txt +++ b/host/include/uhd/usrp/CMakeLists.txt @@ -26,6 +26,7 @@ INSTALL(FILES #### dboard headers ### dboard_base.hpp + dboard_eeprom.hpp dboard_id.hpp dboard_iface.hpp dboard_manager.hpp diff --git a/host/include/uhd/usrp/dboard_eeprom.hpp b/host/include/uhd/usrp/dboard_eeprom.hpp new file mode 100644 index 000000000..6be88c85a --- /dev/null +++ b/host/include/uhd/usrp/dboard_eeprom.hpp @@ -0,0 +1,55 @@ +// +// Copyright 2010 Ettus Research LLC +// +// This program 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 3 of the License, or +// (at your option) any later version. +// +// This program 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, see . +// + +#ifndef INCLUDED_UHD_USRP_DBOARD_EEPROM_HPP +#define INCLUDED_UHD_USRP_DBOARD_EEPROM_HPP + +#include +#include +#include +#include + +namespace uhd{ namespace usrp{ + +struct UHD_API dboard_eeprom_t{ + /*! + * The dboard id that was read from eeprom or will be set to eeprom. + */ + dboard_id_t id; + + /*! + * Create a dboard eeprom struct from the bytes read out of eeprom + * \param buf the vector of bytes + */ + dboard_eeprom_t(const uhd::byte_vector_t &buf = uhd::byte_vector_t(0)); + + /*! + * Get the bytes that would be written to dboard eeprom + * \return a vector of bytes + */ + uhd::byte_vector_t get_eeprom_bytes(void); + + /*! + * Get the number of bytes to read out of eeprom. + * \return the number of bytes we are interested in + */ + static size_t num_bytes(void); +}; + +}} //namespace + +#endif /* INCLUDED_UHD_USRP_DBOARD_EEPROM_HPP */ diff --git a/host/lib/CMakeLists.txt b/host/lib/CMakeLists.txt index 5495620ec..ffbf15484 100644 --- a/host/lib/CMakeLists.txt +++ b/host/lib/CMakeLists.txt @@ -55,6 +55,7 @@ SET(libuhd_sources transport/if_addrs.cpp transport/udp_simple.cpp usrp/dboard_base.cpp + usrp/dboard_eeprom.cpp usrp/simple_usrp.cpp usrp/dboard_manager.cpp usrp/tune_helper.cpp diff --git a/host/lib/usrp/dboard_eeprom.cpp b/host/lib/usrp/dboard_eeprom.cpp new file mode 100644 index 000000000..a8fac602a --- /dev/null +++ b/host/lib/usrp/dboard_eeprom.cpp @@ -0,0 +1,78 @@ +// +// Copyright 2010 Ettus Research LLC +// +// This program 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 3 of the License, or +// (at your option) any later version. +// +// This program 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, see . +// + +#include +#include + +using namespace uhd; +using namespace uhd::usrp; + +//////////////////////////////////////////////////////////////////////// +// format of daughterboard EEPROM +// 00: 0xDB code for ``I'm a daughterboard'' +// 01: .. Daughterboard ID (LSB) +// 02: .. Daughterboard ID (MSB) +// 03: .. io bits 7-0 direction (bit set if it's an output from m'board) +// 04: .. io bits 15-8 direction (bit set if it's an output from m'board) +// 05: .. ADC0 DC offset correction (LSB) +// 06: .. ADC0 DC offset correction (MSB) +// 07: .. ADC1 DC offset correction (LSB) +// 08: .. ADC1 DC offset correction (MSB) +// ... +// 1f: .. negative of the sum of bytes [0x00, 0x1e] + +#define DB_EEPROM_MAGIC 0x00 +#define DB_EEPROM_MAGIC_VALUE 0xDB +#define DB_EEPROM_ID_LSB 0x01 +#define DB_EEPROM_ID_MSB 0x02 +#define DB_EEPROM_OE_LSB 0x03 +#define DB_EEPROM_OE_MSB 0x04 +#define DB_EEPROM_OFFSET_0_LSB 0x05 // offset correction for ADC or DAC 0 +#define DB_EEPROM_OFFSET_0_MSB 0x06 +#define DB_EEPROM_OFFSET_1_LSB 0x07 // offset correction for ADC or DAC 1 +#define DB_EEPROM_OFFSET_1_MSB 0x08 +#define DB_EEPROM_CHKSUM 0x1f + +#define DB_EEPROM_CLEN 0x20 // length of common portion of eeprom + +#define DB_EEPROM_CUSTOM_BASE DB_EEPROM_CLEN // first avail offset for + // daughterboard specific use +//////////////////////////////////////////////////////////////////////// + +dboard_eeprom_t::dboard_eeprom_t(const byte_vector_t &buf){ + try{ + ASSERT_THROW(buf.size() >= num_bytes()); + ASSERT_THROW(buf[DB_EEPROM_MAGIC] == DB_EEPROM_MAGIC_VALUE); + id = \ + (boost::uint16_t(buf[DB_EEPROM_ID_LSB]) << 0) | + (boost::uint16_t(buf[DB_EEPROM_ID_MSB]) << 8) ; + }catch(const uhd::assert_error &e){ + id = dboard_id::NONE; + } +} + +byte_vector_t dboard_eeprom_t::get_eeprom_bytes(void){ + byte_vector_t bytes(3); + bytes[DB_EEPROM_MAGIC] = DB_EEPROM_MAGIC_VALUE; + bytes[DB_EEPROM_ID_LSB] = boost::uint8_t(id >> 0); + bytes[DB_EEPROM_ID_MSB] = boost::uint8_t(id >> 8); + return bytes; +} + +size_t dboard_eeprom_t::num_bytes(void){ + return 3; +} diff --git a/host/lib/usrp/usrp2/dboard_iface.cpp b/host/lib/usrp/usrp2/dboard_iface.cpp index e9acddee6..9d9b745ae 100644 --- a/host/lib/usrp/usrp2/dboard_iface.cpp +++ b/host/lib/usrp/usrp2/dboard_iface.cpp @@ -24,7 +24,6 @@ #include #include //htonl and ntohl #include -#include #include "ad7922_regs.hpp" //aux adc #include "ad5624_regs.hpp" //aux dac @@ -214,42 +213,11 @@ boost::uint32_t usrp2_dboard_iface::read_write_spi( * I2C **********************************************************************/ void usrp2_dboard_iface::write_i2c(int i2c_addr, const byte_vector_t &buf){ - //setup the out data - usrp2_ctrl_data_t out_data; - out_data.id = htonl(USRP2_CTRL_ID_WRITE_THESE_I2C_VALUES_BRO); - out_data.data.i2c_args.addr = i2c_addr; - out_data.data.i2c_args.bytes = buf.size(); - - //limitation of i2c transaction size - 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 = _iface->ctrl_send_and_recv(out_data); - ASSERT_THROW(htonl(in_data.id) == USRP2_CTRL_ID_COOL_IM_DONE_I2C_WRITE_DUDE); + return _iface->write_i2c(i2c_addr, buf); } byte_vector_t usrp2_dboard_iface::read_i2c(int i2c_addr, size_t num_bytes){ - //setup the out data - usrp2_ctrl_data_t out_data; - out_data.id = htonl(USRP2_CTRL_ID_DO_AN_I2C_READ_FOR_ME_BRO); - out_data.data.i2c_args.addr = i2c_addr; - out_data.data.i2c_args.bytes = num_bytes; - - //limitation of i2c transaction size - ASSERT_THROW(num_bytes <= sizeof(out_data.data.i2c_args.data)); - - //send and recv - usrp2_ctrl_data_t in_data = _iface->ctrl_send_and_recv(out_data); - ASSERT_THROW(htonl(in_data.id) == USRP2_CTRL_ID_HERES_THE_I2C_DATA_DUDE); - 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; + return _iface->read_i2c(i2c_addr, num_bytes); } /*********************************************************************** diff --git a/host/lib/usrp/usrp2/dboard_impl.cpp b/host/lib/usrp/usrp2/dboard_impl.cpp index ee23dc83a..d697f11f0 100644 --- a/host/lib/usrp/usrp2/dboard_impl.cpp +++ b/host/lib/usrp/usrp2/dboard_impl.cpp @@ -18,6 +18,7 @@ #include "usrp2_impl.hpp" #include "usrp2_regs.hpp" +#include #include #include #include @@ -33,22 +34,16 @@ using namespace uhd::usrp; * Helper Methods **********************************************************************/ void usrp2_impl::dboard_init(void){ - //grab the dboard ids over the control line - usrp2_ctrl_data_t out_data; - out_data.id = htonl(USRP2_CTRL_ID_GIVE_ME_YOUR_DBOARD_IDS_BRO); - usrp2_ctrl_data_t in_data = _iface->ctrl_send_and_recv(out_data); - ASSERT_THROW(htonl(in_data.id) == USRP2_CTRL_ID_THESE_ARE_MY_DBOARD_IDS_DUDE); - - //extract the dboard ids an convert them - dboard_id_t rx_dboard_id = ntohs(in_data.data.dboard_ids.rx_id); - dboard_id_t tx_dboard_id = ntohs(in_data.data.dboard_ids.tx_id); + //extract the dboard ids + dboard_eeprom_t db_rx_eeprom(_iface->read_eeprom(I2C_ADDR_RX_DB, 0, dboard_eeprom_t::num_bytes())); + dboard_eeprom_t db_tx_eeprom(_iface->read_eeprom(I2C_ADDR_TX_DB, 0, dboard_eeprom_t::num_bytes())); //create a new dboard interface and manager dboard_iface::sptr _dboard_iface( make_usrp2_dboard_iface(_iface, _clk_ctrl) ); _dboard_manager = dboard_manager::make( - rx_dboard_id, tx_dboard_id, _dboard_iface + db_rx_eeprom.id, db_tx_eeprom.id, _dboard_iface ); //load dboards diff --git a/host/lib/usrp/usrp2/fw_common.h b/host/lib/usrp/usrp2/fw_common.h index 640b37ec6..e80001ff2 100644 --- a/host/lib/usrp/usrp2/fw_common.h +++ b/host/lib/usrp/usrp2/fw_common.h @@ -34,7 +34,7 @@ extern "C" { //defines the protocol version in this shared header //increment this value when the protocol is changed -#define USRP2_PROTO_VERSION 1 +#define USRP2_PROTO_VERSION 2 //used to differentiate control packets over data port #define USRP2_INVALID_VRT_HEADER 0 @@ -61,9 +61,6 @@ typedef enum{ USRP2_CTRL_ID_THIS_IS_MY_MAC_ADDR_DUDE = 'M', USRP2_CTRL_ID_HERE_IS_A_NEW_MAC_ADDR_BRO = 'n', - USRP2_CTRL_ID_GIVE_ME_YOUR_DBOARD_IDS_BRO = 'd', - USRP2_CTRL_ID_THESE_ARE_MY_DBOARD_IDS_DUDE = 'D', - USRP2_CTRL_ID_TRANSACT_ME_SOME_SPI_BRO = 's', USRP2_CTRL_ID_OMG_TRANSACTED_SPI_DUDE = 'S', @@ -82,9 +79,6 @@ typedef enum{ USRP2_CTRL_ID_PEEK_AT_THIS_REGISTER_FOR_ME_BRO = 'r', USRP2_CTRL_ID_WOAH_I_DEFINITELY_PEEKED_IT_DUDE = 'R', - USRP2_CTRL_ID_WHATS_THE_HARDWARE_REV_NOS_BRO = 'y', - USRP2_CTRL_ID_TAKE_THE_HARDWARE_REV_NOS_DUDE = 'Y', - USRP2_CTRL_ID_PEACE_OUT = '~' } usrp2_ctrl_id_t; @@ -106,10 +100,6 @@ typedef struct{ union{ _SINS_ uint32_t ip_addr; _SINS_ uint8_t mac_addr[6]; - struct { - _SINS_ uint16_t rx_id; - _SINS_ uint16_t tx_id; - } dboard_ids; struct { _SINS_ uint8_t dev; _SINS_ uint8_t miso_edge; @@ -137,11 +127,6 @@ typedef struct{ _SINS_ uint32_t data; _SINS_ uint8_t num_bytes; //1, 2, 4 } poke_args; - struct { - _SINS_ uint8_t major; - _SINS_ uint8_t minor; - _SINS_ uint8_t _pad[2]; - } hw_rev; } data; } usrp2_ctrl_data_t; diff --git a/host/lib/usrp/usrp2/usrp2_iface.cpp b/host/lib/usrp/usrp2/usrp2_iface.cpp index 1b0dde1b4..c639c0dfe 100644 --- a/host/lib/usrp/usrp2/usrp2_iface.cpp +++ b/host/lib/usrp/usrp2/usrp2_iface.cpp @@ -19,9 +19,11 @@ #include #include #include +#include #include //used for htonl and ntohl #include #include +#include using namespace uhd; @@ -89,6 +91,65 @@ public: return ntohl(out_data.data.spi_args.data); } +/*********************************************************************** + * I2C + **********************************************************************/ + void write_i2c(boost::uint8_t addr, const byte_vector_t &buf){ + //setup the out data + usrp2_ctrl_data_t out_data; + 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 + 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); + ASSERT_THROW(htonl(in_data.id) == USRP2_CTRL_ID_COOL_IM_DONE_I2C_WRITE_DUDE); + } + + byte_vector_t read_i2c(boost::uint8_t addr, size_t num_bytes){ + //setup the out data + usrp2_ctrl_data_t out_data; + 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 + 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); + ASSERT_THROW(htonl(in_data.id) == USRP2_CTRL_ID_HERES_THE_I2C_DATA_DUDE); + 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; + } + +/*********************************************************************** + * EEPROM + **********************************************************************/ + void write_eeprom(boost::uint8_t addr, boost::uint8_t offset, const byte_vector_t &buf){ + BOOST_FOREACH(boost::uint8_t byte, buf){ + //write a byte at a time, its easy that way + byte_vector_t cmd = boost::assign::list_of(offset)(byte); + this->write_i2c(addr, cmd); + } + } + + byte_vector_t read_eeprom(boost::uint8_t addr, boost::uint8_t offset, size_t num_bytes){ + //do a zero byte write to start read cycle + write_i2c(addr, byte_vector_t(1, offset)); + return read_i2c(addr, num_bytes); + } + /*********************************************************************** * Send/Recv over control **********************************************************************/ diff --git a/host/lib/usrp/usrp2/usrp2_iface.hpp b/host/lib/usrp/usrp2/usrp2_iface.hpp index 6667c8998..938359677 100644 --- a/host/lib/usrp/usrp2/usrp2_iface.hpp +++ b/host/lib/usrp/usrp2/usrp2_iface.hpp @@ -25,6 +25,15 @@ #include #include "fw_common.h" +//////////////////////////////////////////////////////////////////////// +// I2C addresses +//////////////////////////////////////////////////////////////////////// +#define I2C_DEV_EEPROM 0x50 // 24LC02[45]: 7-bits 1010xxx +#define I2C_ADDR_MBOARD (I2C_DEV_EEPROM | 0x0) +#define I2C_ADDR_TX_DB (I2C_DEV_EEPROM | 0x4) +#define I2C_ADDR_RX_DB (I2C_DEV_EEPROM | 0x5) +//////////////////////////////////////////////////////////////////////// + /*! * The usrp2 interface class: * Provides a set of functions to implementation layer. @@ -93,6 +102,52 @@ public: bool readback ) = 0; + /*! + * Write bytes over the i2c. + * \param addr the address + * \param buf the vector of bytes + */ + virtual void write_i2c( + boost::uint8_t addr, + const uhd::byte_vector_t &buf + ) = 0; + + /*! + * Read bytes over the i2c. + * \param addr the address + * \param num_bytes number of bytes to read + * \return a vector of bytes + */ + virtual uhd::byte_vector_t read_i2c( + boost::uint8_t addr, + size_t num_bytes + ) = 0; + + /*! + * Write bytes to an eeprom. + * \param addr the address + * \param offset byte offset + * \param buf the vector of bytes + */ + virtual void write_eeprom( + boost::uint8_t addr, + boost::uint8_t offset, + const uhd::byte_vector_t &buf + ) = 0; + + /*! + * Read bytes from an eeprom. + * \param addr the address + * \param offset byte offset + * \param num_bytes number of bytes to read + * \return a vector of bytes + */ + virtual uhd::byte_vector_t read_eeprom( + boost::uint8_t addr, + boost::uint8_t offset, + size_t num_bytes + ) = 0; + /*! * Get the master clock frequency. * \return the frequency in Hz -- cgit v1.2.3 From 15befa19de40fb82f30d71bf21a767e7d8054ba1 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sun, 25 Apr 2010 22:17:16 -0700 Subject: renamed the firmware main image to txrx_uhd.bin --- firmware/microblaze/apps/Makefile.am | 4 +- firmware/microblaze/apps/txrx.c | 691 ----------------------------------- firmware/microblaze/apps/txrx_uhd.c | 691 +++++++++++++++++++++++++++++++++++ host/docs/usrp2.rst | 2 +- 4 files changed, 694 insertions(+), 694 deletions(-) delete mode 100644 firmware/microblaze/apps/txrx.c create mode 100644 firmware/microblaze/apps/txrx_uhd.c (limited to 'firmware') diff --git a/firmware/microblaze/apps/Makefile.am b/firmware/microblaze/apps/Makefile.am index ff426cf8c..a4f79935b 100644 --- a/firmware/microblaze/apps/Makefile.am +++ b/firmware/microblaze/apps/Makefile.am @@ -23,7 +23,7 @@ LDADD = $(top_srcdir)/lib/libu2fw.a AM_CFLAGS += -I$(top_srcdir)/../../host/lib/usrp -noinst_PROGRAMS = txrx.elf +noinst_PROGRAMS = txrx_uhd.elf # blink_leds \ # blink_leds2 \ @@ -66,7 +66,7 @@ noinst_PROGRAMS = txrx.elf # tx_drop_SOURCES = tx_drop.c app_common.c # tx_drop_rate_limited_SOURCES = tx_drop_rate_limited.c app_common.c # tx_drop2_SOURCES = tx_drop2.c app_common.c -txrx_elf_SOURCES = txrx.c +txrx_uhd_elf_SOURCES = txrx_uhd.c # app_common_v2.c #factory_test_SOURCES = factory_test.c app_common_v2.c #eth_serdes_SOURCES = eth_serdes.c app_passthru_v2.c diff --git a/firmware/microblaze/apps/txrx.c b/firmware/microblaze/apps/txrx.c deleted file mode 100644 index 7a6fbd993..000000000 --- a/firmware/microblaze/apps/txrx.c +++ /dev/null @@ -1,691 +0,0 @@ -// -// Copyright 2010 Ettus Research LLC -// -/* - * Copyright 2007,2008 Free Software Foundation, Inc. - * - * This program 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 3 of the License, or - * (at your option) any later version. - * - * This program 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, see . - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#define DEBUG_MODE 0 //0 for normal operation - -#include -#include -#include "u2_init.h" -#include "memory_map.h" -#include "spi.h" -#include "hal_io.h" -#include "buffer_pool.h" -#include "pic.h" -#include -#include "ethernet.h" -#include "nonstdio.h" -#include "dbsm.h" -#include -#include -#include "memcpy_wa.h" -#include -#include -#include -#include "clocks.h" -#include -#include "usrp2/fw_common.h" -#include -#include -#include -#include - -#define LEDS_SW LED_A - -/* - * Full duplex Tx and Rx between ethernet and DSP pipelines - * - * Buffer 1 is used by the cpu to send frames to the host. - * Buffers 2 and 3 are used to double-buffer the DSP Rx to eth flow - * Buffers 4 and 5 are used to double-buffer the eth to DSP Tx eth flow - */ -//#define CPU_RX_BUF 0 // eth -> cpu - -#define DSP_RX_BUF_0 2 // dsp rx -> eth (double buffer) -#define DSP_RX_BUF_1 3 // dsp rx -> eth -#define DSP_TX_BUF_0 4 // eth -> dsp tx (double buffer) -#define DSP_TX_BUF_1 5 // eth -> dsp tx - -/* - * ================================================================ - * configure DSP TX double buffering state machine (eth -> dsp) - * ================================================================ - */ - -// DSP Tx reads ethernet header words -#define DSP_TX_FIRST_LINE ((sizeof(padded_eth_hdr_t) + sizeof(struct ip_hdr) + sizeof(struct udp_hdr))/sizeof(uint32_t)) - -// Receive from ethernet -buf_cmd_args_t dsp_tx_recv_args = { - PORT_ETH, - 0, - BP_LAST_LINE -}; - -// send to DSP Tx -buf_cmd_args_t dsp_tx_send_args = { - PORT_DSP, - DSP_TX_FIRST_LINE, // starts just past transport header - 0 // filled in from last_line register -}; - -dbsm_t dsp_tx_sm; // the state machine - -/* - * ================================================================ - * configure DSP RX double buffering state machine (dsp -> eth) - * ================================================================ - */ - -typedef struct{ - uint32_t control_word; - uint32_t vrt_header[]; -} rx_dsp_buff_t; - -#define MK_RX_CTRL_WORD(num_words) (((num_words)*sizeof(uint32_t)) | (1 << 16)) - -// DSP Rx writes ethernet header words -#define DSP_RX_FIRST_LINE 1 //1 = number of control words (see above) - -// receive from DSP -buf_cmd_args_t dsp_rx_recv_args = { - PORT_DSP, - DSP_RX_FIRST_LINE, - BP_LAST_LINE -}; - -// send to ETH -buf_cmd_args_t dsp_rx_send_args = { - PORT_ETH, - 0, // starts with ethernet header in line 0 - 0, // filled in from list_line register -}; - -dbsm_t dsp_rx_sm; // the state machine - - -// The mac address of the host we're sending to. -eth_mac_addr_t host_mac_addr; - -//controls continuous streaming... -static bool auto_reload_command = false; -static size_t streaming_items_per_frame = 0; -static int streaming_frame_count = 0; -#define FRAMES_PER_CMD 2 - -static void setup_network(void); -static void setup_vrt(void); - -// ---------------------------------------------------------------- -// the fast-path setup global variables -// ---------------------------------------------------------------- -static eth_mac_addr_t fp_mac_addr_src, fp_mac_addr_dst; -static struct socket_address fp_socket_src, fp_socket_dst; - -// ---------------------------------------------------------------- -void start_rx_streaming_cmd(void); -void stop_rx_cmd(void); - -static void print_ip_addr(const void *t){ - uint8_t *p = (uint8_t *)t; - printf("%d.%d.%d.%d", p[0], p[1], p[2], p[3]); -} - -void handle_udp_data_packet( - struct socket_address src, struct socket_address dst, - unsigned char *payload, int payload_len -){ - //store the 2nd word as the following: - streaming_items_per_frame = ((uint32_t *)payload)[1]; - - //its a tiny payload, load the fast-path variables - fp_mac_addr_src = *ethernet_mac_addr(); - arp_cache_lookup_mac(&src.addr, &fp_mac_addr_dst); - fp_socket_src = dst; - fp_socket_dst = src; - printf("Storing for fast path:\n"); - printf(" source mac addr: "); - print_mac_addr(fp_mac_addr_src.addr); newline(); - printf(" source ip addr: "); - print_ip_addr(&fp_socket_src.addr); newline(); - printf(" source udp port: %d\n", fp_socket_src.port); - printf(" destination mac addr: "); - print_mac_addr(fp_mac_addr_dst.addr); newline(); - printf(" destination ip addr: "); - print_ip_addr(&fp_socket_dst.addr); newline(); - printf(" destination udp port: %d\n", fp_socket_dst.port); - newline(); - - //setup network and vrt - setup_network(); - setup_vrt(); - - // kick off the state machine - dbsm_start(&dsp_rx_sm); - -} - -static void inline issue_stream_command(size_t nsamps, bool now, bool chain, uint32_t secs, uint32_t ticks, bool start){ - //printf("Stream cmd: nsamps %d, now %d, chain %d, secs %u, ticks %u\n", (int)nsamps, now, chain, secs, ticks); - sr_rx_ctrl->cmd = MK_RX_CMD(nsamps, now, chain); - - if (start) dbsm_start(&dsp_rx_sm); - - sr_rx_ctrl->time_secs = secs; - sr_rx_ctrl->time_ticks = ticks; // enqueue command -} - -#define OTW_GPIO_BANK_TO_NUM(bank) \ - (((bank) == USRP2_DIR_RX)? (GPIO_RX_BANK) : (GPIO_TX_BANK)) - -void handle_udp_ctrl_packet( - struct socket_address src, struct socket_address dst, - unsigned char *payload, int payload_len -){ - //printf("Got ctrl packet #words: %d\n", (int)payload_len); - usrp2_ctrl_data_t *ctrl_data_in = (usrp2_ctrl_data_t *)payload; - uint32_t ctrl_data_in_id = ctrl_data_in->id; - - //ensure that the protocol versions match - if (payload_len >= sizeof(uint32_t) && ctrl_data_in->proto_ver != USRP2_PROTO_VERSION){ - printf("!Error in control packet handler: Expected protocol version %d, but got %d\n", - USRP2_PROTO_VERSION, ctrl_data_in->proto_ver - ); - ctrl_data_in_id = USRP2_CTRL_ID_GIVE_ME_YOUR_IP_ADDR_BRO; - } - - //ensure that this is not a short packet - if (payload_len < sizeof(usrp2_ctrl_data_t)){ - printf("!Error in control packet handler: Expected payload length %d, but got %d\n", - (int)sizeof(usrp2_ctrl_data_t), payload_len - ); - ctrl_data_in_id = USRP2_CTRL_ID_HUH_WHAT; - } - - //setup the output data - usrp2_ctrl_data_t ctrl_data_out = { - .proto_ver = USRP2_PROTO_VERSION, - .id=USRP2_CTRL_ID_HUH_WHAT, - .seq=ctrl_data_in->seq - }; - - //handle the data based on the id - switch(ctrl_data_in_id){ - - /******************************************************************* - * Addressing - ******************************************************************/ - case USRP2_CTRL_ID_GIVE_ME_YOUR_IP_ADDR_BRO: - ctrl_data_out.id = USRP2_CTRL_ID_THIS_IS_MY_IP_ADDR_DUDE; - memcpy(&ctrl_data_out.data.ip_addr, get_ip_addr(), sizeof(struct ip_addr)); - break; - - case USRP2_CTRL_ID_HERE_IS_A_NEW_IP_ADDR_BRO: - ctrl_data_out.id = USRP2_CTRL_ID_THIS_IS_MY_IP_ADDR_DUDE; - set_ip_addr((struct ip_addr *)&ctrl_data_in->data.ip_addr); - memcpy(&ctrl_data_out.data.ip_addr, get_ip_addr(), sizeof(struct ip_addr)); - break; - - case USRP2_CTRL_ID_GIVE_ME_YOUR_MAC_ADDR_BRO: - ctrl_data_out.id = USRP2_CTRL_ID_THIS_IS_MY_MAC_ADDR_DUDE; - memcpy(&ctrl_data_out.data.mac_addr, ethernet_mac_addr(), sizeof(eth_mac_addr_t)); - break; - - case USRP2_CTRL_ID_HERE_IS_A_NEW_MAC_ADDR_BRO: - ctrl_data_out.id = USRP2_CTRL_ID_THIS_IS_MY_MAC_ADDR_DUDE; - ethernet_set_mac_addr((eth_mac_addr_t *)&ctrl_data_in->data.mac_addr); - memcpy(&ctrl_data_out.data.mac_addr, ethernet_mac_addr(), sizeof(eth_mac_addr_t)); - break; - - case USRP2_CTRL_ID_GIVE_ME_YOUR_DBOARD_IDS_BRO: - ctrl_data_out.id = USRP2_CTRL_ID_THESE_ARE_MY_DBOARD_IDS_DUDE; - ctrl_data_out.data.dboard_ids.tx_id = read_dboard_eeprom(I2C_ADDR_TX_A); - ctrl_data_out.data.dboard_ids.rx_id = read_dboard_eeprom(I2C_ADDR_RX_A); - break; - - /******************************************************************* - * SPI - ******************************************************************/ - case USRP2_CTRL_ID_TRANSACT_ME_SOME_SPI_BRO:{ - //transact - uint32_t result = spi_transact( - (ctrl_data_in->data.spi_args.readback == 0)? SPI_TXONLY : SPI_TXRX, - ctrl_data_in->data.spi_args.dev, //which device - ctrl_data_in->data.spi_args.data, //32 bit data - ctrl_data_in->data.spi_args.num_bits, //length in bits - (ctrl_data_in->data.spi_args.mosi_edge == USRP2_CLK_EDGE_RISE)? SPIF_PUSH_FALL : SPIF_PUSH_RISE | - (ctrl_data_in->data.spi_args.miso_edge == USRP2_CLK_EDGE_RISE)? SPIF_LATCH_RISE : SPIF_LATCH_FALL - ); - - //load output - ctrl_data_out.data.spi_args.data = result; - ctrl_data_out.id = USRP2_CTRL_ID_OMG_TRANSACTED_SPI_DUDE; - } - break; - - /******************************************************************* - * I2C - ******************************************************************/ - case USRP2_CTRL_ID_DO_AN_I2C_READ_FOR_ME_BRO:{ - uint8_t num_bytes = ctrl_data_in->data.i2c_args.bytes; - i2c_read( - ctrl_data_in->data.i2c_args.addr, - ctrl_data_out.data.i2c_args.data, - num_bytes - ); - ctrl_data_out.id = USRP2_CTRL_ID_HERES_THE_I2C_DATA_DUDE; - ctrl_data_out.data.i2c_args.bytes = num_bytes; - } - break; - - case USRP2_CTRL_ID_WRITE_THESE_I2C_VALUES_BRO:{ - uint8_t num_bytes = ctrl_data_in->data.i2c_args.bytes; - i2c_write( - ctrl_data_in->data.i2c_args.addr, - ctrl_data_in->data.i2c_args.data, - num_bytes - ); - ctrl_data_out.id = USRP2_CTRL_ID_COOL_IM_DONE_I2C_WRITE_DUDE; - ctrl_data_out.data.i2c_args.bytes = num_bytes; - } - break; - - /******************************************************************* - * Streaming - ******************************************************************/ - case USRP2_CTRL_ID_SEND_STREAM_COMMAND_FOR_ME_BRO:{ - - //issue two commands and set the auto-reload flag - if (ctrl_data_in->data.stream_cmd.continuous){ - printf("Setting up continuous streaming...\n"); - printf("items per frame: %d\n", (int)streaming_items_per_frame); - hal_set_leds(LED_A, LEDS_SW); - auto_reload_command = true; - streaming_frame_count = FRAMES_PER_CMD; - - issue_stream_command( - streaming_items_per_frame * FRAMES_PER_CMD, - (ctrl_data_in->data.stream_cmd.now == 0)? false : true, //now - true, //chain - ctrl_data_in->data.stream_cmd.secs, - ctrl_data_in->data.stream_cmd.ticks, - true //start - ); - - issue_stream_command( - streaming_items_per_frame * FRAMES_PER_CMD, - true, //now - true, //chain - 0, 0, //time does not matter - false - ); - - } - - //issue regular stream commands (split commands if too large) - else{ - hal_set_leds(0, LEDS_SW); - auto_reload_command = false; - size_t num_samps = ctrl_data_in->data.stream_cmd.num_samps; - if (num_samps == 0) num_samps = 1; //FIXME hack, zero is used when stopping continuous streaming but it somehow makes it inifinite - - bool chain = num_samps > MAX_SAMPLES_PER_CMD; - issue_stream_command( - (chain)? streaming_items_per_frame : num_samps, //nsamps - (ctrl_data_in->data.stream_cmd.now == 0)? false : true, //now - (ctrl_data_in->data.stream_cmd.chain == 0)? chain : true, //chain - ctrl_data_in->data.stream_cmd.secs, - ctrl_data_in->data.stream_cmd.ticks, - false - ); - - //handle rest of the samples that did not fit into one cmd - while(chain){ - num_samps -= MAX_SAMPLES_PER_CMD; - chain = num_samps > MAX_SAMPLES_PER_CMD; - issue_stream_command( - (chain)? streaming_items_per_frame : num_samps, //nsamps - true, //now - (ctrl_data_in->data.stream_cmd.chain == 0)? chain : true, //chain - 0, 0, //time does not matter - false - ); - } - } - ctrl_data_out.id = USRP2_CTRL_ID_GOT_THAT_STREAM_COMMAND_DUDE; - break; - } - - /******************************************************************* - * Peek and Poke Register - ******************************************************************/ - case USRP2_CTRL_ID_POKE_THIS_REGISTER_FOR_ME_BRO: - if (ctrl_data_in->data.poke_args.addr < 0xC000){ - printf("error! tried to poke into 0x%x\n", ctrl_data_in->data.poke_args.addr); - } - else switch(ctrl_data_in->data.poke_args.num_bytes){ - case sizeof(uint32_t): - *((uint32_t *) ctrl_data_in->data.poke_args.addr) = (uint32_t)ctrl_data_in->data.poke_args.data; - break; - - case sizeof(uint16_t): - *((uint16_t *) ctrl_data_in->data.poke_args.addr) = (uint16_t)ctrl_data_in->data.poke_args.data; - break; - - case sizeof(uint8_t): - *((uint8_t *) ctrl_data_in->data.poke_args.addr) = (uint8_t)ctrl_data_in->data.poke_args.data; - break; - - } - ctrl_data_out.id = USRP2_CTRL_ID_OMG_POKED_REGISTER_SO_BAD_DUDE; - break; - - case USRP2_CTRL_ID_PEEK_AT_THIS_REGISTER_FOR_ME_BRO: - switch(ctrl_data_in->data.poke_args.num_bytes){ - case sizeof(uint32_t): - ctrl_data_in->data.poke_args.data = *((uint32_t *) ctrl_data_in->data.poke_args.addr); - break; - - case sizeof(uint16_t): - ctrl_data_in->data.poke_args.data = *((uint16_t *) ctrl_data_in->data.poke_args.addr); - break; - - case sizeof(uint8_t): - ctrl_data_in->data.poke_args.data = *((uint8_t *) ctrl_data_in->data.poke_args.addr); - break; - - } - ctrl_data_out.id = USRP2_CTRL_ID_WOAH_I_DEFINITELY_PEEKED_IT_DUDE; - break; - - /******************************************************************* - * Hardware Rev Numbers - ******************************************************************/ - case USRP2_CTRL_ID_WHATS_THE_HARDWARE_REV_NOS_BRO: - ctrl_data_out.data.hw_rev.major = u2_hw_rev_major; - ctrl_data_out.data.hw_rev.minor = u2_hw_rev_minor; - ctrl_data_out.id = USRP2_CTRL_ID_TAKE_THE_HARDWARE_REV_NOS_DUDE; - break; - - default: - ctrl_data_out.id = USRP2_CTRL_ID_HUH_WHAT; - - } - send_udp_pkt(USRP2_UDP_CTRL_PORT, src, &ctrl_data_out, sizeof(ctrl_data_out)); -} - -/* - * Called when an ethernet packet is received. - * Return true if we handled it here, otherwise - * it'll be passed on to the DSP Tx pipe - */ -static bool -eth_pkt_inspector(dbsm_t *sm, int bufno) -{ - //point me to the ethernet frame - uint32_t *buff = (uint32_t *)buffer_ram(bufno); - - //treat this as fast-path data? - // We have to do this operation as fast as possible. - // Therefore, we do not check all the headers, - // just check that the udp port matches - // and that the vrt header is non zero. - // In the future, a hardware state machine will do this... - if ( //warning! magic numbers approaching.... - (((buff + ((2 + 14 + 20)/sizeof(uint32_t)))[0] & 0xffff) == USRP2_UDP_DATA_PORT) && - ((buff + ((2 + 14 + 20 + 8)/sizeof(uint32_t)))[0] != USRP2_INVALID_VRT_HEADER) - ) return false; - - //test if its an ip recovery packet - typedef struct{ - padded_eth_hdr_t eth_hdr; - char code[4]; - union { - struct ip_addr ip_addr; - } data; - }recovery_packet_t; - recovery_packet_t *recovery_packet = (recovery_packet_t *)buff; - if (recovery_packet->eth_hdr.ethertype == 0xbeee && strncmp(recovery_packet->code, "addr", 4) == 0){ - printf("Got ip recovery packet: "); print_ip_addr(&recovery_packet->data.ip_addr); newline(); - set_ip_addr(&recovery_packet->data.ip_addr); - return true; - } - - //pass it to the slow-path handler - size_t len = buffer_pool_status->last_line[bufno] - 3; - handle_eth_packet(buff, len); - return true; -} - -//------------------------------------------------------------------ - -static bool vrt_has_trailer(void){ - return USRP2_HOST_RX_VRT_TRAILER_WORDS32 > 0; -} - -static void setup_vrt(void){ - // setup RX DSP regs - sr_rx_ctrl->nsamples_per_pkt = streaming_items_per_frame; - sr_rx_ctrl->nchannels = 1; - sr_rx_ctrl->clear_overrun = 1; // reset - sr_rx_ctrl->vrt_header = (0 - | VRTH_PT_IF_DATA_WITH_SID - | (vrt_has_trailer()? VRTH_HAS_TRAILER : 0) - | VRTH_TSI_OTHER - | VRTH_TSF_SAMPLE_CNT - ); - sr_rx_ctrl->vrt_stream_id = 0; - sr_rx_ctrl->vrt_trailer = 0; -} - -/* - * 1's complement sum for IP and UDP headers - * - * init chksum to zero to start. - */ -static unsigned int -CHKSUM(unsigned int x, unsigned int *chksum) -{ - *chksum += x; - *chksum = (*chksum & 0xffff) + (*chksum>>16); - *chksum = (*chksum & 0xffff) + (*chksum>>16); - return x; -} - -/* - * Called when eth phy state changes (w/ interrupts disabled) - */ -volatile bool link_is_up = false; // eth handler sets this -void -link_changed_callback(int speed) -{ - link_is_up = speed != 0; - hal_set_leds(link_is_up ? LED_RJ45 : 0x0, LED_RJ45); - printf("\neth link changed: speed = %d\n", speed); -} - -static void setup_network(void){ - - //setup ethernet header machine - sr_udp_sm->eth_hdr.mac_dst_0_1 = (fp_mac_addr_dst.addr[0] << 8) | fp_mac_addr_dst.addr[1]; - sr_udp_sm->eth_hdr.mac_dst_2_3 = (fp_mac_addr_dst.addr[2] << 8) | fp_mac_addr_dst.addr[3]; - sr_udp_sm->eth_hdr.mac_dst_4_5 = (fp_mac_addr_dst.addr[4] << 8) | fp_mac_addr_dst.addr[5]; - sr_udp_sm->eth_hdr.mac_src_0_1 = (fp_mac_addr_src.addr[0] << 8) | fp_mac_addr_src.addr[1]; - sr_udp_sm->eth_hdr.mac_src_2_3 = (fp_mac_addr_src.addr[2] << 8) | fp_mac_addr_src.addr[3]; - sr_udp_sm->eth_hdr.mac_src_4_5 = (fp_mac_addr_src.addr[4] << 8) | fp_mac_addr_src.addr[5]; - sr_udp_sm->eth_hdr.ether_type = ETHERTYPE_IPV4; - - //setup ip header machine - unsigned int chksum = 0; - sr_udp_sm->ip_hdr.ver_ihl_tos = CHKSUM(0x4500, &chksum); // IPV4, 5 words of header (20 bytes), TOS=0 - sr_udp_sm->ip_hdr.total_length = UDP_SM_INS_IP_LEN; // Don't checksum this line in SW - sr_udp_sm->ip_hdr.identification = CHKSUM(0x0000, &chksum); // ID - sr_udp_sm->ip_hdr.flags_frag_off = CHKSUM(0x4000, &chksum); // don't fragment - sr_udp_sm->ip_hdr.ttl_proto = CHKSUM(0x2011, &chksum); // TTL=32, protocol = UDP (17 decimal) - //sr_udp_sm->ip_hdr.checksum .... filled in below - uint32_t src_ip_addr = fp_socket_src.addr.addr; - uint32_t dst_ip_addr = fp_socket_dst.addr.addr; - sr_udp_sm->ip_hdr.src_addr_high = CHKSUM(src_ip_addr >> 16, &chksum); // IP src high - sr_udp_sm->ip_hdr.src_addr_low = CHKSUM(src_ip_addr & 0xffff, &chksum); // IP src low - sr_udp_sm->ip_hdr.dst_addr_high = CHKSUM(dst_ip_addr >> 16, &chksum); // IP dst high - sr_udp_sm->ip_hdr.dst_addr_low = CHKSUM(dst_ip_addr & 0xffff, &chksum); // IP dst low - sr_udp_sm->ip_hdr.checksum = UDP_SM_INS_IP_HDR_CHKSUM | (chksum & 0xffff); - - //setup the udp header machine - sr_udp_sm->udp_hdr.src_port = fp_socket_src.port; - sr_udp_sm->udp_hdr.dst_port = fp_socket_dst.port; - sr_udp_sm->udp_hdr.length = UDP_SM_INS_UDP_LEN; - sr_udp_sm->udp_hdr.checksum = UDP_SM_LAST_WORD; // zero UDP checksum -} - -/* - * This is called when the DSP Rx chain has filled in a packet. - */ -bool -fw_sets_seqno_inspector(dbsm_t *sm, int buf_this) // returns false -{ - // insert the correct length into the control word and vrt header - rx_dsp_buff_t *buff = (rx_dsp_buff_t*)buffer_ram(buf_this); - size_t vrt_len = buffer_pool_status->last_line[buf_this]-1; - buff->control_word = MK_RX_CTRL_WORD(vrt_len); - buff->vrt_header[0] = (buff->vrt_header[0] & ~VRTH_PKT_SIZE_MASK) | (vrt_len & VRTH_PKT_SIZE_MASK); - - // queue up another rx command when required - if (auto_reload_command && --streaming_frame_count == 0){ - streaming_frame_count = FRAMES_PER_CMD; - sr_rx_ctrl->time_secs = 0; - sr_rx_ctrl->time_ticks = 0; //enqueue last command - } - - return false; // we didn't handle the packet -} - - -inline static void -buffer_irq_handler(unsigned irq) -{ - uint32_t status = buffer_pool_status->status; - - dbsm_process_status(&dsp_tx_sm, status); - dbsm_process_status(&dsp_rx_sm, status); -} - -int -main(void) -{ - u2_init(); - - putstr("\nTxRx-NEWETH\n"); - print_mac_addr(ethernet_mac_addr()->addr); - newline(); - print_ip_addr(get_ip_addr()); newline(); - printf("Control protocol version: %d\n", USRP2_PROTO_VERSION); - - ethernet_register_link_changed_callback(link_changed_callback); - ethernet_init(); - - register_mac_addr(ethernet_mac_addr()); - register_ip_addr(get_ip_addr()); - - register_udp_listener(USRP2_UDP_CTRL_PORT, handle_udp_ctrl_packet); - register_udp_listener(USRP2_UDP_DATA_PORT, handle_udp_data_packet); - - hal_set_led_src(0, LEDS_SW); - -#if 0 - // make bit 15 of Tx gpio's be a s/w output - hal_gpio_set_sel(GPIO_TX_BANK, 15, 's'); - hal_gpio_set_ddr(GPIO_TX_BANK, 0x8000, 0x8000); -#endif - -//set them all to the atr settings by default -hal_gpio_set_sels(GPIO_TX_BANK, "aaaaaaaaaaaaaaaa"); -hal_gpio_set_sels(GPIO_RX_BANK, "aaaaaaaaaaaaaaaa"); - - output_regs->debug_mux_ctrl = 1; -#if DEBUG_MODE - hal_gpio_set_sels(GPIO_TX_BANK, "0000000000000000"); - hal_gpio_set_sels(GPIO_RX_BANK, "0000000000000000"); - hal_gpio_set_ddr(GPIO_TX_BANK, 0xffff, 0xffff); - hal_gpio_set_ddr(GPIO_RX_BANK, 0xffff, 0xffff); -#endif - - - // initialize double buffering state machine for ethernet -> DSP Tx - - dbsm_init(&dsp_tx_sm, DSP_TX_BUF_0, - &dsp_tx_recv_args, &dsp_tx_send_args, - eth_pkt_inspector); - - - // initialize double buffering state machine for DSP RX -> Ethernet - - dbsm_init(&dsp_rx_sm, DSP_RX_BUF_0, - &dsp_rx_recv_args, &dsp_rx_send_args, - fw_sets_seqno_inspector); - - - // tell app_common that this dbsm could be sending to the ethernet - ac_could_be_sending_to_eth = &dsp_rx_sm; - - sr_tx_ctrl->clear_state = 1; - bp_clear_buf(DSP_TX_BUF_0); - bp_clear_buf(DSP_TX_BUF_1); - - // kick off the state machine - dbsm_start(&dsp_tx_sm); - - //int which = 0; - - while(1){ - // hal_gpio_write(GPIO_TX_BANK, which, 0x8000); - // which ^= 0x8000; - - buffer_irq_handler(0); - - int pending = pic_regs->pending; // poll for under or overrun - - if (pending & PIC_UNDERRUN_INT){ - dbsm_handle_tx_underrun(&dsp_tx_sm); - pic_regs->pending = PIC_UNDERRUN_INT; // clear interrupt - putchar('U'); - } - - if (pending & PIC_OVERRUN_INT){ - dbsm_handle_rx_overrun(&dsp_rx_sm); - pic_regs->pending = PIC_OVERRUN_INT; // clear pending interrupt - - // FIXME Figure out how to handle this robustly. - // Any buffers that are emptying should be allowed to drain... - - if (auto_reload_command){ - // restart_streaming(); - // FIXME report error - } - else { - // FIXME report error - } - putchar('O'); - } - } -} diff --git a/firmware/microblaze/apps/txrx_uhd.c b/firmware/microblaze/apps/txrx_uhd.c new file mode 100644 index 000000000..7a6fbd993 --- /dev/null +++ b/firmware/microblaze/apps/txrx_uhd.c @@ -0,0 +1,691 @@ +// +// Copyright 2010 Ettus Research LLC +// +/* + * Copyright 2007,2008 Free Software Foundation, Inc. + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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, see . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#define DEBUG_MODE 0 //0 for normal operation + +#include +#include +#include "u2_init.h" +#include "memory_map.h" +#include "spi.h" +#include "hal_io.h" +#include "buffer_pool.h" +#include "pic.h" +#include +#include "ethernet.h" +#include "nonstdio.h" +#include "dbsm.h" +#include +#include +#include "memcpy_wa.h" +#include +#include +#include +#include "clocks.h" +#include +#include "usrp2/fw_common.h" +#include +#include +#include +#include + +#define LEDS_SW LED_A + +/* + * Full duplex Tx and Rx between ethernet and DSP pipelines + * + * Buffer 1 is used by the cpu to send frames to the host. + * Buffers 2 and 3 are used to double-buffer the DSP Rx to eth flow + * Buffers 4 and 5 are used to double-buffer the eth to DSP Tx eth flow + */ +//#define CPU_RX_BUF 0 // eth -> cpu + +#define DSP_RX_BUF_0 2 // dsp rx -> eth (double buffer) +#define DSP_RX_BUF_1 3 // dsp rx -> eth +#define DSP_TX_BUF_0 4 // eth -> dsp tx (double buffer) +#define DSP_TX_BUF_1 5 // eth -> dsp tx + +/* + * ================================================================ + * configure DSP TX double buffering state machine (eth -> dsp) + * ================================================================ + */ + +// DSP Tx reads ethernet header words +#define DSP_TX_FIRST_LINE ((sizeof(padded_eth_hdr_t) + sizeof(struct ip_hdr) + sizeof(struct udp_hdr))/sizeof(uint32_t)) + +// Receive from ethernet +buf_cmd_args_t dsp_tx_recv_args = { + PORT_ETH, + 0, + BP_LAST_LINE +}; + +// send to DSP Tx +buf_cmd_args_t dsp_tx_send_args = { + PORT_DSP, + DSP_TX_FIRST_LINE, // starts just past transport header + 0 // filled in from last_line register +}; + +dbsm_t dsp_tx_sm; // the state machine + +/* + * ================================================================ + * configure DSP RX double buffering state machine (dsp -> eth) + * ================================================================ + */ + +typedef struct{ + uint32_t control_word; + uint32_t vrt_header[]; +} rx_dsp_buff_t; + +#define MK_RX_CTRL_WORD(num_words) (((num_words)*sizeof(uint32_t)) | (1 << 16)) + +// DSP Rx writes ethernet header words +#define DSP_RX_FIRST_LINE 1 //1 = number of control words (see above) + +// receive from DSP +buf_cmd_args_t dsp_rx_recv_args = { + PORT_DSP, + DSP_RX_FIRST_LINE, + BP_LAST_LINE +}; + +// send to ETH +buf_cmd_args_t dsp_rx_send_args = { + PORT_ETH, + 0, // starts with ethernet header in line 0 + 0, // filled in from list_line register +}; + +dbsm_t dsp_rx_sm; // the state machine + + +// The mac address of the host we're sending to. +eth_mac_addr_t host_mac_addr; + +//controls continuous streaming... +static bool auto_reload_command = false; +static size_t streaming_items_per_frame = 0; +static int streaming_frame_count = 0; +#define FRAMES_PER_CMD 2 + +static void setup_network(void); +static void setup_vrt(void); + +// ---------------------------------------------------------------- +// the fast-path setup global variables +// ---------------------------------------------------------------- +static eth_mac_addr_t fp_mac_addr_src, fp_mac_addr_dst; +static struct socket_address fp_socket_src, fp_socket_dst; + +// ---------------------------------------------------------------- +void start_rx_streaming_cmd(void); +void stop_rx_cmd(void); + +static void print_ip_addr(const void *t){ + uint8_t *p = (uint8_t *)t; + printf("%d.%d.%d.%d", p[0], p[1], p[2], p[3]); +} + +void handle_udp_data_packet( + struct socket_address src, struct socket_address dst, + unsigned char *payload, int payload_len +){ + //store the 2nd word as the following: + streaming_items_per_frame = ((uint32_t *)payload)[1]; + + //its a tiny payload, load the fast-path variables + fp_mac_addr_src = *ethernet_mac_addr(); + arp_cache_lookup_mac(&src.addr, &fp_mac_addr_dst); + fp_socket_src = dst; + fp_socket_dst = src; + printf("Storing for fast path:\n"); + printf(" source mac addr: "); + print_mac_addr(fp_mac_addr_src.addr); newline(); + printf(" source ip addr: "); + print_ip_addr(&fp_socket_src.addr); newline(); + printf(" source udp port: %d\n", fp_socket_src.port); + printf(" destination mac addr: "); + print_mac_addr(fp_mac_addr_dst.addr); newline(); + printf(" destination ip addr: "); + print_ip_addr(&fp_socket_dst.addr); newline(); + printf(" destination udp port: %d\n", fp_socket_dst.port); + newline(); + + //setup network and vrt + setup_network(); + setup_vrt(); + + // kick off the state machine + dbsm_start(&dsp_rx_sm); + +} + +static void inline issue_stream_command(size_t nsamps, bool now, bool chain, uint32_t secs, uint32_t ticks, bool start){ + //printf("Stream cmd: nsamps %d, now %d, chain %d, secs %u, ticks %u\n", (int)nsamps, now, chain, secs, ticks); + sr_rx_ctrl->cmd = MK_RX_CMD(nsamps, now, chain); + + if (start) dbsm_start(&dsp_rx_sm); + + sr_rx_ctrl->time_secs = secs; + sr_rx_ctrl->time_ticks = ticks; // enqueue command +} + +#define OTW_GPIO_BANK_TO_NUM(bank) \ + (((bank) == USRP2_DIR_RX)? (GPIO_RX_BANK) : (GPIO_TX_BANK)) + +void handle_udp_ctrl_packet( + struct socket_address src, struct socket_address dst, + unsigned char *payload, int payload_len +){ + //printf("Got ctrl packet #words: %d\n", (int)payload_len); + usrp2_ctrl_data_t *ctrl_data_in = (usrp2_ctrl_data_t *)payload; + uint32_t ctrl_data_in_id = ctrl_data_in->id; + + //ensure that the protocol versions match + if (payload_len >= sizeof(uint32_t) && ctrl_data_in->proto_ver != USRP2_PROTO_VERSION){ + printf("!Error in control packet handler: Expected protocol version %d, but got %d\n", + USRP2_PROTO_VERSION, ctrl_data_in->proto_ver + ); + ctrl_data_in_id = USRP2_CTRL_ID_GIVE_ME_YOUR_IP_ADDR_BRO; + } + + //ensure that this is not a short packet + if (payload_len < sizeof(usrp2_ctrl_data_t)){ + printf("!Error in control packet handler: Expected payload length %d, but got %d\n", + (int)sizeof(usrp2_ctrl_data_t), payload_len + ); + ctrl_data_in_id = USRP2_CTRL_ID_HUH_WHAT; + } + + //setup the output data + usrp2_ctrl_data_t ctrl_data_out = { + .proto_ver = USRP2_PROTO_VERSION, + .id=USRP2_CTRL_ID_HUH_WHAT, + .seq=ctrl_data_in->seq + }; + + //handle the data based on the id + switch(ctrl_data_in_id){ + + /******************************************************************* + * Addressing + ******************************************************************/ + case USRP2_CTRL_ID_GIVE_ME_YOUR_IP_ADDR_BRO: + ctrl_data_out.id = USRP2_CTRL_ID_THIS_IS_MY_IP_ADDR_DUDE; + memcpy(&ctrl_data_out.data.ip_addr, get_ip_addr(), sizeof(struct ip_addr)); + break; + + case USRP2_CTRL_ID_HERE_IS_A_NEW_IP_ADDR_BRO: + ctrl_data_out.id = USRP2_CTRL_ID_THIS_IS_MY_IP_ADDR_DUDE; + set_ip_addr((struct ip_addr *)&ctrl_data_in->data.ip_addr); + memcpy(&ctrl_data_out.data.ip_addr, get_ip_addr(), sizeof(struct ip_addr)); + break; + + case USRP2_CTRL_ID_GIVE_ME_YOUR_MAC_ADDR_BRO: + ctrl_data_out.id = USRP2_CTRL_ID_THIS_IS_MY_MAC_ADDR_DUDE; + memcpy(&ctrl_data_out.data.mac_addr, ethernet_mac_addr(), sizeof(eth_mac_addr_t)); + break; + + case USRP2_CTRL_ID_HERE_IS_A_NEW_MAC_ADDR_BRO: + ctrl_data_out.id = USRP2_CTRL_ID_THIS_IS_MY_MAC_ADDR_DUDE; + ethernet_set_mac_addr((eth_mac_addr_t *)&ctrl_data_in->data.mac_addr); + memcpy(&ctrl_data_out.data.mac_addr, ethernet_mac_addr(), sizeof(eth_mac_addr_t)); + break; + + case USRP2_CTRL_ID_GIVE_ME_YOUR_DBOARD_IDS_BRO: + ctrl_data_out.id = USRP2_CTRL_ID_THESE_ARE_MY_DBOARD_IDS_DUDE; + ctrl_data_out.data.dboard_ids.tx_id = read_dboard_eeprom(I2C_ADDR_TX_A); + ctrl_data_out.data.dboard_ids.rx_id = read_dboard_eeprom(I2C_ADDR_RX_A); + break; + + /******************************************************************* + * SPI + ******************************************************************/ + case USRP2_CTRL_ID_TRANSACT_ME_SOME_SPI_BRO:{ + //transact + uint32_t result = spi_transact( + (ctrl_data_in->data.spi_args.readback == 0)? SPI_TXONLY : SPI_TXRX, + ctrl_data_in->data.spi_args.dev, //which device + ctrl_data_in->data.spi_args.data, //32 bit data + ctrl_data_in->data.spi_args.num_bits, //length in bits + (ctrl_data_in->data.spi_args.mosi_edge == USRP2_CLK_EDGE_RISE)? SPIF_PUSH_FALL : SPIF_PUSH_RISE | + (ctrl_data_in->data.spi_args.miso_edge == USRP2_CLK_EDGE_RISE)? SPIF_LATCH_RISE : SPIF_LATCH_FALL + ); + + //load output + ctrl_data_out.data.spi_args.data = result; + ctrl_data_out.id = USRP2_CTRL_ID_OMG_TRANSACTED_SPI_DUDE; + } + break; + + /******************************************************************* + * I2C + ******************************************************************/ + case USRP2_CTRL_ID_DO_AN_I2C_READ_FOR_ME_BRO:{ + uint8_t num_bytes = ctrl_data_in->data.i2c_args.bytes; + i2c_read( + ctrl_data_in->data.i2c_args.addr, + ctrl_data_out.data.i2c_args.data, + num_bytes + ); + ctrl_data_out.id = USRP2_CTRL_ID_HERES_THE_I2C_DATA_DUDE; + ctrl_data_out.data.i2c_args.bytes = num_bytes; + } + break; + + case USRP2_CTRL_ID_WRITE_THESE_I2C_VALUES_BRO:{ + uint8_t num_bytes = ctrl_data_in->data.i2c_args.bytes; + i2c_write( + ctrl_data_in->data.i2c_args.addr, + ctrl_data_in->data.i2c_args.data, + num_bytes + ); + ctrl_data_out.id = USRP2_CTRL_ID_COOL_IM_DONE_I2C_WRITE_DUDE; + ctrl_data_out.data.i2c_args.bytes = num_bytes; + } + break; + + /******************************************************************* + * Streaming + ******************************************************************/ + case USRP2_CTRL_ID_SEND_STREAM_COMMAND_FOR_ME_BRO:{ + + //issue two commands and set the auto-reload flag + if (ctrl_data_in->data.stream_cmd.continuous){ + printf("Setting up continuous streaming...\n"); + printf("items per frame: %d\n", (int)streaming_items_per_frame); + hal_set_leds(LED_A, LEDS_SW); + auto_reload_command = true; + streaming_frame_count = FRAMES_PER_CMD; + + issue_stream_command( + streaming_items_per_frame * FRAMES_PER_CMD, + (ctrl_data_in->data.stream_cmd.now == 0)? false : true, //now + true, //chain + ctrl_data_in->data.stream_cmd.secs, + ctrl_data_in->data.stream_cmd.ticks, + true //start + ); + + issue_stream_command( + streaming_items_per_frame * FRAMES_PER_CMD, + true, //now + true, //chain + 0, 0, //time does not matter + false + ); + + } + + //issue regular stream commands (split commands if too large) + else{ + hal_set_leds(0, LEDS_SW); + auto_reload_command = false; + size_t num_samps = ctrl_data_in->data.stream_cmd.num_samps; + if (num_samps == 0) num_samps = 1; //FIXME hack, zero is used when stopping continuous streaming but it somehow makes it inifinite + + bool chain = num_samps > MAX_SAMPLES_PER_CMD; + issue_stream_command( + (chain)? streaming_items_per_frame : num_samps, //nsamps + (ctrl_data_in->data.stream_cmd.now == 0)? false : true, //now + (ctrl_data_in->data.stream_cmd.chain == 0)? chain : true, //chain + ctrl_data_in->data.stream_cmd.secs, + ctrl_data_in->data.stream_cmd.ticks, + false + ); + + //handle rest of the samples that did not fit into one cmd + while(chain){ + num_samps -= MAX_SAMPLES_PER_CMD; + chain = num_samps > MAX_SAMPLES_PER_CMD; + issue_stream_command( + (chain)? streaming_items_per_frame : num_samps, //nsamps + true, //now + (ctrl_data_in->data.stream_cmd.chain == 0)? chain : true, //chain + 0, 0, //time does not matter + false + ); + } + } + ctrl_data_out.id = USRP2_CTRL_ID_GOT_THAT_STREAM_COMMAND_DUDE; + break; + } + + /******************************************************************* + * Peek and Poke Register + ******************************************************************/ + case USRP2_CTRL_ID_POKE_THIS_REGISTER_FOR_ME_BRO: + if (ctrl_data_in->data.poke_args.addr < 0xC000){ + printf("error! tried to poke into 0x%x\n", ctrl_data_in->data.poke_args.addr); + } + else switch(ctrl_data_in->data.poke_args.num_bytes){ + case sizeof(uint32_t): + *((uint32_t *) ctrl_data_in->data.poke_args.addr) = (uint32_t)ctrl_data_in->data.poke_args.data; + break; + + case sizeof(uint16_t): + *((uint16_t *) ctrl_data_in->data.poke_args.addr) = (uint16_t)ctrl_data_in->data.poke_args.data; + break; + + case sizeof(uint8_t): + *((uint8_t *) ctrl_data_in->data.poke_args.addr) = (uint8_t)ctrl_data_in->data.poke_args.data; + break; + + } + ctrl_data_out.id = USRP2_CTRL_ID_OMG_POKED_REGISTER_SO_BAD_DUDE; + break; + + case USRP2_CTRL_ID_PEEK_AT_THIS_REGISTER_FOR_ME_BRO: + switch(ctrl_data_in->data.poke_args.num_bytes){ + case sizeof(uint32_t): + ctrl_data_in->data.poke_args.data = *((uint32_t *) ctrl_data_in->data.poke_args.addr); + break; + + case sizeof(uint16_t): + ctrl_data_in->data.poke_args.data = *((uint16_t *) ctrl_data_in->data.poke_args.addr); + break; + + case sizeof(uint8_t): + ctrl_data_in->data.poke_args.data = *((uint8_t *) ctrl_data_in->data.poke_args.addr); + break; + + } + ctrl_data_out.id = USRP2_CTRL_ID_WOAH_I_DEFINITELY_PEEKED_IT_DUDE; + break; + + /******************************************************************* + * Hardware Rev Numbers + ******************************************************************/ + case USRP2_CTRL_ID_WHATS_THE_HARDWARE_REV_NOS_BRO: + ctrl_data_out.data.hw_rev.major = u2_hw_rev_major; + ctrl_data_out.data.hw_rev.minor = u2_hw_rev_minor; + ctrl_data_out.id = USRP2_CTRL_ID_TAKE_THE_HARDWARE_REV_NOS_DUDE; + break; + + default: + ctrl_data_out.id = USRP2_CTRL_ID_HUH_WHAT; + + } + send_udp_pkt(USRP2_UDP_CTRL_PORT, src, &ctrl_data_out, sizeof(ctrl_data_out)); +} + +/* + * Called when an ethernet packet is received. + * Return true if we handled it here, otherwise + * it'll be passed on to the DSP Tx pipe + */ +static bool +eth_pkt_inspector(dbsm_t *sm, int bufno) +{ + //point me to the ethernet frame + uint32_t *buff = (uint32_t *)buffer_ram(bufno); + + //treat this as fast-path data? + // We have to do this operation as fast as possible. + // Therefore, we do not check all the headers, + // just check that the udp port matches + // and that the vrt header is non zero. + // In the future, a hardware state machine will do this... + if ( //warning! magic numbers approaching.... + (((buff + ((2 + 14 + 20)/sizeof(uint32_t)))[0] & 0xffff) == USRP2_UDP_DATA_PORT) && + ((buff + ((2 + 14 + 20 + 8)/sizeof(uint32_t)))[0] != USRP2_INVALID_VRT_HEADER) + ) return false; + + //test if its an ip recovery packet + typedef struct{ + padded_eth_hdr_t eth_hdr; + char code[4]; + union { + struct ip_addr ip_addr; + } data; + }recovery_packet_t; + recovery_packet_t *recovery_packet = (recovery_packet_t *)buff; + if (recovery_packet->eth_hdr.ethertype == 0xbeee && strncmp(recovery_packet->code, "addr", 4) == 0){ + printf("Got ip recovery packet: "); print_ip_addr(&recovery_packet->data.ip_addr); newline(); + set_ip_addr(&recovery_packet->data.ip_addr); + return true; + } + + //pass it to the slow-path handler + size_t len = buffer_pool_status->last_line[bufno] - 3; + handle_eth_packet(buff, len); + return true; +} + +//------------------------------------------------------------------ + +static bool vrt_has_trailer(void){ + return USRP2_HOST_RX_VRT_TRAILER_WORDS32 > 0; +} + +static void setup_vrt(void){ + // setup RX DSP regs + sr_rx_ctrl->nsamples_per_pkt = streaming_items_per_frame; + sr_rx_ctrl->nchannels = 1; + sr_rx_ctrl->clear_overrun = 1; // reset + sr_rx_ctrl->vrt_header = (0 + | VRTH_PT_IF_DATA_WITH_SID + | (vrt_has_trailer()? VRTH_HAS_TRAILER : 0) + | VRTH_TSI_OTHER + | VRTH_TSF_SAMPLE_CNT + ); + sr_rx_ctrl->vrt_stream_id = 0; + sr_rx_ctrl->vrt_trailer = 0; +} + +/* + * 1's complement sum for IP and UDP headers + * + * init chksum to zero to start. + */ +static unsigned int +CHKSUM(unsigned int x, unsigned int *chksum) +{ + *chksum += x; + *chksum = (*chksum & 0xffff) + (*chksum>>16); + *chksum = (*chksum & 0xffff) + (*chksum>>16); + return x; +} + +/* + * Called when eth phy state changes (w/ interrupts disabled) + */ +volatile bool link_is_up = false; // eth handler sets this +void +link_changed_callback(int speed) +{ + link_is_up = speed != 0; + hal_set_leds(link_is_up ? LED_RJ45 : 0x0, LED_RJ45); + printf("\neth link changed: speed = %d\n", speed); +} + +static void setup_network(void){ + + //setup ethernet header machine + sr_udp_sm->eth_hdr.mac_dst_0_1 = (fp_mac_addr_dst.addr[0] << 8) | fp_mac_addr_dst.addr[1]; + sr_udp_sm->eth_hdr.mac_dst_2_3 = (fp_mac_addr_dst.addr[2] << 8) | fp_mac_addr_dst.addr[3]; + sr_udp_sm->eth_hdr.mac_dst_4_5 = (fp_mac_addr_dst.addr[4] << 8) | fp_mac_addr_dst.addr[5]; + sr_udp_sm->eth_hdr.mac_src_0_1 = (fp_mac_addr_src.addr[0] << 8) | fp_mac_addr_src.addr[1]; + sr_udp_sm->eth_hdr.mac_src_2_3 = (fp_mac_addr_src.addr[2] << 8) | fp_mac_addr_src.addr[3]; + sr_udp_sm->eth_hdr.mac_src_4_5 = (fp_mac_addr_src.addr[4] << 8) | fp_mac_addr_src.addr[5]; + sr_udp_sm->eth_hdr.ether_type = ETHERTYPE_IPV4; + + //setup ip header machine + unsigned int chksum = 0; + sr_udp_sm->ip_hdr.ver_ihl_tos = CHKSUM(0x4500, &chksum); // IPV4, 5 words of header (20 bytes), TOS=0 + sr_udp_sm->ip_hdr.total_length = UDP_SM_INS_IP_LEN; // Don't checksum this line in SW + sr_udp_sm->ip_hdr.identification = CHKSUM(0x0000, &chksum); // ID + sr_udp_sm->ip_hdr.flags_frag_off = CHKSUM(0x4000, &chksum); // don't fragment + sr_udp_sm->ip_hdr.ttl_proto = CHKSUM(0x2011, &chksum); // TTL=32, protocol = UDP (17 decimal) + //sr_udp_sm->ip_hdr.checksum .... filled in below + uint32_t src_ip_addr = fp_socket_src.addr.addr; + uint32_t dst_ip_addr = fp_socket_dst.addr.addr; + sr_udp_sm->ip_hdr.src_addr_high = CHKSUM(src_ip_addr >> 16, &chksum); // IP src high + sr_udp_sm->ip_hdr.src_addr_low = CHKSUM(src_ip_addr & 0xffff, &chksum); // IP src low + sr_udp_sm->ip_hdr.dst_addr_high = CHKSUM(dst_ip_addr >> 16, &chksum); // IP dst high + sr_udp_sm->ip_hdr.dst_addr_low = CHKSUM(dst_ip_addr & 0xffff, &chksum); // IP dst low + sr_udp_sm->ip_hdr.checksum = UDP_SM_INS_IP_HDR_CHKSUM | (chksum & 0xffff); + + //setup the udp header machine + sr_udp_sm->udp_hdr.src_port = fp_socket_src.port; + sr_udp_sm->udp_hdr.dst_port = fp_socket_dst.port; + sr_udp_sm->udp_hdr.length = UDP_SM_INS_UDP_LEN; + sr_udp_sm->udp_hdr.checksum = UDP_SM_LAST_WORD; // zero UDP checksum +} + +/* + * This is called when the DSP Rx chain has filled in a packet. + */ +bool +fw_sets_seqno_inspector(dbsm_t *sm, int buf_this) // returns false +{ + // insert the correct length into the control word and vrt header + rx_dsp_buff_t *buff = (rx_dsp_buff_t*)buffer_ram(buf_this); + size_t vrt_len = buffer_pool_status->last_line[buf_this]-1; + buff->control_word = MK_RX_CTRL_WORD(vrt_len); + buff->vrt_header[0] = (buff->vrt_header[0] & ~VRTH_PKT_SIZE_MASK) | (vrt_len & VRTH_PKT_SIZE_MASK); + + // queue up another rx command when required + if (auto_reload_command && --streaming_frame_count == 0){ + streaming_frame_count = FRAMES_PER_CMD; + sr_rx_ctrl->time_secs = 0; + sr_rx_ctrl->time_ticks = 0; //enqueue last command + } + + return false; // we didn't handle the packet +} + + +inline static void +buffer_irq_handler(unsigned irq) +{ + uint32_t status = buffer_pool_status->status; + + dbsm_process_status(&dsp_tx_sm, status); + dbsm_process_status(&dsp_rx_sm, status); +} + +int +main(void) +{ + u2_init(); + + putstr("\nTxRx-NEWETH\n"); + print_mac_addr(ethernet_mac_addr()->addr); + newline(); + print_ip_addr(get_ip_addr()); newline(); + printf("Control protocol version: %d\n", USRP2_PROTO_VERSION); + + ethernet_register_link_changed_callback(link_changed_callback); + ethernet_init(); + + register_mac_addr(ethernet_mac_addr()); + register_ip_addr(get_ip_addr()); + + register_udp_listener(USRP2_UDP_CTRL_PORT, handle_udp_ctrl_packet); + register_udp_listener(USRP2_UDP_DATA_PORT, handle_udp_data_packet); + + hal_set_led_src(0, LEDS_SW); + +#if 0 + // make bit 15 of Tx gpio's be a s/w output + hal_gpio_set_sel(GPIO_TX_BANK, 15, 's'); + hal_gpio_set_ddr(GPIO_TX_BANK, 0x8000, 0x8000); +#endif + +//set them all to the atr settings by default +hal_gpio_set_sels(GPIO_TX_BANK, "aaaaaaaaaaaaaaaa"); +hal_gpio_set_sels(GPIO_RX_BANK, "aaaaaaaaaaaaaaaa"); + + output_regs->debug_mux_ctrl = 1; +#if DEBUG_MODE + hal_gpio_set_sels(GPIO_TX_BANK, "0000000000000000"); + hal_gpio_set_sels(GPIO_RX_BANK, "0000000000000000"); + hal_gpio_set_ddr(GPIO_TX_BANK, 0xffff, 0xffff); + hal_gpio_set_ddr(GPIO_RX_BANK, 0xffff, 0xffff); +#endif + + + // initialize double buffering state machine for ethernet -> DSP Tx + + dbsm_init(&dsp_tx_sm, DSP_TX_BUF_0, + &dsp_tx_recv_args, &dsp_tx_send_args, + eth_pkt_inspector); + + + // initialize double buffering state machine for DSP RX -> Ethernet + + dbsm_init(&dsp_rx_sm, DSP_RX_BUF_0, + &dsp_rx_recv_args, &dsp_rx_send_args, + fw_sets_seqno_inspector); + + + // tell app_common that this dbsm could be sending to the ethernet + ac_could_be_sending_to_eth = &dsp_rx_sm; + + sr_tx_ctrl->clear_state = 1; + bp_clear_buf(DSP_TX_BUF_0); + bp_clear_buf(DSP_TX_BUF_1); + + // kick off the state machine + dbsm_start(&dsp_tx_sm); + + //int which = 0; + + while(1){ + // hal_gpio_write(GPIO_TX_BANK, which, 0x8000); + // which ^= 0x8000; + + buffer_irq_handler(0); + + int pending = pic_regs->pending; // poll for under or overrun + + if (pending & PIC_UNDERRUN_INT){ + dbsm_handle_tx_underrun(&dsp_tx_sm); + pic_regs->pending = PIC_UNDERRUN_INT; // clear interrupt + putchar('U'); + } + + if (pending & PIC_OVERRUN_INT){ + dbsm_handle_rx_overrun(&dsp_rx_sm); + pic_regs->pending = PIC_OVERRUN_INT; // clear pending interrupt + + // FIXME Figure out how to handle this robustly. + // Any buffers that are emptying should be allowed to drain... + + if (auto_reload_command){ + // restart_streaming(); + // FIXME report error + } + else { + // FIXME report error + } + putchar('O'); + } + } +} diff --git a/host/docs/usrp2.rst b/host/docs/usrp2.rst index 32e7374c8..092332442 100644 --- a/host/docs/usrp2.rst +++ b/host/docs/usrp2.rst @@ -39,7 +39,7 @@ Run the following commands: ./configure --host=mb make -*The image file will be ./apps/txrx.bin* +*The image file will be ./apps/txrx_uhd.bin* ------------------------------------------------------------------------ Load the images onto the SD card -- cgit v1.2.3