1 /******************************************************************************
2 *
3 * Copyright (C) 2018 ST Microelectronics S.A.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *
18 ******************************************************************************/
19 #include "Iso13239CRC.h"
20
21 //************************************ Functions *******************************
22
23 /*******************************************************************************
24 **
25 ** Function computeCrc
26 **
27 ** Description Computes the 16-bit CRC of the specified function.
28 **
29 ** Parameters data - data to compute the CRC over.
30 ** len - data length.
31 **
32 ** Returns CRC of the data. -1 if something went wrong.
33 **
34 *******************************************************************************/
computeCrc(uint8_t * data,int len)35 uint16_t computeCrc(uint8_t *data, int len) {
36 uint16_t tempCrc;
37
38 tempCrc = (unsigned short)CRC_PRESET;
39
40 int i, k;
41 for (i = 0; i < len; i++) {
42 tempCrc = tempCrc ^ ((unsigned short)data[i]);
43 for (k = 0; k < 8; k++) {
44 if ((tempCrc & 0x0001) == 0x0001) {
45 tempCrc = (tempCrc >> 1) ^ CRC_POLYNOMIAL;
46 } else {
47 tempCrc = tempCrc >> 1;
48 }
49 }
50 }
51 tempCrc = ~tempCrc;
52 data[len] = (uint8_t)tempCrc;
53 data[len + 1] = (uint8_t)(tempCrc >> 8);
54
55 return tempCrc;
56 }
57