long getCalibrationInfo(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint8 sendBuffer[8]; uint8 recBuffer[136]; int sentRec = 0; /* reading block 0 from memory */ sendBuffer[1] = (uint8)(0xF8); //command byte sendBuffer[2] = (uint8)(0x01); //number of data words sendBuffer[3] = (uint8)(0x2A); //extended command number sendBuffer[6] = (uint8)(0x00); sendBuffer[7] = (uint8)(0x00); //Blocknum = 0 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuffer, 8); if(sentRec < 8) { if(sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuffer, 136); if(sentRec < 136) { if(sentRec == 0) goto readError0; else goto readError1; } if(recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x41) || recBuffer[3] != (uint8)(0x2A)) goto commandByteError; //block data starts on byte 8 of the buffer caliInfo->unipolarSlope[0] = FPuint8ArrayToFPDouble(recBuffer + 8, 0); caliInfo->unipolarOffset[0] = FPuint8ArrayToFPDouble(recBuffer + 8, 8); caliInfo->unipolarSlope[1] = FPuint8ArrayToFPDouble(recBuffer + 8, 16); caliInfo->unipolarOffset[1] = FPuint8ArrayToFPDouble(recBuffer + 8, 24); caliInfo->unipolarSlope[2] = FPuint8ArrayToFPDouble(recBuffer + 8, 32); caliInfo->unipolarOffset[2] = FPuint8ArrayToFPDouble(recBuffer + 8, 40); caliInfo->unipolarSlope[3] = FPuint8ArrayToFPDouble(recBuffer + 8, 48); caliInfo->unipolarOffset[3] = FPuint8ArrayToFPDouble(recBuffer + 8, 56); /* reading block 1 from memory */ sendBuffer[7] = (uint8)(0x01); //Blocknum = 1 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuffer, 8); if(sentRec < 8) { if(sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuffer, 136); if(sentRec < 136) { if(sentRec == 0) goto readError0; else goto readError1; } if(recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x41) || recBuffer[3] != (uint8)(0x2A)) { goto commandByteError; } caliInfo->bipolarSlope = FPuint8ArrayToFPDouble(recBuffer + 8, 0); caliInfo->bipolarOffset = FPuint8ArrayToFPDouble(recBuffer + 8, 8); /* reading block 2 from memory */ sendBuffer[7] = (uint8)(0x02); //Blocknum = 2 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuffer, 8); if(sentRec < 8) { if(sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuffer, 136); if(sentRec < 136) { if(sentRec == 0) goto readError0; else goto readError1; } if(recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x41) || recBuffer[3] != (uint8)(0x2A)) { goto commandByteError; } caliInfo->DACSlope[0] = FPuint8ArrayToFPDouble(recBuffer + 8, 0); caliInfo->DACOffset[0] = FPuint8ArrayToFPDouble(recBuffer + 8, 8); caliInfo->DACSlope[1] = FPuint8ArrayToFPDouble(recBuffer + 8, 16); caliInfo->DACOffset[1] = FPuint8ArrayToFPDouble(recBuffer + 8, 24); caliInfo->tempSlope = FPuint8ArrayToFPDouble(recBuffer + 8, 32); caliInfo->tempSlopeLow = FPuint8ArrayToFPDouble(recBuffer + 8, 48); caliInfo->calTemp = FPuint8ArrayToFPDouble(recBuffer + 8, 64); caliInfo->Vref = FPuint8ArrayToFPDouble(recBuffer + 8, 72); caliInfo->VrefDiv2 = FPuint8ArrayToFPDouble(recBuffer + 8, 88); caliInfo->VsSlope = FPuint8ArrayToFPDouble(recBuffer + 8, 96); /* reading block 3 from memory */ sendBuffer[7] = (uint8)(0x03); //Blocknum = 3 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuffer, 8); if(sentRec < 8) { if(sentRec == -1) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuffer, 136); if(sentRec < 136) { if(sentRec == -1) goto readError0; else goto readError1; } if(recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x41) || recBuffer[3] != (uint8)(0x2A)) goto commandByteError; //block data starts on byte 8 of the buffer caliInfo->hiResUnipolarSlope = FPuint8ArrayToFPDouble(recBuffer + 8, 0); caliInfo->hiResUnipolarOffset = FPuint8ArrayToFPDouble(recBuffer + 8, 8); /* reading block 4 from memory */ sendBuffer[7] = (uint8)(0x04); //Blocknum = 4 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuffer, 8); if(sentRec < 8) { if(sentRec == -1) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuffer, 136); if(sentRec < 136) { if(sentRec == -1) goto readError0; else goto readError1; } if(recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x41) || recBuffer[3] != (uint8)(0x2A)) goto commandByteError; //block data starts on byte 8 of the buffer caliInfo->hiResBipolarSlope = FPuint8ArrayToFPDouble(recBuffer + 8, 0); caliInfo->hiResBipolarOffset = FPuint8ArrayToFPDouble(recBuffer + 8, 8); caliInfo->prodID = 9; return 0; writeError0: printf("Error : getCalibrationInfo write failed\n"); return -1; writeError1: printf("Error : getCalibrationInfo did not write all of the buffer\n"); return -1; readError0: printf("Error : getCalibrationInfo read failed\n"); return -1; readError1: printf("Error : getCalibrationInfo did not read all of the buffer\n"); return -1; commandByteError: printf("Error : received buffer at byte 1, 2, or 3 are not 0xA3, 0x01, 0x2A \n"); return -1; }
HANDLE openUSBConnection(int localID) { BYTE buffer[38]; uint16 checksumTotal = 0; HANDLE hDevice = 0; uint32 numDevices = 0; uint32 dev; int i; numDevices = LJUSB_GetDevCount(UE9_PRODUCT_ID); if(numDevices == 0) { printf("Open error: No UE9 devices could be found\n"); return NULL; } for(dev = 1; dev <= numDevices; dev++) { hDevice = LJUSB_OpenDevice(dev, 0, UE9_PRODUCT_ID); if(hDevice != NULL) { if(localID < 0) { return hDevice; } else { checksumTotal = 0; //setting up a CommConfig command buffer[1] = (BYTE)(0x78); buffer[2] = (BYTE)(0x10); buffer[3] = (BYTE)(0x01); for(i = 6; i < 38; i++) buffer[i] = (BYTE)(0x00); extendedChecksum(buffer,38); if(LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, buffer, 38) != 38) goto locid_error; for(i = 0; i < 38; i++) buffer[i] = 0; if(LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, buffer, 38) != 38) goto locid_error; checksumTotal = extendedChecksum16(buffer, 38); if( (BYTE)((checksumTotal / 256) & 0xff) != buffer[5]) goto locid_error; if( (BYTE)(checksumTotal & 0xff) != buffer[4]) goto locid_error; if( extendedChecksum8(buffer) != buffer[0]) goto locid_error; if( buffer[1] != (BYTE)(0x78) || buffer[2] != (BYTE)(0x10) || buffer[3] != (BYTE)(0x01) ) goto locid_error; if( (int)buffer[8] == localID) return hDevice; else LJUSB_CloseDevice(hDevice); } //else localID >= 0 end } //if hDevice != NULL end } //for end printf("Open error: could not find a UE9 with a local ID of %d\n", localID); return NULL; locid_error: printf("Open error: problem when checking local ID\n"); return NULL; }
//Sends 1000 Feedback low-level commands to read digital IO and analog inputs. //On the first send, the following are set: DAC0 to 2.5 volts, DAC1 to 3.5 //volts, and digital IOs to inputs. int allIO(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint8 sendBuff[34], recBuff[64], settlingTime; uint8 numChannels; //Number of AIN channels, 0-16. uint16 checksumTotal, bytesVoltage, ainMask; int sendChars, recChars, i, j; int initialize; //boolean to init. DAC and digital IO settings int numIterations, valueDIPort; long time, ainResolution; double valueAIN[16]; numIterations = 1000; initialize = 1; time = 0; numChannels = 8; ainResolution = 12; for( i = 0; i < 16; i++ ) valueAIN[i] = 9999.0; settlingTime = 0; ainMask = pow(2.0, numChannels) - 1; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x0E); //Number of data words sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 255; //FIOMask : setting the mask of all FIOs sendBuff[7] = 0; //FIODir : setting all FIO directions to input sendBuff[8] = 0; //FIOState : all FIO directions are input, so // state writes do not apply sendBuff[9] = 255; //EIOMask : setting the mask of all EIOs sendBuff[10] = 0; //EIODir : setting all EIO directions to input sendBuff[11] = 0; //EIOState : all EIO directions are input, so // state writes do not apply sendBuff[12] = 15; //CIOMask : setting the mask of all CIOs sendBuff[13] = 0; //CIODirState : setting all CIO directions to input, // state writes do not apply sendBuff[14] = 7; //MIOMask : setting the mask of all MIOs sendBuff[15] = 0; //MIODirState : setting all MIO directions to input, // state writes do not apply //Getting binary DAC0 value of 2.5 volts if( getDacBinVoltCalibrated(caliInfo, 0, 2.500, &bytesVoltage) < 0 ) return -1; //Setting the voltage of DAC0 to 2.5 sendBuff[16] = (uint8)(bytesVoltage & 255); //low bits of DAC0 sendBuff[17] = (uint8)(bytesVoltage/256) + 192; //high bits of DAC0 //(bit 7 : Enable, // bit 6: Update) //Getting binary DAC1 value of 3.5 volts if( getDacBinVoltCalibrated(caliInfo, 1, 3.500, &bytesVoltage) < 0 ) return -1; //Setting the voltage of DAC1 to 3.5 volts sendBuff[18] = (uint8)(bytesVoltage & 255); //low bits of DAC1 sendBuff[19] = (uint8)(bytesVoltage/256) + 192; //high bits of DAC1 //(bit 7 : Enable, // bit 6: Update) //AINMask - reading the number of AINs specified by numChannels sendBuff[20] = ainMask & 255; //AINMask (low byte) sendBuff[21] = ainMask/256; //AINMask (high byte) sendBuff[22] = 14; //AIN14ChannelNumber : setting to channel 14 sendBuff[23] = 15; //AIN15ChannelNumber : setting to channel 15 sendBuff[24] = ainResolution; //Resolution : Resolution specified by // ainResolution sendBuff[25] = settlingTime; //SettlingTime //Setting all BipGains (Gain = 1, Bipolar = 1) for( i = 26; i < 34; i++ ) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff, 34); time = getTickCount(); for( i = 0; i < numIterations; i++ ) { //Sending command to UE9 sendChars = LJUSB_Write(hDevice, sendBuff, 34); if( sendChars < 34 ) { if( sendChars == 0 ) printf("Feedback error (Iteration %d) : write failed\n", i); else printf("Feedback error (Iteration %d) : did not write all of the buffer\n", i); return -1; } //Reading response from UE9 recChars = LJUSB_Read(hDevice, recBuff, 64); if( recChars < 64 ) { if( recChars == 0 ) printf("Feedback error (Iteration %d) : read failed\n", i); else printf("Feedback error (Iteration %d) : did not read all of the buffer\n", i); return -1; } checksumTotal = extendedChecksum16(recBuff, 64); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Feedback error (Iteration %d) : read buffer has bad checksum16(MSB)\n", i); return -1; } if( (uint8)(checksumTotal & 255) != recBuff[4] ) { printf("Feedback error (Iteration %d) : read buffer has bad checksum16(LSB)\n", i); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Feedback error (Iteration %d) : read buffer has bad checksum8\n", i); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x1D) || recBuff[3] != (uint8)(0x00) ) { printf("Feedback error (Iteration %d) : read buffer has wrong command bytes\n", i); return -1; } for( j = 0; j < numChannels && j < 16; j++ ) getAinVoltCalibrated(caliInfo, 0x00, (uint8)ainResolution, recBuff[12 + j*2] + recBuff[13 + j*2]*256, &valueAIN[j]); valueDIPort = recBuff[7] + recBuff[9]*256 + (recBuff[10] & 15)*65536 + (recBuff[11] & 7)*1048576; if( initialize == 1 ) { //Unsetting digital IO bit masks since we only want to read states now sendBuff[6] = 0; //FIOMask sendBuff[9] = 0; //EIOMask sendBuff[12] = 0; //CIOMask sendBuff[14] = 0; //MIOMask //Turning off Update bit of DACs sendBuff[17] = sendBuff[17] - 64; //high bits of DAC0 sendBuff[19] = sendBuff[19] - 64; //high bits of DAC1 extendedChecksum(sendBuff, 34); initialize = 0; } } time = getTickCount() - time; printf("Milleseconds per iteration = %.3f\n", (double)time / (double)numIterations); printf("\nDigital Input (FIO0-7, EIO0-7, CIO0-3, MIO0-2) = %d\n",valueDIPort); printf("\nAIN readings from last iteration:\n"); for( j = 0; j < numChannels; j++ ) printf("%.3f\n", valueAIN[j]); return 0; }
//Sends a CommConfig low-level command to read the configuration settings //associated with the Comm chip. int commConfig_example(int socketFD) { uint8 sendBuff[38]; uint8 recBuff[38]; int sendChars, recChars; int i, j; uint16 checksumTotal; char str[3]; sendBuff[1] = (uint8)(0x78); //command bytes sendBuff[2] = (uint8)(0x10); //number of data words sendBuff[3] = (uint8)(0x01); //extended command number //WriteMask, LocalID, PowerLevel, etc. are all passed a value of //zero since we only want to read Comm configuration settings, //not change them for(i = 6; i < 38; i++) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff, 38); //Sending command to UE9 sendChars = send(socketFD, sendBuff, 38, 0); if(sendChars < 38) { if(sendChars == -1) printf("Error : send failed\n"); else printf("Error : did not send all of the buffer\n"); return -1; } //Receiving response from UE9 recChars = recv(socketFD, recBuff, 38, 0); if(recChars < 38) { if(recChars == -1) printf("Error : receive failed\n"); else printf("Error : did not receive all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 38); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("Error : received buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("Error : received buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("Error : received buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0x78) || recBuff[2] != (uint8)(0x10) || recBuff[3] != (uint8)(0x01) ) { printf("Error : received buffer has wrong command bytes \n"); return -1; } printf("LocalID (byte 8): %d\n", recBuff[8]); printf("PowerLevel (byte 9): %d\n", recBuff[9]); printf("ipAddress (bytes 10-13): %d.%d.%d.%d\n", recBuff[13], recBuff[12], recBuff[11], recBuff[10]); printf("Gateway (bytes 14 - 17): %d.%d.%d.%d\n", recBuff[17], recBuff[16], recBuff[15], recBuff[14]); printf("Subnet (bytes 18 - 21): %d.%d.%d.%d\n", recBuff[21], recBuff[20], recBuff[19], recBuff[18]); printf("PortA (bytes 22 - 23): %d\n", recBuff[22] + (recBuff[23] * 256 )); printf("PortA (bytes 24 - 25): %d\n", recBuff[24] + (recBuff[25] * 256 )); printf("DHCPEnabled (byte 26): %d\n", recBuff[26]); printf("ProductID (byte 27): %d\n", recBuff[27]); printf("MACAddress (bytes 28 - 33): "); for(i = 5; i >= 0 ; i--) { sprintf(str, "%x", (int)(recBuff[i+28])); for(j = 0; j < 3; j++) { if(str[j] == '\0') break; } if(j < 2) { str[1] = str[0]; str[2] = '\0'; str[0] = '0'; } printf("%s", str); if(i > 2) printf("."); if(i == 2) printf(" "); } printf("\nHWVersion (bytes 34-35): %.3f\n", (unsigned int)recBuff[35] + (double)recBuff[34]/100.0); printf("CommFWVersion (bytes 36-37): %.3f\n\n", (unsigned int)recBuff[37] + (double)recBuff[36]/100.0); return 0; }
HANDLE openUSBConnection(int localID) { BYTE sendBuffer[26], recBuffer[38]; uint16 checksumTotal = 0; HANDLE hDevice = 0; uint32 numDevices = 0; uint32 dev; int i; numDevices = LJUSB_GetDevCount(U6_PRODUCT_ID); if(numDevices == 0) { printf("Open error: No U6 devices could be found\n"); return NULL; } for(dev = 1; dev <= numDevices; dev++) { hDevice = LJUSB_OpenDevice(dev, 0, U6_PRODUCT_ID); if(hDevice != NULL) { if(localID < 0) { return hDevice; } else { checksumTotal = 0; //setting up a U6Config sendBuffer[1] = (uint8)(0xF8); sendBuffer[2] = (uint8)(0x0A); sendBuffer[3] = (uint8)(0x08); for(i = 6; i < 26; i++) sendBuffer[i] = (uint8)(0x00); extendedChecksum(sendBuffer, 26); if(LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuffer, 26) != 26) goto locid_error; if(LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuffer, 38) != 38) goto locid_error; checksumTotal = extendedChecksum16(recBuffer, 38); if( (uint8)((checksumTotal / 256) & 0xff) != recBuffer[5]) goto locid_error; if( (uint8)(checksumTotal & 0xff) != recBuffer[4]) goto locid_error; if( extendedChecksum8(recBuffer) != recBuffer[0]) goto locid_error; if( recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x10) || recBuffer[3] != (uint8)(0x08) ) goto locid_error; if( recBuffer[6] != 0) goto locid_error; if( (int)recBuffer[21] == localID) return hDevice; else LJUSB_CloseDevice(hDevice); } //else localID >= 0 end } //if hDevice != NULL end } //for end printf("Open error: could not find a U6 with a local ID of %d\n", localID); return NULL; locid_error: printf("Open error: problem when checking local ID\n"); return NULL; }
long I2C(HANDLE hDevice, uint8 I2COptions, uint8 SpeedAdjust, uint8 SDAPinNum, uint8 SCLPinNum, uint8 Address, uint8 NumI2CBytesToSend, uint8 NumI2CBytesToReceive, uint8 *I2CBytesCommand, uint8 *Errorcode, uint8 *AckArray, uint8 *I2CBytesResponse) { uint8 *sendBuff, *recBuff; int sendChars, recChars, sendSize, recSize, i, ret; uint16 checksumTotal = 0; uint32 ackArrayTotal, expectedAckArray; *Errorcode = 0; ret = 0; sendSize = 6 + 8 + ((NumI2CBytesToSend%2 != 0)?(NumI2CBytesToSend + 1):(NumI2CBytesToSend)); recSize = 6 + 6 + ((NumI2CBytesToReceive%2 != 0)?(NumI2CBytesToReceive + 1):(NumI2CBytesToReceive)); sendBuff = malloc(sizeof(uint8)*sendSize); recBuff = malloc(sizeof(uint8)*recSize); sendBuff[sendSize - 1] = 0; //I2C command sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (sendSize - 6)/2; //number of data words = 4 + NumI2CBytesToSend sendBuff[3] = (uint8)(0x3B); //extended command number sendBuff[6] = I2COptions; //I2COptions sendBuff[7] = SpeedAdjust; //SpeedAdjust sendBuff[8] = SDAPinNum; //SDAPinNum sendBuff[9] = SCLPinNum; //SCLPinNum sendBuff[10] = Address; //Address sendBuff[11] = 0; //Reserved sendBuff[12] = NumI2CBytesToSend; //NumI2CByteToSend sendBuff[13] = NumI2CBytesToReceive; //NumI2CBytesToReceive for(i = 0; i < NumI2CBytesToSend; i++) sendBuff[14 + i] = I2CBytesCommand[i]; //I2CByte extendedChecksum(sendBuff, sendSize); //Sending command to U6 sendChars = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuff, sendSize); if(sendChars < sendSize) { if(sendChars == 0) printf("I2C Error : write failed\n"); else printf("I2C Error : did not write all of the buffer\n"); ret = -1; goto cleanmem; } //Reading response from U6 recChars = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuff, recSize); if(recChars < recSize) { if(recChars == 0) printf("I2C Error : read failed\n"); else { printf("I2C Error : did not read all of the buffer\n"); if(recChars >= 12) *Errorcode = recBuff[6]; } ret = -1; goto cleanmem; } *Errorcode = recBuff[6]; AckArray[0] = recBuff[8]; AckArray[1] = recBuff[9]; AckArray[2] = recBuff[10]; AckArray[3] = recBuff[11]; for(i = 0; i < NumI2CBytesToReceive; i++) I2CBytesResponse[i] = recBuff[12 + i]; if((uint8)(extendedChecksum8(recBuff)) != recBuff[0]) { printf("I2C Error : read buffer has bad checksum (%d)\n", recBuff[0]); ret = -1; } if(recBuff[1] != (uint8)(0xF8)) { printf("I2C Error : read buffer has incorrect command byte (%d)\n", recBuff[1]); ret = -1; } if(recBuff[2] != (uint8)((recSize - 6)/2)) { printf("I2C Error : read buffer has incorrect number of data words (%d)\n", recBuff[2]); ret = -1; } if(recBuff[3] != (uint8)(0x3B)) { printf("I2C Error : read buffer has incorrect extended command number (%d)\n", recBuff[3]); ret = -1; } checksumTotal = extendedChecksum16(recBuff, recSize); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] || (uint8)(checksumTotal & 255) != recBuff[4]) { printf("I2C error : read buffer has bad checksum16 (%d)\n", checksumTotal); ret = -1; } //ackArray should ack the Address byte in the first ack bit ackArrayTotal = AckArray[0] + AckArray[1]*256 + AckArray[2]*65536 + AckArray[3]*16777216; expectedAckArray = pow(2.0, NumI2CBytesToSend+1)-1; if(ackArrayTotal != expectedAckArray) printf("I2C error : expected an ack of %d, but received %d\n", expectedAckArray, ackArrayTotal); cleanmem: free(sendBuff); free(recBuff); sendBuff = NULL; recBuff = NULL; return ret; }
long getCalibrationInfo(HANDLE hDevice, u3CalibrationInfo *caliInfo) { uint8 sendBuffer[8], recBuffer[40]; uint8 cU3SendBuffer[26], cU3RecBuffer[38]; int sentRec = 0; int i = 0; /* sending ConfigU3 command to get hardware version and see if HV */ cU3SendBuffer[1] = (uint8)(0xF8); //command byte cU3SendBuffer[2] = (uint8)(0x0A); //number of data words cU3SendBuffer[3] = (uint8)(0x08); //extended command number //setting WriteMask0 and all other bytes to 0 since we only want to read the response for (i = 6; i < 26; i++) cU3SendBuffer[i] = 0; extendedChecksum(cU3SendBuffer, 26); sentRec = LJUSB_BulkWrite(hDevice, U3_PIPE_EP1_OUT, cU3SendBuffer, 26); if (sentRec < 26) { if (sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, U3_PIPE_EP1_IN, cU3RecBuffer, 38); if (sentRec < 38) { if (sentRec == 0) goto readError0; else goto readError1; } if (cU3RecBuffer[1] != (uint8)(0xF8) || cU3RecBuffer[2] != (uint8)(0x10) || cU3RecBuffer[3] != (uint8)(0x08)) goto commandByteError; caliInfo->hardwareVersion = cU3RecBuffer[14] + cU3RecBuffer[13]/100.0; caliInfo->highVoltage = (((cU3RecBuffer[37]&18) == 18)?1:0); /* reading block 0 from memory */ sendBuffer[1] = (uint8)(0xF8); //command byte sendBuffer[2] = (uint8)(0x01); //number of data words sendBuffer[3] = (uint8)(0x2D); //extended command number sendBuffer[6] = 0; sendBuffer[7] = 0; //Blocknum = 0 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, U3_PIPE_EP1_OUT, sendBuffer, 8); if (sentRec < 8) { if (sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, U3_PIPE_EP1_IN, recBuffer, 40); if (sentRec < 40) { if (sentRec == 0) goto readError0; else goto readError1; } if (recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x11) || recBuffer[3] != (uint8)(0x2D)) goto commandByteError; //block data starts on byte 8 of the buffer caliInfo->ainSESlope = FPuint8ArrayToFPDouble(recBuffer + 8, 0); caliInfo->ainSEOffset = FPuint8ArrayToFPDouble(recBuffer + 8, 8); caliInfo->ainDiffSlope = FPuint8ArrayToFPDouble(recBuffer + 8, 16); caliInfo->ainDiffOffset = FPuint8ArrayToFPDouble(recBuffer + 8, 24); /* reading block 1 from memory */ sendBuffer[7] = 1; //Blocknum = 1 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, U3_PIPE_EP1_OUT, sendBuffer, 8); if (sentRec < 8) { if (sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, U3_PIPE_EP1_IN, recBuffer, 40);; if (sentRec < 40) { if (sentRec == 0) goto readError0; else goto readError1; } if (recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x11) || recBuffer[3] != (uint8)(0x2D)) goto commandByteError; caliInfo->dacSlope[0] = FPuint8ArrayToFPDouble(recBuffer + 8, 0); caliInfo->dacOffset[0] = FPuint8ArrayToFPDouble(recBuffer + 8, 8); caliInfo->dacSlope[1] = FPuint8ArrayToFPDouble(recBuffer + 8, 16); caliInfo->dacOffset[1] = FPuint8ArrayToFPDouble(recBuffer + 8, 24); /* reading block 2 from memory */ sendBuffer[7] = 2; //Blocknum = 2 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, U3_PIPE_EP1_OUT, sendBuffer, 8); if (sentRec < 8) { if (sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, U3_PIPE_EP1_IN, recBuffer, 40); if (sentRec < 40) { if (sentRec == 0) goto readError0; else goto readError1; } if (recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x11) || recBuffer[3] != (uint8)(0x2D)) goto commandByteError; caliInfo->tempSlope = FPuint8ArrayToFPDouble(recBuffer + 8, 0); caliInfo->vref = FPuint8ArrayToFPDouble(recBuffer + 8, 8); caliInfo->vref15 = FPuint8ArrayToFPDouble(recBuffer + 8, 16); caliInfo->vreg = FPuint8ArrayToFPDouble(recBuffer + 8, 24); /* reading block 3 from memory */ sendBuffer[7] = 3; //Blocknum = 3 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, U3_PIPE_EP1_OUT, sendBuffer, 8); if (sentRec < 8) { if (sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, U3_PIPE_EP1_IN, recBuffer, 40); if (sentRec < 40) { if (sentRec == 0) goto readError0; else goto readError1; } if (recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x11) || recBuffer[3] != (uint8)(0x2D)) goto commandByteError; for (i = 0; i < 4; i++) caliInfo->hvAINSlope[i] = FPuint8ArrayToFPDouble(recBuffer + 8, i*8); /* reading block 4 from memory */ sendBuffer[7] = 4; //Blocknum = 4 extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, U3_PIPE_EP1_OUT, sendBuffer, 8); if (sentRec < 8) { if (sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, U3_PIPE_EP1_IN, recBuffer, 40); if (sentRec < 40) { if (sentRec == 0) goto readError0; else goto readError1; } if (recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x11) || recBuffer[3] != (uint8)(0x2D)) goto commandByteError; for (i = 0; i < 4; i++) caliInfo->hvAINOffset[i] = FPuint8ArrayToFPDouble(recBuffer + 8, i*8); caliInfo->prodID = 3; return 0; writeError0: printf("Error : getCalibrationInfo write failed\n"); return -1; writeError1: printf("Error : getCalibrationInfo did not write all of the buffer\n"); return -1; readError0: printf("Error : getCalibrationInfo read failed\n"); return -1; readError1: printf("Error : getCalibrationInfo did not read all of the buffer\n"); return -1; commandByteError: printf("Error : getCalibrationInfo received wrong command bytes for ReadMem\n"); return -1; }
long ehConfigTimerClock(HANDLE hDevice, uint8 inTimerClockConfig, uint8 inTimerClockDivisor, uint8 *outTimerClockConfig, uint8 *outTimerClockDivisor) { uint8 sendBuff[10], recBuff[10]; uint16 checksumTotal; int sendChars, recChars; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x02); //Number of data words sendBuff[3] = (uint8)(0x0A); //Extended command number sendBuff[6] = 0; //Reserved sendBuff[7] = 0; //Reserved sendBuff[8] = inTimerClockConfig; //TimerClockConfig sendBuff[9] = inTimerClockDivisor; //TimerClockDivisor extendedChecksum(sendBuff, 10); //Sending command to U6 if( (sendChars = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuff, 10)) < 10) { if(sendChars == 0) printf("ehConfigTimerClock error : write failed\n"); else printf("ehConfigTimerClock error : did not write all of the buffer\n"); return -1; } //Reading response from U6 if( (recChars = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuff, 10)) < 10) { if(recChars == 0) printf("ehConfigTimerClock error : read failed\n"); else printf("ehConfigTimerClock error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 10); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5]) { printf("ehConfigTimerClock error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehConfigTimerClock error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehConfigTimerClock error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x02) || recBuff[3] != (uint8)(0x0A) ) { printf("ehConfigTimerClock error : read buffer has wrong command bytes\n"); return -1; } if(outTimerClockConfig != NULL) *outTimerClockConfig = recBuff[8]; if(outTimerClockDivisor != NULL) *outTimerClockDivisor = recBuff[9]; if( recBuff[6] != 0) { printf("ehConfigTimerClock error : read buffer received errorcode %d\n", recBuff[6]); return recBuff[6]; } return 0; }
//Sends a StreamConfig low-level command to configure the stream to read only 4 //analog inputs (AI0 - AI3). int StreamConfig_example(HANDLE hDevice) { int sendBuffSize; sendBuffSize = 12 + 2*NumChannels; uint8 sendBuff[sendBuffSize], recBuff[8]; uint16 checksumTotal, scanInterval; int sendChars, recChars, i; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = NumChannels + 3; //Number of data words : NumChannels + 3 sendBuff[3] = (uint8)(0x11); //Extended command number sendBuff[6] = (uint8)NumChannels; //NumChannels sendBuff[7] = ainResolution; //Resolution sendBuff[8] = 0; //SettlingTime = 0 sendBuff[9] = 0; //ScanConfig: scan pulse and external scan //trigger disabled stream clock //frequency = 4 MHz scanInterval = 4000; sendBuff[10] = (uint8)(scanInterval&0x00FF); //Scan interval (low byte) sendBuff[11] = (uint8)(scanInterval/256); //Scan interval (high byte) for( i = 0; i < NumChannels; i++ ) { sendBuff[12 + i*2] = i; //channel # = i sendBuff[13 + i*2] = 0; //BipGain (Bip = unipolar, Gain = 1) } extendedChecksum(sendBuff, sendBuffSize); //Sending command to UE9 sendChars = LJUSB_Write(hDevice, sendBuff, sendBuffSize); if( sendChars < sendBuffSize ) { if( sendChars == 0 ) printf("Error : write failed (StreamConfig).\n"); else printf("Error : did not write all of the buffer (StreamConfig).\n"); return -1; } //Reading response from UE9 recChars = LJUSB_Read(hDevice, recBuff, 8); if( recChars < 8 ) { if( recChars == 0 ) printf("Error : read failed (StreamConfig).\n"); else printf("Error : did not read all of the buffer (StreamConfig).\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 8); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Error : read buffer has bad checksum16(MSB) (StreamConfig).\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Error : read buffer has bad checksum16(LSB) (StreamConfig).\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Error : read buffer has bad checksum8 (StreamConfig).\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x01) || recBuff[3] != (uint8)(0x11) ) { printf("Error : read buffer has wrong command bytes (StreamConfig).\n"); return -1; } if( recBuff[6] != 0 ) { printf("Errorcode # %d from StreamConfig read.\n", (unsigned int)recBuff[6]); return -1; } return 0; }
//Sends a Feedback low-level command to set DAC0, DAC1, read FIO0-FIO3 and //AI0-AI3. int feedback_example(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint8 sendBuff[34], recBuff[64], ainResolution, gainBip; uint16 checksumTotal, bytesVoltage; uint32 tempDir, tempState; int sendChars, recChars, i; double voltage; ainResolution = 12; //ainResolution = 18; //high-res mode for UE9 Pro only gainBip = 0; //(Gain = 1, Bipolar = 0) sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x0E); //Number of data words sendBuff[3] = (uint8)(0x00); //Extended command number //All these bytes are set to zero since we are not changing the FIO, EIO, CIO //and MIO directions and states for( i = 6; i <= 15; i++ ) sendBuff[i] = (uint8)(0x00); if( getDacBinVoltCalibrated(caliInfo, 0, 2.500, &bytesVoltage) < 0 ) return -1; //setting the voltage of DAC0 sendBuff[16] = (uint8)( bytesVoltage & (0x00FF) ); //low bits of DAC0 sendBuff[17] = (uint8)( bytesVoltage / 256 ) + 192; //high bits of DAC0 //(bit 7 : Enable, // bit 6: Update) if( getDacBinVoltCalibrated(caliInfo, 1, 3.500, &bytesVoltage) < 0 ) return -1; //setting the voltage of DAC1 sendBuff[18] = (uint8)( bytesVoltage & (0x00FF) ); //low bits of DAC1 sendBuff[19] = (uint8)( bytesVoltage / 256 ) + 192; //high bits of DAC1 //(bit 7 : Enable, // bit 6: Update) sendBuff[20] = (uint8)(0x0f); //AINMask - reading AIN0-AIN3, not AIN4-AIN7 sendBuff[21] = (uint8)(0x00); //AINMask - not reading AIN8-AIN15 sendBuff[22] = (uint8)(0x00); //AIN14ChannelNumber - not using sendBuff[23] = (uint8)(0x00); //AIN15ChannelNumber - not using sendBuff[24] = ainResolution; //Resolution = 12 //Setting BipGains for( i = 25; i < 34; i++ ) sendBuff[i] = gainBip; extendedChecksum(sendBuff, 34); //Sending command to UE9 sendChars = LJUSB_Write(hDevice, sendBuff, 34); if( sendChars < 34 ) { if( sendChars == 0 ) printf("Error : write failed\n"); else printf("Error : did not write all of the buffer\n"); return -1; } //Reading response from UE9 recChars = LJUSB_Read(hDevice, recBuff, 64); if( recChars < 64 ) { if( recChars == 0 ) printf("Error : read failed\n"); else printf("Error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 64); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("Error : read buffer has bad checksum16(LSB)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x1D) || recBuff[3] != (uint8)(0x00) ) { printf("Error : read buffer has wrong command bytes \n"); return -1; } printf("Set DAC0 to 2.500 volts and DAC1 to 3.500 volts.\n\n"); printf("Flexible digital I/O directions and states (FIO0 - FIO3):\n"); for( i = 0; i < 4; i++ ) { tempDir = ( (uint32)(recBuff[6] / pow(2, i)) & (0x01) ); tempState = ( (uint32)(recBuff[7] / pow(2, i)) & (0x01) ); printf(" FI%d: %u and %u\n", i, tempDir, tempState); } printf("\nAnalog Inputs (AI0 - AI3):\n"); for( i = 0; i < 4; i++ ) { bytesVoltage = recBuff[12 + 2*i] + recBuff[13 + 2*i]*256; //getting analog voltage if( getAinVoltCalibrated(caliInfo, gainBip, ainResolution, bytesVoltage, &voltage) < 0 ) return -1; printf(" AI%d: %.4f V\n", i, voltage); } printf("\n"); return 0; }
//Sends a ControlConfig low-level command to read the configuration settings //associated with the Control chip. int controlConfig_example(int socketFD, ue9CalibrationInfo *caliInfo) { uint8 sendBuff[18], recBuff[24]; int sendChars, recChars; int i; uint16 checksumTotal; double dac; sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (uint8)(0x06); //number of data words sendBuff[3] = (uint8)(0x08); //extended command number //WriteMask, PowerLevel, FIODir, etc. are all passed a value of //zero since we only want to read Control configuration settings, //not change them for(i = 6; i < 18; i++) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff,18); //Sending command to UE9 sendChars = send(socketFD, sendBuff, 18, 0); if(sendChars < 18) { if(sendChars == -1) printf("Error : send failed\n"); else printf("Error : did not send all of the buffer\n"); return -1; } //Receiving response from UE9 recChars = recv(socketFD, recBuff, 24, 0); if(recChars < 24) { if(recChars == -1) printf("Error : receive failed\n"); else printf("Error : did not receive all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 24); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("Error : received buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("Error : received buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("Error : received buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x09) || recBuff[3] != (uint8)(0x08) ) { printf("Error : received buffer has wrong command bytes \n"); return -1; } if( recBuff[6] != 0) { printf("Errorcode (byte 6): %d\n", (uint32)recBuff[6]); return -1; } printf("PowerLevel default (byte 7): %d\n", (uint32)recBuff[7]); printf("ResetSource (byte 8): %d\n", (uint32)recBuff[8]); printf("ControlFW Version (bytes 9 and 10): %.3f\n", (uint32)recBuff[10] + (double)recBuff[9]/100.0); printf("ControlBL Version (bytes 11 and 12): %.3f\n", (uint32)recBuff[12] + (double)recBuff[11]/100.0); printf("HiRes Flag (byte 13): %d\n", (uint32)(recBuff[13]) & (0x01)); printf("FIO default directions and states (bytes 14 and 15):\n"); for(i = 0; i < 8; i++) printf(" FIO%d: %d and %d\n", i, ((recBuff[14]/(uint32)pow(2, i)) & 0x01), ((recBuff[15]/(uint32)pow(2, i)) & 0x01) ); printf("EIO default directions and states (bytes 16 and 17):\n"); for(i = 0; i < 8; i++) printf(" EIO%d: %d and %d\n", i, ((recBuff[16]/(uint32)pow(2, i)) & 0x01), ((recBuff[17]/(uint32)pow(2, i)) & 0x01) ); printf("CIO default directions and states (byte 18):\n"); for(i = 0; i <= 3; i++) printf(" CIO%d: %d and %d\n", i, ((recBuff[18]/(uint32)pow(2, 4 + i)) & 0x01), ((recBuff[18]/(uint32)pow(2, i)) & 0x01) ); printf("MIO default directions and states (byte 19):\n"); for(i = 0; i <= 2; i++) printf(" MIO%d: %d and %d\n", i, ((recBuff[19]/(uint32)pow(2, 4 + i)) & 0x01), ((recBuff[19]/(uint32)pow(2, i)) & 0x01) ); printf("DAC0 default (bytes 20 and 21):\n Enabled: %d\n", ((recBuff[21]/128) & 0x01) ); //getting DAC0 binary value dac = (double)( (unsigned int)recBuff[20] + (((unsigned int)recBuff[21] & 15)*256) ); //getting DAC0 analog value (Volts = (Bits - Offset)/Slope ) dac = (dac - caliInfo->DACOffset[0])/caliInfo->DACSlope[0]; printf(" Voltage: %.3f V\n", dac); printf("DAC1 default (bytes 22 and 23):\n Enabled: %d\n", ((recBuff[23]/128) & 0x01) ); //getting DAC1 binary value dac = (double)( (uint32)recBuff[22] + ((recBuff[23] & 15)*256) ); //getting DAC1 analog value ( Volts = (Bits - Offset)/Slope ) dac = (dac - caliInfo->DACOffset[1])/caliInfo->DACSlope[1]; printf(" Voltage: %.3f V\n\n", dac); return 0; }
//Calls the Feedback low-level command numIterations times and calculates the //time per iteration. int allIO(HANDLE hDevice, u6CalibrationInfo *caliInfo) { uint8 *sendBuff, *recBuff; uint16 checksumTotal, bits16; uint32 bits32; int sendChars, recChars, i, j, sendSize, recSize; double valueAIN[14]; long time; int ret = 0; for( i = 0; i < 14; i++ ) valueAIN[i] = 9999; //Setting up a Feedback command that will set CIO0-3 as input, and setting //DAC0 voltage sendBuff = (uint8 *)malloc(18*sizeof(uint8)); //Creating an array of size 18 recBuff = (uint8 *)malloc(10*sizeof(uint8)); //Creating an array of size 10 sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = 6; //Number of data words (.5 word for echo, 5.5 //words for IOTypes and data) sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 0; //Echo sendBuff[7] = 29; //IOType is PortDirWrite sendBuff[8] = 0; //FIO Writemask sendBuff[9] = 0; //EIO Writemask sendBuff[10] = 15; //CIO Writemask sendBuff[11] = 0; //FIO Direction sendBuff[12] = 0; //EIO Direction sendBuff[13] = 0; //CIO Direction //Setting DAC0 with 2.5 volt output sendBuff[14] = 38; //IOType is DAC0(16-bit) //Value is 2.5 volts (in binary) getDacBinVoltCalibrated16Bit(caliInfo, 0, 2.5, &bits16); sendBuff[15] = (uint8)(bits16&255); sendBuff[16] = (uint8)(bits16/256); sendBuff[17] = 0; //extra padding byte extendedChecksum(sendBuff, 18); //Sending command to U6 if( (sendChars = LJUSB_Write(hDevice, sendBuff, 18)) < 18 ) { if(sendChars == 0) printf("Feedback (CIO input) error : write failed\n"); else printf("Feedback (CIO input) error : did not write all of the buffer\n"); ret = -1; goto cleanmem; } //Reading response from U6 if( (recChars = LJUSB_Read(hDevice, recBuff, 10)) < 10 ) { if( recChars == 0 ) { printf("Feedback (CIO input) error : read failed\n"); ret = -1; goto cleanmem; } else printf("Feedback (CIO input) error : did not read all of the buffer\n"); } checksumTotal = extendedChecksum16(recBuff, 10); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Feedback (CIO input) error : read buffer has bad checksum16(MSB)\n"); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Feedback (CIO input) error : read buffer has bad checksum16(LBS)\n"); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Feedback (CIO input) error : read buffer has bad checksum8\n"); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[3] != (uint8)(0x00) ) { printf("Feedback (CIO input) error : read buffer has wrong command bytes \n"); ret = -1; goto cleanmem; } if( recBuff[6] != 0 ) { printf("Feedback (CIO input) error : received errorcode %d for frame %d in Feedback response. \n", recBuff[6], recBuff[7]); ret = -1; goto cleanmem; } free(sendBuff); free(recBuff); //Setting up Feedback command that will run numIterations times if( ((sendSize = 7+numChannels*4) % 2) != 0 ) sendSize++; //Need an extra byte sendBuff = (uint8 *)malloc(sendSize*sizeof(uint8)); //Creating an array of size sendSize if( ((recSize = 9+numChannels*3) % 2) != 0 ) recSize++; //Need an extra byte recBuff = (uint8 *)malloc(recSize*sizeof(uint8)); //Creating an array of size recSize sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (sendSize - 6)/2; //Number of data words sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 0; //Echo sendBuff[sendSize - 1] = 0; //Setting last byte to zero in case it is the extra padding byte //Setting AIN read commands for( j = 0; j < numChannels; j++ ) { sendBuff[7 + j*4] = 2; //IOType is AIN24 //Positive Channel (bits 0 - 4), LongSettling (bit 6) and QuickSample (bit 7) sendBuff[8 + j*4] = j; //Positive Channel sendBuff[9 + j*4] = (uint8)(resolution&15) + (uint8)((gainIndex&15)*16); //ResolutionIndex (Bits 0-3), GainIndex (Bits 4-7) sendBuff[10 + j*4] = (uint8)(settlingFactor&7); //SettlingFactor (Bits 0-2) if( j%2 == 0 ) sendBuff[10 + j*4] += (uint8)((differential&1)*128); //Differential (Bits 7) } extendedChecksum(sendBuff, sendSize); time = getTickCount(); for( i = 0; i < numIterations; i++ ) { //Sending command to U6 if( (sendChars = LJUSB_Write(hDevice, sendBuff, sendSize)) < sendSize ) { if(sendChars == 0) printf("Feedback error (Iteration %d): write failed\n", i); else printf("Feedback error (Iteration %d): did not write all of the buffer\n", i); ret = -1; goto cleanmem; } //Reading response from U6 if( (recChars = LJUSB_Read(hDevice, recBuff, recSize)) < recSize ) { if( recChars == 0 ) { printf("Feedback error (Iteration %d): read failed\n", i); ret = -1; goto cleanmem; } } checksumTotal = extendedChecksum16(recBuff, recChars); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Feedback error (Iteration %d): read buffer has bad checksum16(MSB)\n", i); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Feedback error (Iteration %d): read buffer has bad checksum16(LBS)\n", i); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Feedback error (Iteration %d): read buffer has bad checksum8\n", i); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[3] != (uint8)(0x00) ) { printf("Feedback error (Iteration %d): read buffer has wrong command bytes \n", i); ret = -1; goto cleanmem; } if( recBuff[6] != 0 ) { printf("Feedback error (Iteration %d): received errorcode %d for frame %d in Feedback response. \n", i, recBuff[6], recBuff[7]); ret = -1; goto cleanmem; } if( recChars != recSize ) { printf("Feedback error (Iteration %d): received packet if %d size when expecting %d\n", i, recChars, recSize); ret = -1; goto cleanmem; } //Getting AIN voltages for(j = 0; j < numChannels; j++) { bits32 = recBuff[9+j*3] + recBuff[10+j*3]*256 + recBuff[11+j*3]*65536; getAinVoltCalibrated(caliInfo, resolution, gainIndex, 1, bits32, &valueAIN[j]); } } time = getTickCount() - time; printf("Milliseconds per iteration = %.3f\n", (double)time / (double)numIterations); printf("\nAIN readings from last iteration:\n"); for( j = 0; j < numChannels; j++ ) printf("%.3f\n", valueAIN[j]); cleanmem: free(sendBuff); free(recBuff); sendBuff = NULL; recBuff = NULL; return ret; }
long getCalibrationInfo(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint8 sendBuffer[8]; uint8 recBuffer[136]; int sentRec = 0, i = 0, j = 0, ccTotal = 0, count = 0; /* Setting up command */ sendBuffer[1] = (uint8)(0xF8); //command byte sendBuffer[2] = (uint8)(0x01); //number of data words sendBuffer[3] = (uint8)(0x2A); //extended command number sendBuffer[6] = (uint8)(0x00); for (i = 0; i < 5; i ++) { /* reading block 1 from memory */ sendBuffer[7] = (uint8)i; //Blocknum = i extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuffer, 8); if (sentRec < 8) { if (sentRec == 0) printf("Error : getCalibrationInfo write failed\n"); else printf("Error : getCalibrationInfo did not write all of the buffer\n"); return -1; } sentRec = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuffer, 136); if (sentRec < 136) { if (sentRec == 0) printf("Error : getCalibrationInfo read failed\n"); else printf("Error : getCalibrationInfo did not read all of the buffer\n"); } if (recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x41) || recBuffer[3] != (uint8)(0x2A)) { printf("Error : received buffer at byte 1, 2, or 3 are not 0xA3, 0x01, 0x2A \n"); return -1; } //Reading out calbration constants if (i == 0) ccTotal = 8; if (i == 1) ccTotal = 2; if (i == 2) ccTotal = 13; if (i == 3) ccTotal = 2; if (i == 4) ccTotal = 2; for (j = 0; j < ccTotal; j++) { if (i != 2 || (i == 2 && j != 5 && j != 7) ) { //block data starts on byte 8 of the buffer caliInfo->ccConstants[count] = FPuint8ArrayToFPDouble(recBuffer + 8, j*8); count++; } } } caliInfo->prodID = 9; return 0; }
//Sends a StreamConfig low-level command to configure the stream. int StreamConfig_example(HANDLE hDevice) { int sendBuffSize; sendBuffSize = 14+NumChannels*2; uint8 sendBuff[sendBuffSize], recBuff[8]; int sendChars, recChars; uint16 checksumTotal; uint16 scanInterval; int i; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = 4 + NumChannels; //Number of data words = NumChannels + 4 sendBuff[3] = (uint8)(0x11); //Extended command number sendBuff[6] = NumChannels; //NumChannels sendBuff[7] = 1; //ResolutionIndex sendBuff[8] = SamplesPerPacket; //SamplesPerPacket sendBuff[9] = 0; //Reserved sendBuff[10] = 0; //SettlingFactor: 0 sendBuff[11] = 0; //ScanConfig: // Bit 3: Internal stream clock frequency = b0: 4 MHz // Bit 1: Divide Clock by 256 = b0 scanInterval = 4000; sendBuff[12] = (uint8)(scanInterval&(0x00FF)); //scan interval (low byte) sendBuff[13] = (uint8)(scanInterval/256); //scan interval (high byte) for( i = 0; i < NumChannels; i++ ) { sendBuff[14 + i*2] = i; //ChannelNumber (Positive) = i sendBuff[15 + i*2] = 0; //ChannelOptions: // Bit 7: Differential = 0 // Bit 5-4: GainIndex = 0 (+-10V) } extendedChecksum(sendBuff, sendBuffSize); //Sending command to U6 sendChars = LJUSB_Write(hDevice, sendBuff, sendBuffSize); if( sendChars < sendBuffSize ) { if( sendChars == 0 ) printf("Error : write failed (StreamConfig).\n"); else printf("Error : did not write all of the buffer (StreamConfig).\n"); return -1; } for( i = 0; i < 8; i++ ) recBuff[i] = 0; //Reading response from U6 recChars = LJUSB_Read(hDevice, recBuff, 8); if( recChars < 8 ) { if( recChars == 0 ) printf("Error : read failed (StreamConfig).\n"); else printf("Error : did not read all of the buffer, %d (StreamConfig).\n", recChars); for( i = 0; i < 8; i++) printf("%d ", recBuff[i]); return -1; } checksumTotal = extendedChecksum16(recBuff, 8); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("Error : read buffer has bad checksum16(MSB) (StreamConfig).\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Error : read buffer has bad checksum16(LSB) (StreamConfig).\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Error : read buffer has bad checksum8 (StreamConfig).\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x01) || recBuff[3] != (uint8)(0x11) || recBuff[7] != (uint8)(0x00) ) { printf("Error : read buffer has wrong command bytes (StreamConfig).\n"); return -1; } if( recBuff[6] != 0 ) { printf("Errorcode # %d from StreamConfig read.\n", (unsigned int)recBuff[6]); return -1; } return 0; }
HANDLE openUSBConnection(int localID) { BYTE buffer[38]; uint16 checksumTotal = 0; uint32 numDevices = 0; uint32 dev; int i, serial; HANDLE hDevice = 0; numDevices = LJUSB_GetDevCount(UE9_PRODUCT_ID); if( numDevices == 0 ) { printf("Open error: No UE9 devices could be found\n"); return NULL; } for( dev = 1; dev <= numDevices; dev++ ) { hDevice = LJUSB_OpenDevice(dev, 0, UE9_PRODUCT_ID); if( hDevice != NULL ) { if( localID < 0 ) { return hDevice; } else { checksumTotal = 0; //Setting up a CommConfig command buffer[1] = (BYTE)(0x78); buffer[2] = (BYTE)(0x10); buffer[3] = (BYTE)(0x01); for( i = 6; i < 38; i++ ) buffer[i] = (BYTE)(0x00); extendedChecksum(buffer,38); if( LJUSB_Write(hDevice, buffer, 38) != 38 ) goto locid_error; for( i = 0; i < 38; i++ ) buffer[i] = 0; if( LJUSB_Read(hDevice, buffer, 38) != 38 ) goto locid_error; checksumTotal = extendedChecksum16(buffer, 38); if( (BYTE)((checksumTotal / 256) & 0xFF) != buffer[5] ) goto locid_error; if( (BYTE)(checksumTotal & 0xFF) != buffer[4] ) goto locid_error; if( extendedChecksum8(buffer) != buffer[0] ) goto locid_error; if( buffer[1] != (BYTE)(0x78) || buffer[2] != (BYTE)(0x10) || buffer[3] != (BYTE)(0x01) ) goto locid_error; //Check local ID if( (int)buffer[8] == localID ) return hDevice; //Check serial number serial = buffer[28] + buffer[29]*256 + buffer[30]*65536 + 0x10000000; if( serial == localID ) return hDevice; //No matches. Not our device. LJUSB_CloseDevice(hDevice); } //else localID >= 0 end } //if hDevice != NULL end } //for end printf("Open error: could not find a UE9 with a local ID or serial number of %d\n", localID); return NULL; locid_error: printf("Open error: problem when checking local ID and serial number\n"); return NULL; }
//Sends a ConfigIO low-level command to turn off timers/counters int ConfigIO_example(HANDLE hDevice) { uint8 sendBuff[16], recBuff[16]; uint16 checksumTotal; int sendChars, recChars, i; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x03); //Number of data words sendBuff[3] = (uint8)(0x0B); //Extended command number sendBuff[6] = 1; //Writemask : Setting writemask for TimerCounterConfig (bit 0) sendBuff[7] = 0; //NumberTimersEnabled : Setting to zero to disable all timers. sendBuff[8] = 0; //CounterEnable: Setting bit 0 and bit 1 to zero to disable both counters sendBuff[9] = 0; //TimerCounterPinOffset for( i = 10; i < 16; i++ ) sendBuff[i] = 0; //Reserved extendedChecksum(sendBuff, 16); //Sending command to U6 if( (sendChars = LJUSB_Write(hDevice, sendBuff, 16)) < 16 ) { if( sendChars == 0 ) printf("ConfigIO error : write failed\n"); else printf("ConfigIO error : did not write all of the buffer\n"); return -1; } //Reading response from U6 if( (recChars = LJUSB_Read(hDevice, recBuff, 16)) < 16 ) { if( recChars == 0 ) printf("ConfigIO error : read failed\n"); else printf("ConfigIO error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 15); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5] ) { printf("ConfigIO error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("ConfigIO error : read buffer has bad checksum16(LSB)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("ConfigIO error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x05) || recBuff[3] != (uint8)(0x0B) ) { printf("ConfigIO error : read buffer has wrong command bytes\n"); return -1; } if( recBuff[6] != 0 ) { printf("ConfigIO error : read buffer received errorcode %d\n", recBuff[6]); return -1; } if( recBuff[8] != 0 ) { printf("ConfigIO error : NumberTimersEnabled was not set to 0\n"); return -1; } if( recBuff[9] != 0 ) { printf("ConfigIO error : CounterEnable was not set to 0\n"); return -1; } return 0; }
long getCalibrationInfo(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { BYTE sendBuffer[8], recBuffer[136]; int sentRec = 0, i = 0, j = 0, ccTotal = 0, count = 0; /* Setting up command */ sendBuffer[1] = (BYTE)(0xF8); //command byte sendBuffer[2] = (BYTE)(0x01); //number of data words sendBuffer[3] = (BYTE)(0x2A); //extended command number sendBuffer[6] = (BYTE)(0x00); for( i = 0; i < 5; i++ ) { /* Reading block 1 from memory */ sendBuffer[7] = (BYTE)i; //Blocknum = i extendedChecksum(sendBuffer, 8); sentRec = LJUSB_Write(hDevice, sendBuffer, 8); if( sentRec < 8 ) { if( sentRec == 0 ) printf("getCalibrationInfo error : write failed\n"); else printf("getCalibrationInfo error : did not write all of the buffer\n"); return -1; } sentRec = LJUSB_Read(hDevice, recBuffer, 136); if( sentRec < 136 ) { if( sentRec == 0 ) printf("getCalibrationInfo Error : read failed\n"); else printf("getCalibrationInfo Error : did not read all of the buffer\n"); } if( recBuffer[1] != (BYTE)(0xF8) || recBuffer[2] != (BYTE)(0x41) || recBuffer[3] != (BYTE)(0x2A) ) { printf("getCalibrationInfo error: incorrect command bytes for ReadMem response"); return -1; } //Reading out calbration constants if( i == 0 ) ccTotal = 8; if( i == 1 ) ccTotal = 2; if( i == 2 ) ccTotal = 13; if( i == 3 ) ccTotal = 2; if( i == 4 ) ccTotal = 2; for( j = 0; j < ccTotal; j++ ) { if( i != 2 || (i == 2 && j != 5 && j != 7) ) { //Block data starts on byte 8 of the buffer caliInfo->ccConstants[count] = FPuint8ArrayToFPDouble(recBuffer + 8, j*8); count++; } } } caliInfo->prodID = 9; return 0; }
long ehFeedback(HANDLE hDevice, uint8 *inIOTypesDataBuff, long inIOTypesDataSize, uint8 *outErrorcode, uint8 *outErrorFrame, uint8 *outDataBuff, long outDataSize) { uint8 *sendBuff, *recBuff; uint16 checksumTotal; int sendChars, recChars, i, sendDWSize, recDWSize, commandBytes, ret; ret = 0; commandBytes = 6; if(((sendDWSize = inIOTypesDataSize + 1)%2) != 0) sendDWSize++; if(((recDWSize = outDataSize + 3)%2) != 0) recDWSize++; sendBuff = malloc(sizeof(uint8)*(commandBytes + sendDWSize)); recBuff = malloc(sizeof(uint8)*(commandBytes + recDWSize)); if(sendBuff == NULL || recBuff == NULL) { ret = -1; goto cleanmem; } sendBuff[sendDWSize + commandBytes - 1] = 0; /* Setting up Feedback command */ sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = sendDWSize/2; //Number of data words (.5 word for echo, 1.5 //words for IOTypes) sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 0; //Echo for(i = 0; i < inIOTypesDataSize; i++) sendBuff[i+commandBytes+1] = inIOTypesDataBuff[i]; extendedChecksum(sendBuff, (sendDWSize+commandBytes)); //Sending command to U6 if( (sendChars = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuff, (sendDWSize+commandBytes))) < sendDWSize+commandBytes) { if(sendChars == 0) printf("ehFeedback error : write failed\n"); else printf("ehFeedback error : did not write all of the buffer\n"); ret = -1; goto cleanmem; } //Reading response from U6 if( (recChars = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuff, (commandBytes+recDWSize))) < commandBytes+recDWSize) { if(recChars == -1) { printf("ehFeedback error : read failed\n"); ret = -1; goto cleanmem; } else if(recChars < 8) { printf("ehFeedback error : response buffer is too small\n"); for(i = 0; i < recChars; i++) printf("%d ", recBuff[i]); ret = -1; goto cleanmem; } else printf("ehFeedback error : did not read all of the expected buffer (received %d, expected %d )\n", recChars, commandBytes+recDWSize); } checksumTotal = extendedChecksum16(recBuff, recChars); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5]) { printf("ehFeedback error : read buffer has bad checksum16(MSB)\n"); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehFeedback error : read buffer has bad checksum16(LBS)\n"); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehFeedback error : read buffer has bad checksum8\n"); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[3] != (uint8)(0x00) ) { printf("ehFeedback error : read buffer has wrong command bytes \n"); ret = -1; goto cleanmem; } *outErrorcode = recBuff[6]; *outErrorFrame = recBuff[7]; for(i = 0; i+commandBytes+3 < recChars && i < outDataSize; i++) outDataBuff[i] = recBuff[i+commandBytes+3]; cleanmem: free(sendBuff); free(recBuff); sendBuff = NULL; recBuff = NULL; return ret; }
long ehConfigIO(HANDLE hDevice, uint8 inWriteMask, uint8 inTimerCounterConfig, uint8 inDAC1Enable, uint8 inFIOAnalog, uint8 inEIOAnalog, uint8 *outTimerCounterConfig, uint8 *outDAC1Enable, uint8 *outFIOAnalog, uint8 *outEIOAnalog) { uint8 sendBuff[12], recBuff[12]; uint16 checksumTotal; int sendChars, recChars; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x03); //Number of data words sendBuff[3] = (uint8)(0x0B); //Extended command number sendBuff[6] = inWriteMask; //Writemask sendBuff[7] = 0; //Reserved sendBuff[8] = inTimerCounterConfig; //TimerCounterConfig sendBuff[9] = inDAC1Enable; //DAC1 enable : not enabling sendBuff[10] = inFIOAnalog; //FIOAnalog sendBuff[11] = inEIOAnalog; //EIOAnalog extendedChecksum(sendBuff, 12); //Sending command to U3 if( (sendChars = LJUSB_BulkWrite(hDevice, U3_PIPE_EP1_OUT, sendBuff, 12)) < 12) { if(sendChars == 0) printf("ehConfigIO error : write failed\n"); else printf("ehConfigIO error : did not write all of the buffer\n"); return -1; } //Reading response from U3 if( (recChars = LJUSB_BulkRead(hDevice, U3_PIPE_EP2_IN, recBuff, 12)) < 12) { if(recChars == 0) printf("ehConfigIO error : read failed\n"); else printf("ehConfigIO error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 12); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5]) { printf("ehConfigIO error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehConfigIO error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehConfigIO error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x03) || recBuff[3] != (uint8)(0x0B) ) { printf("ehConfigIO error : read buffer has wrong command bytes\n"); return -1; } if( recBuff[6] != 0) { printf("ehConfigIO error : read buffer received errorcode %d\n", recBuff[6]); return (int)recBuff[6]; } if(outTimerCounterConfig != NULL) *outTimerCounterConfig = recBuff[8]; if(outDAC1Enable != NULL) *outDAC1Enable = recBuff[9]; if(outFIOAnalog != NULL) *outFIOAnalog = recBuff[10]; if(outEIOAnalog != NULL) *outEIOAnalog = recBuff[11]; return 0; }
long getCalibrationInfo(HANDLE hDevice, u6CalibrationInfo *caliInfo) { uint8 sendBuffer[64], recBuffer[64]; int sentRec = 0; int i = 0; int offset = 0; /* sending ConfigU6 command to get see if hi res */ sendBuffer[1] = (uint8)(0xF8); //command byte sendBuffer[2] = (uint8)(0x0A); //number of data words sendBuffer[3] = (uint8)(0x08); //extended command number //setting WriteMask0 and all other bytes to 0 since we only want to read the response for(i = 6; i < 26; i++) sendBuffer[i] = 0; extendedChecksum(sendBuffer, 26); sentRec = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuffer, 26); if(sentRec < 26) { if(sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuffer, 38); if(sentRec < 38) { if(sentRec == 0) goto readError0; else goto readError1; } if(recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x10) || recBuffer[3] != (uint8)(0x08)) goto commandByteError; caliInfo->hiRes = (((recBuffer[37]&8) == 8)?1:0); for(i = 0; i < 10; i++) { /* reading block i from memory */ sendBuffer[1] = (uint8)(0xF8); //command byte sendBuffer[2] = (uint8)(0x01); //number of data words sendBuffer[3] = (uint8)(0x2D); //extended command number sendBuffer[6] = 0; sendBuffer[7] = (uint8)i; //Blocknum = i extendedChecksum(sendBuffer, 8); sentRec = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuffer, 8); if(sentRec < 8) { if(sentRec == 0) goto writeError0; else goto writeError1; } sentRec = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuffer, 40); if(sentRec < 40) { if(sentRec == 0) goto readError0; else goto readError1; } if(recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x11) || recBuffer[3] != (uint8)(0x2D)) goto commandByteError; offset = i*4; //block data starts on byte 8 of the buffer caliInfo->ccConstants[offset] = FPuint8ArrayToFPDouble(recBuffer + 8, 0); caliInfo->ccConstants[offset + 1] = FPuint8ArrayToFPDouble(recBuffer + 8, 8); caliInfo->ccConstants[offset + 2] = FPuint8ArrayToFPDouble(recBuffer + 8, 16); caliInfo->ccConstants[offset + 3] = FPuint8ArrayToFPDouble(recBuffer + 8, 24); } caliInfo->prodID = 6; return 0; writeError0: printf("Error : getCalibrationInfo write failed\n"); return -1; writeError1: printf("Error : getCalibrationInfo did not write all of the buffer\n"); return -1; readError0: printf("Error : getCalibrationInfo read failed\n"); return -1; readError1: printf("Error : getCalibrationInfo did not read all of the buffer\n"); return -1; commandByteError: printf("Error : getCalibrationInfo received wrong command bytes for ReadMem\n"); return -1; }
long ehDIO_Feedback(HANDLE hDevice, uint8 channel, uint8 direction, uint8 *state) { uint8 sendBuff[34], recBuff[64]; int sendChars, recChars; int i; uint8 tempDir, tempState, tempByte; uint16 checksumTotal; sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (uint8)(0x0E); //number of data words sendBuff[3] = (uint8)(0x00); //extended command number for(i = 6; i < 34; i++) sendBuff[i] = 0; tempDir = ((direction < 1) ? 0 : 1); tempState = ((*state < 1) ? 0 : 1); if(channel <= 7) { tempByte = pow(2, channel); sendBuff[6] = tempByte; if(tempDir) sendBuff[7] = tempByte; if(tempState) sendBuff[8] = tempByte; } else if(channel <= 15) { tempByte = pow(2, (channel - 8)); sendBuff[9] = tempByte; if(tempDir) sendBuff[10] = tempByte; if(tempState) sendBuff[11] = tempByte; } else if(channel <= 19) { tempByte = pow(2, (channel - 16)); sendBuff[12] = tempByte; if(tempDir) sendBuff[13] = tempByte*16; if(tempState) sendBuff[13] += tempByte; } else if(channel <= 22) { tempByte = pow(2, (channel - 20)); sendBuff[14] = tempByte; if(tempDir) sendBuff[15] = tempByte*16; if(tempState) sendBuff[15] += tempByte; } else { printf("DIO Feedback error: Invalid Channel\n"); return -1; } extendedChecksum(sendBuff, 34); //Sending command to UE9 sendChars = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuff, 34); if(sendChars < 34) { if(sendChars == 0) printf("DIO Feedback error : write failed\n"); else printf("DIO Feedback error : did not write all of the buffer\n"); return -1; } //Reading response from UE9 recChars = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuff, 64); if(recChars < 64) { if(recChars == 0) printf("DIO Feedback error : read failed\n"); else printf("DIO Feedback error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 64); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("DIO Feedback error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("DIO Feedback error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("DIO Feedback error : read buffer has bad checksum8\n"); return -1; } if(recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x1D) || recBuff[3] != (uint8)(0x00)) { printf("DIO Feedback error : read buffer has wrong command bytes \n"); return -1; } if(channel <= 7) *state = ((recBuff[7]&tempByte) ? 1 : 0); else if(channel <= 15) *state = ((recBuff[9]&tempByte) ? 1 : 0); else if(channel <= 19) *state = ((recBuff[10]&tempByte) ? 1 : 0); else if(channel <= 22) *state = ((recBuff[11]&tempByte) ? 1 : 0); return 0; }
long ehConfigIO(HANDLE hDevice, uint8 inWriteMask, uint8 inNumberTimersEnabled, uint8 inCounterEnable, uint8 inPinOffset, uint8 *outNumberTimersEnabled, uint8 *outCounterEnable, uint8 *outPinOffset) { uint8 sendBuff[16], recBuff[16]; uint16 checksumTotal; int sendChars, recChars, i; sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (uint8)(0x05); //Number of data words sendBuff[3] = (uint8)(0x0B); //Extended command number sendBuff[6] = inWriteMask; //Writemask sendBuff[7] = inNumberTimersEnabled; sendBuff[8] = inCounterEnable; sendBuff[9] = inPinOffset; for(i = 10; i < 16; i++) sendBuff[i] = 0; extendedChecksum(sendBuff, 16); //Sending command to U6 if( (sendChars = LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuff, 16)) < 16) { if(sendChars == 0) printf("ehConfigIO error : write failed\n"); else printf("ehConfigIO error : did not write all of the buffer\n"); return -1; } //Reading response from U6 if( (recChars = LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuff, 16)) < 16) { if(recChars == 0) printf("ehConfigIO error : read failed\n"); else printf("ehConfigIO error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 16); if( (uint8)((checksumTotal / 256 ) & 0xff) != recBuff[5]) { printf("ehConfigIO error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehConfigIO error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehConfigIO error : read buffer has bad checksum8\n"); return -1; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x05) || recBuff[3] != (uint8)(0x0B) ) { printf("ehConfigIO error : read buffer has wrong command bytes\n"); return -1; } if( recBuff[6] != 0) { printf("ehConfigIO error : read buffer received errorcode %d\n", recBuff[6]); return (int)recBuff[6]; } if(outNumberTimersEnabled != NULL) *outNumberTimersEnabled = recBuff[7]; if(outCounterEnable != NULL) *outCounterEnable = recBuff[8]; if(outPinOffset != NULL) *outPinOffset = recBuff[9]; return 0; }
long ehTimerCounter(HANDLE hDevice, uint8 inTimerClockDivisor, uint8 inEnableMask, uint8 inTimerClockBase, uint8 inUpdateReset, uint8 *inTimerMode, uint16 *inTimerValue, uint8 *inCounterMode, uint32 *outTimer, uint32 *outCounter) { uint8 sendBuff[30], recBuff[40]; int sendChars, recChars, i, j; uint16 checksumTotal; sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (uint8)(0x0C); //number of data words sendBuff[3] = (uint8)(0x18); //extended command number sendBuff[6] = inTimerClockDivisor; //TimerClockDivisor sendBuff[7] = inEnableMask; //EnableMask sendBuff[8] = inTimerClockBase; //TimerClockBase sendBuff[9] = inUpdateReset; //UpdateReset for(i = 0; i < 6; i++) { sendBuff[10 + i*3] = inTimerMode[i]; //TimerMode sendBuff[11 + i*3] = (uint8)(inTimerValue[i]&0x00FF); //TimerValue (low byte) sendBuff[12 + i*3] = (uint8)((inTimerValue[i]&0xFF00)/256); //TimerValue (high byte) } for(i = 0; i < 2; i++) sendBuff[28 + i] = inCounterMode[i]; //CounterMode extendedChecksum(sendBuff, 30); //Sending command to UE9 sendChars = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuff, 30); if(sendChars < 30) { if(sendChars == 0) printf("ehTimerCounter error : write failed\n"); else printf("ehTimerCounter error : did not write all of the buffer\n"); return -1; } //Reading response from UE9 recChars = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuff, 40); if(recChars < 40) { if(recChars == 0) printf("ehTimerCounter error : read failed\n"); else printf("ehTimerCounter error : did not read all of the buffer\n"); return -1; } checksumTotal = extendedChecksum16(recBuff, 40); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("ehTimerCounter error : read buffer has bad checksum16(MSB)\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("ehTimerCounter error : read buffer has bad checksum16(LBS)\n"); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("ehTimerCounter error : read buffer has bad checksum8\n"); return -1; } if(recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x11) || recBuff[3] != (uint8)(0x18)) { printf("ehTimerCounter error : read buffer has wrong command bytes for TimerCounter\n"); return -1; } if(outTimer != NULL) { for(i = 0; i < 6; i++) { outTimer[i] = 0; for(j = 0; j < 4; j++) outTimer[i] += recBuff[8 + j + i*4]*pow(2, 8*j); } } if(outCounter != NULL) { for(i = 0; i < 2; i++) { outCounter[i] = 0; for(j = 0; j < 4; j++) outCounter[i] += recBuff[32 + j + i*4]*pow(2, 8*j); } } return recBuff[6]; }
//Sends a TimerCounter low-level command to enable and set two Timers //(FIO0, FIO1) and two Counters (FIO2, FIO3), then sends a TimerCounter //command a second later to read from the Counters and last sends a //TimerCounter command to disable the Timers and Counters. int timerCounter_example(HANDLE hDevice) { //Note: if using the quadrature input timer mode, the returned 32 bit integer // is signed uint8 sendBuff[30], recBuff[40]; int sendChars, recChars, i; uint32 cnt; //Enable timers and counters sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (uint8)(0x0C); //number of data words sendBuff[3] = (uint8)(0x18); //extended command number sendBuff[6] = (uint8)(0x03); //TimerClockDivisor = 3 sendBuff[7] = (uint8)(0x9A); //Updating: 2 Timers enabled, Counter0 and //Counter1 enabled sendBuff[8] = (uint8)(0x00); //TimerClockConfig = 750 kHz (if using system //clock, call ControlConfig first and set the //PowerLevel to a fixed state) sendBuff[9] = (uint8)(0x00); //UpdateReset - not resetting anything sendBuff[10] = (uint8)(0x00); //Timer0Mode = 16-Bit PWM //Timer0Value = 32768 sendBuff[11] = (uint8)(0x00); //Timer0Value (low byte) sendBuff[12] = (uint8)(0x80); //Timer0Value (high byte) //Timer1Value = 32768 sendBuff[13] = (uint8)(0x01); //Timer1Mode = 8-Bit PWM sendBuff[14] = (uint8)(0x00); //Timer1Value (low byte) sendBuff[15] = (uint8)(0x80); //Timer1Value (high byte) //timer modes and values for timers 2 - 5, which are not enabled for(i = 16; i < 28; i++) sendBuff[i] = (uint8)(0x00); sendBuff[28] = (uint8)(0x00); //Counter0Mode (pass 0) sendBuff[29] = (uint8)(0x00); //Counter1Mode (pass 0) extendedChecksum(sendBuff, 30); //Sending command to UE9 sendChars = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuff, 30); if(sendChars < 30) { if(sendChars == 0) goto writeError0; else goto writeError1; } //Reading response from UE9 recChars = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuff, 40); if(recChars < 40) { if(recChars == 0) goto readError0; else goto readError1; } if(errorCheck(recBuff) == -1) return -1; //wait 1 sec, read counters sleep(1); sendBuff[1] = (uint8)(0xF8); //command bytes sendBuff[2] = (uint8)(0x0C); //number of data words sendBuff[3] = (uint8)(0x18); //extended command number //Not updating our configuration, just want to back the counters for(i = 6; i < 30; i++) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff, 30); //Sending command to UE9 sendChars = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuff, 30); if(sendChars < 30) { if(sendChars == 0) goto writeError0; else goto writeError1; } //Reading response from UE9 recChars = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuff, 40); if(recChars < 40) { if(recChars == 0) goto readError0; else goto readError1; } if(errorCheck(recBuff) == -1) return -1; printf("Current counts from counters after 1 second:\n"); for(i = 0; i < 2; i++) { cnt = (unsigned int)recBuff[32 + 4*i] + (unsigned int)recBuff[33 + 4*i]*256 + (unsigned int)recBuff[34 + 4*i]*65536 + (unsigned int)recBuff[35 + 4*i]*16777216; printf(" Counter%d : %u\n", i, cnt); } //disable timers and counters sendBuff[1] = (uint8)(0xF8); //command bytes sendBuff[2] = (uint8)(0x0C); //number of data words sendBuff[3] = (uint8)(0x18); //extended command number sendBuff[6] = (uint8)(0x00); //TimerClockDivisor = 0 sendBuff[7] = (uint8)(0x80); //Updating: 0 Timers enabled, Counter0 and //Counter1 disabled //setting bytes 8 - 30 to zero since nothing is enabled for(i = 8; i < 30; i++) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff, 30); //Sending command to UE9 sendChars = LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, sendBuff, 30); if(sendChars < 30) { if(sendChars == 0) goto writeError0; else goto writeError1; } //Reading response from UE9 recChars = LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, recBuff, 40); if(recChars < 40) { if(recChars == 0) goto readError0; else goto readError1; } if(errorCheck(recBuff) == -1) return -1; return 0; writeError0: printf("Error : write failed\n"); return -1; writeError1: printf("Error : did not write all of the buffer\n"); return -1; readError0: printf("Error : read failed\n"); return -1; readError1: printf("Error : did not read all of the buffer\n"); return -1; }
//Sends a StreamConfig low-level command to configure the stream to read //NumChannels analog inputs. int StreamConfig_example(int socketFD) { int sendBuffSize; uint8 *sendBuff; uint8 recBuff[8]; int sendChars, recChars, i, ret; uint16 checksumTotal, scanInterval; sendBuffSize = 12 + 2*NumChannels; sendBuff = malloc(sizeof(uint8)*sendBuffSize); ret = 0; sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = NumChannels + 3; //number of data words : NumChannels + 3 sendBuff[3] = (uint8)(0x11); //extended command number sendBuff[6] = (uint8)NumChannels; //NumChannels sendBuff[7] = ainResolution; //resolution sendBuff[8] = 0; //SettlingTime = 0 sendBuff[9] = 0; //ScanConfig: scan pulse and external scan //trigger disabled, stream clock //frequency = 4 MHz scanInterval = 4000; sendBuff[10] = (uint8)(scanInterval & 0x00FF); //scan interval (low byte) sendBuff[11] = (uint8)(scanInterval / 256); //scan interval (high byte) for(i = 0; i < NumChannels; i++) { sendBuff[12 + i*2] = i; //channel # = i sendBuff[13 + i*2] = 0; //BipGain (Bip = unipolar, Gain = 1) } extendedChecksum(sendBuff, sendBuffSize); //Sending command to UE9 sendChars = send(socketFD, sendBuff, sendBuffSize, 0); if(sendChars < 20) { if(sendChars == -1) printf("Error : send failed (StreamConfig)\n"); else printf("Error : did not send all of the buffer (StreamConfig)\n"); ret = -1; goto cleanmem; } //Receiving response from UE9 recChars = recv(socketFD, recBuff, 8, 0); if(recChars < 8) { if(recChars == -1) printf("Error : receive failed (StreamConfig)\n"); else printf("Error : did not receive all of the buffer (StreamConfig)\n"); goto cleanmem; } checksumTotal = extendedChecksum16(recBuff, 8); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("Error : received buffer has bad checksum16(MSB) (StreamConfig)\n"); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4]) { printf("Error : received buffer has bad checksum16(LBS) (StreamConfig)\n"); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("Error : received buffer has bad checksum8 (StreamConfig)\n"); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x01) || recBuff[3] != (uint8)(0x11) || recBuff[7] != (uint8)(0x00)) { printf("Error : received buffer has wrong command bytes (StreamConfig)\n"); ret = -1; goto cleanmem; } if(recBuff[6] != 0) { printf("Errorcode # %d from StreamConfig received.\n", (unsigned int)recBuff[6]); ret = -1; goto cleanmem; } cleanmem: free(sendBuff); sendBuff = NULL; return ret; }