Skip to content

feat: added application layer for spi bus gateway - #205

Open
IM-TechieScientist wants to merge 3 commits into
fossasia:dev26from
IM-TechieScientist:spi-scpi-gateway
Open

feat: added application layer for spi bus gateway #205
IM-TechieScientist wants to merge 3 commits into
fossasia:dev26from
IM-TechieScientist:spi-scpi-gateway

Conversation

@IM-TechieScientist

@IM-TechieScientist IM-TechieScientist commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Adds the application layer for the SPI bus gateway. Follows up on #204 and working on #203.

Commands added:

BUS:SPI:OPEN
BUS:SPI:OPEN?
BUS:SPI:CLOSe
BUS:SPI:CONFigure:BUS
BUS:SPI:CONFigure:BUS?
BUS:SPI:CONFigure:RATE
BUS:SPI:CONFigure:RATE?
BUS:SPI:CONFigure:MODE
BUS:SPI:CONFigure:MODE?
BUS:SPI:CONFigure:DUMMY
BUS:SPI:CONFigure:DUMMY?
BUS:SPI:WRITe
BUS:SPI:READ?
BUS:SPI:EXCHange?
BUS:SPI:TRANsact?

Defaults:

  • SPI1 only
  • 1 MHz
  • mode 0
  • dummy byte 0xff
  • max transfer 512 bytes

PR is stacked on #204. Actual PR size is about 500 lines. Will rebase as previous prs are merged.

@sourcery-ai

sourcery-ai Bot commented Aug 1, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds a full SPI bus gateway application layer (SCPI commands through gateway → system bus → Pico SPI LL), and introduces a digital pattern generator SCPI interface, wiring both into the common protocol, build system, and docs.

Sequence diagram for BUS:SPI:TRANsact? SPI gateway flow

sequenceDiagram
    actor Client
    participant scpi_core as scpi_t
    participant spi_protocol as scpi_cmd_bus_spi_transact_q
    participant spi_gateway as spi_gateway_transact
    participant system_spi as SPI_transact
    participant spi_ll as SPI_LL_transfer

    Client->>scpi_core: BUS:SPI:TRANsact?
    scpi_core->>spi_protocol: scpi_cmd_bus_spi_transact_q
    spi_protocol->>spi_gateway: spi_gateway_transact(tx_data, tx_len, response_buffer, read_len)
    spi_gateway->>system_spi: SPI_transact(handle, tx_data, tx_len, response_buffer, read_len)
    system_spi->>spi_ll: SPI_LL_select(bus)
    system_spi->>spi_ll: SPI_LL_transfer(bus, tx_data, NULL, tx_len, dummy_byte)
    system_spi->>spi_ll: SPI_LL_transfer(bus, NULL, response_buffer, read_len, dummy_byte)
    system_spi->>spi_ll: SPI_LL_deselect(bus)
    spi_ll-->>system_spi: bytes_read
    system_spi-->>spi_gateway: bytes_read
    spi_gateway-->>spi_protocol: bytes_read
    spi_protocol-->>scpi_core: SCPI_ResultArbitraryBlock(context, response_buffer, bytes_read)
    scpi_core-->>Client: arbitrary block response
Loading

Sequence diagram for PG:STARt digital pattern generator flow

sequenceDiagram
    actor Client
    participant scpi_core as scpi_t
    participant pg_protocol as scpi_cmd_pattern_generator_start
    participant pg_commands as pg_start
    participant pattern_generator as pattern_generator_start

    Client->>scpi_core: PG:STARt
    scpi_core->>pg_protocol: scpi_cmd_pattern_generator_start
    pg_protocol->>pg_commands: pg_start()
    pg_commands->>pattern_generator: pattern_generator_start(pg, pattern_buffer, pattern_words, mode)
    pattern_generator-->>pg_commands: result
    pg_commands-->>pg_protocol: bool
    alt [start succeeded]
        pg_protocol-->>scpi_core: SCPI_RES_OK
    else [start failed]
        pg_protocol->>scpi_core: SCPI_ErrorPush(SCPI_ERROR_EXECUTION_ERROR)
        pg_protocol-->>scpi_core: SCPI_RES_ERR
    end
    scpi_core-->>Client: command completion status
Loading

File-Level Changes

Change Details Files
Introduce low-level SPI abstraction on Pico and a hardware-independent SPI bus layer used by the gateway.
  • Add spi_ll.h/c providing SPI1-only bus selection, GPIO validation, mode/bit-order mapping, CS handling, and unified transfer API using Pico SDK
  • Implement system-level spi.h/c that wraps spi_ll, owns SPI_Handle lifetime, exposes default configs and high-level exchange/write/read/transact operations with dummy-byte handling
  • Enforce single active handle per bus and provide basic parameter/range validation for SPI configuration and transfers
src/platform/spi_ll.h
src/platform/spi_ll.c
src/system/bus/spi.h
src/system/bus/spi.c
Add SPI gateway application layer and SCPI command handlers for BUS:SPI.*.
  • Implement spi_gateway_commands API to hold mutable SPI configuration state, open/close the bus, and perform bounded transfers (up to 512 bytes) via system SPI functions
  • Expose SCPI handlers for BUS:SPI:OPEN/CLOSe, CONFigure:* and WRITe/READ?/EXCHange?/TRANsact? that validate parameters, enforce gateway-open state, push SCPI errors, and marshal arbitrary blocks
  • Add static response buffer for read/exchange/transact responses and integrate new SPI SCPI handlers into common SCPI command table
src/application/gateway/spi_commands.h
src/application/gateway/spi_commands.c
src/application/protocol/bus/spi.h
src/application/protocol/bus/spi.c
src/application/protocol/common.c
README.md
Introduce digital pattern generator application commands and SCPI protocol, with lifecycle integration into the main protocol task.
  • Add pg_commands module that tracks PG configuration/state (pins, rate, mode, pattern buffer), validates constraints, wraps system/pattern_generator APIs, and provides pg_reset_state/pg_task
  • Implement SCPI-facing pg.c that maps PG:* commands to pg_* helpers, with choice parsing for mode, arbitrary block upload for data, and textual status reporting
  • Wire PG commands and periodic pg_task into the protocol common layer and include new modules in the build
src/application/pattern_generator_commands.h
src/application/pattern_generator_commands.c
src/application/protocol/pg.c
src/application/protocol/common.c
CMakeLists.txt

Possibly linked issues

  • #(unknown): PR implements the SPI SCPI gateway and BUS:SPI commands requested, plus underlying SPI layers and documentation.
  • Implement SCPI gateway for SPI bus #203: PR introduces platform, system, and SCPI gateway layers for SPI1, fulfilling the basic SPI bus implementation issue.

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

@IM-TechieScientist
IM-TechieScientist marked this pull request as ready for review August 3, 2026 20:27
Copilot AI review requested due to automatic review settings August 3, 2026 20:27

@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.

Sorry @IM-TechieScientist, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

Copilot AI 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.

Pull request overview

Adds a new SPI bus gateway across platform → system → application layers, exposing SPI1 via SCPI commands and documenting usage/defaults. This builds on the SPI LL interface work referenced in the stacked PR (#204) and advances Issue #203 (SPI SCPI gateway).

Changes:

  • Introduces a low-level SPI platform interface (spi_ll.*) and a hardware-independent system SPI bus API (system/bus/spi.*).
  • Adds SCPI command handlers and gateway state for SPI operations (open/configure/write/read/exchange/transact).
  • Updates build configuration and README to include/describe the SPI gateway and its defaults.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/system/bus/spi.h New hardware-independent SPI bus interface API.
src/system/bus/spi.c System-layer SPI handle lifecycle + transfer helpers built on spi_ll.
src/platform/spi_ll.h New RP2040/Pico SDK-facing SPI low-level interface.
src/platform/spi_ll.c Low-level SPI1 init/deinit, CS control, and blocking transfers.
src/application/protocol/common.c Registers new SPI SCPI commands in the command table.
src/application/protocol/bus/spi.h Declares SPI SCPI command handler functions.
src/application/protocol/bus/spi.c Implements SPI SCPI command handlers backed by the gateway API.
src/application/gateway/spi_commands.h Declares SPI gateway configuration/state and transfer entrypoints.
src/application/gateway/spi_commands.c Implements SPI gateway state machine and calls into system SPI API.
README.md Documents SPI gateway defaults and available SCPI commands.
CMakeLists.txt Adds new SPI source files to the firmware build.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/platform/spi_ll.c
Comment on lines +158 to +160
gpio_init(config->cs_gpio);
gpio_set_dir(config->cs_gpio, GPIO_OUT);

Comment thread src/system/bus/spi.c
return (SPI_BitOrder)bit_order;
}

size_t SPI_get_bus_count(void) { return SPI_LL_BUS_COUNT; }
Comment on lines +52 to +56
bool spi_gateway_set_mode(uint32_t mode)
{
if (gateway_spi || mode > SPI_GATEWAY_DEFAULT_MODE + 3u) {
return false;
}
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.

2 participants