void usbAInConfigR_USB2600(usb_dev_handle *udev, ScanList scanList[NCHAN_2600]) { static __u8 ScanList[15]; __u8 requesttype = (DEVICE_TO_HOST | VENDOR_TYPE | DEVICE_RECIPIENT); if (usbStatus_USB2600(udev) | AIN_SCAN_RUNNING) { usbAInScanStop_USB2600(udev); } usb_control_msg(udev, requesttype, AIN_CONFIG, 0x0, 0x0, (char *) ScanList, sizeof(ScanList), HS_DELAY); }
void usbAInConfig_USB2600(usb_dev_handle *udev, ScanList scanList[NCHAN_2600]) { /* This command reads or writes the analog input channel configurations. This command will result in a bus stall if an AInScan is currently running. mode: SINGLE_ENDED (Single-Ended) CALIBRATION (Calibration mode) range: 0: +/- 10V range 1: +/- 5V range 2: +/- 2V range 3: _/- 1V range */ int i; static __u8 ScanList[64]; __u8 requesttype = (HOST_TO_DEVICE | VENDOR_TYPE | DEVICE_RECIPIENT); for (i = 0; i < NCHAN_2600; i++) { if (scanList[i].channel >= 0 && scanList[i].channel < NCHAN_2600) { ScanList[i] = ((scanList[i].range << 5) | (scanList[i].channel & 0x1f)); } else { printf("Error in Scan List[%d] mode = %#x channel = %d range = %#x\n", i, scanList[i].mode, scanList[i].channel, scanList[i].range); return; } if (scanList[i].mode & LAST_CHANNEL) { break; } } if (usbStatus_USB2600(udev) | AIN_SCAN_RUNNING) { usbAInScanStop_USB2600(udev); } usb_control_msg(udev, requesttype, AIN_CONFIG, 0x0, i, (char *) &ScanList[0], sizeof(ScanList), HS_DELAY); }
int main (int argc, char **argv) { usb_dev_handle *udev = NULL; double frequency; double voltage; float temperature; float table_AIN[NGAINS_2600][2]; float table_AOUT[NCHAN_AO_26X7][2]; ScanList list[NCHAN_2600]; // scan list used to configure the A/D channels. int usb26X7 = FALSE; int ch; int i, flag; int nSamples = 0; __u8 input, timer; int temp, ret; __u8 options; char serial[9]; __u32 period; __u16 version; __u16 value; __u16 dataAIn[512]; // holds 16 bit unsigned analog input data __u16 dataAOut[128]; // holds 16 bit unsigned analog output data __u8 mode, gain, channel; udev = NULL; if ((udev = usb_device_find_USB_MCC(USB2637_PID))) { printf("Success, found a USB 2637.\n"); usb26X7 = TRUE; } else if ((udev = usb_device_find_USB_MCC(USB2633_PID))) { printf("Success, found a USB 2633.\n"); } else if ((udev = usb_device_find_USB_MCC(USB2623_PID))) { printf("Success, found a USB 2623.\n"); } else if ((udev = usb_device_find_USB_MCC(USB2627_PID))) { printf("Success, found a USB 2627.\n"); usb26X7 = TRUE; } else { printf("Failure, did not find a USB 2600!\n"); return 0; } // some initialization usbInit_2600(udev); usbBuildGainTable_USB2600(udev, table_AIN); for (i = 0; i < NGAINS_2600; i++) { printf("Gain: %d Slope = %f Offset = %f\n", i, table_AIN[i][0], table_AIN[i][1]); } if (usb26X7) { usbBuildGainTable_USB26X7(udev, table_AOUT); printf("\nAnalog Output Table\n"); for (i = 0; i < NCHAN_AO_26X7; i++) { printf("Gain: %d Slope = %f Offset = %f\n", i, table_AOUT[i][0], table_AOUT[i][1]); } } while(1) { printf("\nUSB 2600 Testing\n"); printf("----------------\n"); printf("Hit 'b' to blink\n"); printf("Hit 'c' to test counter\n"); printf("Hit 'd' to test digitial IO\n"); printf("Hit 'i' to test Analog Input\n"); printf("Hit 'I' to test Analog Input Scan\n"); printf("Hit 'o' to test Analog Output\n"); printf("Hit 'O' to test Analog Output Scan\n"); printf("Hit 'r' to reset the device\n"); printf("Hit 's' to get serial number\n"); printf("Hit 'S' to get Status\n"); printf("Hit 't' to test the timers\n"); printf("Hit 'T' to get temperature\n"); printf("Hit 'v' to get version numbers\n"); printf("Hit 'e' to exit\n"); while((ch = getchar()) == '\0' || ch == '\n'); switch(ch) { case 'b': /* test to see if LED blinks */ printf("Enter number or times to blink: "); scanf("%hhd", &options); usbBlink_USB2600(udev, options); break; case 'c': usbCounterInit_USB2600(udev, COUNTER0); printf("Connect A0 to CTR0\n"); usbDTristateW_USB2600(udev,0, 0x0); toContinue(); for (i = 0; i < 100; i++) { usbDLatchW_USB2600(udev, 0, 0x0); usbDLatchW_USB2600(udev, 0, 0x1); } printf("Count = %d. Should read 100.\n", usbCounter_USB2600(udev,COUNTER0)); break; case 'd': printf("\nTesting Digital I/O...\n"); printf("connect pins Port A [0-8] <--> Port B[0-8]\n"); usbDTristateW_USB2600(udev,0,0x00); usbDTristateW_USB2600(udev,1,0xff); printf("Digital Port 0 Tristate Register = %#x\n", usbDTristateR_USB2600(udev,0)); printf("Digital Port 1 Tristate Register = %#x\n", usbDTristateR_USB2600(udev,1)); do { printf("Enter a byte number [0-0xff]: " ); scanf("%x", &temp); usbDLatchW_USB2600(udev, 0, (__u16)temp); temp = usbDLatchR_USB2600(udev,0); input = usbDPort_USB2600(udev,1); printf("The number you entered = %#x Latched value = %#x\n\n",input, temp); for (i = 0; i < 8; i++) { printf("Bit %d = %d\n", i, (temp>>i)&0x1); } } while (toContinue()); break; case 'o': if (!usb26X7) { printf("Ananlog output only on USB-26X7 models.\n"); break; } printf("Output value on VDAC 0\n"); do { printf("Enter output voltage [-10 to 10]: "); scanf("%lf", &voltage); usbAOut_USB26X7(udev, 0, voltage, table_AOUT); } while (toContinue()); break; case 'O': if (!usb26X7) { printf("Analog output only on the USB-26X7 model.\n"); break; } channel = 0; printf("Test of Analog Output Scan.\n"); printf("Hook scope up to VDAC 0\n"); printf("Enter desired frequency of sine wave [Hz]: "); scanf("%lf", &frequency); frequency *= 128.; for (i = 0; i < 128; i++) { voltage = 10*sin(2.*M_PI*i/128.); voltage = (voltage/10.*32768. + 32768.); dataAOut[i] = voltage*table_AOUT[channel][0] + table_AOUT[channel][1]; // dataAOut[i] = 0x0; // dataAOut[i+1] = 0xffff; } usbAOutScanStop_USB26X7(udev); usbAOutScanStart_USB26X7(udev, 0, 0, frequency, (0x1 << channel)); printf("Hit \'s <CR>\' to stop "); flag = fcntl(fileno(stdin), F_GETFL); fcntl(0, F_SETFL, flag | O_NONBLOCK); do { if (usb_bulk_write(udev, USB_ENDPOINT_OUT|2, (char *) dataAOut, sizeof(dataAOut), 1000) < 0) { //perror("usb_bulk_write error in AOutScan."); } } while (!isalpha(getchar())); fcntl(fileno(stdin), F_SETFL, flag); usbAOutScanStop_USB26X7(udev); break; case 'e': cleanup_USB2600(udev); return 0; case 'i': printf("Input channel [0-7]: "); scanf("%hhd", &channel); printf("Gain Range for channel %d: 1 = +/-10V 2 = +/- 5V 3 = +/- 2V 4 = +/- 1V: ",channel); while((ch = getchar()) == '\0' || ch == '\n'); switch(ch) { case '1': gain = BP_10V; break; case '2': gain = BP_5V; break; case '3': gain = BP_2V; break; case '4': gain = BP_1V; break; default: gain = BP_10V; break; } mode = (LAST_CHANNEL | SINGLE_ENDED); list[0].range = gain; list[0].mode = mode; list[0].channel = channel; usbAInConfig_USB2600(udev, list); for (i = 0; i < 20; i++) { value = usbAIn_USB2600(udev, channel); value = rint(value*table_AIN[gain][0] + table_AIN[gain][1]); printf("Channel %d Mode = %#x Gain = %d Sample[%d] = %#x Volts = %lf\n", list[0].channel, list[0].mode, list[0].range, i, value, volts_USB2600(udev,gain,value)); usleep(50000); } break; case 'I': printf("Testing USB-2600 Analog Input Scan.\n"); usbAInScanStop_USB2600(udev); usbAInScanClearFIFO_USB2600(udev); printf("Enter number of samples (less than 512): "); scanf("%d", &nSamples); printf("Input channel [0-7]: "); scanf("%hhd", &channel); printf("Gain Range for channel %d: 1 = +/-10V 2 = +/- 5V 3 = +/- 2V 4 = +/- 1V: ",channel); while((ch = getchar()) == '\0' || ch == '\n'); switch(ch) { case '1': gain = BP_10V; break; case '2': gain = BP_5V; break; case '3': gain = BP_2V; break; case '4': gain = BP_1V; break; default: gain = BP_10V; break; } mode = (LAST_CHANNEL | SINGLE_ENDED); list[0].range = gain; list[0].mode = mode; list[0].channel = channel; usbAInConfig_USB2600(udev, list); printf("Enter sampling frequency [Hz]: "); scanf("%lf", &frequency); usbAInScanStart_USB2600(udev, nSamples, 0, frequency, 0xff, 0); sleep(1); ret = usbAInScanRead_USB2600(udev, nSamples, 1, dataAIn); printf("Number samples read = %d\n", ret/2); for (i = 0; i < nSamples; i++) { dataAIn[i] = rint(dataAIn[i]*table_AIN[gain][0] + table_AIN[gain][1]); printf("Channel %d Mode = %d Gain = %d Sample[%d] = %#x Volts = %lf\n", channel, mode, gain, i, dataAIn[i], volts_USB2600(udev,gain,dataAIn[i])); } break; case 'r': usbReset_USB2600(udev); return 0; break; case 's': usbGetSerialNumber_USB2600(udev, serial); printf("Serial number = %s\n", serial); break; case 'S': printf("Status = %#x\n", usbStatus_USB2600(udev)); break; case 't': printf("Enter timer [0-3]: "); scanf("%hhd", &timer); printf("Enter frequency of timer: "); scanf("%lf", &frequency); period = 64.E6/frequency - 1; usbTimerPeriodW_USB2600(udev, timer, period); usbTimerPulseWidthW_USB2600(udev, timer, period / 2); usbTimerCountW_USB2600(udev, timer, 0); usbTimerDelayW_USB2600(udev, timer, 0); usbTimerControlW_USB2600(udev, timer, 0x1); toContinue(); usbTimerControlW_USB2600(udev, timer, 0x0); break; case 'T': usbTemperature_USB2600(udev, &temperature); printf("Temperature = %.2f deg C or %.2f deg F \n", temperature, 9.0/5.0*temperature + 32.); break; case 'v': version = 0xbeef; usbFPGAVersion_USB2600(udev, &version); printf("FPGA version %02x.%02x\n", version >> 0x8, version & 0xff); break; default: break; } } }
void usbAInScanStart_USB2600(usb_dev_handle *udev, __u32 count, __u32 retrig_count, double frequency, __u8 packet_size, __u8 options) { /* This command starts the analog input channel scan. The gain ranges that are currently set on the desired channels will be used (these may be changed with AInConfig) This command will result in a bus stall if an AInScan is currently running. Notes: The pacer rate is set by an internal 32-bit incrementing timer running at a base rate of 64 MHz. The timer is controlled by pacer_period. If burst mode is specified, then this value is the period of the scan and the A/D is clocked at this maximum rate (1 MHz) for each channel in the scan. If burst mode is not specified, then this value is the period of the A/D readings. A pulse will be output at the AI_CLK_OUT pin at every pacer_period interval regardless of the mode. If pacer_period is set to 0, the device does not generate an A/D clock. It uses the AI_CLK_IN pin as the pacer source. Burst mode operates in the same fashion: if specified, the scan starts on every rising edge of AI_CLK_IN and the A/D is clocked at 1 MHz for the number of channels in the scan; if not specified, the A/D is clocked on every rising edge of AI_CLK_IN. The timer will be reset and sample acquired wien its value equals timer_period. The equation for calculating timer_period is: timer_period = [64MHz / (sample frequency)] - 1 The data will be returned in packets utilizing a bulk IN endpoint. The data will be in the format: lowchannel sample 0: lowchannel + 1 sample 0: ... :hichannel sample 0 lowchannel sample 1: lowchannel + 1 sample 1: ... :hichannel sample 1 ... lowchannel sample n: lowchannel + 1 sample n: ... :hichannel sample n The scan will not begin until the AInScanStart comman is sent (and any trigger conditiions are met.) Data will be sent until reaching the specified count or an AInScanStop comman is sent. The packet_size parameter is used for low sampling rates to avoid delays in receiving the sampled data. The buffer will be sent, rather than waiting for the buffer to fill. This mode should not be used for high sample rates in order to avoid data loss. The external trigger may be used to start data collection synchronously. If the bit is set, the device will wait until the appropriate trigger edge is detected, then begin sampling data at the specified rate. No messages will be sent until the trigger is detected. The retrigger mode option and the retrig_count parameter are only used if trigger is used. This option will cause the trigger to be rearmed after retrig_count samples are acquired, with a total of count samples being returned from the entire scan. */ struct t_AInScan { __u32 count; // The total number of scans to perform (0 for continuous scan) __u32 retrig_count; // The numer of scans to perform for each trigger in retrigger mode. __u32 pacer_period; // The pacer timer period value. (0 for AI_CLK_IN). __u8 packet_size; // Number of samples - 1 to transfer at a time. __u8 options; /* bit 0: 1 = burst mode bit 1: Reserved bit 2: Reserved bit 3: 1 = use trigger bit 4: Reserved bit 5: Reserved bit 6: 1 = retrigger mode, 0 = normal trigger bit 7: Reserved */ __u8 pad[2]; } AInScan; __u8 requesttype = (HOST_TO_DEVICE | VENDOR_TYPE | DEVICE_RECIPIENT); __u8 status; AInScan.count = count; AInScan.retrig_count = retrig_count; AInScan.pacer_period = rint((64.E6 / frequency) - 1); if (packet_size > wMaxPacketSize/2 - 1) packet_size = wMaxPacketSize/2 - 1; AInScan.packet_size = packet_size; AInScan.options = options; status = usbStatus_USB2600(udev); if (!(status & AIN_SCAN_DONE)) { printf("Analog In scan not done.\n"); usbAInScanStop_USB2600(udev); } /* Pack the data into 14 bytes */ usb_control_msg(udev, requesttype, AIN_SCAN_START, 0x0, 0x0, (char *) &AInScan, 14, HS_DELAY); }