aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBob McWhirter <[email protected]>2021-06-08 15:09:17 -0400
committerBob McWhirter <[email protected]>2021-06-08 15:25:38 -0400
commit62955894673b17aad1edba6e34855d5f3a5e8438 (patch)
tree024c04d0822e15cc21fba897e534f42eef0a2946 /examples
parent4f1f63f336e3b8df2e9d5672f145eac4e4ffef9d (diff)
Bring over DAC example (relies upon stm32-data update)
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32h7/Cargo.toml6
-rw-r--r--examples/stm32h7/src/bin/dac.rs92
2 files changed, 94 insertions, 4 deletions
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml
index 23874a565..d7288e4c7 100644
--- a/examples/stm32h7/Cargo.toml
+++ b/examples/stm32h7/Cargo.toml
@@ -1,7 +1,7 @@
1[package] 1[package]
2authors = ["Dario Nieuwenhuis <[email protected]>"] 2authors = ["Dario Nieuwenhuis <[email protected]>"]
3edition = "2018" 3edition = "2018"
4name = "embassy-stm32h4-examples" 4name = "embassy-stm32h7-examples"
5version = "0.1.0" 5version = "0.1.0"
6resolver = "2" 6resolver = "2"
7 7
@@ -23,8 +23,6 @@ embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["
23embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } 23embassy-extras = {version = "0.1.0", path = "../../embassy-extras" }
24stm32h7 = { version = "0.13", features = ["stm32h743"]} 24stm32h7 = { version = "0.13", features = ["stm32h743"]}
25stm32h7xx-hal = { version = "0.9.0", features = ["stm32h743"] } 25stm32h7xx-hal = { version = "0.9.0", features = ["stm32h743"] }
26#stm32l4 = { version = "0.13", features = ["stm32l4x5" ] }
27#stm32l4xx-hal = { version = "0.6.0", features = ["stm32l4x5"] }
28 26
29defmt = "0.2.0" 27defmt = "0.2.0"
30defmt-rtt = "0.2.0" 28defmt-rtt = "0.2.0"
@@ -37,5 +35,5 @@ futures = { version = "0.3.8", default-features = false, features = ["async-awai
37rtt-target = { version = "0.3", features = ["cortex-m"] } 35rtt-target = { version = "0.3", features = ["cortex-m"] }
38heapless = { version = "0.7.1", default-features = false } 36heapless = { version = "0.7.1", default-features = false }
39 37
40#micromath = "2.0.0" 38micromath = "2.0.0"
41 39
diff --git a/examples/stm32h7/src/bin/dac.rs b/examples/stm32h7/src/bin/dac.rs
new file mode 100644
index 000000000..c6f3de7b6
--- /dev/null
+++ b/examples/stm32h7/src/bin/dac.rs
@@ -0,0 +1,92 @@
1#![no_std]
2#![no_main]
3#![feature(trait_alias)]
4#![feature(min_type_alias_impl_trait)]
5#![feature(impl_trait_in_bindings)]
6#![feature(type_alias_impl_trait)]
7
8#[path = "../example_common.rs"]
9mod example_common;
10
11use embassy_stm32::gpio::{Level, Output, Input, Pull, NoPin};
12use embedded_hal::digital::v2::{OutputPin, InputPin};
13use example_common::*;
14
15use cortex_m_rt::entry;
16use stm32h7::stm32h743 as pac;
17use embassy_stm32::spi::{Spi, MODE_0, ByteOrder, Config};
18use embassy_stm32::time::Hertz;
19use embedded_hal::blocking::spi::Transfer;
20use stm32h7xx_hal::{rcc, prelude::*};
21use embassy_stm32::dac::{Dac, Value, Channel};
22
23#[entry]
24fn main() -> ! {
25 info!("Hello World, dude!");
26
27 let pp = pac::Peripherals::take().unwrap();
28
29 let pwrcfg = pp.PWR.constrain()
30 .freeze();
31
32 let rcc = pp.RCC.constrain();
33
34 let ccdr = rcc
35 .sys_ck(96.mhz())
36 .pclk1(48.mhz())
37 .pclk2(48.mhz())
38 .pclk3(48.mhz())
39 .pclk4(48.mhz())
40 .pll1_q_ck(48.mhz())
41 .freeze(pwrcfg, &pp.SYSCFG);
42
43 let pp = unsafe { pac::Peripherals::steal() };
44
45 pp.DBGMCU.cr.modify(|_, w| {
46 w.dbgsleep_d1().set_bit();
47 w.dbgstby_d1().set_bit();
48 w.dbgstop_d1().set_bit();
49 w.d1dbgcken().set_bit();
50 w
51 });
52
53 pp.RCC.apb1lenr.modify(|_, w|{
54 w.dac12en().set_bit();
55 w
56 });
57
58 pp.RCC.ahb4enr.modify(|_, w| {
59 w.gpioaen().set_bit();
60 w.gpioben().set_bit();
61 w.gpiocen().set_bit();
62 w.gpioden().set_bit();
63 w.gpioeen().set_bit();
64 w.gpiofen().set_bit();
65 w
66 });
67
68 let p = embassy_stm32::init(Default::default());
69
70 let mut dac = Dac::new(p.DAC1, p.PA4, NoPin);
71
72 loop {
73 for v in 0..=255 {
74 dac.set(Channel::Ch1, Value::Bit8(to_sine_wave(v)));
75 dac.trigger( Channel::Ch1 );
76 }
77 }
78}
79
80use micromath::F32Ext;
81
82fn to_sine_wave(v: u8) -> u8 {
83 if v >= 128 {
84 // top half
85 let r = 3.14 * ( (v-128) as f32/ 128.0) ;
86 (r.sin() * 128.0 + 127.0) as u8
87 } else {
88 // bottom half
89 let r = 3.14 + 3.14 * (v as f32/ 128.0);
90 (r.sin() * 128.0 + 127.0) as u8
91 }
92}