#include <usb.h>
#include <semaphore.h>
#include <sys/types.h>
#include <pthread.h>

#define DIGILENT_PRODUCT_ID 0x0005
#define DIGILENT_VENDOR_ID  0x1443

usb_dev_handle 		*dev;			// USB device handle for opening device
struct usb_device *udev;
sem_t iflock;


#define NEXSDR_RFPATH_BYPASS 0
#define NEXSDR_RFPATH_LPFLNA 1
#define NEXSDR_LNAGAIN_HIGH  1
#define NEXSDR_LNAGAIN_LOW   0
#define NEXSDR_LNASLOPE_POS  0
#define NEXSDR_LNASLOPE_NEG  1
#define NEXSDR_ADC_RATE      76800000

#define	A8201_REG_MCLKMODE	0x00
#define	A8201_REG_CIC		0x05
#define	A8201_REG_FIR1		0x07
#define	A8201_REG_FIR2A		0x08
#define	A8201_REG_FIR2B		0x09
#define	A8201_REG_INTLV		0x0A
#define	A8201_REG_DAC		0x0B
#define	A8201_REG_PGAPD		0x0C

#define A8201_PWD		0x01

#define REGADR_FIFO0      0x00
#define REGADR_FIFO1      0x08
#define REGADR_CTRL       0x10
#define REGADR_SPI_CMD_LO 0x1A
#define REGADR_SPI_CMD_HI 0x1B
#define REGADR_SPI_DAT_LO 0x18
#define REGADR_SPI_DAT_HI 0x19
#define REGADR_SPI_SEND   0x1C
#define REGADR_FIFO_OVF   0x20

#define CTRL_RFSW         0x01
#define CTRL_HILO         0x02
#define CTRL_MODE         0x04
#define CTRL_RESET        0x08
#define CTRL_SYNC         0x10
#define CTRL_OVF_RST      0x20
#define CTRL_FIFO0_RST    0x40
#define CTRL_FIFO1_RST    0x80

#define NEXSDR_BLOCK_SIZE 4096
#define NEXSDR_NUM_BUFFERS 16
#define ADC_MULT		65536

static void quisk_start_chas_rx1(void);
static void quisk_stop_chas_rx1(void);


/* AFEDRI8201 FIR Filter Coefficients (CIC droop compensation and LPF)	*/
static int fir1[32] = {		// parameters for 192ks/s
	-5,-44,-10,54,34,-66,-76,70,140,-50,-224,-11,312,132,-383,/*-321*/-322,401,580,-324,
	/*-895*/-896,102,/*1238*/1239,328,-1562,-1063,1785,2307,/*-1717*/-1718,
	/*-4696*/-4697,/*432*/431,11355,/*17121*/17122
};

/* AFEDRI8201 FIR FIlter Coefficients (LPF)	(2nd FIR, these are for 90% filter)*/
static int fir2[63] = {
	-4,-14,0,15,5,-15,-11,15,18,-11,-26,5,34,6,-39,-21,41,39,-36,-60,23,80,0,-95,
	-32,101,72,-94,-117,71,160,-28,-196,-33,216,111,-213,-200,179,292,-109,-375,0,
	435,147,-459,/*-328*/-329,430,536,-329,-759,138,983,173,-1194,-657,1377,1454,
	-1519,/*-3082*/-3083,1608,/*10299*/10300,/*14751*/14752
};

