Skip to content

Commit 4afaf47

Browse files
bors[bot]YuhanLiin
andauthored
Merge #688
688: Fix generated code for MSP430 atomics r=burrbull a=YuhanLiin Rebased from #685 Co-authored-by: YuhanLiin <[email protected]>
2 parents c425172 + adda4aa commit 4afaf47

File tree

6 files changed

+70
-24
lines changed

6 files changed

+70
-24
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ jobs:
7474
# Test MSRV
7575
- { rust: 1.60.0, vendor: Nordic, options: "" }
7676
# Use nightly for architectures which don't support stable
77-
- { rust: nightly, vendor: OTHER, options: "" }
77+
- { rust: nightly, vendor: MSP430, options: "--nightly" }
78+
- { rust: nightly, vendor: MSP430, options: "" }
7879
- { rust: nightly, vendor: Espressif, options: "" }
7980

8081
steps:

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212

1313
## [v0.27.1] - 2022-10-25
1414

15-
- fix cli error with --help/version
15+
- Fix cli error with --help/version
1616
- Don't cast fields with width 17-31 and non-zero offset.
17+
- Fix generated code for MSP430 atomics
1718

1819
## [v0.27.0] - 2022-10-24
1920

ci/script.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,17 @@ main() {
460460
# test_svd LPC5410x_v0.4
461461
;;
462462

463-
# test other targets (architectures)
464-
OTHER)
463+
# MSP430
464+
MSP430)
465465
echo '[dependencies.msp430]' >> $td/Cargo.toml
466-
echo 'version = "0.3.0"' >> $td/Cargo.toml
466+
echo 'version = "0.4.0"' >> $td/Cargo.toml
467+
468+
echo '[dependencies.portable-atomic]' >> $td/Cargo.toml
469+
echo 'version = "0.3.15"' >> $td/Cargo.toml
467470

468471
# Test MSP430
469472
test_svd_for_target msp430 https://raw.githubusercontent.com/pftbest/msp430g2553/v0.3.0-svd/msp430g2553.svd
473+
test_svd_for_target msp430 https://raw.githubusercontent.com/YuhanLiin/msp430fr2355/master/msp430fr2355.svd
470474
;;
471475

472476
# Community-provided RISC-V SVDs

ci/svd2rust-regress/src/svd_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::path::PathBuf;
66
use std::process::{Command, Output};
77

88
const CRATES_ALL: &[&str] = &["critical-section = \"1.0\"", "vcell = \"0.1.2\""];
9-
const CRATES_MSP430: &[&str] = &["msp430 = \"0.2.2\"", "msp430-rt = \"0.2.0\""];
10-
const CRATES_MSP430_NIGHTLY: &[&str] = &["msp430-atomic = \"0.1.2\""];
9+
const CRATES_MSP430: &[&str] = &["msp430 = \"0.4.0\"", "msp430-rt = \"0.4.0\""];
10+
const CRATES_MSP430_NIGHTLY: &[&str] = &["portable-atomic = \"0.3.15\""];
1111
const CRATES_CORTEX_M: &[&str] = &["cortex-m = \"0.7.6\"", "cortex-m-rt = \"0.6.13\""];
1212
const CRATES_RISCV: &[&str] = &["riscv = \"0.9.0\"", "riscv-rt = \"0.9.0\""];
1313
const CRATES_XTENSALX: &[&str] = &["xtensa-lx-rt = \"0.9.0\"", "xtensa-lx = \"0.6.0\""];

src/generate/generic_msp430_atomic.rs

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,91 @@
1-
use msp430_atomic::AtomicOperations;
1+
mod atomic {
2+
use portable_atomic::{AtomicU16, AtomicU8, Ordering};
23

3-
impl<REG: Writable> Reg<REG>
4+
pub trait AtomicOperations {
5+
unsafe fn atomic_or(ptr: *mut Self, val: Self);
6+
unsafe fn atomic_and(ptr: *mut Self, val: Self);
7+
unsafe fn atomic_xor(ptr: *mut Self, val: Self);
8+
}
9+
10+
macro_rules! impl_atomics {
11+
($U:ty, $Atomic:ty) => {
12+
impl AtomicOperations for $U {
13+
unsafe fn atomic_or(ptr: *mut Self, val: Self) {
14+
(*(ptr as *const $Atomic)).fetch_or(val, Ordering::SeqCst);
15+
}
16+
17+
unsafe fn atomic_and(ptr: *mut Self, val: Self) {
18+
(*(ptr as *const $Atomic)).fetch_and(val, Ordering::SeqCst);
19+
}
20+
21+
unsafe fn atomic_xor(ptr: *mut Self, val: Self) {
22+
(*(ptr as *const $Atomic)).fetch_xor(val, Ordering::SeqCst);
23+
}
24+
}
25+
};
26+
}
27+
impl_atomics!(u8, AtomicU8);
28+
impl_atomics!(u16, AtomicU16);
29+
}
30+
use atomic::AtomicOperations;
31+
32+
impl<REG: Readable + Writable> Reg<REG>
433
where
5-
Self: Readable + Writable,
634
REG::Ux: AtomicOperations + Default + core::ops::Not<Output = REG::Ux>,
735
{
836
/// Set high every bit in the register that was set in the write proxy. Leave other bits
937
/// untouched. The write is done in a single atomic instruction.
38+
///
39+
/// # Safety
40+
///
41+
/// The resultant bit pattern may not be valid for the register.
1042
#[inline(always)]
1143
pub unsafe fn set_bits<F>(&self, f: F)
1244
where
13-
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
45+
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
1446
{
15-
let bits = f(&mut W {
47+
let bits = f(&mut REG::Writer::from(W {
1648
bits: Default::default(),
1749
_reg: marker::PhantomData,
18-
})
50+
}))
1951
.bits;
2052
REG::Ux::atomic_or(self.register.as_ptr(), bits);
2153
}
2254

2355
/// Clear every bit in the register that was cleared in the write proxy. Leave other bits
2456
/// untouched. The write is done in a single atomic instruction.
57+
///
58+
/// # Safety
59+
///
60+
/// The resultant bit pattern may not be valid for the register.
2561
#[inline(always)]
2662
pub unsafe fn clear_bits<F>(&self, f: F)
2763
where
28-
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
64+
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
2965
{
30-
let bits = f(&mut W {
66+
let bits = f(&mut REG::Writer::from(W {
3167
bits: !REG::Ux::default(),
3268
_reg: marker::PhantomData,
33-
})
69+
}))
3470
.bits;
3571
REG::Ux::atomic_and(self.register.as_ptr(), bits);
3672
}
3773

3874
/// Toggle every bit in the register that was set in the write proxy. Leave other bits
3975
/// untouched. The write is done in a single atomic instruction.
76+
///
77+
/// # Safety
78+
///
79+
/// The resultant bit pattern may not be valid for the register.
4080
#[inline(always)]
4181
pub unsafe fn toggle_bits<F>(&self, f: F)
4282
where
43-
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
83+
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
4484
{
45-
let bits = f(&mut W {
85+
let bits = f(&mut REG::Writer::from(W {
4686
bits: Default::default(),
4787
_reg: marker::PhantomData,
48-
})
88+
}))
4989
.bits;
5090
REG::Ux::atomic_xor(self.register.as_ptr(), bits);
5191
}

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,21 @@
122122
//! - [`msp430-rt`](https://crates.io/crates/msp430-rt) v0.4.x
123123
//! - [`vcell`](https://crates.io/crates/vcell) v0.1.x
124124
//!
125-
//! If the `--nightly` flag is provided to `svd2rust`, then `msp430-atomic` v0.1.4 is also needed.
125+
//! If the `--nightly` flag is provided to `svd2rust`, then `portable-atomic` v0.3.15 is also needed.
126126
//! Furthermore the "device" feature of `msp430-rt` must be enabled when the `rt` feature is
127127
//! enabled. The `Cargo.toml` of the device crate will look like this:
128128
//!
129129
//! ``` toml
130130
//! [dependencies]
131131
//! critical-section = { version = "1.0", optional = true }
132132
//! msp430 = "0.4.0"
133-
//! msp430-atomic = "0.1.4" # Only when using the --nightly flag
133+
//! portable-atomic = "0.3.15" # Only when using the --nightly flag
134134
//! msp430-rt = { version = "0.4.0", optional = true }
135135
//! vcell = "0.1.0"
136136
//!
137137
//! [features]
138138
//! rt = ["msp430-rt/device"]
139-
//! unstable = ["msp430-atomic"]
139+
//! unstable = ["portable-atomic"]
140140
//! ```
141141
//!
142142
//! ## Other targets
@@ -504,8 +504,8 @@
504504
//! ```ignore
505505
//! // These can be called from different contexts even though they are modifying the same register
506506
//! P1.p1out.set_bits(|w| unsafe { w.bits(1 << 1) });
507-
//! P1.p1out.clear(|w| unsafe { w.bits(!(1 << 2)) });
508-
//! P1.p1out.toggle(|w| unsafe { w.bits(1 << 4) });
507+
//! P1.p1out.clear_bits(|w| unsafe { w.bits(!(1 << 2)) });
508+
//! P1.p1out.toggle_bits(|w| unsafe { w.bits(1 << 4) });
509509
//! ```
510510
#![recursion_limit = "128"]
511511

0 commit comments

Comments
 (0)