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

📄 eval_cmnd.c

📁 C89c51 usb驱动程序,实现了usb转串口的功能,提供了一个虚拟的串口
💻 C
📖 第 1 页 / 共 2 页
字号:
*  NB: If the application requires a lot of data to be copied, it might be necessary to
*  service the watchdog occasionally, if the WDT is set to the shortest (default) time.
*/
void
default_params_cmd( void )
{
	const  char   *phbSource;
	far    uint8  *phbDestin;
	uint8  bByteCount = MAX_PARAMS;
	
	phbSource = kszGreeting;         // Use const strings as test data
	phbDestin = ghabParam;
	while ( bByteCount-- != 0 )      // Copy strings to ERAM array
	{
		*phbDestin++ = *phbSource++ ;
	}	
	store_persistent_data();         // Commit to EEPROM
}


/*
*  Command function 'SE':  Show system error flags (word).
*/
void
show_errors_cmd( void )
{
	uint16   wBit;

	putconstr( kszHead16bits );
	for ( wBit = 0x8000;  wBit != 0;  wBit >>= 1 )
	{
		putBoolean( (gwSystemError & wBit) ? 1 : 0 );
		putch( SPACE );
		if ( wBit == 0x0100 )  
			{ putch( SPACE ); putch( SPACE ); }
	}
	putNewLine();
	gwSystemError = 0;		/* Clear transient error flags */
}


/*
*  Command function 'VN':  Print firmware version number.
*/
void
version_cmd( void )
{
	putconstr( kszGreeting );
	putconstr( kszVersion );
	putNewLine();
}


/*
*  Command function 'RS':  Reset MCU / restart program.
*  Turns off interrupts and forces watchdog reset.
*/
void
reset_MCU_cmd( void )
{
	di();
	while ( 1 )
		continue;
}



/***********************************  DEBUG COMMANDS  ************************************
*
*
*  Command function 'Dx':  Dumps a 256 byte block of memory in Hex ASCII format.
*
*  The command mnemonic may be 'DC', 'DD' or 'DE'.
*  If it is 'DC', the 64kB program CODE memory space is accessed;
*  if it is 'DE', the EEPROM space is mapped in and the dump block size is 128 bytes;
*  otherwise, the extended data memory (& I/O device) space is accessed.
*
*  In the case of 'DE', the command argument is an EEPROM page number (0..7);
*  otherwise the argument is a hexadecimal address (optional).
*  If the address is below 0x0400, the on-chip extended RAM is accessed.
*  If the address is above 0x0FFF, external memory (or an I/O device) is accessed.
*  If no address is given, the previous value is used, incremented by 256.
*  The dump begins on a 16 byte boundary ($aaa0), regardless of the argument LSD.
*
*  Arg1 @ CmdLine[3] is start addr (0000..FFFF) (optional), or EEPROM page # (0..7)
*/
void
dump_memory_cmd( void )
{
	static  uint16  uwStartAddr;    // remembered for next time command used
	uint16  uwAddr, uwArgValue;
	uint8   ubRow, ubCol, ubDat;
	char    c2;
	uint8   ubPageRows = 16;

	c2 = toupper( gacCmdLine[1] );
	uwArgValue = hexatoi( &gacCmdLine[3] );      // defaults to 0 if invalid arg.
	
	if ( c2 == 'E' )     // Assume EEPROM page # given
	{
		uwAddr = (uwArgValue & 7) * 128;
		ubPageRows = 8;
	}
	else if ( isHexDigit( gacCmdLine[3] ) )      // Start address given...
	{
		uwStartAddr = uwArgValue & 0xFFF0;       // ... save it for next time
		uwAddr = uwStartAddr;
	}
	else  uwAddr = uwStartAddr;    // No arg given -- use last value

	for ( ubRow = 0;  ubRow < ubPageRows;  ubRow++ )
	{
		putHexword( uwAddr );
		putch( SPACE );
		for ( ubCol = 0;  ubCol < 16;  ubCol++ )
		{
			putch( SPACE );
			if ( ubCol == 8 )  putch( SPACE );
			if ( c2 == 'E' )  ubDat = eeprom_read_byte( uwAddr++ );
			else if ( c2 == 'C' )  ubDat = peek_code_byte( uwAddr++ );
			else  ubDat = peek_data_byte( uwAddr++ );
			putHexbyte( ubDat );
		}
		putch( SPACE );
		putch( SPACE );
		uwAddr -= 16;
		for ( ubCol = 0;  ubCol < 16;  ubCol++ )
		{
			if ( c2 == 'E' )  ubDat = eeprom_read_byte( uwAddr++ );
			else if ( c2 == 'C' )  ubDat = peek_code_byte( uwAddr++ );
			else  ubDat = peek_data_byte( uwAddr++ );
			if ( ubDat >= 32 && ubDat < 127 )  putch( ubDat );
			else  putch( SPACE );
		}
		putNewLine();
	}
	if ( c2 != 'E' )  uwStartAddr += 256;    // Show next 256-byte block next time
}


/*
*  Command function 'RM': Read and output MCU low RAM (idata) contents (2 hex)
*
*  Arg1 @ CmdLine[3] is RAM addr (0..FF)
*/
void
read_MCU_mem_cmd( void )
{
	putch( SPACE );
	putHexbyte( *(uint8 idata *)hexatoi( &gacCmdLine[3] ) );
	putNewLine();
}


/*
*  Command function 'WM':  Write MCU low RAM (idata) contents (2 hex).
*
*  Arg1 @ CmdLine[3] is 2-digit RAM address (00..FF)
*  Arg2 @ CmdLine[6] is 2-digit data (byte) to write.
*  The write is not verified.
*/
void
write_MCU_mem_cmd( void )
{
	if ( !isHexDigit( gacCmdLine[3] )        /* Crude syntax check */
	||   !isHexDigit( gacCmdLine[4] )
	||    gacCmdLine[5] != SPACE
	||   !isHexDigit( gacCmdLine[6] ) )
	{
		putconstr( kszCommandError );
	}
	else
		*(uint8 idata *)hexatoi( &gacCmdLine[3] ) = (uint8)hexatoi( &gacCmdLine[6] );
}


/*
*   Command function 'RP': Read 8051 I/O port pin.
*   Intended to show the state of the external port pin, not the internal port latch!
*
*	NB: For this command to function correctly, the compiler must generate direct (MOV P?,r?)
*	    instructions in order to read the 8051 port pins, i.e. not the internal port latch.
*	    (Depending on the compiler, it may be necessary to use assembler code to read the
*       port pins. The Hi-Tech C-8051 compiler V9.xx generates the required code.)
*
*  Arg1 @ CmdLine[3] is port addr (0..3)
*  Arg2 @ CmdLine[5] is bit position (0..7)
*/
void
read_port_cmd( void )
{
	uint8  bPortPins;
	uint8  bPortNumber = hexctobin( gacCmdLine[3] );
	uint8  bBitPosn = hexctobin( gacCmdLine[5] );
	uint8  bBitMask = 1 << bBitPosn;

	if ( bPortNumber > 3 || bBitPosn > 7 )		/* Crude syntax check */
	{
		putconstr( kszCommandError );
		return;
	}

	if      ( bPortNumber == 0 )  bPortPins = P0;	/* This should read the port pins */
	else if ( bPortNumber == 1 )  bPortPins = P1;
	else if ( bPortNumber == 2 )  bPortPins = P2;
	else if ( bPortNumber == 3 )  bPortPins = P3;

	putch( 'P' ); putHexdigit( bPortNumber ); putch( '.' );
	putHexdigit( bBitPosn ); putch( '=' ); putBoolean( bPortPins & bBitMask );
	putNewLine();
}


/*
*   Command function 'WP':  Write 8051 I/O port (latch) bit.
*
*   Arg1 @ CmdLine[3] is port addr (0..3)
*   Arg2 @ CmdLine[5] is bit position (0..7)
*   Arg3 @ CmdLine[7] is bit value to write (0,1)
*
*	NB: For this command to function correctly, the compiler must generate Read/Modify/Write
*	    instructions in order to read the 8051 port latches, i.e. not the external port pins.
*	    (Depending on the compiler, it may be necessary to use assembler code to modify
*       port latches. The Hi-Tech C-8051 compiler V9.xx generates the required code. (?) )
*/
void
write_port_cmd( void )
{
	uint8  bPortNumber = hexctobin( gacCmdLine[3] );
	uint8  bBitPosn = hexctobin( gacCmdLine[5] );
	uint8  bBitMask = 1 << bBitPosn;
	bool   yBitState = hexctobin( gacCmdLine[7] );
	
	if ( bPortNumber > 3 || bBitPosn > 7 || yBitState > 2 )		/* Crude syntax check */
	{
		putconstr( kszCommandError );
		return;
	}

	if ( bPortNumber == 0 )
	{
		if ( yBitState == 0 )  Clear_Bit( P0, bBitMask );
		else   Set_Bit( P0, bBitMask );
	}
	else if ( bPortNumber == 1 )
	{
		if ( yBitState == 0 )  Clear_Bit( P1, bBitMask );
		else   Set_Bit( P1, bBitMask );
	}
	else if ( bPortNumber == 2 )
	{
		if ( yBitState == 0 )  Clear_Bit( P2, bBitMask );
		else   Set_Bit( P2, bBitMask );
	}
	else if ( bPortNumber == 3 )
	{
		if ( yBitState == 0 )  Clear_Bit( P3, bBitMask );
		else   Set_Bit( P3, bBitMask );
	}
}


/*
*  Command function 'RE': Read and output extended (far) memory byte.
*
*  Arg1 @ CmdLine[3] is RAM addr (0..FFFF)
*/
void
read_extd_cmd( void )
{
	putch( SPACE );
	putHexbyte( *(uint8 far *)hexatoi( &gacCmdLine[3] ) );
	putNewLine();
}


/*
*  Command function 'WE':  Write extended mamory (far) byte.
*
*  Arg1 @ CmdLine[3] is 4-digit RAM address (0000..FFFF)
*  Arg2 @ CmdLine[8] is 2-digit data (byte) to write.
*  The write is not verified.
*/
void
write_extd_cmd( void )
{
	if ( !isHexDigit( gacCmdLine[3] )        /* Crude syntax check */
	||    gacCmdLine[7] != SPACE
	||   !isHexDigit( gacCmdLine[8] ) )
	{
		putconstr( kszCommandError );
	}
	else
		*(uint8 far *)hexatoi( &gacCmdLine[3] ) = (uint8)hexatoi( &gacCmdLine[8] );
}


/*
*  Command function 'XE':  Execute function at specified address.
*  If the function returns properly, control will be passed back to the CLI.
*
*  Arg1 @ CmdLine[3] is the 4-digit target address.
*/
void
execute_cmd( void )
{
	if ( gacCmdLine[2] != SPACE
	||   !isHexDigit( gacCmdLine[3] ) )    /* No target address given */
	{
		putconstr( kszCommandError );
	}
	else
		(*(pfnvoid) hexatoi( &gacCmdLine[3] ))( );
}



/****************************  COMMON CLI FUNCTION "LIBRARY"  ***************************
*
*
*  Output NUL-terminated string (constant) to host PC port.
*  The string must be stored in the ".const" section (program code space).
*
*  The Hi-Tech C 8051 compiler deals with constant strings in this manner...
*  Define a "const char" array for each string, and pass the array name to the string
*  output function as its argument, e.g:
*
*          const char kszHello[] = "hello, world";
*          ...
*
*          putconstr( kszHello );
*
*  Newline (0x0A) is expanded to CR + LF (0x0D + 0x0A).
*  See also: putstr( ), putfarstr( ).
*
*  Called by:  command functions, etc, (NOT background process)
*  Entry args: pksz = address of const NUL-terminated string
*  Returns:    --
*  Affects:    --
*/

void
putconstr( const char * pksz )
{
	char   c;

	while ( (c = *pksz++) != NUL )
	{
		if ( c == 0x0A )
		{
			putch( CR );
			putch( LF );
		}
		else   putch( c );
	}
}


/*
*  Output NUL-terminated string, i.e. "near" or "idata" char array, in page zero RAM.
*  The string must be stored in the MCU internal RAM page zero (address range 00..FF).
*  Using the compiler Small Memory Model (recommended), variables which are not explicitly
*  declared "far" or "const" will be allocated to page zero RAM.
*
*  Newline (0x0A) is expanded to CR + LF (0x0D + 0x0A).
*  See also: putconstr( ), putfarstr( );
*
*  Called by:  command functions, etc, (NOT background process)
*  Entry args: psz = address of NUL-terminated string in page zero RAM.
*  Returns:    --
*  Affects:    --
*/

void
putstr( char * psz )
{
	char   c;

	while ( (c = *psz++) != NUL )
	{
		if ( c == 0x0A )
		{
			putch( CR );
			putch( LF );
		}
		else   putch( c );
	}
}


/*
*  Output a NUL-terminated "far" string.
*  The string must be stored in the Extended Memory area (Data map), which may be
*  on-chip extended RAM (addr < 0x0400), or external RAM or external Flash/EEPROM,
*  but not internal (on-chip) EEPROM.
*
*  Newline (0x0A) is expanded to CR + LF (0x0D + 0x0A).
*  See also: putconstr( ), putstr( );
*
*  Called by:  command functions, etc, (NOT background process)
*  Entry args: phsz = address of NUL-terminated string in "far" data memory.
*  Returns:    --
*  Affects:    --
*/

void
putfarstr( char far * phsz )
{
	char   c;

	while ( (c = *phsz++) != NUL )
	{
		if ( c == 0x0A )
		{
			putch( CR );
			putch( LF );
		}
		else   putch( c );
	}
}


/*
*  Output newline (CR+LF)...
*
*  Called by:  various
*  Entry args: --
*  Returns:    --
*  Affects:
*/

void
putNewLine( void )
{
	putch( CR );
	putch( LF );
}


/*
*  Output Boolean value as ASCII '0' or '1'.
*
*  Called by:  command functions, etc, (NOT background process)
*  Entry args: b = Boolean variable (zero or non-zero)
*  Returns:    void
*  Affects:
*/

void
putBoolean( bool  b )
{
	if ( b )  putch( '1');
	else  putch ( '0' );
}


/*
*  Output 4 LS bits of a byte as Hex (or BCD) ASCII char.
*
*  Called by:  command functions, etc, (NOT background process)
*  Entry args: d = value of Hex digit (0 to 0xf)
*  Returns:    void
*  Affects:    --
*/

void
putHexdigit( uint8 d )
{
	d &= 0x0F;
	if ( d < 10 )  putch ( '0' + d );
	else  putch ( 'A' + d - 10 );
}


/*
*  Output byte as 2 Hex ASCII chars, MSD first.
*
*  Called by:  command functions, etc, (NOT background process)
*  Entry args: b = byte to output
*  Returns:    void
*  Affects:    --
*/

void
putHexbyte( uint8 b )
{
	putHexdigit( b >> 4 );
	putHexdigit( b );
}


/*
*  Output a 16-bit unsigned word as an ASCII decimal number.
*  The number of digit places to be output (0 to 5) is specified as a parameter.
*  If the decimal word value is larger than can fit into the number of places
*  specified, then the output will be truncated to the least significant digit(s).
*  If the decimal word value is smaller than can occupy the number of places
*  specified, then the output will be padded with leading 0's.
*
*
*  Called by:  command functions, etc, (NOT background process)
*  Entry args: (uint16) uwArg1 = word to output
*              (uint8) ubPlaces = number of digit places to output (1..5)
*  Returns:    void
*  Affects:    --
*/

void
putDecimal( uint16 uwArg1, uint8 ubPlaces )
{
	int8   bPos;
	uint8  aubDigit[5];    /* BCD result, 1 byte for each digit */

	if ( ubPlaces > 5 )  ubPlaces = 5;
	for ( bPos = 4;  bPos >= 0;  --bPos )
	{
		aubDigit[bPos] = uwArg1 % 10;
		uwArg1 /= 10;
	}
	for ( bPos = 5 - ubPlaces;  bPos < 5;  ++bPos )
		putHexdigit( aubDigit[bPos] );
}


/*
*  Output 16-bit word as 4 Hex ASCII chars, MSD first.
*
*  Called by:  command functions, etc, (NOT background process)
*  Entry args: uwArg1 = word to output
*  Returns:    void
*  Affects:    --
*/

void
putHexword( uint16 uwArg1 )
{
	putHexdigit( (uint8) (uwArg1 >> 12) );
	putHexdigit( (uint8) (uwArg1 >> 8) );
	putHexdigit( (uint8) (uwArg1 >> 4) );
	putHexdigit( (uint8) (uwArg1 & 0xF) );
}


/*** end ***/

⌨️ 快捷键说明

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