From 5f01e56728c3a06f1d2298dea6dde000a43acc83 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Thu, 4 Aug 2022 03:02:57 +0300 Subject: Merge v1, v2 DAC and update register definitions --- examples/stm32f1/Cargo.toml | 3 ++- examples/stm32f1/src/bin/dac.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 examples/stm32f1/src/bin/dac.rs (limited to 'examples') diff --git a/examples/stm32f1/Cargo.toml b/examples/stm32f1/Cargo.toml index 9ce553b6d..638f1eff6 100644 --- a/examples/stm32f1/Cargo.toml +++ b/examples/stm32f1/Cargo.toml @@ -6,7 +6,7 @@ version = "0.1.0" [dependencies] embassy-util = { version = "0.1.0", path = "../../embassy-util", features = ["defmt"] } embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "defmt-timestamp-uptime", "time-tick-32768hz"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f103c8", "unstable-pac", "memory-x", "time-driver-any"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f103rc", "unstable-pac", "memory-x", "time-driver-any"] } embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", features = ["defmt"] } @@ -20,6 +20,7 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] } heapless = { version = "0.7.5", default-features = false } nb = "1.0.0" +micromath = "2.0.0" [profile.dev] opt-level = "s" diff --git a/examples/stm32f1/src/bin/dac.rs b/examples/stm32f1/src/bin/dac.rs new file mode 100644 index 000000000..392f5bf4d --- /dev/null +++ b/examples/stm32f1/src/bin/dac.rs @@ -0,0 +1,37 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::executor::Spawner; +use embassy_stm32::dac::{Channel, Dac, Value}; +use embassy_stm32::Peripherals; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner, p: Peripherals) -> ! { + info!("Hello World, dude!"); + + let mut dac = Dac::new_1ch(p.DAC, p.PA4); + + loop { + for v in 0..=255 { + unwrap!(dac.set(Channel::Ch1, Value::Bit8(to_sine_wave(v)))); + unwrap!(dac.trigger(Channel::Ch1)); + } + } +} + +use micromath::F32Ext; + +fn to_sine_wave(v: u8) -> u8 { + if v >= 128 { + // top half + let r = 3.14 * ((v - 128) as f32 / 128.0); + (r.sin() * 128.0 + 127.0) as u8 + } else { + // bottom half + let r = 3.14 + 3.14 * (v as f32 / 128.0); + (r.sin() * 128.0 + 127.0) as u8 + } +} -- cgit From d990740dd5f04980006196c5a416416679d7b457 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Thu, 4 Aug 2022 03:07:42 +0300 Subject: Restore F1 example chip --- examples/stm32f1/Cargo.toml | 2 +- examples/stm32f1/src/bin/dac.rs | 37 ------------------------------------- 2 files changed, 1 insertion(+), 38 deletions(-) delete mode 100644 examples/stm32f1/src/bin/dac.rs (limited to 'examples') diff --git a/examples/stm32f1/Cargo.toml b/examples/stm32f1/Cargo.toml index 638f1eff6..6e77497ae 100644 --- a/examples/stm32f1/Cargo.toml +++ b/examples/stm32f1/Cargo.toml @@ -6,7 +6,7 @@ version = "0.1.0" [dependencies] embassy-util = { version = "0.1.0", path = "../../embassy-util", features = ["defmt"] } embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "defmt-timestamp-uptime", "time-tick-32768hz"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f103rc", "unstable-pac", "memory-x", "time-driver-any"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f103c8", "unstable-pac", "memory-x", "time-driver-any"] } embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", features = ["defmt"] } diff --git a/examples/stm32f1/src/bin/dac.rs b/examples/stm32f1/src/bin/dac.rs deleted file mode 100644 index 392f5bf4d..000000000 --- a/examples/stm32f1/src/bin/dac.rs +++ /dev/null @@ -1,37 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use defmt::*; -use embassy_executor::executor::Spawner; -use embassy_stm32::dac::{Channel, Dac, Value}; -use embassy_stm32::Peripherals; -use {defmt_rtt as _, panic_probe as _}; - -#[embassy_executor::main] -async fn main(_spawner: Spawner, p: Peripherals) -> ! { - info!("Hello World, dude!"); - - let mut dac = Dac::new_1ch(p.DAC, p.PA4); - - loop { - for v in 0..=255 { - unwrap!(dac.set(Channel::Ch1, Value::Bit8(to_sine_wave(v)))); - unwrap!(dac.trigger(Channel::Ch1)); - } - } -} - -use micromath::F32Ext; - -fn to_sine_wave(v: u8) -> u8 { - if v >= 128 { - // top half - let r = 3.14 * ((v - 128) as f32 / 128.0); - (r.sin() * 128.0 + 127.0) as u8 - } else { - // bottom half - let r = 3.14 + 3.14 * (v as f32 / 128.0); - (r.sin() * 128.0 + 127.0) as u8 - } -} -- cgit From 8a25906eff30951e68969c67aabc16ac55826c39 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Thu, 4 Aug 2022 03:31:59 +0300 Subject: Add DACv1 example for F4 --- examples/stm32f1/Cargo.toml | 1 - examples/stm32f4/Cargo.toml | 1 + examples/stm32f4/src/bin/dac.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 examples/stm32f4/src/bin/dac.rs (limited to 'examples') diff --git a/examples/stm32f1/Cargo.toml b/examples/stm32f1/Cargo.toml index 6e77497ae..9ce553b6d 100644 --- a/examples/stm32f1/Cargo.toml +++ b/examples/stm32f1/Cargo.toml @@ -20,7 +20,6 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] } heapless = { version = "0.7.5", default-features = false } nb = "1.0.0" -micromath = "2.0.0" [profile.dev] opt-level = "s" diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index 37464b1e0..3c58320dd 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml @@ -21,6 +21,7 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa heapless = { version = "0.7.5", default-features = false } nb = "1.0.0" embedded-storage = "0.3.0" +micromath = "2.0.0" usb-device = "0.2" usbd-serial = "0.1.1" diff --git a/examples/stm32f4/src/bin/dac.rs b/examples/stm32f4/src/bin/dac.rs new file mode 100644 index 000000000..392f5bf4d --- /dev/null +++ b/examples/stm32f4/src/bin/dac.rs @@ -0,0 +1,37 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::executor::Spawner; +use embassy_stm32::dac::{Channel, Dac, Value}; +use embassy_stm32::Peripherals; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner, p: Peripherals) -> ! { + info!("Hello World, dude!"); + + let mut dac = Dac::new_1ch(p.DAC, p.PA4); + + loop { + for v in 0..=255 { + unwrap!(dac.set(Channel::Ch1, Value::Bit8(to_sine_wave(v)))); + unwrap!(dac.trigger(Channel::Ch1)); + } + } +} + +use micromath::F32Ext; + +fn to_sine_wave(v: u8) -> u8 { + if v >= 128 { + // top half + let r = 3.14 * ((v - 128) as f32 / 128.0); + (r.sin() * 128.0 + 127.0) as u8 + } else { + // bottom half + let r = 3.14 + 3.14 * (v as f32 / 128.0); + (r.sin() * 128.0 + 127.0) as u8 + } +} -- cgit