/*---------------------------------------------------------------------- * calcChecksum * Calculate checksum on a received data buffer. * * Note: The last two bytes, which contain the received checksum, * are not included in the calculation. * * parameters: buffer : pointer to the start of the received buffer. * length - the length (in chars) of the buffer. * * returns: the calculated checksum. *--------------------------------------------------------------------*/ int calcChecksum( unsigned char* buffer, int length) { int CHECKSUM_MASK = 0xFFFF; int checkSum, i; if (length<4) return -1; checkSum = buffer[0] & LSB_MASK; for (i=1; i<length-2; i = i+2) { checkSum += convert2short(&buffer[i]); } return(checkSum & CHECKSUM_MASK); }
int i3dmgx3_getFirmwareVersion(int portNum, char *firmware) { unsigned char cmd = (char) CMD_FIRWARE_VERSION; unsigned char Bresponse[7] = {0}; unsigned short wChecksum = 0; unsigned short wCalculatedCheckSum = 0; short firmwareNum=0; short majorNum, minorNum, buildNum; int status; int responseLength = 7; status = sendBuffData(portNum, &cmd, 1); if (DEBUG) printf("FirmWare_send: tx status : %d\n", status); if (status == I3DMGX3_COMM_OK) { if (responseLength>0) { status = receiveData(portNum, &Bresponse[0], responseLength); if (DEBUG) printf("FirmWare i3dmgx3_send: rx status : %d and responseLength %d\n", status, responseLength); if (status==I3DMGX3_COMM_OK) { wChecksum = convert2ushort(&Bresponse[responseLength-2]); wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes if(wChecksum != wCalculatedCheckSum) return status = I3DMGX3_CHECKSUM_ERROR; }else return status = I3DMGX3_COMM_READ_ERROR; } firmwareNum = convert2short(&Bresponse[3]); if (firmwareNum > 0) { /* format for firmware number is #.#.## */ majorNum = firmwareNum / 1000; minorNum = (firmwareNum % 1000) / 100; buildNum = firmwareNum % 100; sprintf(firmware, "%d.%d.%d", majorNum, minorNum, buildNum); } return I3DMGX3_OK; }else return status; }
//=========================================================================== // SetSampleSetting //--------------------------------------------------------------------------- // Description: Allows user to set one of the optional Sample Settings // // Return: Confirmation that selection was set. //=========================================================================== int SetSampleSetting(int portNum, BYTE byteOne, BYTE byteTwo, unsigned char *Bresponse, short FW_Check) { int status = 0; unsigned char DR3_write_buff[21]; int responseLength = 19; unsigned short wChecksum = 0; unsigned short wCalculatedCheckSum = 0; unsigned short Decimation = (1000/100); //m_DRvalue); unsigned short DigFilter = 20; int ytrewq=0; unsigned char cmd = ( unsigned char) CMD_FIRWARE_VERSION; //0xE9 BYTE FWbuff[7] = {0}; short firmwareNum=0; int northComp=0, upComp=0; int NCval =0, Value=0; //char Jjunk[20]; int ncOption = 0; purge_port(portNum); if(FW_Check >= 1){ //include case 8 and 9 //this feature requires firmware version of 1.1.27 or greater status = sendBuffData(portNum, &cmd, 1); if (status == I3DMGX3_COMM_OK) { status = receiveData(portNum, &FWbuff[0], 7); firmwareNum = convert2short(&FWbuff[3]); if (firmwareNum < 1127){ printf("Firmware version must be 1.1.27 or greater to perfom this selection\n"); return -1; } } } if(FW_Check == 8){ //North Compensation while(Value == 0){ printf("Would you like to Modify the North Compensation? (Y)es or (N)o \n"); while(!ReadCharNoReturn(&ncOption)) { Sleep(50); } switch (ncOption){ case 'n': case 'N': northComp = 10; Value = 1; break; case 'y': case 'Y': while (NCval == 0){ northComp = 0; printf("Enter a value between 1 and 1000 to set:"); scanf("%d", &northComp); if ((northComp < 1 )|| (northComp > 1000) ){ printf(" Invalid entry: out of range \n\n"); NCval = 0; //scanf("%s", Jjunk); northComp = 0; } else NCval = 1; } Value =1; break; default:printf("Invalid selection\n"); Value =0; break; } } } if(FW_Check == 9){ //Up Compensation Value = 0; NCval = 0; while(Value == 0){ printf("Would you like to Modify the Up Compensation value? (Y)es or (N)o \n"); while(!ReadCharNoReturn(&ncOption)) { Sleep(50); } switch (ncOption){ case 'n': case 'N': upComp = 10; Value = 1; break; case 'y': case 'Y': while (NCval == 0){ upComp = 0; printf("Enter a value between 1 and 1000 to set:"); scanf("%d", &upComp); if ((upComp < 1 )|| (upComp > 1000) ){ printf(" Invalid entry: out of range \n\n"); NCval = 0; upComp = 0; } else NCval = 1; } Value =1; //return; //remove after validation break; default:printf("Invalid selection\n"); Value =0; break; } } } DR3_write_buff[0] = 0xDB; //Command identifier for Write to Data-Rate DR3_write_buff[1] = 0xA8; //Required identifier for Data-Rate Write was C1 DR3_write_buff[2] = 0xB9; //Required identifier for Data-Rate Write was 29 DR3_write_buff[3] = 0x01; //Identifies an update to data rate, 0 == no update // 1 == change the parameters to new values // 2 == change and write to non-volatile memory // 3 == same as 2 but does not send a reply DR3_write_buff[4] = (Decimation & MSB_MASK) >> 8; //1st byte of Decimation value to write DR3_write_buff[5] = Decimation & LSB_MASK; //2ond byte of Decimation value to write DR3_write_buff[6] = byteOne; //First Byte of Data Conditioning Function Selector //0xC0; DR3_write_buff[7] = byteTwo; //Second Byte of Data Conditioning Function Selector //0x00; DR3_write_buff[8] = 0x0F; //Gyro and Accel digital filter window size default is 15 DR3_write_buff[9] = 0x1E; //Mag digital filter window size default is 17 if(FW_Check == 9){ DR3_write_buff[10] = (upComp & MSB_MASK) >> 8; DR3_write_buff[11] = upComp & LSB_MASK; }else{