@@ -308,7 +308,7 @@ where
308
308
USART :: reset ( rcc) ;
309
309
310
310
PINS :: remap ( mapr) ;
311
- Self :: apply_config ( & usart , config. into ( ) , clocks) ;
311
+ apply_config :: < USART > ( config. into ( ) , clocks) ;
312
312
313
313
// UE: enable USART
314
314
// RE: enable receiver
@@ -324,38 +324,45 @@ where
324
324
rx : Rx :: new ( ) ,
325
325
}
326
326
}
327
+ }
327
328
328
- fn apply_config ( usart : & USART , config : Config , clocks : & Clocks ) {
329
- // Configure baud rate
330
- let brr = USART :: clock ( clocks) . raw ( ) / config. baudrate . 0 ;
331
- assert ! ( brr >= 16 , "impossible baud rate" ) ;
332
- usart. brr . write ( |w| unsafe { w. bits ( brr) } ) ;
329
+ fn apply_config < USART : Instance > ( config : Config , clocks : & Clocks ) {
330
+ let usart = unsafe { & * USART :: ptr ( ) } ;
333
331
334
- // Configure word
335
- let ( parity_is_used, parity_is_odd) = match config. parity {
336
- Parity :: ParityNone => ( false , false ) ,
337
- Parity :: ParityEven => ( true , false ) ,
338
- Parity :: ParityOdd => ( true , true ) ,
339
- } ;
340
- usart. cr1 . modify ( |_r, w| {
341
- w. m ( ) . bit ( match config. wordlength {
342
- WordLength :: Bits8 => false ,
343
- WordLength :: Bits9 => true ,
344
- } ) ;
345
- w. ps ( ) . bit ( parity_is_odd) ;
346
- w. pce ( ) . bit ( parity_is_used)
347
- } ) ;
332
+ // Configure baud rate
333
+ let brr = USART :: clock ( clocks) . raw ( ) / config. baudrate . 0 ;
334
+ assert ! ( brr >= 16 , "impossible baud rate" ) ;
335
+ usart. brr . write ( |w| unsafe { w. bits ( brr) } ) ;
348
336
349
- // Configure stop bits
350
- let stop_bits = match config. stopbits {
351
- StopBits :: STOP1 => 0b00 ,
352
- StopBits :: STOP0P5 => 0b01 ,
353
- StopBits :: STOP2 => 0b10 ,
354
- StopBits :: STOP1P5 => 0b11 ,
355
- } ;
356
- usart. cr2 . modify ( |_r, w| w. stop ( ) . bits ( stop_bits) ) ;
357
- }
337
+ // Configure word
338
+ let ( parity_is_used, parity_is_odd) = match config. parity {
339
+ Parity :: ParityNone => ( false , false ) ,
340
+ Parity :: ParityEven => ( true , false ) ,
341
+ Parity :: ParityOdd => ( true , true ) ,
342
+ } ;
343
+ usart. cr1 . modify ( |_r, w| {
344
+ w. m ( ) . bit ( match config. wordlength {
345
+ WordLength :: Bits8 => false ,
346
+ WordLength :: Bits9 => true ,
347
+ } ) ;
348
+ w. ps ( ) . bit ( parity_is_odd) ;
349
+ w. pce ( ) . bit ( parity_is_used)
350
+ } ) ;
351
+
352
+ // Configure stop bits
353
+ let stop_bits = match config. stopbits {
354
+ StopBits :: STOP1 => 0b00 ,
355
+ StopBits :: STOP0P5 => 0b01 ,
356
+ StopBits :: STOP2 => 0b10 ,
357
+ StopBits :: STOP1P5 => 0b11 ,
358
+ } ;
359
+ usart. cr2 . modify ( |_r, w| w. stop ( ) . bits ( stop_bits) ) ;
360
+ }
358
361
362
+ impl < USART , PINS > Serial < USART , PINS >
363
+ where
364
+ USART : Instance ,
365
+ {
359
366
/// Reconfigure the USART instance.
360
367
///
361
368
/// If a transmission is currently in progress, this returns
@@ -365,15 +372,33 @@ where
365
372
config : impl Into < Config > ,
366
373
clocks : & Clocks ,
367
374
) -> nb:: Result < ( ) , Infallible > {
368
- // if we're currently busy transmitting, we have to wait until that is
369
- // over -- regarding reception, we assume that the caller -- with
370
- // exclusive access to the Serial instance due to &mut self -- knows
371
- // what they're doing.
372
- self . tx . flush ( ) ?;
373
- Self :: apply_config ( & self . usart , config. into ( ) , clocks) ;
374
- Ok ( ( ) )
375
+ reconfigure ( & mut self . tx , & mut self . rx , config, clocks)
375
376
}
377
+ }
376
378
379
+ /// Reconfigure the USART instance.
380
+ ///
381
+ /// If a transmission is currently in progress, this returns
382
+ /// [`nb::Error::WouldBlock`].
383
+ pub fn reconfigure < USART : Instance > (
384
+ tx : & mut Tx < USART > ,
385
+ #[ allow( unused_variables) ] rx : & mut Rx < USART > ,
386
+ config : impl Into < Config > ,
387
+ clocks : & Clocks ,
388
+ ) -> nb:: Result < ( ) , Infallible > {
389
+ // if we're currently busy transmitting, we have to wait until that is
390
+ // over -- regarding reception, we assume that the caller -- with
391
+ // exclusive access to the Serial instance due to &mut self -- knows
392
+ // what they're doing.
393
+ tx. flush ( ) ?;
394
+ apply_config :: < USART > ( config. into ( ) , clocks) ;
395
+ Ok ( ( ) )
396
+ }
397
+
398
+ impl < USART , PINS > Serial < USART , PINS >
399
+ where
400
+ USART : Instance ,
401
+ {
377
402
/// Starts listening to the USART by enabling the _Received data
378
403
/// ready to be read (RXNE)_ interrupt and _Transmit data
379
404
/// register empty (TXE)_ interrupt
0 commit comments