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

📄 testtool.c

📁 sttesttool.rar的标准testtool.rar驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
/*******************************************************************************

File name   : testtool.c

Description : Testtool module standard API functions source file

COPYRIGHT (C) STMicroelectronics 2005.

Date               Modification                                     Name
----               ------------                                     ----
05 oct 1998        Ported to 5510                                    FR
09 mar 1999        testtool.h & debug.h removed                      FQ
                   hardware.h & uart.h & flashdrv.h removed
                   readkey(), io_xxx(), pollkeyxxx() moved to report.c
02 apr 1999        sttbx used instead of report                      FQ
20 jul 1999        STTBX_Init() & Term() moved to startup.c          FQ
06 sep 1999        Remove os20.h & symbol.h, add init. param.        FQ
28 dec 1999        Remove display_byte, modif. display & search      FQ
24 jan 2000        Change to STAPI style & removal of unused code    FQ
31 aou 2000        Create STTST_SetMode();                           FQ
02 jul 2001        Modified delay calculation in wait_time           CL
02 Oct 2001        Decrease stack use.                               HSdLM
17 Oct 2001        Force 32 bits aligned accesses for ST40           HSdLM
28 Nov 2001        Fix pb in display (ascii chars)                   HSdLM
*******************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

/* Includes --------------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "stcommon.h"
#include "clilib.h"
#ifndef STTBX_INPUT
#define STTBX_INPUT
#endif

#ifdef ST_OSLINUX
#ifndef NO_STAPIGAT
#include "iocstapigat.h"
#endif
#else
#ifndef STTBX_PRINT
    #define STTBX_PRINT
#endif
#ifndef STTBX_REPORT
    #define STTBX_REPORT
#endif
#include "sttbx.h"
#endif

#ifdef ST_OS21
#define FORCE_32BITS_ALIGN
#endif

/* Private Types ------------------------------------------------------------ */

/* Private Constants -------------------------------------------------------- */

/* Private Variables (static)------------------------------------------------ */

static long SearchBase = 0;   /* used by FindString() & FindNextString() */
static long SearchRange = 0;
static long SearchOccurNb = 0;
static char SearchString[CLI_MAX_LINE_LENGTH];
static S16  SearchLen;
static char PrintBuffer[CLI_MAX_LINE_LENGTH];

/* Global Variables --------------------------------------------------------- */

STTST_InitParams_t sttst_InitParams;
S16 STTST_RunMode;       /* interactive or batch mode */

/* Private Macros ----------------------------------------------------------- */

#ifdef FORCE_32BITS_ALIGN
#define GETBYTE1(v) (U8)((v)&0xFF)
#define GETBYTE2(v) (U8)(((v)&0xFF00)>>8)
#define GETBYTE3(v) (U8)(((v)&0xFF0000)>>16)
#define GETBYTE4(v) (U8)(((v)&0xFF000000)>>24)
#endif

/* Private Function prototypes ---------------------------------------------- */

/* Functions ---------------------------------------------------------------- */

/*extern BOOL io_zap (STTST_Parse_t *pars_p, char *result_sym_p);*/


/****************************************************************************/
/*********************** COMMANDS FOR MEMORY ACCESS *************************/
/****************************************************************************/

/* ========================================================================
testtool command : display memory in 32bits style
=========================================================================== */
static BOOL DisplayMemory(STTST_Parse_t *pars_p, char *result_sym_p)
{
    BOOL  IsBad;
    long  cnt;
    int   i;
    long  base;
    long  length;
    long  def_base;
    long  def_range;
    long  address;
    char  filename[FILENAME_MAX];
    FILE* dumpfile=NULL;
    char  options[40];
#ifdef FORCE_32BITS_ALIGN
    volatile long *pt;
#else
    char  *pt;
#endif

    /* get default values from symbol variables */
    STTST_EvaluateInteger("BASEADDRESS", (S32 *)&def_base, (S16) 10);
    STTST_EvaluateInteger("RANGE", (S32 *) &def_range, 10);

    IsBad = STTST_GetInteger(pars_p, (S32) def_base, (S32 *)&base );
    if (IsBad)
    {
        sprintf( PrintBuffer, "Invalid base address (default is BASEADDRESS=h%08lx)", def_base );
        STTST_TagCurrentLine(pars_p, PrintBuffer);
    }
    else
    {
        IsBad = (STTST_GetInteger(pars_p, (S32)def_range, (S32 *)&length) || (length <= 0));
        if (IsBad)
        {
            sprintf( PrintBuffer, "Invalid length (default is RANGE=h%03lx)", def_range );
            STTST_TagCurrentLine(pars_p, PrintBuffer);
        }
        else
        {
            IsBad = STTST_GetString(pars_p, "", filename, FILENAME_MAX);
            if ( IsBad )
            {
                STTST_TagCurrentLine(pars_p, "Illegal file name");
            }
            else
            {
                memset( options, 0, sizeof(options) );
                IsBad = STTST_GetString(pars_p, "w", options, 4);
                if ( IsBad || ( options[0]!='W' && options[0]!='w' && options[0]!='A' && options[0]!='a') )
                {
                    STTST_TagCurrentLine(pars_p, "Illegal option (W=write, A=append)");
                    IsBad = TRUE;
                }
                else
                {
                    if ( options[0]== 'W' )
                    {
                        options[0] = 'w';
                    }
                    if ( options[0]== 'A' )
                    {
                        options[0] = 'a';
                    }
                    if ( strcmp(filename,"")!=0 )
                    {
                        dumpfile = fopen(filename, options);
                        if (dumpfile == NULL)
                        {
                            STTBX_Print(("The dump file [%s] cannot be opened !\n",filename));
                            IsBad = TRUE;
                        }
                    }
                }
            }
        }
    }
    if ( !IsBad )
    {
        /* Do a formatted display, address and four values a line */
        /* Watch for a bus error if we stray out of legal memory  */
        address = base;
        cnt = 0;
        while ((cnt <= length))
        {
#ifdef FORCE_32BITS_ALIGN
            pt = (volatile long *)(address & 0xFFFFFFFC);
            sprintf( PrintBuffer,
                    "h%08lx: %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x",
                    address,
                    GETBYTE1(*pt)    , GETBYTE2(*pt)    , GETBYTE3(*pt)    , GETBYTE4(*pt),
                    GETBYTE1(*(pt+1)), GETBYTE2(*(pt+1)), GETBYTE3(*(pt+1)), GETBYTE4(*(pt+1)),
                    GETBYTE1(*(pt+2)), GETBYTE2(*(pt+2)), GETBYTE3(*(pt+2)), GETBYTE4(*(pt+2)),
                    GETBYTE1(*(pt+3)), GETBYTE2(*(pt+3)), GETBYTE3(*(pt+3)), GETBYTE4(*(pt+3)));
#else
            pt = (char *)address;
            sprintf( PrintBuffer,
                    "h%08lx: %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x",
                    address,
                    *pt     , *(pt+1) , *(pt+2) , *(pt+3),
                    *(pt+4) , *(pt+5) , *(pt+6) , *(pt+7),
                    *(pt+8) , *(pt+9) , *(pt+10), *(pt+11),
                    *(pt+12), *(pt+13), *(pt+14), *(pt+15) );

#endif /* #ifdef FORCE_32BITS_ALIGN */
            if ( strcmp(filename,"")!=0 )
            {
                fprintf(dumpfile, "%s", PrintBuffer);
            }
            else
            {
                STTBX_Print(("  %s", PrintBuffer));
            }
#ifdef FORCE_32BITS_ALIGN
            for (i=0;i<4;i++)
            {
                PrintBuffer[4*i]   = GETBYTE1(*(pt+i));
                PrintBuffer[4*i+1] = GETBYTE2(*(pt+i));
                PrintBuffer[4*i+2] = GETBYTE3(*(pt+i));
                PrintBuffer[4*i+3] = GETBYTE4(*(pt+i));
            }
            for (i=0;i<16;i++)
            {
                if (isprint(PrintBuffer[i]) == 0)
                {
                    PrintBuffer[i] = '.';
                }
            }
#else
            for (i=0;i<16;i++)
            {
                PrintBuffer[i] = (U8)*(pt+i);
                if (isprint((int)PrintBuffer[i]) == 0)
                {
                    PrintBuffer[i] = '.';
                }
            }
#endif /* #ifdef FORCE_32BITS_ALIGN */
            PrintBuffer[16] = '\0';
            if ( strcmp(filename,"")!=0 )
            {
                fprintf(dumpfile, "  %s\n", PrintBuffer);
            }
            else
            {
                STTBX_Print(("  %s\n", PrintBuffer));
            }
            /* protect ourselves from address rollover */
            if (address >= 0x7ffffff0)
            {
                address = (long)0x80000000;
            }
            else
            {
                address += 16;
            }
            cnt +=16;
        }
        if ( strcmp(filename,"")!=0 )
        {
            STTBX_Print(("Dump stored in [%s] file\n",filename));
            fclose(dumpfile);
        }
    }
    IsBad |= STTST_AssignInteger(result_sym_p, (S32)base, FALSE) ;

    return(IsBad);
}

/* ========================================================================
testtool command : finds string in range of memory (created by FQ, Jan 99)
=========================================================================== */
static BOOL do_find(char *string_p, long *base_p, long range_p, long max_occur_p,
                S16 len_p, char *result_sym_p)
{
    BOOL  IsBad;
    char  *pt;
    long  match =0;
    S16   cpt;
    U32   length;

    length = (U32)len_p;
    IsBad = TRUE; /* not found */
    pt = (char *)(*base_p) ;
    cpt = 0;
    while ( ( pt < (char *)(*base_p+range_p) ) && ( cpt < max_occur_p ) )
    {
        if (*pt == string_p[0])
        {
            if ( memcmp(pt, string_p, len_p) == 0 )
            {
                IsBad = FALSE;
                match = (long)pt;
                /*STTBX_Print(("Match found at #%lx\n", match));*/
                STTBX_Print(("Match found at h%lx\n", match));
                if ( cpt==0 )
                {
                    IsBad = STTST_AssignInteger(result_sym_p, (S32)match, FALSE);
                }
                pt = pt + length;
                cpt++;
            }
            else
            {
                pt++;
            }
        }
        else
        {
            pt++;
        }
    } /* end while */
    if ( cpt > 0 )
    {
        *base_p = match+len_p; /* base address for the next search */
    }
    if ( IsBad || (cpt==0) )
    {
    IsBad = STTST_AssignInteger(result_sym_p, (S32)0, FALSE);
    }
    return(IsBad);
}

/* ========================================================================
testtool command : finds string in range of memory (modif. by FQ, Jan 99)
=========================================================================== */
static BOOL FindString(STTST_Parse_t *pars_p, char *result_sym_p)
{
    BOOL IsBad;
    long def_base;
    long def_range;
    long def_data;
    char temp[4];
    char *pt;
    long match;
    long lval;
    BOOL not_a_string =FALSE;
    S16  cpt;

    /* get default values from symbol variables */
    STTST_EvaluateInteger("BASEADDRESS", (S32 *)&def_base, 10);
    STTST_EvaluateInteger("RANGE", (S32 *)&def_range, 10);
    STTST_EvaluateInteger("DATAVALUE", (S32 *)&def_data, 10);
    match = 0;

    /* get the data item (string or integer) */
    IsBad = STTST_GetItem(pars_p, "", SearchString, CLI_MAX_LINE_LENGTH);
    if ( IsBad )
    {
        STTST_TagCurrentLine(pars_p, "Illegal search item");
    }
    else
    {
        STTST_GetTokenCount( pars_p, &cpt );
        if ( cpt == 1 )
        {
            SearchString[0] = (char)def_data;
            not_a_string = TRUE;
            SearchLen = 1;
        }
        else
        {
            /* is it an integer value ? */
            IsBad = STTST_EvaluateInteger(pars_p->token, (S32 *)&lval, 10);
            if ( IsBad)
            {
                /* is it a string ? */
                IsBad = STTST_EvaluateString(pars_p->token, SearchString, CLI_MAX_LINE_LENGTH);
                if ( IsBad || strlen(SearchString) == 0 )
                {
                    STTST_TagCurrentLine(pars_p, "Illegal search string");
                    IsBad = TRUE;
                    SearchLen = 0;
                }
                else
                {
                    not_a_string = FALSE;
                    SearchLen = strlen(SearchString);
                }
            }
            else
            {
                not_a_string = TRUE;
                SearchLen = 4;
                /* is it an hexadecimal value ? */
                if ( pars_p->token[0]=='#' || pars_p->token[0]=='h' )
                {
                    SearchLen = pars_p->tok_len/2 ;
                    if ( SearchLen > 4 )
                    {
                        IsBad = TRUE;
                        STTST_TagCurrentLine(pars_p, "Illegal search value (max. is 4 bytes for an hexadecimal value)");
                    }
                }
                /* Be carefull with endianess on long value with DCU ST20.                */
                /* ex: sea #12345678 --> lval is #78563412 --> string should be #12345678 */
                /* ex: sea #1234     --> lval is #34120000 --> string should be #12340000 */
                pt = (char *)&lval;
                temp[0] = pt[3];
                temp[1] = pt[2];
                temp[2] = pt[1];
                temp[3] = pt[0];
                pt = temp + ( 4 - SearchLen ) ; /* skip the zeros */
                memcpy( SearchString, pt, SearchLen );
            }
        } /* end if cpt */
    } /* end if item ok */

    if (!IsBad)
    {
        /* get start address */
        IsBad = STTST_GetInteger(pars_p, (S32)def_base, (S32 *)&SearchBase);
        if (IsBad)
        {

⌨️ 快捷键说明

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