Skip to content

Commit

Permalink
Initializes CRC lookup tables at module initialization (#1051)
Browse files Browse the repository at this point in the history
* Initializes CRC lookup tables at module initialization

* space

---------

Co-authored-by: Shane Smiskol <[email protected]>
  • Loading branch information
deanlee and sshane authored Jun 8, 2024
1 parent c6d0f16 commit 776bca1
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
14 changes: 9 additions & 5 deletions can/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@ void gen_crc_lookup_table_16(uint16_t poly, uint16_t crc_lut[]) {
}
}

void init_crc_lookup_tables() {
// At init time, set up static lookup tables for fast CRC computation.
gen_crc_lookup_table_8(0x2F, crc8_lut_8h2f); // CRC-8 8H2F/AUTOSAR for Volkswagen
gen_crc_lookup_table_16(0x1021, crc16_lut_xmodem); // CRC-16 XMODEM for HKG CAN FD
}
// Initializes CRC lookup tables at module initialization
struct CrcInitializer {
CrcInitializer() {
gen_crc_lookup_table_8(0x2F, crc8_lut_8h2f); // CRC-8 8H2F/AUTOSAR for Volkswagen
gen_crc_lookup_table_16(0x1021, crc16_lut_xmodem); // CRC-16 XMODEM for HKG CAN FD
}
};

static CrcInitializer crcInitializer;

unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d) {
// Volkswagen uses standard CRC8 8H2F/AUTOSAR, but they compute it with
Expand Down
2 changes: 0 additions & 2 deletions can/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#define MAX_BAD_COUNTER 5
#define CAN_INVALID_CNT 5

void init_crc_lookup_tables();

// Car specific functions
unsigned int honda_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
unsigned int toyota_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
Expand Down
1 change: 0 additions & 1 deletion can/packer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ CANPacker::CANPacker(const std::string& dbc_name) {
signal_lookup[std::make_pair(msg.address, sig.name)] = sig;
}
}
init_crc_lookup_tables();
}

std::vector<uint8_t> CANPacker::pack(uint32_t address, const std::vector<SignalPackValue> &signals) {
Expand Down
2 changes: 0 additions & 2 deletions can/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ CANParser::CANParser(int abus, const std::string& dbc_name, const std::vector<st
: bus(abus), aligned_buf(kj::heapArray<capnp::word>(1024)) {
dbc = dbc_lookup(dbc_name);
assert(dbc);
init_crc_lookup_tables();

bus_timeout_threshold = std::numeric_limits<uint64_t>::max();

Expand Down Expand Up @@ -138,7 +137,6 @@ CANParser::CANParser(int abus, const std::string& dbc_name, bool ignore_checksum

dbc = dbc_lookup(dbc_name);
assert(dbc);
init_crc_lookup_tables();

for (const auto& msg : dbc->msgs) {
MessageState state = {
Expand Down

0 comments on commit 776bca1

Please sign in to comment.