diff options
| -rw-r--r-- | embassy-stm32/Cargo.toml | 54 | ||||
| -rw-r--r-- | embassy-stm32/src/lib.rs | 3 | ||||
| -rw-r--r-- | examples/stm32f7/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32f7/build.rs | 40 | ||||
| -rw-r--r-- | examples/stm32f7/memory.x | 12 | ||||
| -rw-r--r-- | examples/stm32h5/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32h5/memory.x | 5 | ||||
| -rw-r--r-- | examples/stm32h7/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/stm32h7/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32h7/memory.x | 5 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/dac_dma.rs | 140 | ||||
| -rw-r--r-- | examples/stm32l4/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32l4/build.rs | 30 | ||||
| -rw-r--r-- | examples/stm32l4/memory.x | 7 |
14 files changed, 188 insertions, 118 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 6f34c7416..ba279f795 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml | |||
| @@ -67,6 +67,7 @@ cfg-if = "1.0.0" | |||
| 67 | embedded-io = { version = "0.4.0", features = ["async"], optional = true } | 67 | embedded-io = { version = "0.4.0", features = ["async"], optional = true } |
| 68 | chrono = { version = "^0.4", default-features = false, optional = true} | 68 | chrono = { version = "^0.4", default-features = false, optional = true} |
| 69 | bit_field = "0.10.2" | 69 | bit_field = "0.10.2" |
| 70 | document-features = "0.2.7" | ||
| 70 | 71 | ||
| 71 | [dev-dependencies] | 72 | [dev-dependencies] |
| 72 | critical-section = { version = "1.1", features = ["std"] } | 73 | critical-section = { version = "1.1", features = ["std"] } |
| @@ -78,40 +79,63 @@ stm32-metapac = { version = "13", default-features = false, features = ["metadat | |||
| 78 | 79 | ||
| 79 | [features] | 80 | [features] |
| 80 | default = ["rt"] | 81 | default = ["rt"] |
| 82 | |||
| 83 | ## Enable `stm32-metapac`'s `rt` feature | ||
| 81 | rt = ["stm32-metapac/rt"] | 84 | rt = ["stm32-metapac/rt"] |
| 82 | 85 | ||
| 86 | ## Use [`defmt`](https://docs.rs/defmt/latest/defmt/) for logging | ||
| 83 | defmt = ["dep:defmt", "bxcan/unstable-defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-internal/defmt", "embedded-io?/defmt", "embassy-usb-driver?/defmt", "embassy-net-driver/defmt"] | 87 | defmt = ["dep:defmt", "bxcan/unstable-defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-internal/defmt", "embedded-io?/defmt", "embassy-usb-driver?/defmt", "embassy-net-driver/defmt"] |
| 84 | memory-x = ["stm32-metapac/memory-x"] | 88 | |
| 85 | exti = [] | 89 | exti = [] |
| 86 | 90 | ||
| 87 | # Enables additional driver features that depend on embassy-time | 91 | ## Automatically generate `memory.x` file using [`stm32-metapac`](https://docs.rs/stm32-metapac/) |
| 92 | memory-x = ["stm32-metapac/memory-x"] | ||
| 93 | |||
| 94 | ## Enable nightly-only features | ||
| 95 | nightly = ["embedded-hal-1", "embedded-hal-async", "embedded-storage-async", "dep:embedded-io", "dep:embassy-usb-driver", "embassy-embedded-hal/nightly"] | ||
| 96 | |||
| 97 | ## Re-export stm32-metapac at `embassy_stm32::pac`. | ||
| 98 | ## This is unstable because semver-minor (non-breaking) releases of embassy-stm32 may major-bump (breaking) the stm32-metapac version. | ||
| 99 | ## If this is an issue for you, you're encouraged to directly depend on a fixed version of the PAC. | ||
| 100 | ## There are no plans to make this stable. | ||
| 101 | unstable-pac = [] | ||
| 102 | |||
| 103 | ## Implement embedded-hal 1.0 alpha traits. | ||
| 104 | ## Implement embedded-hal-async traits if `nightly` is set as well. | ||
| 105 | unstable-traits = ["embedded-hal-1", "dep:embedded-hal-nb"] | ||
| 106 | |||
| 107 | #! ## Time | ||
| 108 | |||
| 109 | ## Enables additional driver features that depend on embassy-time | ||
| 88 | time = ["dep:embassy-time"] | 110 | time = ["dep:embassy-time"] |
| 89 | 111 | ||
| 90 | # Features starting with `_` are for internal use only. They're not intended | 112 | # Features starting with `_` are for internal use only. They're not intended |
| 91 | # to be enabled by other crates, and are not covered by semver guarantees. | 113 | # to be enabled by other crates, and are not covered by semver guarantees. |
| 92 | _time-driver = ["time"] | 114 | _time-driver = ["time"] |
| 115 | |||
| 116 | ## Use any time driver | ||
| 93 | time-driver-any = ["_time-driver"] | 117 | time-driver-any = ["_time-driver"] |
| 118 | ## Use TIM2 as time driver | ||
| 94 | time-driver-tim2 = ["_time-driver"] | 119 | time-driver-tim2 = ["_time-driver"] |
| 120 | ## Use TIM3 as time driver | ||
| 95 | time-driver-tim3 = ["_time-driver"] | 121 | time-driver-tim3 = ["_time-driver"] |
| 122 | ## Use TIM4 as time driver | ||
| 96 | time-driver-tim4 = ["_time-driver"] | 123 | time-driver-tim4 = ["_time-driver"] |
| 124 | ## Use TIM5 as time driver | ||
| 97 | time-driver-tim5 = ["_time-driver"] | 125 | time-driver-tim5 = ["_time-driver"] |
| 126 | ## Use TIM12 as time driver | ||
| 98 | time-driver-tim12 = ["_time-driver"] | 127 | time-driver-tim12 = ["_time-driver"] |
| 128 | ## Use TIM15 as time driver | ||
| 99 | time-driver-tim15 = ["_time-driver"] | 129 | time-driver-tim15 = ["_time-driver"] |
| 100 | 130 | ||
| 101 | # Enable nightly-only features | ||
| 102 | nightly = ["embedded-hal-1", "embedded-hal-async", "embedded-storage-async", "dep:embedded-io", "dep:embassy-usb-driver", "embassy-embedded-hal/nightly"] | ||
| 103 | |||
| 104 | # Reexport stm32-metapac at `embassy_stm32::pac`. | ||
| 105 | # This is unstable because semver-minor (non-breaking) releases of embassy-stm32 may major-bump (breaking) the stm32-metapac version. | ||
| 106 | # If this is an issue for you, you're encouraged to directly depend on a fixed version of the PAC. | ||
| 107 | # There are no plans to make this stable. | ||
| 108 | unstable-pac = [] | ||
| 109 | 131 | ||
| 110 | # Implement embedded-hal 1.0 alpha traits. | 132 | #! ## Chip-selection features |
| 111 | # Implement embedded-hal-async traits if `nightly` is set as well. | 133 | #! Select your chip by specifying the model as a feature, e.g. `stm32c011d6`. |
| 112 | unstable-traits = ["embedded-hal-1", "dep:embedded-hal-nb"] | 134 | #! Check the `Cargo.toml` for the latest list of supported chips. |
| 135 | #! | ||
| 136 | #! **Important:** Do not forget to adapt the target chip in your toolchain, | ||
| 137 | #! e.g. in `.cargo/config.toml`. | ||
| 113 | 138 | ||
| 114 | # Chip-selection features | ||
| 115 | stm32c011d6 = [ "stm32-metapac/stm32c011d6" ] | 139 | stm32c011d6 = [ "stm32-metapac/stm32c011d6" ] |
| 116 | stm32c011f4 = [ "stm32-metapac/stm32c011f4" ] | 140 | stm32c011f4 = [ "stm32-metapac/stm32c011f4" ] |
| 117 | stm32c011f6 = [ "stm32-metapac/stm32c011f6" ] | 141 | stm32c011f6 = [ "stm32-metapac/stm32c011f6" ] |
| @@ -1445,4 +1469,4 @@ stm32wle5cb = [ "stm32-metapac/stm32wle5cb" ] | |||
| 1445 | stm32wle5cc = [ "stm32-metapac/stm32wle5cc" ] | 1469 | stm32wle5cc = [ "stm32-metapac/stm32wle5cc" ] |
| 1446 | stm32wle5j8 = [ "stm32-metapac/stm32wle5j8" ] | 1470 | stm32wle5j8 = [ "stm32-metapac/stm32wle5j8" ] |
| 1447 | stm32wle5jb = [ "stm32-metapac/stm32wle5jb" ] | 1471 | stm32wle5jb = [ "stm32-metapac/stm32wle5jb" ] |
| 1448 | stm32wle5jc = [ "stm32-metapac/stm32wle5jc" ] \ No newline at end of file | 1472 | stm32wle5jc = [ "stm32-metapac/stm32wle5jc" ] |
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index bb2ef2fc0..9e67596b0 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs | |||
| @@ -1,6 +1,9 @@ | |||
| 1 | #![cfg_attr(not(test), no_std)] | 1 | #![cfg_attr(not(test), no_std)] |
| 2 | #![cfg_attr(feature = "nightly", feature(async_fn_in_trait, impl_trait_projections))] | 2 | #![cfg_attr(feature = "nightly", feature(async_fn_in_trait, impl_trait_projections))] |
| 3 | 3 | ||
| 4 | //! ## Feature flags | ||
| 5 | #![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)] | ||
| 6 | |||
| 4 | // This must go FIRST so that all the other modules see its macros. | 7 | // This must go FIRST so that all the other modules see its macros. |
| 5 | pub mod fmt; | 8 | pub mod fmt; |
| 6 | include!(concat!(env!("OUT_DIR"), "/_macros.rs")); | 9 | include!(concat!(env!("OUT_DIR"), "/_macros.rs")); |
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. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f767zi", "memory-x", "unstable-pac", "time-driver-any", "exti"] } |
| 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-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 | 1 | fn main() { |
| 2 | use std::fs::File; | ||
| 3 | use std::io::prelude::*; | ||
| 4 | use std::path::PathBuf; | ||
| 5 | use std::{env, io}; | ||
| 6 | |||
| 7 | #[derive(Debug)] | ||
| 8 | enum Error { | ||
| 9 | Env(env::VarError), | ||
| 10 | Io(io::Error), | ||
| 11 | } | ||
| 12 | |||
| 13 | impl From<env::VarError> for Error { | ||
| 14 | fn from(error: env::VarError) -> Self { | ||
| 15 | Self::Env(error) | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | impl From<io::Error> for Error { | ||
| 20 | fn from(error: io::Error) -> Self { | ||
| 21 | Self::Io(error) | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | fn 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 */ | ||
| 2 | MEMORY | ||
| 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. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h563zi", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h563zi", "memory-x", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } |
| 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } | 12 | embassy-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 @@ | |||
| 1 | MEMORY | ||
| 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] |
| 2 | runner = 'probe-rs run --chip STM32H743ZITx' | 2 | runner = 'probe-rs run --chip STM32H7A3ZITxQ' |
| 3 | 3 | ||
| 4 | [build] | 4 | [build] |
| 5 | target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) | 5 | target = "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. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "time-driver-any", "exti", "memory-x", "unstable-pac", "unstable-traits"] } |
| 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } | 12 | embassy-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 @@ | |||
| 1 | MEMORY | ||
| 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 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::dac::{DacChannel, ValueArray}; | ||
| 8 | use embassy_stm32::pac::timer::vals::{Mms, Opm}; | ||
| 9 | use embassy_stm32::peripherals::{TIM6, TIM7}; | ||
| 10 | use embassy_stm32::rcc::low_level::RccPeripheral; | ||
| 11 | use embassy_stm32::time::{mhz, Hertz}; | ||
| 12 | use embassy_stm32::timer::low_level::Basic16bitInstance; | ||
| 13 | use micromath::F32Ext; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | pub type Dac1Type = | ||
| 17 | embassy_stm32::dac::DacCh1<'static, embassy_stm32::peripherals::DAC1, embassy_stm32::peripherals::DMA1_CH3>; | ||
| 18 | |||
| 19 | pub type Dac2Type = | ||
| 20 | embassy_stm32::dac::DacCh2<'static, embassy_stm32::peripherals::DAC1, embassy_stm32::peripherals::DMA1_CH4>; | ||
| 21 | |||
| 22 | #[embassy_executor::main] | ||
| 23 | async 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] | ||
| 40 | async 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] | ||
| 84 | async 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 | |||
| 120 | fn 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 | |||
| 132 | fn 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. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits", "chrono"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "memory-x", "time-driver-any", "exti", "unstable-traits", "chrono"] } |
| 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-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 | |||
| 11 | use std::env; | ||
| 12 | use std::fs::File; | ||
| 13 | use std::io::Write; | ||
| 14 | use std::path::PathBuf; | ||
| 15 | |||
| 16 | fn main() { | 1 | fn 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 @@ | |||
| 1 | MEMORY | ||
| 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 | } | ||
