Skip to content

Use critical_section::CriticalSection for GPIO configuration #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Updated the `cast` dependency from 0.2 to 0.3
- Updated `stm32f0` peripheral access crate from 0.14 to 0.15
- Use `critical_section::CriticalSection` for GPIO configuration (breaking change)

### Added

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ targets = ["thumbv6m-none-eabi"]
bare-metal = { version = "1.0.0" }
cast = "0.3"
cortex-m = "0.7"
critical-section = "1.0"
embedded-hal = { version = "0.2", features = ["unproven"] }
stm32f0 = "0.15"
nb = "1"
Expand Down
11 changes: 6 additions & 5 deletions examples/adc_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use stm32f0xx_hal as hal;

use crate::hal::{pac, prelude::*};

use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core};
use cortex_m::peripheral::syst::SystClkSource::Core;
use cortex_m_rt::{entry, exception};
use critical_section::Mutex;

use core::{cell::RefCell, fmt::Write};

Expand All @@ -25,7 +26,7 @@ fn main() -> ! {
hal::pac::Peripherals::take(),
cortex_m::peripheral::Peripherals::take(),
) {
cortex_m::interrupt::free(move |cs| {
critical_section::with(move |cs| {
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);

Expand All @@ -46,8 +47,8 @@ fn main() -> ! {
syst.enable_interrupt();

// USART1 at PA9 (TX) and PA10(RX)
let tx = gpioa.pa9.into_alternate_af1(cs);
let rx = gpioa.pa10.into_alternate_af1(cs);
let tx = gpioa.pa9.into_alternate_af1(&cs);
let rx = gpioa.pa10.into_alternate_af1(&cs);

// Initialiase UART
let (mut tx, _) =
Expand All @@ -74,7 +75,7 @@ fn SysTick() {
use core::ops::DerefMut;

// Enter critical section
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
// Get access to the Mutex protected shared data
if let Some(ref mut shared) = SHARED.borrow(cs).borrow_mut().deref_mut() {
// Read temperature data from internal sensor using ADC
Expand Down
2 changes: 1 addition & 1 deletion examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> ! {
let gpioa = p.GPIOA.split(&mut rcc);

// (Re-)configure PA1 as output
let mut led = cortex_m::interrupt::free(|cs| gpioa.pa1.into_push_pull_output(cs));
let mut led = critical_section::with(|cs| gpioa.pa1.into_push_pull_output(&cs));

loop {
// Turn PA1 on a million times in a row
Expand Down
6 changes: 3 additions & 3 deletions examples/blinky_adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ fn main() -> ! {

let gpioa = p.GPIOA.split(&mut rcc);

let (mut led, mut an_in) = cortex_m::interrupt::free(move |cs| {
let (mut led, mut an_in) = critical_section::with(move |cs| {
(
// (Re-)configure PA1 as output
gpioa.pa1.into_push_pull_output(cs),
gpioa.pa1.into_push_pull_output(&cs),
// (Re-)configure PA0 as analog input
gpioa.pa0.into_analog(cs),
gpioa.pa0.into_analog(&cs),
)
});

Expand Down
2 changes: 1 addition & 1 deletion examples/blinky_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() -> ! {
let gpioa = p.GPIOA.split(&mut rcc);

// (Re-)configure PA1 as output
let mut led = cortex_m::interrupt::free(move |cs| gpioa.pa1.into_push_pull_output(cs));
let mut led = critical_section::with(move |cs| gpioa.pa1.into_push_pull_output(&cs));

// Get delay provider
let mut delay = Delay::new(cp.SYST, &rcc);
Expand Down
6 changes: 3 additions & 3 deletions examples/blinky_multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ fn main() -> ! {
let gpioa = p.GPIOA.split(&mut rcc);
let gpiob = p.GPIOB.split(&mut rcc);

let (led1, led2) = cortex_m::interrupt::free(move |cs| {
let (led1, led2) = critical_section::with(move |cs| {
(
// (Re-)configure PA1 as output
gpioa.pa1.into_push_pull_output(cs),
gpioa.pa1.into_push_pull_output(&cs),
// (Re-)configure PB1 as output
gpiob.pb1.into_push_pull_output(cs),
gpiob.pb1.into_push_pull_output(&cs),
)
});

Expand Down
2 changes: 1 addition & 1 deletion examples/blinky_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> ! {
let gpioa = p.GPIOA.split(&mut rcc);

// (Re-)configure PA1 as output
let mut led = cortex_m::interrupt::free(move |cs| gpioa.pa1.into_push_pull_output(cs));
let mut led = critical_section::with(move |cs| gpioa.pa1.into_push_pull_output(&cs));

// Set up a timer expiring after 1s
let mut timer = Timer::tim1(p.TIM1, Hertz(1), &mut rcc);
Expand Down
11 changes: 6 additions & 5 deletions examples/blinky_timer_irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use crate::hal::{
use cortex_m_rt::entry;

use core::cell::RefCell;
use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals};
use cortex_m::peripheral::Peripherals as c_m_Peripherals;
use critical_section::Mutex;

// A type definition for the GPIO pin to be used for our LED
type LEDPIN = gpioa::PA5<Output<PushPull>>;
Expand All @@ -35,14 +36,14 @@ fn TIM7() {
static mut INT: Option<Timer<TIM7>> = None;

let led = LED.get_or_insert_with(|| {
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
// Move LED pin here, leaving a None in its place
GLED.borrow(cs).replace(None).unwrap()
})
});

let int = INT.get_or_insert_with(|| {
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
// Move LED pin here, leaving a None in its place
GINT.borrow(cs).replace(None).unwrap()
})
Expand All @@ -55,7 +56,7 @@ fn TIM7() {
#[entry]
fn main() -> ! {
if let (Some(mut p), Some(cp)) = (Peripherals::take(), c_m_Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
critical_section::with(move |cs| {
let mut rcc = p
.RCC
.configure()
Expand All @@ -68,7 +69,7 @@ fn main() -> ! {
let gpioa = p.GPIOA.split(&mut rcc);

// (Re-)configure PA5 as output
let led = gpioa.pa5.into_push_pull_output(cs);
let led = gpioa.pa5.into_push_pull_output(&cs);

// Move the pin into our global storage
*GLED.borrow(cs).borrow_mut() = Some(led);
Expand Down
2 changes: 1 addition & 1 deletion examples/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> ! {
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
let gpioa = dp.GPIOA.split(&mut rcc);

let pa4 = cortex_m::interrupt::free(move |cs| gpioa.pa4.into_analog(cs));
let pa4 = critical_section::with(move |cs| gpioa.pa4.into_analog(&cs));

let mut dac = dac(dp.DAC, pa4, &mut rcc);

Expand Down
9 changes: 5 additions & 4 deletions examples/flash_systick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use stm32f0xx_hal as hal;

use crate::hal::{gpio::*, pac, prelude::*};

use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core, Peripherals};
use cortex_m::{peripheral::syst::SystClkSource::Core, Peripherals};
use cortex_m_rt::{entry, exception};
use critical_section::Mutex;

use core::cell::RefCell;
use core::ops::DerefMut;
Expand All @@ -19,13 +20,13 @@ static GPIO: Mutex<RefCell<Option<gpioa::PA1<Output<PushPull>>>>> = Mutex::new(R
#[entry]
fn main() -> ! {
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
critical_section::with(move |cs| {
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH);

let gpioa = p.GPIOA.split(&mut rcc);

// (Re-)configure PA1 as output
let led = gpioa.pa1.into_push_pull_output(cs);
let led = gpioa.pa1.into_push_pull_output(&cs);

// Transfer GPIO into a shared structure
*GPIO.borrow(cs).borrow_mut() = Some(led);
Expand Down Expand Up @@ -62,7 +63,7 @@ fn SysTick() {
static mut STATE: u8 = 1;

// Enter critical section
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
// Borrow access to our GPIO pin from the shared structure
if let Some(ref mut led) = *GPIO.borrow(cs).borrow_mut().deref_mut() {
// Check state variable, keep LED off most of the time and turn it on every 10th tick
Expand Down
9 changes: 5 additions & 4 deletions examples/flash_systick_fancier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use stm32f0xx_hal as hal;

use crate::hal::{gpio::*, pac, prelude::*};

use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core, Peripherals};
use cortex_m::{peripheral::syst::SystClkSource::Core, Peripherals};
use cortex_m_rt::{entry, exception};
use critical_section::Mutex;

use core::cell::RefCell;
use core::mem::swap;
Expand All @@ -22,14 +23,14 @@ static GPIO: Mutex<RefCell<Option<LEDPIN>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
critical_section::with(move |cs| {
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH);

// Get access to individual pins in the GPIO port
let gpioa = p.GPIOB.split(&mut rcc);

// (Re-)configure the pin connected to our LED as output
let led = gpioa.pb3.into_push_pull_output(cs);
let led = gpioa.pb3.into_push_pull_output(&cs);

// Transfer GPIO into a shared structure
swap(&mut Some(led), &mut GPIO.borrow(cs).borrow_mut());
Expand Down Expand Up @@ -88,7 +89,7 @@ fn SysTick() {
// Otherwise move it out of the Mutex protected shared region into our exception handler
else {
// Enter critical section
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
// Swap globally stored data with SysTick private data
swap(LED, &mut GPIO.borrow(cs).borrow_mut());
});
Expand Down
6 changes: 3 additions & 3 deletions examples/i2c_find_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
if let Some(p) = pac::Peripherals::take() {
cortex_m::interrupt::free(move |cs| {
critical_section::with(move |cs| {
let mut flash = p.FLASH;
let mut rcc = p.RCC.configure().freeze(&mut flash);

let gpiob = p.GPIOB.split(&mut rcc);

// Configure pins for I2C
let sda = gpiob.pb7.into_alternate_af1(cs);
let scl = gpiob.pb8.into_alternate_af1(cs);
let sda = gpiob.pb7.into_alternate_af1(&cs);
let scl = gpiob.pb8.into_alternate_af1(&cs);

// Configure I2C with 100kHz rate
let mut i2c = I2c::i2c1(p.I2C1, (scl, sda), 100.khz(), &mut rcc);
Expand Down
11 changes: 6 additions & 5 deletions examples/led_hal_button_irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::hal::{
prelude::*,
};

use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals};
use cortex_m::peripheral::Peripherals as c_m_Peripherals;
use cortex_m_rt::entry;
use critical_section::Mutex;

use core::{cell::RefCell, ops::DerefMut};

Expand All @@ -29,7 +30,7 @@ static INT: Mutex<RefCell<Option<EXTI>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (Peripherals::take(), c_m_Peripherals::take()) {
cortex_m::interrupt::free(move |cs| {
critical_section::with(move |cs| {
// Enable clock for SYSCFG
let rcc = p.RCC;
rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());
Expand All @@ -43,10 +44,10 @@ fn main() -> ! {
let exti = p.EXTI;

// Configure PB1 as input (button)
let _ = gpiob.pb1.into_pull_down_input(cs);
let _ = gpiob.pb1.into_pull_down_input(&cs);

// Configure PA1 as output (LED)
let mut led = gpioa.pa1.into_push_pull_output(cs);
let mut led = gpioa.pa1.into_push_pull_output(&cs);

// Turn off LED
led.set_low().ok();
Expand Down Expand Up @@ -88,7 +89,7 @@ fn main() -> ! {
#[interrupt]
fn EXTI0_1() {
// Enter critical section
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
// Obtain all Mutex protected resources
if let (&mut Some(ref mut led), &mut Some(ref mut delay), &mut Some(ref mut exti)) = (
LED.borrow(cs).borrow_mut().deref_mut(),
Expand Down
6 changes: 3 additions & 3 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ fn main() -> ! {
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);

let gpioa = dp.GPIOA.split(&mut rcc);
let channels = cortex_m::interrupt::free(move |cs| {
let channels = critical_section::with(move |cs| {
(
gpioa.pa8.into_alternate_af2(cs),
gpioa.pa9.into_alternate_af2(cs),
gpioa.pa8.into_alternate_af2(&cs),
gpioa.pa9.into_alternate_af2(&cs),
)
});

Expand Down
6 changes: 3 additions & 3 deletions examples/pwm_complementary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ fn main() -> ! {
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);

let gpioa = dp.GPIOA.split(&mut rcc);
let channels = cortex_m::interrupt::free(move |cs| {
let channels = critical_section::with(move |cs| {
(
gpioa.pa8.into_alternate_af2(cs), // on TIM1_CH1
gpioa.pa7.into_alternate_af2(cs), // on TIM1_CH1N
gpioa.pa8.into_alternate_af2(&cs), // on TIM1_CH1
gpioa.pa7.into_alternate_af2(&cs), // on TIM1_CH1N
)
});

Expand Down
6 changes: 3 additions & 3 deletions examples/serial_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ fn main() -> ! {

let gpioa = p.GPIOA.split(&mut rcc);

let (tx, rx) = cortex_m::interrupt::free(move |cs| {
let (tx, rx) = critical_section::with(move |cs| {
(
gpioa.pa9.into_alternate_af1(cs),
gpioa.pa10.into_alternate_af1(cs),
gpioa.pa9.into_alternate_af1(&cs),
gpioa.pa10.into_alternate_af1(&cs),
)
});

Expand Down
12 changes: 6 additions & 6 deletions examples/serial_spi_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ fn main() -> ! {

let gpioa = p.GPIOA.split(&mut rcc);

let (sck, miso, mosi, tx, rx) = cortex_m::interrupt::free(move |cs| {
let (sck, miso, mosi, tx, rx) = critical_section::with(move |cs| {
(
// SPI pins
gpioa.pa5.into_alternate_af0(cs),
gpioa.pa6.into_alternate_af0(cs),
gpioa.pa7.into_alternate_af0(cs),
gpioa.pa5.into_alternate_af0(&cs),
gpioa.pa6.into_alternate_af0(&cs),
gpioa.pa7.into_alternate_af0(&cs),
// USART pins
gpioa.pa9.into_alternate_af1(cs),
gpioa.pa10.into_alternate_af1(cs),
gpioa.pa9.into_alternate_af1(&cs),
gpioa.pa10.into_alternate_af1(&cs),
)
});

Expand Down
Loading
Loading