Docs: Add descriptive module docstring to protocol.pyDocs protocol header#276
Docs: Add descriptive module docstring to protocol.pyDocs protocol header#276TejasAnalyst wants to merge 5 commits intofossasia:mainfrom
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates SPI bus implementation to use burst transfer protocol constants and simplify chip select handling, adjusts SPI/UART tests to compute PWM frequencies dynamically from device instances instead of static constants, and adds a descriptive module-level docstring to the central protocol constants registry module. Sequence diagram for updated SPI 8-bit burst transfersequenceDiagram
actor User
participant SPIClient
participant SPIPrimitive
participant PSLabDevice
participant PSLabHardware
User->>SPIClient: transfer8(data)
SPIClient->>SPIPrimitive: transfer8(data)
activate SPIPrimitive
SPIPrimitive->>SPIPrimitive: _transfer(data, 8)
Note over SPIPrimitive: Selects command from
Note over SPIPrimitive: _TRANSFER_COMMANDS_MAP[8] = CP.SEND_SPI8_BURST
SPIPrimitive->>PSLabDevice: send_byte(CP.SPI_HEADER)
PSLabDevice-->>SPIPrimitive: ack
SPIPrimitive->>PSLabDevice: send_byte(CP.SEND_SPI8_BURST)
PSLabDevice-->>SPIPrimitive: ack
SPIPrimitive->>PSLabDevice: send_byte(payload_bytes)
PSLabDevice-->>SPIPrimitive: response_bytes
SPIPrimitive-->>SPIClient: data_in
SPIClient-->>User: data_in
deactivate SPIPrimitive
Note over SPIPrimitive,PSLabDevice: No explicit _start/_stop chip select
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- In
tests/test_spi.py,PWM_FERQUENCYwas removed butverify_valuenow referencesPWM_FREQUENCY, which is undefined and will break the test; either reintroduce a correctly spelled constant or compute the frequency locally where it’s used. - Some of the new comments in the tests (e.g.,
# Bot ka formula...,# 500000.0 ki jagah dynamic formula) are partially non-English and a bit informal; consider rewriting them in clear, concise English to match the rest of the codebase’s style. - The updated comments above the removed
PWM_FERQUENCY/PWM_FERQUENCYconstants in the tests still describe using a static value, but the fixtures now derive frequencies dynamically from the fixtures (spi_master,uart); it would be clearer if those comments were updated to reflect the new behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `tests/test_spi.py`, `PWM_FERQUENCY` was removed but `verify_value` now references `PWM_FREQUENCY`, which is undefined and will break the test; either reintroduce a correctly spelled constant or compute the frequency locally where it’s used.
- Some of the new comments in the tests (e.g., `# Bot ka formula...`, `# 500000.0 ki jagah dynamic formula`) are partially non-English and a bit informal; consider rewriting them in clear, concise English to match the rest of the codebase’s style.
- The updated comments above the removed `PWM_FERQUENCY`/`PWM_FERQUENCY` constants in the tests still describe using a static value, but the fixtures now derive frequencies dynamically from the fixtures (`spi_master`, `uart`); it would be clearer if those comments were updated to reflect the new behavior.
## Individual Comments
### Comment 1
<location> `pslab/bus/spi.py:217-226` </location>
<code_context>
cls._smp,
)
- def _start(self):
- """Select SPI channel to enable.
-
- Basically sets the relevant chip select pin to LOW.
-
- External ChipSelect pins:
- version < 5 : {6, 7} # RC5, RC4 (dropped support)
- version == 5 : {} (don't have any external CS pins)
- version == 6 : {7} # RC4
- """
- self._device.send_byte(CP.SPI_HEADER)
- self._device.send_byte(CP.START_SPI)
- self._device.send_byte(7) # SPI.CS v6
- # No ACK because `RESPONSE == DO_NOT_BOTHER` in firmware.
-
</code_context>
<issue_to_address>
**issue (bug_risk):** Consider the impact of removing explicit CS start/stop toggling on devices needing tight control over chip select.
With `_start`/`_stop` removed and no other explicit CS handling shown, chip-select timing now depends entirely on the new burst commands (or other firmware behavior). Some SPI devices require a CS low–high transition per logical operation. If the old API guaranteed per-call CS toggling and the new behavior keeps CS asserted across calls, this could change observable behavior for existing users. Please confirm the new CS behavior still gives appropriate transaction boundaries, or document that CS is now managed differently so callers can adjust their usage if needed.
</issue_to_address>
### Comment 2
<location> `tests/test_spi.py:78` </location>
<code_context>
):
sck_ts = sck_timestamps[smp::2]
- pwm_half_period = ((1 / PWM_FERQUENCY) * 1e6) / 2 # microsecond
+ pwm_half_period = ((1 / PWM_FREQUENCY) * 1e6) / 2 # microsecond
pattern = ""
</code_context>
<issue_to_address>
**issue (bug_risk):** PWM_FREQUENCY is undefined and will cause a NameError, breaking the SPI tests.
The previous code used `PWM_FERQUENCY = SPIMaster._frequency * 2 / 3` and referenced that constant. Now `PWM_FERQUENCY` is removed and this line uses `PWM_FREQUENCY`, which is never defined, so the tests will fail with a NameError. Please either restore a correctly named constant derived from `spi_master._frequency`, or pass the computed frequency into `verify_value` instead of relying on a global constant.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| def _start(self): | ||
| """Select SPI channel to enable. | ||
|
|
||
| Basically sets the relevant chip select pin to LOW. | ||
|
|
||
| External ChipSelect pins: | ||
| version < 5 : {6, 7} # RC5, RC4 (dropped support) | ||
| version == 5 : {} (don't have any external CS pins) | ||
| version == 6 : {7} # RC4 | ||
| """ |
There was a problem hiding this comment.
issue (bug_risk): Consider the impact of removing explicit CS start/stop toggling on devices needing tight control over chip select.
With _start/_stop removed and no other explicit CS handling shown, chip-select timing now depends entirely on the new burst commands (or other firmware behavior). Some SPI devices require a CS low–high transition per logical operation. If the old API guaranteed per-call CS toggling and the new behavior keeps CS asserted across calls, this could change observable behavior for existing users. Please confirm the new CS behavior still gives appropriate transaction boundaries, or document that CS is now managed differently so callers can adjust their usage if needed.
| ): | ||
| sck_ts = sck_timestamps[smp::2] | ||
| pwm_half_period = ((1 / PWM_FERQUENCY) * 1e6) / 2 # microsecond | ||
| pwm_half_period = ((1 / PWM_FREQUENCY) * 1e6) / 2 # microsecond |
There was a problem hiding this comment.
issue (bug_risk): PWM_FREQUENCY is undefined and will cause a NameError, breaking the SPI tests.
The previous code used PWM_FERQUENCY = SPIMaster._frequency * 2 / 3 and referenced that constant. Now PWM_FERQUENCY is removed and this line uses PWM_FREQUENCY, which is never defined, so the tests will fail with a NameError. Please either restore a correctly named constant derived from spi_master._frequency, or pass the computed frequency into verify_value instead of relying on a global constant.
This PR addresses a TODO in pslab/protocol.py by adding a professional module-level docstring. It clarifies that this module acts as a centralized registry for all byte-level communication protocol constants used to interact with the PSLab hardware.
Summary by Sourcery
Update SPI and UART communication handling and corresponding tests for dynamic configuration support.
Bug Fixes:
Enhancements:
Tests: