FormazioneSitael / Mbed 2 deprecated I2C_PASSED

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CRC32BIT_LOOKUP_Md0.c Source File

CRC32BIT_LOOKUP_Md0.c

00001 /* =======================================================================================
00002 * (c)
00003 * (c)                                  MEL SYSTEM S.r.l.
00004 * (c)
00005 * (c)                       Libreria VB32_Lib (Vital Board 32 Bit)
00006 * (c)
00007 * (c) La libreria VB32_Lib implementa le tutte le funzionalit� necessarie per  l'utilizzo
00008 * (c) dei due processori AT91SAM9 della scheda VB32. La libreria permette di gestire
00009 * (c) tutte  le  periferiche  interne  (DBGU, USART, SPI, ecc..) e  tutte le  periferiche
00010 * (c) esterne (HILSCHER COM-C DPM, ecc..) del processore AT91SAM9.
00011 * (c)
00012 * (c) ===================================================================================
00013 * (c)
00014 * (c)                           Copyright (C) MEL SYSTEM S.r.l.
00015 * (c)
00016 * (c)                             Tutti i diritti riservati.
00017 * (c)
00018 * (c) Questo programma e' tutelato dalle leggi sul copyright,  dalle  leggi  sui  diritti
00019 * (c) d'autore e  dai  trattati  internazionali.  La  riproduzione  o  distribuzione  non
00020 * (c) autorizzata di questo programma, o di parte di esso, sara' perseguibile  civilmente
00021 * (c) e penalmente.
00022 * (c)
00023 * =======================================================================================
00024 */
00025 
00026 /* =======================================================================================
00027  * Elenco delle modifiche (data - autore - descrizione)
00028  * =======================================================================================
00029  *
00030  * Ultima:
00031  *
00032  * 23/04/2013 - M. Giannese - creazione del modulo
00033  * =======================================================================================
00034  */
00035 
00036 /* =======================================================================================
00037 * File inclusi
00038 * =======================================================================================
00039 */
00040 #include "CRC32BIT_LOOKUP_Int.h"
00041 
00042 /* =======================================================================================
00043 * Funzioni
00044 * =======================================================================================
00045 */
00046 
00047 /* =======================================================================================
00048 * \fun Inizializza la struttura _CrcLUTable con i valori di CRC calcolati in funzione
00049 *      dei valori di crcSettings
00050 *
00051 
00052 * \par [in] crcSettings: struttura delle impostazioni per il calcolo del CRC
00053 * \val -
00054 *
00055 * \par [out] _CrcLUTable : la struttura che contiene la look up table
00056 * \val -
00057 *
00058 
00059 * =======================================================================================
00060 */
00061 
00062 ERROR_T CRC32BIT_LOOKUP_Init(CRC32BIT_LOOKUP_TS *pCRCLUTable)
00063 {
00064 ERROR_T ERROR_Value = E_NULL;
00065 UINT32_T UINT32_elementInBytes = 0;
00066 UINT32_T UINT32_bitInBytes = 0;
00067 UINT32_T UINT32_value = 0;
00068 
00069     /* Viene calcolata la look-up table */
00070     if (pCRCLUTable->UINT32_Poly != 0)
00071     {
00072         for (UINT32_elementInBytes = 0; UINT32_elementInBytes < CRC32BIT_LOOKUP_NUMELEMENTS_IN_BYTE; ++UINT32_elementInBytes)
00073         {
00074             UINT32_value = UINT32_elementInBytes << 24;
00075 
00076             for (UINT32_bitInBytes = CRC32BIT_LOOKUP_NUMBITS_IN_BYTE; UINT32_bitInBytes > 0; --UINT32_bitInBytes)
00077             {
00078                 if (UINT32_value & TOPBIT)
00079                 {
00080                     UINT32_value = (UINT32_value << 1) ^ pCRCLUTable->UINT32_Poly;
00081                 }
00082                 else
00083                 {
00084                     UINT32_value = (UINT32_value << 1);
00085                 }
00086             }
00087 
00088             pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_elementInBytes] = UINT32_value;
00089         }
00090 
00091         ERROR_Value = E_OK;
00092     }
00093     else
00094     {
00095         ERROR_Value = E_STD_INVALID_PARAM;
00096     }
00097     return ERROR_Value;
00098 }
00099 
00100 /* =======================================================================================
00101 * \fun Restituisce il CRC PROPER a 32 bit calcolato su un vettore di byte
00102 *
00103 * \par [in] vBYTE_Data: Puntatore al primo byte del vettore di cui calcolare il CRC
00104 * \val 0x00..0xFF (1) [-]
00105 *
00106 * \par [in] UINT32_DataSize: Numero di byte del vettore:
00107 * \val -
00108 *
00109 * \par [in] pCRCLUTable: look-up table
00110 * \val - [-]
00111 *
00112 * \ret il crc calcolato
00113 * =======================================================================================
00114 */
00115 UINT32_T CRC32BIT_LOOKUP_Compute(const BYTE_T vBYTE_Data[],
00116                                  const UINT32_T UINT32_DataSize,
00117                                  const CRC32BIT_LOOKUP_TS *pCRCLUTable)
00118 {
00119 UINT32_T UINT32_Crc = 0;
00120 
00121    /* si esegue il calcolo del CRC32 proper */
00122    UINT32_Crc = CRC32BIT_LOOKUP_Update(0, vBYTE_Data, UINT32_DataSize, TRUE, pCRCLUTable);
00123 
00124    return UINT32_Crc;
00125 }
00126 
00127 /* =======================================================================================
00128 * \fun Aggiorna e restituisce il CRC PROPER a 32 bit calcolato su un vettore di byte 
00129 *
00130 * \par [in] pSettings: Puntatore alla struttura delle impostazioni per il calcolo del CRC
00131 * \val -
00132 *
00133 * \par [in] UINT32_PartialCrc : Il crc da cui partire
00134 * \val -
00135 *
00136 * \par [in] vBYTE_Data: Puntatore al primo byte del vettore di cui calcolare il CRC
00137 * \val -
00138 *
00139 * \par [in] UINT32_DataSize: Numero di byte del vettore:
00140 * \val -
00141 *
00142 * \par [in] BOOL_FirstData: Indica che � il primo pacchetto di dati per cui si deve 
00143 *           utilizzare il valore di inizializzazione del CRC
00144 * \val -
00145 *
00146 * \par [in] BOOL_LastData: Indica che � l'ultimo pacchetto di dati per cui si deve
00147 *           effetture la riflessione in uscita o l'XOR finale
00148 * \val -
00149 *
00150 * \par [in] pCRCLUTable: look-up table
00151 * \val -
00152 *
00153 * \ret il crc calcolato
00154 * =======================================================================================
00155 */
00156 
00157 UINT32_T CRC32BIT_LOOKUP_Update(const UINT32_T UINT32_PartialCrc,
00158                                 const BYTE_T vBYTE_Data[], 
00159                                 const UINT32_T UINT32_DataSize,
00160                                 const BOOL_T BOOL_FirstData,
00161                                 const CRC32BIT_LOOKUP_TS *pCRCLUTable)
00162 {
00163 UINT32_T UINT32_Crc = 0;
00164 UINT32_T UINT32_Idx = 0;
00165 UINT32_T UINT32_TableIdx = 0;
00166 BYTE_T   BYTE_Value = 0;
00167 
00168    /* se si tratta del primo pacchetto dati il valore iniziale del CRC � 0xFFFFFFFF, altrimenti
00169    � quello parziale ricevuto in input */
00170    if (BOOL_FirstData == TRUE)
00171    {
00172       UINT32_Crc = pCRCLUTable->UINT32_Init;
00173    }
00174    else
00175    {
00176       UINT32_Crc = UINT32_PartialCrc;
00177    }/* Fine IF */
00178 
00179    UINT32_Idx = 0;
00180 
00181 
00182    /* per migliorare le performance si riducono il numero di confronti nel while
00183       effettuando i calcoli su blocchi di 8 byte */
00184    while ((UINT32_DataSize - UINT32_Idx) >= CRC32BIT_LOOKUP_BLOCK_SIZE)
00185    {
00186        /* verifica se nei parametri di inizializzazione � stata attivata la riflessione dei byte */
00187        if (pCRCLUTable->BOOL_RefIn == TRUE)
00188        {
00189             /* step 1 */
00190             BYTE_Value = CRC32BIT_LOOKUP_ReflectByte(vBYTE_Data[UINT32_Idx]);
00191             UINT32_TableIdx = (UINT32_T)(BYTE_Value ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00192             UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00193             UINT32_Idx = UINT32_Idx + 1;
00194 
00195             /* step 2 */
00196             BYTE_Value = CRC32BIT_LOOKUP_ReflectByte(vBYTE_Data[UINT32_Idx]);
00197             UINT32_TableIdx = (UINT32_T)(BYTE_Value ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00198             UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00199             UINT32_Idx = UINT32_Idx + 1;
00200 
00201             /* step 3 */
00202             BYTE_Value = CRC32BIT_LOOKUP_ReflectByte(vBYTE_Data[UINT32_Idx]);
00203             UINT32_TableIdx = (UINT32_T)(BYTE_Value ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00204             UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00205             UINT32_Idx = UINT32_Idx + 1;
00206 
00207             /* step 4 */
00208             BYTE_Value = CRC32BIT_LOOKUP_ReflectByte(vBYTE_Data[UINT32_Idx]);
00209             UINT32_TableIdx = (UINT32_T)(BYTE_Value ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00210             UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00211             UINT32_Idx = UINT32_Idx + 1;
00212 
00213             /* step 5 */
00214             BYTE_Value = CRC32BIT_LOOKUP_ReflectByte(vBYTE_Data[UINT32_Idx]);
00215             UINT32_TableIdx = (UINT32_T)(BYTE_Value ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00216             UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00217             UINT32_Idx = UINT32_Idx + 1;
00218 
00219             /* step 6 */
00220             BYTE_Value = CRC32BIT_LOOKUP_ReflectByte(vBYTE_Data[UINT32_Idx]);
00221             UINT32_TableIdx = (UINT32_T)(BYTE_Value ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00222             UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00223             UINT32_Idx = UINT32_Idx + 1;
00224 
00225             /* step 7 */
00226             BYTE_Value = CRC32BIT_LOOKUP_ReflectByte(vBYTE_Data[UINT32_Idx]);
00227             UINT32_TableIdx = (UINT32_T)(BYTE_Value ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00228             UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00229             UINT32_Idx = UINT32_Idx + 1;
00230 
00231             /* step 8 */
00232             BYTE_Value = CRC32BIT_LOOKUP_ReflectByte(vBYTE_Data[UINT32_Idx]);
00233             UINT32_TableIdx = (UINT32_T)(BYTE_Value ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00234             UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00235             UINT32_Idx = UINT32_Idx + 1;
00236        }
00237        else
00238        {
00239            /* step 1 */
00240           UINT32_TableIdx = (UINT32_T)(vBYTE_Data[UINT32_Idx] ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00241           UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00242           UINT32_Idx = UINT32_Idx + 1;
00243 
00244           /* step 2 */
00245           UINT32_TableIdx = (UINT32_T)(vBYTE_Data[UINT32_Idx] ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00246           UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00247           UINT32_Idx = UINT32_Idx + 1;
00248 
00249           /* step 3 */
00250           UINT32_TableIdx = (UINT32_T)(vBYTE_Data[UINT32_Idx] ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00251           UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00252           UINT32_Idx = UINT32_Idx + 1;
00253 
00254           /* step 4 */
00255           UINT32_TableIdx = (UINT32_T)(vBYTE_Data[UINT32_Idx] ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00256           UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00257           UINT32_Idx = UINT32_Idx + 1;
00258 
00259           /* step 5 */
00260           UINT32_TableIdx = (UINT32_T)(vBYTE_Data[UINT32_Idx] ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00261           UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00262           UINT32_Idx = UINT32_Idx + 1;
00263 
00264           /* step 6 */
00265           UINT32_TableIdx = (UINT32_T)(vBYTE_Data[UINT32_Idx] ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00266           UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00267           UINT32_Idx = UINT32_Idx + 1;
00268 
00269           /* step 7 */
00270           UINT32_TableIdx = (UINT32_T)(vBYTE_Data[UINT32_Idx] ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00271           UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00272           UINT32_Idx = UINT32_Idx + 1;
00273 
00274           /* step 8 */
00275           UINT32_TableIdx = (UINT32_T)(vBYTE_Data[UINT32_Idx] ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00276           UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00277           UINT32_Idx = UINT32_Idx + 1;
00278        }/* Fine IF */
00279    }/* Fine WHILE */
00280 
00281    /* si continua con i byte rimanenti */
00282    while (UINT32_Idx < UINT32_DataSize)
00283    {
00284        /* verifica se nei parametri di inizializzazione � stata attivata la riflessione dei byte */
00285        if (pCRCLUTable->BOOL_RefIn == TRUE)
00286        {
00287            BYTE_Value = CRC32BIT_LOOKUP_ReflectByte(vBYTE_Data[UINT32_Idx]);
00288            UINT32_TableIdx = (UINT32_T)(BYTE_Value ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00289            UINT32_Crc =  pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00290            UINT32_Idx = UINT32_Idx + 1;
00291        }
00292        else
00293        {
00294            UINT32_TableIdx = (UINT32_T)(vBYTE_Data[UINT32_Idx] ^ (UINT32_Crc >> CRC32BIT_LOOKUP_AUGMENTATION));
00295            UINT32_Crc = pCRCLUTable->_vDWORD_CRC32LookUpTable[UINT32_TableIdx] ^ (UINT32_Crc << CRC32BIT_LOOKUP_NUMBITS_IN_BYTE);
00296            UINT32_Idx = UINT32_Idx + 1;
00297        }
00298    }/* Fine WHILW */
00299 
00300    /* verifica se nei parametri di inizializzazione � attivata la riflessione in uscita */
00301    if (pCRCLUTable->BOOL_RefOut == TRUE)
00302    {
00303         UINT32_Crc = CRC32BIT_LOOKUP_ReflectUINT32(UINT32_Crc);
00304    }/* Fine IF */
00305 
00306    UINT32_Crc = UINT32_Crc ^ pCRCLUTable->UINT32_XorOut;
00307 
00308    return UINT32_Crc;
00309 }
00310 
00311 /* =======================================================================================
00312 * \fun Riflette i bit di un byte
00313 *
00314 * \par [in] BYTE_Value : Byte da riflettere
00315 * \val -
00316 *
00317 * \ret il byte riflesso
00318 * =======================================================================================
00319 */
00320 BYTE_T CRC32BIT_LOOKUP_ReflectByte(BYTE_T BYTE_Value)
00321 {
00322 BYTE_T   BYTE_ReflectedValue = 0;
00323 BYTE_T   BYTE_Temp           = 0;
00324 UINT16_T UINT16_NumOfShifts  = 0;
00325 BYTE_T   BYTE_Mask           = 0;
00326 UINT16_T UINT16_Idx          = 0;
00327 
00328    BYTE_ReflectedValue = 0;
00329 
00330    /* cicla su tutti i bit del byte per effettuarne la riflessione */
00331    UINT16_Idx = 0;
00332    while (UINT16_Idx < CRC32BIT_LOOKUP_NUMBITS_IN_BYTE)
00333    {
00334       BYTE_Temp = (BYTE_T) (BYTE_Value >> UINT16_Idx);
00335 
00336       if (BYTE_Temp & CRC32BIT_LOOKUP_BYTELSB_MASK)
00337       {
00338          UINT16_NumOfShifts = CRC32BIT_LOOKUP_NUMBITS_IN_BYTE - UINT16_Idx - 1;
00339 
00340          BYTE_Mask = (BYTE_T) (CRC32BIT_LOOKUP_BYTELSB_MASK << UINT16_NumOfShifts);
00341 
00342          BYTE_ReflectedValue = (BYTE_T) (BYTE_ReflectedValue ^ BYTE_Mask);
00343       }/* Fine IF */
00344 
00345       UINT16_Idx = UINT16_Idx + 1;
00346    }/* Fine WHILE */
00347 
00348    return BYTE_ReflectedValue;
00349 }
00350 
00351 /* =======================================================================================
00352 * \fun Riflette i bit di una double word
00353 *
00354 * \par [in] UINT32_Value : Double Word da riflettere
00355 * \val -
00356 *
00357 * \ret la double word riflessa
00358 * =======================================================================================
00359 */
00360 UINT32_T CRC32BIT_LOOKUP_ReflectUINT32(UINT32_T UINT32_Value)
00361 {
00362 UINT32_T  UINT32_ReflectedValue = 0;
00363 UINT32_T  UINT32_Temp       = 0;
00364 UINT32_T  UINT32_Mask       = 0;
00365 UINT16_T UINT16_Idx         = 0;
00366 UINT16_T UINT16_NumOfShifts = 0;
00367 
00368    UINT32_ReflectedValue = 0;
00369 
00370    /* cicla su tutti i bit della DWORD per effettuarne la riflessione */
00371    UINT16_Idx = 0;
00372    while (UINT16_Idx < CRC32BIT_LOOKUP_NUMBITS_UINT32)
00373    {
00374       UINT32_Temp = (UINT32_Value >> UINT16_Idx);
00375 
00376       if (UINT32_Temp & CRC32BIT_LOOKUP_UINT32LSB_MASK)
00377       {
00378          UINT16_NumOfShifts = CRC32BIT_LOOKUP_NUMBITS_UINT32 - UINT16_Idx - 1;
00379 
00380          UINT32_Mask = (UINT32_T) (CRC32BIT_LOOKUP_UINT32LSB_MASK << UINT16_NumOfShifts);
00381 
00382          UINT32_ReflectedValue = (UINT32_ReflectedValue ^ UINT32_Mask);
00383       }/* Fine IF */
00384 
00385       UINT16_Idx = UINT16_Idx + 1;
00386    }/* Fine WHILE */
00387 
00388    return UINT32_ReflectedValue;
00389 }