📄 sbl1_trulspk_crcdetection.c
字号:
/* ================================================================================================
Property of Freescale
Freescale Confidential Proprietary
Freescale Copyright (C) 2007 All rights reserved
------------------------------------------------------------------------------------------------
$RCSfile: SBL1_TRULSPK_CrcDetection.c.rca $
$Revision: 1.5 $
$Date: Sat Jul 26 12:02:15 2008 $
==================================================================================================*/
/*-------------------------------------------------------------------------------------------------*/
/**
@file SBL1_TRULSPK_CrcDetection.c
@brief C file for CRC Detection
*/
/*-------------------------------------------------------------------------------------------------*/
#include "SBL_CM_Types.h"
#include "SBL1_TRULSPK_CrcDetection.h"
#include "SBL1_SP_ErrorCodes.h"
/*------------------------------------------------------------------------------------------------*/
/*!
@fn INT32 SBL1_TRULSPK_CrcDetection(
UINT8 *ptr_in_data,
INT32 trans_blk_size,
UINT16 *ptr_crc_result,
UINT32 *ptr_lut
)
@brief This module will perform the CRC detection on packed data.
@param ptr_in_data Pointer to Input data on which CRC is performed.
@param trans_blk_size Transport Block Size
@param ptr_crc_result Pointer to the memory location for returning the CRC check.
@param ptr_lut Pointer to CRC LUT.
@return return value of the function
*/
/*-------------------------------------------------------------------------------------------------*/
UINT32 SBL1_TRULSPK_CrcDetection(
UINT8 *ptr_in_data,
INT32 trans_blk_size,
UINT16 *ptr_crc_result,
UINT32 *ptr_lut
)
{
#ifdef PACKED_BYTE_IO
UINT8 in_data; /* Input byte */
UINT8 crc_reg_high; /* Highest byte of CRC Value */
UINT32 crc_reg; /* CRC Register */
UINT32 crc_reg_rev; /* CRC Register Reversed */
UINT32 rx_crc; /* Received CRC */
UINT32 mask; /* Mask for ANDing */
UINT32 *ptr_crc_lut; /* Pointer to the CRC-24 look up table */
UINT16 no_bytes_tb_blk; /* No: of complete bytes in a TrBlk */
UINT16 remain_valid_bits; /* No: of valid bits in the last byte in a block of bytes*/
UINT16 no_crc_bytes; /* No: of bytes occupied by CRC bits*/
UINT16 loopcount_i; /* Loop variable */
#endif
#ifndef PACKED_BYTE_IO /* Unpacked Byte I/O, so use BitWise algorithm */
/* Calling Bitwise CRCCheck function */
SBL1_TRULSPK_BW_CrcDetection(ptr_in_data, trans_blk_size, ptr_crc_result);
#endif
#ifdef PACKED_BYTE_IO
/* checking the size of CRC and initializing the pointer to
the appropriate CRC look up table*/
ptr_crc_lut=&ptr_lut[0];
/* Initializing variables */
crc_reg=0;
crc_reg_rev=0;
rx_crc=0;
no_bytes_tb_blk=(UINT16)(trans_blk_size>>3);
remain_valid_bits=(UINT16)(trans_blk_size&0x7);
mask=(0xffffffff)>>(32-CRC_LEN);
no_crc_bytes=(UINT16)((((trans_blk_size+CRC_LEN-1)>>3)+1)-no_bytes_tb_blk);
for(loopcount_i=0;loopcount_i<no_bytes_tb_blk;loopcount_i++)
{
in_data=(UINT8)*ptr_in_data++;
crc_reg_high=(UINT8)(crc_reg>>(CRC_LEN-8)); /* Highest byte of CRC Reg */
/* Updating CRC Register */
crc_reg=(crc_reg<<8)^(ptr_crc_lut[crc_reg_high^in_data]);
}
/* Handling the case when the TrBlk Size is NOT a multiple of 8 */
if (remain_valid_bits>0)
{
in_data=(UINT8)*ptr_in_data; /* Copying a byte from the input
buffer and making it unsigned */
crc_reg_high=(UINT8)(crc_reg>>(CRC_LEN-8));/* Highest byte of CRC Reg */
/* Updating CRC Register */
crc_reg=(crc_reg<<(remain_valid_bits))^(ptr_crc_lut[(crc_reg_high^in_data)>>(8-remain_valid_bits)]);
}
/* Reversing the CRC Register */
for(loopcount_i=0;loopcount_i<CRC_LEN;loopcount_i++)
{
crc_reg_rev=(crc_reg_rev<<1)|((crc_reg)&0x1);
crc_reg>>=1;
}
/* Forming the received CRC value from the input byte array */
for(loopcount_i=0;loopcount_i<no_crc_bytes;loopcount_i++)
{
rx_crc<<=8;
rx_crc|= (UINT8)(*ptr_in_data++);
}
remain_valid_bits=(UINT16)((trans_blk_size+CRC_LEN)&0x7); /* checking if the CRC appended data
size is a multiple of 8 */
if(remain_valid_bits) /* It is not. So shift right to remove */
{ /* vacant positions at the LSB.*/
rx_crc>>=(8-remain_valid_bits);
}
remain_valid_bits=(UINT16)(trans_blk_size & 0x7); /* Seeing if the TrBlkSize is
a multiple of 8 */
/* The following line was removed in order to much to the encoded files
TBD - CHECK IF NECESSARY ACCORDING TO THE ENCODER */ /* IM */
/*
*ptr_in_cpy=(INT8)(*ptr_in_cpy &(0xff <<(8-remain_valid_bits)));*/ /* Setting the bits to the right of the
last data bit to zeros */
/* Checking for match between Received and calculated CRC */
if( (rx_crc^ crc_reg_rev) & mask )
{
*ptr_crc_result =(UINT16)CRC_CHK_ERROR; /* placing th result in the flag passed
as argument to the function */
}
else
{
*ptr_crc_result =(UINT16)CRC_CHK_NO_ERROR; /* placing th result in the flag passed
as argument to the function */
}
#endif /* End of #ifdef PACKED_BYTE_IO */
return(SUCCESS);
}
/*------------------------------------------------------------------------------------------------*/
/*!
@fn INT32 SBL1_TRULSPK_BW_CrcDetection(
UINT8 *ptr_in_data,
INT32 trans_blk_size,
UINT16 *ptr_crc_result,
)
@brief This module will perform the CRC detection on Unpacked data.
@param ptr_in_data Pointer to Input data on which CRC is performed.
@param trans_blk_size Transport Block Size
@param ptr_crc_result Pointer to the memory location for returning the CRC check.
Pass/Fail status.
@return return value of the function
*/
/*-------------------------------------------------------------------------------------------------*/
INT32 SBL1_TRULSPK_BW_CrcDetection(
UINT8 *ptr_in_data,
INT32 trans_blk_size,
UINT16 *ptr_crc_result
)
{
UINT32 bw_poly; /* Generator polynomial for Bitwise CRC calculation */
UINT32 crc_reg; /* CRC Register */
UINT32 rx_crc; /* Received CRC */
UINT32 xor_poly; /* Polynomial to be XORed with the
CRC Register(0 or bw_poly) */
UINT16 crc_reg_crc; /* LSB of CRC register */
UINT16 poly_flag; /* Flag for obtaining xor_poly */
INT32 loopcount_i; /* Loop variable */
/*bw_poly=CRC_GEN_POLY;*/
bw_poly=1;
/* Initializing variables */
crc_reg=0;
crc_reg_crc =0;
rx_crc =0;
/* Calculating the CRC for the Transport block */
for(loopcount_i=0;loopcount_i<trans_blk_size;loopcount_i++)
{
poly_flag=(UINT16)(ptr_in_data[loopcount_i]^crc_reg_crc); /* XORing the LSB of CRC Register and
the current data bit */
crc_reg=crc_reg>>1; /* Right shift CRC Register by one */
xor_poly=poly_flag*bw_poly; /* If poly_flag=1, CRC register is to be
XORed with bw_poly otherwise with '0' */
crc_reg=crc_reg^xor_poly;
crc_reg_crc=(UINT16)(crc_reg&0x1); /* Finding LSB of CRC Register for
next iteration */
}
/* Packing the Received CRC */
for(loopcount_i=trans_blk_size;loopcount_i<(trans_blk_size+CRC_LEN);loopcount_i++)
{
rx_crc=(rx_crc <<1) |ptr_in_data[loopcount_i];
}
/* Checking for match between calculated and received CRC */
if(crc_reg^rx_crc)
{
#ifdef RESULT_AFTER_DATA
ptr_in_data[trans_blk_size]=(INT8)CRC_CHK_ERROR; /* Error in Rx data.Placing CRC Check result in the
byte immediately following the last data byte */
#else
*ptr_crc_result =(UINT16)CRC_CHK_ERROR; /* placing the result in the flag passed
as argument to the function */
#endif
}
else
{
#ifdef RESULT_AFTER_DATA
ptr_in_data[trans_blk_size]=(INT8)CRC_CHK_NO_ERROR; /* Error in Rx data.Placing CRC Check result in the
byte immediately following the last data byte */
#else
*ptr_crc_result =(UINT16)CRC_CHK_NO_ERROR; /* placing the result in the flag passed
as argument to the function */
#endif
}
return(SUCCESS);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -