Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ns_crc.c
00001 /* 00002 * Copyright (c) 2015, 2017, Arm Limited and affiliates. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include "ns_crc.h" 00018 00019 //#define CRC_TEST 00020 00021 #define POLYNOMIAL_CRC15_CCIT 0x1021 00022 #define POLYNOMIAL_CRC15_ANSI 0x8005 00023 00024 uint16_t crc16_ansi(uint8_t *message, int nBytes) { 00025 return crc16_calc(message, nBytes, POLYNOMIAL_CRC15_ANSI); 00026 } 00027 uint16_t crc16_ccitt(uint8_t *message, int nBytes) { 00028 return crc16_calc(message, nBytes, POLYNOMIAL_CRC15_CCIT); 00029 } 00030 00031 uint16_t crc16_calc(uint8_t* data, uint16_t data_length, uint16_t polynomial) 00032 { 00033 int crc = 0; 00034 int i, bit; 00035 for( i=0; i<data_length; i++ ) 00036 { 00037 crc ^= (int)data[i] << 8; 00038 for (bit = 0; bit < 8; bit++) 00039 { 00040 if ( crc & 0x8000 ) { 00041 crc = (crc << 1) ^ polynomial; 00042 } else { 00043 crc <<= 1; 00044 } 00045 } 00046 } 00047 return crc&0xffff; 00048 } 00049 00050 00051 00052 #ifdef CRC_TEST //unity test 00053 #include <stdio.h> 00054 #include <string.h> 00055 //#include "unity.h" 00056 00057 #ifndef TEST_ASSERT_EQUAL_INT 00058 #define TEST_ASSERT_EQUAL_INT(a,b, s) do{ if(a!=b){printf("%s: 0x%04x != 0x%04x - FAIL\n", s, a, b);} else printf("%s: 0x%04x == 0x%04x - PASS\n", s,a,b);}while(0) 00059 #endif 00060 00061 char data[] = "arm"; 00062 /* Unity test code starts */ 00063 void setUp(void) 00064 { 00065 } 00066 00067 void tearDown(void) 00068 { 00069 } 00070 00071 void test_param_crc16_ansi(void) 00072 { 00073 uint16_t crc = crc16_ibm(data, strlen(data)); 00074 TEST_ASSERT_EQUAL_INT(crc, 0x2406, "CRC16-ansi"); 00075 } 00076 void test_param_crc16_ccitt(void) 00077 { 00078 uint16_t crc = crc16_ccitt(data, strlen(data)); 00079 TEST_ASSERT_EQUAL_INT(crc, 0x7F6B, "CRC16-CCIT"); 00080 } 00081 00082 void main(void) 00083 { 00084 test_param_crc16_ansi(); 00085 test_param_crc16_ccitt(); 00086 } 00087 00088 #endif
Generated on Tue Jul 12 2022 14:24:29 by
