Skip to content

Commit f206126

Browse files
bors[bot]YuhanLiin
andauthored
Merge #693
693: Generate atomic module outside of MSP430 r=burrbull a=YuhanLiin Currently the `--nightly` flag generates atomic register modification code for MSP430. This PR expands this code generation to all architectures. Co-authored-by: YuhanLiin <[email protected]>
2 parents 25d4b15 + 90dd0ff commit f206126

File tree

11 files changed

+62
-49
lines changed

11 files changed

+62
-49
lines changed

.github/workflows/ci.yml

+9-2
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,36 @@ jobs:
5353
- { rust: stable, vendor: Freescale, options: all }
5454
- { rust: stable, vendor: Freescale, options: "" }
5555
- { rust: stable, vendor: Fujitsu, options: "" }
56+
- { rust: stable, vendor: Fujitsu, options: "--atomics" }
5657
- { rust: stable, vendor: GD32, options: all }
5758
- { rust: stable, vendor: GD32, options: "" }
5859
- { rust: stable, vendor: Holtek, options: all }
5960
- { rust: stable, vendor: Holtek, options: "" }
6061
- { rust: stable, vendor: Microchip, options: "" }
62+
- { rust: stable, vendor: Microchip, options: "--atomics" }
6163
- { rust: stable, vendor: Nordic, options: all }
6264
- { rust: stable, vendor: Nordic, options: "" }
6365
- { rust: stable, vendor: Nuvoton, options: "" }
66+
- { rust: stable, vendor: Nuvoton, options: "--atomics" }
6467
- { rust: stable, vendor: NXP, options: all }
6568
- { rust: stable, vendor: NXP, options: "" }
6669
- { rust: stable, vendor: RISC-V, options: "" }
70+
- { rust: stable, vendor: RISC-V, options: "--atomics" }
6771
- { rust: stable, vendor: SiliconLabs, options: all }
6872
- { rust: stable, vendor: SiliconLabs, options: "" }
6973
- { rust: stable, vendor: Spansion, options: "" }
74+
- { rust: stable, vendor: Spansion, options: "--atomics" }
7075
- { rust: stable, vendor: STMicro, options: "" }
71-
- { rust: stable, vendor: STM32-patched, options: "--strict --const_generic --derive_more --pascal_enum_values --max_cluster_size" }
76+
- { rust: stable, vendor: STMicro, options: "--atomics" }
77+
- { rust: stable, vendor: STM32-patched, options: "--strict --const_generic --derive_more --pascal_enum_values --max_cluster_size --atomics" }
7278
- { rust: stable, vendor: Toshiba, options: all }
7379
- { rust: stable, vendor: Toshiba, options: "" }
7480
# Test MSRV
7581
- { rust: 1.61.0, vendor: Nordic, options: "" }
7682
# Use nightly for architectures which don't support stable
77-
- { rust: nightly, vendor: MSP430, options: "--nightly" }
83+
- { rust: nightly, vendor: MSP430, options: "--atomics" }
7884
- { rust: nightly, vendor: MSP430, options: "" }
85+
- { rust: nightly, vendor: Espressif, options: "--atomics" }
7986
- { rust: nightly, vendor: Espressif, options: "" }
8087

8188
steps:

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- Generate atomic register code for non-MSP430 targets
11+
- Change --nightly flag to --atomics
12+
1013
## [v0.27.2] - 2022-11-06
1114

1215
- mark alternate register accessors with `const`, bump `pac` MSRV to 1.61

ci/script.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ main() {
3131

3232
case $OPTIONS in
3333
all)
34-
options="--const_generic --strict --derive_more"
34+
options="--const_generic --strict --derive_more --atomics"
3535
;;
3636
*)
3737
options=$OPTIONS
@@ -46,6 +46,9 @@ main() {
4646
if [[ "$options" == *"--derive_more"* ]]; then
4747
echo 'derive_more = "0.99"' >> $td/Cargo.toml
4848
fi
49+
if [[ "$options" == *"--atomics"* ]]; then
50+
echo 'portable-atomic = { version = "0.3.16", default-features = false }' >> $td/Cargo.toml
51+
fi
4952
echo '[profile.dev]' >> $td/Cargo.toml
5053
echo 'incremental = false' >> $td/Cargo.toml
5154

@@ -465,9 +468,6 @@ main() {
465468
echo '[dependencies.msp430]' >> $td/Cargo.toml
466469
echo 'version = "0.4.0"' >> $td/Cargo.toml
467470

468-
echo '[dependencies.portable-atomic]' >> $td/Cargo.toml
469-
echo 'version = "0.3.15"' >> $td/Cargo.toml
470-
471471
# Test MSP430
472472
test_svd_for_target msp430 https://raw.githubusercontent.com/pftbest/msp430g2553/v0.3.0-svd/msp430g2553.svd
473473
test_svd_for_target msp430 https://raw.githubusercontent.com/YuhanLiin/msp430fr2355/master/msp430fr2355.svd

ci/svd2rust-regress/src/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ struct Opt {
2929
bin_path: Option<PathBuf>,
3030

3131
// TODO: Consider using the same strategy cargo uses for passing args to rustc via `--`
32-
/// Run svd2rust with `--nightly`
33-
#[structopt(long = "nightly")]
34-
nightly: bool,
32+
/// Run svd2rust with `--atomics`
33+
#[structopt(long = "atomics")]
34+
atomics: bool,
3535

3636
/// Filter by chip name, case sensitive, may be combined with other filters
3737
#[structopt(short = "c", long = "chip", raw(validator = "validate_chips"))]
@@ -240,7 +240,7 @@ fn main() {
240240
tests.par_iter().for_each(|t| {
241241
let start = Instant::now();
242242

243-
match svd_test::test(t, &bin_path, rustfmt_bin_path, opt.nightly, opt.verbose) {
243+
match svd_test::test(t, &bin_path, rustfmt_bin_path, opt.atomics, opt.verbose) {
244244
Ok(s) => {
245245
if let Some(stderrs) = s {
246246
let mut buf = String::new();

ci/svd2rust-regress/src/svd_test.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use std::process::{Command, Output};
77

88
const CRATES_ALL: &[&str] = &["critical-section = \"1.0\"", "vcell = \"0.1.2\""];
99
const CRATES_MSP430: &[&str] = &["msp430 = \"0.4.0\"", "msp430-rt = \"0.4.0\""];
10-
const CRATES_MSP430_NIGHTLY: &[&str] = &["portable-atomic = \"0.3.15\""];
10+
const CRATES_ATOMICS: &[&str] =
11+
&["portable-atomic = { version = \"0.3.16\", default-features = false }"];
1112
const CRATES_CORTEX_M: &[&str] = &["cortex-m = \"0.7.6\"", "cortex-m-rt = \"0.6.13\""];
1213
const CRATES_RISCV: &[&str] = &["riscv = \"0.9.0\"", "riscv-rt = \"0.9.0\""];
1314
const CRATES_XTENSALX: &[&str] = &["xtensa-lx-rt = \"0.9.0\"", "xtensa-lx = \"0.6.0\""];
@@ -84,7 +85,7 @@ pub fn test(
8485
t: &TestCase,
8586
bin_path: &PathBuf,
8687
rustfmt_bin_path: Option<&PathBuf>,
87-
nightly: bool,
88+
atomics: bool,
8889
verbosity: u8,
8990
) -> Result<Option<Vec<PathBuf>>> {
9091
let user = match std::env::var("USER") {
@@ -136,11 +137,8 @@ pub fn test(
136137
Msp430 => CRATES_MSP430.iter(),
137138
XtensaLX => CRATES_XTENSALX.iter(),
138139
})
139-
.chain(if nightly {
140-
match &t.arch {
141-
Msp430 => CRATES_MSP430_NIGHTLY.iter(),
142-
_ => [].iter(),
143-
}
140+
.chain(if atomics {
141+
CRATES_ATOMICS.iter()
144142
} else {
145143
[].iter()
146144
})
@@ -179,8 +177,8 @@ pub fn test(
179177
XtensaLX => "xtensa-lx",
180178
};
181179
let mut svd2rust_bin = Command::new(bin_path);
182-
if nightly {
183-
svd2rust_bin.arg("--nightly");
180+
if atomics {
181+
svd2rust_bin.arg("--atomics");
184182
}
185183

186184
let output = svd2rust_bin

ci/svd2rust-regress/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4195,7 +4195,7 @@ pub const TESTS: &[&TestCase] = &[
41954195
mfgr: TexasInstruments,
41964196
chip: "msp430fr2355",
41974197
svd_url: Some(
4198-
"https://raw.githubusercontent.com/YuhanLiin/msp430fr2355/rt-up/msp430fr2355.svd",
4198+
"https://raw.githubusercontent.com/YuhanLiin/msp430fr2355/master/msp430fr2355.svd",
41994199
),
42004200
should_pass: true,
42014201
run_when: Always,

src/generate/device.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
147147
}
148148

149149
let generic_file = include_str!("generic.rs");
150-
let msp430_atomic_file = include_str!("generic_msp430_atomic.rs");
150+
let generic_atomic_file = include_str!("generic_atomic.rs");
151151
let array_proxy = include_str!("array_proxy.rs");
152152
if config.generic_mod {
153153
let mut file = File::create(config.output_dir.join("generic.rs"))?;
154154
writeln!(file, "{}", generic_file)?;
155-
if config.target == Target::Msp430 && config.nightly {
156-
writeln!(file, "\n{}", msp430_atomic_file)?;
155+
if config.atomics {
156+
writeln!(file, "\n{}", generic_atomic_file)?;
157157
}
158158
if config.const_generic {
159159
writeln!(file, "{}", array_proxy)?;
@@ -169,8 +169,8 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
169169
}
170170
} else {
171171
let mut tokens = syn::parse_file(generic_file)?.into_token_stream();
172-
if config.target == Target::Msp430 && config.nightly {
173-
syn::parse_file(msp430_atomic_file)?.to_tokens(&mut tokens);
172+
if config.atomics {
173+
syn::parse_file(generic_atomic_file)?.to_tokens(&mut tokens);
174174
}
175175
if config.const_generic {
176176
syn::parse_file(array_proxy)?.to_tokens(&mut tokens);

src/generate/generic_msp430_atomic.rs renamed to src/generate/generic_atomic.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod atomic {
2-
use portable_atomic::{AtomicU16, AtomicU8, Ordering};
2+
use portable_atomic::Ordering;
33

44
pub trait AtomicOperations {
55
unsafe fn atomic_or(ptr: *mut Self, val: Self);
@@ -11,21 +11,30 @@ mod atomic {
1111
($U:ty, $Atomic:ty) => {
1212
impl AtomicOperations for $U {
1313
unsafe fn atomic_or(ptr: *mut Self, val: Self) {
14-
(*(ptr as *const $Atomic)).fetch_or(val, Ordering::SeqCst);
14+
(*(ptr as *const $Atomic)).or(val, Ordering::SeqCst);
1515
}
1616

1717
unsafe fn atomic_and(ptr: *mut Self, val: Self) {
18-
(*(ptr as *const $Atomic)).fetch_and(val, Ordering::SeqCst);
18+
(*(ptr as *const $Atomic)).and(val, Ordering::SeqCst);
1919
}
2020

2121
unsafe fn atomic_xor(ptr: *mut Self, val: Self) {
22-
(*(ptr as *const $Atomic)).fetch_xor(val, Ordering::SeqCst);
22+
(*(ptr as *const $Atomic)).xor(val, Ordering::SeqCst);
2323
}
2424
}
2525
};
2626
}
27-
impl_atomics!(u8, AtomicU8);
28-
impl_atomics!(u16, AtomicU16);
27+
28+
impl_atomics!(u8, portable_atomic::AtomicU8);
29+
impl_atomics!(u16, portable_atomic::AtomicU16);
30+
31+
// Exclude 16-bit archs from 32-bit atomics
32+
#[cfg(not(target_pointer_width = "16"))]
33+
impl_atomics!(u32, portable_atomic::AtomicU32);
34+
35+
// Enable 64-bit atomics for 64-bit RISCV
36+
#[cfg(any(target_pointer_width = "64", target_has_atomic = "64"))]
37+
impl_atomics!(u64, portable_atomic::AtomicU64);
2938
}
3039
use atomic::AtomicOperations;
3140

src/lib.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,18 @@
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 `portable-atomic` v0.3.15 is also needed.
126-
//! Furthermore the "device" feature of `msp430-rt` must be enabled when the `rt` feature is
125+
//! The "device" feature of `msp430-rt` must be enabled when the `rt` feature is
127126
//! enabled. The `Cargo.toml` of the device crate will look like this:
128127
//!
129128
//! ``` toml
130129
//! [dependencies]
131130
//! critical-section = { version = "1.0", optional = true }
132131
//! msp430 = "0.4.0"
133-
//! portable-atomic = "0.3.15" # Only when using the --nightly flag
134132
//! msp430-rt = { version = "0.4.0", optional = true }
135133
//! vcell = "0.1.0"
136134
//!
137135
//! [features]
138136
//! rt = ["msp430-rt/device"]
139-
//! unstable = ["portable-atomic"]
140137
//! ```
141138
//!
142139
//! ## Other targets
@@ -487,17 +484,16 @@
487484
//!
488485
//! [`interrupt`]: https://docs.rs/cortex-m-rt-macros/0.1/cortex_m_rt_macros/attr.interrupt.html
489486
//!
490-
//! ## the `--nightly` flag
487+
//! ## the `--atomics` flag
491488
//!
492-
//! The `--nightly` flag can be passed to `svd2rust` to enable features in the generated api that are only available to a nightly
493-
//! compiler. The following features are gated by the `--nightly` flag:
489+
//! The `--atomics` flag can be passed to `svd2rust` to extends the register API with operations to
490+
//! atomically set, clear, and toggle specific bits. The atomic operations allow limited
491+
//! modification of register bits without read-modify-write sequences. As such, they can be
492+
//! concurrently called on different bits in the same register without data races. This flag won't
493+
//! work for RISCV chips without the atomic extension.
494494
//!
495-
//! ### MSP430
496-
//!
497-
//! Extends the register API with operations to atomically set, clear, and toggle specific bits.
498-
//! The atomic operations allow limited modification of register bits without read-modify-write
499-
//! sequences. As such, they can be concurrently called on different bits in the same register
500-
//! without data races.
495+
//! `portable-atomic` v0.3.16 must be added to the dependencies, with default features off to
496+
//! disable the `fallback` feature.
501497
//!
502498
//! Usage examples:
503499
//!

src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ fn run() -> Result<()> {
6767
.value_name("ARCH"),
6868
)
6969
.arg(
70-
Arg::new("nightly")
71-
.long("nightly")
70+
Arg::new("atomics")
71+
.long("atomics")
7272
.action(ArgAction::SetTrue)
73-
.help("Enable features only available to nightly rustc"),
73+
.help("Generate atomic register modification API"),
7474
)
7575
.arg(
7676
Arg::new("const_generic")

src/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Config {
2727
#[cfg_attr(feature = "serde", serde(default))]
2828
pub target: Target,
2929
#[cfg_attr(feature = "serde", serde(default))]
30-
pub nightly: bool,
30+
pub atomics: bool,
3131
#[cfg_attr(feature = "serde", serde(default))]
3232
pub generic_mod: bool,
3333
#[cfg_attr(feature = "serde", serde(default))]
@@ -68,7 +68,7 @@ impl Default for Config {
6868
fn default() -> Self {
6969
Self {
7070
target: Target::default(),
71-
nightly: false,
71+
atomics: false,
7272
generic_mod: false,
7373
make_mod: false,
7474
const_generic: false,

0 commit comments

Comments
 (0)