Skip to content

Commit d230335

Browse files
committed
Use set instead of bits
1 parent cf5a479 commit d230335

24 files changed

+230
-325
lines changed

examples/qspi_mdma.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ fn main() -> ! {
7070
);
7171
qspi.configure_mode(QspiMode::FourBit).unwrap();
7272
// Disable address phase
73-
qspi.inner_mut()
74-
.ccr()
75-
.modify(|_, w| unsafe { w.admode().bits(0) });
73+
qspi.inner_mut().ccr().modify(|_, w| w.admode().set(0));
7674

7775
// Source buffer in TCM
7876
let mut source_buffer: [u8; 80] = [0x4A; 80];

src/adc.rs

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -767,33 +767,31 @@ macro_rules! adc_hal {
767767
fn set_chan_smp(&mut self, chan: u8) {
768768
let t = self.get_sample_time().into();
769769
if chan <= 9 {
770-
//NOTE(unsafe) Only valid bit patterns written
771-
self.rb.smpr1().modify(|_, w| unsafe { match chan {
772-
0 => w.smp0().bits(t),
773-
1 => w.smp1().bits(t),
774-
2 => w.smp2().bits(t),
775-
3 => w.smp3().bits(t),
776-
4 => w.smp4().bits(t),
777-
5 => w.smp5().bits(t),
778-
6 => w.smp6().bits(t),
779-
7 => w.smp7().bits(t),
780-
8 => w.smp8().bits(t),
781-
9 => w.smp9().bits(t),
770+
self.rb.smpr1().modify(|_, w| { match chan {
771+
0 => w.smp0().set(t),
772+
1 => w.smp1().set(t),
773+
2 => w.smp2().set(t),
774+
3 => w.smp3().set(t),
775+
4 => w.smp4().set(t),
776+
5 => w.smp5().set(t),
777+
6 => w.smp6().set(t),
778+
7 => w.smp7().set(t),
779+
8 => w.smp8().set(t),
780+
9 => w.smp9().set(t),
782781
_ => unreachable!(),
783782
}});
784783
} else {
785-
//NOTE(unsafe) Only valid bit patterns written
786-
self.rb.smpr2().modify(|_, w| unsafe { match chan {
787-
10 => w.smp10().bits(t),
788-
11 => w.smp11().bits(t),
789-
12 => w.smp12().bits(t),
790-
13 => w.smp13().bits(t),
791-
14 => w.smp14().bits(t),
792-
15 => w.smp15().bits(t),
793-
16 => w.smp16().bits(t),
794-
17 => w.smp17().bits(t),
795-
18 => w.smp18().bits(t),
796-
19 => w.smp19().bits(t),
784+
self.rb.smpr2().modify(|_, w| { match chan {
785+
10 => w.smp10().set(t),
786+
11 => w.smp11().set(t),
787+
12 => w.smp12().set(t),
788+
13 => w.smp13().set(t),
789+
14 => w.smp14().set(t),
790+
15 => w.smp15().set(t),
791+
16 => w.smp16().set(t),
792+
17 => w.smp17().set(t),
793+
18 => w.smp18().set(t),
794+
19 => w.smp19().set(t),
797795
_ => unreachable!(),
798796
}});
799797
}
@@ -805,16 +803,14 @@ macro_rules! adc_hal {
805803

806804
// Set LSHIFT[3:0]
807805
//NOTE(unsafe) Only valid bit patterns written
808-
unsafe {
809-
self.rb.cfgr2().modify(|_, w| w.lshift().bits(self.get_lshift().value()));
810-
}
806+
self.rb.cfgr2().modify(|_, w| w.lshift().set(self.get_lshift().value()));
811807

812808
// Select channel (with preselection, refer to RM0433 Rev 7 - Chapter 25.4.12)
813809
self.rb.pcsel().modify(|r, w| unsafe { w.pcsel().bits(r.pcsel().bits() | (1 << chan)) });
814810
self.set_chan_smp(chan);
815811
self.rb.sqr1().modify(|_, w| unsafe {
816812
w.sq1().bits(chan)
817-
.l().bits(0)
813+
.l().set(0)
818814
});
819815
self.current_channel = Some(chan);
820816

@@ -854,14 +850,10 @@ macro_rules! adc_hal {
854850
// Set resolution
855851
self.rb.cfgr().modify(|_, w| unsafe { w.res().bits(self.get_resolution().into()) });
856852

857-
858-
//NOTE(unsafe) Only valid bit patterns written
859-
unsafe {
860-
self.rb.cfgr().modify(|_, w| w.dmngt().bits(match mode {
861-
AdcDmaMode::OneShot => 0b01,
862-
AdcDmaMode::Circular => 0b11,
863-
}));
864-
}
853+
self.rb.cfgr().modify(|_, w| w.dmngt().set(match mode {
854+
AdcDmaMode::OneShot => 0b01,
855+
AdcDmaMode::Circular => 0b11,
856+
}));
865857

866858
// Set continuous mode
867859
self.rb.cfgr().modify(|_, w| w.cont().set_bit().discen().clear_bit() );

src/crc.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,18 @@ impl Crc {
3838
// manual says unit must be reset (or DR read) before change of polynomial
3939
// (technically only in case of ongoing calculation, but DR is buffered)
4040
//NOTE(unsafe) Only valid bit patterns are written
41-
self.reg.cr().modify(|_, w| unsafe {
41+
self.reg.cr().modify(|_, w| {
4242
w.polysize()
43-
.bits(config.poly.polysize())
43+
.set(config.poly.polysize())
4444
.rev_in()
45-
.bits(config.get_reverse_input())
45+
.set(config.get_reverse_input())
4646
.rev_out()
4747
.bit(config.reverse_output)
4848
.reset()
4949
.set_bit()
5050
});
51-
//NOTE(unsafe) All bit patterns are valid
52-
self.reg
53-
.pol()
54-
.write(|w| unsafe { w.pol().bits(config.poly.pol()) });
55-
//NOTE(unsafe) All bit patterns are valid
56-
self.reg
57-
.init()
58-
.write(|w| unsafe { w.init().bits(config.initial) });
51+
self.reg.pol().write(|w| w.pol().set(config.poly.pol()));
52+
self.reg.init().write(|w| w.init().set(config.initial));
5953
// writing to INIT sets DR to its value
6054
}
6155

@@ -67,23 +61,18 @@ impl Crc {
6761
let mut words = data.chunks_exact(4);
6862
for word in words.by_ref() {
6963
let word = u32::from_be_bytes(word.try_into().unwrap());
70-
//NOTE(unsafe) All bit patterns are valid
71-
self.reg.dr().write(|w| unsafe { w.dr().bits(word) });
64+
self.reg.dr().write(|w| w.dr().set(word));
7265
}
7366

7467
// there will be at most 3 bytes remaining, so 1 half-word and 1 byte
7568
let mut half_word = words.remainder().chunks_exact(2);
7669
if let Some(half_word) = half_word.next() {
7770
let half_word = u16::from_be_bytes(half_word.try_into().unwrap());
78-
//NOTE(unsafe) All bit patterns are valid
79-
self.reg
80-
.dr16()
81-
.write(|w| unsafe { w.dr16().bits(half_word) });
71+
self.reg.dr16().write(|w| w.dr16().set(half_word));
8272
}
8373

8474
if let Some(byte) = half_word.remainder().first() {
85-
//NOTE(unsafe) All bit patterns are valid
86-
self.reg.dr8().write(|w| unsafe { w.dr8().bits(*byte) });
75+
self.reg.dr8().write(|w| w.dr8().set(*byte));
8776
}
8877
}
8978

@@ -136,7 +125,7 @@ impl Crc {
136125
/// The IDR is not involved with CRC calculation.
137126
pub fn set_idr(&mut self, value: u32) {
138127
//NOTE(unsafe) All bit patterns are valid
139-
self.reg.idr().write(|w| unsafe { w.idr().bits(value) });
128+
self.reg.idr().write(|w| w.idr().set(value));
140129
}
141130

142131
/// Get the current value of the independent data register.

src/dma/bdma.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ where
357357
unsafe {
358358
Self::stream()
359359
.cr()
360-
.modify(|_, w| w.pl().bits(priority.bits()));
360+
.modify(|_, w| w.pl().set(priority.bits()));
361361
}
362362
}
363363

@@ -466,7 +466,7 @@ where
466466
//NOTE(unsafe) We only access the registers that belongs to the StreamX
467467
//NOTE(unsafe) All bit patterns are valid for ndt
468468
unsafe {
469-
Self::stream().ndtr().write(|w| w.ndt().bits(value));
469+
Self::stream().ndtr().write(|w| w.ndt().set(value));
470470
}
471471
}
472472
#[inline(always)]

src/dma/dma.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl<I: Instance, const S: u8> StreamX<I, S> {
356356
unsafe {
357357
Self::stream()
358358
.fcr()
359-
.modify(|_, w| w.fth().bits(fifo_threshold.bits()));
359+
.modify(|_, w| w.fth().set(fifo_threshold.bits()));
360360
}
361361
}
362362

@@ -376,7 +376,7 @@ impl<I: Instance, const S: u8> StreamX<I, S> {
376376
unsafe {
377377
Self::stream()
378378
.cr()
379-
.modify(|_, w| w.mburst().bits(memory_burst.bits()));
379+
.modify(|_, w| w.mburst().set(memory_burst.bits()));
380380
}
381381
}
382382

@@ -387,7 +387,7 @@ impl<I: Instance, const S: u8> StreamX<I, S> {
387387
unsafe {
388388
Self::stream()
389389
.cr()
390-
.modify(|_, w| w.pburst().bits(peripheral_burst.bits()));
390+
.modify(|_, w| w.pburst().set(peripheral_burst.bits()));
391391
}
392392
}
393393

@@ -518,7 +518,7 @@ where
518518
unsafe {
519519
Self::stream()
520520
.cr()
521-
.modify(|_, w| w.pl().bits(priority.bits()));
521+
.modify(|_, w| w.pl().set(priority.bits()));
522522
}
523523
}
524524

@@ -645,7 +645,7 @@ where
645645
//NOTE(unsafe) We only access the registers that belongs to the StreamX
646646
//NOTE(unsafe) All bit pattern for ndt are valid
647647
unsafe {
648-
Self::stream().ndtr().write(|w| w.ndt().bits(value));
648+
Self::stream().ndtr().write(|w| w.ndt().set(value));
649649
}
650650
}
651651

src/gpio.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,9 @@ where
317317
let offset = 2 * { N };
318318

319319
unsafe {
320-
(*Gpio::<P>::ptr()).ospeedr().modify(|r, w| {
321-
w.bits(
322-
(r.bits() & !(0b11 << offset)) | ((speed as u32) << offset),
323-
)
324-
});
320+
(*Gpio::<P>::ptr())
321+
.ospeedr()
322+
.modify(|_, w| w.ospeedr(offset).set(speed as u8));
325323
}
326324
}
327325

@@ -339,11 +337,11 @@ where
339337
/// Set the internal pull-up and pull-down resistor
340338
pub fn set_internal_resistor(&mut self, resistor: Pull) {
341339
let offset = 2 * { N };
342-
let value = resistor as u32;
340+
let value = resistor as u8;
343341
unsafe {
344-
(*Gpio::<P>::ptr()).pupdr().modify(|r, w| {
345-
w.bits((r.bits() & !(0b11 << offset)) | (value << offset))
346-
});
342+
(*Gpio::<P>::ptr())
343+
.pupdr()
344+
.modify(|_, w| w.pupdr(offset).bits(value));
347345
}
348346
}
349347

@@ -428,25 +426,25 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
428426
fn _set_high(&mut self) {
429427
// NOTE(unsafe) atomic write to a stateless register
430428
unsafe {
431-
(*Gpio::<P>::ptr()).bsrr().write(|w| w.bits(1 << N));
429+
(*Gpio::<P>::ptr()).bsrr().write(|w| w.bs(N).set_bit());
432430
}
433431
}
434432
#[inline(always)]
435433
fn _set_low(&mut self) {
436434
// NOTE(unsafe) atomic write to a stateless register
437435
unsafe {
438-
(*Gpio::<P>::ptr()).bsrr().write(|w| w.bits(1 << (16 + N)));
436+
(*Gpio::<P>::ptr()).bsrr().write(|w| w.br(N).set_bit());
439437
}
440438
}
441439
#[inline(always)]
442440
fn _is_set_low(&self) -> bool {
443441
// NOTE(unsafe) atomic read with no side effects
444-
unsafe { (*Gpio::<P>::ptr()).odr().read().bits() & (1 << N) == 0 }
442+
unsafe { (*Gpio::<P>::ptr()).odr().read().odr(N).is_low() }
445443
}
446444
#[inline(always)]
447445
fn _is_low(&self) -> bool {
448446
// NOTE(unsafe) atomic read with no side effects
449-
unsafe { (*Gpio::<P>::ptr()).idr().read().bits() & (1 << N) == 0 }
447+
unsafe { (*Gpio::<P>::ptr()).idr().read().idr(N).is_low() }
450448
}
451449
}
452450

0 commit comments

Comments
 (0)