Skip to content

Commit c6b96c3

Browse files
authored
Merge pull request #41 from HarkonenBade/down-with-ub
Apply critical sections to uses of registers where exclusivity is not guaranteed
2 parents f54423a + 7ea2af8 commit c6b96c3

23 files changed

+461
-506
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
### Changed
1616

1717
- Optimize delay implemenation (#42) - @david-sawatzke
18+
- Enforced more rigorous safety guarentees (#41 - Very breaking change) - @HarkonenBade
1819

1920
### Fixed
2021

examples/adc_values.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,41 @@ fn main() -> ! {
2929
hal::stm32::Peripherals::take(),
3030
cortex_m::peripheral::Peripherals::take(),
3131
) {
32-
let gpioa = p.GPIOA.split();
33-
let rcc = p.RCC.constrain();
34-
let clocks = rcc.cfgr.sysclk(8.mhz()).freeze();
32+
cortex_m::interrupt::free(move |cs| {
33+
let mut flash = p.FLASH;
34+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
3535

36-
let mut syst = cp.SYST;
36+
let gpioa = p.GPIOA.split(&mut rcc);
3737

38-
// Set source for SysTick counter, here full operating frequency (== 8MHz)
39-
syst.set_clock_source(Core);
38+
let mut syst = cp.SYST;
4039

41-
// Set reload value, i.e. timer delay 8 MHz/counts
42-
syst.set_reload(8_000_000 - 1);
40+
// Set source for SysTick counter, here full operating frequency (== 8MHz)
41+
syst.set_clock_source(Core);
4342

44-
// Start SysTick counter
45-
syst.enable_counter();
43+
// Set reload value, i.e. timer delay 8 MHz/counts
44+
syst.set_reload(8_000_000 - 1);
4645

47-
// Start SysTick interrupt generation
48-
syst.enable_interrupt();
46+
// Start SysTick counter
47+
syst.enable_counter();
4948

50-
// USART1 at PA9 (TX) and PA10(RX)
51-
let tx = gpioa.pa9.into_alternate_af1();
52-
let rx = gpioa.pa10.into_alternate_af1();
49+
// Start SysTick interrupt generation
50+
syst.enable_interrupt();
5351

54-
// Initialiase UART
55-
let (mut tx, _) =
56-
hal::serial::Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), clocks).split();
52+
// USART1 at PA9 (TX) and PA10(RX)
53+
let tx = gpioa.pa9.into_alternate_af1(cs);
54+
let rx = gpioa.pa10.into_alternate_af1(cs);
5755

58-
// Initialise ADC
59-
let adc = hal::adc::Adc::new(p.ADC);
56+
// Initialiase UART
57+
let (mut tx, _) =
58+
hal::serial::Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc).split();
6059

61-
// Output a friendly greeting
62-
tx.write_str("\n\rThis ADC example will read various values using the ADC and print them out to the serial terminal\r\n").ok();
60+
// Initialise ADC
61+
let adc = hal::adc::Adc::new(p.ADC, &mut rcc);
6362

64-
// Move all components under Mutex supervision
65-
cortex_m::interrupt::free(move |cs| {
63+
// Output a friendly greeting
64+
tx.write_str("\n\rThis ADC example will read various values using the ADC and print them out to the serial terminal\r\n").ok();
65+
66+
// Move all components under Mutex supervision
6667
*SHARED.borrow(cs).borrow_mut() = Some(Shared { adc, tx });
6768
});
6869
}

examples/blinky.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@ use cortex_m_rt::entry;
1414
#[entry]
1515
fn main() -> ! {
1616
if let Some(p) = stm32::Peripherals::take() {
17-
let gpioa = p.GPIOA.split();
18-
19-
/* (Re-)configure PA1 as output */
20-
let mut led = gpioa.pa1.into_push_pull_output();
21-
22-
loop {
23-
/* Turn PA1 on a million times in a row */
24-
for _ in 0..1_000_000 {
25-
led.set_high();
26-
}
27-
/* Then turn PA1 off a million times in a row */
28-
for _ in 0..1_000_000 {
29-
led.set_low();
17+
cortex_m::interrupt::free(move |cs| {
18+
let mut flash = p.FLASH;
19+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
20+
21+
let gpioa = p.GPIOA.split(&mut rcc);
22+
23+
/* (Re-)configure PA1 as output */
24+
let mut led = gpioa.pa1.into_push_pull_output(cs);
25+
26+
loop {
27+
/* Turn PA1 on a million times in a row */
28+
for _ in 0..1_000_000 {
29+
led.set_high();
30+
}
31+
/* Then turn PA1 off a million times in a row */
32+
for _ in 0..1_000_000 {
33+
led.set_low();
34+
}
3035
}
31-
}
36+
});
3237
}
3338

3439
loop {

examples/blinky_adc.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,35 @@ use cortex_m_rt::entry;
1818
#[entry]
1919
fn main() -> ! {
2020
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
21-
let gpioa = p.GPIOA.split();
21+
cortex_m::interrupt::free(move |cs| {
22+
let mut flash = p.FLASH;
23+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
2224

23-
/* (Re-)configure PA1 as output */
24-
let mut led = gpioa.pa1.into_push_pull_output();
25+
let gpioa = p.GPIOA.split(&mut rcc);
2526

26-
/* (Re-)configure PA0 as analog in */
27-
let mut an_in = gpioa.pa0.into_analog();
27+
/* (Re-)configure PA1 as output */
28+
let mut led = gpioa.pa1.into_push_pull_output(cs);
2829

29-
/* Constrain clocking registers */
30-
let rcc = p.RCC.constrain();
30+
/* (Re-)configure PA0 as analog in */
31+
let mut an_in = gpioa.pa0.into_analog(cs);
3132

32-
/* Configure clock to 8 MHz (i.e. the default) and freeze it */
33-
let clocks = rcc.cfgr.sysclk(8.mhz()).freeze();
33+
/* Get delay provider */
34+
let mut delay = Delay::new(cp.SYST, &rcc);
3435

35-
/* Get delay provider */
36-
let mut delay = Delay::new(cp.SYST, clocks);
36+
let mut adc = Adc::new(p.ADC, &mut rcc);
3737

38-
let mut adc = Adc::new(p.ADC);
38+
loop {
39+
led.toggle();
3940

40-
loop {
41-
led.toggle();
41+
let val: u16 = adc.read(&mut an_in).unwrap();
4242

43-
let val: u16 = adc.read(&mut an_in).unwrap();
43+
/* shift the value right by 3, same as divide by 8, reduces
44+
the 0-4095 range into something approximating 1-512 */
45+
let time: u16 = (val >> 3) + 1;
4446

45-
/* shift the value right by 3, same as divide by 8, reduces
46-
the 0-4095 range into something approximating 1-512 */
47-
let time: u16 = (val >> 3) + 1;
48-
49-
delay.delay_ms(time);
50-
}
47+
delay.delay_ms(time);
48+
}
49+
});
5150
}
5251

5352
loop {

examples/blinky_delay.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,23 @@ use cortex_m_rt::entry;
1616
#[entry]
1717
fn main() -> ! {
1818
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
19-
let gpioa = p.GPIOA.split();
19+
cortex_m::interrupt::free(move |cs| {
20+
let mut flash = p.FLASH;
21+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
2022

21-
/* (Re-)configure PA1 as output */
22-
let mut led = gpioa.pa1.into_push_pull_output();
23+
let gpioa = p.GPIOA.split(&mut rcc);
2324

24-
/* Constrain clocking registers */
25-
let rcc = p.RCC.constrain();
25+
/* (Re-)configure PA1 as output */
26+
let mut led = gpioa.pa1.into_push_pull_output(cs);
2627

27-
/* Configure clock to 8 MHz (i.e. the default) and freeze it */
28-
let clocks = rcc.cfgr.sysclk(8.mhz()).freeze();
28+
/* Get delay provider */
29+
let mut delay = Delay::new(cp.SYST, &rcc);
2930

30-
/* Get delay provider */
31-
let mut delay = Delay::new(cp.SYST, clocks);
32-
33-
loop {
34-
led.toggle();
35-
delay.delay_ms(1_000_u16);
36-
}
31+
loop {
32+
led.toggle();
33+
delay.delay_ms(1_000_u16);
34+
}
35+
});
3736
}
3837

3938
loop {

examples/blinky_multiple.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,36 @@ use cortex_m_rt::entry;
1616
#[entry]
1717
fn main() -> ! {
1818
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
19-
let gpioa = p.GPIOA.split();
20-
let gpiob = p.GPIOB.split();
21-
22-
/* (Re-)configure PA1 as output */
23-
let led1 = gpioa.pa1.into_push_pull_output();
24-
25-
/* (Re-)configure PB1 as output */
26-
let led2 = gpiob.pb1.into_push_pull_output();
27-
28-
/* Constrain clocking registers */
29-
let rcc = p.RCC.constrain();
30-
31-
/* Configure clock to 8 MHz (i.e. the default) and freeze it */
32-
let clocks = rcc.cfgr.sysclk(8.mhz()).freeze();
33-
34-
/* Get delay provider */
35-
let mut delay = Delay::new(cp.SYST, clocks);
36-
37-
/* Store them together */
38-
let mut leds = [led1.downgrade(), led2.downgrade()];
39-
loop {
40-
for l in &mut leds {
41-
l.set_high();
42-
}
43-
delay.delay_ms(1_000_u16);
44-
45-
for l in &mut leds {
46-
l.set_low();
19+
cortex_m::interrupt::free(move |cs| {
20+
let mut flash = p.FLASH;
21+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
22+
23+
let gpioa = p.GPIOA.split(&mut rcc);
24+
let gpiob = p.GPIOB.split(&mut rcc);
25+
26+
/* (Re-)configure PA1 as output */
27+
let led1 = gpioa.pa1.into_push_pull_output(cs);
28+
29+
/* (Re-)configure PB1 as output */
30+
let led2 = gpiob.pb1.into_push_pull_output(cs);
31+
32+
/* Get delay provider */
33+
let mut delay = Delay::new(cp.SYST, &rcc);
34+
35+
/* Store them together */
36+
let mut leds = [led1.downgrade(), led2.downgrade()];
37+
loop {
38+
for l in &mut leds {
39+
l.set_high();
40+
}
41+
delay.delay_ms(1_000_u16);
42+
43+
for l in &mut leds {
44+
l.set_low();
45+
}
46+
delay.delay_ms(1_000_u16);
4747
}
48-
delay.delay_ms(1_000_u16);
49-
}
48+
});
5049
}
5150

5251
loop {

examples/blinky_timer.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@ use nb::block;
1717
#[entry]
1818
fn main() -> ! {
1919
if let Some(p) = stm32::Peripherals::take() {
20-
let gpioa = p.GPIOA.split();
21-
/* (Re-)configure PA1 as output */
22-
let mut led = gpioa.pa1.into_push_pull_output();
20+
cortex_m::interrupt::free(move |cs| {
21+
let mut flash = p.FLASH;
22+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
2323

24-
/* Constrain clocking registers */
25-
let rcc = p.RCC.constrain();
24+
let gpioa = p.GPIOA.split(&mut rcc);
25+
/* (Re-)configure PA1 as output */
26+
let mut led = gpioa.pa1.into_push_pull_output(cs);
2627

27-
/* Configure clock to 8 MHz (i.e. the default) and freeze it */
28-
let clocks = rcc.cfgr.sysclk(8.mhz()).freeze();
28+
let mut timer = Timer::tim1(p.TIM1, Hertz(1), &mut rcc);
2929

30-
let mut timer = Timer::tim1(p.TIM1, Hertz(1), clocks);
31-
32-
loop {
33-
led.toggle();
34-
block!(timer.wait()).ok();
35-
}
30+
loop {
31+
led.toggle();
32+
block!(timer.wait()).ok();
33+
}
34+
});
3635
}
3736

3837
loop {

examples/flash_systick.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,34 @@ static GPIO: Mutex<RefCell<Option<gpioa::PA1<Output<PushPull>>>>> = Mutex::new(R
2323
#[entry]
2424
fn main() -> ! {
2525
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
26-
let gpioa = p.GPIOA.split();
27-
let rcc = p.RCC.constrain();
28-
let _ = rcc.cfgr.sysclk(48.mhz()).freeze();
29-
let mut syst = cp.SYST;
26+
cortex_m::interrupt::free(move |cs| {
27+
let mut flash = p.FLASH;
28+
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut flash);
3029

31-
/* (Re-)configure PA1 as output */
32-
let led = gpioa.pa1.into_push_pull_output();
30+
let gpioa = p.GPIOA.split(&mut rcc);
31+
32+
let mut syst = cp.SYST;
33+
34+
/* (Re-)configure PA1 as output */
35+
let led = gpioa.pa1.into_push_pull_output(cs);
3336

34-
cortex_m::interrupt::free(move |cs| {
3537
*GPIO.borrow(cs).borrow_mut() = Some(led);
36-
});
3738

38-
/* Initialise SysTick counter with a defined value */
39-
unsafe { syst.cvr.write(1) };
39+
/* Initialise SysTick counter with a defined value */
40+
unsafe { syst.cvr.write(1) };
4041

41-
/* Set source for SysTick counter, here full operating frequency (== 8MHz) */
42-
syst.set_clock_source(Core);
42+
/* Set source for SysTick counter, here full operating frequency (== 8MHz) */
43+
syst.set_clock_source(Core);
4344

44-
/* Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms */
45-
syst.set_reload(4_000_000 - 1);
45+
/* Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms */
46+
syst.set_reload(4_000_000 - 1);
4647

47-
/* Start counter */
48-
syst.enable_counter();
48+
/* Start counter */
49+
syst.enable_counter();
4950

50-
/* Start interrupt generation */
51-
syst.enable_interrupt();
51+
/* Start interrupt generation */
52+
syst.enable_interrupt();
53+
});
5254
}
5355

5456
loop {

0 commit comments

Comments
 (0)