⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usrp_prims.cc

📁 这是用python语言写的一个数字广播的信号处理工具包。利用它
💻 CC
📖 第 1 页 / 共 3 页
字号:
/* -*- c++ -*- *//* * Copyright 2003,2004,2006 Free Software Foundation, Inc. *  * This file is part of GNU Radio *  * GNU Radio is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3, or (at your option) * any later version. *  * GNU Radio is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with GNU Radio; see the file COPYING.  If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, * Boston, MA 02110-1301, USA. */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include "usrp_prims.h"#include "usrp_commands.h"#include "usrp_ids.h"#include "usrp_i2c_addr.h"#include "fpga_regs_common.h"#include "fpga_regs_standard.h"#include <usb.h>#include <errno.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <time.h>		// FIXME should check with autoconf (nanosleep)#include <algorithm>#include <ad9862.h>#include <assert.h>extern "C" {#include "md5.h"};#define VERBOSE 0using namespace ad9862;static const int FIRMWARE_HASH_SLOT	= 0;static const int FPGA_HASH_SLOT 	= 1;static const int hash_slot_addr[2] = {  USRP_HASH_SLOT_0_ADDR,  USRP_HASH_SLOT_1_ADDR};static char *default_firmware_filename = "std.ihx";static char *default_fpga_filename     = "std_2rxhb_2tx.rbf";#include "std_paths.h"#include <stdio.h>static char *find_file (const char *filename, int hw_rev){  char **sp = std_paths;  static char path[1000];  char *s;  s = getenv("USRP_PATH");  if (s) {    snprintf (path, sizeof (path), "%s/rev%d/%s", s, hw_rev, filename);    if (access (path, R_OK) == 0)      return path;  }  while (*sp){    snprintf (path, sizeof (path), "%s/rev%d/%s", *sp, hw_rev, filename);    if (access (path, R_OK) == 0)      return path;    sp++;  }  return 0;}static const char *get_proto_filename(const std::string user_filename, const char *env_var, const char *def){  if (user_filename.length() != 0)    return user_filename.c_str();  char *s = getenv(env_var);  if (s && *s)    return s;  return def;}static void power_down_9862s (struct usb_dev_handle *udh);voidusrp_one_time_init (){  static bool first = true;  if (first){    first = false;    usb_init ();			// usb library init    usb_find_busses ();    usb_find_devices ();  }}voidusrp_rescan (){  usb_find_busses ();  usb_find_devices ();}// ----------------------------------------------------------------// Danger, big, fragile KLUDGE.  The problem is that we want to be// able to get from a usb_dev_handle back to a usb_device, and the// right way to do this is buried in a non-installed include file.static struct usb_device *dev_handle_to_dev (usb_dev_handle *udh){  struct usb_dev_handle_kludge {    int			 fd;    struct usb_bus	*bus;    struct usb_device	*device;  };  return ((struct usb_dev_handle_kludge *) udh)->device;}// ----------------------------------------------------------------/* * q must be a real USRP, not an FX2.  Return its hardware rev number. */intusrp_hw_rev (struct usb_device *q){  return q->descriptor.bcdDevice & 0x00FF;}/* * q must be a real USRP, not an FX2.  Return true if it's configured. */static bool_usrp_configured_p (struct usb_device *q){  return (q->descriptor.bcdDevice & 0xFF00) != 0;}boolusrp_usrp_p (struct usb_device *q){  return (q->descriptor.idVendor == USB_VID_FSF	  && q->descriptor.idProduct == USB_PID_FSF_USRP);}boolusrp_fx2_p (struct usb_device *q){  return (q->descriptor.idVendor == USB_VID_CYPRESS	  && q->descriptor.idProduct == USB_PID_CYPRESS_FX2);}boolusrp_usrp0_p (struct usb_device *q){  return usrp_usrp_p (q) && usrp_hw_rev (q) == 0;}boolusrp_usrp1_p (struct usb_device *q){  return usrp_usrp_p (q) && usrp_hw_rev (q) == 1;}boolusrp_usrp2_p (struct usb_device *q){  return usrp_usrp_p (q) && usrp_hw_rev (q) == 2;}boolusrp_unconfigured_usrp_p (struct usb_device *q){  return usrp_usrp_p (q) && !_usrp_configured_p (q);}boolusrp_configured_usrp_p (struct usb_device *q){  return usrp_usrp_p (q) && _usrp_configured_p (q);}// ----------------------------------------------------------------struct usb_device *usrp_find_device (int nth, bool fx2_ok_p){  struct usb_bus *p;  struct usb_device *q;  int	 n_found = 0;  usrp_one_time_init ();    p = usb_get_busses();  while (p != NULL){    q = p->devices;    while (q != NULL){      if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){	if (n_found == nth)	// return this one	  return q;	n_found++;		// keep looking      }      q = q->next;    }    p = p->next;  }  return 0;	// not found}static struct usb_dev_handle *usrp_open_interface (struct usb_device *dev, int interface, int altinterface){  struct usb_dev_handle *udh = usb_open (dev);  if (udh == 0)    return 0;  if (dev != dev_handle_to_dev (udh)){    fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);    abort ();  }#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)  // There's no get get_configuration function, and with some of the newer kernels  // setting the configuration, even if to the same value, hoses any other processes  // that have it open.  Hence we opt to not set it at all (We've only  // got a single configuration anyway).  This may hose the win32 stuff...  // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06  if (usb_set_configuration (udh, 1) < 0){    /*     * Ignore this error.       *     * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that     * it returns -EBUSY if _any_ of the interfaces of a device are open.     * We've only got a single configuration, so setting it doesn't even seem     * like it should be required.     */  }#endif  if (usb_claim_interface (udh, interface) < 0){    fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);    fprintf (stderr, "%s\n", usb_strerror());    usb_close (udh);    return 0;  }  if (usb_set_altinterface (udh, altinterface) < 0){    fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);    fprintf (stderr, "%s\n", usb_strerror());    usb_release_interface (udh, interface);    usb_close (udh);    return 0;  }  return udh;}struct usb_dev_handle *usrp_open_cmd_interface (struct usb_device *dev){  return usrp_open_interface (dev, USRP_CMD_INTERFACE, USRP_CMD_ALTINTERFACE);}struct usb_dev_handle *usrp_open_rx_interface (struct usb_device *dev){  return usrp_open_interface (dev, USRP_RX_INTERFACE, USRP_RX_ALTINTERFACE);}struct usb_dev_handle *usrp_open_tx_interface (struct usb_device *dev){  return usrp_open_interface (dev, USRP_TX_INTERFACE, USRP_TX_ALTINTERFACE);}boolusrp_close_interface (struct usb_dev_handle *udh){  // we're assuming that closing an interface automatically releases it.  return usb_close (udh) == 0;}// ----------------------------------------------------------------// write internal ram using Cypress vendor extensionstatic boolwrite_internal_ram (struct usb_dev_handle *udh, unsigned char *buf,		    int start_addr, size_t len){  int addr;  int n;  int a;  int quanta = MAX_EP0_PKTSIZE;  for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){    n = len + start_addr - addr;    if (n > quanta)      n = quanta;    a = usb_control_msg (udh, 0x40, 0xA0,			 addr, 0, (char *)(buf + (addr - start_addr)), n, 1000);    if (a < 0){      fprintf(stderr,"write_internal_ram failed: %s\n", usb_strerror());      return false;    }  }  return true;}// ----------------------------------------------------------------// whack the CPUCS register using the upload RAM vendor extensionstatic boolreset_cpu (struct usb_dev_handle *udh, bool reset_p){  unsigned char v;  if (reset_p)    v = 1;		// hold processor in reset  else    v = 0;	        // release reset  return write_internal_ram (udh, &v, 0xE600, 1);}// ----------------------------------------------------------------// Load intel format file into cypress FX2 (8051)static bool_usrp_load_firmware (struct usb_dev_handle *udh, const char *filename,		     unsigned char hash[USRP_HASH_SIZE]){  FILE	*f = fopen (filename, "ra");  if (f == 0){    perror (filename);    return false;  }  if (!reset_cpu (udh, true))	// hold CPU in reset while loading firmware    goto fail;    char s[1024];  int length;  int addr;  int type;  unsigned char data[256];  unsigned char checksum, a;  unsigned int b;  int i;  while (!feof(f)){    fgets(s, sizeof (s), f); /* we should not use more than 263 bytes normally */    if(s[0]!=':'){      fprintf(stderr,"%s: invalid line: \"%s\"\n", filename, s);      goto fail;    }    sscanf(s+1, "%02x", &length);    sscanf(s+3, "%04x", &addr);    sscanf(s+7, "%02x", &type);    if(type==0){      a=length+(addr &0xff)+(addr>>8)+type;      for(i=0;i<length;i++){	sscanf (s+9+i*2,"%02x", &b);	data[i]=b;	a=a+data[i];      }      sscanf (s+9+length*2,"%02x", &b);      checksum=b;      if (((a+checksum)&0xff)!=0x00){	fprintf (stderr, "  ** Checksum failed: got 0x%02x versus 0x%02x\n", (-a)&0xff, checksum);	goto fail;      }      if (!write_internal_ram (udh, data, addr, length))	goto fail;    }    else if (type == 0x01){      // EOF      break;    }    else if (type == 0x02){      fprintf(stderr, "Extended address: whatever I do with it?\n");      fprintf (stderr, "%s: invalid line: \"%s\"\n", filename, s);      goto fail;    }  }  // we jam the hash value into the FX2 memory before letting  // the cpu out of reset.  When it comes out of reset it  // may renumerate which will invalidate udh.  if (!usrp_set_hash (udh, FIRMWARE_HASH_SLOT, hash))    fprintf (stderr, "usrp: failed to write firmware hash slot\n");  if (!reset_cpu (udh, false))		// take CPU out of reset    goto fail;  fclose (f);  return true; fail:  fclose (f);  return false;}// ----------------------------------------------------------------// write vendor extension command to USRPstatic intwrite_cmd (struct usb_dev_handle *udh,	   int request, int value, int index,	   unsigned char *bytes, int len){  int	requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;  int r = usb_control_msg (udh, requesttype, request, value, index,			   (char *) bytes, len, 1000);  if (r < 0){    // we get EPIPE if the firmware stalls the endpoint.    if (errno != EPIPE)      fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -