diff options
author | Josh Blum <josh@joshknows.com> | 2010-03-15 16:00:07 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2010-03-15 16:00:07 -0700 |
commit | e4997af8453980922b469e5d3b66a7b26910dad3 (patch) | |
tree | f19255b35f70670f7674334299453189b2c2a880 | |
parent | 7590f187d0414fd05e23828488166bc4bc88df26 (diff) | |
download | uhd-e4997af8453980922b469e5d3b66a7b26910dad3.tar.gz uhd-e4997af8453980922b469e5d3b66a7b26910dad3.tar.bz2 uhd-e4997af8453980922b469e5d3b66a7b26910dad3.zip |
Ability to burn mac addr and ip addr to usrp2 (over ip/udp for now).
Added firmware support and usrp2 burner host app.
-rw-r--r-- | firmware/microblaze/apps/txrx.c | 44 | ||||
-rw-r--r-- | firmware/microblaze/include/usrp2_i2c_addr.h | 1 | ||||
-rw-r--r-- | firmware/microblaze/lib/ethernet.c | 76 | ||||
-rw-r--r-- | firmware/microblaze/lib/ethernet.h | 11 | ||||
-rw-r--r-- | host/apps/CMakeLists.txt | 5 | ||||
-rw-r--r-- | host/apps/usrp2_burner.cpp | 83 | ||||
-rw-r--r-- | host/include/uhd/wax.hpp | 14 |
7 files changed, 194 insertions, 40 deletions
diff --git a/firmware/microblaze/apps/txrx.c b/firmware/microblaze/apps/txrx.c index 18bbdd23d..97376ffbd 100644 --- a/firmware/microblaze/apps/txrx.c +++ b/firmware/microblaze/apps/txrx.c @@ -153,9 +153,12 @@ static eth_mac_addr_t get_my_eth_mac_addr(void){ } static struct ip_addr get_my_ip_addr(void){ - struct ip_addr addr; - addr.addr = 192 << 24 | 168 << 16 | 10 << 8 | 2 << 0; - return addr; + return *get_ip_addr(); +} + +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( @@ -170,21 +173,13 @@ void handle_udp_data_packet( printf("Storing for fast path:\n"); printf(" source mac addr: "); print_mac_addr(fp_mac_addr_src.addr); newline(); - printf(" source ip addr: %d.%d.%d.%d\n", - ((const unsigned char*)&fp_socket_src.addr.addr)[0], - ((const unsigned char*)&fp_socket_src.addr.addr)[1], - ((const unsigned char*)&fp_socket_src.addr.addr)[2], - ((const unsigned char*)&fp_socket_src.addr.addr)[3] - ); + 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: %d.%d.%d.%d\n", - ((const unsigned char*)&fp_socket_dst.addr.addr)[0], - ((const unsigned char*)&fp_socket_dst.addr.addr)[1], - ((const unsigned char*)&fp_socket_dst.addr.addr)[2], - ((const unsigned char*)&fp_socket_dst.addr.addr)[3] - ); + printf(" destination ip addr: "); + print_ip_addr(&fp_socket_dst.addr); newline(); printf(" destination udp port: %d\n", fp_socket_dst.port); newline(); } @@ -217,14 +212,24 @@ void handle_udp_ctrl_packet( ******************************************************************/ case USRP2_CTRL_ID_GIVE_ME_YOUR_IP_ADDR_BRO: ctrl_data_out.id = USRP2_CTRL_ID_THIS_IS_MY_IP_ADDR_DUDE; - struct ip_addr ip_addr = get_my_ip_addr(); - memcpy(&ctrl_data_out.data.ip_addr, &ip_addr, sizeof(ip_addr)); + 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; - eth_mac_addr_t mac_addr = get_my_eth_mac_addr(); - memcpy(&ctrl_data_out.data.mac_addr, &mac_addr, sizeof(mac_addr)); + 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: @@ -720,6 +725,7 @@ main(void) putstr("\nTxRx-NEWETH\n"); print_mac_addr(ethernet_mac_addr()->addr); newline(); + print_ip_addr(get_ip_addr()); newline(); ethernet_register_link_changed_callback(link_changed_callback); ethernet_init(); diff --git a/firmware/microblaze/include/usrp2_i2c_addr.h b/firmware/microblaze/include/usrp2_i2c_addr.h index 4111cdd55..46f5a7556 100644 --- a/firmware/microblaze/include/usrp2_i2c_addr.h +++ b/firmware/microblaze/include/usrp2_i2c_addr.h @@ -41,6 +41,7 @@ #define MBOARD_REV_LSB 0x00 #define MBOARD_REV_MSB 0x01 #define MBOARD_MAC_ADDR 0x02 +#define MBOARD_IP_ADDR 0x0C // format of daughterboard EEPROM diff --git a/firmware/microblaze/lib/ethernet.c b/firmware/microblaze/lib/ethernet.c index 5402a6c9f..757a36ce4 100644 --- a/firmware/microblaze/lib/ethernet.c +++ b/firmware/microblaze/lib/ethernet.c @@ -271,58 +271,100 @@ ethernet_init(void) } static bool -unprogrammed(const eth_mac_addr_t *t) +unprogrammed(const void *t, size_t len) { int i; + uint8_t *p = (uint8_t *)t; bool all_zeros = true; bool all_ones = true; - for (i = 0; i < 6; i++){ - all_zeros &= t->addr[i] == 0x00; - all_ones &= t->addr[i] == 0xff; + for (i = 0; i < len; i++){ + all_zeros &= p[i] == 0x00; + all_ones &= p[i] == 0xff; } return all_ones | all_zeros; } -static int8_t src_addr_initialized = false; -static eth_mac_addr_t src_addr = {{ +//////////////////// MAC Addr Stuff /////////////////////// + +static int8_t src_mac_addr_initialized = false; +static eth_mac_addr_t src_mac_addr = {{ 0x00, 0x50, 0xC2, 0x85, 0x3f, 0xff }}; const eth_mac_addr_t * ethernet_mac_addr(void) { - if (!src_addr_initialized){ // fetch from eeprom - src_addr_initialized = true; + if (!src_mac_addr_initialized){ // fetch from eeprom + src_mac_addr_initialized = true; // if we're simulating, don't read the EEPROM model, it's REALLY slow - if (hwconfig_simulation_p()) - return &src_addr; + if (hwconfig_simulation_p()) + return &src_mac_addr; eth_mac_addr_t tmp; - bool ok = eeprom_read(I2C_ADDR_MBOARD, MBOARD_MAC_ADDR, &tmp.addr[0], 6); - if (!ok || unprogrammed(&tmp)){ + bool ok = eeprom_read(I2C_ADDR_MBOARD, MBOARD_MAC_ADDR, &tmp, sizeof(tmp)); + if (!ok || unprogrammed(&tmp, sizeof(tmp))){ // use the default } else - src_addr = tmp; + src_mac_addr = tmp; } - return &src_addr; + return &src_mac_addr; } bool ethernet_set_mac_addr(const eth_mac_addr_t *t) { - bool ok = eeprom_write(I2C_ADDR_MBOARD, MBOARD_MAC_ADDR, &t->addr[0], 6); + bool ok = eeprom_write(I2C_ADDR_MBOARD, MBOARD_MAC_ADDR, &t, sizeof(eth_mac_addr_t)); if (ok){ - src_addr = *t; - src_addr_initialized = true; + src_mac_addr = *t; + src_mac_addr_initialized = true; eth_mac_set_addr(t); } return ok; } +//////////////////// IP Addr Stuff /////////////////////// + +static int8_t src_ip_addr_initialized = false; +static struct ip_addr src_ip_addr = { + (192 << 24 | 168 << 16 | 10 << 8 | 2 << 0) +}; + + +const struct ip_addr *get_ip_addr(void) +{ + if (!src_ip_addr_initialized){ // fetch from eeprom + src_ip_addr_initialized = true; + + // if we're simulating, don't read the EEPROM model, it's REALLY slow + if (hwconfig_simulation_p()) + return &src_ip_addr; + + struct ip_addr tmp; + bool ok = eeprom_read(I2C_ADDR_MBOARD, MBOARD_IP_ADDR, &tmp, sizeof(tmp)); + if (!ok || unprogrammed(&tmp, sizeof(tmp))){ + // use the default + } + else + src_ip_addr = tmp; + } + + return &src_ip_addr; +} + +bool set_ip_addr(const struct ip_addr *t){ + bool ok = eeprom_write(I2C_ADDR_MBOARD, MBOARD_IP_ADDR, &t, sizeof(struct ip_addr)); + if (ok){ + src_ip_addr = *t; + src_ip_addr_initialized = true; + } + + return ok; +} + int ethernet_check_errors(void) { diff --git a/firmware/microblaze/lib/ethernet.h b/firmware/microblaze/lib/ethernet.h index 70b7077c6..8c6d8b567 100644 --- a/firmware/microblaze/lib/ethernet.h +++ b/firmware/microblaze/lib/ethernet.h @@ -20,6 +20,7 @@ #define INCLUDED_ETHERNET_H #include <net/eth_mac_addr.h> +#include <lwip/ip_addr.h> #include <stdbool.h> typedef void (*ethernet_link_changed_callback_t)(int speed); @@ -48,6 +49,16 @@ const eth_mac_addr_t *ethernet_mac_addr(void); */ bool ethernet_set_mac_addr(const eth_mac_addr_t *t); +/*! + * \returns IP address + */ +const struct ip_addr *get_ip_addr(void); + +/*! + * \brief write ip address to eeprom and begin using it + */ +bool set_ip_addr(const struct ip_addr *t); + /* * \brief read RMON regs and return error mask diff --git a/host/apps/CMakeLists.txt b/host/apps/CMakeLists.txt index f4428f958..4deb41965 100644 --- a/host/apps/CMakeLists.txt +++ b/host/apps/CMakeLists.txt @@ -16,7 +16,8 @@ # ADD_EXECUTABLE(discover_usrps discover_usrps.cpp) - TARGET_LINK_LIBRARIES(discover_usrps uhd) - INSTALL(TARGETS discover_usrps RUNTIME DESTINATION ${RUNTIME_DIR}) + +ADD_EXECUTABLE(usrp2_burner usrp2_burner.cpp) +TARGET_LINK_LIBRARIES(usrp2_burner uhd) diff --git a/host/apps/usrp2_burner.cpp b/host/apps/usrp2_burner.cpp new file mode 100644 index 000000000..08ec8daf9 --- /dev/null +++ b/host/apps/usrp2_burner.cpp @@ -0,0 +1,83 @@ +// +// 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 <http://www.gnu.org/licenses/>. +// + +#include <uhd/usrp/usrp2.hpp> +#include <uhd/props.hpp> +#include <boost/program_options.hpp> +#include <boost/format.hpp> +#include <iostream> + +namespace po = boost::program_options; + +int main(int argc, char *argv[]){ + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "help message") + ("addr", po::value<std::string>(), "resolvable network address") + ("new-ip", po::value<std::string>(), "new ip address (optional)") + ("new-mac", po::value<std::string>(), "new mac address (optional)") + ; + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + //print the help message + if (vm.count("help")){ + std::cout << boost::format("USRP2 Burner %s") % desc << std::endl; + return ~0; + } + + //load the options into the address + uhd::device_addr_t device_addr; + if (vm.count("addr")){ + device_addr["addr"] = vm["addr"].as<std::string>(); + } + else{ + std::cerr << "Error: missing addr option" << std::endl; + return ~0; + } + + //create a usrp2 device + uhd::device::sptr u2_dev = uhd::usrp::usrp2::make(device_addr); + //FIXME usees the default mboard for now (until the mimo link is supported) + wax::obj u2_mb = (*u2_dev)[uhd::DEVICE_PROP_MBOARD]; + + //try to set the new ip (if provided) + if (vm.count("new-ip")){ + std::cout << "Burning a new ip address into the usrp2 eeprom:" << std::endl; + std::string old_ip = u2_mb[std::string("ip-addr")].as<std::string>(); + std::cout << boost::format(" Old IP Address: %s") % old_ip << std::endl; + std::string new_ip = vm["new-ip"].as<std::string>(); + std::cout << boost::format(" New IP Address: %s") % new_ip << std::endl; + u2_mb[std::string("ip-addr")] = new_ip; + std::cout << " Done" << std::endl; + } + + //try to set the new mac (if provided) + if (vm.count("new-mac")){ + std::cout << "Burning a new mac address into the usrp2 eeprom:" << std::endl; + std::string old_mac = u2_mb[std::string("mac-addr")].as<std::string>(); + std::cout << boost::format(" Old MAC Address: %s") % old_mac << std::endl; + std::string new_mac = vm["new-mac"].as<std::string>(); + std::cout << boost::format(" New MAC Address: %s") % new_mac << std::endl; + u2_mb[std::string("mac-addr")] = new_mac; + std::cout << " Done" << std::endl; + } + + return 0; +} diff --git a/host/include/uhd/wax.hpp b/host/include/uhd/wax.hpp index 1d5054351..4fd54ba14 100644 --- a/host/include/uhd/wax.hpp +++ b/host/include/uhd/wax.hpp @@ -124,6 +124,17 @@ namespace wax{ */ const std::type_info & type(void) const; + /*! + * Cast this obj into the desired type. + * Usage myobj.as<type>() + * + * \return an object of the desired type + * \throw wax::bad_cast when the cast fails + */ + template<class T> T as(void) const{ + return boost::any_cast<T>(resolve()); + } + private: //private interface (override in subclasses) virtual void get(const obj &, obj &); @@ -137,7 +148,6 @@ namespace wax{ * \return a boost any type with contents */ boost::any resolve(void) const; - template<class T> friend T cast(const obj &); //private contents of this obj boost::any _contents; @@ -159,7 +169,7 @@ namespace wax{ * \throw wax::bad_cast when the cast fails */ template<class T> T cast(const obj &val){ - return boost::any_cast<T>(val.resolve()); + return val.as<T>(); } } //namespace wax |