aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/rust.yml2
-rw-r--r--embassy-stm32/src/bdma/mod.rs2
-rw-r--r--embassy-stm32/src/rcc/mod.rs29
-rw-r--r--examples/stm32f0/.cargo/config.toml10
-rw-r--r--examples/stm32f0/Cargo.toml29
-rw-r--r--examples/stm32f0/build.rs31
-rw-r--r--examples/stm32f0/memory.x6
-rw-r--r--examples/stm32f0/src/bin/hello.rs23
-rw-r--r--examples/stm32f0/src/example_common.rs17
-rw-r--r--stm32-metapac-gen/src/lib.rs70
10 files changed, 195 insertions, 24 deletions
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index 2035c96dc..118143daa 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -88,6 +88,8 @@ jobs:
88 target: thumbv6m-none-eabi 88 target: thumbv6m-none-eabi
89 - package: examples/stm32wb55 89 - package: examples/stm32wb55
90 target: thumbv7em-none-eabihf 90 target: thumbv7em-none-eabihf
91 - package: examples/stm32f0
92 target: thumbv6m-none-eabi
91 93
92 steps: 94 steps:
93 - uses: actions/checkout@v2 95 - uses: actions/checkout@v2
diff --git a/embassy-stm32/src/bdma/mod.rs b/embassy-stm32/src/bdma/mod.rs
index 39e8bec3b..f57358f68 100644
--- a/embassy-stm32/src/bdma/mod.rs
+++ b/embassy-stm32/src/bdma/mod.rs
@@ -177,7 +177,7 @@ pub(crate) unsafe fn init() {
177 } 177 }
178 pac::peripherals! { 178 pac::peripherals! {
179 (bdma, $peri:ident) => { 179 (bdma, $peri:ident) => {
180 crate::peripherals::$peri::enable(); 180 <crate::peripherals::$peri as RccPeripheral>::enable();
181 }; 181 };
182 } 182 }
183} 183}
diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs
index b7b692f15..c02db58fe 100644
--- a/embassy-stm32/src/rcc/mod.rs
+++ b/embassy-stm32/src/rcc/mod.rs
@@ -123,4 +123,33 @@ crate::pac::peripheral_rcc!(
123 123
124 impl RccPeripheral for peripherals::$inst {} 124 impl RccPeripheral for peripherals::$inst {}
125 }; 125 };
126 ($inst:ident, $clk:ident, $enable:ident, $perien:ident) => {
127 impl sealed::RccPeripheral for peripherals::$inst {
128 fn frequency() -> crate::time::Hertz {
129 critical_section::with(|_| {
130 unsafe {
131 let freqs = get_freqs();
132 freqs.$clk
133 }
134 })
135 }
136 fn enable() {
137 critical_section::with(|_| {
138 unsafe {
139 crate::pac::RCC.$enable().modify(|w| w.$perien(true));
140 }
141 })
142 }
143 fn disable() {
144 critical_section::with(|_| {
145 unsafe {
146 crate::pac::RCC.$enable().modify(|w| w.$perien(false));
147 }
148 })
149 }
150 fn reset() {}
151 }
152
153 impl RccPeripheral for peripherals::$inst {}
154 };
126); 155);
diff --git a/examples/stm32f0/.cargo/config.toml b/examples/stm32f0/.cargo/config.toml
new file mode 100644
index 000000000..7506fa91b
--- /dev/null
+++ b/examples/stm32f0/.cargo/config.toml
@@ -0,0 +1,10 @@
1[target.thumbv6m-none-eabi]
2runner = 'probe-run --chip STM32F030F4Px'
3rustflags = [
4 # LLD (shipped with the Rust toolchain) is used as the default linker
5 "-C", "link-arg=-Tlink.x",
6 "-C", "link-arg=-Tdefmt.x",
7]
8
9[build]
10target = "thumbv6m-none-eabi"
diff --git a/examples/stm32f0/Cargo.toml b/examples/stm32f0/Cargo.toml
new file mode 100644
index 000000000..cc2c3f2b7
--- /dev/null
+++ b/examples/stm32f0/Cargo.toml
@@ -0,0 +1,29 @@
1[package]
2name = "embassy-stm32f0-examples"
3version = "0.1.0"
4authors = ["Thales Fragoso <[email protected]>"]
5edition = "2018"
6resolver = "2"
7
8# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9
10[dependencies]
11cortex-m = { version = "0.7.1", features = ["inline-asm"] }
12cortex-m-rt = "0.6.13"
13defmt = "0.2.0"
14defmt-rtt = "0.2.0"
15panic-probe = { version = "0.2.0" }
16rtt-target = { version = "0.3", features = ["cortex-m"] }
17embassy = { path = "../../embassy", features = ["defmt"] }
18embassy-stm32 = { path = "../../embassy-stm32", features = ["defmt", "stm32f030f4"] }
19
20[features]
21default = [
22 "defmt-default",
23]
24defmt-default = []
25defmt-trace = []
26defmt-debug = []
27defmt-info = []
28defmt-warn = []
29defmt-error = []
diff --git a/examples/stm32f0/build.rs b/examples/stm32f0/build.rs
new file mode 100644
index 000000000..d534cc3df
--- /dev/null
+++ b/examples/stm32f0/build.rs
@@ -0,0 +1,31 @@
1//! This build script copies the `memory.x` file from the crate root into
2//! a directory where the linker can always find it at build time.
3//! For many projects this is optional, as the linker always searches the
4//! project root directory -- wherever `Cargo.toml` is. However, if you
5//! are using a workspace or have a more complicated build setup, this
6//! build script becomes required. Additionally, by requesting that
7//! Cargo re-run the build script whenever `memory.x` is changed,
8//! updating `memory.x` ensures a rebuild of the application with the
9//! new memory settings.
10
11use std::env;
12use std::fs::File;
13use std::io::Write;
14use std::path::PathBuf;
15
16fn main() {
17 // Put `memory.x` in our output directory and ensure it's
18 // on the linker search path.
19 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
20 File::create(out.join("memory.x"))
21 .unwrap()
22 .write_all(include_bytes!("memory.x"))
23 .unwrap();
24 println!("cargo:rustc-link-search={}", out.display());
25
26 // By default, Cargo will re-run a build script whenever
27 // any file in the project changes. By specifying `memory.x`
28 // here, we ensure the build script is only re-run when
29 // `memory.x` is changed.
30 println!("cargo:rerun-if-changed=memory.x");
31}
diff --git a/examples/stm32f0/memory.x b/examples/stm32f0/memory.x
new file mode 100644
index 000000000..3bddaed41
--- /dev/null
+++ b/examples/stm32f0/memory.x
@@ -0,0 +1,6 @@
1MEMORY
2{
3 FLASH : ORIGIN = 0x08000000, LENGTH = 16K
4 /* DTCM */
5 RAM : ORIGIN = 0x20000000, LENGTH = 4K
6}
diff --git a/examples/stm32f0/src/bin/hello.rs b/examples/stm32f0/src/bin/hello.rs
new file mode 100644
index 000000000..a78b9892f
--- /dev/null
+++ b/examples/stm32f0/src/bin/hello.rs
@@ -0,0 +1,23 @@
1#![no_std]
2#![no_main]
3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
7
8use defmt::info;
9
10use embassy::executor::Spawner;
11use embassy::time::{Duration, Timer};
12use embassy_stm32::Peripherals;
13
14#[path = "../example_common.rs"]
15mod example_common;
16
17#[embassy::main]
18async fn main(_spawner: Spawner, _p: Peripherals) -> ! {
19 loop {
20 Timer::after(Duration::from_secs(1)).await;
21 info!("Hello");
22 }
23}
diff --git a/examples/stm32f0/src/example_common.rs b/examples/stm32f0/src/example_common.rs
new file mode 100644
index 000000000..54d633837
--- /dev/null
+++ b/examples/stm32f0/src/example_common.rs
@@ -0,0 +1,17 @@
1#![macro_use]
2
3use defmt_rtt as _; // global logger
4use panic_probe as _;
5
6pub use defmt::*;
7
8use core::sync::atomic::{AtomicUsize, Ordering};
9
10defmt::timestamp! {"{=u64}", {
11 static COUNT: AtomicUsize = AtomicUsize::new(0);
12 // NOTE(no-CAS) `timestamps` runs with interrupts disabled
13 let n = COUNT.load(Ordering::Relaxed);
14 COUNT.store(n + 1, Ordering::Relaxed);
15 n as u64
16 }
17}
diff --git a/stm32-metapac-gen/src/lib.rs b/stm32-metapac-gen/src/lib.rs
index 37254f6f0..a9ee88002 100644
--- a/stm32-metapac-gen/src/lib.rs
+++ b/stm32-metapac-gen/src/lib.rs
@@ -142,18 +142,17 @@ macro_rules! peripheral_count {{
142} 142}
143 143
144fn make_dma_channel_counts(out: &mut String, data: &HashMap<String, u8>) { 144fn make_dma_channel_counts(out: &mut String, data: &HashMap<String, u8>) {
145 write!(out, 145 write!(
146 "#[macro_export] 146 out,
147 "#[macro_export]
147macro_rules! dma_channels_count {{ 148macro_rules! dma_channels_count {{
148 ").unwrap(); 149 "
150 )
151 .unwrap();
149 for (name, count) in data { 152 for (name, count) in data {
150 write!(out, 153 write!(out, "({}) => ({});\n", name, count,).unwrap();
151 "({}) => ({});\n",
152 name, count,
153 ).unwrap();
154 } 154 }
155 write!(out, 155 write!(out, " }}\n").unwrap();
156 " }}\n").unwrap();
157} 156}
158 157
159fn make_table(out: &mut String, name: &str, data: &Vec<Vec<String>>) { 158fn make_table(out: &mut String, name: &str, data: &Vec<Vec<String>>) {
@@ -409,11 +408,25 @@ pub fn gen(options: Options) {
409 if let Some(clock_prefix) = clock_prefix { 408 if let Some(clock_prefix) = clock_prefix {
410 // Workaround for clock registers being split on some chip families. Assume fields are 409 // Workaround for clock registers being split on some chip families. Assume fields are
411 // named after peripheral and look for first field matching and use that register. 410 // named after peripheral and look for first field matching and use that register.
412 let en = find_reg_for_field(&rcc, clock_prefix, &format!("{}EN", name)); 411 let mut en = find_reg_for_field(&rcc, clock_prefix, &format!("{}EN", name));
413 let rst = find_reg_for_field(&rcc, clock_prefix, &format!("{}RST", name)); 412 let mut rst =
413 find_reg_for_field(&rcc, clock_prefix, &format!("{}RST", name));
414
415 if en.is_none() && name.ends_with("1") {
416 en = find_reg_for_field(
417 &rcc,
418 clock_prefix,
419 &format!("{}EN", &name[..name.len() - 1]),
420 );
421 rst = find_reg_for_field(
422 &rcc,
423 clock_prefix,
424 &format!("{}RST", &name[..name.len() - 1]),
425 );
426 }
414 427
415 match (en, rst) { 428 match (en, rst) {
416 (Some((enable_reg, enable_field)), Some((reset_reg, reset_field))) => { 429 (Some((enable_reg, enable_field)), reset_reg_field) => {
417 let clock = if clock_prefix.is_empty() { 430 let clock = if clock_prefix.is_empty() {
418 let re = Regex::new("([A-Z]+\\d*).*").unwrap(); 431 let re = Regex::new("([A-Z]+\\d*).*").unwrap();
419 if !re.is_match(enable_reg) { 432 if !re.is_match(enable_reg) {
@@ -436,22 +449,33 @@ pub fn gen(options: Options) {
436 }; 449 };
437 450
438 if !name.starts_with("GPIO") { 451 if !name.starts_with("GPIO") {
439 peripheral_rcc_table.push(vec![ 452 let mut row = Vec::with_capacity(6);
440 name.clone(), 453 row.push(name.clone());
441 clock, 454 row.push(clock);
442 enable_reg.to_ascii_lowercase(), 455 row.push(enable_reg.to_ascii_lowercase());
443 reset_reg.to_ascii_lowercase(), 456
444 format!("set_{}", enable_field.to_ascii_lowercase()), 457 if let Some((reset_reg, reset_field)) = reset_reg_field {
445 format!("set_{}", reset_field.to_ascii_lowercase()), 458 row.push(reset_reg.to_ascii_lowercase());
446 ]); 459 row.push(format!(
460 "set_{}",
461 enable_field.to_ascii_lowercase()
462 ));
463 row.push(format!(
464 "set_{}",
465 reset_field.to_ascii_lowercase()
466 ));
467 } else {
468 row.push(format!(
469 "set_{}",
470 enable_field.to_ascii_lowercase()
471 ));
472 }
473 peripheral_rcc_table.push(row);
447 } 474 }
448 } 475 }
449 (None, Some(_)) => { 476 (None, Some(_)) => {
450 print!("Unable to find enable register for {}", name) 477 print!("Unable to find enable register for {}", name)
451 } 478 }
452 (Some(_), None) => {
453 print!("Unable to find reset register for {}", name)
454 }
455 (None, None) => { 479 (None, None) => {
456 print!("Unable to find enable and reset register for {}", name) 480 print!("Unable to find enable and reset register for {}", name)
457 } 481 }