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

📄 test_ecc.c

📁 <B>SMSC USB2.0 Flash硬盘驱动源码</B>
💻 C
字号:
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// ecc validation tests
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
#include "project.h"

//-----------------------------------------------------------------------------
// BIT Controll Macro
//-----------------------------------------------------------------------------
static code char k_tbl_bitdata[] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80} ;
#define _setbit(__buffer,__bitaddr) (__buffer[(uint8)((__bitaddr)/8)] |=  k_tbl_bitdata[(__bitaddr)%8])
#define _clrbit(__buffer,__bitaddr) (__buffer[(uint8)((__bitaddr)/8)] &= ~k_tbl_bitdata[(__bitaddr)%8])
#define _chkbit(__buffer,__bitaddr) (__buffer[(uint8)((__bitaddr)/8)] &   k_tbl_bitdata[(__bitaddr)%8])
#define _flpbit(__buffer,__bitaddr) (__buffer[(uint8)((__bitaddr)/8)] ^=  k_tbl_bitdata[(__bitaddr)%8])

//+----------------------------------------------------------------------
//-----------------------------------------------------------------------

xdata uint8 buffer_aa[256] ;
xdata uint8 ecc_aa_lp_hi ;
xdata uint8 ecc_aa_lp_lo ;
xdata uint8 ecc_aa_cp ;

xdata uint8 buffer_55[256] ;
xdata uint8 ecc_55_lp_hi ;
xdata uint8 ecc_55_lp_lo ;
xdata uint8 ecc_55_cp ;

xdata uint8 buffer_a[256] ;
xdata t_ecc ecc_a ;

xdata uint8 buffer_b[256] ;
xdata t_ecc ecc_b ;

extern t_result ecc_sw_correct_ex(uint8 *buf ) reentrant ;

//+----------------------------------------------------------------------
//-----------------------------------------------------------------------
t_result ecc_test_err_1(uint8* buf, uint16 bit_position) reentrant 
{
  t_result result ;

  // zap buffer
  _setbit(buf, bit_position) ;

  result = k_success ;
  
  // calculate ecc, results in ecc_lp_hi/lo/cp
  ecc_sw_calculate(buf) ;

  // compare ecc_* with buff_ecc* (set before calling this function)
  if( 0==( (ecc_lp_hi^buf_ecc_lp_hi) | (ecc_lp_lo^buf_ecc_lp_lo) | (ecc_cp^buf_ecc_cp) ) )
  {
    result = k_error ;
    TRACE1(87, ecc, 0, "bit %d:  failed to detect erroneous 1", bit_position) ;
  }

  if(k_success != ecc_sw_correct_ex(buf))
  {
    result = k_error ;
    TRACE1(88, ecc, 0, "bit %d:  failed to correct errorneous 1", bit_position) ;
  }
  
  return result ;
}

//+----------------------------------------------------------------------
//-----------------------------------------------------------------------
t_result ecc_test_err_0(uint8* buf, uint16 bit_position) reentrant 
{
  t_result result ;

  // zap buffer
  _clrbit(buf, bit_position) ;

  result = k_success ;
  
  // calculate ecc, results in ecc_lp_hi/lo/cp
  ecc_sw_calculate(buf) ;

  // compare ecc_* with buff_ecc* (set before calling this function)
  // I expect this to show a difference.  
  if( 0==( (ecc_lp_hi^buf_ecc_lp_hi) | (ecc_lp_lo^buf_ecc_lp_lo) | (ecc_cp^buf_ecc_cp) ) )
  {
    result = k_error ;
    TRACE1(89, ecc, 0, "bit %d:  failed to detect erroneous 1", bit_position) ;
  }

  if(k_success != ecc_sw_correct_ex(buf))
  {
    result = k_error ;
    TRACE1(90, ecc, 0, "bit %d:  failed to correct errorneous 1", bit_position) ;
  }
  return result ;
}

//+----------------------------------------------------------------------
//-----------------------------------------------------------------------
t_result ecc_test_buffer(uint8* buf) reentrant 
{
  t_result result ;

  result = k_success ;
  
  // calculate ecc, results in ecc_lp_hi/lo/cp
  ecc_sw_calculate(buf) ;

  if(k_success != ecc_sw_correct_ex(buf))
  {
    // TRACE0(91, ecc, 0, "ecc_test: ecc_sw_correct_ex() failed") ;
    result = k_error ;
  }
  return result ;
}

//+----------------------------------------------------------------------
//-----------------------------------------------------------------------

void ecc_test_init_buffers(void) reentrant
{
  uint16 i ;

  // fill buffer w/ all aa's 
  for(i=0; i< 256; i++)
  {
    buffer_aa[i]=0xaa ;
    buffer_55[i]=0x55 ;
    buffer_a[i]=i ;
    buffer_b[i]=(255-i) ;
  }

  // calculate ecc values
  ecc_sw_calculate(buffer_aa) ; 
  ecc_aa_lp_hi = ecc_lp_hi ;
  ecc_aa_lp_lo = ecc_lp_lo ;
  ecc_aa_cp = ecc_cp ;

  ecc_sw_calculate(buffer_55) ;
  ecc_55_lp_hi = ecc_lp_hi ;
  ecc_55_lp_lo = ecc_lp_lo ;
  ecc_55_cp = ecc_cp ;

  ecc_sw_calculate(buffer_a) ;
  ecc_a.u8.lp_hi = ecc_lp_hi ;
  ecc_a.u8.lp_lo = ecc_lp_lo ;
  ecc_a.u8.cp = ecc_cp ;
  
  ecc_sw_calculate(buffer_b) ;
  ecc_b.u8.lp_hi = ecc_lp_hi ;
  ecc_b.u8.lp_lo = ecc_lp_lo ;
  ecc_b.u8.cp = ecc_cp ;
  
}

void ecc_test_sw_one_bit(void) reentrant
{
  uint16 i ;
  uint16 bit_position ;
  uint8  result ;

  result = k_success ;

  // initialize buffers to known values
  ecc_test_init_buffers() ;

  // test every bit position of both buffers
  for(bit_position=0; bit_position < 2048; bit_position++)
  {
    result = k_success ;
    if (bit_position&0x0001)
    {
      // buffer 55 ecc data
      buf_ecc_lp_hi = ecc_55_lp_hi ;
      buf_ecc_lp_lo = ecc_55_lp_lo ;
      buf_ecc_cp    = ecc_55_cp ;
      
      if( k_success != ecc_test_err_1(buffer_55, bit_position) )
      {
        result = k_error ;
        TRACE1(91, ecc, 0, "bit %d:  failed to detect erroneous 1, buffer_55", bit_position) ;
      }
        
      // set buffer aa's ecc data
      buf_ecc_lp_hi = ecc_aa_lp_hi ;
      buf_ecc_lp_lo = ecc_aa_lp_lo ; 
      buf_ecc_cp = ecc_aa_cp ; 
      if (k_success != ecc_test_err_0(buffer_aa, bit_position) )
      {
        result = k_error ;
        TRACE1(92, ecc, 0, "bit %d:  failed to detect erroneous 0", bit_position) ;
      }

    }
    else
    {
      buf_ecc_lp_hi = ecc_55_lp_hi ;
      buf_ecc_lp_lo = ecc_55_lp_lo ; 
      buf_ecc_cp = ecc_55_cp ; 
      if (k_success != ecc_test_err_0(buffer_55, bit_position) )
      {
        result = k_error ;
        TRACE1(93, ecc, 0, "bit %d:  failed to detect & correct erroneous 0 in buffer", bit_position) ;
      }

      
      // for even bit positions, check erroneous 1 errors in buffer_aa
      buf_ecc_lp_hi = ecc_aa_lp_hi ;
      buf_ecc_lp_lo = ecc_aa_lp_lo ; 
      buf_ecc_cp = ecc_aa_cp ; 
      if (k_success != ecc_test_err_1( buffer_aa, bit_position ))
      {
        result = k_error ;
        TRACE1(94, ecc, 0, "bit %d: failed to detected & correct erroneous 1 in buffer", bit_position) ;
      }
    }

    // verify buffers
    for( i=0; i<256;i++)
    {
      if( buffer_55[i] != 0x55 )
      {
        result = k_error ;
        TRACE3(95, ecc, 0, "bit %d:  buffer_55 corrupted.  buffer[%d]: 0x%02x, s/b 0x55", bit_position, i, buffer_55[i]) ;
        // fix it
        buffer_55[i] = 0x55 ;
      }
      if( buffer_aa[i] != 0xaa )
      {
        result = k_error ;
        TRACE3(96, ecc, 0, "bit %d:  buffer_aa corrupted.  buffer[%d]: 0x%02x, s/b 0xaa", bit_position, i, buffer_aa[i]) ;
        // fix it
        buffer_aa[i] = 0xaa ;
      }
    }

    if ( k_success == result ) 
    {
      TRACE1(97, ecc, 0, "bit %d: pass", bit_position) ;
    }
  }

  if( k_success == result )
  {
  }

  result = k_success ;
  for(i=0; i<8;i++)
  {
    buf_ecc_lp_hi = ecc_aa_lp_hi^(0x01<<i) ;
    buf_ecc_lp_lo = ecc_aa_lp_lo ; 
    buf_ecc_cp = ecc_aa_cp ; 

    ecc_lp_hi = ecc_aa_lp_hi ;
    ecc_lp_lo = ecc_aa_lp_lo ; 
    ecc_cp = ecc_aa_cp ; 

    if (k_success != ecc_test_buffer(buffer_aa)) 
    {
      result = k_false ;
      TRACE1(98, ecc, 0, "buf_ecc_lp_hi bit %d: failed to correct single bit error", i) ;
    }
  }

  if(k_success == result)
  {
    TRACE0(99, ecc, 0, "buf_ecc_lp_hi single bit correction: passed") ;
  }

  result = k_success ;
  for(i=0; i<8;i++)
  {
    buf_ecc_lp_hi = ecc_aa_lp_hi ;
    buf_ecc_lp_lo = ecc_aa_lp_lo^(0x01<<i) ; 
    buf_ecc_cp = ecc_aa_cp ; 

    ecc_lp_hi = ecc_aa_lp_hi ;
    ecc_lp_lo = ecc_aa_lp_lo ; 
    ecc_cp = ecc_aa_cp ; 

    if (k_success != ecc_test_buffer(buffer_aa)) 
    {
      result = k_false ;
      TRACE1(100, ecc, 0, "buf_ecc_lp_lo bit %d: failed to correct single bit error", i) ;
    }
  }

  if(k_success == result)
  {
    TRACE0(101, ecc, 0, "buf_ecc_lp_lo single bit correction: passed") ;
  }

  result = k_success ;
  for(i=0; i<8;i++)
  {
    buf_ecc_lp_hi = ecc_aa_lp_hi ;
    buf_ecc_lp_lo = ecc_aa_lp_lo ;
    buf_ecc_cp = ecc_aa_cp^(0x01<<i) ;  

    ecc_lp_hi = ecc_aa_lp_hi ;
    ecc_lp_lo = ecc_aa_lp_lo ; 
    ecc_cp = ecc_aa_cp ; 

    if (k_success != ecc_test_buffer(buffer_aa)) 
    {
      result = k_false ;
      TRACE1(102, ecc, 0, "buf_ecc_cp bit %d: failed to correct single bit error", i) ;
    }
  }

  if(k_success == result)
  {
    TRACE0(103, ecc, 0, "buf_ecc_lp_lo single bit correction: passed") ;
  }
}


//+----------------------------------------------------------------------
//-----------------------------------------------------------------------
void ecc_test_sw_two_bit(void) reentrant
{
  uint16 bit1 ;
  uint16 bit2 ;
  
  uint8  result ;

  result = k_success ;

  // initialize buffers to known values
  ecc_test_init_buffers() ;

  // test every bit position of both buffers
  for(bit1=0; bit1 < 2048; bit1++)
  {
    result = k_success ;
    for(bit2=bit1+1; bit2<2048; bit2++)
    {
      // flip bits one and two
      _flpbit(buffer_55, bit1) ;
      _flpbit(buffer_55, bit2) ;
      _flpbit(buffer_aa, bit1) ;
      _flpbit(buffer_aa, bit2) ;

      buf_ecc_lp_hi = ecc_55_lp_hi ;
      buf_ecc_lp_lo = ecc_55_lp_lo ;
      buf_ecc_cp = ecc_55_cp ;
      if( k_success == ecc_test_buffer(buffer_55) )
      {
        TRACE2(104, ecc, 0, "bits %d, %d:  failed - error bits not detected ", bit1, bit2) ;
        result = k_error ;
      }

      buf_ecc_lp_hi = ecc_aa_lp_hi ;
      buf_ecc_lp_lo = ecc_aa_lp_lo ;
      buf_ecc_cp = ecc_aa_cp ;
      if( k_success == ecc_test_buffer(buffer_aa) )
      {
        TRACE2(105, ecc, 0, "bits %d, %d:  failed - error bits not detected", bit1, bit2) ;
        result = k_error ;
      }
      // reset the buffers for next round
      ecc_test_init_buffers() ;
    }

    for(bit2=0; bit2<7;bit2++)
    {
      _flpbit(buffer_55, bit1) ;
      _flpbit(buffer_aa, bit1) ;
      buf_ecc_lp_hi = ecc_55_lp_hi ^ (0x01<<bit2) ;
      buf_ecc_lp_lo = ecc_55_lp_lo ;
      buf_ecc_cp = ecc_55_cp ;

      if( k_success == ecc_test_buffer(buffer_55) )
      {
        TRACE2(106, ecc, 0, "bit %d, buf_ecc_lp_hi bit %d, failed - error bits not detected", bit1, bit2) ;
        result = k_error ;
      }

      buf_ecc_lp_hi = ecc_aa_lp_hi ^ (0x01<<bit2) ;
      buf_ecc_lp_lo = ecc_aa_lp_lo ;
      buf_ecc_cp = ecc_aa_cp ;
      if( k_success == ecc_test_buffer(buffer_aa) )
      {
        TRACE2(107, ecc, 0, "bit %d, buf_ecc_lp_hi bit %d, failed - error bits not detected", bit1, bit2) ;
        result = k_error ;
      }
      // reset buffers
      ecc_test_init_buffers() ;
    }

    for(bit2=0; bit2<7;bit2++)
    {
      _flpbit(buffer_55, bit1) ;
      _flpbit(buffer_aa, bit1) ;
      
      buf_ecc_lp_hi = ecc_55_lp_hi ;
      buf_ecc_lp_lo = ecc_55_lp_lo ^ (0x01<<bit2) ;
      buf_ecc_cp = ecc_55_cp ;
      if( k_success == ecc_test_buffer(buffer_55) )
      {
        TRACE2(108, ecc, 0, "bit %d, buf_ecc_lp_lo bit %d, failed - error bits not detected", bit1, bit2) ;
        result = k_error ;
      }

      buf_ecc_lp_hi = ecc_aa_lp_hi ;
      buf_ecc_lp_lo = ecc_aa_lp_lo ^ (0x01<<bit2) ;
      buf_ecc_cp = ecc_aa_cp ;
      if( k_success == ecc_test_buffer(buffer_aa) )
      {
        TRACE2(109, ecc, 0, "bit %d, buf_ecc_lp_lo bit %d, failed - error bits not detected", bit1, bit2) ;
        result = k_error ;
      }
      // reset buffers
      ecc_test_init_buffers() ;
    }

    // note:  only check cp msbits (bits 1 and 0 are unused by the algorithm)
    for(bit2=2; bit2<7;bit2++)
    {
      _flpbit(buffer_55, bit1) ;
      _flpbit(buffer_aa, bit1) ;

      buf_ecc_lp_hi = ecc_55_lp_hi ;
      buf_ecc_lp_lo = ecc_55_lp_lo ;
      buf_ecc_cp    = ecc_55_cp ^ (0x01<<bit2) ;
      if( k_success == ecc_test_buffer(buffer_55) )
      {
        TRACE2(110, ecc, 0, "bit %d, buf_ecc_cp bit %d, failed - error bits not detected", bit1, bit2) ;
        result = k_error ;
      }

      buf_ecc_lp_hi = ecc_aa_lp_hi ;
      buf_ecc_lp_lo = ecc_aa_lp_lo ;
      buf_ecc_cp    = ecc_aa_cp ^ (0x01<<bit2) ;
      if( k_success == ecc_test_buffer(buffer_aa) )
      {
        TRACE2(111, ecc, 0, "bit %d, buf_ecc_cp bit %d, failed - error bits not detected", bit1, bit2) ;
        result = k_error ;
      }
      // reset buffers
      ecc_test_init_buffers() ;
    }

    if( k_success == result )
    {
      TRACE1(112, ecc, 0, "bit %d:  all 2-bit detection combinations passed", bit1) ;
    }
  }

}

//+----------------------------------------------------------------------
//-----------------------------------------------------------------------
void ecc_test_hw_one_bit(void) reentrant
{
  TRACE0(113, ecc, 0, "ecc_hw_one_bit() not implemented yet") ;
}

//+----------------------------------------------------------------------
//-----------------------------------------------------------------------
void ecc_test_hw_two_bit(void) reentrant
{
  TRACE0(114, ecc, 0, "ecc_hw_two_bit() not implemented yet") ;
}

//+----------------------------------------------------------------------
//-----------------------------------------------------------------------
void ecc_test() reentrant 
{
  TRACE0(115, ecc, 0, "==== ECC Error Correction and Detection Test Suite ====") ;

  TRACE0(116, ecc, 0, "Test single bit error detection & correction (software)") ;
  ecc_test_sw_one_bit() ;
  TRACE0(117, ecc, 0, "---------------------------------------------------") ;
  TRACE0(118, ecc, 0, " 2-bit software ecc test") ;
  ecc_test_sw_two_bit() ;
  TRACE0(119, ecc, 0, "---------------------------------------------------") ;
  TRACE0(120, ecc, 0, " 1-bit hardware ecc test") ;
  ecc_test_hw_one_bit() ;
  TRACE0(121, ecc, 0, "---------------------------------------------------") ;
  TRACE0(122, ecc, 0, " 2-bit hardware ecc test") ;
  ecc_test_hw_two_bit() ;

  TRACE0(123, ecc, 0, "==== ECC Error Correct End Of Test ===") ;

}

⌨️ 快捷键说明

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