Skip to content

Commit f234f5e

Browse files
committed
GPIO - Use nice new pac methods
1 parent 899c279 commit f234f5e

File tree

1 file changed

+77
-90
lines changed

1 file changed

+77
-90
lines changed

src/gpio.rs

Lines changed: 77 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ macro_rules! gpio {
277277

278278
fn set_high(&mut self) -> Result<(), ()> {
279279
// NOTE(unsafe) atomic write to a stateless register
280-
unsafe { (*$GPIOX::ptr()).bsrr().write(|w| w.bits(1 << self.i)) };
280+
unsafe { (*$GPIOX::ptr()).bsrr().write(|w| w.bs(self.i).set_bit()) };
281281
Ok(())
282282
}
283283

284284
fn set_low(&mut self) -> Result<(), ()> {
285285
// NOTE(unsafe) atomic write to a stateless register
286-
unsafe { (*$GPIOX::ptr()).bsrr().write(|w| w.bits(1 << (self.i + 16))) };
286+
unsafe { (*$GPIOX::ptr()).bsrr().write(|w| w.br(self.i).set_bit()) };
287287
Ok(())
288288
}
289289
}
@@ -296,7 +296,7 @@ macro_rules! gpio {
296296

297297
fn is_set_low(&self) -> Result<bool, ()> {
298298
// NOTE(unsafe) atomic read with no side effects
299-
let is_set_low = unsafe { (*$GPIOX::ptr()).odr().read().bits() & (1 << self.i) == 0 };
299+
let is_set_low = unsafe { (*$GPIOX::ptr()).odr().read().odr(self.i).is_low() };
300300
Ok(is_set_low)
301301
}
302302
}
@@ -314,7 +314,7 @@ macro_rules! gpio {
314314

315315
fn is_low(&self) -> Result<bool, ()> {
316316
// NOTE(unsafe) atomic read with no side effects
317-
let is_low = unsafe { (*$GPIOX::ptr()).idr().read().bits() & (1 << self.i) == 0 };
317+
let is_low = unsafe { (*$GPIOX::ptr()).idr().read().idr(self.i).is_low() };
318318
Ok(is_low)
319319
}
320320
}
@@ -329,7 +329,7 @@ macro_rules! gpio {
329329

330330
fn is_low(&self) -> Result<bool, ()> {
331331
// NOTE(unsafe) atomic read with no side effects
332-
let is_low = unsafe { (*$GPIOX::ptr()).idr().read().bits() & (1 << self.i) == 0 };
332+
let is_low = unsafe { (*$GPIOX::ptr()).idr().read().idr(self.i).is_low() };
333333
Ok(is_low)
334334
}
335335
}
@@ -382,97 +382,89 @@ macro_rules! gpio {
382382
impl<MODE> $PXi<MODE> {
383383
/// Configures the pin to operate as a floating input pin
384384
pub fn into_floating_input(self) -> $PXi<Input<Floating>> {
385-
let offset = 2 * $i;
386-
unsafe {
385+
cortex_m::interrupt::free(|_| unsafe {
387386
let gpio = &(*$GPIOX::ptr());
388-
gpio.pupdr().modify(|r, w| {
389-
w.bits(r.bits() & !(0b11 << offset))
387+
gpio.pupdr().modify(|_, w| {
388+
w.pupdr($i).floating()
390389
});
391-
gpio.moder().modify(|r, w| {
392-
w.bits(r.bits() & !(0b11 << offset))
390+
gpio.moder().modify(|_, w| {
391+
w.moder($i).input()
393392
})
394-
};
393+
});
395394
$PXi { _mode: PhantomData }
396395
}
397396

398397
/// Configures the pin to operate as a pulled down input pin
399398
pub fn into_pull_down_input(self) -> $PXi<Input<PullDown>> {
400-
let offset = 2 * $i;
401-
unsafe {
399+
cortex_m::interrupt::free(|_| unsafe {
402400
let gpio = &(*$GPIOX::ptr());
403-
gpio.pupdr().modify(|r, w| {
404-
w.bits((r.bits() & !(0b11 << offset)) | (0b10 << offset))
401+
gpio.pupdr().modify(|_, w| {
402+
w.pupdr($i).pull_down()
405403
});
406-
gpio.moder().modify(|r, w| {
407-
w.bits(r.bits() & !(0b11 << offset))
408-
})
409-
};
404+
405+
});
410406
$PXi { _mode: PhantomData }
411407
}
412408

413409
/// Configures the pin to operate as a pulled up input pin
414410
pub fn into_pull_up_input(self) -> $PXi<Input<PullUp>> {
415-
let offset = 2 * $i;
416-
unsafe {
411+
cortex_m::interrupt::free(|_| unsafe {
417412
let gpio = &(*$GPIOX::ptr());
418-
gpio.pupdr().modify(|r, w| {
419-
w.bits((r.bits() & !(0b11 << offset)) | (0b01 << offset))
413+
gpio.pupdr().modify(|_, w| {
414+
w.pupdr($i).pull_up()
420415
});
421-
gpio.moder().modify(|r, w| {
422-
w.bits(r.bits() & !(0b11 << offset))
416+
gpio.moder().modify(|_, w| {
417+
w.moder($i).input()
423418
})
424-
};
419+
});
425420
$PXi { _mode: PhantomData }
426421
}
427422

428423
/// Configures the pin to operate as an analog pin
429424
pub fn into_analog(self) -> $PXi<Analog> {
430-
let offset = 2 * $i;
431-
unsafe {
425+
cortex_m::interrupt::free(|_| unsafe {
432426
let gpio = &(*$GPIOX::ptr());
433-
gpio.pupdr().modify(|r, w| {
434-
w.bits(r.bits() & !(0b11 << offset))
427+
gpio.pupdr().modify(|_, w| {
428+
w.pupdr($i).floating()
435429
});
436-
gpio.moder().modify(|r, w| {
437-
w.bits((r.bits() & !(0b11 << offset)) | (0b11 << offset))
438-
});
439-
}
430+
gpio.moder().modify(|_, w| {
431+
w.moder($i).analog()
432+
})
433+
});
440434
$PXi { _mode: PhantomData }
441435
}
442436

443437
/// Configures the pin to operate as an open drain output pin
444438
pub fn into_open_drain_output(self) -> $PXi<Output<OpenDrain>> {
445-
let offset = 2 * $i;
446-
unsafe {
439+
cortex_m::interrupt::free(|_| unsafe {
447440
let gpio = &(*$GPIOX::ptr());
448-
gpio.pupdr().modify(|r, w| {
449-
w.bits(r.bits() & !(0b11 << offset))
441+
gpio.pupdr().modify(|_, w| {
442+
w.pupdr($i).floating()
450443
});
451-
gpio.otyper().modify(|r, w| {
452-
w.bits(r.bits() | (0b1 << $i))
444+
gpio.otyper().modify(|_, w| {
445+
w.ot($i).open_drain()
453446
});
454-
gpio.moder().modify(|r, w| {
455-
w.bits((r.bits() & !(0b11 << offset)) | (0b01 << offset))
456-
})
457-
};
447+
gpio.moder().modify(|_, w| {
448+
w.moder($i).output()
449+
});
450+
});
458451
$PXi { _mode: PhantomData }
459452
}
460453

461454
/// Configures the pin to operate as an push pull output pin
462455
pub fn into_push_pull_output(self) -> $PXi<Output<PushPull>> {
463-
let offset = 2 * $i;
464-
unsafe {
456+
cortex_m::interrupt::free(|_| unsafe {
465457
let gpio = &(*$GPIOX::ptr());
466-
gpio.pupdr().modify(|r, w| {
467-
w.bits(r.bits() & !(0b11 << offset))
458+
gpio.pupdr().modify(|_, w| {
459+
w.pupdr($i).floating()
468460
});
469-
gpio.otyper().modify(|r, w| {
470-
w.bits(r.bits() & !(0b1 << $i))
461+
gpio.otyper().modify(|_, w| {
462+
w.ot($i).push_pull()
471463
});
472-
gpio.moder().modify(|r, w| {
473-
w.bits((r.bits() & !(0b11 << offset)) | (0b01 << offset))
474-
})
475-
};
464+
gpio.moder().modify(|_, w| {
465+
w.moder($i).output()
466+
});
467+
});
476468
$PXi { _mode: PhantomData }
477469
}
478470

@@ -484,64 +476,59 @@ macro_rules! gpio {
484476

485477
/// Set pin speed
486478
pub fn set_speed(self, speed: Speed) -> Self {
487-
let offset = 2 * $i;
488-
unsafe {
489-
(*$GPIOX::ptr()).ospeedr().modify(|r, w| {
490-
w.bits((r.bits() & !(0b11 << offset)) | ((speed as u32) << offset))
479+
cortex_m::interrupt::free(|_| unsafe {
480+
(*$GPIOX::ptr()).ospeedr().modify(|_, w| {
481+
w.ospeedr($i).set(speed as u8)
491482
});
492-
}
483+
});
493484
self
494485
}
495486

496487
pub fn into_alternate<const A: u8>(self) -> $PXi<Alternate<A>> {
497-
let mode = A as u32;
498-
let offset = 2 * $i;
499-
let offset2 = 4 * $i;
500-
unsafe {
488+
let mode = A as u8;
489+
cortex_m::interrupt::free(|_| unsafe {
501490
let gpio = &(*$GPIOX::ptr());
502-
if offset2 < 32 {
503-
gpio.afrl().modify(|r, w| {
504-
w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))
491+
if $i < 8 {
492+
gpio.afrl().modify(|_, w| {
493+
w.afr($i).set(mode)
505494
});
506495
} else {
507-
let offset2 = offset2 - 32;
508-
gpio.afrh().modify(|r, w| {
509-
w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))
496+
let i = $i - 8;
497+
gpio.afrh().modify(|_, w| {
498+
w.afr(i).set(mode)
510499
});
511500
}
512-
gpio.moder().modify(|r, w| {
513-
w.bits((r.bits() & !(0b11 << offset)) | (0b10 << offset))
501+
gpio.moder().modify(|_, w| {
502+
w.moder($i).alternate()
514503
});
515-
gpio.otyper().modify(|r, w| {
516-
w.bits(r.bits() & !(0b1 << $i))
504+
gpio.otyper().modify(|_, w| {
505+
w.ot($i).push_pull()
517506
});
518-
}
507+
});
519508
$PXi { _mode: PhantomData }
520509
}
521510

522511
pub fn into_alternate_open_drain<const A: u8>(self) -> $PXi<AlternateOD<A>> {
523-
let mode = A as u32;
524-
let offset = 2 * $i;
525-
let offset2 = 4 * $i;
526-
unsafe {
512+
let mode = A as u8;
513+
cortex_m::interrupt::free(|_| unsafe {
527514
let gpio = &(*$GPIOX::ptr());
528-
if offset2 < 32 {
529-
gpio.afrl().modify(|r, w| {
530-
w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))
515+
if $i < 8 {
516+
gpio.afrl().modify(|_, w| {
517+
w.afr($i).set(mode)
531518
});
532519
} else {
533-
let offset2 = offset2 - 32;
534-
gpio.afrh().modify(|r, w| {
535-
w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))
520+
let i = $i - 8;
521+
gpio.afrh().modify(|_, w| {
522+
w.afr(i).set(mode)
536523
});
537524
}
538-
gpio.otyper().modify(|r, w| {
539-
w.bits(r.bits() | (0b1 << $i))
525+
gpio.moder().modify(|_, w| {
526+
w.moder($i).alternate()
540527
});
541-
gpio.moder().modify(|r, w| {
542-
w.bits((r.bits() & !(0b11 << offset)) | (0b10 << offset))
528+
gpio.otyper().modify(|_, w| {
529+
w.ot($i).open_drain()
543530
});
544-
}
531+
});
545532
$PXi { _mode: PhantomData }
546533
}
547534
}

0 commit comments

Comments
 (0)