www.pudn.com > SMSC USB2.0.zip > test_ecc.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<