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

Allow USB to work with V2 clocking API #808

Closed
wants to merge 6 commits into from

Conversation

rnd-ash
Copy link
Contributor

@rnd-ash rnd-ash commented Jan 7, 2025

Summary

Continuing my work to allow more peripherals to work with the V2 clocking API, this PR adds a new method (Maybe name should be changed to something more permanent), that allows the UsbBus to be constructed with the new V2 clocking API.

I've left the original constructor untouched in order to maintain compatibility.

Now, in the BSPs, USB can be constructed like so:

pub fn usb(
    usb: pac::Usb,
    pclk_usb: Pclk<Usb, Gclk0Id>, // Can use any Gclk here
    mclk: &mut pac::Mclk,
    d_minus: impl Into<UsbDm>,
    d_plus: impl Into<UsbDp>,
) -> UsbBusAllocator<UsbBus> {
    UsbBusAllocator::new(UsbBus::new_with_v2_clock(pclk_usb, mclk, d_minus.into(), d_plus.into(), usb))
}

@rnd-ash
Copy link
Contributor Author

rnd-ash commented Jan 7, 2025

Additional note. I'm not sure why we even need the pclk here, since the original constructor never made use of the UsbClock that it was given. Maybe we can remove the clock entirely from the V1 constructor and then it would work with both clocking APIs?

@jbeaurivage
Copy link
Contributor

@rnd-ash, now that I have a better grasp of the v2 clocking system I feel more comfortable commenting on this:
The design philosophy behind clocks::v2 is that the entire clock tree should be configured by the user. This includes the main clock, but also enabling the PCLKs and AHB/APB clocks as well.
Case in point, the MCLK peripheral isn't even given out when creating the tokens with clocks_at_reset. I think one would need to use unsafe code just to use new_with_v2_clock, breaking the guarantees laid out by clock::v2.

The types being passed is just a way of proving that the clocks are in fact correctly enabled, at compile time. What's more, these types must be kept around in the USB struct until it's dismantled (ie, give ownership to the peripheral struct), because otherwise one could "prove" the clocks are setup correctly when setting up a peripheral, then immediately go and use those types to disable the clocks, rendering the peripheral unusable.

@rnd-ash
Copy link
Contributor Author

rnd-ash commented Feb 11, 2025

When I created this PR, I was learning the crate, and I didn't realise until recently that you can call pclk.into() to create a USB clock that the V1 API requires.

Therefore, I believe it's safe to close this PR as it is semi pointless

@rnd-ash rnd-ash deleted the usb_clockv2 branch February 11, 2025 17:57
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.

2 participants