@@ -562,8 +562,8 @@ const USART4_BASE: StaticRef<UsartRegisters> =
562562pub struct Uart < ' a > {
563563 registers : StaticRef < UsartRegisters > ,
564564 instance : u8 ,
565- clocks : OptionalCell < & ' a Clock > ,
566- flexcomm : OptionalCell < & ' a Flexcomm > ,
565+ clocks : & ' a Clock ,
566+ flexcomm : & ' a Flexcomm ,
567567
568568 uart_clock_source : Cell < FrgClockSource > ,
569569
@@ -582,12 +582,17 @@ pub struct Uart<'a> {
582582}
583583
584584impl < ' a > Uart < ' a > {
585- pub fn new ( registers : StaticRef < UsartRegisters > , instance : u8 ) -> Self {
585+ pub fn new (
586+ registers : StaticRef < UsartRegisters > ,
587+ instance : u8 ,
588+ clocks : & ' a Clock ,
589+ flexcomm : & ' a Flexcomm ,
590+ ) -> Self {
586591 Self {
587592 registers,
588593 instance,
589- clocks : OptionalCell :: empty ( ) ,
590- flexcomm : OptionalCell :: empty ( ) ,
594+ clocks,
595+ flexcomm,
591596
592597 uart_clock_source : Cell :: new ( FrgClockSource :: Fro12Mhz ) ,
593598
@@ -605,20 +610,12 @@ impl<'a> Uart<'a> {
605610 rx_status : Cell :: new ( UARTStateRX :: Idle ) ,
606611 }
607612 }
608- pub fn new_uart0 ( ) -> Self {
609- Self :: new ( USART0_BASE , 0 )
613+ pub fn new_uart0 ( clocks : & ' a Clock , flexcomm : & ' a Flexcomm ) -> Self {
614+ Self :: new ( USART0_BASE , 0 , clocks , flexcomm )
610615 }
611616
612- pub fn new_uart4 ( ) -> Self {
613- Self :: new ( USART4_BASE , 4 )
614- }
615-
616- pub fn set_clocks ( & self , clocks : & ' a Clock ) {
617- self . clocks . set ( clocks) ;
618- }
619-
620- pub fn set_flexcomm ( & self , flexcomm : & ' a Flexcomm ) {
621- self . flexcomm . set ( flexcomm) ;
617+ pub fn new_uart4 ( clocks : & ' a Clock , flexcomm : & ' a Flexcomm ) -> Self {
618+ Self :: new ( USART4_BASE , 4 , clocks, flexcomm)
622619 }
623620
624621 pub fn set_clock_source ( & self , source : FrgClockSource ) {
@@ -836,17 +833,15 @@ impl<'a> Uart<'a> {
836833
837834impl Configure for Uart < ' _ > {
838835 fn configure ( & self , params : Parameters ) -> Result < ( ) , ErrorCode > {
839- let clocks = self . clocks . get ( ) . ok_or ( ErrorCode :: OFF ) ?;
840- let flexcomm = self . flexcomm . get ( ) . ok_or ( ErrorCode :: OFF ) ?;
841836 let clock_source = self . uart_clock_source . get ( ) ;
842837 let frg_id = FrgId :: from_u32 ( self . instance . into ( ) ) . ok_or ( ErrorCode :: INVAL ) ?;
843- clocks. setup_uart_clock ( frg_id, clock_source) ;
844- flexcomm. configure_for_uart ( ) ;
838+ self . clocks . setup_uart_clock ( frg_id, clock_source) ;
839+ self . flexcomm . configure_for_uart ( ) ;
845840
846841 // --- Disable USART before configuration ---
847842 self . registers . cfg . modify ( CFG :: ENABLE :: CLEAR ) ;
848843
849- let clk = clocks. get_frg_clock_frequency ( clock_source) ;
844+ let clk = self . clocks . get_frg_clock_frequency ( clock_source) ;
850845 let brg_val = ( clk / ( 16 * params. baud_rate ) ) . saturating_sub ( 1 ) ;
851846 if brg_val > 0xFFFF {
852847 return Err ( ErrorCode :: INVAL ) ; // Baud rate not possible
0 commit comments