signed char ispVMRead()

in common/recipes-utils/cpldupdate/files/ivm_core.c [2474:2782]


signed char ispVMRead( unsigned short a_usiDataSize )
{
	//09/11/07 NN added local variables initialization
	unsigned short usDataSizeIndex    = 0;
	unsigned short usErrorCount       = 0;
	unsigned short usLastBitIndex     = 0;
	unsigned char cDataByte           = 0;
	unsigned char cMaskByte           = 0;
	unsigned char cInDataByte         = 0;
	unsigned char cCurBit             = 0;
	unsigned char cByteIndex          = 0;
	unsigned short usBufferIndex      = 0;
	unsigned char ucDisplayByte       = 0x00;
	unsigned char ucDisplayFlag       = 0x01;
	char StrChecksum[256]            = {0};
	unsigned char g_usCalculateChecksum = 0x00;

	//09/11/07 NN Type cast mismatch variables
	usLastBitIndex = (unsigned short)(a_usiDataSize - 1);

#ifndef VME_DEBUG
	/****************************************************************************
	*
	* If mask is not all zeros, then set the display flag to 0x00, otherwise
	* it shall be set to 0x01 to indicate that data read from the device shall
	* be displayed. If VME_DEBUG is defined, always display data.
	*
	*****************************************************************************/

	for ( usDataSizeIndex = 0; usDataSizeIndex < ( a_usiDataSize + 7 ) / 8; usDataSizeIndex++ ) {
		if ( g_usDataType & MASK_DATA ) {
			if ( g_pucOutMaskData[ usDataSizeIndex ] != 0x00 ) {
				ucDisplayFlag = 0x00;
				break;
			}
		}
		else if ( g_usDataType & CMASK_DATA ) {
			g_usCalculateChecksum = 0x01;
			ucDisplayFlag = 0x00;
			break;
		}
		else {
			ucDisplayFlag = 0x00;
			break;
		}
	}
#endif //VME_DEBUG

	/****************************************************************************
	*
	* Begin shifting data in and out of the device.
	*
	*****************************************************************************/

	for ( usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize; usDataSizeIndex++ ) {
		if ( cByteIndex == 0 ) {

			/***************************************************************
			*
			* Grab byte from TDO buffer.
			*
			***************************************************************/

			if ( g_usDataType & TDO_DATA ) {
				cDataByte = g_pucOutData[ usBufferIndex ];
			}

			/***************************************************************
			*
			* Grab byte from MASK buffer.
			*
			***************************************************************/

			if ( g_usDataType & MASK_DATA ) {
				cMaskByte = g_pucOutMaskData[ usBufferIndex ];
			}
			else {
				cMaskByte = 0xFF;
			}

			/***************************************************************
			*
			* Grab byte from CMASK buffer.
			*
			***************************************************************/

			if ( g_usDataType & CMASK_DATA ) {
				cMaskByte = 0x00;
				g_usCalculateChecksum = 0x01;
			}

			/***************************************************************
			*
			* Grab byte from TDI buffer.
			*
			***************************************************************/

			if ( g_usDataType & TDI_DATA ) {
				cInDataByte = g_pucInData[ usBufferIndex ];
			}

			usBufferIndex++;
		}

		cCurBit = readPort();

		if ( ucDisplayFlag ) {
			ucDisplayByte <<= 1;
			ucDisplayByte |= cCurBit;
		}

		/****************************************************************************
		*
		* Check if data read from port matches with expected TDO.
		*
		*****************************************************************************/

		if ( g_usDataType & TDO_DATA ) {
			//08/28/08 NN Added Calculate checksum support.
			if( g_usCalculateChecksum )
			{
				if(cCurBit == 0x01)
					g_usChecksum += (1 << (g_uiChecksumIndex % 8));
				g_uiChecksumIndex++;
			}
			else
			{
				if ( ( ( ( cMaskByte << cByteIndex ) & 0x80 ) ? 0x01 : 0x00 ) ) {
					if ( cCurBit != ( unsigned char ) ( ( ( cDataByte << cByteIndex ) & 0x80 ) ? 0x01 : 0x00 ) ) {
						usErrorCount++;
					}
				}
			}
		}

		/****************************************************************************
		*
		* Write TDI data to the port.
		*
		*****************************************************************************/

		writePort( g_ucPinTDI, ( unsigned char ) ( ( ( cInDataByte << cByteIndex ) & 0x80 ) ? 0x01 : 0x00 ) );

		if ( usDataSizeIndex < usLastBitIndex ) {

			/****************************************************************************
			*
			* Clock data out from the data shift register.
			*
			*****************************************************************************/

			sclock();
		}
		else if ( g_usFlowControl & CASCADE ) {

			/****************************************************************************
			*
			* Clock in last bit for the first N - 1 cascaded frames.
			*
			*****************************************************************************/

			sclock();
		}

		/***************************************************************
		*
		* Increment the byte index. If it exceeds 7, then reset it back
		* to zero.
		*
		***************************************************************/

		cByteIndex++;
		if ( cByteIndex >= 8 ) {
			if ( ucDisplayFlag ) {

				/***************************************************************
				*
				* Store displayed data in the TDO buffer. By reusing the TDO
				* buffer to store displayed data, there is no need to allocate
				* a buffer simply to hold display data. This will not cause any
				* false verification errors because the true TDO byte has already
				* been consumed.
				*
				***************************************************************/

				g_pucOutData[ usBufferIndex - 1 ] = ucDisplayByte;
				ucDisplayByte = 0;
			}

			cByteIndex = 0;
		}
		//09/12/07 Nguyen changed to display the 1 bit expected data
		else if(a_usiDataSize == 1)
		{
			if ( ucDisplayFlag ) {

				/***************************************************************
				*
				* Store displayed data in the TDO buffer. By reusing the TDO
				* buffer to store displayed data, there is no need to allocate
				* a buffer simply to hold display data. This will not cause any
				* false verification errors because the true TDO byte has already
				* been consumed.
				*
				***************************************************************/

				/****************************************************************************
				*
				* Flip ucDisplayByte and store it in cDataByte.
				*
				*****************************************************************************/
				cDataByte = 0x00;
				for ( usBufferIndex = 0; usBufferIndex < 8; usBufferIndex++ ) {
					cDataByte <<= 1;
					if ( ucDisplayByte & 0x01 ) {
						cDataByte |= 0x01;
					}
					ucDisplayByte >>= 1;
				}
				g_pucOutData[ 0 ] = cDataByte;
				ucDisplayByte = 0;
			}

			cByteIndex = 0;
		}
	}
	if ( ucDisplayFlag ) {

		/****************************************************************************
		*
		* Display data read from the device.
		*
		*****************************************************************************/

#ifdef VME_DEBUG
		printf( "RECIEVED TDO (" );
#else
		vme_out_string( "Display Data: 0x" );
#endif //VME_DEBUG

		//09/11/07 NN Type cast mismatch variables
		for ( usDataSizeIndex = (unsigned short)( ( a_usiDataSize + 7 ) / 8 ); usDataSizeIndex > 0 ; usDataSizeIndex-- ) {
			cMaskByte = g_pucOutData[ usDataSizeIndex - 1 ];
			cDataByte = 0x00;

			/****************************************************************************
			*
			* Flip cMaskByte and store it in cDataByte.
			*
			*****************************************************************************/

			for ( usBufferIndex = 0; usBufferIndex < 8; usBufferIndex++ ) {
				cDataByte <<= 1;
				if ( cMaskByte & 0x01 ) {
					cDataByte |= 0x01;
				}
				cMaskByte >>= 1;
			}
#ifdef VME_DEBUG
			printf( "%.2X", cDataByte );
			if ( ( ( ( a_usiDataSize + 7 ) / 8 ) - usDataSizeIndex ) % 40 == 39 ) {
				printf( "\n\t\t" );
			}
#else
			vme_out_hex( cDataByte );
#endif //VME_DEBUG
		}

#ifdef VME_DEBUG
		printf( ")\n\n" );
#else
		vme_out_string( "\n\n" );
#endif //VME_DEBUG
		//09/02/08 Nguyen changed to display the data Checksum
		if(g_usChecksum != 0)
		{
			g_usChecksum &= 0xFFFF;
			sprintf(StrChecksum,"Data Checksum: %04x\n\n",g_usChecksum);
			vme_out_string(StrChecksum);
			g_usChecksum = 0;
		}
	}

	if ( usErrorCount > 0 ) {
		if ( g_usFlowControl & VERIFYUES ) {
			vme_out_string( "USERCODE verification failed.  Continue programming......\n\n" );
			g_usFlowControl &= ~( VERIFYUES );
			return 0;
		}
		else {

#ifdef VME_DEBUG
			printf( "TOTAL ERRORS: %d\n", usErrorCount );
#endif //VME_DEBUG

			return VME_VERIFICATION_FAILURE;
		}
	}
	else {
		if ( g_usFlowControl & VERIFYUES ) {
			vme_out_string( "USERCODE verification passed.  Programming aborted. \n\n" );
			g_usFlowControl &= ~( VERIFYUES );
			return 1;
		}
		else {
			return 0;
		}
	}
}