Skip to content

fix: read SPI response words in bytes - #309

Open
YashAnand69 wants to merge 1 commit into
fossasia:mainfrom
YashAnand69:fix/spi-response-word-bytes
Open

YashAnand69 wants to merge 1 commit into
fossasia:mainfrom
YashAnand69:fix/spi-response-word-bytes

Conversation

@YashAnand69

@YashAnand69 YashAnand69 commented Oct 9, 2026 •

Copy link
Copy Markdown

_transfer passes an 8- or 16-bit width to ConnectionHandler.read, whose argument is a byte count. Firmware returns one or two response bytes followed by an ACK. The oversized read consumes the ACK and fails unpacking instead of completing the transfer.

Read the packed integer's byte size, then consume the ACK separately. This fixes scalar and bulk read/write/transfer operations without changing commands or public APIs. It is separate from #275's firmware-v3 command migration; that patch retains this read length.

Validation:

  • Fourteen public SPISlave regressions fail on unchanged main. A byte-oriented response stream checks exact response lengths, ACK boundaries, sequential replies and command framing.
  • Available hardware-independent suite: 42 passed (baseline 28); changed line covered and same-subset coverage increases. Configured Black, Flake8, Bandit and pydocstyle checks pass.
  • Wheel/source distribution and documentation builds pass. Documentation diagnostics match baseline: five distinct messages, Sphinx counter seven.
  • No physical PSLab or full hardware record/playback validation was performed; byte-stream tests exercise the existing protocol contract.

No overlapping response-length fix found in open issues/PRs. Developed and tested with OpenAI Codex assistance.

Summary by Sourcery

Fix SPI transfers to preserve response words and acknowledgements as separate protocol frames.

Bug Fixes:

  • Correct SPI response framing by reading the packed response word's byte length and consuming acknowledgements separately.

Tests:

  • Add byte-oriented SPI regression coverage for scalar and bulk 8-bit and 16-bit operations, including sequential transfers and command framing.

Summary by CodeRabbit

  • Bug Fixes

    • Corrected SPI response handling so 8-bit and 16-bit operations read the expected number of bytes, preventing response data from being misframed.
  • Tests

    • Added coverage for single and bulk SPI transfers, reads, and writes, including sequential operations and response framing.

@sourcery-ai

sourcery-ai Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Fixes SPI response framing by reading exactly the packed response word size before consuming the ACK, preventing oversized reads from consuming acknowledgement bytes. Adds transport-level regressions covering 8-/16-bit scalar and bulk operations, command framing, and sequential transfers without changing the public API.

Sequence diagram for SPI response framing

sequenceDiagram
    participant SPI as SPISlave
    participant Device as ConnectionHandler
    participant Firmware

    SPI->>Device: send_byte(CP.SPI_HEADER)
    SPI->>Device: send_byte(command)
    SPI->>Device: write(interger_type.pack(data))
    Device->>Firmware: transmit command and packed data
    Firmware-->>Device: response bytes
    Device-->>SPI: read(interger_type.size)
    Device-->>SPI: get_ack()
    SPI-->>SPI: interger_type.unpack(response)
Loading

File-Level Changes

Change Details Files
Read SPI response payloads using their packed byte size and consume acknowledgements independently.
  • Replace the bit-width read length with the packed integer type size.
  • Preserve existing command sequencing and public APIs while fixing scalar and bulk operations.
pslab/bus/spi.py
Add byte-stream regression coverage for response and ACK framing across SPI operations.
  • Exercise 8-bit and 16-bit scalar and bulk transfer, read, and write paths.
  • Assert exact read sizes, command framing, transmitted payloads, ACK boundaries, and sequential reply preservation.
tests/test_spi_response_framing.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e79cb181-acc1-4191-81a9-924394fe8fba

📥 Commits

Reviewing files that changed from the base of the PR and between 08de0d0 and 187c9f5.


📒 Files selected for processing (2)
  • pslab/bus/spi.py
  • tests/test_spi_response_framing.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.



📝 Walkthrough

Walkthrough

SPI transfers now read response words using the selected integer type’s byte size. Tests cover 8-bit and 16-bit operations, framing, and response exhaustion.

Changes

SPI response framing

Layer / File(s) Summary
Transfer response reads and framing tests
pslab/bus/spi.py, tests/test_spi_response_framing.py
_transfer reads the selected integer type’s byte size. Tests check read sizes, command framing, results, and response exhaustion for single, bulk, and sequential transfers.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix


Merge Risk: ⚪ Minimal · up to 187c9

SPI response reads now follow the selected value’s byte width, while acknowledgements remain separate. The supplied test coverage checks framing and response exhaustion across the supported widths; no actionable merge-blocking risk is identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: reading SPI response words in bytes.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.


  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR


  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant