Repository navigation
Conversation
Code Review by Qodo
Context used 1. Busy-wait worst-case undocumented
|
PR Summary by QodoAdd assembly and C tutorials for the ICSSG hardware spinlock
AI Description
Merge Risk
|
|
Code review by qodo was updated up to the latest commit 035d859 |
|
Code review by qodo was updated up to the latest commit 3164531 |
Closes TexasInstruments#120. Demonstrates the PRU_ICSSG local hardware spinlock accessed over the broadside interface (XIN/XOUT), as recommended in the issue: - lock id (0-63) in R1.b0 - acquire: XIN 0x90, &R1.b3, 1 -> status in R1.b3 bit 0 (1 = acquired) - release: XOUT 0x90, &R1.b3, 1 (same id in R1.b0) Files: - spinlock.asm: C-callable spinlock_acquire()/spinlock_release(). - main.c: C example - PRU0/PRU1 contend for one lock, scope via debug GPO. - spinlock_example.asm: pure-asm example with M_SPINLOCK_ACQUIRE/RELEASE macros. - readme.md: usage, semantics, and build notes. All sources assemble/compile with clpru --silicon_version=3 (ti-cgt-pru 2.3.3); main.c builds for both PRU0 and PRU1. Co-Authored-By: Claude Opus 4.8 <[email protected]>
3164531 to
f22b51c
Compare
|
Code review by qodo was updated up to the latest commit f22b51c |
Add the required PRU assembly function metadata (pseudocode, registers modified, peak cycle count) to spinlock_acquire and spinlock_release, matching the repo convention (e.g. multicore_scheduler pwm_toggle.asm). Documents timing and register-clobber assumptions for the low-level synchronization primitive. Comments only; assembles with clpru 2.3.3. Co-Authored-By: Claude Opus 4.8 <[email protected]>
commented
Jun 26, 2026
|
Added the required PRU assembly function metadata to |
Tested on LP-AM243 (ICSSG0 PRU0/PRU1). The lock gives mutual exclusion (200000/200000 contended increments, zero overlaps), but main.c starved PRU1: it re-acquired straight after releasing, before PRU1's retry loop came round, and PRU1 never got the lock in 200 samples. main.c now waits OUTSIDE_TIME cycles after releasing, and drops the debug pin before the release so the two pins never overlap on a scope. readme: --silicon_version=4 for AM64x/AM243x PRU_ICSSG (open-pru's AM243x makefiles use PRU_VERSION 4; the asm assembles to identical code with -v3), document that an owner re-acquire also reads 1, and that the lock is not fair. Co-Authored-By: Claude Opus 5.5 <[email protected]>
commented
Oct 16, 2026
|
Tested on LP-AM243 silicon (AM2434, ICSSG0 PRU0 + PRU1). The spinlock works as described. The first run found one problem in the C example, fixed in 67c816b. Test: a CCS-scripted harness that builds the PR's files with
Found and fixed: Also in 67c816b (readme):
|
- spinlock_acquire returns bit 0 of the status only (AND, same cycle count), matching the documented 0/1 contract. Silicon returned exactly 0x00/0x01, so behaviour on LP-AM243 is unchanged. - spinlock_example.asm macros: pseudocode, registers modified, cycles. - main.c: file name in the header, no TODO marker, single spaces. - readme: the busy-wait has no upper bound. Co-Authored-By: Claude Opus 5.5 <[email protected]>
commented
Oct 16, 2026
|
Pushed ad52b00, which addresses the latest Qodo review. Each of the 7 open threads has a reply and is resolved. The earlier threads are outdated: they refer to files the PR no longer has.
Tested on hardware: the pru_spinlock test passes 25/25 on an LP-AM243 (ICSSG) with exactly this code (2026-10-16 04:30 UTC). It covers acquire, release, contention between PRU0 and PRU1 and the re-acquire case. The |
| /* Spinlock flag id (valid range 0-63). Both PRUs must use the same id. */ | ||
| #define SPINLOCK_FLAG 11 | ||
|
|
||
| /* R30 drives the PRU GPO signals; use a debug pin to scope lock ownership. */ |
There was a problem hiding this comment.
Is the vision here to have the customer write code themselves (including creating their own OpenPRU project)? If so, I would want this to be divided into a "classwork" section where the user adds code themselves with the guidance of helpful comments, and a "solutions" folder with the full buildable project. Otherwise, this should be a full project which has all the infrastructure to build from a top-level or project-level make. If there is a separate vision, let's talk, but for now I do want every OpenPRU project merged into the repo to compile from a top-level build (unless you convince me otherwise). Ok to delay this until after #178 gets merged if we want to take advantage of the additional pinmux settings that are in that pull request. Note that there will be a second pull request to add pinmux settings for both PRU0 and PRU1 by default in the near future
There was a problem hiding this comment.
The plan is a full buildable project, not classwork. I'll wait for #178 to merge so the examples can use its pinmux and drive real debug pins, then restructure. Once it's done, both examples will build from the top-level make.
| @@ -0,0 +1,89 @@ | |||
| /* | |||
| * SPDX-License-Identifier: BSD-3-Clause | |||
There was a problem hiding this comment.
After the humans align on the desired project structure (one potential option below, but this might NOT be the structure the humans agree on), please create the project structure by following docs_ai/task_create_project.md
academy
---> spinlock
-------> spinlock
-------> spinlock_c
There was a problem hiding this comment.
Agreed: full projects, using your layout:
academy
---> spinlock
-------> spinlock (assembly: spinlock_example.asm)
-------> spinlock_c (C: main.c + spinlock.asm, PRU0 and PRU1)
I'll create them with docs_ai/task_create_project.md after #178 merges. Then I'll post a scope capture of the two debug pins from the C project.
…adme, 2026 headers spinlock_acquire returns R1.b3 with MOV, as in the issue TexasInstruments#120 reference (same encoding size and cycles). Readme reduced to what the files are, a pointer to the PRU Academy spinlock page, and the three behaviours measured on LP-AM243. main.c points to the c_and_assembly lab and no longer reads as if PRU1 never gets the lock. spinlock_example.asm notes that R1.b0 must hold the lock id at release.
…ssue TexasInstruments#120 reference clpru already gives each macro expansion its own \ label (checked: two expansions in one file assemble, and each QBBC loops on its own XIN). Same machine code.
Signed-off-by: Pratheesh Gangadhar <[email protected]>
commented
Nov 20, 2026
|
The five failing Build jobs on |
…/issue-120-spinlock-examples
Review on TexasInstruments#142 asked for every merged project to build from the top-level make. Split the example into two projects that follow the gpio_toggle / gpio_toggle_c layout from TexasInstruments#178: - academy/spinlock/spinlock: firmware/spinlock_example.asm (assembly) - academy/spinlock/spinlock_c: firmware/main.c + firmware/spinlock.asm (C), PRU0 and PRU1 firmware Each builds PRU0 and PRU1 firmware plus an R5F FreeRTOS loader for am64x-evm, am243x-evm and am243x-lp, and is listed in academy/makefile and academy/readme.md. am261x/am263x/am62x are N-hw: the local spinlock is a PRU_ICSSG feature. spinlock_c debug pins: - The pinmux for both pins is in example.syscfg. The EVM loader also enables the TCA6424 I/O expander, as in TexasInstruments#178. It is guarded by PRU_GPIO_VIA_IOEXP, which only the EVM makefiles define, because the LaunchPad has no expander. - Each core now drives its own GPO4. PRU1 used GPO5, which on LP-AM243 goes to the CAN transceiver's MCAN0_STB and to no header. - The LaunchPad's PRU1 build passes -DDEBUG_PIN_SHIFT=0, because PRU1 GPO4 reaches no header there either. Its pins are BoosterPack J2.17 (PRU0 GPO4) and J2.11 (PRU1 GPO0), per SPRUJ12E. - The make build now passes -DPRU0/-DPRU1, as the CCS projectspecs already did, so main.c's per-core hold time applies there too. - Dropped the unreachable __halt() after while(1). It caused a compiler warning, and the generated code is unchanged. Built with CCS 21.0, MCU+ SDK am243x 12.01.00.33, SysConfig 1.28.0, clpru 2.3.3 and armllvm 4.0.4: - `make` in academy/ for am243x exits 0, building both projects. - `make pru` for am64x exits 0. The am64x R5F loader was not built, because no am64x MCU+ SDK is installed here. - The PRU objects match the 2026-10-16 LP-AM243 hardware run (25/25 passed) instruction for instruction. The only difference is PRU1's SET/CLR bit number in main.obj. Signed-off-by: Pratheesh Gangadhar <[email protected]>
| | am243x | FIXME | MCU PLUS SDK FIXME, OpenPRU FIXME | | ||
| | am64x | FIXME | MCU PLUS SDK FIXME, OpenPRU FIXME | |
There was a problem hiding this comment.
1. Validated hw & sw table ships unfilled fixme placeholders 📘 Rule violation ⚙ Maintainability
The Validated HW & SW table in the new spinlock_c/readme.md lists the am243x and am64x hardware as FIXME and both software entries as MCU PLUS SDK FIXME, OpenPRU FIXME, so no board revision, SDK version or toolchain version is recorded. The PR description and commit notes cite a specific validation run (LP-AM243, CCS 21.0, MCU+ SDK am243x 12.01.00.33, clpru 2.3.3), but none of that reaches the README, so a reader cannot tell which hardware revision or SDK the examples were validated on. The same placeholder table is repeated in the assembly spinlock/readme.md.
Agent Prompt
## Issue description
The `Validated HW & SW` section in `academy/spinlock/spinlock_c/readme.md` has `FIXME` placeholders for the hardware revision and for the MCU PLUS SDK and OpenPRU versions. The PR's own verification notes name the hardware and versions used.
## Fix Focus Areas
- academy/spinlock/spinlock_c/readme.md[70-73]
- academy/spinlock/spinlock/readme.md[41-44]
## Recommended Fix
Replace each `FIXME` cell with the concrete board revision (for LP-AM243 and the EVM used) and the specific MCU PLUS SDK version (for example am243x 12.01.00.33), the OpenPRU commit or tag, and the toolchain version (clpru 2.3.3). Apply the same change to the assembly readme.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
There was a problem hiding this comment.
Agreed. These are placeholders until the bench run of these exact projects on LP-AM243 finishes. That run is in progress. I'll fill in both tables with the board, MCU+ SDK, SysConfig and compiler versions in a follow-up commit.
Address the Qodo review findings on the spinlock sources: - Use lowercase instruction mnemonics, padded to the operand column (five spaces after three-letter ops, four after four-letter ops). - Name the assembly macros m_spinlock_acquire and m_spinlock_release, and use a ?-suffixed macro-local label for the acquire retry loop. - Mark the C-callable spinlock_acquire/spinlock_release with .asmfunc/.endasmfunc. - Mark the acquire busy-wait as INTENTIONALLY_UNBOUNDED_LOOP. - Document the flag_id range (0-63) at each function, and say how the peak cycle counts were obtained (hand-counted, one cycle per instruction). - Add pseudocode comments to the R5F loader functions. The PRU machine code is unchanged: spinlock.asm and spinlock_example.asm assemble to the same instruction words as before. Signed-off-by: Pratheesh Gangadhar <[email protected]>
| m_spinlock_release .macro | ||
| xout INT_SPIN_XID, &r1.b3, 1 ; release lock held in R1.b0 |
There was a problem hiding this comment.
2. Lock release may not send the lock id it is meant to free 📎 Requirement gap ≡ Correctness
The m_spinlock_release macro issues xout INT_SPIN_XID, &r1.b3, 1, so the byte transferred is the status register R1.b3 and not the lock id in R1.b0 that the comments and the spec say must be presented on release. The same operand is used in the C-callable spinlock_release in spinlock.asm, where the pseudocode says R1.b0 = flag_id then XOUT, but the code never transfers R1.b0. If the accelerator identifies the lock from the transferred byte, release targets whatever value the last XIN left in R1.b3 and may not free the lock this core holds, which would leave the other PRU spinning on a lock nobody releases. This is not checked against hardware, so the fix should be confirmed against the accelerator's XOUT operand semantics.
Agent Prompt
## Issue description
The spinlock release XOUT transfers R1.b3 (the status byte) while the documented convention presents the lock id in R1.b0. Confirm the accelerator's XOUT operand semantics and make release transfer the lock id that was acquired.
## Fix Focus Areas
- academy/spinlock/spinlock/firmware/spinlock_example.asm[58-65]
- academy/spinlock/spinlock_c/firmware/spinlock.asm[79-94]
## Recommended Fix
In `m_spinlock_release` and `spinlock_release`, make the XOUT source the register that carries the lock id (R1.b0), or document explicitly why R1.b3 is the correct operand and update the pseudocode and readme accordingly. Keep the acquire XIN using `&r1.b3` as status. Verify the release on hardware with a scope or debugger.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
There was a problem hiding this comment.
The hardware disagrees. The silicon test in this PR (LP-AM243, ICSSG0, PRU0 and PRU1 contending) used this PR's spinlock.asm from before c69b3b06, which assembles to the same instruction words: lock id 11, release as xout 0x90, &r1.b3, 1. After a successful acquire, R1.b3 holds 1. If the accelerator took the id from the transferred byte, every release would free lock 1, lock 11 would never be freed, and the other core would spin forever. Instead the lock was handed over 3,999 times, strictly alternating, with 0 overlapping holds. So the id is taken from R1.b0. 70e9b288 states this in the comments, so the operand no longer looks like a bug.
…isters Address the remaining Qodo review findings on c69b3b0: - Assert that PRUICSS_open() returned a handle before it is used, in both R5F examples. Drivers_open() returns void, so replace the misleading "check return status" comment. - State the preserved registers in the register-usage block of each acquire/release routine and macro. - Say explicitly that the release XOUT uses R1.b3 as its operand while the lock id is taken from R1.b0, so R1.b0 must still hold the id. Signed-off-by: Pratheesh Gangadhar <[email protected]>
Closes #120.
Adds HW spinlock usage examples (assembly and C) under
academy/spinlock/.Approach
Uses the PRU_ICSSG local hardware spinlock accessed over the broadside interface (
XIN/XOUT), per the recommendation in #120. The spinlock accelerator owns 64 ownership flags (lock id 0–63), reached with broadside device id0x90(= 144) for the local PRU subsystem:R1.b0.XIN 0x90, &R1.b3, 1→ acquisition status inR1.b3bit 0 (1= acquired). OneXINis one attempt; busy-wait until acquired.XOUT 0x90, &R1.b3, 1with the same lock id still inR1.b0.A spinlock provides mutual exclusion only (not a mailbox); the README covers the correct signalling pattern (guard a shared buffer + raise a system event).
Files
spinlock.asm— C-callablespinlock_acquire()/spinlock_release()built on the broadside primitives.main.c— C example: PRU0 and PRU1 contend for one lock and drive a per-core debug GPO while holding it, so ownership is observable on a scope.spinlock_example.asm— pure-assembly example usingM_SPINLOCK_ACQUIRE/M_SPINLOCK_RELEASEmacros.readme.md— semantics, usage pattern, and build notes.Validation
All sources assemble/compile with
clpru --silicon_version=3(ti-cgt-pru 2.3.3);main.cbuilds for both PRU0 (-DPRU0) and PRU1.Notes
Squashed to a single commit. An earlier revision of this PR used the SoC-level memory-mapped Spinlock module (
0x2A000000); it has been reworked to the broadside local-spinlock approach requested in the issue.