Fork of the GitHub

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rfal_crc.cpp Source File

rfal_crc.cpp

00001 
00002 /******************************************************************************
00003   * @attention
00004   *
00005   * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
00006   *
00007   * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
00008   * You may not use this file except in compliance with the License.
00009   * You may obtain a copy of the License at:
00010   *
00011   *        http://www.st.com/myliberty
00012   *
00013   * Unless required by applicable law or agreed to in writing, software 
00014   * distributed under the License is distributed on an "AS IS" BASIS, 
00015   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
00016   * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
00017   * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
00018   * See the License for the specific language governing permissions and
00019   * limitations under the License.
00020   *
00021 ******************************************************************************/
00022 
00023 /*
00024  *      PROJECT:   ST25R391x firmware
00025  *      $Revision: $
00026  *      LANGUAGE:  ISO C99
00027  */
00028 
00029 /*! \file rfal_crc.c
00030  *
00031  *  \author Oliver Regenfelder
00032  *
00033  *  \brief CRC calculation implementation
00034  *
00035  */
00036 
00037 /*
00038 ******************************************************************************
00039 * INCLUDES
00040 ******************************************************************************
00041 */
00042 #include "rfal_crc.h"
00043 
00044 /*
00045 ******************************************************************************
00046 * LOCAL FUNCTION PROTOTYPES
00047 ******************************************************************************
00048 */
00049 static uint16_t rfalCrcUpdateCcitt(uint16_t crc, uint8_t dat);
00050 
00051 /*
00052 ******************************************************************************
00053 * GLOBAL FUNCTIONS
00054 ******************************************************************************
00055 */
00056 uint16_t rfalCrcCalculateCcitt(uint16_t preloadValue, const uint8_t* buf, uint16_t length)
00057 {
00058     uint16_t crc = preloadValue;
00059     uint16_t index;
00060 
00061     for (index = 0; index < length; index++)
00062     {
00063         crc = rfalCrcUpdateCcitt(crc, buf[index]);
00064     }
00065 
00066     return crc;
00067 }
00068 
00069 /*
00070 ******************************************************************************
00071 * LOCAL FUNCTIONS
00072 ******************************************************************************
00073 */
00074 static uint16_t rfalCrcUpdateCcitt(uint16_t crc, uint8_t dat)
00075 {
00076     dat ^= ((uint8_t)crc) & 0xFF;
00077     dat ^= dat << 4;
00078 
00079     crc = (crc >> 8)^(((uint16_t) dat) << 8)^(((uint16_t) dat) << 3)^(((uint16_t) dat) >> 4);
00080 
00081     return crc;
00082 }
00083