aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-10-12 00:14:02 +0000
committerGitHub <[email protected]>2023-10-12 00:14:02 +0000
commitac84631a2aa0243b8e7dccbe8af324f3f5cd19e0 (patch)
tree87535352950f8353972037c441902066dd19d710
parentc283e2d1b914b58459bd39f59c2bda0e55d5d0bd (diff)
parent70a91945fca97229518f4f8f011233650448771d (diff)
Merge pull request #2046 from embassy-rs/stm32-remove-polyfill
stm32: remove atomic-polyfill.
-rw-r--r--cyw43/Cargo.toml1
-rw-r--r--embassy-net/Cargo.toml1
-rw-r--r--embassy-net/src/tcp.rs15
-rw-r--r--embassy-stm32/Cargo.toml1
-rw-r--r--embassy-stm32/build.rs34
-rw-r--r--embassy-stm32/src/dma/bdma.rs18
-rw-r--r--embassy-stm32/src/lib.rs4
-rw-r--r--embassy-stm32/src/low_power.rs2
-rw-r--r--embassy-stm32/src/rcc/mod.rs16
-rw-r--r--embassy-stm32/src/time_driver.rs22
-rw-r--r--embassy-time/Cargo.toml1
-rw-r--r--examples/stm32h7/src/bin/eth_client.rs4
12 files changed, 78 insertions, 41 deletions
diff --git a/cyw43/Cargo.toml b/cyw43/Cargo.toml
index dae7419c1..c201405e7 100644
--- a/cyw43/Cargo.toml
+++ b/cyw43/Cargo.toml
@@ -15,7 +15,6 @@ embassy-time = { version = "0.1.3", path = "../embassy-time"}
15embassy-sync = { version = "0.3.0", path = "../embassy-sync"} 15embassy-sync = { version = "0.3.0", path = "../embassy-sync"}
16embassy-futures = { version = "0.1.0", path = "../embassy-futures"} 16embassy-futures = { version = "0.1.0", path = "../embassy-futures"}
17embassy-net-driver-channel = { version = "0.1.0", path = "../embassy-net-driver-channel"} 17embassy-net-driver-channel = { version = "0.1.0", path = "../embassy-net-driver-channel"}
18atomic-polyfill = "0.1.5"
19 18
20defmt = { version = "0.3", optional = true } 19defmt = { version = "0.3", optional = true }
21log = { version = "0.4.17", optional = true } 20log = { version = "0.4.17", optional = true }
diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml
index c2fffba84..a3c18046e 100644
--- a/embassy-net/Cargo.toml
+++ b/embassy-net/Cargo.toml
@@ -64,4 +64,3 @@ stable_deref_trait = { version = "1.2.0", default-features = false }
64futures = { version = "0.3.17", default-features = false, features = [ "async-await" ] } 64futures = { version = "0.3.17", default-features = false, features = [ "async-await" ] }
65atomic-pool = "1.0" 65atomic-pool = "1.0"
66embedded-nal-async = { version = "0.6.0", optional = true } 66embedded-nal-async = { version = "0.6.0", optional = true }
67atomic-polyfill = { version = "1.0" }
diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs
index a12fd382a..b5615cb66 100644
--- a/embassy-net/src/tcp.rs
+++ b/embassy-net/src/tcp.rs
@@ -579,11 +579,10 @@ mod embedded_io_impls {
579/// TCP client compatible with `embedded-nal-async` traits. 579/// TCP client compatible with `embedded-nal-async` traits.
580#[cfg(feature = "nightly")] 580#[cfg(feature = "nightly")]
581pub mod client { 581pub mod client {
582 use core::cell::UnsafeCell; 582 use core::cell::{Cell, UnsafeCell};
583 use core::mem::MaybeUninit; 583 use core::mem::MaybeUninit;
584 use core::ptr::NonNull; 584 use core::ptr::NonNull;
585 585
586 use atomic_polyfill::{AtomicBool, Ordering};
587 use embedded_nal_async::IpAddr; 586 use embedded_nal_async::IpAddr;
588 587
589 use super::*; 588 use super::*;
@@ -702,15 +701,13 @@ pub mod client {
702 } 701 }
703 } 702 }
704 703
705 unsafe impl<const N: usize, const TX_SZ: usize, const RX_SZ: usize> Sync for TcpClientState<N, TX_SZ, RX_SZ> {}
706
707 struct Pool<T, const N: usize> { 704 struct Pool<T, const N: usize> {
708 used: [AtomicBool; N], 705 used: [Cell<bool>; N],
709 data: [UnsafeCell<MaybeUninit<T>>; N], 706 data: [UnsafeCell<MaybeUninit<T>>; N],
710 } 707 }
711 708
712 impl<T, const N: usize> Pool<T, N> { 709 impl<T, const N: usize> Pool<T, N> {
713 const VALUE: AtomicBool = AtomicBool::new(false); 710 const VALUE: Cell<bool> = Cell::new(false);
714 const UNINIT: UnsafeCell<MaybeUninit<T>> = UnsafeCell::new(MaybeUninit::uninit()); 711 const UNINIT: UnsafeCell<MaybeUninit<T>> = UnsafeCell::new(MaybeUninit::uninit());
715 712
716 const fn new() -> Self { 713 const fn new() -> Self {
@@ -724,7 +721,9 @@ pub mod client {
724 impl<T, const N: usize> Pool<T, N> { 721 impl<T, const N: usize> Pool<T, N> {
725 fn alloc(&self) -> Option<NonNull<T>> { 722 fn alloc(&self) -> Option<NonNull<T>> {
726 for n in 0..N { 723 for n in 0..N {
727 if self.used[n].swap(true, Ordering::SeqCst) == false { 724 // this can't race because Pool is not Sync.
725 if !self.used[n].get() {
726 self.used[n].set(true);
728 let p = self.data[n].get() as *mut T; 727 let p = self.data[n].get() as *mut T;
729 return Some(unsafe { NonNull::new_unchecked(p) }); 728 return Some(unsafe { NonNull::new_unchecked(p) });
730 } 729 }
@@ -738,7 +737,7 @@ pub mod client {
738 let n = p.as_ptr().offset_from(origin); 737 let n = p.as_ptr().offset_from(origin);
739 assert!(n >= 0); 738 assert!(n >= 0);
740 assert!((n as usize) < N); 739 assert!((n as usize) < N);
741 self.used[n as usize].store(false, Ordering::SeqCst); 740 self.used[n as usize].set(false);
742 } 741 }
743 } 742 }
744} 743}
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index 33583d22b..a2c5da2bc 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -58,7 +58,6 @@ rand_core = "0.6.3"
58sdio-host = "0.5.0" 58sdio-host = "0.5.0"
59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } 59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true }
60critical-section = "1.1" 60critical-section = "1.1"
61atomic-polyfill = "1.0.1"
62stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-6bfa5a0dcec6a9bd42cea94ba11eeae1a17a7f2c" } 61stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-6bfa5a0dcec6a9bd42cea94ba11eeae1a17a7f2c" }
63vcell = "0.1.3" 62vcell = "0.1.3"
64bxcan = "0.7.0" 63bxcan = "0.7.0"
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs
index 810da37e9..fa10b7f77 100644
--- a/embassy-stm32/build.rs
+++ b/embassy-stm32/build.rs
@@ -8,6 +8,32 @@ use quote::{format_ident, quote};
8use stm32_metapac::metadata::{MemoryRegionKind, METADATA}; 8use stm32_metapac::metadata::{MemoryRegionKind, METADATA};
9 9
10fn main() { 10fn main() {
11 let target = env::var("TARGET").unwrap();
12
13 if target.starts_with("thumbv6m-") {
14 println!("cargo:rustc-cfg=cortex_m");
15 println!("cargo:rustc-cfg=armv6m");
16 } else if target.starts_with("thumbv7m-") {
17 println!("cargo:rustc-cfg=cortex_m");
18 println!("cargo:rustc-cfg=armv7m");
19 } else if target.starts_with("thumbv7em-") {
20 println!("cargo:rustc-cfg=cortex_m");
21 println!("cargo:rustc-cfg=armv7m");
22 println!("cargo:rustc-cfg=armv7em"); // (not currently used)
23 } else if target.starts_with("thumbv8m.base") {
24 println!("cargo:rustc-cfg=cortex_m");
25 println!("cargo:rustc-cfg=armv8m");
26 println!("cargo:rustc-cfg=armv8m_base");
27 } else if target.starts_with("thumbv8m.main") {
28 println!("cargo:rustc-cfg=cortex_m");
29 println!("cargo:rustc-cfg=armv8m");
30 println!("cargo:rustc-cfg=armv8m_main");
31 }
32
33 if target.ends_with("-eabihf") {
34 println!("cargo:rustc-cfg=has_fpu");
35 }
36
11 let chip_name = match env::vars() 37 let chip_name = match env::vars()
12 .map(|(a, _)| a) 38 .map(|(a, _)| a)
13 .filter(|x| x.starts_with("CARGO_FEATURE_STM32")) 39 .filter(|x| x.starts_with("CARGO_FEATURE_STM32"))
@@ -434,20 +460,20 @@ fn main() {
434 unsafe { crate::rcc::get_freqs().#clk } 460 unsafe { crate::rcc::get_freqs().#clk }
435 } 461 }
436 fn enable() { 462 fn enable() {
437 critical_section::with(|_| { 463 critical_section::with(|_cs| {
438 #before_enable 464 #before_enable
439 #[cfg(feature = "low-power")] 465 #[cfg(feature = "low-power")]
440 crate::rcc::clock_refcount_add(); 466 crate::rcc::clock_refcount_add(_cs);
441 crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(true)); 467 crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(true));
442 #after_enable 468 #after_enable
443 }) 469 })
444 } 470 }
445 fn disable() { 471 fn disable() {
446 critical_section::with(|_| { 472 critical_section::with(|_cs| {
447 #before_disable 473 #before_disable
448 crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(false)); 474 crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(false));
449 #[cfg(feature = "low-power")] 475 #[cfg(feature = "low-power")]
450 crate::rcc::clock_refcount_sub(); 476 crate::rcc::clock_refcount_sub(_cs);
451 }) 477 })
452 } 478 }
453 fn reset() { 479 fn reset() {
diff --git a/embassy-stm32/src/dma/bdma.rs b/embassy-stm32/src/dma/bdma.rs
index 20ff29bef..62eb65b1c 100644
--- a/embassy-stm32/src/dma/bdma.rs
+++ b/embassy-stm32/src/dma/bdma.rs
@@ -2,10 +2,9 @@
2 2
3use core::future::Future; 3use core::future::Future;
4use core::pin::Pin; 4use core::pin::Pin;
5use core::sync::atomic::{fence, Ordering}; 5use core::sync::atomic::{fence, AtomicUsize, Ordering};
6use core::task::{Context, Poll, Waker}; 6use core::task::{Context, Poll, Waker};
7 7
8use atomic_polyfill::AtomicUsize;
9use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef}; 8use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
10use embassy_sync::waitqueue::AtomicWaker; 9use embassy_sync::waitqueue::AtomicWaker;
11 10
@@ -127,7 +126,13 @@ pub(crate) unsafe fn on_irq_inner(dma: pac::bdma::Dma, channel_num: usize, index
127 } else if isr.tcif(channel_num) && cr.read().tcie() { 126 } else if isr.tcif(channel_num) && cr.read().tcie() {
128 // Acknowledge transfer complete interrupt 127 // Acknowledge transfer complete interrupt
129 dma.ifcr().write(|w| w.set_tcif(channel_num, true)); 128 dma.ifcr().write(|w| w.set_tcif(channel_num, true));
129 #[cfg(not(armv6m))]
130 STATE.complete_count[index].fetch_add(1, Ordering::Release); 130 STATE.complete_count[index].fetch_add(1, Ordering::Release);
131 #[cfg(armv6m)]
132 critical_section::with(|_| {
133 let x = STATE.complete_count[index].load(Ordering::Relaxed);
134 STATE.complete_count[index].store(x + 1, Ordering::Release);
135 })
131 } else { 136 } else {
132 return; 137 return;
133 } 138 }
@@ -391,7 +396,14 @@ impl<'a, C: Channel> DmaCtrl for DmaCtrlImpl<'a, C> {
391 } 396 }
392 397
393 fn reset_complete_count(&mut self) -> usize { 398 fn reset_complete_count(&mut self) -> usize {
394 STATE.complete_count[self.0.index()].swap(0, Ordering::AcqRel) 399 #[cfg(not(armv6m))]
400 return STATE.complete_count[self.0.index()].swap(0, Ordering::AcqRel);
401 #[cfg(armv6m)]
402 return critical_section::with(|_| {
403 let x = STATE.complete_count[self.0.index()].load(Ordering::Acquire);
404 STATE.complete_count[self.0.index()].store(0, Ordering::Release);
405 x
406 });
395 } 407 }
396 408
397 fn set_waker(&mut self, waker: &Waker) { 409 fn set_waker(&mut self, waker: &Waker) {
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs
index e883678b5..37c94f827 100644
--- a/embassy-stm32/src/lib.rs
+++ b/embassy-stm32/src/lib.rs
@@ -225,7 +225,9 @@ pub fn init(config: Config) -> Peripherals {
225 225
226 #[cfg(feature = "low-power")] 226 #[cfg(feature = "low-power")]
227 while !crate::rcc::low_power_ready() { 227 while !crate::rcc::low_power_ready() {
228 crate::rcc::clock_refcount_sub(); 228 critical_section::with(|cs| {
229 crate::rcc::clock_refcount_sub(cs);
230 });
229 } 231 }
230 } 232 }
231 233
diff --git a/embassy-stm32/src/low_power.rs b/embassy-stm32/src/low_power.rs
index bb714b8ca..861a59d7b 100644
--- a/embassy-stm32/src/low_power.rs
+++ b/embassy-stm32/src/low_power.rs
@@ -1,7 +1,7 @@
1use core::arch::asm; 1use core::arch::asm;
2use core::marker::PhantomData; 2use core::marker::PhantomData;
3use core::sync::atomic::{compiler_fence, Ordering};
3 4
4use atomic_polyfill::{compiler_fence, Ordering};
5use cortex_m::peripheral::SCB; 5use cortex_m::peripheral::SCB;
6use embassy_executor::*; 6use embassy_executor::*;
7 7
diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs
index a9e53d424..abb53fd10 100644
--- a/embassy-stm32/src/rcc/mod.rs
+++ b/embassy-stm32/src/rcc/mod.rs
@@ -27,9 +27,10 @@ pub use mco::*;
27#[cfg_attr(rcc_wba, path = "wba.rs")] 27#[cfg_attr(rcc_wba, path = "wba.rs")]
28#[cfg_attr(any(rcc_wl5, rcc_wle), path = "wl.rs")] 28#[cfg_attr(any(rcc_wl5, rcc_wle), path = "wl.rs")]
29mod _version; 29mod _version;
30pub use _version::*;
31#[cfg(feature = "low-power")] 30#[cfg(feature = "low-power")]
32use atomic_polyfill::{AtomicU32, Ordering}; 31use core::sync::atomic::{AtomicU32, Ordering};
32
33pub use _version::*;
33 34
34// Model Clock Configuration 35// Model Clock Configuration
35// 36//
@@ -145,14 +146,17 @@ pub fn low_power_ready() -> bool {
145} 146}
146 147
147#[cfg(feature = "low-power")] 148#[cfg(feature = "low-power")]
148pub(crate) fn clock_refcount_add() { 149pub(crate) fn clock_refcount_add(_cs: critical_section::CriticalSection) {
149 // We don't check for overflow because constructing more than u32 peripherals is unlikely 150 // We don't check for overflow because constructing more than u32 peripherals is unlikely
150 CLOCK_REFCOUNT.fetch_add(1, Ordering::Relaxed); 151 let n = CLOCK_REFCOUNT.load(Ordering::Relaxed);
152 CLOCK_REFCOUNT.store(n + 1, Ordering::Relaxed);
151} 153}
152 154
153#[cfg(feature = "low-power")] 155#[cfg(feature = "low-power")]
154pub(crate) fn clock_refcount_sub() { 156pub(crate) fn clock_refcount_sub(_cs: critical_section::CriticalSection) {
155 assert!(CLOCK_REFCOUNT.fetch_sub(1, Ordering::Relaxed) != 0); 157 let n = CLOCK_REFCOUNT.load(Ordering::Relaxed);
158 assert!(n != 0);
159 CLOCK_REFCOUNT.store(n - 1, Ordering::Relaxed);
156} 160}
157 161
158/// Frozen clock frequencies 162/// Frozen clock frequencies
diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs
index 917502412..e88198e6f 100644
--- a/embassy-stm32/src/time_driver.rs
+++ b/embassy-stm32/src/time_driver.rs
@@ -1,9 +1,8 @@
1use core::cell::Cell; 1use core::cell::Cell;
2use core::convert::TryInto; 2use core::convert::TryInto;
3use core::sync::atomic::{compiler_fence, Ordering}; 3use core::sync::atomic::{compiler_fence, AtomicU32, AtomicU8, Ordering};
4use core::{mem, ptr}; 4use core::{mem, ptr};
5 5
6use atomic_polyfill::{AtomicU32, AtomicU8};
7use critical_section::CriticalSection; 6use critical_section::CriticalSection;
8use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; 7use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
9use embassy_sync::blocking_mutex::Mutex; 8use embassy_sync::blocking_mutex::Mutex;
@@ -229,7 +228,9 @@ impl RtcDriver {
229 fn next_period(&self) { 228 fn next_period(&self) {
230 let r = T::regs_gp16(); 229 let r = T::regs_gp16();
231 230
232 let period = self.period.fetch_add(1, Ordering::Relaxed) + 1; 231 // We only modify the period from the timer interrupt, so we know this can't race.
232 let period = self.period.load(Ordering::Relaxed) + 1;
233 self.period.store(period, Ordering::Relaxed);
233 let t = (period as u64) << 15; 234 let t = (period as u64) << 15;
234 235
235 critical_section::with(move |cs| { 236 critical_section::with(move |cs| {
@@ -403,18 +404,15 @@ impl Driver for RtcDriver {
403 } 404 }
404 405
405 unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> { 406 unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> {
406 let id = self.alarm_count.fetch_update(Ordering::AcqRel, Ordering::Acquire, |x| { 407 critical_section::with(|_| {
407 if x < ALARM_COUNT as u8 { 408 let id = self.alarm_count.load(Ordering::Relaxed);
408 Some(x + 1) 409 if id < ALARM_COUNT as u8 {
410 self.alarm_count.store(id + 1, Ordering::Relaxed);
411 Some(AlarmHandle::new(id))
409 } else { 412 } else {
410 None 413 None
411 } 414 }
412 }); 415 })
413
414 match id {
415 Ok(id) => Some(AlarmHandle::new(id)),
416 Err(_) => None,
417 }
418 } 416 }
419 417
420 fn set_alarm_callback(&self, alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ()) { 418 fn set_alarm_callback(&self, alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ()) {
diff --git a/embassy-time/Cargo.toml b/embassy-time/Cargo.toml
index 8f034a9de..e4b88d784 100644
--- a/embassy-time/Cargo.toml
+++ b/embassy-time/Cargo.toml
@@ -218,7 +218,6 @@ embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-rc.1", optional =
218embedded-hal-async = { version = "=1.0.0-rc.1", optional = true} 218embedded-hal-async = { version = "=1.0.0-rc.1", optional = true}
219 219
220futures-util = { version = "0.3.17", default-features = false } 220futures-util = { version = "0.3.17", default-features = false }
221atomic-polyfill = "1.0.1"
222critical-section = "1.1" 221critical-section = "1.1"
223cfg-if = "1.0.0" 222cfg-if = "1.0.0"
224heapless = "0.7" 223heapless = "0.7"
diff --git a/examples/stm32h7/src/bin/eth_client.rs b/examples/stm32h7/src/bin/eth_client.rs
index 4db7aa252..09d27cdbd 100644
--- a/examples/stm32h7/src/bin/eth_client.rs
+++ b/examples/stm32h7/src/bin/eth_client.rs
@@ -105,8 +105,8 @@ async fn main(spawner: Spawner) -> ! {
105 105
106 info!("Network task initialized"); 106 info!("Network task initialized");
107 107
108 static STATE: TcpClientState<1, 1024, 1024> = TcpClientState::new(); 108 let state: TcpClientState<1, 1024, 1024> = TcpClientState::new();
109 let client = TcpClient::new(&stack, &STATE); 109 let client = TcpClient::new(&stack, &state);
110 110
111 loop { 111 loop {
112 let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(10, 42, 0, 1), 8000)); 112 let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(10, 42, 0, 1), 8000));