aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-07-28 17:07:05 +0000
committerGitHub <[email protected]>2023-07-28 17:07:05 +0000
commite97b14c068b5e5f7975311d4aef41c854ef4e16c (patch)
tree99b761bb715b659af83ac2e317e49a12264653fb /examples
parentd752a3f9808ec9be64c720d3f80f152f0b7507df (diff)
parent6dd2fc59418c9d96f049f184694ddaf4845a4425 (diff)
Merge pull request #1705 from JuliDi/stm32h7-dac-dma-example
[STM32] H7 DAC DMA example and feature documentation
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f7/Cargo.toml2
-rw-r--r--examples/stm32f7/build.rs40
-rw-r--r--examples/stm32f7/memory.x12
-rw-r--r--examples/stm32h5/Cargo.toml2
-rw-r--r--examples/stm32h5/memory.x5
-rw-r--r--examples/stm32h7/.cargo/config.toml2
-rw-r--r--examples/stm32h7/Cargo.toml2
-rw-r--r--examples/stm32h7/memory.x5
-rw-r--r--examples/stm32h7/src/bin/dac_dma.rs140
-rw-r--r--examples/stm32l4/Cargo.toml2
-rw-r--r--examples/stm32l4/build.rs30
-rw-r--r--examples/stm32l4/memory.x7
12 files changed, 146 insertions, 103 deletions
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml
index a6964c7bc..a379cbbe3 100644
--- a/examples/stm32f7/Cargo.toml
+++ b/examples/stm32f7/Cargo.toml
@@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0"
6 6
7[dependencies] 7[dependencies]
8# Change stm32f767zi to your chip name, if necessary. 8# Change stm32f767zi to your chip name, if necessary.
9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"] } 9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f767zi", "memory-x", "unstable-pac", "time-driver-any", "exti"] }
10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } 10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] }
11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } 11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
12embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } 12embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
diff --git a/examples/stm32f7/build.rs b/examples/stm32f7/build.rs
index 2b5d412a9..8cd32d7ed 100644
--- a/examples/stm32f7/build.rs
+++ b/examples/stm32f7/build.rs
@@ -1,43 +1,5 @@
1//! adapted from https://github.com/stm32-rs/stm32f7xx-hal/blob/master/build.rs 1fn main() {
2use std::fs::File;
3use std::io::prelude::*;
4use std::path::PathBuf;
5use std::{env, io};
6
7#[derive(Debug)]
8enum Error {
9 Env(env::VarError),
10 Io(io::Error),
11}
12
13impl From<env::VarError> for Error {
14 fn from(error: env::VarError) -> Self {
15 Self::Env(error)
16 }
17}
18
19impl From<io::Error> for Error {
20 fn from(error: io::Error) -> Self {
21 Self::Io(error)
22 }
23}
24
25fn main() -> Result<(), Error> {
26 println!("cargo:rerun-if-changed=build.rs");
27 println!("cargo:rerun-if-changed=memory.x");
28
29 let out_dir = env::var("OUT_DIR")?;
30 let out_dir = PathBuf::from(out_dir);
31
32 let memory_x = include_bytes!("memory.x").as_ref();
33 File::create(out_dir.join("memory.x"))?.write_all(memory_x)?;
34
35 // Tell Cargo where to find the file.
36 println!("cargo:rustc-link-search={}", out_dir.display());
37
38 println!("cargo:rustc-link-arg-bins=--nmagic"); 2 println!("cargo:rustc-link-arg-bins=--nmagic");
39 println!("cargo:rustc-link-arg-bins=-Tlink.x"); 3 println!("cargo:rustc-link-arg-bins=-Tlink.x");
40 println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); 4 println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
41
42 Ok(())
43} 5}
diff --git a/examples/stm32f7/memory.x b/examples/stm32f7/memory.x
deleted file mode 100644
index 899f7a4b8..000000000
--- a/examples/stm32f7/memory.x
+++ /dev/null
@@ -1,12 +0,0 @@
1/* For STM32F765,767,768,769,777,778,779 devices */
2MEMORY
3{
4 /* NOTE K = KiBi = 1024 bytes */
5 FLASH : ORIGIN = 0x08000000, LENGTH = 2M
6 RAM : ORIGIN = 0x20000000, LENGTH = 368K + 16K
7}
8
9/* This is where the call stack will be allocated. */
10/* The stack is of the full descending type. */
11/* NOTE Do NOT modify `_stack_start` unless you know what you are doing */
12_stack_start = ORIGIN(RAM) + LENGTH(RAM);
diff --git a/examples/stm32h5/Cargo.toml b/examples/stm32h5/Cargo.toml
index aebc795c1..51d3bad1f 100644
--- a/examples/stm32h5/Cargo.toml
+++ b/examples/stm32h5/Cargo.toml
@@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0"
6 6
7[dependencies] 7[dependencies]
8# Change stm32h563zi to your chip name, if necessary. 8# Change stm32h563zi to your chip name, if necessary.
9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h563zi", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } 9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h563zi", "memory-x", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] }
10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } 10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] }
11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } 11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
12embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } 12embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] }
diff --git a/examples/stm32h5/memory.x b/examples/stm32h5/memory.x
deleted file mode 100644
index 456061509..000000000
--- a/examples/stm32h5/memory.x
+++ /dev/null
@@ -1,5 +0,0 @@
1MEMORY
2{
3 FLASH : ORIGIN = 0x08000000, LENGTH = 0x200000
4 RAM : ORIGIN = 0x20000000, LENGTH = 0x50000
5}
diff --git a/examples/stm32h7/.cargo/config.toml b/examples/stm32h7/.cargo/config.toml
index 5f680dbce..4160bf855 100644
--- a/examples/stm32h7/.cargo/config.toml
+++ b/examples/stm32h7/.cargo/config.toml
@@ -1,5 +1,5 @@
1[target.thumbv7em-none-eabihf] 1[target.thumbv7em-none-eabihf]
2runner = 'probe-rs run --chip STM32H743ZITx' 2runner = 'probe-rs run --chip STM32H7A3ZITxQ'
3 3
4[build] 4[build]
5target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) 5target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml
index 2d82c0d0d..3c1232e67 100644
--- a/examples/stm32h7/Cargo.toml
+++ b/examples/stm32h7/Cargo.toml
@@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0"
6 6
7[dependencies] 7[dependencies]
8# Change stm32h743bi to your chip name, if necessary. 8# Change stm32h743bi to your chip name, if necessary.
9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } 9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "time-driver-any", "exti", "memory-x", "unstable-pac", "unstable-traits"] }
10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } 10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] }
11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } 11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
12embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } 12embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] }
diff --git a/examples/stm32h7/memory.x b/examples/stm32h7/memory.x
deleted file mode 100644
index 026b14b9b..000000000
--- a/examples/stm32h7/memory.x
+++ /dev/null
@@ -1,5 +0,0 @@
1MEMORY
2{
3 FLASH : ORIGIN = 0x8000000, LENGTH = 1024K
4 RAM : ORIGIN = 0x24000000, LENGTH = 384K
5}
diff --git a/examples/stm32h7/src/bin/dac_dma.rs b/examples/stm32h7/src/bin/dac_dma.rs
new file mode 100644
index 000000000..a9cb5d1ed
--- /dev/null
+++ b/examples/stm32h7/src/bin/dac_dma.rs
@@ -0,0 +1,140 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_stm32::dac::{DacChannel, ValueArray};
8use embassy_stm32::pac::timer::vals::{Mms, Opm};
9use embassy_stm32::peripherals::{TIM6, TIM7};
10use embassy_stm32::rcc::low_level::RccPeripheral;
11use embassy_stm32::time::{mhz, Hertz};
12use embassy_stm32::timer::low_level::Basic16bitInstance;
13use micromath::F32Ext;
14use {defmt_rtt as _, panic_probe as _};
15
16pub type Dac1Type =
17 embassy_stm32::dac::DacCh1<'static, embassy_stm32::peripherals::DAC1, embassy_stm32::peripherals::DMA1_CH3>;
18
19pub type Dac2Type =
20 embassy_stm32::dac::DacCh2<'static, embassy_stm32::peripherals::DAC1, embassy_stm32::peripherals::DMA1_CH4>;
21
22#[embassy_executor::main]
23async fn main(spawner: Spawner) {
24 let mut config = embassy_stm32::Config::default();
25 config.rcc.sys_ck = Some(mhz(400));
26 config.rcc.hclk = Some(mhz(100));
27 config.rcc.pll1.q_ck = Some(mhz(100));
28
29 // Initialize the board and obtain a Peripherals instance
30 let p: embassy_stm32::Peripherals = embassy_stm32::init(config);
31
32 // Obtain two independent channels (p.DAC1 can only be consumed once, though!)
33 let (dac_ch1, dac_ch2) = embassy_stm32::dac::Dac::new(p.DAC1, p.DMA1_CH3, p.DMA1_CH4, p.PA4, p.PA5).split();
34
35 spawner.spawn(dac_task1(dac_ch1)).ok();
36 spawner.spawn(dac_task2(dac_ch2)).ok();
37}
38
39#[embassy_executor::task]
40async fn dac_task1(mut dac: Dac1Type) {
41 let data: &[u8; 256] = &calculate_array::<256>();
42
43 info!("TIM6 frequency is {}", TIM6::frequency());
44 const FREQUENCY: Hertz = Hertz::hz(200);
45
46 // Compute the reload value such that we obtain the FREQUENCY for the sine
47 let reload: u32 = (TIM6::frequency().0 / FREQUENCY.0) / data.len() as u32;
48
49 // Depends on your clock and on the specific chip used, you may need higher or lower values here
50 if reload < 10 {
51 error!("Reload value {} below threshold!", reload);
52 }
53
54 dac.select_trigger(embassy_stm32::dac::Ch1Trigger::Tim6).unwrap();
55 dac.enable_channel().unwrap();
56
57 TIM6::enable();
58 TIM6::regs().arr().modify(|w| w.set_arr(reload as u16 - 1));
59 TIM6::regs().cr2().modify(|w| w.set_mms(Mms::UPDATE));
60 TIM6::regs().cr1().modify(|w| {
61 w.set_opm(Opm::DISABLED);
62 w.set_cen(true);
63 });
64
65 debug!(
66 "TIM6 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}",
67 TIM6::frequency(),
68 FREQUENCY,
69 reload,
70 reload as u16,
71 data.len()
72 );
73
74 // Loop technically not necessary if DMA circular mode is enabled
75 loop {
76 info!("Loop DAC1");
77 if let Err(e) = dac.write(ValueArray::Bit8(data), true).await {
78 error!("Could not write to dac: {}", e);
79 }
80 }
81}
82
83#[embassy_executor::task]
84async fn dac_task2(mut dac: Dac2Type) {
85 let data: &[u8; 256] = &calculate_array::<256>();
86
87 info!("TIM7 frequency is {}", TIM7::frequency());
88
89 const FREQUENCY: Hertz = Hertz::hz(600);
90 let reload: u32 = (TIM7::frequency().0 / FREQUENCY.0) / data.len() as u32;
91
92 if reload < 10 {
93 error!("Reload value {} below threshold!", reload);
94 }
95
96 TIM7::enable();
97 TIM7::regs().arr().modify(|w| w.set_arr(reload as u16 - 1));
98 TIM7::regs().cr2().modify(|w| w.set_mms(Mms::UPDATE));
99 TIM7::regs().cr1().modify(|w| {
100 w.set_opm(Opm::DISABLED);
101 w.set_cen(true);
102 });
103
104 dac.select_trigger(embassy_stm32::dac::Ch2Trigger::Tim7).unwrap();
105
106 debug!(
107 "TIM7 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}",
108 TIM7::frequency(),
109 FREQUENCY,
110 reload,
111 reload as u16,
112 data.len()
113 );
114
115 if let Err(e) = dac.write(ValueArray::Bit8(data), true).await {
116 error!("Could not write to dac: {}", e);
117 }
118}
119
120fn to_sine_wave(v: u8) -> u8 {
121 if v >= 128 {
122 // top half
123 let r = 3.14 * ((v - 128) as f32 / 128.0);
124 (r.sin() * 128.0 + 127.0) as u8
125 } else {
126 // bottom half
127 let r = 3.14 + 3.14 * (v as f32 / 128.0);
128 (r.sin() * 128.0 + 127.0) as u8
129 }
130}
131
132fn calculate_array<const N: usize>() -> [u8; N] {
133 let mut res = [0; N];
134 let mut i = 0;
135 while i < N {
136 res[i] = to_sine_wave(i as u8);
137 i += 1;
138 }
139 res
140}
diff --git a/examples/stm32l4/Cargo.toml b/examples/stm32l4/Cargo.toml
index 0f770e2f0..3b27d8e81 100644
--- a/examples/stm32l4/Cargo.toml
+++ b/examples/stm32l4/Cargo.toml
@@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0"
6 6
7[dependencies] 7[dependencies]
8# Change stm32l4s5vi to your chip name, if necessary. 8# Change stm32l4s5vi to your chip name, if necessary.
9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits", "chrono"] } 9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "memory-x", "time-driver-any", "exti", "unstable-traits", "chrono"] }
10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } 10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] }
11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } 11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
12embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } 12embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
diff --git a/examples/stm32l4/build.rs b/examples/stm32l4/build.rs
index 30691aa97..8cd32d7ed 100644
--- a/examples/stm32l4/build.rs
+++ b/examples/stm32l4/build.rs
@@ -1,34 +1,4 @@
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() { 1fn 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
32 println!("cargo:rustc-link-arg-bins=--nmagic"); 2 println!("cargo:rustc-link-arg-bins=--nmagic");
33 println!("cargo:rustc-link-arg-bins=-Tlink.x"); 3 println!("cargo:rustc-link-arg-bins=-Tlink.x");
34 println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); 4 println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
diff --git a/examples/stm32l4/memory.x b/examples/stm32l4/memory.x
deleted file mode 100644
index eb87d1b54..000000000
--- a/examples/stm32l4/memory.x
+++ /dev/null
@@ -1,7 +0,0 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 /* These values correspond to the STM32L4S5 */
5 FLASH : ORIGIN = 0x08000000, LENGTH = 1024K
6 RAM : ORIGIN = 0x20000000, LENGTH = 128K
7}