Repository navigation
Spi driver demo - #161
Spi driver demo#161a1248924 wants to merge 3 commits into
Conversation
PR Summary by QodoAdd AM261x spi_driver example: bit-banged PRU SPI master/slave demo
AI Description
Merge Risk
|
45b14ca to
c7b425c
Compare
|
Code review by qodo was updated up to the latest commit c7b425c |
c7b425c to
a278e17
Compare
|
Code review by qodo was updated up to the latest commit a278e17 |
a278e17 to
dc18a4d
Compare
|
Code review by qodo was updated up to the latest commit dc18a4d |
dc18a4d to
41a382f
Compare
|
Code review by qodo was updated up to the latest commit 41a382f |
41a382f to
394ef65
Compare
|
Code review by qodo was updated up to the latest commit 394ef65 |
394ef65 to
cbef2f2
Compare
|
Code review by qodo was updated up to the latest commit cbef2f2 |
cbef2f2 to
ff049dd
Compare
|
Code review by qodo was updated up to the latest commit ff049dd |
ff049dd to
3f78d69
Compare
|
Code review by qodo was updated up to the latest commit 3f78d69 |
|
Code review by qodo was updated up to the latest commit 2f41801 |
2f41801 to
ae7524d
Compare
|
Code review by qodo was updated up to the latest commit ae7524d |
|
/agentic_review |
|
Code review by qodo was updated up to the latest commit b797160 |
b797160 to
4861871
Compare
|
Code review by qodo was updated up to the latest commit 4861871 |
pratheesh
left a comment
There was a problem hiding this comment.
Thanks for the updates — the idle-level fix, the register-width delay loads, the edge-relative delay wording and the dead-code cleanup all look good, and I confirmed against the SDK that 225 MHz is the default ICSSM1 core clock, so your 5 MHz capture and the readme figures agree. (I'd earlier suspected 200 MHz; that was wrong for ICSSM1.)
What's left:
- Moving the SPI macros to
source/firmware/common/changesspi_loopback'smakebuild.pru_rules.maksearchessource/firmware/commonbefore the project's../../, sospi_loopbacknow picks up the commonspi_master_macros.incinstead of its own copy (I checked by adding a sentinel.emsgto the common file). That copy replaces the unrolled.loop Nwith a hardwareloop, which adds one cycle per delay, so the 12.8 MHz9, 7call inspi_loopbackgets ~2 cycles/bit slower, while the CCS projectspec still copies the old file. Please either keep the existing macro unchanged and add the register-delay version under a new name, or switchspi_loopbackto the common file in this PR and adjust/re-check its timing, then drop the duplicate copies. DebugP_scanfdoesn't report parse failures. It returns the UART read status, not thesscanfcount (DebugP_uartScanf.c:114), so the newscanf_statuscheck never fires and a non-hex line leavesinputat the previous value, which is then accepted. Resettinginputto an out-of-range value (e.g.0xFFFFFFFF) before each call would work with your existing> 0xFFFFcheck.- PRU1 still cannot resynchronise (
icss_m1_pru1_fw/main.asm,WAIT_CS_LOWis checked once and the edge waits have no exit). One missed or extra SCLK edge leaves it off by a bit until it is reloaded. Please also test CS in the edge waits (or add a bounded spin) and restart at word 0 when CS goes high. - Timeout handling. Clearing
cfg_triggeron timeout helps, but PRU0 clears it again when a late burst finishes, so a retry can still pick up the previous burst's data. A sequence number echoed by PRU0, or treating a timeout as fatal and reloading the PRUs, would close it. - Smaller items
PRU_CLK_CYC_PRD_CONST: at 225 MHz the period is 4.44 ns, so the value 5 gives fewer cycles than requested (35 ns -> 31 ns). Either use 4 or note the effective CS setup/hold, and mention in the readme that the 225 MHz comes from the SDK default for ICSSM1 (ICSSM0 defaults to 200 MHz).empty_example.c:47,70: take the DMEM bases fromPRUICSS_getAttrs()aspru_eqepdoes.- Delay range is 1–255 in the table (
readme.md:62-63) and 1–65535 in the text (:149); please pick one and clamp it. - Stale comments: MODE3/LSB in
empty_example.c:6, 25-26, 31, 54, 125andicss_m1_pru1_fw/main.asm:102; the removed 0x08/0x0C slots atempty_example.c:20-21, 29; both DMEM rows nameddelay_comp_1(:24). readme.md:42FIXME, andspi_driveris missing fromexamples/readme.md.- PR description is still empty; commit sign-offs would be easier to parse as
Signed-off-by:trailers.
|
Regarding point 1, |
7a0c3c4 to
2faf4aa
Compare
|
/agentic_review |
| .asg R4.b0, bitId | ||
| .asg R20, delay_comp1_reg | ||
| .asg R21, delay_comp2_reg |
There was a problem hiding this comment.
3. Register aliases bypass the required layout 📘 Rule violation ≡ Correctness
The PRU0 firmware binds symbolic names directly to registers with .asg, including R20, R21, and R6. Any code using these aliases bypasses the required struct-based register layout and makes register ownership harder to verify across macros.
Agent Prompt
## Issue description
The firmware creates direct `.asg` aliases whose right-hand sides are PRU registers instead of binding symbols through a `.struct` and `.sassign` layout.
## Fix Focus Areas
- examples/spi_driver/firmware/am261x-lp/icss_m1_pru0_fw/main.asm[80-98]
## Recommended Fix
Define the register allocation in a `.struct`, bind the base register with `.sassign`, and replace direct register aliases with references to the assigned struct fields.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| /* Must be called BEFORE PRUICSS_loadFirmware(PRU0) - loadFirmware resets and | ||
| * starts the core immediately, so config has to already be in DMEM0. */ | ||
| static void PRU_SPI_setConfig(const PRU_SPI_Config *cfg) | ||
| { | ||
| dmem_write32(pru_dmem0, DMEM_CFG_MODE, (uint32_t)cfg->mode); | ||
| dmem_write32(pru_dmem0, DMEM_CFG_BITORDER, (uint32_t)cfg->bitOrder); | ||
| dmem_write32(pru_dmem0, DMEM_CFG_DELAY_COMP1, (uint32_t)cfg->delay_comp_1); | ||
| dmem_write32(pru_dmem0, DMEM_CFG_DELAY_COMP2, (uint32_t)cfg->delay_comp_2); |
There was a problem hiding this comment.
5. Spi helpers lack algorithm documentation 📘 Rule violation ✧ Quality
PRU_SPI_setConfig has only a brief precondition comment and no pseudocode or algorithm section. A reader cannot obtain the function's stepwise inputs, writes, or side effects from its documentation before changing the configuration path.
Agent Prompt
## Issue description
The new configuration helper is documented only with a precondition and does not include the required language-agnostic pseudocode.
## Fix Focus Areas
- examples/spi_driver/mcuplus/empty_example.c[110-117]
## Recommended Fix
Add a clearly marked `Pseudocode:` section describing the configuration input, the four DMEM writes, and the resulting PRU startup state.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| static int PRU_SPI_runTransaction5(const uint32_t *commandWords, uint32_t *responsesOut) | ||
| { | ||
| dmem_write32(pru_dmem0, DMEM_CFG_CMD0, commandWords[0]); | ||
| dmem_write32(pru_dmem0, DMEM_CFG_CMD1, commandWords[1]); | ||
| dmem_write32(pru_dmem0, DMEM_CFG_CMD2, commandWords[2]); | ||
| dmem_write32(pru_dmem0, DMEM_CFG_CMD3, commandWords[3]); | ||
| dmem_write32(pru_dmem0, DMEM_CFG_CMD4, commandWords[4]); |
There was a problem hiding this comment.
13. Short transaction buffers can corrupt memory 📘 Rule violation ≡ Correctness
PRU_SPI_runTransaction5 dereferences both pointer parameters at five fixed indices without checking for null or verifying that each buffer contains five elements. A caller that violates the comment-only precondition causes out-of-bounds reads or writes before the transaction completes.
Agent Prompt
## Issue description
The transaction helper uses fixed indices 0 through 4 on both input and output pointers without mechanical size or null validation.
## Fix Focus Areas
- examples/spi_driver/mcuplus/empty_example.c[124-132]
- examples/spi_driver/mcuplus/empty_example.c[148-152]
## Recommended Fix
Add explicit null and length validation at the function boundary, or replace the raw pointers with a fixed-size five-element interface such as an array parameter or a structure that guarantees the required capacity; return failure before any DMEM access when validation fails.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
pratheesh
left a comment
There was a problem hiding this comment.
Most of my 10-09 points are fixed at 2faf4aa4. The macro split leaves spi_loopback unchanged, DebugP_scanf input is validated, the clock note is in, and a timeout now reloads both PRUs. Both spi_driver and both spi_loopback firmwares assemble and link with clpru 2.3.3.
Still needed:
- PRU1 can still hang inside a word. The CS check happens only between words (
icss_m1_pru1_fw/main.asm:122-130), and the edge-wait loops inspi_slave_macros.inchave no CS test or bound. One missing or extra clock edge mid-word leaves PRU1 stuck until a reload. PRU_SPI_setConfigpassesdelay_comp_1/2through unchecked. Clamp or reject anything outside the documented 1-65535.- The 5 ms
TRIGGER_POLL_TIMEOUT_US(empty_example.c:69) is shorter than a valid burst. Five 16-bit words at 65535/65535 take about 80 x 131k = 10.5M cycles, about 47 ms at 225 MHz. That burst gets reported as a timeout and reloads the PRUs. Derive the timeout from the configured delays. (This is from the arithmetic; I haven't run it.) - Minor:
readme.md:42still has a FIXME.icss_m1_pru0_fw/main.asm:88says "MODE3/LSB" for registers that all 8 paths use.- The
empty_example.cheader says "MODE3/MSB only", but PRU0 implements all 8 paths. - The commits carry "signed-off by" in the subject line instead of a
Signed-off-by:trailer.
|
regarding this comment : -> this would require changes in the working of the SPI macro itself so i think that should be in a seperate PR, can mention in readme since PRU1 in this example is emulating an encoder ? agreed with other changes, will fix |
signed-off by Ayushman <a-ayushman@ti.com>
signed off by : Ayushman <a-ayushman@ti.com>
signed-off by Ayushman <a-ayushman@ti.com>
f201699 to
f54e17b
Compare
| m_transfer_packet_spi_master_gpo_sclk_drv rx0, tx0, PACKET_SIZE, bitId, SCLK_PIN, SDI_PIN, SDO_PIN, delay_comp1_reg, delay_comp2_reg, "MODE0", "MSB" | ||
| m_transfer_packet_spi_master_gpo_sclk_drv rx1, tx1, PACKET_SIZE, bitId, SCLK_PIN, SDI_PIN, SDO_PIN, delay_comp1_reg, delay_comp2_reg, "MODE0", "MSB" | ||
| m_transfer_packet_spi_master_gpo_sclk_drv rx2, tx2, PACKET_SIZE, bitId, SCLK_PIN, SDI_PIN, SDO_PIN, delay_comp1_reg, delay_comp2_reg, "MODE0", "MSB" | ||
| m_transfer_packet_spi_master_gpo_sclk_drv rx3, tx3, PACKET_SIZE, bitId, SCLK_PIN, SDI_PIN, SDO_PIN, delay_comp1_reg, delay_comp2_reg, "MODE0", "MSB" |
There was a problem hiding this comment.
4. Long transfer-macro calls break the 128-column hard limit 📘 Rule violation ✧ Quality
The m_transfer_packet_spi_master_gpo_sclk_drv calls in DO_5X_TRANSFER_M0_MSB are about 151 characters each, and the comment above the SCLK delay definition in empty_example.c is about 152 characters; both exceed the 128-column limit. The same call length occurs in every other DO_5X_TRANSFER_* block, extending the issue across the burst section, while the limit also applies to comments in source files.
Agent Prompt
## Issue description
The 5x transfer macro calls in `main.asm` and the comment above the `SCLK` delay definition in `empty_example.c` exceed the 128-column limit.
## Fix Focus Areas
- examples/spi_driver/firmware/am261x-lp/icss_m1_pru0_fw/main.asm[405-462]
- examples/spi_driver/mcuplus/empty_example.c[216-216]
## Recommended Fix
Split each `m_transfer_packet_spi_master_gpo_sclk_drv` call across lines, or move shared arguments (`PACKET_SIZE`, `bitId`, `SCLK_PIN`, `SDI_PIN`, `SDO_PIN`, `delay_comp1_reg`, `delay_comp2_reg`) into named `.asg`/`.set` symbols defined earlier. Apply the change to every `DO_5X_TRANSFER_*` block so each line fits within 128 columns. Split the comment into two or three `/* ... */` lines of no more than 128 characters each, preserving its content.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| qbeq skip_sub_delay_1,delay_comp1_reg,0 | ||
| sub delay_comp1_reg,delay_comp1_reg,1 |
There was a problem hiding this comment.
2. Clock-delay lines use a single space after the mnemonic 📘 Rule violation ✧ Quality
The qbeq and sub instructions that compensate the delay registers are followed by only one space, but the spacing rule requires four spaces after a 4-letter mnemonic and five after a 3-letter mnemonic. This breaks the column alignment used by the rest of main.asm, which uses four spaces after qbeq and five after qba/jal.
Agent Prompt
## Issue description
In main.asm the `qbeq` and `sub` lines around the delay compensation do not use the required spacing after the mnemonic (4 spaces after 4-letter `qbeq`, 5 spaces after 3-letter `sub`).
## Fix Focus Areas
- examples/spi_driver/firmware/am261x-lp/icss_m1_pru0_fw/main.asm[116-120]
## Recommended Fix
Change `qbeq skip_sub_delay_1,...` and `qbeq skip_sub_delay_2,...` to use four spaces after `qbeq`, and change `sub delay_comp1_reg,...` and `sub delay_comp2_reg,...` to use five spaces after `sub`.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| uint32_t commandWords[5]; | ||
| uint32_t responses[5]; |
There was a problem hiding this comment.
5. Five-word transfer size repeated as bare literal 📘 Rule violation ≡ Correctness
The buffer size 5 is written as a bare literal in the array declarations commandWords[5] and responses[5], and again in the loop bounds i < 5U and i < 5U further down. Changing the burst length would require editing every one of these places consistently, and nothing ties them together.
Agent Prompt
## Issue description
The 5-word command/response burst size is repeated as a bare literal across empty_example.c (array sizes and loop bounds), so it is not defined in one place.
## Fix Focus Areas
- examples/spi_driver/mcuplus/empty_example.c[237-243]
- examples/spi_driver/mcuplus/empty_example.c[280-280]
## Recommended Fix
Define `#define PRU_SPI_WORDS_PER_BURST (5U)` near the other DMEM defines, and use it for `commandWords`, `responses`, and every loop bound that currently uses `5U`.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| #define DMEM_CFG_MODE (0x00U) | ||
| #define DMEM_CFG_BITORDER (0x04U) |
There was a problem hiding this comment.
6. Alignment spaces in defines break the spacing rule 📘 Rule violation ✧ Quality
The #define lines for the DMEM offsets use runs of several spaces to align their values, and the main.asm symbol table does the same with .set. The whitespace rule permits only single spaces in source files outside line-end markers, and the C block also leaves a whitespace-only line at line 213.
Agent Prompt
## Issue description
The new source files contain alignment runs of multiple spaces in `#define` and `.set` lines, plus a whitespace-only line in empty_example.c, which the whitespace rule forbids.
## Fix Focus Areas
- examples/spi_driver/mcuplus/empty_example.c[45-49]
- examples/spi_driver/mcuplus/empty_example.c[213-213]
- examples/spi_driver/firmware/am261x-lp/icss_m1_pru0_fw/main.asm[51-78]
## Recommended Fix
Replace each run of two or more spaces between a name and its value with a single space, and delete the whitespace-only line at empty_example.c line 213. Apply the same change to the `.set` declarations in main.asm (the indentation at the start of each line is a separate rule and can stay).
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| ; Out-of-line subroutines: 5 independent full-duplex transfers within one | ||
| ; CS-low burst, 16-bit each, one per (MODE x bit-order) combination. Reached | ||
| ; via jal (full-range jump) instead of being inlined in DO_M*_*, since 5 | ||
| ; macro expansions inline pushed every qba in the file past the 10-bit | ||
| ; PC-relative offset limit. tx0..4 must already hold the 5 command words; | ||
| ; rx0..4 receive the 5 response words independently (each transfer uses its | ||
| ; own register pair). No inter-transfer delay - back-to-back at full rate. | ||
| DO_5X_TRANSFER_M0_MSB: | ||
| m_transfer_packet_spi_master_gpo_sclk_drv rx0, tx0, PACKET_SIZE, bitId, SCLK_PIN, SDI_PIN, SDO_PIN, delay_comp1_reg, delay_comp2_reg, "MODE0", "MSB" |
There was a problem hiding this comment.
7. Burst subroutines omit the registers they clobber 📘 Rule violation ✧ Quality
The DO_5X_TRANSFER_* subroutines write rx0-rx4, invoke transfer macros that update bitId and r_dataReg, and return with jmp retAddr, but their header comment lists only the inputs and outputs. A caller that wants to know which registers a jal to these subroutines destroys has to read the macro bodies to find out.
Agent Prompt
## Issue description
The DO_5X_TRANSFER_* subroutines in main.asm have no list of registers they modify, even though they write rx0-rx4 and the macros they call write other registers.
## Fix Focus Areas
- examples/spi_driver/firmware/am261x-lp/icss_m1_pru0_fw/main.asm[398-411]
## Recommended Fix
Add a `; Modifies:` line to the comment block above DO_5X_TRANSFER_M0_MSB (and the other variants), listing rx0-rx4 and every register written by the invoked transfer macro, including bitId and any temporaries it uses. State `Modifies: none` only where that is true.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| #define DELAY_COMP_MIN (1U) | ||
| #define DELAY_COMP_MAX (0xFFFFU) |
There was a problem hiding this comment.
8. Fast spi settings corrupt demo responses 🐞 Bug ≡ Correctness
PRU_SPI_setConfig accepts delay values down to 1 without checking whether the resulting clock is within the bundled slave's practical limit. With both values set to 1, the example's clock formula gives about 18.75 MHz, above the slave macro's documented 12.8 MHz limit, so an accepted configuration can miss edges and return incorrect words.
Agent Prompt
## Issue description
The configuration validation accepts delay settings that produce an SPI clock faster than the bundled PRU1 slave's documented practical limit.
## Fix Focus Areas
- examples/spi_driver/mcuplus/empty_example.c[118-148]
- examples/spi_driver/readme.md[59-65]
## Recommended Fix
Add a validation bound for the supported demo clock, accounting for the PRU clock and macro overhead, and document it separately from the hardware-loop counter range. Reject unsupported values before starting PRU0.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| uint8_t delay_comp_1; | ||
| uint8_t delay_comp_2; | ||
| } PRU_SPI_Config; |
There was a problem hiding this comment.
3. Copied spi examples truncate delay values 🐞 Bug ⚙ Maintainability
The README's PRU_SPI_Config example still declares both delay fields as uint8_t, while the implemented fields are uint32_t and accept values through 65535. A reader copying that documented declaration into another application will truncate supported values above 255 before they reach the firmware.
Agent Prompt
## Issue description
The README declares eight-bit delay fields even though the implemented configuration accepts 32-bit fields and validates values through 65535.
## Fix Focus Areas
- examples/spi_driver/readme.md[77-95]
- examples/spi_driver/mcuplus/empty_example.c[94-100]
## Recommended Fix
Update the README configuration and helper declarations to match the C implementation, including the delay field widths, configuration return type, and current transaction timeout.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
am261x: add SPI driver for configuring and running PRU SPI macros for 5x16 bit command/response
signed-off by Ayushman a-ayushman@ti.com