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

📄 idblock_recovery.c

📁 CBUF.LIB OVERVIEW: The circular buffers here are standard rotating FIFO buffers. THey are c
💻 C
📖 第 1 页 / 共 2 页
字号:
		printf("Error while installing version 2 System ID Block!\n");
		printf("Recommend using write_IDblock.c to remake ID block\n");
		printf("or contact Z-World for further instructions.\n");
#endif	// V_L > 0
		exit(70);
	}

	// finally, erase old ID Block "B" copy (just because)
	memset(&mySIDBB, 0xFF, sizeof(SysIDBlock));
	_overwrite_block_flag = 1;
	errorResult = WriteFlash(addrSIDBB, &mySIDBB, sizeof(SysIDBlock));
	if (errorResult) {
#if (VERBOSITY_LEVEL >= 0)
		printf("Error while erasing old System ID Block \"B\" copy.\n");
		printf("This error is harmless, because Dynamic C will\n");
		printf("use the valid System ID Block \"A\" copy already installed.\n");
#endif	// V_L > 0
		exit(80);
	}

#if (VERBOSITY_LEVEL >= 0)
	// success!
	printf("\nVersion 4 User Blocks converted to version 2 User Block.\n");
	printf("%s utility successfully completed.\n", PROGRAM_NAME);
#endif	// V_L > 0

}



//////////////////////////////////////////////////////////////////////////////////////
//  Start of support functions
//////////////////////////////////////////////////////////////////////////////////////
int CalcIDBlockCRC(SysIDBlockType *sidb)
{
	char savedMarker[sizeof(sidb->marker)];
	char *tempPtr;
	size_t crcLength, length, offset;
	int savedCRC, tempCRC;

	// temporarily save System ID Block CRC and marker
	savedCRC = sidb->idBlockCRC;
	memcpy(savedMarker, &sidb->marker, sizeof(sidb->marker));

	// prepare System ID Block for CRC scan
	sidb->idBlockCRC = 0;
	memcpy(&sidb->marker, &myValidMarker, sizeof(sidb->marker));

	// ID Block CRC includes the first element
	tempCRC = 0;
	offset = 0;
	tempPtr = (char *) sidb;
	length = (unsigned) &sidb->reserved - (unsigned) sidb;

	// initiate the ID Block CRC
	while (offset < length) {
		crcLength = length - offset;
		crcLength = (crcLength > 255u) ? 255u : crcLength;
		tempCRC = getcrc(tempPtr, (char) crcLength, tempCRC);
		offset += crcLength;
		tempPtr += crcLength;
	}

	// ID Block CRC skips over sidb->reserved
	offset += sizeof(sidb->reserved);
	tempPtr += sizeof(sidb->reserved);
	length = sizeof(SysIDBlock);

	// now complete the ID Block CRC
	while (offset < length) {
		crcLength = length - offset;
		crcLength = (crcLength > 255u) ? 255u : crcLength;
		tempCRC = getcrc(tempPtr, (char) crcLength, tempCRC);
		offset += crcLength;
		tempPtr += crcLength;
	}

	// restore System ID Block to original state
	sidb->idBlockCRC = savedCRC;
	memcpy(&sidb->marker, savedMarker, sizeof(sidb->marker));

	return tempCRC;
}

int RecoverBL21xxCC(void)
{
	unsigned ccLength;
	int errorResult, errorResultA, errorResultB;

	//BL2100  0x0B00   // 22MHz Mini-E, RCM2200 ethernet, analog I/O
	//BL2101  0x0B06   // 22MHz Mini-E, RCM2200 ethernet, 0-10V analog I/O
	//BL2105  0x0B04   // 22MHz Mini-E, RCM2250 ethernet, analog I/O
	//BL2120  0x0B02   // 22MHz Mini-E, RCM2300 no ethernet, analog I/O
	//BL2121  0x0B07   // 22MHz Mini-E, RCM2300 no ethernet, 0-10V analog I/O
	if (SysIDBlock.productID != BL2100) {
		if (SysIDBlock.productID != BL2101) {
			if (SysIDBlock.productID != BL2105) {
				if (SysIDBlock.productID != BL2120) {
					if (SysIDBlock.productID != BL2121) {
#if (VERBOSITY_LEVEL > 2)
						printf("Not a BL2100, BL2101, BL2105, BL2120, or BL2121.\n");
						printf("Not attempting to recover calibration constants.\n");
#endif	// V_L > 2
						return 0;
					}
				}
			}
		}
	}

	if (SysIDBlock.userBlockSize != 0x3F7Cul) {
#if (VERBOSITY_LEVEL >= 0)
		printf("Nonstandard BL21xx User Block size.\n");
		printf("Quitting without making any changes.\n");
		printf("Recommend using write_IDblock.c to remake ID block\n");
		printf("and using the ad_calib.c and dacal.c sample\n");
		printf("programs to re-calibrate your analog circuit,\n");
      printf("located in \\samples\\BL2100\\adc and \\samples\\BL2100\\dac\n\n");
		printf("               \"OR\" \n\n");
		printf("Contact Z-World for further instructions.\n");
#endif	// V_L > 0
		return 1;
	}

	// get (from hard-coded address offset)
	//  & check User Block A calibration constants
	addrUBA_CC = addrUBA - 0x0400ul;
	xmem2root(myUBACCBuffer, addrUBA_CC, sizeof(myUBACCBuffer));

	// get (from hard-coded address offset)
	//  & check User Block B calibration constants
	addrUBB_CC = addrUBB - 0x0400ul;
	xmem2root(myUBBCCBuffer, addrUBB_CC, sizeof(myUBBCCBuffer));

	validCCBuffer = myUBACCBuffer;	// default to UBA CC Buffer
	errorResultA = ReportCCInfo(myUBACCBuffer, "UBA_CC");
	if (errorResultA) {
		validCCBuffer = myUBBCCBuffer;	// switch to UBB CC buffer
	}
	errorResultB = ReportCCInfo(myUBBCCBuffer, "UBB_CC");

	if (errorResultA && errorResultB) {
#if (VERBOSITY_LEVEL >= 0)
		printf("\nNo calibration constants found.\n");
		printf("Quitting without making any changes.\n\n");
		printf("Recommend using the ad_calib.c and dacal.c samples\n");
      printf("located in \\samples\\BL2100\\adc and \\samples\\BL2100\\dac\n");
		printf("to re-calibrate your analog circuit,\n\n");
		printf("               \"OR\" \n\n");
		printf("contact Z-World for further instructions.\n");
#endif	// V_L > 0
		return 2;
	}

	if (!errorResultA && !errorResultB) {
#if (VERBOSITY_LEVEL > 1)
		printf("\nValid calibration constants found.\n");
		printf("No calibration constants recovery required.\n");
#endif	// V_L > 1
		return 0;
	}

	// write calibration constants into other User Block
	ccLength = 0x400u - (unsigned) SysIDBlock.idBlockSize;
	_overwrite_block_flag = 1;
	if (validCCBuffer == myUBACCBuffer) {
		// copy calibration constants into valid User Block B
		errorResult = WriteFlash(addrUBB_CC, validCCBuffer, ccLength);
	} else {
		// copy calibration constants into valid User Block A
		errorResult = WriteFlash(addrUBA_CC, validCCBuffer, ccLength);
	}
	if (errorResult) {
#if (VERBOSITY_LEVEL >= 0)
		printf("Error writing calibration constants into User Block.\n");
		printf("Recommend running Write_IDblock.c and \n");
		printf("using the ad_calib.c and dacal.c samples\n");
      printf("located in \\samples\\BL2100\\adc and \\samples\\BL2100\\dac\n");
		printf("programs to re-calibrate your analog circuit,\n\n");
		printf("               \"OR\" \n\n");
		printf("contact Z-World for further instructions.\n");
#endif	// V_L > 0
		return 3;
	} else {
#if (VERBOSITY_LEVEL > 1)
		printf("Calibration constants successfully written into User Block.\n");
#endif	// V_L > 1
	}
	return 0;
}

int ReportCCInfo(char *ubccBuffer, char *ubccName)
{
	int errorResult, i;

#if (VERBOSITY_LEVEL > 2)
	printf("\n\nCurrent %s User Block Calibration Constants Info\n\n", ubccName);
#endif	// V_L > 0

	// DAC calibration constants are at start of buffer
	memcpy(myDACCalib, ubccBuffer, DACHANNELS * 2 * sizeof(float));

	errorResult = 0;	// default to no error

	for (i = 0; i < DACHANNELS; i++) {
#if (VERBOSITY_LEVEL > 2)
		printf("DAC%d gain:    %f.\n", i, myDACCalib[i][0]);
		printf("DAC%d offset:  %f.\n\n", i, myDACCalib[i][1]);
#endif	// V_L > 0
		if ((myDACCalib[i][0] < MIN_DACGAIN) ||
		    (myDACCalib[i][0] > MAX_DACGAIN) ||
		    (myDACCalib[i][1] < MIN_DACOFST) ||
		    (myDACCalib[i][1] > MAX_DACOFST))
		{
			errorResult++;	// count up the errors!
		}
	}

	return errorResult;
}

int ReportSysIDBlockInfo(SysIDBlockType *sidb, char *sidbName)
{
	int i, result, testCRC;

#if (VERBOSITY_LEVEL >= 0)
	printf("\n\nCurrent %s System ID Block Info\n\n", sidbName);
	printf("tableVersion:   0x%04x\n", sidb->tableVersion);
	printf("productID:      0x%04x\n", sidb->productID);
#if (VERBOSITY_LEVEL > 2)
	printf("vendorID:       0x%04x\n", sidb->vendorID);
	printf("timestamp:      0x");
	for (i = 0; i < sizeof(sidb->timestamp); i++) {
		printf("%02x", (int) sidb->timestamp[i]);
	}
	printf("\n");
	printf("flashID:        0x%08lx\n", sidb->flashID);
	printf("flashType:      0x%04x\n", sidb->flashType);
	printf("flashSize:      0x%04x\n", sidb->flashSize);
	printf("sectorSize:     0x%04x\n", sidb->sectorSize);
	printf("numSectors:     0x%04x\n", sidb->numSectors);
	printf("flashSpeed:     0x%04x\n", sidb->flashSpeed);
	printf("flash2ID:       0x%08lx\n", sidb->flash2ID);
	printf("flash2Type:     0x%04x\n", sidb->flash2Type);
	printf("flash2Size:     0x%04x\n", sidb->flash2Size);
	printf("sector2Size:    0x%04x\n", sidb->sector2Size);
	printf("num2Sectors:    0x%04x\n", sidb->num2Sectors);
	printf("flash2Speed:    0x%04x\n", sidb->flash2Speed);
	printf("ramID:          0x%08lx\n", sidb->ramID);
	printf("ramSize:        0x%04x\n", sidb->ramSize);
	printf("ramSpeed:       0x%04x\n", sidb->ramSpeed);
	printf("cpuID:          0x%04x\n", sidb->cpuID);
#endif	// V_L > 2
#if (VERBOSITY_LEVEL > 1)
	printf("crystalFreq:    0x%08lx\n", sidb->crystalFreq);
	printf("macAddr:        0x");
	for (i = 0; i < sizeof(sidb->macAddr); i++) {
		printf("%02x", (int) sidb->macAddr[i]);
	}
	printf("\n");
#endif	// V_L > 1
#if (VERBOSITY_LEVEL > 2)
	printf("serialNumber:   0x");
	for (i = 0; i < sizeof(sidb->serialNumber); i++) {
		printf("%02x", (int) sidb->serialNumber[i]);
	}
	printf("\n");
	printf("productName:    0x");
	for (i = 0; i < sizeof(sidb->productName); i++) {
		printf("%02x", (int) sidb->productName[i]);
	}
	printf("\n");
	printf("reserved:       0x");
	for (i = 0; i < sizeof(sidb->reserved); i++) {
		printf("%02x", (int) sidb->reserved[i]);
	}
	printf("\n");
	printf("idBlockSize:    0x%08lx\n", sidb->idBlockSize);
	printf("userBlockSize:  0x%04x\n", sidb->userBlockSize);
	printf("userBlockLoc:   0x%04x\n", sidb->userBlockLoc);
	printf("idBlockCRC:     0x%04x\n", sidb->idBlockCRC);
	printf("marker:         0x");
	for (i = 0; i < sizeof(sidb->marker); i++) {
		printf("%02x", (int) sidb->marker[i]);
	}
	printf("\n");
#endif	// V_L > 2
#endif	// V_L > 0

	// get test CRC for this ID Block
	testCRC = CalcIDBlockCRC(sidb);

	// default to valid, active System ID Block result
	result = 0;

	// if bad CRC, report invalid+inactive System ID Block
	result |= (testCRC == sidb->idBlockCRC) ? 0x00 : 0x03;

	if (!memcmp(myValidMarker, &sidb->marker, sizeof(sidb->marker))) {
		// this is an active (valid marker) block, do nothing to result!
	} else if (!memcmp(myInvalidMarker, &sidb->marker, sizeof(sidb->marker))) {
		// this is an inactive (just an invalid marker) block
		result |= 0x01;
	} else {
		// this is an inactive+invalid block
		result |= 0x03;
	}

	return result;
}

⌨️ 快捷键说明

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