www.pudn.com > usbhost_source.rar > DEBUG.C


/** 
*	debug.c 
*	シリアル通信デバッグルーチン 
*	ハイパーターミナル対応のため、CRで改行、ループバックあり 
*	Copyright (c)2002 Junichi Tomaru 
*/ 
#include	"h8hst.h" 
 
#define		DEBUGPORT	1	/* 通信デバッグのポート(SCI1) */ 
 
const char	hexcode[] = { '0', '1', '2', '3', '4', '5', '6', '7', 
						'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 
char	debbuf[64]; 
int	pos; 
 
/** 
*	8ビット数値を2進数文字列にする 
*/ 
char	*bin8binstr( unsigned char dat, char *buf ) 
{ 
	*( buf + 8 ) = '\0'; 
	if(( dat & 0x1 ) == 1 ){	*( buf + 7 ) = '1';	} 
	else{				*( buf + 7 ) = '0';	} 
	if(( dat & 0x2 ) == 2 ){	*( buf + 6 ) = '1';	} 
	else{				*( buf + 6 ) = '0';	} 
	if(( dat & 0x4 ) == 4 ){	*( buf + 5 ) = '1';	} 
	else{				*( buf + 5 ) = '0';	} 
	if(( dat & 0x8 ) == 8 ){	*( buf + 4 ) = '1';	} 
	else{				*( buf + 4 ) = '0';	} 
	if(( dat & 0x10 ) == 0x10 ){	*( buf + 3 ) = '1';	} 
	else{				*( buf + 3 ) = '0';	} 
	if(( dat & 0x20 ) == 0x20 ){	*( buf + 2 ) = '1';	} 
	else{				*( buf + 2 ) = '0';	} 
	if(( dat & 0x40 ) == 0x40 ){	*( buf + 1 ) = '1';	} 
	else{				*( buf + 1 ) = '0';	} 
	if(( dat & 0x80 ) == 0x80 ){	*( buf + 0 ) = '1';	} 
	else{				*( buf + 0 ) = '0';	} 
	return	buf; 
} 
 
 
/** 
*	8ビット数値を16進文字列にする 
*/ 
char	*bin8tohex( unsigned char bin, char *buf ) 
{ 
	*( buf + 0 ) = hexcode[bin/0x10]; 
	*( buf + 1 ) = hexcode[bin%0x10]; 
	*( buf + 2 ) = '\0'; 
	return	buf; 
} 
 
 
/** 
*	16ビット数値を16進文字列に 
*/ 
char	*bin16tohex( unsigned bin, char *buf ) 
{ 
	*( buf + 0 ) = hexcode[bin/0x1000]; 
	bin %= 0x1000; 
	*( buf + 1 ) = hexcode[bin/0x100]; 
	bin %= 0x100; 
	*( buf + 2 ) = hexcode[bin/0x10]; 
	*( buf + 3 ) = hexcode[bin%0x10]; 
	*( buf + 4 ) = '\0'; 
	return	buf; 
} 
 
 
/** 
*	32ビット数値を16進文字列に 
*/ 
char	*bin32tohex( unsigned long bin, char *buf ) 
{ 
	*( buf + 0 ) = hexcode[bin/0x10000000]; 
	bin %= 0x10000000; 
	*( buf + 1 ) = hexcode[bin/0x1000000]; 
	bin %= 0x1000000; 
	*( buf + 2 ) = hexcode[bin/0x100000]; 
	bin %= 0x100000; 
	*( buf + 3 ) = hexcode[bin/0x10000]; 
	bin %= 0x10000; 
	*( buf + 4 ) = hexcode[bin/0x1000]; 
	bin %= 0x1000; 
	*( buf + 5 ) = hexcode[bin/0x100]; 
	bin %= 0x100; 
	*( buf + 6 ) = hexcode[bin/0x10]; 
	*( buf + 7 ) = hexcode[bin%0x10]; 
	*( buf + 8 ) = '\0'; 
	return	buf; 
} 
 
 
/** 
*	コマンド入力 
*/ 
bool	getdebcom( void ) 
{ 
	char	c; 
 
	if( getrxcount( DEBUGPORT ) != 0 ){		/* 受信あったら */ 
		c = getsio1( DEBUGPORT ); 
		if( c == '\r' ){			/* CR */ 
			setsio1w( DEBUGPORT, '\r' ); 
			setsio1w( DEBUGPORT, '\n' ); 
			debbuf[pos] = '\0'; 
			pos = 0; 
			return	TRUE; 
		} 
		else if( c == '\b' ){			/* BS */ 
			if( pos > 0 ){ 
				pos--; 
				setsio1w( DEBUGPORT, '\b' ); 
				setsio1w( DEBUGPORT, ' ' ); 
				setsio1w( DEBUGPORT, '\b' ); 
			} 
			else{ 
				pos = 0; 
			} 
			return	FALSE; 
		} 
		else{ 
			setsio1w( DEBUGPORT, c );	/* ループバック */ 
			debbuf[pos] = c; 
			pos++; 
			return	FALSE; 
		} 
	} 
	return	FALSE; 
} 
 
 
/** 
*	ダンプサブルーチン 
*/ 
void	dump( char *p ) 
{ 
	int	j, k; 
	char	buf[16]; 
 
	setsiostr( DEBUGPORT, "LOW8BITS : 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F \r\n" ); 
	setsiostr( DEBUGPORT, "           ----------------------------------------------- \r\n" ); 
	for( j = 0; j < 16; j++ ){ 
		setsiostr( DEBUGPORT, bin32tohex(( long )p + j * 16, buf )); 
		setsiostr( DEBUGPORT, " : " ); 
		for( k = 0; k < 16; k ++ ){ 
			setsiostr( DEBUGPORT, bin8tohex( *( p + j * 16 + k ), buf )); 
			setsiostr( DEBUGPORT, " " ); 
		} 
		setsiostr( DEBUGPORT, "\r\n" ); 
	} 
} 
 
 
/** 
*	メモリダンプ 
*/ 
void	memdump( char *p ) 
{ 
	long	l; 
 
	l = strtol( p, NULL, 16 ); 
	dump(( char * )l ); 
} 
 
 
/** 
*	メモリセット 
*/ 
void	setmemory( char *p ) 
{ 
	byte	*n; 
	long	ofs; 
	int	j; 
	char	buf[8]; 
	char	**ep; 
 
	n = NULL; 
	ep = &p; 
	ofs = strtol( p, ep, 16 ); 
	p = *ep + 1; 
	j = ( int )strtol( p, ep, 16 ); 
	*( n + ofs ) = ( byte )j; 
	setsiostr( DEBUGPORT, bin8tohex( *( n + ofs ), buf )); 
	setsiostr( DEBUGPORT, "\r\n" ); 
} 
 
 
/** 
*	ポート入力 
*/ 
void	portin( char *p ) 
{ 
	long	a; 
	char	i, buf[16]; 
 
	a = strtol( p, NULL, 16 ); 
	i = *(( unsigned char * )a ); 
	setsiostr( DEBUGPORT, "IN:" ); 
	setsiostr( DEBUGPORT, bin8tohex( i, buf )); 
	setsiostr( DEBUGPORT, "\r\n" ); 
} 
 
 
/** 
*	ポート出力 
*/ 
void	portout( char *p ) 
{ 
	long	addr; 
	char	data, buf[8]; 
	char	**ep; 
 
	ep = &p; 
	addr = strtol( p, ep, 16 ); 
	p = *ep + 1; 
	data = (char)strtol( p, ep, 16 ); 
	*(( unsigned char * )addr ) = data; 
	setsiostr( DEBUGPORT, bin8tohex( *(( unsigned char * )addr ), buf )); 
	setsiostr( DEBUGPORT, "\r\n" ); 
} 
 
 
/** 
*	ヘルプ 
*/ 
void	help( void ) 
{ 
	setsiostr( DEBUGPORT, "\r\n" ); 
	setsiostr( DEBUGPORT, "               ---- H8-USB Host Board Command Help ----\r\n" ); 
	setsiostr( DEBUGPORT, "DUMP                                  Memory Dump\r\n" ); 
	setsiostr( DEBUGPORT, "FILMEM       Memory Fill\r\n" ); 
	setsiostr( DEBUGPORT, "HELP                                             Help\r\n" ); 
	setsiostr( DEBUGPORT, "INPUT                                 Byte Input\r\n" ); 
	setsiostr( DEBUGPORT, "OUTPUT                     Byte Output\r\n" ); 
	setsiostr( DEBUGPORT, "REBOOT                                           Restart\r\n" ); 
	setsiostr( DEBUGPORT, "SETMEM                     Enter Memory\r\n" ); 
	setsiostr( DEBUGPORT, "USBRD                                 SL811HS REGISTER READ\r\n" ); 
	setsiostr( DEBUGPORT, "USBWR                      SL811HS REGISTER WRITE\r\n" ); 
	setsiostr( DEBUGPORT, "VERSION                                          Version\r\n" ); 
} 
 
 
/** 
*	メモリフィル 
*/ 
void	fillmem( char *p ) 
{ 
	long	s; 
	int	j, k; 
	char	*n, **ep; 
 
	n = NULL; 
	ep = &p; 
	s = strtol( p, ep, 16 ); 
	p = *ep + 1; 
	j = ( int )strtol( p, ep, 16 ); 
	p = *ep + 1; 
	k = ( int )strtol( p, ep, 16 ); 
	memset( n + s, k, j ); 
	dump( n + s ); 
} 
 
 
/** 
*	SL811HSのレジスタを読む 
*/ 
void	usbread( char *p ) 
{ 
	byte	addr; 
	char	i, buf[16]; 
 
	addr = (byte)strtol( p, NULL, 16 ); 
	i = sl_read( addr ); 
	setsiostr( DEBUGPORT, "IN:" ); 
	setsiostr( DEBUGPORT, bin8tohex( i, buf )); 
	setsiostr( DEBUGPORT, "\r\n" ); 
} 
 
 
/** 
*	SL811HSのレジスタに書く 
*/ 
void	usbwrite( char *p ) 
{ 
	byte	addr; 
	char	data, buf[8]; 
	char	**ep; 
 
	ep = &p; 
	addr = (byte)strtol( p, ep, 16 ); 
	p = *ep + 1; 
	data = (char)strtol( p, ep, 16 ); 
	sl_write( addr, data ); 
	setsiostr( DEBUGPORT, bin8tohex( data, buf )); 
	setsiostr( DEBUGPORT, "\r\n" ); 
} 
 
 
/** 
*	バージョン 
*/ 
void	version( void ) 
{ 
	setsiostr( DEBUGPORT, "\r\n" ); 
	setsiostr( DEBUGPORT, "      --- H8 USB HOST" ); 
	setsiostr( DEBUGPORT, vers ); 
	setsiostr( DEBUGPORT, " ---\r\n" ); 
	setsiostr( DEBUGPORT, revs ); 
	setsiostr( DEBUGPORT, " Copyright Junichi Tomaru 2004\r\n" ); 
} 
 
 
/** 
*	プロンプト 
*/ 
void	prompt( void ) 
{ 
	pos = 0; 
	debbuf[0] = '\0'; 
	setsiostr( DEBUGPORT, "\r\n*>" ); 
} 
 
 
/** 
*	デバッグモード初期化 
*/ 
void	debuginit( void ) 
{ 
	pos = 0; 
	debbuf[0] = '\0'; 
	setsiostr( DEBUGPORT, "\r\n" ); 
	setsiostr( DEBUGPORT, "Welcome to H8-USB HOST BOARD.\r\n" ); 
	prompt( ); 
} 
 
 
/** 
*	デバッグ 
*/ 
void	debugmain( void ) 
{ 
	char	i, *p, *q, buf[16]; 
 
	if( getdebcom( ) == TRUE ){ 
		p = strchr( debbuf, ' ' ); 
		if( p == NULL ){ 
			p = strchr( debbuf, '\0' ); 
		} 
		for( q = debbuf, i = 0; p != q; q++, i++ ){/* 比較のため大文字にする */ 
			buf[i] = ( char )toupper( *q ); 
		} 
		buf[i] = '\0'; 
		while( *p == ' ' ){			/* スペース飛ばす */ 
			p++; 
		} 
		if( strcmp( buf, "DUMP" ) == 0 ){ 
			memdump( p );			/* ダンプ */ 
		} 
		else if( strcmp( buf, "FILLMEM" ) == 0 ){ 
			fillmem( p );			/* メモリフィル */ 
		} 
		else if( strcmp( buf, "HELP" ) == 0 ){ 
			help( );			/* ヘルプ */ 
		} 
		else if( strcmp( buf, "INPUT" ) == 0 ){ 
			portin( p );			/* 入力 */ 
		} 
		else if( strcmp( buf, "OUTPUT" ) == 0 ){ 
			portout( p );			/* 出力 */ 
		} 
		else if( strcmp( buf, "REBOOT" ) == 0 ){ 
			while( gettxcount( 1 ) != 0 ){	/* 送信完了待ち */ 
			} 
			start( );			/* リセット */ 
		} 
		else if( strcmp( buf, "SETMEM" ) == 0 ){ 
			setmemory( p );			/* メモリセット */ 
		} 
		else if( strcmp( buf, "USBRD" ) == 0 ){ 
			usbread( p );			/* USB811HSレジスタ読み込み */ 
		} 
		else if( strcmp( buf, "USBWR" ) == 0 ){ 
			usbwrite( p );			/* USB811HSレジスタ書き込み */ 
		} 
		else if( strcmp( buf, "VERSION" ) == 0 ){ 
			version( );			/* バージョン */ 
		} 
		else if( debbuf[0] != '\0' ){		/* コマンドおかしい */ 
			setsiostr( DEBUGPORT, "ILLEGAL COMMAND.\r\n" ); 
		} 
		prompt( ); 
	} 
} 
 
 
/* end of debug.c */