/*-------------------------------------------------------------------------*/
/* FILENAME: dsk6xtst.c -- DSK Confidence    Test - HOST Code              */
/*                                                                         */
/*  Rev 2.16   17   July 2000  T.J.Dillon                                  */
/*                                                                         */
/*-------------------------------------------------------------------------*/
/*  HISTORY                                                                */
/*  Rev 1.00   Created                                                     */
/*  Rev 1.10   Mods a) LEDS and CODEC parameter passing                    */
/*                  b) Removed FLASH checksum                              */
/*                  c) Changes for new Win32 DLL API for generic API       */
/*  Rev 1.20   Mods -- Added -pport options                                */
/*  Rev 1.21   Mods -- Reorganize to be consistent with Manufacturing Test */
/*  Rev 1.25   Mods -- Update with latest Win32 DLL API                    */
/*  Rev 1.26   Mods -- Added -pd option                                    */
/*  Rev 1.30   Mods -- Added board revision info                           */
/*  Rev 2.00   Mods -- Update for C6711 DSK                                */
/*  Rev 2.10   Mods -- Update for Larger SDRAMs                            */
/*  Rev 2.15   Mods -- Added -n option                                     */
/*  Rev 2.16   Mods -- Added newer memory test for SDRAM                   */
/*                                                                         */
/*-------------------------------------------------------------------------*/

#include <stdio.h>
#include <windows.h>
#include "dsk6211.h"
#include "dsk6xHSB.h"
#include "dsk6xtst.h"                                           

const ULONG DSPHS_BUFFER_ADDRESS = 0x200; /* Start Int Mem */
static FILE     *pLogFile = NULL;
#define STRING_BUFFER_SIZE  (80)
#define ULONG unsigned long

static void LogPrint( char *format, ULONG parm1, ULONG parm2,
                      ULONG parm3, ULONG parm4, int printflag );
static void PressAnyKey( void );
static void PrintMenu( void );
static int CheckResults( ULONG hSB[] );

BOOL  AllTests=TRUE;  
BOOL  InfoOnly=FALSE;  
BOOL  MenuTests=FALSE;
BOOL  Debug=FALSE;  
BOOL  NoDateOrTime=FALSE;  

/*-------------------------------------------------------------------------*/
/* main()                                                                  */
/*-------------------------------------------------------------------------*/

void main(int argc1, char *argv1[])
{
  unsigned int dip_settings;
  unsigned int bd_rev;

  dskHANDLE hBd;        /* Board device handle                             */
  short  iBd    = 0;    /* Board index                                     */
  BOOL   bExcl  = 1;    /* Exclusive open = TRUE                           */
  short  iMp    = 1;    /* Map selector = MAP1                             */
  char coffNam[20]= "CnfDSP.out";
                        /* COFF file name                                  */
  char *sLogFile;
  char stringBuffer[STRING_BUFFER_SIZE];
  BOOL bVerbose = 0;    /* COFF load verbose mode = FALSE                  */
  BOOL bClr     = 0;    /* Clear bss mode = FALSE                          */
  BOOL bDump    = 0;    /* Dump mode = FALSE                               */

  char YesOrNo[3]="\0";                                           
  ULONG handShakingBuffer[HS_BUFFER_LEN], readLength, writeLength;
  ULONG nPort=0xa;
  int handShakingFlag, dspProcFlag;                                
  int choice,i,error_count=0;
  int numblink,ledperiod;/* User params for LEDS test in menu+debug mode   */                                                      
  ULONG command;         /* Command variable to run each test on DSP       */
  ULONG writevalue,startaddr,sizemem;
                         /* User params for PGMEM test in menu+debug mode  */
  sLogFile = DefaultLog;  /* Default name for log file "dsk6xtst.log"      */

  /*-----------------------------------------------------------------------*/
  /* Parse Command Line                                                    */
  /*-----------------------------------------------------------------------*/
  for(i=1; argc1 > i; i++)
  {
    if( argv1[i][0] != '-' )
    {
      sLogFile = argv1[i];
    }
    else
    {
      switch(tolower(argv1[i][1]))
      {
        case 'h':
        case '?':
          printf( "Syntax: %s [-?] [-u] [-d] [logfilename]\n",TestName );
          printf( "\n");
          printf( "  -?,h   : Displays command line help info.\n");
          printf( "  -pport : Specifies board to test (a,b,c or d).\n");
          printf( "  -u     : Execute individual tests via menu.\n");
          printf( "  -d     : Debug mode.\n");
          printf( "  -n     : Turns off Date and Time.\n");
          printf("\nTMS320C6211/6711 DSK %s Test, ",TestType);
          printf("Version 2.16,  Jul 2000");
          printf("\nCopyright (c) 2000 ");
          printf("by Texas Instruments Incorporated. ");
          printf("\nAll rights reserved.\n\n");
          InfoOnly=TRUE;
          AllTests=FALSE;
          break;
        case 'p':
          switch(tolower(argv1[i][2]))
          {
            case 'a':
              nPort=0xa;
              break;
            case 'b':
              nPort=0xb;
              break;
            case 'c':
              nPort=0xc;
              break;
            case 'd':
              nPort=0xd;
              break;
            default:
              nPort=0xa;
              break;
          }
          break;
        case 'u':
          MenuTests=TRUE;
          AllTests=FALSE;
          break;
        case 'd':
          Debug=TRUE;
          break;
        case 'n':
          NoDateOrTime=TRUE;
          break;
        default:
          AllTests=TRUE;
          break;
      }
    }
  }

  if(InfoOnly)
    sLogFile = NULL;

  /*-----------------------------------------------------------------------*/
  /* Open Log File                                                         */
  /*-----------------------------------------------------------------------*/
  if ( sLogFile != NULL )
  {
    pLogFile = fopen( sLogFile, "w" );
    if ( pLogFile == NULL )
    {
      printf( "WARNING: Unable to open log file \"%s\".\n", sLogFile );
      PressAnyKey();
    }
    else
    { 
      if(InfoOnly)
      {  // do nothing  
      }  
      else
      {
        LogPrint("\nTMS320C6211/6711 DSK %s Test, ", (ULONG)TestType,0,0,0,1 );
        LogPrint("Version 2.16,  Jul 2000", 0,0,0,0,1 );
        LogPrint("\nCopyright (c) 2000 ", 0,0,0,0,1 );
        LogPrint("  by  Texas Instruments Incorporated. ", 0,0,0,0,1 );
        LogPrint("\nAll rights reserved.\n", 0,0,0,0,1 );
        if(!NoDateOrTime)
		{
          LogPrint("\nTest Run Time: ", 0,0,0,0,1 );
          GetTimeFormat( LOCALE_SYSTEM_DEFAULT, 0, NULL,
            "hh':'mm':'ss tt", stringBuffer, STRING_BUFFER_SIZE );
          LogPrint( stringBuffer, 0,0,0,0,1 );
          LogPrint( "   ", 0,0,0,0,1 );
          LogPrint("Test Run Date: ", 0,0,0,0,1 );
          GetDateFormat( LOCALE_SYSTEM_DEFAULT, 0, NULL,
            "MMM dd',' yyyy", stringBuffer, STRING_BUFFER_SIZE );
          LogPrint( stringBuffer, 0,0,0,0,1 );
		}
        LogPrint( "\n\n", 0,0,0,0,1 );
      }
    }
  }

  /*-----------------------------------------------------------------------*/
  /* Open a driver connection to a dsk6x board.                            */
  /*-----------------------------------------------------------------------*/
  if ( !dsk6x_open(nPort,&hBd) )
  {
    LogPrint("FAILED: dsk6x_open()!\n", 0,0,0,0,1);
    exit(1);
  }

  /*-----------------------------------------------------------------------*/
  /* Cause a DSP reset.                                                    */
  /*-----------------------------------------------------------------------*/
  if ( !dsk6x_reset_dsp(hBd,0,1) )
  {
    LogPrint("FAILED: dsk6x_reset_dsp()!\n", 0,0,0,0,1);
    exit(2);
  }

  /*-----------------------------------------------------------------------*/
  /* Establish a connection to the HPI of a target board.                  */
  /*-----------------------------------------------------------------------*/
  if ( !dsk6x_hpi_open(hBd))
  {
    LogPrint("FAILED: dsk6x_hpi_open()!\n", 0,0,0,0,1);
    exit(3);
  }

  /*-----------------------------------------------------------------------*/
  /* Read a COFF file and write the data to DSP memory.                    */
  /*-----------------------------------------------------------------------*/
  if (dsk6x_coff_load(hBd,coffNam,bVerbose,bClr,bDump))
  {
    LogPrint("FAILED: dsk6x_coff_load()!\n", 0,0,0,0,1);
    exit(4);
  }

  /*-----------------------------------------------------------------------*/
  /* Generate a DSPINT to start program                                    */
  /*-----------------------------------------------------------------------*/
  if (!dsk6x_hpi_generate_int(hBd))
  {
    LogPrint("FAILED: dsk6x_hpi_generate_int()!\n", 0,0,0,0,1);
    exit(5);
  }

  /*-----------------------------------------------------------------------*/
  /* If AllTests (default) or MenuTests then Handshake with DSP            */
  /*-----------------------------------------------------------------------*/
  if(AllTests || MenuTests)
  {

    /*---------------------------------------------------------------------*/
    /* Handshaking. Keep reading until hand shake is done.                 */
    /*---------------------------------------------------------------------*/
    do
    {
	    readLength = HS_BUFFER_LEN*4; /* 4 bytes for 1 word */

      /*-------------------------------------------------------------------*/
      /* Test if handShakingBuffer actually read (completely)              */
      /*-------------------------------------------------------------------*/
	    if (!dsk6x_hpi_read(hBd, handShakingBuffer, &readLength, 
        DSPHS_BUFFER_ADDRESS) || readLength != HS_BUFFER_LEN*4)
      { /* evm6x_hpi_read() failed */
		    continue; // try again
      }

      /*-------------------------------------------------------------------*/
      /* Test if handShakingBuffer contains correct ID                     */
      /*-------------------------------------------------------------------*/
	    handShakingFlag = 0;  // reset hand shaking flag
	    if(handShakingBuffer[0] != ID_0)
		    handShakingFlag = 1;
	    if(handShakingBuffer[1] != ID_1)
		    handShakingFlag = 1;
	    if(handShakingBuffer[2] != ID_2)
		    handShakingFlag = 1;
    } while (handShakingFlag == 1);

    /*---------------------------------------------------------------------*/
    /* Read dip settings                                                   */
    /*---------------------------------------------------------------------*/
    dip_settings=handShakingBuffer[5]&0x07000000;
    bd_rev=((handShakingBuffer[5])>>29)&0x7;
    LogPrint(" USER_SW3=%d ",((dip_settings&0x4000000)>>26),0,0,0,1);
    LogPrint(" USER_SW2=%d ",((dip_settings&0x2000000)>>25),0,0,0,1);
    LogPrint(" USER_SW1=%d ",((dip_settings&0x1000000)>>24),0,0,0,1);
    LogPrint(" Board Rev=%d\n\n",bd_rev,0,0,0,1);

    /*---------------------------------------------------------------------*/
    /* Send hand shaking ACK to Target(DSP)                                */
    /*---------------------------------------------------------------------*/
    writeLength = 4;
    handShakingBuffer[4] = HOST_RECEIVE_HAND_SHAKING_INFO;
    dsk6x_hpi_write(hBd, &handShakingBuffer[4], &writeLength, 
      DSPHS_BUFFER_ADDRESS+4*4);

  } // End of if(AllTests || MenuTests)

  /*-----------------------------------------------------------------------*/
  /* Choose AllTests or MenuTests                                          */
  /*-----------------------------------------------------------------------*/
  if(AllTests)
  {

    /*---------------------------------------------------------------------*/
    /* Send All commands to DSP (default mode for DSK Confidence Test)     */
    /*---------------------------------------------------------------------*/
    for(command=ISRAM;command<(CODEC+1);command+=0x1000)
    {
      LogPrint("Command=", 0,0,0,0,1);

      if(command == ISRAM)
        LogPrint("ISRAM",0,0,0,0,1);
		  else if(command == SDRAM)
        LogPrint("SDRAM",0,0,0,0,1);
		  else if(command == FLASH)
        LogPrint("FLASH",0,0,0,0,1);
		  else if(command == MCBSP)
        LogPrint("MCBSP",0,0,0,0,1);
		  else if(command == TIMER)
        LogPrint("TIMER",0,0,0,0,1);
		  else if(command == QDMA)
        LogPrint("QDMA.",0,0,0,0,1);
		  else if(command == LEDS)
        LogPrint("LEDS.",0,0,0,0,1);
		  else if(command == CODEC)
        LogPrint("CODEC",0,0,0,0,1);

      if(command == LEDS)
        LogPrint("..Are They Flashing?.......Result=>", 0,0,0,0,1);
      else if(command == CODEC)
        LogPrint("..Is Tone/Music Playing?...Result=>", 0,0,0,0,1);
      else
        LogPrint("...........................Result=>", 0,0,0,0,1);

      /*-------------------------------------------------------------------*/
      /* Send parameters to Target(DSP)                                    */
      /*-------------------------------------------------------------------*/
      if (command == LEDS)
      {
        handShakingBuffer[0]=LEDBlinks;
        handShakingBuffer[1]=LEDPeriod;
        handShakingBuffer[2]=0;
  	    writeLength = 3*4;
  	    dsk6x_hpi_write(hBd, handShakingBuffer, &writeLength, 
          DSPHS_BUFFER_ADDRESS);
      }
      else if (command == CODEC)
      {
        handShakingBuffer[0]=CodecTone;
        handShakingBuffer[1]=CodecCDPlay;
        handShakingBuffer[2]=0;
  	    writeLength = 3*4;
  	    dsk6x_hpi_write(hBd, handShakingBuffer, &writeLength, 
          DSPHS_BUFFER_ADDRESS);
      }

      /*-------------------------------------------------------------------*/
      /* Send command to Target(DSP)                                       */
      /*-------------------------------------------------------------------*/
      writeLength = 4;
      handShakingBuffer[3] = command; 
      dsk6x_hpi_write(hBd, &handShakingBuffer[3], &writeLength, 
        DSPHS_BUFFER_ADDRESS+3*4);

      /*-------------------------------------------------------------------*/
      /* Tell Target(DSP) that Host is ready                               */
      /*-------------------------------------------------------------------*/
      writeLength = 4;
      handShakingBuffer[4] = HOST_STATUS_INPUT_READY; 
      dsk6x_hpi_write(hBd, &handShakingBuffer[4], &writeLength, 
        DSPHS_BUFFER_ADDRESS+4*4);

      dspProcFlag = 1;

      /*-------------------------------------------------------------------*/
      /* Wait for Target(DSP) to tell you it is done!                      */
      /*-------------------------------------------------------------------*/
      do
      {
	      readLength = 4;
	      dsk6x_hpi_read(hBd, &handShakingBuffer[4], &readLength, 
          DSPHS_BUFFER_ADDRESS+4*4);

	      if(handShakingBuffer[4] == DSP_STATUS_OUTPUT_READY)
		    dspProcFlag = 0;  // DSP is done processing

      } while(dspProcFlag);

      /*-------------------------------------------------------------------*/
      /* Check Results                                                     */
      /*-------------------------------------------------------------------*/
	    readLength = 6*4;
	    dsk6x_hpi_read(hBd, handShakingBuffer, &readLength, DSPHS_BUFFER_ADDRESS);
      error_count+=CheckResults(handShakingBuffer);

    } // End of for loop

  } // End of if(AllTests)
  else if(MenuTests)
  {

    /*---------------------------------------------------------------------*/
    /* Display Menu First time                                             */
    /*---------------------------------------------------------------------*/
    PrintMenu();

    /*---------------------------------------------------------------------*/
    /* Do while loop for repeat of choices                                 */
    /*---------------------------------------------------------------------*/
    do
    {

      /*-------------------------------------------------------------------*/
      /* Request Choice 1 to 9 only for CnfTDSP.out                        */
      /*-------------------------------------------------------------------*/
      do
      {
        printf("\nChoice: ");
        scanf("%d", &choice);
        LogPrint("\nChoice: %d\n ", choice,0,0,0,0);

        if(Debug)
        {        
          if(choice == 7)      // LEDS user input
          {
            printf(" Number of blinks, Time period (msec): ");
            scanf("%d,%d", &numblink, &ledperiod);
            LogPrint(" Number of blinks, Time period (msec): %d,%d\n ",
              numblink,ledperiod,0,0,0);
          }
          if(choice == 10)     // PGMEM user input
          {
            printf(" Value(hex), Relative Addr(hex), Size(hex): ");
            scanf("%x,%x,%x", &writevalue, &startaddr, &sizemem);
            LogPrint(" Value(hex), Relative Addr(hex), Size(hex): %#09x,%#09x,%#09x\n ",
              writevalue,startaddr,sizemem,0,0);
          }
        }

        LogPrint("Command=", 0,0,0,0,1);

        if(choice == 1)
        {
			    command = ISRAM;
          LogPrint("ISRAM",0,0,0,0,1);
        }
		    else if(choice == 2)
        {
			    command = SDRAM;
          LogPrint("SDRAM",0,0,0,0,1);
        }
		    else if(choice == 3)
        {
    			command = FLASH;
			    LogPrint("FLASH", 0,0,0,0,1);
        }
		    else if(choice == 4)
        {
			    command = MCBSP;
          LogPrint("MCBSP",0,0,0,0,1);
        }
		    else if(choice == 5)
        {
			    command = TIMER;
          LogPrint("TIMER",0,0,0,0,1);
        }
		    else if(choice == 6)
        {
			    command = QDMA;
          LogPrint("QDMA.",0,0,0,0,1);
        }
		    else if(choice == 7)
        {
			    command = LEDS;
          LogPrint("LEDS.",0,0,0,0,1);
        }
		    else if(choice == 8)
        {
			    command = CODEC;
          LogPrint("CODEC",0,0,0,0,1);
        }
		    else if(choice == 9)
        {
			    command = MENU;
          LogPrint("MENU\n",0,0,0,0,1);
          PrintMenu();
        }
		    else if((choice == 10) && Debug)
        {
			    command = PGMEM;
          LogPrint("PGMEM",0,0,0,0,1);
        }
		    else 
          if(Debug)
            LogPrint("\nERROR! Only choices are 1-10 (9 for menu)!\n", 
              0,0,0,0,1);
          else
            LogPrint("\nERROR! Only choices are 1-9 (9 for menu)!\n", 
              0,0,0,0,1);

        if(command == LEDS)
        {
          if(Debug)
          {
            handShakingBuffer[0]=numblink;
            handShakingBuffer[1]=ledperiod;
            handShakingBuffer[2]=((~(dip_settings>>24)+0x8)<<24)&0xF000000;
          }
          else
          {
            handShakingBuffer[0]=LEDBlinks;
            handShakingBuffer[1]=LEDPeriod;
            handShakingBuffer[2]=0;
          }
          LogPrint("..Are They Flashing?.......Result=>", 0,0,0,0,1);
        }
        else if(command == CODEC)
        {
          handShakingBuffer[0]=CodecTone;
          handShakingBuffer[1]=CodecCDPlay;
          handShakingBuffer[2]=0;
          LogPrint("..Is Tone/Music Playing?...Result=>", 0,0,0,0,1);
        }
        else if(command == MENU)
          LogPrint("\n", 0,0,0,0,1);
        else if(command == PGMEM)
        {
          handShakingBuffer[0]=writevalue;
          handShakingBuffer[1]=startaddr;
          handShakingBuffer[2]=sizemem;
          LogPrint("..(SDRAM)..................Result=>", 0,0,0,0,1);
        }
        else
          LogPrint("...........................Result=>", 0,0,0,0,1);

      } while(choice<1 || choice>10 || choice==9);

      /*-------------------------------------------------------------------*/
      /* Send parameters to Target(DSP)                                    */
      /*-------------------------------------------------------------------*/
      if ((command == LEDS)||(command == CODEC)||(command == PGMEM))
      {
  	    writeLength = 3*4;
  	    dsk6x_hpi_write(hBd, handShakingBuffer, &writeLength, 
          DSPHS_BUFFER_ADDRESS);
      }

      /*-------------------------------------------------------------------*/
      /* Send command to Target(DSP)                                       */
      /*-------------------------------------------------------------------*/
      writeLength = 4;
      handShakingBuffer[3] = command; 
      dsk6x_hpi_write(hBd, &handShakingBuffer[3], &writeLength, 
        DSPHS_BUFFER_ADDRESS+3*4);

      /*-------------------------------------------------------------------*/
      /* Tell Target(DSP) that Host is ready                               */
      /*-------------------------------------------------------------------*/
      writeLength = 4;
      handShakingBuffer[4] = HOST_STATUS_INPUT_READY; 
      dsk6x_hpi_write(hBd, &handShakingBuffer[4], &writeLength, 
        DSPHS_BUFFER_ADDRESS+4*4);

      dspProcFlag = 1;

      /*-------------------------------------------------------------------*/
      /* Wait for Target(DSP) to tell you it is done!                      */
      /*-------------------------------------------------------------------*/
      do
      {
	      readLength = 4;
	      dsk6x_hpi_read(hBd, &handShakingBuffer[4], &readLength, 
          DSPHS_BUFFER_ADDRESS+4*4);

	      if(handShakingBuffer[4] == DSP_STATUS_OUTPUT_READY)
		      dspProcFlag = 0;  // DSP is done processing

      } while(dspProcFlag);

      /*-------------------------------------------------------------------*/
      /* Check Result                                                      */
      /*-------------------------------------------------------------------*/
	    readLength = 6*4;
	    dsk6x_hpi_read(hBd, handShakingBuffer, &readLength, DSPHS_BUFFER_ADDRESS);
      error_count+=CheckResults(handShakingBuffer);

      /*-------------------------------------------------------------------*/
      /* Request Yes/No for repeat processing                              */
      /*-------------------------------------------------------------------*/
      printf("\nDo you want to make another Choice? (Yes/No): ");
      scanf("%s", YesOrNo);
      LogPrint("\nDo you want to make another Choice? (Yes/No): %s\n", 
        (ULONG)YesOrNo,0,0,0,0);

    /*---------------------------------------------------------------------*/
    /* Test for yes answer to repeat processing                            */
    /*---------------------------------------------------------------------*/
    } while (!strncmp("Yes",YesOrNo,1) || !strncmp("yes",YesOrNo,1));

  } // End of if(MenuTests) choice
  else if(InfoOnly)
  {
    // don't send commands to DSP
  }

  /*-----------------------------------------------------------------------*/
  /* Tell Target(DSP) that HOST is done!                                   */
  /*-----------------------------------------------------------------------*/
  writeLength = 4;
  handShakingBuffer[4] = HOST_STATUS_END_PROCESSING; 
  dsk6x_hpi_write(hBd, &handShakingBuffer[4], &writeLength, 
    DSPHS_BUFFER_ADDRESS+4*4);

  /*-----------------------------------------------------------------------*/
  /* Close the new HPI session started with evm6x_hpi_open()               */
  /*-----------------------------------------------------------------------*/
  if (!dsk6x_hpi_close(hBd))
  {
    LogPrint("FAILED: dsk6x_hpi_close()!\n", 0,0,0,0,1);
    exit(6);
  }

  /*-----------------------------------------------------------------------*/
  /* Close a previously opened driver connection to a board.               */
  /*-----------------------------------------------------------------------*/
  if (!dsk6x_close(hBd))
  {
    LogPrint("FAILED: dsk6x_close()!\n", 0,0,0,0,1);
    exit(7);
  }

  /*-----------------------------------------------------------------------*/
  /* Cleanup                                                               */
  /*-----------------------------------------------------------------------*/
  if(InfoOnly)
  { 
    // don't write into log file
  }
  else
  {
    if(error_count)
    {
      LogPrint( "\nTMS320C6211/6711 DSK %s Test FAILED!", (ULONG)TestType,0,0,0,1);
      LogPrint( "\n ... Found %d test failure(s)!\n", error_count,0,0,0,1);
    }
    else
    {
      LogPrint( "\nTMS320C6211/6711 DSK %s Test PASSED!", (ULONG)TestType,0,0,0,1);
      LogPrint( "\n ... No test failures found!\n", 0,0,0,0,1);
    }

    LogPrint( "\n\nClosing Log File: %s\n", (ULONG)sLogFile,0,0,0,1);
    LogPrint( "\n** TMS320C6211/6711 DSK %s Test Complete! **\n", (ULONG)TestType,0,0,0,1);
  }

  if(pLogFile != NULL)
  {
    fclose(pLogFile);
  }

  exit(0);

} /* end of main() */

/*-------------------------------------------------------------------------*/
/* LogPrint() - used to print to a log file and stdout (if printflag=1)    */
/*-------------------------------------------------------------------------*/
static void LogPrint( char *format, ULONG parm1, ULONG parm2,
                      ULONG parm3, ULONG parm4, int printflag )
{
    if ( pLogFile != NULL )
    {
        fprintf( pLogFile, format, parm1, parm2, parm3, parm4 );
    }

    if (printflag)
      printf( format, parm1, parm2, parm3, parm4 );
}

/*-------------------------------------------------------------------------*/
/* PressAnyKey() - used to stall and show user error message on log file   */
/*-------------------------------------------------------------------------*/
static void PressAnyKey( void )
{
    int     c;
	printf( "Press any key to continue (cntrl-C to abort)." );
	c = getchar();
    printf( "\n" );
    if ( c == 0x03 ) { exit( -1 ); }
    return;
} /* end of PressAnyKey() */

/*-------------------------------------------------------------------------*/
/* PrintMenu() - used to print list of tests in menu mode                  */
/*-------------------------------------------------------------------------*/
static void PrintMenu( void )
{
    LogPrint("\nMake a numeric choice from the following list: \n", 0,0,0,0,1);
    LogPrint("\n", 0,0,0,0,1);
    LogPrint("\t1) Internal SRAM test \n", 0,0,0,0,1);
    LogPrint("\t2) External SDRAM test \n", 0,0,0,0,1);
    LogPrint("\t3) External FLASH test \n", 0,0,0,0,1);
    LogPrint("\t4) McBSP Loopback test \n", 0,0,0,0,1);
    LogPrint("\t5) Timer test \n", 0,0,0,0,1);
    LogPrint("\t6) QDMA test \n", 0,0,0,0,1);
    LogPrint("\t7) LED test \n", 0,0,0,0,1);
    LogPrint("\t8) CODEC test \n", 0,0,0,0,1);
    LogPrint("\n\t9) Display this menu \n", 0,0,0,0,1);
    if(Debug)
      LogPrint("\n\t10) PGMEM test (SDRAM) \n", 0,0,0,0,1);
}

/*-------------------------------------------------------------------------*/
/* CheckResults() - decodes returned values (ie. error conditions) from DSP*/
/*-------------------------------------------------------------------------*/
static int CheckResults( ULONG hSB[] )
{
  int error=0;

  switch(hSB[5])
  {
    /*---------------------------------------------------------------------*/
    /* Decode test results OK                                              */
    /*---------------------------------------------------------------------*/
    case ISRAM_OK:
    case SDRAM_OK:
    case MCBSP_OK:
    case TIMER_OK:
    case QDMA_OK:
    case LEDS_OK:
    case CODEC_OK:
      LogPrint(" PASSED!\n", 0,
        0,0,0,1);
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for internal SRAM test errors                   */
    /*---------------------------------------------------------------------*/
    case ISRAM_ERR55:                    /* Write 55 error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0x55 to multiple locations in ISRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total 55 errors=%#09x Total Locations=%#09x\n",
          hSB[0],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
    case ISRAM_ERRAA:                    /* Write AA error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0xAA to multiple locations in ISRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total AA errors=%#09x Total Locations=%#09x\n",
          hSB[1],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
    case ISRAM_ERROR:                    /* Write 55+Write AA error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0x55 to multiple locations in ISRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total 55 errors=%#09x Total Locations=%#09x\n",
          hSB[0],hSB[2],0,0,1);
        LogPrint(" Attempted to write 0xAA to multiple locations in ISRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total AA errors=%#09x Total Locations=%#09x\n",
          hSB[1],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for external SDRAM test errors                  */
    /*---------------------------------------------------------------------*/
    case SDRAM_ERR55:                    /* Write 55 error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0x55 to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total 55 errors=%#09x Total Locations=%#09x\n",
          hSB[0],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
    case SDRAM_ERR5A:                    /* Write 5A error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0x5A to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total 5A errors=%#09x Total Locations=%#09x\n",
          hSB[0],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
    case SDRAM_ERRAA:                    /* Write AA error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0xAA to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total AA errors=%#09x Total Locations=%#09x\n",
          hSB[1],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
    case SDRAM_ERRA5:                    /* Write A5 error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0xA5 to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total A5 errors=%#09x Total Locations=%#09x\n",
          hSB[1],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
/* OLD version of SDRAM_ERROR (earlier than 2.16)
/*    case SDRAM_ERROR:                    * Write 55+Write AA error *
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0x55 to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total 55 errors=%#09x Total Locations=%#09x\n",
          hSB[0],hSB[2],0,0,1);
        LogPrint(" Attempted to write 0xAA to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total AA errors=%#09x Total Locations=%#09x\n",
          hSB[1],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;*/
    case SDRAM_ERROR:                    /* Write 5A+Write A5 error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0x5A to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total 5A errors=%#09x Total Locations=%#09x\n",
          hSB[0],hSB[2],0,0,1);
        LogPrint(" Attempted to write 0xA5 to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total A5 errors=%#09x Total Locations=%#09x\n",
          hSB[1],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for FLASH test                                  */
    /*---------------------------------------------------------------------*/
    case FLASH_OK:                       /* No Error */
      LogPrint(" PASSED!\n", 0,
        0,0,0,1);
/*      LogPrint(" Checksum=%#09x\n",hSB[2],0,0,0,1); */
/*      LogPrint("\n", 0,0,0,0,1); */
      break;
/*    case FLASH_ERRCK:                     Checksum error 
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Compared a computed checksum to one read from the flash\n", 0,
          0,0,0,1);
        LogPrint(" Expected Checksum=%#09x Computed Checksum=%#09x\n",
          hSB[1],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;*/
    case FLASH_ERR55:                    /* Write 55 error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0x55 to multiple locations in FLASH\n", 0,
          0,0,0,1);
        LogPrint(" Total 55 errors=%#09x\n",
          hSB[0],0,0,0,1);
/*        LogPrint(" Expected Checksum=%#09x Computed Checksum=%#09x\n",
          hSB[1],hSB[2],0,0,1);*/
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
/*    case FLASH_ERR77:                     Checksum+Write 55 error 
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0x55 to multiple locations in FLASH\n", 0,
          0,0,0,1);
        LogPrint(" Total 55 errors=%#09x\n",
          hSB[0],0,0,0,1);
        LogPrint(" Compared a computed checksum to one read from the flash\n", 0,
          0,0,0,1);
        LogPrint(" Expected Checksum=%#09x Computed Checksum=%#09x\n",
          hSB[1],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;*/
    case FLASH_ERRAA:                    /* Write AA error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0xAA to multiple locations in FLASH\n", 0,
          0,0,0,1);
        LogPrint(" Total AA errors=%#09x\n",
          hSB[0],0,0,0,1);
/*        LogPrint(" Expected Checksum=%#09x Computed Checksum=%#09x\n",
          hSB[1],hSB[2],0,0,1);*/
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
/*    case FLASH_ERRBB:                     Checksum+Write AA error 
      LogPrint(" FAIL! checksum+flash_test(AA) of FLASH\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0xAA to multiple locations in FLASH\n", 0,
          0,0,0,1);
        LogPrint(" Total AA errors=%#09x\n",
          hSB[0],0,0,0,1);
        LogPrint(" Compared a computed checksum to one read from the flash\n", 0,
          0,0,0,1);
        LogPrint(" Expected Checksum=%#09x Computed Checksum=%#09x\n",
          hSB[1],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;*/
    case FLASH_ERROR:                    /* Write 55+Write AA error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write 0x55 to multiple locations in FLASH\n", 0,
          0,0,0,1);
        LogPrint(" Total 55 errors=%#09x\n",
          hSB[0],0,0,0,1);
        LogPrint(" Attempted to write 0xAA to multiple locations in FLASH\n", 0,
          0,0,0,1);
        LogPrint(" Total AA errors=%#09x\n",
          hSB[0],0,0,0,1);
/*        if(hSB[1]!=hSB[2])                     +Checksum error 
        {
          LogPrint(" Compared a computed checksum to one read from the flash\n", 0,
            0,0,0,1);
          LogPrint(" Expected Checksum=%#09x Computed Checksum=%#09x\n",
            hSB[1],hSB[2],0,0,1);
        }*/
        LogPrint("\n", 0,0,0,0,1);
      }
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for MCBSP test error                            */
    /*---------------------------------------------------------------------*/
    case MCBSP_ERROR:                    /* McBSP0 error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to test digital loopback mode with MCBSP\n", 0,
          0,0,0,1);
        LogPrint(" Error return value=%#09x\n",
          hSB[0],0,0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for TIMER test error                            */
    /*---------------------------------------------------------------------*/
    case TIMER_ERROR:                    /* TIMER0 error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to use TIMER\n", 0,
          0,0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for QDMA test error                             */
    /*---------------------------------------------------------------------*/
    case QDMA_ERROR:                    /* QDMA error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to use QDMA\n", 0,
          0,0,0,1);
        LogPrint(" Error return value=%#09x\n",
          hSB[0],0,0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for LEDS test error                             */
    /*---------------------------------------------------------------------*/
    case LEDS_ERROR:                    /* LEDS error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to flash LEDS\n", 0,
          0,0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for CODEC test error                            */
    /*---------------------------------------------------------------------*/
    case CODEC_ERRTN:                    /* Tone test error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to play tone on CODEC\n", 0,
          0,0,0,1);
        LogPrint(" Error in setup of Control Reg %d\n", 0,
          hSB[0],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
    case CODEC_ERRCD:                    /* Play CD error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to play CD/MIC input on CODEC\n", 0,
          0,0,0,1);
        LogPrint(" Error in setup of Control Reg %d\n", 0,
          hSB[1],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
    case CODEC_ERROR:                    /* Tone+Play CD error */
      LogPrint(" FAILED!\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to play tone on CODEC\n", 0,
          0,0,0,1);
        LogPrint(" Error in setup of Control Reg %d\n", 0,
          hSB[0],0,0,1);
        LogPrint(" Attempted to play CD/MIC input on CODEC\n", 0,
          0,0,0,1);
        LogPrint(" Error in setup of Control Reg %d\n", 0,
          hSB[1],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for PGMEM test error                            */
    /*---------------------------------------------------------------------*/
    case PGMEM_OK:                    /* No error - Report result */
      LogPrint(" PASSED!\n", 0,
        0,0,0,1);
      if(Debug)
      {
        LogPrint(" Successful write of a value to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Value Written=%#09x First Address=%#09x\n",
          hSB[0],hSB[1],0,0,1);
        LogPrint(" Total Locations=%#09x\n",
          hSB[2],0,0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;
    case PGMEM_ERROR:                    /* PGMEM Write error */
      LogPrint(" FAIL! PGMEM test\n", 0,
        0,0,0,1);
      error++;
      if(Debug && error)
      {
        LogPrint(" Attempted to write a value to multiple locations in SDRAM\n", 0,
          0,0,0,1);
        LogPrint(" Total errors=%#09x Total locations=%#09x\n",
          hSB[0],hSB[2],0,0,1);
        LogPrint("\n", 0,0,0,0,1);
      }
      break;

    /*---------------------------------------------------------------------*/
    /* Decode test results for other cases                                 */
    /*---------------------------------------------------------------------*/
    case DEFAULT_ERROR:                    /* DSP doesn't have this test */
      LogPrint(" Unknown command sent to DSP!\n", 0,0,0,0,1);
      LogPrint("\n", 0,0,0,0,1);
      break;
    case TEST_DISABLED:                    /* DSP returned TEST_DISABLED */
      LogPrint(" Test Disabled on DSP!\n", 0,0,0,0,1);
      LogPrint("\n", 0,0,0,0,1);
      break;
    default:                    /* DSP returned a zero code */
      LogPrint(" No Error Code Returned!!!\n", 0,0,0,0,1);
      LogPrint("\n", 0,0,0,0,1);
      break;
  }

  return(error);
}