-
Notifications
You must be signed in to change notification settings - Fork 2
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
i2c: Add I2C Controller implementation #27
Open
astapleton
wants to merge
6
commits into
master
Choose a base branch
from
as/i2c-controller
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1669783
i2c: Add I2C Controller implementation
astapleton 730c3e2
Move definitions to sub-module, only define for H503 for now
astapleton c2daf01
Require stm32h503 for example
astapleton 97c985b
Update I2C driver for new PAC
astapleton f90c274
Add macro for double register read with reasoning
astapleton cecddec
Fixed I2C definitions after new PAC integration
astapleton 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 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 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,64 @@ | ||
#![deny(warnings)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[macro_use] | ||
mod utilities; | ||
use embedded_hal::{delay::DelayNs, i2c::I2c}; | ||
use fugit::SecsDurationU32; | ||
use stm32h5xx_hal::{delay::Delay, pac, prelude::*}; | ||
|
||
use cortex_m_rt::entry; | ||
|
||
use log::info; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
utilities::logger::init(); | ||
log::set_max_level(log::LevelFilter::Debug); | ||
let cp = cortex_m::Peripherals::take().unwrap(); | ||
let dp = pac::Peripherals::take().unwrap(); | ||
|
||
// Constrain and Freeze power | ||
info!("Setup PWR... "); | ||
let pwr = dp.PWR.constrain(); | ||
let pwrcfg = pwr.freeze(); | ||
|
||
// Constrain and Freeze clock | ||
info!("Setup RCC... "); | ||
let rcc = dp.RCC.constrain(); | ||
let ccdr = rcc.sys_ck(100.MHz()).freeze(pwrcfg, &dp.SBS); | ||
|
||
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB); | ||
|
||
let mut delay = Delay::new(cp.SYST, &ccdr.clocks); | ||
let duration = SecsDurationU32::secs(1).to_millis(); | ||
|
||
// Configure the SCL and the SDA pin for our I2C bus | ||
let scl = gpiob.pb5.into_alternate_open_drain(); | ||
let sda = gpiob.pb3.into_alternate_open_drain(); | ||
|
||
info!(""); | ||
info!("stm32h5xx-hal example - I2C"); | ||
info!(""); | ||
|
||
let mut i2c = | ||
dp.I2C2 | ||
.i2c((scl, sda), 100.kHz(), ccdr.peripheral.I2C2, &ccdr.clocks); | ||
|
||
// The STM32H503 NUCLEO board does not have any I2C peripherals, so put in the address of | ||
// whatever peripheral you connect | ||
let device_addr: u8 = 0x18; | ||
// This implements a typical 8-bit register read operation, writing the register address and | ||
// then issuing a repeat start to read the register value. Tweak these settings to read the | ||
// desired amount of bytes from the register address. | ||
let register_addr = 0x02; | ||
let write = &[register_addr]; | ||
let mut read = [0u8; 1]; | ||
|
||
loop { | ||
i2c.write_read(device_addr, write, &mut read).unwrap(); | ||
info!("Read reg {register_addr}: {read:X?}"); | ||
delay.delay_ms(duration); | ||
} | ||
} |
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.
I really would avoid using the
nb
crate nowadays. There's been talk of entirely deprecating it. It was intended to support embeddedasync
beforeasync
was possible on embedded, but there's more modern approaches now withasync
frameworks targeting embedded.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.
Ah, thanks for this context. I'll look at
embedded-hal-async
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.
@ryan-summers I looked at
embedded-hal-async
, but I'm not sure how it solves the same problem? It doesn't provide any tool to manage repeatedly trying a blocking call. Is the suggestion to remove non-blocking calls entirely? Wouldn't they be useful in interrupts, or for creating a Future implementation for async?Is itembedded-hal-nb
that might be deprecated rather than thenb
crate? That wasn't actually at 1.0 when I originally created this driver, so I didn't implement those APIs.Turns out I did implement this for the SPI driver.
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.
@ryan-summers Coming back to this, is the recommendation to declare non-blocking/async methods that return a
Future
instead of using thenb
crate? Or have non-blocking methods that just check a status and returnbool
/Result<bool>
instead of returning annb::Error::WouldBlock
and then have methods that can't be easily decomposed to a bool return aFuture
? If you're not using an async runtime, how would one implement interrupt based operations? Just manually invoking poll on a future?