/*-------------------------------------------------------------------------*/
/* FILENAME: cnfdsp.c -- DSK Confidence test - DSP Code                    */
/*                                                                         */
/*  Rev 2.15   17 July 2000  T.J.Dillon - SDRAM update, C6711 update       */
/*                                                                         */
/*-------------------------------------------------------------------------*/

#include <stdio.h>
#include <csl.h>
#include <csl_chip.h>
#include <csl_edma.h>
#include <csl_emif.h>
#include <csl_irq.h>
#include <csl_mcbsp.h>
#include <csl_timer.h>
#include <bsl.h>
#include <bsl_board.h>
#include <bsl_dip.h>
#include <bsl_ad535.h>
#include "cnfDSP.h"
#include "dsk6xHSB.h"                                     

#pragma DATA_SECTION(handShakingBuffer, "my_DataSect")
int handShakingBuffer[HS_BUFFER_LEN];

/*******************************************************************************
 * Global variables                                                            *
 *******************************************************************************/
TIMER_Handle hTimer;
TIMER_Config myTimerConfig = {
                   0x000003F0,    /* CTL register - CPU CLK/4        */
                   0x00000500,    /* PRD register                    */
                   0x00000000     /* CNT register                    */
                   };
MCBSP_Handle hMcbsp;
AD535_Handle hAD535;
AD535_Config myConfig = { AD535_LOOPBACK_DISABLE,
                          AD535_MICGAIN_OFF,
                          AD535_GAIN_0DB,
                          AD535_GAIN_0DB
                        };

short sin_table[] = {0, 23170>>2, 32767>>2, 23170>>2,0, -23170>>2, -32767>>2, -23170>>2}; 
int flag;
//static int exp_chksum,cmp_chksum;

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

int main()
{
  int src,dst,size; /* Used for QDMA */
  int error,errortone;
  Uint32 io_port_values;
  Uint32 pwb_assy_rev;
  int ext_mem_size;
  
  /* Initialize the chip support library, required */
  CSL_init();
  /* Initialize the board support library, required */
  BSL_init();
  
  /* dsp and periphiral initialization */    
  CHIP_RSET(CSR,0x100);            /* Disable all interrupts              */
  CHIP_RSET(IER,1);                /* Disable all interrupts except NMI   */
  CHIP_RSET(ICR,0xffff);           /* Clear all pending interrupts        */

/*************************************************************************
* Standard 6211 DSK includes 2 MT48LC1M16A1-7 devices =>4MB SDRAM        *
*   16Mb (16-bit x 2 banks x 512K) parts = 2MB / part                    *
*   EMIF_SDCTRL=0x07227000                                               *
*   EMIF_SDEXT=0x54529                         Board Rev = 1             *
*------------------------------------------------------------------------*
* Standard 6711 DSK includes 2 MT48LC4M16A2-8 devices =>16MB SDRAM       *
*   64Mb (16-bit x 4 banks x 1M)   parts = 8MB / part                    *
*   EMIF_SDCTRL=0x57116000                                               *
*   EMIF_SDEXT=0x54529 (Hitachi 0x54509)       Board Rev = 2             *
*------------------------------------------------------------------------*
* Other 6711 DSK configurations are as follows:                          *
*   128Mb (16-bit x 4 banks x 2M)  parts = 16MB / part (=>32MB SDRAM)    *
*   EMIF_SDCTRL=0x53116000                                               *
*   EMIF_SDEXT=0x54529 (Hitachi 0x54509)       Board Rev = 3             *
*   ---------------------------------------------------------------------*
*   256Mb (16-bit x 4 banks x 4M)  parts = 32MB / part (=>64MB SDRAM)    *
*   EMIF_SDCTRL=0x63116000                                               *
*   EMIF_SDEXT=0x54529                         Board Rev = 4             *
*************************************************************************/

/******************************************************************************\
 * CONFIGURE EMIF                                                             *
\******************************************************************************/  
  EMIF_configArgs(0x00003300,            /* EMIF global control register         */
               0xFFFFFF30,            /* CE0 - SDRAM                          */
               0xFFFFFF23,            /* CE1 - 32-bit asynch                  */
               0xFFFFFF23,            /* CE2 - 32-bit asynch on daughterboard */
               0xFFFFFF23,            /* CE3 - 32-bit asynch on daughterboard */
               0x07117000,            /* SDRAM control register (100 MHz)     */
               0x0000061A,            /* SDRAM Timing register                */
               0x00054519             /* SDRAM Extension register             */
              );

#if PRINT
  printf("\n*TMS320C6211/6711 DSK Confidence Test*\n");
#endif

  /*-----------------------------------------------------------------------*/
  /* Read DIP switches                                                     */
  /*-----------------------------------------------------------------------*/
  io_port_values = BOARD_readReg(BOARD_REG_IOPORT);
  pwb_assy_rev=get_bdrev();   
  EMIF_RSET(CECTL1,0xFFFFFF03);     /* CE1 - 8-bit asynch                   */

#if PRINT  
  printf("\n USER_SW3=%d ", DIP_get(DIP_3));
  printf(" USER_SW2=%d ", DIP_get(DIP_2));
  printf(" USER_SW1=%d ", DIP_get(DIP_1));
  printf(" Board Rev=%d\n\n",pwb_assy_rev);
#endif 

  /*-----------------------------------------------------------------------*/
  /* Modify SDRAM parameter value(s) according to board revision           */
  /*-----------------------------------------------------------------------*/
  switch (pwb_assy_rev) {
    case 0: EMIF_RSET(SDCTL,0x07126000);
            ext_mem_size = EXTERNAL_MEM_SIZE;
            break;
    case 1: EMIF_RSET(SDCTL,0x07227000);
            ext_mem_size = EXTERNAL_MEM_SIZE;
            break;
    case 2: EMIF_RSET(SDCTL,0x57116000);
            ext_mem_size = 4*EXTERNAL_MEM_SIZE;
            break;
    case 3: EMIF_RSET(SDCTL,0x53116000);
            ext_mem_size = 8*EXTERNAL_MEM_SIZE;
            break;
    case 4: EMIF_RSET(SDCTL,0x63116000);
            ext_mem_size = 16*EXTERNAL_MEM_SIZE;
            break;
  }
	 
  /*-----------------------------------------------------------------------*/
  /* Perform Hand Shaking                                                  */
  /*-----------------------------------------------------------------------*/
  handShakingBuffer[0] = ID_0;
  handShakingBuffer[1] = ID_1;
  handShakingBuffer[2] = ID_2;
  handShakingBuffer[3] = 0;
  handShakingBuffer[4] = 0;
  handShakingBuffer[5] = io_port_values;

  /*-----------------------------------------------------------------------*/
  /* Wait for Host ACK                                                     */
  /*-----------------------------------------------------------------------*/
  while(handShakingBuffer[4] != HOST_RECEIVE_HAND_SHAKING_INFO);

  /*-----------------------------------------------------------------------*/
  /* Reset handShakingBuffer                                               */
  /*-----------------------------------------------------------------------*/
  handShakingBuffer[0] = 0;
  handShakingBuffer[1] = 0;
  handShakingBuffer[2] = 0;

#if PRINT
  printf("Hand Shake Complete.\n");
#endif
  
  /*-----------------------------------------------------------------------*/
  /* Run DSP forever                                                       */
  /*-----------------------------------------------------------------------*/
  while(1)
  {

  /*-----------------------------------------------------------------------*/
  /* Test for HOST ready                                                   */
  /*-----------------------------------------------------------------------*/
#if PRINT
    printf("\nDSP Ready for HOST Command.\n");
#endif

    /*---------------------------------------------------------------------*/
    /* Wait until HOST has a command and parameters ready                  */
    /*---------------------------------------------------------------------*/
	  while(handShakingBuffer[4] != HOST_STATUS_INPUT_READY
		  && handShakingBuffer[4] != HOST_STATUS_END_PROCESSING);

    /*---------------------------------------------------------------------*/
    /* Quit if HOST requests it or Process a HOST command                  */
    /*---------------------------------------------------------------------*/
	  if(handShakingBuffer[4] == HOST_STATUS_END_PROCESSING)
    {
#if PRINT
      printf("\nHOST Requested End of Processing.\n");
#endif
		  break;
    }
    else
    {
      handShakingBuffer[4] = DSP_PROCESSING_COMMAND;
    }

#if PRINT
    printf("\nNew HOST Command Read: ");
#endif
	  
    /*---------------------------------------------------------------------*/
    /* Process a HOST command                                              */
    /*---------------------------------------------------------------------*/
		switch(handShakingBuffer[3])
		{
      /*-------------------------------------------------------------------*/
      /* Run the Internal SRAM test and return information if(error)       */
      /*-------------------------------------------------------------------*/
 			case ISRAM:
#if PRINT
        printf("ISRAM\n");
#endif
        /*-----------------------------------------------------------------*/
        /* Attempt to write 0x55555555 to numerous locations and read same */
        /*-----------------------------------------------------------------*/
#if PRINT
        printf(" Write 5's.\n");
#endif
        if((error=mem_test(ALL_5,INTERNAL_MEM_START,INTERNAL_MEM_SIZE)) != 0)
        {
          handShakingBuffer[0]=error;
          handShakingBuffer[2]=INTERNAL_MEM_SIZE;
          handShakingBuffer[5]=ISRAM_ERR55;
        }
        else
          handShakingBuffer[5]=ISRAM_OK;

        /*-----------------------------------------------------------------*/
        /* Attempt to write 0xAAAAAAAA to numerous locations and read same */
        /*-----------------------------------------------------------------*/
#if PRINT
        printf(" Write A's.\n");
#endif
        if((error=mem_test(ALL_A,INTERNAL_MEM_START,INTERNAL_MEM_SIZE)) != 0)
        {
          handShakingBuffer[1]=error;
          handShakingBuffer[2]=INTERNAL_MEM_SIZE;
          handShakingBuffer[5] |=ISRAM_ERRAA;
        }
        else
          handShakingBuffer[5] |=ISRAM_OK;
				break;

      /*-------------------------------------------------------------------*/
      /* Run the External SDRAM test and return information if(error)      */
      /*-------------------------------------------------------------------*/
      case SDRAM:
#if PRINT
        printf("SDRAM\n");
#endif
        /*-----------------------------------------------------------------*/
        /* Attempt to write 0x5A5A5A5A to numerous locations and read same */
        /*-----------------------------------------------------------------*/
#if PRINT
        printf(" Write 5A's.\n");
#endif
        if((error=mem_test_alt(ALT_5A,EXTERNAL_MEM_START,ext_mem_size)) != 0)
        {
          handShakingBuffer[0]=error;
          handShakingBuffer[2]=ext_mem_size;
          handShakingBuffer[5]=SDRAM_ERR5A;
        }
        else
          handShakingBuffer[5]=SDRAM_OK;

        /*-----------------------------------------------------------------*/
        /* Attempt to write 0xA5A5A5A5 to numerous locations and read same */
        /*-----------------------------------------------------------------*/
#if PRINT
        printf(" Write A5's.\n");
#endif
        if((error=mem_test_alt(ALT_A5,EXTERNAL_MEM_START,ext_mem_size)) != 0)
        {
          handShakingBuffer[1]=error;
          handShakingBuffer[2]=ext_mem_size;
          handShakingBuffer[5] |=SDRAM_ERRA5;
        }
        else
          handShakingBuffer[5] |=SDRAM_OK;
				break;

      /*-------------------------------------------------------------------*/
      /* Run the External FLASH test and return information if(error)      */
      /*-------------------------------------------------------------------*/
      case FLASH:
#if PRINT
        printf("FLASH\n");
#endif
        /*-----------------------------------------------------------------*/
        /* Use if FLASH on Factory Board includes Checksum stored in POST  */
        /*-----------------------------------------------------------------*/
/* This test is turned off because POST changed ... it has no Checksum
#if PRINT
        printf(" CHECKSUM");
#endif
        if((error=flash_checksum(FLASH_START,POST_SIZE)) != 0)
        {
          handShakingBuffer[0]=error;
          handShakingBuffer[5]=FLASH_ERRCK;
        }
        else
          handShakingBuffer[5]=FLASH_OK;
          
        handShakingBuffer[1]=exp_chksum;
        handShakingBuffer[2]=cmp_chksum;

#if PRINT
          printf("\n  Expected=0x%08x\n  Computed=0x%08x\n",exp_chksum,cmp_chksum);
#endif
*/
        /*-----------------------------------------------------------------*/
        /* Attempt to write 0x55555555 to numerous locations and read same */
        /*-----------------------------------------------------------------*/
#if PRINT
        printf(" Write 5's.\n");
#endif
        if((error=flash_test(0x55,POST_END,FLASH_WRITE_SIZE)) != 0)
        {
          handShakingBuffer[0]=error;
/*          handShakingBuffer[5] |=FLASH_ERR55;    Use When Checksum first */
          handShakingBuffer[5]=FLASH_ERR55;
        }
        else
/*          handShakingBuffer[5] |=FLASH_OK;       Use When Checksum first */ 
          handShakingBuffer[5]=FLASH_OK; 

        /*-----------------------------------------------------------------*/
        /* Attempt to write 0xAAAAAAAA to numerous locations and read same */
        /*-----------------------------------------------------------------*/
#if PRINT
        printf(" Write A's.\n");
#endif
        if((error=flash_test(0xAA,POST_END,FLASH_WRITE_SIZE)) != 0)
        {
          handShakingBuffer[0]=error;
          handShakingBuffer[5] |=FLASH_ERRAA;
        }
        else
          handShakingBuffer[5] |=FLASH_OK;

        /*-----------------------------------------------------------------*/
        /* Example of Status word of hSB - Used when FLASH test turned off */
        /*-----------------------------------------------------------------*/
//        handShakingBuffer[5]=TEST_DISABLED;
        break;

      /*-------------------------------------------------------------------*/
      /* Run the MCBSP test and return information if(error)               */
      /*-------------------------------------------------------------------*/
      case MCBSP:
#if PRINT
        printf("MCBSP\n");
#endif
#if PRINT
        printf(" Digital Loopback Mode.\n");
#endif
        /* open McBSP channel before testing */
        hMcbsp = MCBSP_open(MCBSP_DEV0, MCBSP_OPEN_RESET);

        if((error=mcbsp0_test()) != 0)
        {
          handShakingBuffer[0]=error;
          handShakingBuffer[5]=MCBSP_ERROR;
        }
        else
          handShakingBuffer[5]=MCBSP_OK;
        
        /* close McBSP channel before proceeding */
        MCBSP_close(hMcbsp);
        break;

      /*-------------------------------------------------------------------*/
      /* Run the TIMER test and return status                              */
      /*-------------------------------------------------------------------*/
      case TIMER:
#if PRINT
        printf("TIMER\n");
#endif
        flag = 0;
        /* set up Timer 0 for short period and start timer */
        hTimer = TIMER_open(TIMER_DEV0, TIMER_OPEN_RESET);
        TIMER_config(hTimer, &myTimerConfig);
         
        /* interrupt configuration */
        IRQ_clear(IRQ_EVT_TINT0);    /* clear interrupt 14                    */
        IRQ_enable(IRQ_EVT_TINT0);   /* enable interrupt 14 (TIMER 0) and NMI */
        CHIP_RSET(CSR,CHIP_RGET(CSR) | 1 );  /* enable global interrupts      */

        TIMER_start(hTimer);
        
        /* wait for interrupt 14 to pause timer and toggle flag to 1 */
        while (flag == 0);
        TIMER_resume(hTimer);
        /* wait for interrupt 14 to pause timer and toggle flag to 0 */
        while (flag == 1);
        
        CHIP_RSET(IER,0);             /* disable all interrupts                */   
        handShakingBuffer[5]=TIMER_OK;
        break;

      /*-------------------------------------------------------------------*/
      /* Run the QDMA test and return information if(error)                */
      /*-------------------------------------------------------------------*/
      case QDMA:
#if PRINT
        printf("QDMA\n");
#endif
        IRQ_clear(IRQ_EVT_EDMAINT);  /* clear interrupt 14                    */
        IRQ_enable(IRQ_EVT_EDMAINT); /* enable interrupt 14 (TIMER 0) and NMI */
        CHIP_RSET(CSR,CHIP_RGET(CSR) | 1 );  /* enable global interrupts      */
 
        flag = 0;
        
        src = 0xf000;
        dst = 0x80000000;
        size = 500;
        qdma_start(src,dst,size);
        
        /* wait for interrupt 8 to halt transfer and toggle flag to 1 */
        while (flag == 0)  ;
        error = qdma_check(src,dst,size);
        if(error != 0)
        {
          handShakingBuffer[0]=error;
          handShakingBuffer[5]=QDMA_ERROR;
        }
        else
          handShakingBuffer[5]=QDMA_OK;

        CHIP_RSET(IER,0);          /* disable all interrupts                */   
        break;

      /*-------------------------------------------------------------------*/
      /* Run the LEDS test and return information for debug                */
      /*-------------------------------------------------------------------*/
      case LEDS:
#if PRINT
        printf("LEDS\n");
#endif
        /*-----------------------------------------------------------------*/
        /* Simultaneous menu and debug modes allow user to enter values    */
        /*-----------------------------------------------------------------*/
#if PRINT
        printf(" Flash %d times with %d msec period.\n",
          handShakingBuffer[0],handShakingBuffer[1]);
#endif
        led_blink(handShakingBuffer[0],handShakingBuffer[1],handShakingBuffer[2]);
        handShakingBuffer[5]=LEDS_OK;
        break;

      /*-------------------------------------------------------------------*/
      /* Run the CODEC test and return status                              */
      /*-------------------------------------------------------------------*/
      case CODEC:
#if PRINT
        printf("CODEC\n");
#endif
        hAD535 = AD535_open(AD535_localId);
        AD535_config(hAD535, &myConfig);

#if PRINT
        printf(" Play 1KHz tone.\n");
#endif
        if((errortone=play_codec(handShakingBuffer[0], 1)) != 0)
        {
          handShakingBuffer[0]=errortone;
          handShakingBuffer[5]=CODEC_ERRTN;
        }
        else
          handShakingBuffer[5] =CODEC_OK;

#if PRINT
        printf(" Play CD/MIC input.\n");
#endif
        if((error=play_codec(handShakingBuffer[1], 0)) != 0)
        {
          handShakingBuffer[1]=error;
          handShakingBuffer[5] |=CODEC_ERRCD;
        }
        else
          handShakingBuffer[5] |=CODEC_OK;
				break;

      /*-------------------------------------------------------------------*/
      /* Run the PGMEM test and return information for debug               */
      /*-------------------------------------------------------------------*/
 			case PGMEM:
#if PRINT
        printf("PGMEM\n");
#endif
        /*-----------------------------------------------------------------*/
        /* Simultaneous menu and debug modes allow user to enter values    */
        /*-----------------------------------------------------------------*/
#if PRINT
        printf(" Write 0x%08x to 0x%08x for 0x%08x words.\n",
          handShakingBuffer[0],(EXTERNAL_MEM_START+handShakingBuffer[1]),
          handShakingBuffer[2]);
#endif
        if((error=mem_test(handShakingBuffer[0],(EXTERNAL_MEM_START+handShakingBuffer[1]),
          handShakingBuffer[2])) != 0)
        {
          handShakingBuffer[0]=error;
          handShakingBuffer[5]=PGMEM_ERROR;
        }
        else
        {
          handShakingBuffer[1]+=EXTERNAL_MEM_START;
          handShakingBuffer[5]=PGMEM_OK;
        }
				break;

      /*-------------------------------------------------------------------*/
      /* If user adds new command with out a case ... it winds up here     */
      /*-------------------------------------------------------------------*/
      default:
#if PRINT
        printf("ERROR\n");
#endif
        handShakingBuffer[5]=DEFAULT_ERROR;
				break;
		}

    /*---------------------------------------------------------------------*/
    /* Tell the HOST that DSP processing is done for this command          */
    /*---------------------------------------------------------------------*/
		handShakingBuffer[4] = DSP_STATUS_OUTPUT_READY;
  }

  /*-----------------------------------------------------------------------*/
  /* Wrap it up                                                            */
  /*-----------------------------------------------------------------------*/
#if PRINT
  printf("\nThat's All Folks!\n");
#endif

  TIMER_close(hTimer);
  AD535_close(hAD535);

  return(0);
}

/*-------------------------------------------------------------------------*/
/* mem_test() - used to test internal SRAM and external SDRAM              */
/*-------------------------------------------------------------------------*/
int mem_test (int pattern, int start_address, int size_in_word )
{
  int i;
  int temp;
  int error = 0;
  int *mem_ptr = (int *)start_address;
  
  for(i=0;i<size_in_word;i++)    /* write pattern to the memory */
  {
    *mem_ptr++ = pattern;
  }

  mem_ptr = (int *)start_address;
  
  for(i=0;i<size_in_word;i++)  /* read data back from memory */
  {
    temp = *mem_ptr++;
    
#if SPECIAL==ENDPNT
    if(i==0)
      printf(" Read=0x%08x at Address=0x%08x\n",temp,(mem_ptr-1));
    else if((i==size_in_word-1))
      printf(" Read=0x%08x at Address=0x%08x\n",temp,(mem_ptr-1));
#endif    

#if SPECIAL==INTERV
    if(!(i%(1024*256)))
      printf(" Read=0x%08x at Address=0x%08x\n",temp,(mem_ptr-1));
#endif    
    
    if ( temp != pattern)
    {
      error++;
#if SPECIAL==MEMTST
      if(error<25)
        printf(" Read=0x%08x at Address=0x%08x\n",temp,(mem_ptr-1));
#endif
    }
  } 
  
  return error;
}

/*-------------------------------------------------------------------------*/
/* mem_test_alt() - used to test internal SRAM and external SDRAM          */
/*-------------------------------------------------------------------------*/
int mem_test_alt (int pattern, int start_address, int size_in_word )
{
  int i;
  int temp_read,temp_expected;
  int error = 0;
  int *mem_ptr = (int *)start_address;
  
  for(i=0;i<size_in_word;i++)    /* write pattern to the memory */
  {
    if(i%2)
      *mem_ptr++ = ~pattern;     /* flip alternating bits       */
    else
      *mem_ptr++ = pattern;
  }

  mem_ptr = (int *)start_address;
  
  for(i=0;i<size_in_word;i++)    /* read data back from memory  */
  {
    temp_read = *mem_ptr++;
    
#if SPECIAL==ENDPNT
    if(i==0)
      printf(" Read=0x%08x at Address=0x%08x\n",temp_read,(mem_ptr-1));
    else if((i==size_in_word-1))
      printf(" Read=0x%08x at Address=0x%08x\n",temp_read,(mem_ptr-1));
#endif    

#if SPECIAL==INTERV
    if(!(i%(1024*256)))
      printf(" Read=0x%08x at Address=0x%08x\n",temp_read,(mem_ptr-1));
#endif    
    
    if(i%2)
    {
      temp_expected = ~pattern;  /* flip alternating bits       */
    }
    else
    {
      temp_expected = pattern;
    }
    
    if ( temp_read != temp_expected )
    {
      error++;
#if SPECIAL==MEMTST
      if(error<25)
        printf(" Read=0x%08x at Address=0x%08x\n",temp_read,(mem_ptr-1));
#endif
    }
  } 
  
  return error;
}

/*-------------------------------------------------------------------------*/
/* flash_test() - used to test external FLASH                              */
/*-------------------------------------------------------------------------*/
int flash_test( char pattern, int start_address, int size_in_byte)
{
  int i=0, j=0;
  int error = 0;
  int write_size = 0;
  int read_size  = 0;
  
  char *flash_ptr = (char *)start_address;
  char buffer[FLASH_PAGE_SIZE];
  char *buffer_ptr;
  
  /* initialize buffer to write from */
  for (i=0; i<FLASH_PAGE_SIZE;i++)
    buffer[i] = pattern;
    
  i = size_in_byte;
  
  /* write to flash  */
  while (i > 0){
    if (i < FLASH_PAGE_SIZE) write_size = i;
    else write_size = FLASH_PAGE_SIZE;
    FLASH_write((Uint32)buffer, (Uint32)flash_ptr, (Uint32)write_size);
    i = i - FLASH_PAGE_SIZE;
    flash_ptr = flash_ptr + FLASH_PAGE_SIZE;
  }

  flash_ptr = (char *)start_address;
  buffer_ptr = buffer;

  /* read from flash */
  while (i > 0){
    if (i < FLASH_PAGE_SIZE) read_size = i;
    else read_size = FLASH_PAGE_SIZE;
    FLASH_read((Uint32)flash_ptr, (Uint32)buffer, (Uint32)read_size);
    i = i - FLASH_PAGE_SIZE;
    flash_ptr = flash_ptr + FLASH_PAGE_SIZE;
    /* test flash */
    j=0;
    for(j=0;j<read_size;j++){
      if (*buffer_ptr++ != pattern){
        error++;
      #if SPECIAL==FLATST
        if(error<25)
          printf(" Read=0x%08x at Address=0x%08x\n",*flash_ptr,(flash_ptr-1));
      #endif
      }
    }/* END FOR */  
  }/* END WHILE */
  
  return error;
}


/*-------------------------------------------------------------------------*/
/* flash_checksum() - used to test checksum in POST on external FLASH      */
/*-------------------------------------------------------------------------*/
/*int flash_checksum (int start_address, int size_in_byte)
{
  int i = 0, error = 0;
  int checksum,*checksum_ptr;
  char *flash_ptr = (char *)start_address;
  int temp;
  
  i=0;     
  checksum = 0;
  
  while(i < size_in_byte-4)
  {  
    temp = *flash_ptr++;
    temp &= 0xff;
    checksum = checksum + temp;
    i++;
  }
   
  checksum_ptr = (int *)flash_ptr;

  exp_chksum=*checksum_ptr;
  cmp_chksum=checksum;

  if( exp_chksum != cmp_chksum) error = 1;
//  if( *checksum_ptr != checksum) error = 1;
  
  return error;
}
*/

/*-------------------------------------------------------------------------*/
/* mcbsp0_test() - used to test McBSP0                                     */
/*-------------------------------------------------------------------------*/
int mcbsp0_test()
{
  volatile Uint32 temp =0,temp1;
  	
  /* set up McBSP0 */
  MCBSP_configArgs(hMcbsp,
                0x00C1A001, /* SPCR- serial port control reg.              */
                0x00010040, /* RCR - recieve control reg.16 bit data/frame */
                0x00010040, /* XCR - xmit control reg. 16 bit data/frame   */
                0x2014004A, /* SRGR- sample rate generator, baud rate 1MHz */
                0x00000000, /* MCR - multichannel control reg.             */
                0x00000000, /* RCER- recieve channel enable reg.           */
                0x00000000, /* RCER- recieve channel enable reg.           */
                0x00000A00  /* PCR - pin control reg.                      */
               );
  
  MCBSP_write(hMcbsp, 0xAAAA);
  temp1 = MCBSP_read(hMcbsp);
  while(!MCBSP_rrdy(hMcbsp)); 
  temp1 = MCBSP_read(hMcbsp);
  
  MCBSP_write(hMcbsp, 0x5555);
  while(!MCBSP_xrdy(hMcbsp));
  while(!MCBSP_rrdy(hMcbsp)); 
  temp = MCBSP_read(hMcbsp);
  temp &= 0xffff;

  if (temp != 0x5555)
  {
    temp = 1;
    return temp;
  }
    
  MCBSP_write(hMcbsp, 0xAAAA);
  while(!MCBSP_xrdy(hMcbsp));
  while(!MCBSP_rrdy(hMcbsp)); 
  temp = MCBSP_read(hMcbsp);
  temp &= 0xffff;
    
  if (temp != 0xAAAA)
  {
    temp = 1;
    return temp;
  }    
  return 0;
}


/*-------------------------------------------------------------------------*/
/* qdma_start() - used to start QDMA                                       */
/*-------------------------------------------------------------------------*/
void qdma_start(int src, int dst, int size)
{ 
  /* EDMA_CIER_SET(0x100);
  EDMA_QSRC_SET(src);
  EDMA_QDST_SET(dst);
  EDMA_QIDX_SET(0x00000000); 
  EDMA_QOPT_SET(0x41380001);
  EDMA_QSCNT_SET(size);*/
  
  EDMA_RSET(CIER,0x100);
  EDMA_RSET(QSRC,src);
  EDMA_RSET(QDST,dst);
  EDMA_RSET(QIDX,0x00000000); 
  EDMA_RSET(QOPT,0x41380001);
  EDMA_RSET(QCNT,size);
}
  
/*-------------------------------------------------------------------------*/
/* qdma_check() - used to test QDMA operation                              */
/*-------------------------------------------------------------------------*/
int qdma_check( int src, int dst, int size)
{
  int i,error = 0;
  int *src_ptr = (int *)src;
  int *dst_ptr = (int *)dst;

  for (i=0;i<size;i++)
  {
    if (*src_ptr++ != *dst_ptr++) error++;
  }
  
  return error;
}

/*-------------------------------------------------------------------------*/
/* qdma_isr() - interrupt service routine for qdma                         */
/*-------------------------------------------------------------------------*/
interrupt void qdma_isr()
{
  /* clear pending EDMA interrupt 8 */
  EDMA_RSET(CIPR,EDMA_RGET(CIPR) | 0x0100); 
  flag = 1;
}

/*-------------------------------------------------------------------------*/
/* led_blink() - used to blink all the LEDS on the DSK                     */
/*-------------------------------------------------------------------------*/
void led_blink(int count, int ms_period, Uint32 leds_to_light)
{
  int i;
  volatile int led1=0,led2=0,led3=0;
  
  /* decode the led mask */
  if ((leds_to_light>>24) & 1) led1 = 1;
  if ((leds_to_light>>25) & 1) led2 = 1;
  if ((leds_to_light>>26) & 1) led3 = 1;
  
  /* set up timer to be used for delay_msec() */
  myTimerConfig.ctl = 0x00000300;
  myTimerConfig.prd = 0xFFFFFFFF;
  myTimerConfig.cnt = 0;
  TIMER_config(hTimer, &myTimerConfig);
    
  for (i=0;i<count;i++)
  {
    LED_off(LED_ALL);           /* turn off all three leds    */
    delay_msec((Uint32)ms_period/2);
    if (led1) LED_on(LED_1);    /* turn on leds_to_light      */
    if (led2) LED_on(LED_2);
    if (led3) LED_on(LED_3);
    delay_msec((Uint32)ms_period/2);
  }
  LED_off(LED_ALL);             /* turn off all three leds    */
}


/*-------------------------------------------------------------------------*/
/* get_bdrev() - used to read board revision bits                          */
/*-------------------------------------------------------------------------*/
Uint32 get_bdrev(void) {

  return((BOARD_readReg(BOARD_REG_IOPORT)>>29)&0x7);
}

/*-------------------------------------------------------------------------*/
/* delay_msec() - used to delay DSP by user specified time in msec         */
/*-------------------------------------------------------------------------*/
void delay_msec(Uint32 msec){
  /* Assume 150 MHz CPU, timer peirod = 4/150 MHz */
  Uint32 timer_limit;
  Uint32 timer_start;

  timer_limit = ((Uint32)msec*9375)<<2;
  timer_start = TIMER_getCount(hTimer);
  TIMER_resume(hTimer);
  
  while ( (TIMER_getCount(hTimer) - timer_start) < timer_limit );
  TIMER_pause(hTimer);
}

/*-------------------------------------------------------------------------*/
/* timer0_isr() - interrupt service routine for TIMER0                     */
/*-------------------------------------------------------------------------*/
interrupt void timer0_isr(){

  TIMER_pause(hTimer);
  if(flag == 0) 
    flag = 1;                      /* toggle the flag */
  else 
    flag = 0;
}

/*-------------------------------------------------------------------------*/
/* play_codec() - used to test codec operation                             */
/*-------------------------------------------------------------------------*/
int play_codec(int number, int tone_playbk)
{
  int i,j;

  AD535_reset(hAD535);
  
  AD535_writeReg(hAD535, AD535_REG_CTRL3, 0x06);
  if( AD535_readReg(hAD535, AD535_REG_CTRL3) != 0x06) {
  #if PRINT
    printf ("  Error in setting up control register 3.\n");
  #endif
    return(3);
  }
  
  AD535_writeReg(hAD535, AD535_REG_CTRL4, 0x00);
  if( AD535_readReg(hAD535, AD535_REG_CTRL4) != 0x00) {
  #if PRINT
    printf ("  Error in setting up control register 4.\n");
  #endif
    return(4);
  }
  
  AD535_writeReg(hAD535, AD535_REG_CTRL5, 0x02);
  if( (AD535_readReg(hAD535, AD535_REG_CTRL5) & 0xFE) != 0x02) {
  #if PRINT
    printf ("  Error in setting up control register 5.\n");
  #endif
    return(5);
  }  

  if(tone_playbk) {
    for (i=0;i<number;i++){
      for (j=0;j<8;j++)
        AD535_write(hAD535, (int)sin_table[j]);
    }
  }
  else {
    for (i=0;i<number;i++)
      AD535_write(hAD535, AD535_read(hAD535) );
  }
  return(0);
}