diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e4c3a9..ca08645 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - Updated the `cast` dependency from 0.2 to 0.3 +- Use `bare_metal::CriticalSection` for GPIO configuration (breaking change) ### Added diff --git a/examples/adc_values.rs b/examples/adc_values.rs index bd7cd8d..ac45773 100644 --- a/examples/adc_values.rs +++ b/examples/adc_values.rs @@ -7,7 +7,8 @@ use stm32f0xx_hal as hal; use crate::hal::{pac, prelude::*}; -use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core}; +use bare_metal::Mutex; +use cortex_m::peripheral::syst::SystClkSource::Core; use cortex_m_rt::{entry, exception}; use core::{cell::RefCell, fmt::Write}; @@ -25,7 +26,12 @@ fn main() -> ! { hal::pac::Peripherals::take(), cortex_m::peripheral::Peripherals::take(), ) { - cortex_m::interrupt::free(move |cs| { + cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + let mut flash = p.FLASH; let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash); @@ -60,7 +66,7 @@ fn main() -> ! { 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(); // Move all components under Mutex supervision - *SHARED.borrow(cs).borrow_mut() = Some(Shared { adc, tx }); + *SHARED.borrow(*cs).borrow_mut() = Some(Shared { adc, tx }); }); } @@ -74,7 +80,12 @@ fn SysTick() { use core::ops::DerefMut; // Enter critical section - cortex_m::interrupt::free(|cs| { + cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { bare_metal::CriticalSection::new() }; + // 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 diff --git a/examples/blinky.rs b/examples/blinky.rs index df1c90c..76f5d15 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -17,7 +17,13 @@ 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 = cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + gpioa.pa1.into_push_pull_output(cs) + }); loop { // Turn PA1 on a million times in a row diff --git a/examples/blinky_adc.rs b/examples/blinky_adc.rs index c74c2c3..03c53df 100644 --- a/examples/blinky_adc.rs +++ b/examples/blinky_adc.rs @@ -17,7 +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) = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + ( // (Re-)configure PA1 as output gpioa.pa1.into_push_pull_output(cs), diff --git a/examples/blinky_delay.rs b/examples/blinky_delay.rs index 22fdd52..7894552 100644 --- a/examples/blinky_delay.rs +++ b/examples/blinky_delay.rs @@ -18,7 +18,13 @@ 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 = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + gpioa.pa1.into_push_pull_output(cs) + }); // Get delay provider let mut delay = Delay::new(cp.SYST, &rcc); diff --git a/examples/blinky_multiple.rs b/examples/blinky_multiple.rs index a4939ea..489bbc5 100644 --- a/examples/blinky_multiple.rs +++ b/examples/blinky_multiple.rs @@ -18,7 +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) = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + ( // (Re-)configure PA1 as output gpioa.pa1.into_push_pull_output(cs), diff --git a/examples/blinky_timer.rs b/examples/blinky_timer.rs index 994dcc0..87facfa 100644 --- a/examples/blinky_timer.rs +++ b/examples/blinky_timer.rs @@ -17,7 +17,13 @@ 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 = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + 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); diff --git a/examples/blinky_timer_irq.rs b/examples/blinky_timer_irq.rs index 9504638..a181ca2 100644 --- a/examples/blinky_timer_irq.rs +++ b/examples/blinky_timer_irq.rs @@ -15,8 +15,9 @@ use crate::hal::{ use cortex_m_rt::entry; +use bare_metal::Mutex; use core::cell::RefCell; -use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals}; +use cortex_m::peripheral::Peripherals as c_m_Peripherals; // A type definition for the GPIO pin to be used for our LED type LEDPIN = gpioa::PA5>; @@ -35,14 +36,24 @@ fn TIM7() { static mut INT: Option> = None; let led = LED.get_or_insert_with(|| { - cortex_m::interrupt::free(|cs| { + cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { bare_metal::CriticalSection::new() }; + // 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| { + cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { bare_metal::CriticalSection::new() }; + // Move LED pin here, leaving a None in its place GINT.borrow(cs).replace(None).unwrap() }) @@ -55,7 +66,12 @@ fn TIM7() { #[entry] fn main() -> ! { if let (Some(mut p), Some(cp)) = (Peripherals::take(), c_m_Peripherals::take()) { - cortex_m::interrupt::free(move |cs| { + cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + let mut rcc = p .RCC .configure() @@ -71,7 +87,7 @@ fn main() -> ! { let led = gpioa.pa5.into_push_pull_output(cs); // Move the pin into our global storage - *GLED.borrow(cs).borrow_mut() = Some(led); + *GLED.borrow(*cs).borrow_mut() = Some(led); // Set up a timer expiring after 1s let mut timer = Timer::tim7(p.TIM7, Hertz(1), &mut rcc); @@ -80,7 +96,7 @@ fn main() -> ! { timer.listen(Event::TimeOut); // Move the timer into our global storage - *GINT.borrow(cs).borrow_mut() = Some(timer); + *GINT.borrow(*cs).borrow_mut() = Some(timer); // Enable TIM7 IRQ, set prio 1 and clear any pending IRQs let mut nvic = cp.NVIC; diff --git a/examples/dac.rs b/examples/dac.rs index 6ba0d23..e5cd542 100644 --- a/examples/dac.rs +++ b/examples/dac.rs @@ -24,7 +24,13 @@ 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 = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + gpioa.pa4.into_analog(cs) + }); let mut dac = dac(dp.DAC, pa4, &mut rcc); diff --git a/examples/flash_systick.rs b/examples/flash_systick.rs index 05589de..6c1a5c0 100644 --- a/examples/flash_systick.rs +++ b/examples/flash_systick.rs @@ -7,7 +7,8 @@ use stm32f0xx_hal as hal; use crate::hal::{gpio::*, pac, prelude::*}; -use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core, Peripherals}; +use bare_metal::Mutex; +use cortex_m::{peripheral::syst::SystClkSource::Core, Peripherals}; use cortex_m_rt::{entry, exception}; use core::cell::RefCell; @@ -19,7 +20,12 @@ static GPIO: Mutex>>>> = Mutex::new(R #[entry] fn main() -> ! { if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) { - cortex_m::interrupt::free(move |cs| { + cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH); let gpioa = p.GPIOA.split(&mut rcc); @@ -28,7 +34,7 @@ fn main() -> ! { let led = gpioa.pa1.into_push_pull_output(cs); // Transfer GPIO into a shared structure - *GPIO.borrow(cs).borrow_mut() = Some(led); + *GPIO.borrow(*cs).borrow_mut() = Some(led); let mut syst = cp.SYST; @@ -62,7 +68,12 @@ fn SysTick() { static mut STATE: u8 = 1; // Enter critical section - cortex_m::interrupt::free(|cs| { + cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { bare_metal::CriticalSection::new() }; + // 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 diff --git a/examples/flash_systick_fancier.rs b/examples/flash_systick_fancier.rs index de4ca2e..8afb941 100644 --- a/examples/flash_systick_fancier.rs +++ b/examples/flash_systick_fancier.rs @@ -7,7 +7,8 @@ use stm32f0xx_hal as hal; use crate::hal::{gpio::*, pac, prelude::*}; -use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core, Peripherals}; +use bare_metal::Mutex; +use cortex_m::{peripheral::syst::SystClkSource::Core, Peripherals}; use cortex_m_rt::{entry, exception}; use core::cell::RefCell; @@ -22,7 +23,12 @@ static GPIO: Mutex>> = 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| { + cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH); // Get access to individual pins in the GPIO port @@ -32,7 +38,7 @@ fn main() -> ! { 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()); + swap(&mut Some(led), &mut GPIO.borrow(*cs).borrow_mut()); let mut syst = cp.SYST; @@ -88,7 +94,12 @@ 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| { + cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { bare_metal::CriticalSection::new() }; + // Swap globally stored data with SysTick private data swap(LED, &mut GPIO.borrow(cs).borrow_mut()); }); diff --git a/examples/i2c_find_address.rs b/examples/i2c_find_address.rs index 4ca8454..c94ded6 100644 --- a/examples/i2c_find_address.rs +++ b/examples/i2c_find_address.rs @@ -14,7 +14,12 @@ use cortex_m_rt::entry; #[entry] fn main() -> ! { if let Some(p) = pac::Peripherals::take() { - cortex_m::interrupt::free(move |cs| { + cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + let mut flash = p.FLASH; let mut rcc = p.RCC.configure().freeze(&mut flash); diff --git a/examples/led_hal_button_irq.rs b/examples/led_hal_button_irq.rs index a635809..ee30b43 100644 --- a/examples/led_hal_button_irq.rs +++ b/examples/led_hal_button_irq.rs @@ -12,7 +12,8 @@ use crate::hal::{ prelude::*, }; -use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals}; +use bare_metal::Mutex; +use cortex_m::peripheral::Peripherals as c_m_Peripherals; use cortex_m_rt::entry; use core::{cell::RefCell, ops::DerefMut}; @@ -29,7 +30,12 @@ static INT: Mutex>> = 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| { + cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + // Enable clock for SYSCFG let rcc = p.RCC; rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit()); @@ -64,9 +70,9 @@ fn main() -> ! { exti.rtsr.modify(|_, w| w.tr1().set_bit()); // Move control over LED and DELAY and EXTI into global mutexes - *LED.borrow(cs).borrow_mut() = Some(led); - *DELAY.borrow(cs).borrow_mut() = Some(delay); - *INT.borrow(cs).borrow_mut() = Some(exti); + *LED.borrow(*cs).borrow_mut() = Some(led); + *DELAY.borrow(*cs).borrow_mut() = Some(delay); + *INT.borrow(*cs).borrow_mut() = Some(exti); // Enable EXTI IRQ, set prio 1 and clear any pending IRQs let mut nvic = cp.NVIC; @@ -88,7 +94,12 @@ fn main() -> ! { #[interrupt] fn EXTI0_1() { // Enter critical section - cortex_m::interrupt::free(|cs| { + cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { bare_metal::CriticalSection::new() }; + // 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(), diff --git a/examples/pwm.rs b/examples/pwm.rs index 8990f2f..1eebc11 100644 --- a/examples/pwm.rs +++ b/examples/pwm.rs @@ -1,4 +1,3 @@ -#![deny(unsafe_code)] #![no_main] #![no_std] @@ -18,7 +17,12 @@ 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 = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + ( gpioa.pa8.into_alternate_af2(cs), gpioa.pa9.into_alternate_af2(cs), diff --git a/examples/pwm_complementary.rs b/examples/pwm_complementary.rs index f190c20..45df081 100644 --- a/examples/pwm_complementary.rs +++ b/examples/pwm_complementary.rs @@ -1,4 +1,3 @@ -#![deny(unsafe_code)] #![no_main] #![no_std] @@ -18,7 +17,12 @@ 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 = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + ( gpioa.pa8.into_alternate_af2(cs), // on TIM1_CH1 gpioa.pa7.into_alternate_af2(cs), // on TIM1_CH1N diff --git a/examples/serial_echo.rs b/examples/serial_echo.rs index d0c70d3..36a8b51 100644 --- a/examples/serial_echo.rs +++ b/examples/serial_echo.rs @@ -17,7 +17,12 @@ fn main() -> ! { let gpioa = p.GPIOA.split(&mut rcc); - let (tx, rx) = cortex_m::interrupt::free(move |cs| { + let (tx, rx) = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + ( gpioa.pa9.into_alternate_af1(cs), gpioa.pa10.into_alternate_af1(cs), diff --git a/examples/serial_spi_bridge.rs b/examples/serial_spi_bridge.rs index b3bacd5..88df41d 100644 --- a/examples/serial_spi_bridge.rs +++ b/examples/serial_spi_bridge.rs @@ -36,7 +36,11 @@ 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) = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; ( // SPI pins gpioa.pa5.into_alternate_af0(cs), diff --git a/examples/serial_stopwatch.rs b/examples/serial_stopwatch.rs index 37c5127..b5b2284 100644 --- a/examples/serial_stopwatch.rs +++ b/examples/serial_stopwatch.rs @@ -15,7 +15,8 @@ use core::cell::RefCell; use core::fmt::Write as _; use core::ops::DerefMut; -use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals}; +use bare_metal::Mutex; +use cortex_m::peripheral::Peripherals as c_m_Peripherals; use cortex_m_rt::entry; // Make timer interrupt registers globally available @@ -36,7 +37,12 @@ static TIME: Mutex> = Mutex::new(RefCell::new(Time { // interrupt trips when the timer timed out #[interrupt] fn TIM7() { - cortex_m::interrupt::free(|cs| { + cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { bare_metal::CriticalSection::new() }; + // Move LED pin here, leaving a None in its place GINT.borrow(cs) .borrow_mut() @@ -57,7 +63,12 @@ fn TIM7() { #[entry] fn main() -> ! { if let (Some(p), Some(cp)) = (Peripherals::take(), c_m_Peripherals::take()) { - let mut serial = cortex_m::interrupt::free(move |cs| { + let mut serial = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + let mut flash = p.FLASH; let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut flash); @@ -73,7 +84,7 @@ fn main() -> ! { timer.listen(Event::TimeOut); // Move the timer into our global storage - *GINT.borrow(cs).borrow_mut() = Some(timer); + *GINT.borrow(*cs).borrow_mut() = Some(timer); // Enable TIM7 IRQ, set prio 1 and clear any pending IRQs let mut nvic = cp.NVIC; @@ -98,7 +109,12 @@ fn main() -> ! { // Wait for reception of a single byte let received = nb::block!(serial.read()).unwrap(); - let time = cortex_m::interrupt::free(|cs| { + let time = cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { bare_metal::CriticalSection::new() }; + let mut time = TIME.borrow(cs).borrow_mut(); // If we received a 0, reset the time diff --git a/examples/spi_hal_apa102c.rs b/examples/spi_hal_apa102c.rs index f3c2007..94ee7d0 100644 --- a/examples/spi_hal_apa102c.rs +++ b/examples/spi_hal_apa102c.rs @@ -28,7 +28,11 @@ fn main() -> ! { let gpioa = p.GPIOA.split(&mut rcc); // Configure pins for SPI - let (sck, miso, mosi) = cortex_m::interrupt::free(move |cs| { + let (sck, miso, mosi) = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; ( gpioa.pa5.into_alternate_af0(cs), gpioa.pa6.into_alternate_af0(cs), diff --git a/examples/usb_serial.rs b/examples/usb_serial.rs index 49afe55..b6eafc9 100644 --- a/examples/usb_serial.rs +++ b/examples/usb_serial.rs @@ -35,7 +35,13 @@ fn main() -> ! { // Configure the on-board LED (LD3, green) let gpiob = dp.GPIOB.split(&mut rcc); - let mut led = cortex_m::interrupt::free(|cs| gpiob.pb3.into_push_pull_output(cs)); + let mut led = cortex_m::interrupt::free(|_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + gpiob.pb3.into_push_pull_output(cs) + }); led.set_low().ok(); // Turn off let gpioa = dp.GPIOA.split(&mut rcc); diff --git a/examples/watchdog.rs b/examples/watchdog.rs index bcd265e..3449acb 100644 --- a/examples/watchdog.rs +++ b/examples/watchdog.rs @@ -30,7 +30,13 @@ fn main() -> ! { let mut delay = Delay::new(cp.SYST, &rcc); // Configure serial TX pin - let tx = cortex_m::interrupt::free(move |cs| gpioa.pa9.into_alternate_af1(cs)); + let tx = cortex_m::interrupt::free(move |_| { + // SAFETY: We are in a critical section, but the `cortex_m` critical section + // token is not compatible with the `bare_metal` token. Future version of the + // `cortex_m` crate will not supply *any* token to this callback! + let cs = unsafe { &bare_metal::CriticalSection::new() }; + gpioa.pa9.into_alternate_af1(cs) + }); // Obtain a serial peripheral with for unidirectional communication let mut serial = Serial::usart1tx(p.USART1, tx, 115_200.bps(), &mut rcc); diff --git a/src/adc.rs b/src/adc.rs index 4ff50cc..b9a366d 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -12,7 +12,8 @@ //! use crate::hal::prelude::*; //! use crate::hal::adc::Adc; //! -//! cortex_m::interrupt::free(|cs| { +//! cortex_m::interrupt::free(|_| { +//! let cs = unsafe { &bare_metal::CriticalSection::new() }; //! let mut p = pac::Peripherals::take().unwrap(); //! let mut rcc = p.RCC.configure().freeze(&mut p.FLASH); //! diff --git a/src/dac.rs b/src/dac.rs index 4587276..b89f2bc 100644 --- a/src/dac.rs +++ b/src/dac.rs @@ -29,7 +29,8 @@ //!#[entry] //!fn main() -> ! { //! if let (Some(mut dp), Some(_cp)) = (pac::Peripherals::take(), cortex_m::Peripherals::take()) { -//! cortex_m::interrupt::free(move |cs| { +//! cortex_m::interrupt::free(move |_| { +//! let cs = unsafe { &bare_metal::CriticalSection::new() }; //! let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH); //! //! let gpioa = dp.GPIOA.split(&mut rcc); diff --git a/src/gpio.rs b/src/gpio.rs index dd2757e..277e0ac 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -190,7 +190,7 @@ macro_rules! gpio { pac::$GPIOX }; - use cortex_m::interrupt::CriticalSection; + use bare_metal::CriticalSection; use super::{ Alternate, Analog, Floating, GpioExt, Input, OpenDrain, Output, diff --git a/src/serial.rs b/src/serial.rs index ea8bed9..4b93a55 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -16,7 +16,8 @@ //! //! use nb::block; //! -//! cortex_m::interrupt::free(|cs| { +//! cortex_m::interrupt::free(|_| { +//! let cs = unsafe { &bare_metal::CriticalSection::new() }; //! let rcc = p.RCC.configure().sysclk(48.mhz()).freeze(); //! //! let gpioa = p.GPIOA.split(&mut rcc); @@ -43,7 +44,8 @@ //! //! use nb::block; //! -//! cortex_m::interrupt::free(|cs| { +//! cortex_m::interrupt::free(|_| { +//! let cs = unsafe { &bare_metal::CriticalSection::new() }; //! let rcc = p.RCC.configure().sysclk(48.mhz()).freeze(); //! //! let gpioa = p.GPIOA.split(&mut rcc); diff --git a/src/spi.rs b/src/spi.rs index 17eeef7..0f0fb8f 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -13,7 +13,8 @@ //! use crate::hal::prelude::*; //! use crate::hal::spi::{Spi, Mode, Phase, Polarity}; //! -//! cortex_m::interrupt::free(|cs| { +//! cortex_m::interrupt::free(|_| { +//! let cs = unsafe { &bare_metal::CriticalSection::new() }; //! let mut p = pac::Peripherals::take().unwrap(); //! let mut rcc = p.RCC.constrain().freeze(&mut p.FLASH); //! diff --git a/src/timers.rs b/src/timers.rs index 136bc93..0b22566 100644 --- a/src/timers.rs +++ b/src/timers.rs @@ -13,7 +13,8 @@ //! use crate::hal::timers::*; //! use nb::block; //! -//! cortex_m::interrupt::free(|cs| { +//! cortex_m::interrupt::free(|_| { +//! let cs = unsafe { &bare_metal::CriticalSection::new() }; //! let mut p = pac::Peripherals::take().unwrap(); //! let mut rcc = p.RCC.configure().freeze(&mut p.FLASH); //!