diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-06-08 20:12:28 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-06-08 20:12:28 +0200 |
| commit | 8f8914a7892bf871175ad473066cd5bbfdc422e9 (patch) | |
| tree | 0dd2bf890bbc6b8f12633413f9bd5701ecafa3ab | |
| parent | 80eb0ad5264ab0ddc75ad721aefe4a2cff34afbb (diff) | |
| parent | ecd53c916c5448c441b15a3d8e67c2ed38920607 (diff) | |
Merge pull request #234 from bobmcwhirter/l4-dac-ex
Small changes to support DAC example.
| -rw-r--r-- | examples/stm32l4/Cargo.toml | 4 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/dac.rs | 92 |
2 files changed, 96 insertions, 0 deletions
diff --git a/examples/stm32l4/Cargo.toml b/examples/stm32l4/Cargo.toml index ca53f08b1..48125c4f8 100644 --- a/examples/stm32l4/Cargo.toml +++ b/examples/stm32l4/Cargo.toml | |||
| @@ -22,6 +22,7 @@ embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = | |||
| 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32l4s5vi"] } | 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32l4s5vi"] } |
| 23 | embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } | 23 | embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } |
| 24 | stm32l4 = { version = "0.13", features = ["stm32l4x5" ] } | 24 | stm32l4 = { version = "0.13", features = ["stm32l4x5" ] } |
| 25 | stm32l4xx-hal = { version = "0.6.0", features = ["stm32l4x5"] } | ||
| 25 | 26 | ||
| 26 | defmt = "0.2.0" | 27 | defmt = "0.2.0" |
| 27 | defmt-rtt = "0.2.0" | 28 | defmt-rtt = "0.2.0" |
| @@ -33,3 +34,6 @@ panic-probe = { version = "0.2.0", features= ["print-defmt"] } | |||
| 33 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } | 34 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } |
| 34 | rtt-target = { version = "0.3", features = ["cortex-m"] } | 35 | rtt-target = { version = "0.3", features = ["cortex-m"] } |
| 35 | heapless = { version = "0.7.1", default-features = false } | 36 | heapless = { version = "0.7.1", default-features = false } |
| 37 | |||
| 38 | micromath = "2.0.0" | ||
| 39 | |||
diff --git a/examples/stm32l4/src/bin/dac.rs b/examples/stm32l4/src/bin/dac.rs new file mode 100644 index 000000000..5bd5dafbc --- /dev/null +++ b/examples/stm32l4/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"] | ||
| 9 | mod example_common; | ||
| 10 | |||
| 11 | use embassy_stm32::gpio::{Level, Output, Input, Pull, NoPin}; | ||
| 12 | use embedded_hal::digital::v2::{OutputPin, InputPin}; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | use cortex_m_rt::entry; | ||
| 16 | //use stm32f4::stm32f429 as pac; | ||
| 17 | use stm32l4::stm32l4x5 as pac; | ||
| 18 | use embassy_stm32::spi::{Spi, MODE_0, ByteOrder, Config}; | ||
| 19 | use embassy_stm32::time::Hertz; | ||
| 20 | use embedded_hal::blocking::spi::Transfer; | ||
| 21 | use stm32l4xx_hal::{rcc, prelude::*}; | ||
| 22 | use stm32l4xx_hal::rcc::PllSource; | ||
| 23 | use embassy_stm32::dac::{Dac, Value, Channel}; | ||
| 24 | use stm32l4xx_hal::gpio::PA4; | ||
| 25 | |||
| 26 | #[entry] | ||
| 27 | fn main() -> ! { | ||
| 28 | info!("Hello World, dude!"); | ||
| 29 | //let pp = pac::Peripherals::take().unwrap(); | ||
| 30 | let pp = stm32l4xx_hal::stm32::Peripherals::take().unwrap(); | ||
| 31 | let mut flash = pp.FLASH.constrain(); | ||
| 32 | let mut rcc = pp.RCC.constrain(); | ||
| 33 | let mut pwr = pp.PWR.constrain(&mut rcc.apb1r1); | ||
| 34 | |||
| 35 | // TRY the other clock configuration | ||
| 36 | // let clocks = rcc.cfgr.freeze(&mut flash.acr); | ||
| 37 | let clocks = rcc | ||
| 38 | .cfgr | ||
| 39 | .sysclk(80.mhz()) | ||
| 40 | .pclk1(80.mhz()) | ||
| 41 | .pclk2(80.mhz()) | ||
| 42 | .pll_source(PllSource::HSI16) | ||
| 43 | .freeze(&mut flash.acr, &mut pwr); | ||
| 44 | |||
| 45 | let pp = unsafe { pac::Peripherals::steal() }; | ||
| 46 | |||
| 47 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 48 | w.dbg_sleep().set_bit(); | ||
| 49 | w.dbg_standby().set_bit(); | ||
| 50 | w.dbg_stop().set_bit() | ||
| 51 | }); | ||
| 52 | |||
| 53 | pp.RCC.apb1enr1.modify(|_, w| { | ||
| 54 | w.dac1en().set_bit(); | ||
| 55 | w | ||
| 56 | }); | ||
| 57 | |||
| 58 | pp.RCC.ahb2enr.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 | |||
| 80 | use micromath::F32Ext; | ||
| 81 | |||
| 82 | fn 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 | } | ||
