-
-
Notifications
You must be signed in to change notification settings - Fork 112
feat: take the SPI bus lock around our own transfers #356
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #pragma once | ||
|
|
||
| /** | ||
| * @brief Host-provided mutual exclusion for an SPI bus shared with other peripherals. | ||
| * | ||
| * On boards where the display and/or SD card sit on the same SPI bus as something the | ||
| * host drives itself - a LoRa radio, say - the host owns the arbitration. Installing an | ||
| * implementation here lets the UI take that lock around its own bus traffic, so the host | ||
| * does not have to serialize whole UI cycles to stay safe. | ||
| * | ||
| * Hold time is what matters: the UI takes the lock only around actual transfers, never | ||
| * across rendering, so the bus stays free during the CPU-only majority of a redraw. | ||
| * | ||
| * @note Implementations MUST be reentrant. UI call sites nest (a tile draw takes the | ||
| * lock, and the SD read that feeds it takes it again), so a plain non-recursive mutex | ||
| * will self-deadlock. Wrap one if that is all the host has - see the firmware's | ||
| * tftSetup.cpp for a task-tracking example. | ||
| * | ||
| * When no implementation is installed the guards below are no-ops, which is correct for | ||
| * hosts that do not share the bus. | ||
| */ | ||
| class ISpiLock | ||
| { | ||
| public: | ||
| virtual ~ISpiLock() = default; | ||
| virtual void lock(void) = 0; | ||
| virtual void unlock(void) = 0; | ||
|
|
||
| /** | ||
| * Let a waiter in without giving up the enclosing critical section. | ||
| * | ||
| * Provided for an async-DMA flush: once the display driver can signal completion | ||
| * asynchronously, the UI can release the bus while the transfer runs. Nothing calls | ||
| * it yet, so the default - release and immediately re-take - is sufficient. | ||
| */ | ||
| virtual void yield(void) | ||
| { | ||
| unlock(); | ||
| lock(); | ||
| } | ||
|
|
||
| /** | ||
| * Normally set through DeviceScreen::create(). Kept as a static holder because two | ||
| * classes of call site cannot reach an instance member: the LVGL flush and touch | ||
| * callbacks are static and have no `this`, and the SD layer (`sdCard`, `SDFs`) is | ||
| * file-scope globals with no route to a DeviceGUI. | ||
| */ | ||
| static void install(ISpiLock *lock); | ||
| static ISpiLock *installed(void); | ||
|
|
||
| /// RAII: hold the bus for the enclosing scope. Safe when nothing is installed. | ||
| class Guard | ||
| { | ||
| public: | ||
| Guard(void) : held(ISpiLock::installed()) | ||
| { | ||
| if (held) | ||
| held->lock(); | ||
| } | ||
| ~Guard() | ||
| { | ||
| if (held) | ||
| held->unlock(); | ||
| } | ||
| Guard(const Guard &) = delete; | ||
| Guard &operator=(const Guard &) = delete; | ||
|
|
||
| private: | ||
| ISpiLock *held; | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Document the installed lock’s required lifetime.
install()retains this raw pointer globally and static callbacks dereference it aftercreate()returns. A stack-scoped host lock would leave later guards with a dangling pointer. Require the lock to outlive all UI, LVGL, and SD activity, or give the registry ownership.Proposed documentation
📝 Committable suggestion
🤖 Prompt for AI Agents