@@ -524,13 +524,44 @@ impl Inner {
524524 }
525525}
526526
527+ #[ derive( Debug , Clone , Copy , Eq , PartialEq ) ]
528+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
529+ pub enum UsbBusErr {
530+ /// USB clock freq is not valid for a stable connection
531+ InvalidClockFreq ,
532+ }
533+
527534impl UsbBus {
535+ /// Create a new USB Bus, checking the clock frequency of the USB clock for
536+ /// a stable USB link to most hosts. The `clock` freq must be 48Mhz, otherwise
537+ /// [`UsbBusErr::InvalidClockFreq`] will be returned
528538 pub fn new (
529- _clock : & clock:: UsbClock ,
539+ clock : & clock:: UsbClock ,
530540 mclk : & mut Mclk ,
531541 dm_pad : impl AnyPin < Id = PA24 > ,
532542 dp_pad : impl AnyPin < Id = PA25 > ,
533543 _usb : Usb ,
544+ ) -> Result < Self , UsbBusErr > {
545+ if clock. freq ( ) . to_Hz ( ) != 48_000_000 {
546+ Err ( UsbBusErr :: InvalidClockFreq )
547+ } else {
548+ let res = unsafe { Self :: new_unchecked ( clock, mclk, dm_pad, dp_pad) } ;
549+ Ok ( res)
550+ }
551+ }
552+
553+ /// Creates a new USB Bus, but does NOT perform the USB clock frequency check.
554+ ///
555+ /// SAFETY: For a SAMx to PC connection, the USB clock must be 48Mhz for stability.
556+ /// This function allows you to bypass this check however if you intend to connect multiple SAM chips
557+ /// together with faster running USB clocks for a boost in transfer rate.
558+ ///
559+ /// Consult the datasheet for GCLK_USB absolute maximums
560+ pub unsafe fn new_unchecked (
561+ _clock : & clock:: UsbClock ,
562+ mclk : & mut Mclk ,
563+ dm_pad : impl AnyPin < Id = PA24 > ,
564+ dp_pad : impl AnyPin < Id = PA25 > ,
534565 ) -> Self {
535566 mclk. ahbmask ( ) . modify ( |_, w| w. usb_ ( ) . set_bit ( ) ) ;
536567 mclk. apbbmask ( ) . modify ( |_, w| w. usb_ ( ) . set_bit ( ) ) ;
0 commit comments