Skip to content

Commit db7428d

Browse files
torkeldanielssontherealprof
authored andcommitted
Reduce the scope of some critical sections in the examples
1 parent be4250e commit db7428d

File tree

9 files changed

+194
-190
lines changed

9 files changed

+194
-190
lines changed

examples/blinky_adc.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,38 @@ use cortex_m_rt::entry;
1313
#[entry]
1414
fn main() -> ! {
1515
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
16-
cortex_m::interrupt::free(move |cs| {
17-
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
16+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
1817

19-
let gpioa = p.GPIOA.split(&mut rcc);
18+
let gpioa = p.GPIOA.split(&mut rcc);
2019

21-
// (Re-)configure PA1 as output
22-
let mut led = gpioa.pa1.into_push_pull_output(cs);
23-
24-
// (Re-)configure PA0 as analog input
25-
let mut an_in = gpioa.pa0.into_analog(cs);
20+
let (mut led, mut an_in) = cortex_m::interrupt::free(move |cs| {
21+
(
22+
// (Re-)configure PA1 as output
23+
gpioa.pa1.into_push_pull_output(cs),
24+
// (Re-)configure PA0 as analog input
25+
gpioa.pa0.into_analog(cs),
26+
)
27+
});
2628

27-
// Get delay provider
28-
let mut delay = Delay::new(cp.SYST, &rcc);
29+
// Get delay provider
30+
let mut delay = Delay::new(cp.SYST, &rcc);
2931

30-
// Get access to the ADC
31-
let mut adc = Adc::new(p.ADC, &mut rcc);
32+
// Get access to the ADC
33+
let mut adc = Adc::new(p.ADC, &mut rcc);
3234

33-
loop {
34-
led.toggle().ok();
35+
loop {
36+
led.toggle().ok();
3537

36-
let time: u16 = if let Ok(val) = adc.read(&mut an_in) as Result<u16, _> {
37-
/* shift the value right by 3, same as divide by 8, reduces
38-
the 0-4095 range into something approximating 1-512 */
39-
(val >> 3) + 1
40-
} else {
41-
1000
42-
};
38+
let time: u16 = if let Ok(val) = adc.read(&mut an_in) as Result<u16, _> {
39+
/* shift the value right by 3, same as divide by 8, reduces
40+
the 0-4095 range into something approximating 1-512 */
41+
(val >> 3) + 1
42+
} else {
43+
1000
44+
};
4345

44-
delay.delay_ms(time);
45-
}
46-
});
46+
delay.delay_ms(time);
47+
}
4748
}
4849

4950
loop {

examples/blinky_delay.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ use cortex_m_rt::entry;
1313
#[entry]
1414
fn main() -> ! {
1515
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
16-
cortex_m::interrupt::free(move |cs| {
17-
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
16+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
1817

19-
let gpioa = p.GPIOA.split(&mut rcc);
18+
let gpioa = p.GPIOA.split(&mut rcc);
2019

21-
// (Re-)configure PA1 as output
22-
let mut led = gpioa.pa1.into_push_pull_output(cs);
20+
// (Re-)configure PA1 as output
21+
let mut led = cortex_m::interrupt::free(move |cs| {
22+
gpioa.pa1.into_push_pull_output(cs);
23+
});
2324

24-
// Get delay provider
25-
let mut delay = Delay::new(cp.SYST, &rcc);
25+
// Get delay provider
26+
let mut delay = Delay::new(cp.SYST, &rcc);
2627

27-
loop {
28-
led.toggle().ok();
29-
delay.delay_ms(1_000_u16);
30-
}
31-
});
28+
loop {
29+
led.toggle().ok();
30+
delay.delay_ms(1_000_u16);
31+
}
3232
}
3333

3434
loop {

examples/blinky_multiple.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,36 @@ use cortex_m_rt::entry;
1313
#[entry]
1414
fn main() -> ! {
1515
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
16-
cortex_m::interrupt::free(move |cs| {
17-
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
18-
19-
let gpioa = p.GPIOA.split(&mut rcc);
20-
let gpiob = p.GPIOB.split(&mut rcc);
21-
22-
// (Re-)configure PA1 as output
23-
let led1 = gpioa.pa1.into_push_pull_output(cs);
24-
25-
// (Re-)configure PB1 as output
26-
let led2 = gpiob.pb1.into_push_pull_output(cs);
16+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
17+
18+
let gpioa = p.GPIOA.split(&mut rcc);
19+
let gpiob = p.GPIOB.split(&mut rcc);
20+
21+
let (led1, led2) = cortex_m::interrupt::free(move |cs| {
22+
(
23+
// (Re-)configure PA1 as output
24+
gpioa.pa1.into_push_pull_output(cs),
25+
// (Re-)configure PB1 as output
26+
gpiob.pb1.into_push_pull_output(cs),
27+
)
28+
});
2729

28-
// Get delay provider
29-
let mut delay = Delay::new(cp.SYST, &rcc);
30+
// Get delay provider
31+
let mut delay = Delay::new(cp.SYST, &rcc);
3032

31-
// Store them together after erasing the type
32-
let mut leds = [led1.downgrade(), led2.downgrade()];
33-
loop {
34-
for l in &mut leds {
35-
l.set_high().ok();
36-
}
37-
delay.delay_ms(1_000_u16);
33+
// Store them together after erasing the type
34+
let mut leds = [led1.downgrade(), led2.downgrade()];
35+
loop {
36+
for l in &mut leds {
37+
l.set_high().ok();
38+
}
39+
delay.delay_ms(1_000_u16);
3840

39-
for l in &mut leds {
40-
l.set_low().ok();
41-
}
42-
delay.delay_ms(1_000_u16);
41+
for l in &mut leds {
42+
l.set_low().ok();
4343
}
44-
});
44+
delay.delay_ms(1_000_u16);
45+
}
4546
}
4647

4748
loop {

examples/blinky_timer.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,22 @@ use cortex_m_rt::entry;
1212
#[entry]
1313
fn main() -> ! {
1414
if let Some(mut p) = pac::Peripherals::take() {
15-
cortex_m::interrupt::free(move |cs| {
16-
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
15+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
1716

18-
let gpioa = p.GPIOA.split(&mut rcc);
17+
let gpioa = p.GPIOA.split(&mut rcc);
1918

20-
// (Re-)configure PA1 as output
21-
let mut led = gpioa.pa1.into_push_pull_output(cs);
19+
// (Re-)configure PA1 as output
20+
let mut led = cortex_m::interrupt::free(move |cs| gpioa.pa1.into_push_pull_output(cs));
2221

23-
// Set up a timer expiring after 1s
24-
let mut timer = Timer::tim1(p.TIM1, Hertz(1), &mut rcc);
22+
// Set up a timer expiring after 1s
23+
let mut timer = Timer::tim1(p.TIM1, Hertz(1), &mut rcc);
2524

26-
loop {
27-
led.toggle().ok();
25+
loop {
26+
led.toggle().ok();
2827

29-
// Wait for the timer to expire
30-
nb::block!(timer.wait()).ok();
31-
}
32-
});
28+
// Wait for the timer to expire
29+
nb::block!(timer.wait()).ok();
30+
}
3331
}
3432

3533
loop {

examples/dac.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,37 @@ enum Direction {
2222
#[entry]
2323
fn main() -> ! {
2424
if let (Some(mut dp), Some(_cp)) = (pac::Peripherals::take(), cortex_m::Peripherals::take()) {
25-
cortex_m::interrupt::free(move |cs| {
26-
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
25+
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
2726

27+
let mut dac = cortex_m::interrupt::free(move |cs| {
2828
let gpioa = dp.GPIOA.split(&mut rcc);
29-
let mut dac = dac(dp.DAC, gpioa.pa4.into_analog(cs), &mut rcc);
29+
dac(dp.DAC, gpioa.pa4.into_analog(cs), &mut rcc)
30+
});
3031

31-
dac.enable();
32+
dac.enable();
3233

33-
let mut dir = Direction::Upcounting;
34-
let mut val = 0;
34+
let mut dir = Direction::Upcounting;
35+
let mut val = 0;
3536

36-
dac.set_value(2058);
37-
cortex_m::asm::bkpt();
37+
dac.set_value(2058);
38+
cortex_m::asm::bkpt();
3839

39-
dac.set_value(4095);
40-
cortex_m::asm::bkpt();
40+
dac.set_value(4095);
41+
cortex_m::asm::bkpt();
4142

42-
loop {
43-
dac.set_value(val);
44-
match val {
45-
0 => dir = Direction::Upcounting,
46-
4095 => dir = Direction::Downcounting,
47-
_ => (),
48-
};
43+
loop {
44+
dac.set_value(val);
45+
match val {
46+
0 => dir = Direction::Upcounting,
47+
4095 => dir = Direction::Downcounting,
48+
_ => (),
49+
};
4950

50-
match dir {
51-
Direction::Upcounting => val += 1,
52-
Direction::Downcounting => val -= 1,
53-
}
51+
match dir {
52+
Direction::Upcounting => val += 1,
53+
Direction::Downcounting => val -= 1,
5454
}
55-
});
55+
}
5656
}
5757

5858
loop {

examples/serial_echo.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,27 @@ use cortex_m_rt::entry;
1212
#[entry]
1313
fn main() -> ! {
1414
if let Some(p) = pac::Peripherals::take() {
15-
cortex_m::interrupt::free(move |cs| {
16-
let mut flash = p.FLASH;
17-
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut flash);
15+
let mut flash = p.FLASH;
16+
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut flash);
1817

19-
let gpioa = p.GPIOA.split(&mut rcc);
18+
let gpioa = p.GPIOA.split(&mut rcc);
2019

21-
let tx = gpioa.pa9.into_alternate_af1(cs);
22-
let rx = gpioa.pa10.into_alternate_af1(cs);
20+
let (tx, rx) = cortex_m::interrupt::free(move |cs| {
21+
(
22+
gpioa.pa9.into_alternate_af1(cs),
23+
gpioa.pa10.into_alternate_af1(cs),
24+
)
25+
});
2326

24-
let mut serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
27+
let mut serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
2528

26-
loop {
27-
// Wait for reception of a single byte
28-
let received = nb::block!(serial.read()).unwrap();
29+
loop {
30+
// Wait for reception of a single byte
31+
let received = nb::block!(serial.read()).unwrap();
2932

30-
// Send back previously received byte and wait for completion
31-
nb::block!(serial.write(received)).ok();
32-
}
33-
});
33+
// Send back previously received byte and wait for completion
34+
nb::block!(serial.write(received)).ok();
35+
}
3436
}
3537

3638
loop {

examples/serial_spi_bridge.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,37 @@ fn main() -> ! {
3131
};
3232

3333
if let Some(p) = pac::Peripherals::take() {
34-
cortex_m::interrupt::free(move |cs| {
35-
let mut flash = p.FLASH;
36-
let mut rcc = p.RCC.configure().freeze(&mut flash);
34+
let mut flash = p.FLASH;
35+
let mut rcc = p.RCC.configure().freeze(&mut flash);
3736

37+
let (sck, miso, mosi, tx, rx) = cortex_m::interrupt::free(move |cs| {
3838
let gpioa = p.GPIOA.split(&mut rcc);
3939

40-
// Configure pins for SPI
41-
let sck = gpioa.pa5.into_alternate_af0(cs);
42-
let miso = gpioa.pa6.into_alternate_af0(cs);
43-
let mosi = gpioa.pa7.into_alternate_af0(cs);
44-
45-
// Configure SPI with 1MHz rate
46-
let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), MODE, 1.mhz(), &mut rcc);
40+
(
41+
// SPI pins
42+
gpioa.pa5.into_alternate_af0(cs),
43+
gpioa.pa6.into_alternate_af0(cs),
44+
gpioa.pa7.into_alternate_af0(cs),
45+
// USART pins
46+
gpioa.pa9.into_alternate_af1(cs),
47+
gpioa.pa10.into_alternate_af1(cs),
48+
)
49+
});
4750

48-
let tx = gpioa.pa9.into_alternate_af1(cs);
49-
let rx = gpioa.pa10.into_alternate_af1(cs);
51+
// Configure SPI with 1MHz rate
52+
let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), MODE, 1.mhz(), &mut rcc);
5053

51-
let serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
54+
let serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
5255

53-
let (mut tx, mut rx) = serial.split();
56+
let (mut tx, mut rx) = serial.split();
5457

55-
let mut data = [0];
56-
loop {
57-
let serial_received = block!(rx.read()).unwrap();
58-
spi.write(&[serial_received]).ok();
59-
let spi_received = spi.transfer(&mut data).unwrap();
60-
block!(tx.write(spi_received[0])).ok();
61-
}
62-
});
58+
let mut data = [0];
59+
loop {
60+
let serial_received = block!(rx.read()).unwrap();
61+
spi.write(&[serial_received]).ok();
62+
let spi_received = spi.transfer(&mut data).unwrap();
63+
block!(tx.write(spi_received[0])).ok();
64+
}
6365
}
6466

6567
loop {

0 commit comments

Comments
 (0)