parent
32e8de134f
commit
e9da0eeed2
|
@ -0,0 +1,41 @@
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "crc8hondak.h"
|
||||||
|
|
||||||
|
static inline uint8_t crc_init(void)
|
||||||
|
{
|
||||||
|
return 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t crc_next(uint8_t crc, uint8_t data)
|
||||||
|
{
|
||||||
|
uint8_t eor;
|
||||||
|
unsigned int i = 8;
|
||||||
|
|
||||||
|
crc ^= data;
|
||||||
|
do {
|
||||||
|
/* This might produce branchless code */
|
||||||
|
eor = crc & 1 ? 0xb8 : 0;
|
||||||
|
crc >>= 1;
|
||||||
|
crc ^= eor;
|
||||||
|
} while (--i);
|
||||||
|
|
||||||
|
return crc;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t crc_final(uint8_t crc)
|
||||||
|
{
|
||||||
|
return ~crc;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t crc_hondak_calc(const uint8_t *data, size_t len)
|
||||||
|
{
|
||||||
|
uint8_t crc = crc_init();
|
||||||
|
|
||||||
|
if (len) do {
|
||||||
|
crc = crc_next(crc, *data++);
|
||||||
|
} while (--len);
|
||||||
|
|
||||||
|
return crc_final(crc);
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
/*
|
||||||
|
* crc8hondak.h
|
||||||
|
*
|
||||||
|
* Code generated by universal_crc by Danjel McGougan
|
||||||
|
*
|
||||||
|
* matches CRC8_EBU with 0xFF OutXor
|
||||||
|
*
|
||||||
|
* CRC parameters used:
|
||||||
|
* bits: 8
|
||||||
|
* poly: 0x1d
|
||||||
|
* init: 0xff
|
||||||
|
* xor: 0xff
|
||||||
|
* reverse: true
|
||||||
|
* non-direct: false
|
||||||
|
*
|
||||||
|
* CRC of the string "123456789" is 0x68
|
||||||
|
*/
|
||||||
|
|
||||||
|
uint8_t crc_hondak_calc(const uint8_t *data, size_t len);
|
|
@ -12,6 +12,7 @@ UTILSRC_CPP = \
|
||||||
$(UTIL_DIR)/math/error_accumulator.cpp \
|
$(UTIL_DIR)/math/error_accumulator.cpp \
|
||||||
$(UTIL_DIR)/math/efi_pid.cpp \
|
$(UTIL_DIR)/math/efi_pid.cpp \
|
||||||
$(UTIL_DIR)/math/interpolation.cpp \
|
$(UTIL_DIR)/math/interpolation.cpp \
|
||||||
|
$(UTIL_DIR)/math/crc8hondak.cpp \
|
||||||
$(PROJECT_DIR)/util/datalogging.cpp \
|
$(PROJECT_DIR)/util/datalogging.cpp \
|
||||||
$(PROJECT_DIR)/util/loggingcentral.cpp \
|
$(PROJECT_DIR)/util/loggingcentral.cpp \
|
||||||
$(PROJECT_DIR)/util/cli_registry.cpp \
|
$(PROJECT_DIR)/util/cli_registry.cpp \
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
#include "pch.h"
|
||||||
|
#include "crc8hondak.h"
|
||||||
|
|
||||||
|
|
||||||
|
TEST(util, crc8hondak) {
|
||||||
|
const uint8_t data[] = {0x1, 0, 0};
|
||||||
|
|
||||||
|
ASSERT_EQ(140, crc_hondak_calc(data, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://rusefi.com/forum/viewtopic.php?p=47283#p47283
|
||||||
|
TEST(util, crc8hondak_2) {
|
||||||
|
const uint8_t data[] = {0, 0xD0, 0x06, 0};
|
||||||
|
ASSERT_EQ(0x62, crc_hondak_calc(data, 4));
|
||||||
|
}
|
|
@ -3,6 +3,7 @@
|
||||||
CPPSRC += $(PROJECT_DIR)/../unit_tests/tests/util/test_buffered_writer.cpp \
|
CPPSRC += $(PROJECT_DIR)/../unit_tests/tests/util/test_buffered_writer.cpp \
|
||||||
$(PROJECT_DIR)/../unit_tests/tests/util/test_error_accumulator.cpp \
|
$(PROJECT_DIR)/../unit_tests/tests/util/test_error_accumulator.cpp \
|
||||||
$(PROJECT_DIR)/../unit_tests/tests/util/test_exp_average.cpp \
|
$(PROJECT_DIR)/../unit_tests/tests/util/test_exp_average.cpp \
|
||||||
|
$(PROJECT_DIR)/../unit_tests/tests/util/test_honda_crc.cpp \
|
||||||
$(PROJECT_DIR)/../unit_tests/tests/util/test_hash.cpp \
|
$(PROJECT_DIR)/../unit_tests/tests/util/test_hash.cpp \
|
||||||
|
|
||||||
INCDIR += $(PROJECT_DIR)/controllers/system
|
INCDIR += $(PROJECT_DIR)/controllers/system
|
||||||
|
|
Loading…
Reference in New Issue