diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index 2a089980c..1aba349b7 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -69,6 +69,14 @@ unsigned int chrysler_checksum(unsigned int address, uint64_t d, int l) { return ~checksum & 0xFF; } +uint8_t mazda_checksum(uint64_t data) { + uint8_t sum = 0; + for (int i = 0; i < 8; i++) { + sum += data >> (56 - 8*i); + } + return ~sum; +} + // Static lookup table for fast computation of CRC8 poly 0x2F, aka 8H2F/AUTOSAR uint8_t crc8_lut_8h2f[256]; diff --git a/opendbc/can/common.h b/opendbc/can/common.h index 95833402e..8f6504914 100644 --- a/opendbc/can/common.h +++ b/opendbc/can/common.h @@ -19,6 +19,7 @@ unsigned int honda_checksum(unsigned int address, uint64_t d, int l); unsigned int toyota_checksum(unsigned int address, uint64_t d, int l); unsigned int subaru_checksum(unsigned int address, uint64_t d, int l); unsigned int chrysler_checksum(unsigned int address, uint64_t d, int l); +uint8_t mazda_checksum(uint64_t data); void init_crc_lookup_tables(); unsigned int volkswagen_crc(unsigned int address, uint64_t d, int l); unsigned int pedal_checksum(uint64_t d, int l); diff --git a/opendbc/can/common.pxd b/opendbc/can/common.pxd index 273529bf5..4f5930a37 100644 --- a/opendbc/can/common.pxd +++ b/opendbc/can/common.pxd @@ -20,7 +20,8 @@ cdef extern from "common_dbc.h": VOLKSWAGEN_CHECKSUM, VOLKSWAGEN_COUNTER, SUBARU_CHECKSUM, - CHRYSLER_CHECKSUM + CHRYSLER_CHECKSUM, + MAZDA_CHECKSUM cdef struct Signal: const char* name diff --git a/opendbc/can/common_dbc.h b/opendbc/can/common_dbc.h index f40b4ac42..b7e8a5563 100644 --- a/opendbc/can/common_dbc.h +++ b/opendbc/can/common_dbc.h @@ -41,6 +41,7 @@ enum SignalType { VOLKSWAGEN_COUNTER, SUBARU_CHECKSUM, CHRYSLER_CHECKSUM, + MAZDA_CHECKSUM, }; struct Signal { diff --git a/opendbc/can/dbc_template.cc b/opendbc/can/dbc_template.cc index f6d498871..175f6ce93 100644 --- a/opendbc/can/dbc_template.cc +++ b/opendbc/can/dbc_template.cc @@ -33,6 +33,8 @@ const Signal sigs_{{address}}[] = { .type = SignalType::SUBARU_CHECKSUM, {% elif checksum_type == "chrysler" and sig.name == "CHECKSUM" %} .type = SignalType::CHRYSLER_CHECKSUM, + {% elif checksum_type == "mazda" and sig.name == "CHECKSUM" %} + .type = SignalType::MAZDA_CHECKSUM, {% elif address in [512, 513] and sig.name == "CHECKSUM_PEDAL" %} .type = SignalType::PEDAL_CHECKSUM, {% elif address in [512, 513] and sig.name == "COUNTER_PEDAL" %} diff --git a/opendbc/can/packer.cc b/opendbc/can/packer.cc index 5a7312c60..96df4514d 100644 --- a/opendbc/can/packer.cc +++ b/opendbc/can/packer.cc @@ -104,6 +104,9 @@ uint64_t CANPacker::pack(uint32_t address, const std::vector &s } else if (sig.type == SignalType::CHRYSLER_CHECKSUM) { unsigned int chksm = chrysler_checksum(address, ReverseBytes(ret), message_lookup[address].size); ret = set_value(ret, sig, chksm); + } else if (sig.type == SignalType::MAZDA_CHECKSUM) { + unsigned int chksm = mazda_checksum(ret); + ret = set_value(ret, sig, chksm); } else { //WARN("CHECKSUM signal type not valid\n"); } diff --git a/opendbc/can/process_dbc.py b/opendbc/can/process_dbc.py index 04a67db95..412ecd666 100755 --- a/opendbc/can/process_dbc.py +++ b/opendbc/can/process_dbc.py @@ -61,6 +61,13 @@ def process(in_fn, out_fn): checksum_start_bit = 7 counter_start_bit = None little_endian = False + elif can_dbc.name.startswith(("mazda_")): + checksum_type = "mazda" + checksum_size = 8 + counter_size = None + checksum_start_bit = 0 + counter_start_bit = None + little_endian = True else: checksum_type = None checksum_size = None