From 54d171309a0a0c9f61528ccc0e9c8893d796b5c5 Mon Sep 17 00:00:00 2001 From: supersimple33 <40609224+supersimple33@users.noreply.github.com> Date: Tue, 1 Jul 2025 09:48:24 -0400 Subject: [PATCH 1/5] formatting --- hal/src/peripherals/usb/d5x/bus.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hal/src/peripherals/usb/d5x/bus.rs b/hal/src/peripherals/usb/d5x/bus.rs index 1441ff780dc2..08aaac7fd7b3 100644 --- a/hal/src/peripherals/usb/d5x/bus.rs +++ b/hal/src/peripherals/usb/d5x/bus.rs @@ -8,7 +8,7 @@ use super::Descriptors; use crate::calibration::{usb_transn_cal, usb_transp_cal, usb_trim_cal}; use crate::clock; -use crate::gpio::{AlternateH, AnyPin, Pin, PA24, PA25}; +use crate::gpio::{AlternateH, AnyPin, PA24, PA25, Pin}; use crate::pac; use crate::pac::usb::Device; use crate::pac::{Mclk, Usb}; @@ -17,7 +17,7 @@ use core::cell::{Ref, RefCell, RefMut}; use core::marker::PhantomData; use core::mem; use cortex_m::singleton; -use critical_section::{with as disable_interrupts, Mutex}; +use critical_section::{Mutex, with as disable_interrupts}; use usb_device::bus::PollResult; use usb_device::endpoint::{EndpointAddress, EndpointType}; use usb_device::{Result as UsbResult, UsbDirection, UsbError}; From ccef5ea616c1769dbde8b71b5342414fdff8b45d Mon Sep 17 00:00:00 2001 From: supersimple33 <40609224+supersimple33@users.noreply.github.com> Date: Tue, 1 Jul 2025 10:45:00 -0400 Subject: [PATCH 2/5] adding send to gclk id --- hal/src/peripherals/clock/d5x/v2/gclk.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hal/src/peripherals/clock/d5x/v2/gclk.rs b/hal/src/peripherals/clock/d5x/v2/gclk.rs index 18a28eddf99e..3e65c49d0578 100644 --- a/hal/src/peripherals/clock/d5x/v2/gclk.rs +++ b/hal/src/peripherals/clock/d5x/v2/gclk.rs @@ -547,7 +547,7 @@ pub enum DynGclkId { /// /// [type-level programming]: crate::typelevel /// [type-level enums]: crate::typelevel#type-level-enums -pub trait GclkId: Sealed { +pub trait GclkId: Sealed + Send { /// Corresponding variant of [`DynGclkId`] const DYN: DynGclkId; /// Corresponding numeric index (0..12) From 363f9e10dfe76dc4cb0b4f7d61b45ec8044fd4dd Mon Sep 17 00:00:00 2001 From: supersimple33 <40609224+supersimple33@users.noreply.github.com> Date: Tue, 1 Jul 2025 10:49:58 -0400 Subject: [PATCH 3/5] Updated to take v2 clocks Added the three clocks to the inner struct so they can be held onto. Added a generic so that the correct Pclk source can be inferred. Added a free method so the user can get those back when they're done with the usb --- hal/src/peripherals/usb/d5x/bus.rs | 61 ++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/hal/src/peripherals/usb/d5x/bus.rs b/hal/src/peripherals/usb/d5x/bus.rs index 08aaac7fd7b3..e253c94363df 100644 --- a/hal/src/peripherals/usb/d5x/bus.rs +++ b/hal/src/peripherals/usb/d5x/bus.rs @@ -7,11 +7,16 @@ use super::Descriptors; use crate::calibration::{usb_transn_cal, usb_transp_cal, usb_trim_cal}; -use crate::clock; +use crate::clock::v2::{ + self as clock, + ahb::AhbClk, + apb::ApbClk, + pclk::{Pclk, PclkSourceId}, +}; use crate::gpio::{AlternateH, AnyPin, PA24, PA25, Pin}; use crate::pac; +use crate::pac::Usb; use crate::pac::usb::Device; -use crate::pac::{Mclk, Usb}; use crate::usb::devicedesc::DeviceDescBank; use core::cell::{Ref, RefCell, RefMut}; use core::marker::PhantomData; @@ -191,16 +196,19 @@ impl BufferAllocator { } } -struct Inner { +struct Inner { desc: RefCell, _dm_pad: Pin, _dp_pad: Pin, endpoints: RefCell, buffers: RefCell, + pclk: Pclk, + ahb_clk: AhbClk, + apb_clk: ApbClk, } -pub struct UsbBus { - inner: Mutex>, +pub struct UsbBus { + inner: Mutex>>, } struct Bank<'a, T> { @@ -469,7 +477,7 @@ impl Bank<'_, T> { } } -impl Inner { +impl Inner { #[inline] fn epcfg(&self, endpoint: usize) -> &pac::usb::device::device_endpoint::Epcfg { self.usb().device_endpoint(endpoint).epcfg() @@ -524,17 +532,15 @@ impl Inner { } } -impl UsbBus { +impl UsbBus { pub fn new( - _clock: &clock::UsbClock, - mclk: &mut Mclk, + pclk: Pclk, + ahb_clk: AhbClk, + apb_clk: ApbClk, dm_pad: impl AnyPin, dp_pad: impl AnyPin, _usb: Usb, ) -> Self { - mclk.ahbmask().modify(|_, w| w.usb_().set_bit()); - mclk.apbbmask().modify(|_, w| w.usb_().set_bit()); - let desc = RefCell::new(Descriptors::new()); let inner = Inner { @@ -543,15 +549,38 @@ impl UsbBus { desc, buffers: RefCell::new(BufferAllocator::new()), endpoints: RefCell::new(AllEndpoints::new()), + pclk, + ahb_clk, + apb_clk, }; Self { inner: Mutex::new(RefCell::new(inner)), } } + + pub fn free( + self, + ) -> ( + Pclk, + AhbClk, + ApbClk, + Pin, + Pin, + ) { + // Unwrap the Mutex and RefCell to get the Inner + let inner = self.inner.into_inner().into_inner(); + ( + inner.pclk, + inner.ahb_clk, + inner.apb_clk, + inner._dm_pad, + inner._dp_pad, + ) + } } -impl Inner { +impl Inner { fn usb(&self) -> &Device { unsafe { (*Usb::ptr()).device() } } @@ -576,7 +605,7 @@ enum FlushConfigMode { ProtocolReset, } -impl Inner { +impl Inner { fn enable(&mut self) { let usb = self.usb(); usb.ctrla().modify(|_, w| w.swrst().set_bit()); @@ -884,7 +913,7 @@ impl Inner { } } -impl UsbBus { +impl UsbBus { /// Enables the Start Of Frame (SOF) interrupt pub fn enable_sof_interrupt(&self) { disable_interrupts(|cs| self.inner.borrow(cs).borrow_mut().sof_interrupt(true)) @@ -901,7 +930,7 @@ impl UsbBus { } } -impl usb_device::bus::UsbBus for UsbBus { +impl usb_device::bus::UsbBus for UsbBus { fn enable(&mut self) { disable_interrupts(|cs| self.inner.borrow(cs).borrow_mut().enable()) } From 1a1fd493c3f539560138209389f874149a3163d0 Mon Sep 17 00:00:00 2001 From: supersimple33 <40609224+supersimple33@users.noreply.github.com> Date: Tue, 1 Jul 2025 20:46:52 -0400 Subject: [PATCH 4/5] Updating Allocator to v2 Co-authored-by: Ashcon Mohseninia (RAND_ASH) --- boards/grand_central_m4/src/lib.rs | 46 +++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/boards/grand_central_m4/src/lib.rs b/boards/grand_central_m4/src/lib.rs index 16b5a086863d..14ea36d0fb48 100644 --- a/boards/grand_central_m4/src/lib.rs +++ b/boards/grand_central_m4/src/lib.rs @@ -5,8 +5,22 @@ //! an ATSAMD51-based board in an Arduino Mega form factor. pub use atsamd_hal as hal; -pub use hal::ehal; -pub use hal::pac; +pub use hal::{ + clock::v2::{ + self as clock, + ahb::{AhbClk, AhbToken}, + apb::{ApbClk, ApbToken}, + dfll::{self, Dfll}, + gclk::{Gclk, GclkId, GclkToken}, + pclk::{Pclk, PclkToken}, + types::Usb, + Enabled, + }, + ehal, + gpio::{AnyPin, PA24, PA25}, + pac, + sercom::spi::lengths::U1, +}; #[cfg(feature = "rt")] pub use cortex_m_rt::entry; @@ -595,18 +609,22 @@ pub fn uart( #[cfg(feature = "usb")] /// Convenience function for setting up USB -pub fn usb_allocator( +pub fn usb_allocator( usb: pac::Usb, - clocks: &mut GenericClockController, - mclk: &mut pac::Mclk, - dm: impl Into, - dp: impl Into, -) -> UsbBusAllocator { - use pac::gclk::{genctrl::Srcselect, pchctrl::Genselect}; + dfll: Enabled, + gclk_token: GclkToken, + pclk_token: PclkToken, + ahb_clk: AhbClk, + apb_clk: ApbClk, + dm: impl AnyPin, + dp: impl AnyPin, +) -> UsbBusAllocator> { + // SWITCH DFLL INTO USB CLOCK RECOVERY MODE - THIS ALLOWS FOR MORE ACCURATE USB + let (dfll_usb, _) = dfll.into_mode(dfll::FromUsb, |_| {}); + // GCLK3 comes from DFLL, outputs to USB + let (gclk_n, _) = Gclk::from_source(gclk_token, dfll_usb); + let gclk_n_48mhz = gclk_n.enable(); + let (pclk_usb, _) = Pclk::enable(pclk_token, gclk_n_48mhz); - clocks.configure_gclk_divider_and_source(Genselect::Gclk2, 1, Srcselect::Dfll, false); - let usb_gclk = clocks.get_gclk(Genselect::Gclk2).unwrap(); - let usb_clock = &clocks.usb(&usb_gclk).unwrap(); - let (dm, dp) = (dm.into(), dp.into()); - UsbBusAllocator::new(UsbBus::new(usb_clock, mclk, dm, dp, usb)) + return UsbBusAllocator::new(UsbBus::new(pclk_usb, ahb_clk, apb_clk, dm, dp, usb)); } From 3380e4877db8dc735f8f626f004b42219ed378c4 Mon Sep 17 00:00:00 2001 From: supersimple33 <40609224+supersimple33@users.noreply.github.com> Date: Tue, 1 Jul 2025 20:47:27 -0400 Subject: [PATCH 5/5] Using local HAL for now --- boards/grand_central_m4/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/boards/grand_central_m4/Cargo.toml b/boards/grand_central_m4/Cargo.toml index 931b14403da5..1c3d5bbae56c 100644 --- a/boards/grand_central_m4/Cargo.toml +++ b/boards/grand_central_m4/Cargo.toml @@ -17,6 +17,7 @@ optional = true [dependencies.atsamd-hal] default-features = false version = "0.22.0" +path = "../../hal" [dependencies.usb-device] version = "0.3.2"