@@ -490,17 +490,59 @@ macro_rules! gpio {
490
490
}
491
491
}
492
492
493
- // TODO: Add the stateful versions
494
- macro_rules! impl_temp_mode {
493
+ macro_rules! impl_temp_output {
495
494
(
496
495
$fn_name: ident,
496
+ $stateful_fn_name: ident,
497
497
$mode: ty
498
498
) => {
499
499
/**
500
500
Temporarily change the mode of the pin.
501
501
502
- When changing to an output, the value of the pin is
503
- undefined.
502
+ The value of the pin after conversion is undefined. If you
503
+ want to control it, use `$stateful_fn_name`
504
+ */
505
+ pub fn $fn_name(
506
+ & mut self ,
507
+ cr: & mut $CR,
508
+ mut f: impl FnMut ( & mut $PXi<$mode>)
509
+ ) {
510
+ let mut temp = unsafe { $PXi:: <$mode>:: set_mode( cr) } ;
511
+ f( & mut temp) ;
512
+ unsafe {
513
+ Self :: set_mode( cr) ;
514
+ }
515
+ }
516
+
517
+ /**
518
+ Temporarily change the mode of the pin.
519
+
520
+ Note that the new state is set slightly before conversion
521
+ happens. This can cause a short output glitch if switching
522
+ between output modes
523
+ */
524
+ pub fn $stateful_fn_name(
525
+ & mut self ,
526
+ cr: & mut $CR,
527
+ state: State ,
528
+ mut f: impl FnMut ( & mut $PXi<$mode>)
529
+ ) {
530
+ self . set_state( state) ;
531
+ let mut temp = unsafe { $PXi:: <$mode>:: set_mode( cr) } ;
532
+ f( & mut temp) ;
533
+ unsafe {
534
+ Self :: set_mode( cr) ;
535
+ }
536
+ }
537
+ }
538
+ }
539
+ macro_rules! impl_temp_input {
540
+ (
541
+ $fn_name: ident,
542
+ $mode: ty
543
+ ) => {
544
+ /**
545
+ Temporarily change the mode of the pin.
504
546
*/
505
547
pub fn $fn_name(
506
548
& mut self ,
@@ -517,23 +559,25 @@ macro_rules! gpio {
517
559
}
518
560
519
561
impl <MODE > $PXi<MODE > where MODE : Active , $PXi<MODE >: PinMode <$CR> {
520
- impl_temp_mode !(
562
+ impl_temp_output !(
521
563
as_push_pull_output,
564
+ as_push_pull_output_with_state,
522
565
Output <PushPull >
523
566
) ;
524
- impl_temp_mode !(
567
+ impl_temp_output !(
525
568
as_open_drain_output,
569
+ as_open_drain_output_with_state,
526
570
Output <OpenDrain >
527
571
) ;
528
- impl_temp_mode !(
572
+ impl_temp_input !(
529
573
as_floating_input,
530
574
Input <Floating >
531
575
) ;
532
- impl_temp_mode !(
576
+ impl_temp_input !(
533
577
as_pull_up_input,
534
578
Input <PullUp >
535
579
) ;
536
- impl_temp_mode !(
580
+ impl_temp_input !(
537
581
as_pull_down_input,
538
582
Input <PullDown >
539
583
) ;
0 commit comments