Skip to content

Commit

Permalink
Merge pull request #114 from diamondman/new_panda_code
Browse files Browse the repository at this point in the history
Pulled in new panda firmware and updated boardd to support the changes.
  • Loading branch information
geohot authored Jul 12, 2017
2 parents 28b00c1 + 8162b08 commit 753f81a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cereal/log.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ struct CanData {
address @0 :UInt32;
busTime @1 :UInt16;
dat @2 :Data;
src @3 :Int8;
src @3 :UInt8;
}

struct ThermalData {
Expand Down
2 changes: 1 addition & 1 deletion panda
Submodule panda updated 59 files
+5 −1 .gitignore
+23 −8 README.md
+30 −10 TODO
+3 −0 __init__.py
+0 −1 board/.gitignore
+1 −3 board/Makefile
+1 −1 board/Makefile.legacy
+0 −0 board/__init__.py
+0 −1 board/adc.h
+0 −1 board/bootstub.c
+37 −37 board/build.mk
+487 −0 board/can.c
+85 −117 board/can.h
+21 −0 board/config.h
+0 −1 board/dac.h
+122 −0 board/early.c
+14 −98 board/early.h
+2 −1 board/get_sdk.sh
+144 −0 board/gpio.c
+5 −251 board/gpio.h
+66 −0 board/libc.c
+8 −69 board/libc.h
+37 −0 board/llgpio.c
+23 −0 board/llgpio.h
+121 −462 board/main.c
+0 −13 board/panda_safety.h
+39 −0 board/rev.h
+96 −0 board/safety.c
+27 −0 board/safety.h
+18 −7 board/safety_honda.h
+51 −0 board/spi.c
+6 −44 board/spi.h
+2 −0 board/spi_flasher.h
+0 −1 board/timer.h
+26 −20 board/tools/enter_download_mode.py
+209 −0 board/uart.c
+47 −69 board/uart.h
+661 −0 board/usb.c
+50 −541 board/usb.h
+14 −3 boardesp/proxy.c
+12 −0 boardesp/tools/esptool.py
+2 −1 boardesp/webserver.c
+ buy.png
+6 −0 drivers/linux/.gitignore
+6 −0 drivers/linux/Makefile
+618 −0 drivers/linux/panda.c
+2 −0 drivers/linux/test/Makefile
+120 −0 drivers/linux/test/main.c
+4 −0 drivers/linux/test/run.sh
+0 −0 lib/__init__.py
+86 −65 panda/__init__.py
+2 −0 setup.cfg
+63 −0 setup.py
+0 −0 tests/__init__.py
+37 −0 tests/can_printer.py
+12 −7 tests/debug_console.py
+78 −47 tests/loopback_test.py
+11 −9 tests/standalone_test.py
+18 −14 tests/throughput_test.py
40 changes: 31 additions & 9 deletions selfdrive/boardd/boardd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ pthread_mutex_t usb_lock;

bool spoofing_started = false;
bool fake_send = false;
bool loopback_can = false;

// double the FIFO size
#define RECV_SIZE (0x1000)
#define TIMEOUT 0

#define SAFETY_NOOUTPUT 0x0000
#define SAFETY_HONDA 0x0001
#define SAFETY_ALLOUTPUT 0x1337

bool usb_connect() {
int err;

Expand All @@ -48,22 +53,36 @@ bool usb_connect() {
err = libusb_claim_interface(dev_handle, 0);
if (err != 0) { return false; }

if(loopback_can) {
libusb_control_transfer(dev_handle, 0xc0, 0xe5, 1, 0, NULL, 0, TIMEOUT);
}

// power off ESP
libusb_control_transfer(dev_handle, 0xc0, 0xd9, 0, 0, NULL, 0, TIMEOUT);

// forward CAN1 to CAN3...soon
//libusb_control_transfer(dev_handle, 0xc0, 0xdd, 1, 2, NULL, 0, TIMEOUT);

// set UART modes for Honda Accord
for (int uart = 2; uart <= 3; uart++) {
// 9600 baud
libusb_control_transfer(dev_handle, 0xc0, 0xe1, uart, 9600, NULL, 0, TIMEOUT);
libusb_control_transfer(dev_handle, 0x40, 0xe1, uart, 9600, NULL, 0, TIMEOUT);
// even parity
libusb_control_transfer(dev_handle, 0xc0, 0xe2, uart, 1, NULL, 0, TIMEOUT);
libusb_control_transfer(dev_handle, 0x40, 0xe2, uart, 1, NULL, 0, TIMEOUT);
// callback 1
libusb_control_transfer(dev_handle, 0xc0, 0xe3, uart, 1, NULL, 0, TIMEOUT);
libusb_control_transfer(dev_handle, 0x40, 0xe3, uart, 1, NULL, 0, TIMEOUT);
}

// TODO: Boardd should be able to set the baud rate
int baud = 500000;
libusb_control_transfer(dev_handle, 0x40, 0xde, 0, 0,
(unsigned char *)&baud, sizeof(baud), TIMEOUT); // CAN1
libusb_control_transfer(dev_handle, 0x40, 0xde, 1, 0,
(unsigned char *)&baud, sizeof(baud), TIMEOUT); // CAN2

// TODO: Boardd should be able to be told which safety model to use
libusb_control_transfer(dev_handle, 0x40, 0xdc, SAFETY_HONDA, 0, NULL, 0, TIMEOUT);

return true;
}

Expand All @@ -90,7 +109,7 @@ void can_recv(void *s) {

// do recv
pthread_mutex_lock(&usb_lock);

do {
err = libusb_bulk_transfer(dev_handle, 0x81, (uint8_t*)data, RECV_SIZE, &recv, TIMEOUT);
if (err != 0) { handle_usb_issue(err, __func__); }
Expand Down Expand Up @@ -127,13 +146,13 @@ void can_recv(void *s) {
canData[i].setBusTime(data[i*4+1] >> 16);
int len = data[i*4+1]&0xF;
canData[i].setDat(kj::arrayPtr((uint8_t*)&data[i*4+2], len));
canData[i].setSrc((data[i*4+1] >> 4) & 0xf);
canData[i].setSrc((data[i*4+1] >> 4) & 0xff);
}

// send to can
auto words = capnp::messageToFlatArray(msg);
auto bytes = words.asBytes();
zmq_send(s, bytes.begin(), bytes.size(), 0);
zmq_send(s, bytes.begin(), bytes.size(), 0);
}

void can_health(void *s) {
Expand Down Expand Up @@ -181,7 +200,7 @@ void can_health(void *s) {
// send to health
auto words = capnp::messageToFlatArray(msg);
auto bytes = words.asBytes();
zmq_send(s, bytes.begin(), bytes.size(), 0);
zmq_send(s, bytes.begin(), bytes.size(), 0);
}


Expand Down Expand Up @@ -316,6 +335,10 @@ int main() {
fake_send = true;
}

if(getenv("BOARDD_LOOPBACK")){
loopback_can = true;
}

// init libusb
err = libusb_init(&ctx);
assert(err == 0);
Expand Down Expand Up @@ -357,4 +380,3 @@ int main() {
libusb_close(dev_handle);
libusb_exit(ctx);
}

6 changes: 5 additions & 1 deletion selfdrive/boardd/boardd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

# TODO: rewrite in C to save CPU

SAFETY_NOOUTPUT = 0
SAFETY_HONDA = 1
SAFETY_ALLOUTPUT = 0x1337

# *** serialization functions ***
def can_list_to_can_capnp(can_msgs, msgtype='can'):
dat = messaging.new_message()
Expand Down Expand Up @@ -94,6 +98,7 @@ def can_init():
if device.getVendorID() == 0xbbaa and device.getProductID() == 0xddcc:
handle = device.open()
handle.claimInterface(0)
handle.controlWrite(0x40, 0xdc, SAFETY_HONDA, 0, b'')

if handle is None:
print "CAN NOT FOUND"
Expand Down Expand Up @@ -190,4 +195,3 @@ def main(gctx=None):

if __name__ == "__main__":
main()

49 changes: 49 additions & 0 deletions selfdrive/boardd/test_boardd_loopback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Run boardd with the BOARDD_LOOPBACK envvar before running this test."""

import os
import random
import zmq
import time

from selfdrive.boardd.boardd import can_list_to_can_capnp
from selfdrive.messaging import drain_sock, pub_sock, sub_sock
from selfdrive.services import service_list

def get_test_string():
return b"test"+os.urandom(10)

BUS = 0

def main():
context = zmq.Context()

rcv = sub_sock(context, service_list['can'].port) # port 8006
snd = pub_sock(context, service_list['sendcan'].port) # port 8017
time.sleep(0.3) # wait to bind before send/recv

for _ in range(10):
at = random.randint(1024, 2000)
st = get_test_string()[0:8]
snd.send(can_list_to_can_capnp([[at, 0, st, 0]], msgtype='sendcan').to_bytes())
time.sleep(0.1)
res = drain_sock(rcv, True)
assert len(res) == 1

res = res[0].can
assert len(res) == 2

msg0, msg1 = res

assert msg0.dat == st
assert msg1.dat == st

assert msg0.address == at
assert msg1.address == at

assert msg0.src == 0x80 | BUS
assert msg1.src == BUS

print("Success")

if __name__ == "__main__":
main()

0 comments on commit 753f81a

Please sign in to comment.