Skip to content

Commit

Permalink
bugfix(broken atomic): DMA Driver atomic removal
Browse files Browse the repository at this point in the history
This change remvoes all use of atomic from ch32-hal

As discovered in #59, the QingKeV4 atomic implementation is likely
broken. As a result we added a compiler check to make sure the atomic
exetnsion is disabled in ch32-rs/qingke#8. This change updates the
dependency to use the new qingke as well as remove any reference to
`core::atomic` in ch32-hal.
  • Loading branch information
Codetector1374 committed Nov 3, 2024
1 parent d7f1b26 commit 144a644
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jobs:
rustup default nightly
rustup component add rust-src
rustup update
rustup target add riscv32imac-unknown-none-elf
rustup target add riscv32imafc-unknown-none-elf
rustup target add riscv32imc-unknown-none-elf
rustup target add riscv32imfc-unknown-none-elf
rustup target add riscv32i-unknown-none-elf
- name: Fetch Deps
# if this is a pull request
Expand Down
20 changes: 10 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ jobs:
- ch641
include:
- chip: ch32l103f8u6
target: riscv32imac-unknown-none-elf
target: riscv32imc-unknown-none-elf
- chip: ch32v003f4u6
target: riscv32i-unknown-none-elf
- chip: ch32v103c8t6
target: riscv32imac-unknown-none-elf
target: riscv32imc-unknown-none-elf
- chip: ch32v203f8u6
target: riscv32imac-unknown-none-elf
target: riscv32imc-unknown-none-elf
- chip: ch32v208wbu6
target: riscv32imac-unknown-none-elf
target: riscv32imc-unknown-none-elf
- chip: ch32v303vct6
target: riscv32imafc-unknown-none-elf
target: riscv32imfc-unknown-none-elf
- chip: ch32v305fbp6
target: riscv32imafc-unknown-none-elf
target: riscv32imfc-unknown-none-elf
- chip: ch32v307vct6
target: riscv32imafc-unknown-none-elf
target: riscv32imfc-unknown-none-elf
- chip: ch32x035f7p6
target: riscv32imafc-unknown-none-elf
target: riscv32imfc-unknown-none-elf
- chip: ch641
target: riscv32i-unknown-none-elf
runs-on: ubuntu-latest
Expand All @@ -57,8 +57,8 @@ jobs:
rustup default nightly
rustup component add rust-src
rustup update
rustup target add riscv32imac-unknown-none-elf
rustup target add riscv32imafc-unknown-none-elf
rustup target add riscv32imc-unknown-none-elf
rustup target add riscv32imfc-unknown-none-elf
rustup target add riscv32i-unknown-none-elf
- name: Fetch Deps
# if this is a pull request
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ ch32-metapac = { features = [
"rt",
], git = "https://github.com/ch32-rs/ch32-metapac", rev = "cef1944dadf3a5d198e687500b8f5964f29efdf6" }

qingke = { version = "0.4.0", features = ["critical-section-impl"] }
qingke-rt = { version = "0.4.0", optional = true }
qingke = { version = "0.5.0", features = ["critical-section-impl"] }
qingke-rt = { version = "0.5.0", optional = true }

# TODO: remove this
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [
Expand Down
4 changes: 2 additions & 2 deletions examples/ch32l103/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build]
target = "riscv32imac-unknown-none-elf"
target = "riscv32imc-unknown-none-elf"

[target."riscv32imac-unknown-none-elf"]
[target."riscv32imc-unknown-none-elf"]
runner = "wlink -v flash --enable-sdi-print --watch-serial"
# runner = "wlink -v flash"
4 changes: 2 additions & 2 deletions examples/ch32v103/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build]
target = "riscv32imac-unknown-none-elf"
target = "riscv32imc-unknown-none-elf"

[target."riscv32imac-unknown-none-elf"]
[target."riscv32imc-unknown-none-elf"]
rustflags = [
# "-C", "link-arg=-Tlink.x",
]
Expand Down
4 changes: 2 additions & 2 deletions examples/ch32v203/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build]
target = "riscv32imac-unknown-none-elf"
target = "riscv32imc-unknown-none-elf"

[target."riscv32imac-unknown-none-elf"]
[target."riscv32imc-unknown-none-elf"]
rustflags = [
# "-C", "link-arg=-Tlink.x",
]
Expand Down
4 changes: 2 additions & 2 deletions examples/ch32v208/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build]
target = "riscv32imac-unknown-none-elf"
target = "riscv32imc-unknown-none-elf"

[target."riscv32imac-unknown-none-elf"]
[target."riscv32imc-unknown-none-elf"]
runner = "wlink -v flash --enable-sdi-print --watch-serial"
# runner = "wlink -v flash"
4 changes: 2 additions & 2 deletions examples/ch32v305/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build]
target = "riscv32imafc-unknown-none-elf"
target = "riscv32imfc-unknown-none-elf"

[target.riscv32imafc-unknown-none-elf]
[target.riscv32imfc-unknown-none-elf]
# runner = "wlink flash --enable-sdi-print --watch-serial"
runner = "wlink flash"

Expand Down
4 changes: 2 additions & 2 deletions examples/ch32v307/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build]
target = "riscv32imafc-unknown-none-elf"
target = "riscv32imfc-unknown-none-elf"

[target.riscv32imafc-unknown-none-elf]
[target.riscv32imfc-unknown-none-elf]
runner = "wlink flash --enable-sdi-print --watch-serial"
# runner = "wlink -v flash --no-erase --dry-run"
# runner = "probe-rs run --chip CH32V307"
Expand Down
4 changes: 2 additions & 2 deletions examples/ch32x035/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build]
target = "riscv32imac-unknown-none-elf"
target = "riscv32imc-unknown-none-elf"

[target.riscv32imac-unknown-none-elf]
[target.riscv32imc-unknown-none-elf]
runner = "wlink flash --enable-sdi-print --watch-serial"
# runner = "wlink -v flash --no-erase --dry-run"
# runner = "probe-rs run --chip CH32V307"
Expand Down
33 changes: 12 additions & 21 deletions src/dma/dma_bdma.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use core::cell::Cell;
use core::future::{poll_fn, Future};
use core::pin::Pin;
use core::sync::atomic::{compiler_fence, fence, AtomicUsize, Ordering};
use core::sync::atomic::{compiler_fence, fence, Ordering};
use core::task::{Context, Poll, Waker};

use embassy_sync::blocking_mutex::CriticalSectionMutex;
use embassy_sync::waitqueue::AtomicWaker;

use super::ringbuffer::{DmaCtrl, OverrunError, ReadableDmaRingBuffer, WritableDmaRingBuffer};
Expand Down Expand Up @@ -103,13 +105,13 @@ mod bdma_only {

pub(crate) struct ChannelState {
waker: AtomicWaker,
complete_count: AtomicUsize,
complete_count: CriticalSectionMutex<Cell<usize>>,
}

impl ChannelState {
pub(crate) const NEW: Self = Self {
waker: AtomicWaker::new(),
complete_count: AtomicUsize::new(0),
complete_count: CriticalSectionMutex::new(Cell::new(0)),
};
}

Expand Down Expand Up @@ -144,13 +146,9 @@ impl AnyChannel {
} else if isr.tcif(info.num) && cr.read().tcie() {
// Acknowledge transfer complete interrupt
r.ifcr().write(|w| w.set_tcif(info.num, true));
#[cfg(not(qingke_v2))]
state.complete_count.fetch_add(1, Ordering::Release);
#[cfg(qingke_v2)]
critical_section::with(|_| {
let x = state.complete_count.load(Ordering::Relaxed);
state.complete_count.store(x + 1, Ordering::Release);
})
state.complete_count.lock(|v| {
v.set(v.get() + 1);
});
} else {
return;
}
Expand Down Expand Up @@ -180,7 +178,7 @@ impl AnyChannel {
let state: &ChannelState = &STATE[self.id as usize];
let ch = r.ch(info.num);

state.complete_count.store(0, Ordering::Release);
state.complete_count.lock(|c| c.set(0));
self.clear_irqs();

ch.par().write_value(peri_addr as u32); // PADDR
Expand Down Expand Up @@ -246,7 +244,7 @@ impl AnyChannel {
let ch = r.ch(info.num);
let en = ch.cr().read().en();
let circular = ch.cr().read().circ();
let tcif = state.complete_count.load(Ordering::Acquire) != 0;
let tcif = state.complete_count.lock(|c| c.get()) != 0;
en && (circular || !tcif)
}
}
Expand Down Expand Up @@ -471,19 +469,12 @@ impl<'a> DmaCtrl for DmaCtrlImpl<'a> {
}

fn get_complete_count(&self) -> usize {
STATE[self.0.id as usize].complete_count.load(Ordering::Acquire)
STATE[self.0.id as usize].complete_count.lock(|c| c.get())
}

fn reset_complete_count(&mut self) -> usize {
let state = &STATE[self.0.id as usize];
#[cfg(not(qingke_v2))]
return state.complete_count.swap(0, Ordering::AcqRel);
#[cfg(qingke_v2)]
return critical_section::with(|_| {
let x = state.complete_count.load(Ordering::Acquire);
state.complete_count.store(0, Ordering::Release);
x
});
state.complete_count.lock(|c| c.replace(0))
}

fn set_waker(&mut self, waker: &Waker) {
Expand Down

0 comments on commit 144a644

Please sign in to comment.