diff --git a/openpilot/selfdrive/pandad/libpanda.a b/openpilot/selfdrive/pandad/libpanda.a index 7f55076037..7a22a2b14b 100644 Binary files a/openpilot/selfdrive/pandad/libpanda.a and b/openpilot/selfdrive/pandad/libpanda.a differ diff --git a/openpilot/selfdrive/pandad/pandad b/openpilot/selfdrive/pandad/pandad index 0598cf3ef7..b1cec8a4f5 100755 Binary files a/openpilot/selfdrive/pandad/pandad and b/openpilot/selfdrive/pandad/pandad differ diff --git a/openpilot/selfdrive/pandad/spi.cc b/openpilot/selfdrive/pandad/spi.cc index 1bb3a8a2c3..bd0e293c0d 100644 --- a/openpilot/selfdrive/pandad/spi.cc +++ b/openpilot/selfdrive/pandad/spi.cc @@ -226,7 +226,7 @@ static bool check_crc(const uint8_t *data, uint32_t len) { int PandaSpiHandle::spi_transfer_retry(uint8_t endpoint, uint8_t *tx_data, uint16_t tx_len, uint8_t *rx_data, uint16_t max_rx_len, unsigned int timeout) { // timeout of 0 means forever - const double deadline = (timeout == 0) ? INFINITY : millis_since_boot() + std::max(timeout, SPI_MIN_TIMEOUT); + double deadline = (timeout == 0) ? INFINITY : millis_since_boot() + std::max(timeout, SPI_MIN_TIMEOUT); // the id stays the same when we send the request again, so the panda never runs it twice uint32_t id = random_id(); @@ -247,6 +247,7 @@ int PandaSpiHandle::spi_transfer_retry(uint8_t endpoint, uint8_t *tx_data, uint1 if (nack_count > 3) { usleep(std::clamp(nack_count*10, 200, 2000)); } + deadline = (timeout == 0) ? INFINITY : millis_since_boot() + std::max(timeout, SPI_MIN_TIMEOUT); } else if (ret == SpiError::SILENT) { // the panda went quiet. let go of the bus for a bit, someone else might need it LOGW("SPI: panda isn't responding"); diff --git a/openpilot/selfdrive/pandad/spi.o b/openpilot/selfdrive/pandad/spi.o index 10cf0e5497..aef5cd9798 100644 Binary files a/openpilot/selfdrive/pandad/spi.o and b/openpilot/selfdrive/pandad/spi.o differ diff --git a/openpilot/selfdrive/pandad/tests/test_spi_retry.py b/openpilot/selfdrive/pandad/tests/test_spi_retry.py new file mode 100644 index 0000000000..d7611bb6b2 --- /dev/null +++ b/openpilot/selfdrive/pandad/tests/test_spi_retry.py @@ -0,0 +1,121 @@ +"""Compile the production retry loop with scripted transfer results and a clock.""" +from pathlib import Path +import shutil +import subprocess + +import pytest + + +@pytest.fixture(scope="module") +def retry_binary(tmp_path_factory): + source = (Path(__file__).resolve().parents[1] / "spi.cc").read_text() + start = source.index("int PandaSpiHandle::spi_transfer_retry(") + end = source.index("\nint PandaSpiHandle::lltransfer(", start) + method = source[start:end] + directory = tmp_path_factory.mktemp("spi-retry") + cpp = directory / "retry.cc" + cpp.write_text(r''' +#include +#include +#include +#include +#include +#include +#include +#include + +enum SpiError { NACK = -2, ACK_TIMEOUT = -3, SILENT = -4 }; +const unsigned int SPI_MIN_TIMEOUT = 100; +double now_ms = 0; +double millis_since_boot() { return now_ms; } +uint32_t random_id() { static uint32_t next = 0; return ++next; } +void fake_usleep(unsigned int us) { now_ms += us / 1000.0; } +#define usleep fake_usleep +#define SPILOG(...) ((void)0) +#define LOGW(...) ((void)0) + +struct Reply { int result; double delay_ms; bool disconnect = false; }; +class PandaSpiHandle { +public: + bool connected = true; + std::vector replies; + std::vector ids; + std::vector deadlines; + int spi_transfer(uint8_t, uint32_t id, uint8_t *, uint16_t, + uint8_t *, uint16_t, double deadline) { + ids.push_back(id); + deadlines.push_back(deadline); + if (ids.size() > replies.size()) { + std::cerr << "Unexpected extra retry\n"; + std::exit(2); + } + const auto &reply = replies[ids.size() - 1]; + now_ms += reply.delay_ms; + connected = !reply.disconnect; + return now_ms > deadline ? SpiError::ACK_TIMEOUT : reply.result; + } + int spi_transfer_retry(uint8_t, uint8_t *, uint16_t, uint8_t *, uint16_t, unsigned int); +}; + +@METHOD@ + +int main(int argc, char **argv) { + if (argc != 2) return 2; + const std::string name = argv[1]; + PandaSpiHandle handle; + unsigned int timeout = 5; + int expected = 0; + if (name == "late_ack_after_busy") { + handle.replies = {{SpiError::NACK, 80}, {0, 80}}; + } else if (name == "longer_timeout") { + timeout = 500; + handle.replies = {{SpiError::NACK, 400}, {0, 400}}; + } else if (name == "normal_reply") { + handle.replies = {{12, 10}}; + expected = 12; + } else if (name == "repeated_busy") { + handle.replies.assign(8, {SpiError::NACK, 50}); + handle.replies.push_back({0, 50}); + } else if (name == "missing_reply") { + handle.replies.assign(3, {SpiError::ACK_TIMEOUT, 50}); + expected = SpiError::ACK_TIMEOUT; + } else if (name == "invalid_reply_after_busy") { + handle.replies = {{SpiError::NACK, 80}, {SpiError::ACK_TIMEOUT, 80}, {SpiError::ACK_TIMEOUT, 80}}; + expected = SpiError::ACK_TIMEOUT; + } else if (name == "retry_same_request") { + handle.replies = {{SpiError::ACK_TIMEOUT, 40}, {0, 40}}; + } else if (name == "zero_timeout") { + timeout = 0; + handle.replies = {{SpiError::NACK, 1000}, {SpiError::NACK, 1000}, {0, 1000}}; + } else if (name == "disconnect") { + handle.replies = {{SpiError::ACK_TIMEOUT, 1, true}}; + expected = SpiError::ACK_TIMEOUT; + } else { + return 2; + } + const int result = handle.spi_transfer_retry(3, nullptr, 0, nullptr, 0, timeout); + if (result != expected || handle.ids.size() != handle.replies.size()) { + std::cerr << name << ": result " << result << ", expected " << expected + << ", attempts " << handle.ids.size() << "\n"; + return 1; + } + if (name == "late_ack_after_busy" && handle.ids[0] == handle.ids[1]) return 1; + if (name == "retry_same_request" && handle.ids[0] != handle.ids[1]) return 1; + if (name == "invalid_reply_after_busy" && handle.ids[1] != handle.ids[2]) return 1; + if (name == "zero_timeout" && !std::isinf(handle.deadlines.back())) return 1; + return 0; +} +'''.replace("@METHOD@", method)) + compiler = shutil.which("clang++") or shutil.which("g++") + assert compiler, "SPI retry regression requires the repository C++ toolchain" + binary = directory / "retry" + subprocess.run([compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror", str(cpp), "-o", str(binary)], check=True) + return binary + + +@pytest.mark.parametrize("case", [ + "late_ack_after_busy", "repeated_busy", "missing_reply", "invalid_reply_after_busy", + "retry_same_request", "zero_timeout", "disconnect", "longer_timeout", "normal_reply", +]) +def test_spi_retry_reply_deadline(retry_binary, case): + subprocess.run([str(retry_binary), case], check=True, timeout=5) diff --git a/prebuilt.json b/prebuilt.json index 5d2b4c3246..2165f1398f 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "25f85dc7128b4b2d83f38f28431100a9e15396c2ed6902ded1f81a0be872bc94", "schema_version": 1} +{"protected_inventory_sha256": "60c2ed541f431f2f22c64ab995f02028caa9ccfb2c0a8e4c8246d4b2d426fc2b", "schema_version": 1} diff --git a/tools/ci/run_host_tests.py b/tools/ci/run_host_tests.py index b312594b1a..c0cf767ecc 100644 --- a/tools/ci/run_host_tests.py +++ b/tools/ci/run_host_tests.py @@ -13,6 +13,7 @@ import tempfile ROOT = Path(__file__).resolve().parents[2] RUNNER = ROOT / 'tools/test_runner.py' EXTRA_PYTEST_FILES = ( + 'openpilot/selfdrive/pandad/tests/test_spi_retry.py', 'msgq_repo/msgq/tests/test_receive_gil.py', 'openpilot/selfdrive/car/tests/test_ford_settings_worker.py', 'openpilot/system/tests/test_sentry.py',