📄 diag.cxx
字号:
/*========================================================================
//
// diag.c
//
// Infrastructure diagnostic output code
//
//========================================================================
//####COPYRIGHTBEGIN####
//
// -------------------------------------------
// The contents of this file are subject to the Red Hat eCos Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.redhat.com/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations under
// the License.
//
// The Original Code is eCos - Embedded Configurable Operating System,
// released September 30, 1998.
//
// The Initial Developer of the Original Code is Red Hat.
// Portions created by Red Hat are
// Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
// All Rights Reserved.
// -------------------------------------------
//
//####COPYRIGHTEND####
//========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): nickg
// Contributors: nickg
// Date: 1999-02-22
// Purpose: Infrastructure diagnostic output
// Description: Implementations of infrastructure diagnostic routines.
//
//####DESCRIPTIONEND####
//
//======================================================================*/
#include <pkgconf/system.h>
#include <pkgconf/hal.h>
#include <pkgconf/infra.h>
#include <cyg/infra/cyg_type.h> // base types
#include <cyg/infra/diag.h> // HAL polled output
#include <cyg/hal/hal_arch.h> // architectural stuff for...
#include <cyg/hal/hal_intr.h> // interrupt control
#include <cyg/hal/hal_diag.h> // diagnostic output routines
#ifdef CYGDBG_INFRA_DIAG_PRINTF_USE_VARARG
#include <stdarg.h>
#endif
#ifdef CYGDBG_INFRA_DIAG_USE_DEVICE
#include <cyg/io/io_diag.h>
#endif
/*----------------------------------------------------------------------*/
externC void diag_write_num(
cyg_uint32 n, /* number to write */
cyg_ucount8 base, /* radix to write to */
cyg_ucount8 sign, /* sign, '-' if -ve, '+' if +ve */
cyg_bool pfzero, /* prefix with zero ? */
cyg_ucount8 width /* min width of number */
);
class Cyg_dummy_diag_init_class {
public:
Cyg_dummy_diag_init_class() {
#ifdef CYGDBG_INFRA_DIAG_USE_DEVICE
diag_device_init();
#else
HAL_DIAG_INIT();
#endif
}
};
#ifdef CYGDBG_INFRA_DIAG_USE_DEVICE
// Initialize after IO devices.
static Cyg_dummy_diag_init_class cyg_dummy_diag_init_obj
CYGBLD_ATTRIB_INIT_AFTER(CYG_INIT_IO);
/*----------------------------------------------------------------------*/
/* Write single char to output */
externC void diag_write_char(char c)
{
diag_device_write_char(c);
}
#else
// Initialize after HAL.
static Cyg_dummy_diag_init_class cyg_dummy_diag_init_obj
CYGBLD_ATTRIB_INIT_AFTER(CYG_INIT_HAL);
/*----------------------------------------------------------------------*/
/* Write single char to output */
#define PREFIX()
//#define PREFIX() HAL_DIAG_WRITE_CHAR(0x0F)
externC void diag_write_char(char c)
{
/* Translate LF into CRLF */
if( c == '\n' )
{
PREFIX();
HAL_DIAG_WRITE_CHAR('\r');
}
PREFIX();
HAL_DIAG_WRITE_CHAR(c);
}
#endif
/*----------------------------------------------------------------------*/
/* Initialize. Call to pull in diag initializing constructor */
externC void diag_init(void)
{
}
/*----------------------------------------------------------------------*/
/* Write zero terminated string */
externC void diag_write_string(const char *psz)
{
while( *psz ) diag_write_char( *psz++ );
}
/*----------------------------------------------------------------------*/
/* Write decimal value */
externC void diag_write_dec( cyg_int32 n)
{
cyg_ucount8 sign;
if( n < 0 ) n = -n, sign = '-';
else sign = '+';
diag_write_num( n, 10, sign, false, 0);
}
/*----------------------------------------------------------------------*/
/* Write hexadecimal value */
externC void diag_write_hex( cyg_uint32 n)
{
diag_write_num( n, 16, '+', false, 0);
}
/*----------------------------------------------------------------------*/
/* Generic number writing function */
/* The parameters determine what radix is used, the signed-ness of the */
/* number, its minimum width and whether it is zero or space filled on */
/* the left. */
externC void diag_write_long_num(
cyg_uint64 n, /* number to write */
cyg_ucount8 base, /* radix to write to */
cyg_ucount8 sign, /* sign, '-' if -ve, '+' if +ve */
cyg_bool pfzero, /* prefix with zero ? */
cyg_ucount8 width /* min width of number */
)
{
char buf[32];
cyg_count8 bpos;
char bufinit = pfzero?'0':' ';
char *digits = "0123456789ABCDEF";
/* init buffer to padding char: space or zero */
for( bpos = 0; bpos < (cyg_count8)sizeof(buf); bpos++ ) buf[bpos] = bufinit;
/* Set pos to start */
bpos = 0;
/* construct digits into buffer in reverse order */
if( n == 0 ) buf[bpos++] = '0';
else while( n != 0 )
{
cyg_ucount8 d = n % base;
buf[bpos++] = digits[d];
n /= base;
}
/* set pos to width if less. */
if( (cyg_count8)width > bpos ) bpos = width;
/* set sign if negative. */
if( sign == '-' )
{
if( buf[bpos-1] == bufinit ) bpos--;
buf[bpos] = sign;
}
else bpos--;
/* Now write it out in correct order. */
while( bpos >= 0 )
diag_write_char(buf[bpos--]);
}
externC void diag_write_num(
cyg_uint32 n, /* number to write */
cyg_ucount8 base, /* radix to write to */
cyg_ucount8 sign, /* sign, '-' if -ve, '+' if +ve */
cyg_bool pfzero, /* prefix with zero ? */
cyg_ucount8 width /* min width of number */
)
{
diag_write_long_num((long long)n, base, sign, pfzero, width);
}
/*----------------------------------------------------------------------*/
/* perform some simple sanity checks on a string to ensure that it */
/* consists of printable characters and is of reasonable length. */
static cyg_bool diag_check_string( const char *str )
{
cyg_bool result = true;
const char *s;
if( str == NULL ) return false;
for( s = str ; result && *s ; s++ )
{
char c = *s;
/* Check for a reasonable length string. */
if( s-str > 256 ) result = false;
/* We only really support CR and NL at present. If we want to
* use tabs or other special chars, this test will have to be
* expanded.
*/
if( c == '\n' || c == '\r' )
continue;
/* Check for printable chars. This assumes ASCII */
if( c < ' ' || c > '~' )
result = false;
}
return result;
}
/*----------------------------------------------------------------------*/
externC void diag_vprintf( const char *fmt, CYG_ADDRWORD *args)
{
cyg_bool pad = true;
if( !diag_check_string(fmt) )
{
int i;
diag_write_string("<Bad format string: ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -