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

📄 hardware.c

📁 在嵌入式系统中对Lattice CPLD软件升级时所需的VME文件生成所需源代码。
💻 C
📖 第 1 页 / 共 2 页
字号:
		/* Scan instruction or bypass register */
		if ( iIndex % 8 == 0 ) {
			cCurByte = pcSource[ iSourceIndex++ ];
		}
		cBitState = ( char ) ( ( ( cCurByte << iIndex % 8 ) & 0x80 ) ? 0x01 : 0x00 );
		writePort( pinTDI, cBitState );
		sclock();
	}   

	if ( iIndex % 8 == 0 )  {
		cCurByte = pcSource[ iSourceIndex++ ];
	}

	cBitState = ( char ) ( ( ( cCurByte << iIndex % 8 ) & 0x80 ) ? 0x01 : 0x00 );
	writePort( pinTDI, cBitState );
}



/****************************************************************************
                       ispVMSateMachine                                     
 This procedure walk all devices in the daisy chain from a given BSCAN state
 to the next desirable state.
 If the next state is TLR, the BSCAN state machine is brute forced into
 TLR by driving TMS HIGH and pulse TCK 6 times.
 Global
      cCurrentJTAGState     --keeps tap on the current state            
 Input
      cNextJTAGState    --the destination state to trasverse to.
*****************************************************************************/
void ispVMStateMachine( char cNextJTAGState )
{
	char cPathIndex, cStateIndex;
	
	if ( ( cCurrentJTAGState == cNextJTAGState ) && ( cNextJTAGState != RESET ) ) {
		return;
	}
	
	for ( cStateIndex = 0; cStateIndex < 20; cStateIndex++ ) {
		if ( ( cCurrentJTAGState == iStates[ cStateIndex ].CurState ) && ( cNextJTAGState == iStates[cStateIndex].NextState ) ) {
			break;
		}
	}

	cCurrentJTAGState = cNextJTAGState;
	for ( cPathIndex = 0; cPathIndex < iStates[ cStateIndex ].Pulses; cPathIndex++ ) {
		if ( ( iStates[ cStateIndex ].Pattern << cPathIndex ) & 0x80 ) {
			writePort( pinTMS, ( unsigned char ) 0x01 );
		}
		else {
			writePort( pinTMS, ( unsigned char ) 0x00 );
		}
		sclock();
	}
	
	writePort( pinTDI, 0x00 );
	writePort( pinTMS, 0x00 );
}



/***************************************************************************
                       ispVMSend                         
 Send the TDI data stream to devices.   
 The data stream can be instructions or data.                        
 Input:                                                         
 a_usiDataSize---------The length of the data stream.                  
*****************************************************************************/
char ispVMSend( unsigned short a_usiDataSize )
{
	unsigned short iIndex;
	unsigned short iInDataIndex = 0;
	unsigned char cCurByte;
	unsigned char cBitState;
	
	for ( iIndex = 0; iIndex < a_usiDataSize - 1; iIndex++ ) { 
		if ( iIndex % 8 == 0 ) { 
			cCurByte = InData[ iInDataIndex++ ];
		}
		cBitState = ( char ) ( ( ( cCurByte << iIndex % 8 ) & 0x80 ) ? 0x01 : 0x00 );

#ifdef VME_DEBUG
		if ( cBitState ) {
			printf("1");
		}
		else {
			printf("0");
		}
#endif
		writePort( pinTDI, cBitState );
		sclock();
	}

	if ( iIndex % 8 == 0 ) {
		/* Take care of the last bit */
		cCurByte = InData[ iInDataIndex ]; 
	}

	cBitState = ( char ) ( ( ( cCurByte << iIndex % 8 ) & 0x80 ) ? 0x01 : 0x00 );

	writePort( pinTDI, cBitState );
	if ( usFlowControl & CASCADE ) {
		/* 1/15/04 Clock in last bit for the first n-1 cascaded frames */
		sclock();
	}

#ifdef VME_DEBUG
	if ( cBitState ) {
		printf( "1" );
	}
	else {
		printf( "0" );
	}
	printf("\n");
#endif

	return 0;
}

/****************************************************************************
                     ispVMRead
 Read the data stream from devices and verify.                      
 Input:                                                                                                             *
 a_usiDataSize---------The length (in bits) of the data stream.       
*****************************************************************************/
char ispVMRead( unsigned short a_usiDataSize )
{
	unsigned short index,error,last_bit;
	unsigned char cDataByte;
	unsigned char cMaskByte;
	unsigned char cInDataByte;
	unsigned char cCurBit;
	unsigned char cByteIndex;
	unsigned short  i, j, isc_count = 0;
	BYTE result = 0, bytedata = 0;

	j=0;
	error=0;
	if (a_usiDataSize) {
		last_bit = a_usiDataSize-1;
	}
	else {
		return (0);
	}
	
	cByteIndex=0;
	i = 0;
	
	/*check if output mask exist*/
	for (index = 0; index <a_usiDataSize; index++) { 
		if (cByteIndex==0){ 
			if (usDataType&TDO_DATA) {
				cDataByte = OutData[j];
			}
			
			if (usDataType&MASK_DATA) {
				cMaskByte =OutMaskData[j];
			}
			else { 
				cMaskByte = 0xFF;
			}
          
			if (usDataType&TDI_DATA) {
				cInDataByte=InData[j];
			}
			
			j++;
		}
		
		cCurBit = readPort();

#ifdef VME_DEBUG
		if (cCurBit) 
			printf("H");
		else 
			printf("L");
#endif
		if (usDataType&TDO_DATA) {
			if ((((cMaskByte << cByteIndex) & 0x80) ? 0x01 : 0x00)){	
				if (cCurBit != (unsigned char)(((cDataByte << cByteIndex) & 0x80) ? 0x01 : 0x00)){
					error++;  
				}
			}
		}
		
		writePort(pinTDI, (char)(((cInDataByte << cByteIndex) & 0x80) ? 0x01 : 0x00));
		
		if ( index < last_bit ) {
			sclock();  /*clock data out from the data shift registers */
		}
		else if ( usFlowControl & CASCADE ) {
			/* 1/15/04 Clock in last bit for the first n-1 cascaded frames */
			sclock();
		}

		cByteIndex++;
		if (cByteIndex >= 8) {
			cByteIndex=0;
		}
	}

#ifdef VME_DEBUG
	printf("\n");
	printf( "Number of errors found: %d\n", error );
#endif

	if ( error > 0 ) {
		if ( usFlowControl & VERIFYUES ) {
			printf( "USERCODE verification failed.  Continue programming......\n\n" );
			usFlowControl &= ~( VERIFYUES );
			return 0;
		}
		else {
			return -1;
		}
	}
	else {
		if ( usFlowControl & VERIFYUES ) {
			printf( "USERCODE verification passed.  Programming aborted. \n\n" );
			usFlowControl &= ~( VERIFYUES );
			return 1;
		}
		else {
			return 0;
		}
	}
}

/* 05/27/03 Nguyen added to support Dynamic IO */
char ispVMReadandSave( unsigned short int a_usiDataSize )
{
	unsigned short int index,error,last_bit;
	unsigned char cDataByte;
	unsigned char cMaskByte;
	unsigned char cInDataByte;
	unsigned char cCurBit;
	unsigned char cByteIndex;
	short int i, j, isc_count = 0;
	BYTE result = 0, bytedata = 0;
	short int OutBitIndex = 0;
	
	j=0;
	error=0;
	if ( a_usiDataSize ) {
		last_bit = a_usiDataSize - 1;
	}
	else {
		return ( 0 );
	}
	
	cByteIndex = 0;
	i = 0;
	OutBitIndex = 0;
	/*check if output mask exist*/
	for ( index = 0; index < a_usiDataSize; index++ ) {
		if ( cByteIndex == 0 ) { 
			if ( usDataType & DMASK_DATA ) {
				cMaskByte = OutDMaskData[ j ];
			}
			else { 
				cMaskByte = 0x00;
			}
          
			if ( usDataType & TDI_DATA ) {
				cInDataByte = InData[ j ];
			}
			j++;
		}
		
		cCurBit = readPort();

#ifdef VME_DEBUG
		if ( cCurBit ) {
			printf( "H" );
		}
		else {
			printf( "L" );
		}
#endif
		cDataByte = ( char )( ( ( cInDataByte << cByteIndex ) & 0x80 ) ? 0x01 : 0x00 );
		if ( OutBitIndex % 8 == 0 ) {
			OutData[ OutBitIndex / 8 ] = 0x00;
		}
		if ( ( ( ( cMaskByte << cByteIndex ) & 0x80 ) ? 0x01 : 0x00 ) ) {	
			OutData[ OutBitIndex / 8 ] |= (unsigned char)(((cDataByte & 0x1) ? 0x01:0x00) << (7-OutBitIndex%8));	
		}
		else {
			OutData[ OutBitIndex / 8 ] |= (unsigned char)(((cCurBit & 0x1) ? 0x01:0x00) << (7-OutBitIndex%8));	
		}
		OutBitIndex++;
		writePort( pinTDI, cDataByte );
		if ( index < last_bit ) {
			sclock();  /* clock data out from the data shift registers */
		}
		cByteIndex++;
		if ( cByteIndex >= 8 ) {
			cByteIndex = 0;
		}
	}
	return( 0 );
}

/****************************************************************************
                     ispVMStart                                      
 Enable the port to the device and set the State to RESET (TLR)      
*****************************************************************************/
void ispVMStart( void )
{
	writePort( pinCE, 0x01 );   /*turn the Lattice Cable on*/
	ispVMStateMachine( RESET );    /*step devices to RESET state*/
}

/****************************************************************************
*                     ispVMEnd                                        
* Set the State of devices to RESET to enable the devices and disable 
* the port.                                                           
*****************************************************************************/
void ispVMEnd( void )
{
	ispVMStateMachine( RESET );   /*step devices to RESET state */
	ispVMDelay( 1000 );              /*wake up devices*/
	writePort( pinCE, 0x00 );  /*disable the Lattice Cable*/
}

⌨️ 快捷键说明

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