Skip to content
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

Add I2S/SAI pad info #3

Merged
merged 2 commits into from
Apr 24, 2021
Merged

Add I2S/SAI pad info #3

merged 2 commits into from
Apr 24, 2021

Conversation

lkolbly
Copy link
Contributor

@lkolbly lkolbly commented Apr 3, 2021

This adds traits and impls for the SAI peripherals. (also, it updates the Cargo.toml to point at this repo)

This is very 106x-specific, should I wrap it in a cfg(feature = "imxrt106x")? That's the only feature in the crate (on the v0.1 branch), so I'm not sure. The 1010 I believe does have some (2?) SAI peripherals, and the pads are almost certainly not the same.

SAI3 overlaps on several pins with SAI1, so adding it complains about re-implementing a few traits. There may be a clever way around this (in particular, the traits could be split into separate e.g. RxSyncPin and RxBclkPin traits), I can do that if we want but I don't feel a pressing need to do so right now. Also the daisy names are different for some reason.

Testing-wise, I've only tested this with SAI1 in one-bit TX mode, with a specific set of pins. I tried to copy the table as closely as possible.

@teburd
Copy link
Member

teburd commented Apr 5, 2021

Really happy to see this happening!

Copy link
Member

@teburd teburd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good generally, I need to look closer at the SAI pads and alts from the reference doc

Cargo.toml Show resolved Hide resolved
daisy.py Show resolved Hide resolved
@lkolbly
Copy link
Contributor Author

lkolbly commented Apr 5, 2021

I need to look closer at the SAI pads and alts from the reference doc

Please do and take your time, the 3s and the 8s blend together on my screen. :D

Copy link
Member

@mciantyre mciantyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start! A few questions and requests.

Can we target the main branch for this PR? We don't have to add 1010 SAI pins right now, but we should design knowing that those pins will eventually be implemented. I'll take care of backporting to the 0.1 branch.

SAI3 overlaps on several pins with SAI1, so adding it complains about re-implementing a few traits.

The ADC pins had the same issue. Check out the adc module to see how we solved it: the Pin trait is generic over an ADC module. Instead of using custom types for SAI[1, 2, 3] (analogous to ADC1 and ADC2), let's try using typenum types to express the SAI instance. If my thinking is correct, it might work nicely with changes we're considering in the RAL.

There may be a clever way around this (in particular, the traits could be split into separate e.g. RxSyncPin and RxBclkPin traits)

At the moment, I lean towards a single Pin trait to maintain consistency with the rest of the crate. But if we find that multiple traits is cleaner or easier for the user, I'm happy to support it.

Auto-generating the SAI implementations in build.rs diverges with the rest of the pin implementations. I love how the tables are easy to read and review, but I'm not sure it warrants the complexity of build-time generated code. Could we compromise with declarative macros to implement the pins? If auto-generated code is the way to go, let's move the types and routines into the imxrt-iomuxc-build crate, which will make it easier to test (now or later).

I took your branch and started playing around with these ideas. Check out the sai-i2s branch if you want to see what it looks like.

src/sai.rs Outdated
Comment on lines 45 to 55
pub trait TxDataPin: super::IOMUX {
const ALT: u32;
type Module: super::consts::Unsigned;
type Index: super::consts::Unsigned;
}

pub trait RxDataPin: super::IOMUX {
const ALT: u32;
type Module: super::consts::Unsigned;
type Index: super::consts::Unsigned;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do RxDataPin and TxDataPin need an associated daisy register? I see constants like DAISY_SAI1_RX_DATA3_SD_B1_02, which seem to imply additional pin setup.

(Since RX_DATA_03 and TX_DATA_01 are multiplexed on the same pad, I'm guessing that the daisy register applies to both, despite only seeing "RX_DATA" in the name. Let me know if this isn't right!)

Some signals, like SAI1's TX_DATA_00, don't have daisy registers. Consider using an Option if that's the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing that the daisy register applies to both

I don't think so, since I think the daisy registers only apply to inputs so they wouldn't apply to TX.

RxDataPin does have daisy registers though, good catch. I think it has one for every pin.

src/sai.rs Outdated
pub trait TxDataPin: super::IOMUX {
const ALT: u32;
type Module: super::consts::Unsigned;
type Index: super::consts::Unsigned;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a const INDEX: [u32|usize] here, instead of a type number? Comment also applies to RxDataPin::Index.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure you can. Ideally, I think the HAL implementation would look something like this:

    pub fn build_1bit_tx<MCLK, BCLK, SYNC, DATA>(
        self,
        clock_config: ClockConfig,
        mut mclk: MCLK,
        mut bclk: BCLK,
        mut sync: SYNC,
        mut data: DATA,
    ) -> SAI<M>
    where
        MCLK: sai::Pin<Module = M, Signal = sai::MCLK2>,
        BCLK: sai::Pin<Module = M, Signal = sai::TX_BCLK>,
        SYNC: sai::Pin<Module = M, Signal = sai::TX_SYNC>,
        DATA: sai::TxDataPin<Module = M, Index = U0>,

And in particular, the data pin arguments should only accept specific indexes, and I can't get the type bound to work with associated constants.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Yea, I hit a similar limitation when trying to move this entire crate to const generics. Let's stick with the typenums.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type bounds are a very good reason to keep the typenums, I agree!

src/sai.rs Outdated
pub trait Signal: private::Sealed {}

/// A tag that indicates a SAI TX bit clock pad
pub enum TX_BCLK {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's start heeding these clippy naming warnings. I pushed a commit that addresses the CI failures. And I'm thinking about a large rename before the 0.2 release...

For the 0.1 release, it's OK with me to have inconsistent names, particularly if it will save us and others refactoring time in the future.

src/sai.rs Outdated
Comment on lines 68 to 128
pub fn prepare_rxdata<P: RxDataPin>(pin: &mut P) {
super::alternate(pin, P::ALT);
super::set_sion(pin);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This discussion indicates that we might need to disable the drive strength configuration when using the SAI1 multi-channel pins. Have we seen that need in practice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I have not tested SAI input (or even multi-line output), so I'm not really in a position to easily test it. It's not clear to me whether that behaviour would transfer over to the 106x series, since ostensibly the 1010 has no daisy chain registers so the configuration scheme could be different.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this as-is. Once merged, we'll track our uncertainty / future testing with an issue.

build.rs Outdated
Comment on lines 7 to 10
generate_sai(std::fs::File::create(out_dir.join("sai_pads.rs"))?)?;

#[cfg(feature = "imxrt106x")]
imxrt106x(fs::File::create(out_dir.join("imxrt106x.rs"))?)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's namespace this SAI code underneath the auto-generated 106x code, and behind the "imxrt106x" feature flag. It'll make porting to the main branch easier.

@lkolbly
Copy link
Contributor Author

lkolbly commented Apr 11, 2021

I took your branch and started playing around with these ideas. Check out the sai-i2s branch if you want to see what it looks like.

Awesome, that branch looks pretty reasonable to me! I agree that the macro is cleaner than a build script.

I think we will need associated types for the indexes though, rather than consts.

@mciantyre
Copy link
Member

Agreed on keeping typenums for data line indices! Pushed a corresponding commit to the sai-i2s branch in this remote.

Copy link
Member

@mciantyre mciantyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for working through this. I'll leave this open for @BFrog to take a look. Unless we need more time to review, I'll merge after a week, and release 0.1.3 with these changes.

@mciantyre mciantyre added this to the 0.1.3 milestone Apr 19, 2021
Copy link
Member

@teburd teburd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty darn solid to me at this point, again I didn't look too deeply at the pad and iomux values, but more broadly at the macro, api, and such

@mciantyre mciantyre merged commit 164bdba into imxrt-rs:master Apr 24, 2021
@mciantyre mciantyre mentioned this pull request Apr 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants