-
Notifications
You must be signed in to change notification settings - Fork 57
Print GPIO pin configuration for humility gpio --input --with-config
#574
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
Open
lzrd
wants to merge
7
commits into
master
Choose a base branch
from
gpio-config-dump
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac4fb14
Print GPIO pin configuration for `humility gpio --input --with-config`
lzrd 914aa98
Refactor STM32H753 GPIO configuration printing support to a module.
lzrd a684daf
Generalize to support STM32H743 and emit errors for unknown chips.
lzrd 2a14112
fix formatting complaint
lzrd 6e07987
Define the one needed struct instead of involving an stm32 crate.
lzrd c3533ce
GPIO configuration display is adapted to the actual chip being used.
lzrd d5608f4
Change PinConfig to reduce boilerplate `to_string` calls.
lzrd 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,63 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
//! Generic cache for GPIO configuration register blocks. | ||
//! | ||
//! This module provides a chip-independent way to cache raw GPIO register | ||
//! block data from device memory. The cache stores raw bytes indexed by | ||
//! GPIO group identifiers, avoiding repeated memory reads for the same | ||
//! GPIO port. | ||
|
||
use anyhow::Result; | ||
use humility_cli::ExecutionContext; | ||
use std::collections::BTreeMap; | ||
|
||
/// A generic cache for GPIO configuration register blocks. | ||
/// | ||
/// This cache stores raw byte data for GPIO register blocks, indexed by | ||
/// a group identifier (typically a character like 'A', 'B', etc.). The | ||
/// cache is chip-independent and works with any architecture that organizes | ||
/// GPIO pins into groups/ports with contiguous register blocks. | ||
pub struct ConfigCache { | ||
cache: BTreeMap<char, Vec<u8>>, | ||
} | ||
|
||
impl ConfigCache { | ||
/// Creates a new empty configuration cache. | ||
pub fn new() -> ConfigCache { | ||
ConfigCache { cache: BTreeMap::new() } | ||
} | ||
|
||
/// Gets or fetches the raw register block data for a GPIO group. | ||
/// | ||
/// If the data for the specified group is already cached, returns it | ||
/// directly. Otherwise, calls the provided `fetch_fn` to read the data | ||
/// from device memory, caches it, and returns it. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `context` - Execution context with access to the device core | ||
/// * `group` - GPIO group identifier (e.g., 'A', 'B', 'C') | ||
/// * `fetch_fn` - Function that fetches the register block from device memory | ||
/// | ||
/// # Returns | ||
/// | ||
/// A reference to the cached raw register block data | ||
pub fn get_or_fetch<F>( | ||
&mut self, | ||
context: &mut ExecutionContext, | ||
group: char, | ||
fetch_fn: F, | ||
) -> Result<&[u8]> | ||
where | ||
F: FnOnce(&mut ExecutionContext, char) -> Result<Vec<u8>>, | ||
{ | ||
use std::collections::btree_map::Entry; | ||
if let Entry::Vacant(e) = self.cache.entry(group) { | ||
let data = fetch_fn(context, group)?; | ||
e.insert(data); | ||
} | ||
Ok(self.cache.get(&group).unwrap().as_slice()) | ||
} | ||
} |
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.
We never store multiple register block types, so this should be made generic across a register block type
T
. This would let us avoid needing to parse from bytes elsewhere.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.
I'm ok with this separation right now. The code that knows about STM32H7 registers doesn't do IO, doesn't hold state, etc. The code that does the IO is just told to that if you want to talk about a pin group, you need to get me this data. There needs to be a from_bytes somewhere to translate the raw data from the device. I think this approach would work well for non STM32H7 type devices as well. But, I'd like to have a motivating case before making a change here.