mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-06-01 16:16:54 +08:00
Merge branch 'feature/refactor_crc' into 'master'
Refactor CRC algorithm See merge request sdk/ESP8266_RTOS_SDK!614
This commit is contained in:
@ -15,6 +15,7 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "crc.h"
|
||||
#include "ibus_data.h"
|
||||
|
||||
static const uint32_t crc32_le_table[256] = {
|
||||
0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L,
|
||||
@ -113,7 +114,7 @@ uint16_t crc16_le(uint16_t crc, const uint8_t* buf, uint32_t len)
|
||||
crc = ~crc;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
crc = crc16_le_table[(crc ^ buf[i]) & 0xff] ^ (crc >> 8);
|
||||
crc = ESP_IBUS_GET_U16_DATA((crc ^ buf[i]) & 0xff, crc16_le_table) ^ (crc >> 8);
|
||||
}
|
||||
|
||||
return ~crc;
|
||||
@ -137,7 +138,7 @@ uint8_t esp_crc8(uint8_t const* p, uint32_t len)
|
||||
uint8_t crc = 0x00;
|
||||
|
||||
while (len--) {
|
||||
crc = crc8_le_table[crc ^ *p++];
|
||||
crc = ESP_IBUS_GET_U8_DATA(crc ^ *p++, crc8_le_table);
|
||||
}
|
||||
|
||||
return crc;
|
||||
|
81
components/util/test/test_crc.c
Normal file
81
components/util/test/test_crc.c
Normal file
@ -0,0 +1,81 @@
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "crc.h"
|
||||
#include "unity.h"
|
||||
|
||||
#define TEST_CRC_COUNT 4096
|
||||
#define TEST_CRC_DEBUG 1
|
||||
#define TEST_DATA_BYTES 128
|
||||
|
||||
static uint8_t s_crc_src[TEST_DATA_BYTES];
|
||||
|
||||
TEST_CASE("Test CRC8", "[CRC8]")
|
||||
{
|
||||
uint8_t crc = 0;
|
||||
uint32_t crc_time = 0;
|
||||
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
memset(s_crc_src, 11, TEST_DATA_BYTES);
|
||||
|
||||
for (int i = 0; i < TEST_CRC_COUNT; i++) {
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
crc = esp_crc8(s_crc_src, sizeof(s_crc_src));
|
||||
|
||||
crc_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_EQUAL(0xd7, crc);
|
||||
}
|
||||
|
||||
#if TEST_CRC_DEBUG
|
||||
printf("CRC8 test cost time totally %u us, once cost is about %u us\n",
|
||||
crc_time, crc_time / TEST_CRC_COUNT);
|
||||
#endif
|
||||
}
|
||||
|
||||
// no align: CRC8 test cost time totally 955287 us, once cost is about 233 us
|
||||
// align : CRC8 test cost time totally 186694 us, once cost is about 45 us
|
||||
|
||||
TEST_CASE("Test CRC16", "[CRC16]")
|
||||
{
|
||||
uint16_t crc = 0;
|
||||
uint32_t crc_time = 0;
|
||||
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
memset(s_crc_src, 11, TEST_DATA_BYTES);
|
||||
|
||||
for (int i = 0; i < TEST_CRC_COUNT; i++) {
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
crc = crc16_le(0, s_crc_src, sizeof(s_crc_src));
|
||||
|
||||
crc_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_EQUAL(0x495d, crc);
|
||||
}
|
||||
|
||||
#if TEST_CRC_DEBUG
|
||||
printf("CRC16 test cost time totally %u us, once cost is about %u us\n",
|
||||
crc_time, crc_time / TEST_CRC_COUNT);
|
||||
#endif
|
||||
}
|
||||
|
||||
// no align: CRC16 test cost time totally 1114398 us, once cost is about 272 us
|
||||
// align : CRC16 test cost time totally 200268 us, once cost is about 48 us
|
Reference in New Issue
Block a user