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

📄 quadcontmultichannelbank.cpp

📁 ICS564 Loop demo through PCI data path. DAC in Quadrature Upconverter mode .
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		if(0 == chan) dacConfig.dac1_fud = ICS564_DISABLE;
		if(1 == chan) dacConfig.dac2_fud = ICS564_DISABLE;
		if(2 == chan) dacConfig.dac3_fud = ICS564_DISABLE;
		if(3 == chan) dacConfig.dac4_fud = ICS564_DISABLE;
		if(ICS564_ERROR == ics564DACConfigSet(hDevice, &dacConfig)){
			printf("Error: ics564DACConfigSet failed.\n");
			goto MainError;
		}		

		/*  set mode */
		/*  FUD/PDCLK dual pin is now PDCLK	 */
		dacInstr.data_byte = 0x0; /* quadrature upc mode */
		dacInstr.address   = 1;
		if(ICS564_ERROR == ics564DACInstructionSet(hDevice, &dacInstr)){
			printf("Error: ics564DACInstructionSet failed\n");
			goto MainError;
		}
		Sleep(1); /*  wait for DAC serial register transfer to finish	 */
	} /* END for(chan = 0; chan < numChan; chan++) */

	memset(&miscDACConfig,0,sizeof(ICS564_DAC_RESET));
	if((dacClkRate/interpolation) >= 24.0) {
		/*  activate PDCLK pll for freq>=24 MHz */
		miscDACConfig.dac1_clkr = ICS564_ENABLE;
		miscDACConfig.dac2_clkr = ICS564_ENABLE;
		miscDACConfig.dac3_clkr = ICS564_ENABLE;
		miscDACConfig.dac4_clkr = ICS564_ENABLE;
		if(ICS564_ERROR == ics564DACResetSet(hDevice, &miscDACConfig)){
			printf("Error: ics564DACResetSet failed\n");
			goto MainError;
		}
	}
	else {
		miscDACConfig.dac1_clkr = ICS564_DISABLE;
		miscDACConfig.dac2_clkr = ICS564_DISABLE;
		miscDACConfig.dac3_clkr = ICS564_DISABLE;
		miscDACConfig.dac4_clkr = ICS564_DISABLE;
		if(ICS564_ERROR == ics564DACResetSet(hDevice, &miscDACConfig)){
			printf("Error: ics564DACResetSet failed\n");
			goto MainError;
		}
	}

	memset(&fifoControl,0,sizeof(ICS564_FIFO_CONTROL));
	fifoControl.fifo_select  = ICS564_ALLDACS_SELECT;/* selects all fifos */
	fifoControl.fifo_mrs     = ICS564_DISABLE;
	MAKE_FIFO_2K_SAMPLES(1,fifoControl);
	MAKE_FIFO_2K_SAMPLES(2,fifoControl);
	MAKE_FIFO_2K_SAMPLES(3,fifoControl);
	MAKE_FIFO_2K_SAMPLES(4,fifoControl);
	MAKE_FIFO_2K_SAMPLES(5,fifoControl);
	if(ICS564_ERROR == ics564FifoControlSet(hDevice, &fifoControl)){
		printf("Error: ics564FifoControlSet failed\n");
		goto MainError;
	}

	fifoControl.fifo_mrs = ICS564_ENABLE; /*  reset fifos */
	if(ICS564_ERROR == ics564FifoControlSet(hDevice, &fifoControl)){
		printf("Error: ics564FifoControlSet failed\n");
		goto MainError;
	}

	fifoControl.fifo_mrs = ICS564_DISABLE; /*  clear reset */
	if(ICS564_ERROR == ics564FifoControlSet(hDevice, &fifoControl)){
		printf("Error: ics564FifoControlSet failed\n");
		goto MainError;
	}

	memset(&brdControl,0,sizeof(ICS564_CONTROL));
	/*  opmode for board, loop, 1-shot, cont	 */
	brdControl.dac1_mode   = ICS564_CONTINUOUS;
	brdControl.dac2_mode   = ICS564_CONTINUOUS;
	brdControl.dac3_mode   = ICS564_CONTINUOUS;
	brdControl.dac4_mode   = ICS564_CONTINUOUS;
	brdControl.trig_sel    = ICS564_TRIG_INTERNAL;
	brdControl.dac1_dpd    = ICS564_POWER_NORMAL;
	brdControl.dac2_dpd    = ICS564_POWER_NORMAL;
	brdControl.dac3_dpd    = ICS564_POWER_NORMAL;
	brdControl.dac4_dpd    = ICS564_POWER_NORMAL;
	brdControl.en          = ICS564_ENABLE;
	brdControl.dac_clk_sel = ICS564_CLOCK_INTERNAL;
	brdControl.dac_clk_en  = ICS564_DISABLE;

	if(ICS564_ERROR == ics564ControlSet(hDevice,&brdControl)){
		printf("Error: ics564ControlGet failed\n");
		goto MainError;
	}
	Sleep(10); /* let clk stabilize */
	
	/* clear LD bits before ics564LoadSet(), hardware requirement */
	fifoControl.ff1_ld = 0;fifoControl.ff2_ld = 0;fifoControl.ff3_ld = 0;
	fifoControl.ff4_ld = 0;fifoControl.ff5_ld = 0;
	if(ICS564_ERROR == ics564FifoControlSet(hDevice, &fifoControl)){
		printf("Error: ics564FifoControlSet failed\n");
		goto MainError;
	}

	memset(&transferRegs,0,sizeof(ICS564_LOAD));
	/*  load clpd regs */
	if(numChan > 0) transferRegs.load_dac1_regs = ICS564_ENABLE;
	if(numChan > 1) transferRegs.load_dac2_regs = ICS564_ENABLE;
	if(numChan > 2) transferRegs.load_dac3_regs = ICS564_ENABLE;
	if(numChan > 3) transferRegs.load_dac4_regs = ICS564_ENABLE;
	/*  value gates trigger, wr starts register Tx for 4 cplds that have no PCI connection */
	if(ICS564_ERROR == ics564LoadSet(hDevice, &transferRegs)) {
		printf("Error: ics564LoadSet failed\n");
		goto MainError;
	}
	Sleep(1); /*  wait for reg. transfer to finish	 */

	/*  SET LD BITS IF NOT ALREADY SET!!! */
	/*  set LD bits ( after ics564LoadSet() ) */
	/*  ** if LD bits are left low data will not be written to fifo - see IDT fifo data sheet ** */
	fifoControl.fifo_mrs = ICS564_DISABLE; /*  clear reset */
	fifoControl.ff1_ld = ICS564_ENABLE;
	fifoControl.ff2_ld = ICS564_ENABLE;
	fifoControl.ff3_ld = ICS564_ENABLE;
	fifoControl.ff4_ld = ICS564_ENABLE;
	fifoControl.ff5_ld = ICS564_ENABLE;
	if(ICS564_ERROR == ics564FifoControlSet(hDevice, &fifoControl)){
		printf("Error: ics564FifoControlSet failed\n");
		goto MainError;
	}

	/* Build data in the allocated buffer */
	inputRate    = (double)dacClkRate/(double)interpolation; 
	mhertzPerBin = inputRate/((double)numSamples/2.0); 
	/* generate and write data to all active channels */
	for(chan = 0; chan < numChan; chan++) {
		/* Get output frequency increased by 1MHz for each channel */
		/* Ensure that the baseband frequencies for all the channels meet the limits noted earlier */
		binNumber    = (int) floor((basebandFrequency+(double)chan)/mhertzPerBin); 

		printf("\n\rBaseband frequency = %lf MHz on channel %d\n", 
			(basebandFrequency+(double)chan), chan+1);

		lowerData=0;
		upperData=0;
		/*  Generate Baseband Data. lowerData is Q, upperData is I*/
		for (i = 0; i < numSamples/2; i++) /*  complex input format */
		{
			angle = 2.0*3.1415927*((double)(i*binNumber))/((double)numSamples/2.0); 
			upperData   = ((int)floor((double)0x7fff * (cos(angle))))&0xFFFF;/* real part */
			lowerData   = ((int)floor((double)0x7fff * (sin(angle))))&0xFFFF;/* imaginary part */
			upperData <<= 16;
			buf[i]      = upperData | lowerData;
		}

		memset(&userControl,0,sizeof(ICS564_USER_CONTROL));
		// bits 1,2 used to route data to dac fifos, "00" dac1...."11" dac4
		userControl.ready = ICS564_ENABLE;
		userControl.enable_dac1 = (0 == chan)?ICS564_ENABLE:ICS564_DISABLE;
		userControl.enable_dac2 = (1 == chan)?ICS564_ENABLE:ICS564_DISABLE;
		userControl.enable_dac3 = (2 == chan)?ICS564_ENABLE:ICS564_DISABLE;
		userControl.enable_dac4 = (3 == chan)?ICS564_ENABLE:ICS564_DISABLE;
		if(ICS564_ERROR == ics564UserControlSet (hDevice, &userControl)){
			printf("Error: ics564UserControlSet failed.\n");
			goto MainError;
		}

		if(!WriteFile(hDevice, buf, bytesToWrite, &temp, NULL)) {
			LPVOID lpErrBuffer = NULL;
			DWORD  ErrCode; 

			ErrCode = GetLastError();
			FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
					FORMAT_MESSAGE_FROM_SYSTEM, NULL, ErrCode,
					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
					(LPTSTR) &lpErrBuffer, 0, NULL );

			printf ("Error: Write data failed\n");
			printf("    error code = %u\n", ErrCode );
			printf("    message    = %s\n", (LPTSTR) lpErrBuffer );

			LocalFree( lpErrBuffer );
			goto MainError;
        	}

		printf("SHOULD SEE A %lf MHz SIGNAL (baseband + center) ON CHANNEL %d\n", 
			basebandFrequency+(double)(centerFrequency+2*chan), chan+1);
	}

	if(!brdControl.trig_sel){
		/*  trigger */
		trigger = ICS564_ENABLE;
		if(ICS564_ERROR == ics564InternalTriggerSet(hDevice, &trigger)){
			printf("Error: ics564InternalTriggerSet failed\n");
			goto MainError;
		}
	}

	printf("\nDAC in Quadrature Upconverter  mode\n"); 
	printf("Board in loop mode\n"); 
	printf("DAC interpolation = %d\n",(int)interpolation); 
	printf("DAC input rate = %d MHz\n",(int)inputRate); 
	printf("Hit a key to exit  ! \n");


  	temp = 0;
  	while (!kbhit ()) {

		fprintf (stdout, "Cycle: %d                \r", temp++);
		fflush(stdout);
		for (chan = 0; chan < numChan; chan++){
		if(chan==0) value = ics564WaitDAC1EmptyInt(hDevice, &timeout);
		if(chan==1) value = ics564WaitDAC2EmptyInt(hDevice, &timeout);
		if(chan==2) value = ics564WaitDAC3EmptyInt(hDevice, &timeout);
		if(chan==3) value = ics564WaitDAC4EmptyInt(hDevice, &timeout);
		if(ICS564_ERROR == value) {
			printf("Error: WaitDAC1EmptyInt failed\n");
		}

		memset(&userControl,0,sizeof(ICS564_USER_CONTROL));
		// bits 1,2 used to route data to dac fifos, "00" dac1...."11" dac4
		userControl.ready = ICS564_ENABLE;
		userControl.enable_dac1 = (0 == chan)?ICS564_ENABLE:ICS564_DISABLE;
		userControl.enable_dac2 = (1 == chan)?ICS564_ENABLE:ICS564_DISABLE;
		userControl.enable_dac3 = (2 == chan)?ICS564_ENABLE:ICS564_DISABLE;
		userControl.enable_dac4 = (3 == chan)?ICS564_ENABLE:ICS564_DISABLE;
		if(ICS564_ERROR == ics564UserControlSet (hDevice, &userControl)){
			printf("Error: ics564UserControlSet failed.\n");
			goto MainError;
		}

		/* write data when interupt indicates fifo running out */
		if(!WriteFile(hDevice, buf, bytesToWrite, &actualLength, NULL)) {
			LPVOID lpErrBuffer = NULL;
			DWORD  ErrCode; 

			ErrCode = GetLastError();
			FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
					FORMAT_MESSAGE_FROM_SYSTEM, NULL, ErrCode,
					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
					(LPTSTR) &lpErrBuffer, 0, NULL );

			printf ("Error: Loop Write failed\n");
			printf("    error code = %u\n", ErrCode );
			printf("    message    = %s\n", (LPTSTR) lpErrBuffer );

			LocalFree( lpErrBuffer );
			goto MainError;
		}
		}
	}
/*
  	temp = 0;
  	while (!kbhit ()) {
    		printf ("Cycle: %d                \r", temp++);
    		Sleep(10);    		
 	}
*/
	if(ICS564_ERROR == ics564BoardReset(hDevice)){
		printf("Error: ics564BoardReset failed\n");
	}

	// reset dacs - 
	miscDACConfig.dac1_rst = 1;miscDACConfig.dac2_rst = 1;
	miscDACConfig.dac3_rst = 1;miscDACConfig.dac4_rst = 1;
	if(ICS564_ERROR == ics564DACResetSet(hDevice, &miscDACConfig)){
		printf("Error: ics564DACResetSet failed\n");
	}

	//clear dac reset bits
	miscDACConfig.dac1_rst = 0;miscDACConfig.dac2_rst = 0;
	miscDACConfig.dac3_rst = 0;miscDACConfig.dac4_rst = 0;
	if(ICS564_ERROR == ics564DACResetSet(hDevice, &miscDACConfig)){
		printf("Error: ics564DACResetSet failed\n");
	}

MainError:
	if(NULL != buf) {
		free(buf);
		buf = NULL;
	}

	if (INVALID_HANDLE_VALUE != hDevice) {
		CloseHandle (hDevice);
		hDevice = INVALID_HANDLE_VALUE;
	}
}

⌨️ 快捷键说明

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