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

Refactor communication and protocol #180

Merged
merged 52 commits into from
Mar 14, 2025
Merged

Conversation

bessman
Copy link
Collaborator

@bessman bessman commented Sep 13, 2024

Rework communication to happen in a single place: src/transport/packet_handler.c

New protocol has two main differences from the old one:

  1. Every exchange starts with a header which says how many bytes will be sent.
  2. Acknowledgement is mandatory.

Remaining modules to update to new protocol:

  • I2C
  • SPI
  • UART
  • buffer
  • debug
  • device
  • interval
  • light
  • rtc
  • logicanalyzer
  • multimeter
  • oscilloscope
  • powersource
  • sensors
  • wavegenerator
  • ctmu
  • pin_manager
  • system
  • sdcard
  • commands

@bessman bessman deleted the branch fossasia:develop January 12, 2025 15:27
@bessman bessman closed this Jan 12, 2025
@bessman bessman reopened this Jan 12, 2025
@bessman bessman marked this pull request as ready for review February 28, 2025 13:33
Copy link
Contributor

sourcery-ai bot commented Feb 28, 2025

Reviewer's Guide by Sourcery

This pull request refactors the communication protocol and command structure of the PSLab firmware. It introduces a new packet handler for managing communication between the PSLab and the host, and updates all instruments and helper functions to use the new protocol. The new protocol includes a header with command code and payload size, mandatory acknowledgements, and a single entry point for communication. The changes also include input validation, status return values, and removal of debug messages.

Sequence diagram for sending a command to the PSLab device

sequenceDiagram
    participant Host
    participant PacketHandler
    participant Instrument

    Host->>PacketHandler: Send command with header (command code, payload size) and payload
    activate PacketHandler
    PacketHandler->>PacketHandler: Validate header and payload size
    alt Validation fails
        PacketHandler-->>Host: Send error acknowledgement
    else Validation succeeds
        PacketHandler->>Instrument: Call instrument function with payload
        activate Instrument
        Instrument-->>PacketHandler: Return status and response data
        deactivate Instrument
        PacketHandler->>PacketHandler: Prepare response packet
        PacketHandler-->>Host: Send acknowledgement with response data
    end
    deactivate PacketHandler
Loading

File-Level Changes

Change Details Files
Refactored the wave generator instrument to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
  • Added a downsample function to generate short waveform tables from full waveform tables.
  • Changed the sine table definitions to use WAVE_TABLE_FULL_LENGTH and WAVE_TABLE_SHORT_LENGTH.
src/instruments/wavegenerator.c
src/instruments/wavegenerator.h
Refactored the I2C bus communication to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
  • Removed the I2C_BulkWrite and I2C_BulkRead functions.
  • Added helper functions for I2C communication, such as I2C_StartSignal, I2C_StopSignal, I2C_Transmit, and I2C_Receive.
src/bus/i2c/i2c.c
src/bus/i2c/i2c.h
Refactored the UART bus communication to use a new command structure and data handling.
  • Removed interrupt handlers for UART1 and UART2.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
  • Added functions for setting parity, stop bits, and baud rate.
  • Added functions for reading and writing data to the UART bus.
  • Removed passthrough mode functionality.
src/bus/uart/uart.c
src/bus/uart/uart.h
Refactored the SD card communication to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
  • Added functions for mounting, unmounting, opening, closing, reading, and writing files.
src/sdcard/sdcard.c
src/sdcard/sdcard.h
Refactored the oscilloscope instrument to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/instruments/oscilloscope.c
src/instruments/oscilloscope.h
Refactored the logic analyzer instrument to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/instruments/logicanalyzer.c
src/instruments/logicanalyzer.h
Refactored the multimeter instrument to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/instruments/multimeter.c
src/instruments/multimeter.h
Refactored the power source instrument to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/instruments/powersource.c
Refactored the buffer helper functions to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/helpers/buffer.c
src/helpers/buffer.h
Refactored the RTC helper functions to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/helpers/rtc.c
src/helpers/rtc.h
Refactored the device helper functions to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/helpers/device.c
src/helpers/device.h
Refactored the pin manager helper functions to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/registers/system/pin_manager.c
src/registers/system/pin_manager.h
Refactored the sensors helper functions to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/instruments/sensors.c
Refactored the CTMU register functions to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/registers/converters/ctmu.c
src/registers/converters/ctmu.h
Refactored the light helper functions to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/helpers/light.c
Refactored the states to use a new command structure and data handling.
  • Replaced individual UART read calls with a single args array for command parameters.
  • Added status return values to all functions.
  • Removed UART write calls and instead return data via the rets array.
  • Added input validation to check the size of the args array.
  • Replaced response_t with enum Status.
src/states.c
Implemented a new packet handler for communication between the PSLab and the host.
  • Created a new packet handler for receiving commands and sending responses.
  • Implemented functions for reading and writing data to the host.
  • Implemented a function for exchanging data between the PSLab and the host.
  • Added a header structure for command and response packets.
  • Added a function for getting a command function from a command code.
  • Added a function for flushing the UART receive buffer.
src/transport/packet_handler.c
src/transport/packet_handler.h
src/transport/host.c
src/transport/host.h
src/transport/uart2.c
src/transport/uart2.h
Removed debug messages.
  • Removed debug messages from the SD card driver.
  • Removed debug messages from the common commands.
  • Removed debug files.
src/sdcard/sdcard.c
src/commands.c
src/helpers/CMakeLists.txt
src/helpers/debug.c
src/helpers/debug.h

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

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

Here's what I looked at during the review
  • 🟡 General issues: 5 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 5 issues found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@bessman
Copy link
Collaborator Author

bessman commented Feb 28, 2025

This is now theoretically finished. It needs to be thoroughly tested, and even when everything works as intended the communication is of course completely incompatible with the current pslab-python & pslab-android. They will need to be updated to use the new protocol, and then new releases need to be synchronized.

@bessman
Copy link
Collaborator Author

bessman commented Mar 14, 2025

This requires further testing, but I need it for tomorrow's workshop so here goes.

@bessman bessman merged commit 5cb9ab4 into fossasia:develop Mar 14, 2025
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