Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Little endian support #250

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cereal/car.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ struct CarParams {
gm @4;
hondaBosch @5;
ford @6;
tesla @6;
}

# things about the car in the manual
Expand Down
10 changes: 9 additions & 1 deletion selfdrive/boardd/boardd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#define SAFETY_ELM327 0xE327
#define SAFETY_GM 3
#define SAFETY_HONDA_BOSCH 4
#define SAFETY_FORD 5
#define SAFETY_TESLA 6
#define SAFETY_TOYOTA_NOLIMITS 0x1336
#define SAFETY_ALLOUTPUT 0x1337

Expand Down Expand Up @@ -105,6 +107,12 @@ void *safety_setter_thread(void *s) {
case (int)cereal::CarParams::SafetyModels::HONDA_BOSCH:
safety_setting = SAFETY_HONDA_BOSCH;
break;
case (int)cereal::CarParams::SafetyModels::FORD:
safety_setting = SAFETY_FORD;
break;
case (int)cereal::CarParams::SafetyModels::TESLA:
safety_setting = SAFETY_TESLA;
break;
default:
LOGE("unknown safety model: %d", safety_model);
}
Expand Down Expand Up @@ -672,4 +680,4 @@ int main() {

libusb_close(dev_handle);
libusb_exit(ctx);
}
}
1 change: 1 addition & 0 deletions selfdrive/can/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct Signal {
int b1, b2, bo;
bool is_signed;
double factor, offset;
bool is_little_endian;
SignalType type;
};

Expand Down
7 changes: 6 additions & 1 deletion selfdrive/can/dbc_template.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ namespace {
const Signal sigs_{{address}}[] = {
{% for sig in sigs %}
{
{% set b1 = (sig.start_bit//8)*8 + (-sig.start_bit-1) % 8 %}
{% if sig.is_little_endian %}
{% set b1 = sig.start_bit %}
{% else %}
{% set b1 = (sig.start_bit//8)*8 + (-sig.start_bit-1) % 8 %}
{% endif %}
.name = "{{sig.name}}",
.b1 = {{b1}},
.b2 = {{sig.size}},
.bo = {{64 - (b1 + sig.size)}},
.is_signed = {{"true" if sig.is_signed else "false"}},
.factor = {{sig.factor}},
.offset = {{sig.offset}},
.is_little_endian = {{"true" if sig.is_little_endian else "false"}},
{% if checksum_type == "honda" and sig.name == "CHECKSUM" %}
.type = SignalType::HONDA_CHECKSUM,
{% elif checksum_type == "honda" and sig.name == "COUNTER" %}
Expand Down
28 changes: 26 additions & 2 deletions selfdrive/can/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ uint64_t read_u64_be(const uint8_t* v) {
| (uint64_t)v[7]);
}

uint64_t read_u64_le(const uint8_t* v) {
return ((uint64_t)v[0]
| ((uint64_t)v[1] << 8)
| ((uint64_t)v[2] << 16)
| ((uint64_t)v[3] << 24)
| ((uint64_t)v[4] << 32)
| ((uint64_t)v[5] << 40)
| ((uint64_t)v[6] << 48)
| ((uint64_t)v[7] << 56));
}


struct MessageState {
uint32_t address;
Expand All @@ -82,8 +93,14 @@ struct MessageState {
bool parse(uint64_t sec, uint16_t ts_, uint64_t dat) {
for (int i=0; i < parse_sigs.size(); i++) {
auto& sig = parse_sigs[i];
int64_t tmp;

int64_t tmp = (dat >> sig.bo) & ((1ULL << sig.b2)-1);
if (sig.is_little_endian){
tmp = (dat >> sig.b1) & ((1ULL << sig.b2)-1);
} else {
tmp = (dat >> sig.bo) & ((1ULL << sig.b2)-1);
}

if (sig.is_signed) {
tmp -= (tmp >> (sig.b2-1)) ? (1ULL << sig.b2) : 0; //signed
}
Expand Down Expand Up @@ -220,6 +237,7 @@ class CANParser {

void UpdateCans(uint64_t sec, const capnp::List<cereal::CanData>::Reader& cans) {
int msg_count = cans.size();
uint64_t p;

DEBUG("got %d messages\n", msg_count);

Expand All @@ -240,7 +258,13 @@ class CANParser {
uint8_t dat[8] = {0};
memcpy(dat, cmsg.getDat().begin(), cmsg.getDat().size());

uint64_t p = read_u64_be(dat);
// Assumes all signals in the message are of the same type (little or big endian)
auto& sig = message_states[cmsg.getAddress()].parse_sigs[0];
if (sig.is_little_endian) {
p = read_u64_le(dat);
} else {
p = read_u64_be(dat);
}

DEBUG(" proc %X: %llx\n", cmsg.getAddress(), p);

Expand Down