diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-06-29 08:54:28 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-06-29 08:54:28 +0000 |
| commit | 6eb46c419c9f2f071d3c80323e2c68c25b617e20 (patch) | |
| tree | a741b72ea7b403e1d25ab4b2c60a7a9e4a411c6b /examples | |
| parent | 01101e3df0619dca85ab692be8b7c38f249b44e8 (diff) | |
| parent | 96f1525ffe675b7e3ca26f038bc558488c03af9b (diff) | |
Merge pull request #1565 from JuliDi/main
Implement DMA for DAC on STM32
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f4/src/bin/dac.rs | 9 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/dac.rs | 9 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/dac.rs | 17 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/dac_dma.rs | 137 |
4 files changed, 153 insertions, 19 deletions
diff --git a/examples/stm32f4/src/bin/dac.rs b/examples/stm32f4/src/bin/dac.rs index d97ae7082..3a6216712 100644 --- a/examples/stm32f4/src/bin/dac.rs +++ b/examples/stm32f4/src/bin/dac.rs | |||
| @@ -4,7 +4,8 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::dac::{Channel, Dac, Value}; | 7 | use embassy_stm32::dac::{DacCh1, DacChannel, Value}; |
| 8 | use embassy_stm32::dma::NoDma; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | 9 | use {defmt_rtt as _, panic_probe as _}; |
| 9 | 10 | ||
| 10 | #[embassy_executor::main] | 11 | #[embassy_executor::main] |
| @@ -12,12 +13,12 @@ async fn main(_spawner: Spawner) -> ! { | |||
| 12 | let p = embassy_stm32::init(Default::default()); | 13 | let p = embassy_stm32::init(Default::default()); |
| 13 | info!("Hello World, dude!"); | 14 | info!("Hello World, dude!"); |
| 14 | 15 | ||
| 15 | let mut dac = Dac::new_1ch(p.DAC, p.PA4); | 16 | let mut dac = DacCh1::new(p.DAC, NoDma, p.PA4); |
| 16 | 17 | ||
| 17 | loop { | 18 | loop { |
| 18 | for v in 0..=255 { | 19 | for v in 0..=255 { |
| 19 | unwrap!(dac.set(Channel::Ch1, Value::Bit8(to_sine_wave(v)))); | 20 | unwrap!(dac.set(Value::Bit8(to_sine_wave(v)))); |
| 20 | unwrap!(dac.trigger(Channel::Ch1)); | 21 | dac.trigger(); |
| 21 | } | 22 | } |
| 22 | } | 23 | } |
| 23 | } | 24 | } |
diff --git a/examples/stm32h7/src/bin/dac.rs b/examples/stm32h7/src/bin/dac.rs index f12716370..586b4154b 100644 --- a/examples/stm32h7/src/bin/dac.rs +++ b/examples/stm32h7/src/bin/dac.rs | |||
| @@ -4,7 +4,8 @@ | |||
| 4 | 4 | ||
| 5 | use cortex_m_rt::entry; | 5 | use cortex_m_rt::entry; |
| 6 | use defmt::*; | 6 | use defmt::*; |
| 7 | use embassy_stm32::dac::{Channel, Dac, Value}; | 7 | use embassy_stm32::dac::{DacCh1, DacChannel, Value}; |
| 8 | use embassy_stm32::dma::NoDma; | ||
| 8 | use embassy_stm32::time::mhz; | 9 | use embassy_stm32::time::mhz; |
| 9 | use embassy_stm32::Config; | 10 | use embassy_stm32::Config; |
| 10 | use {defmt_rtt as _, panic_probe as _}; | 11 | use {defmt_rtt as _, panic_probe as _}; |
| @@ -19,12 +20,12 @@ fn main() -> ! { | |||
| 19 | config.rcc.pll1.q_ck = Some(mhz(100)); | 20 | config.rcc.pll1.q_ck = Some(mhz(100)); |
| 20 | let p = embassy_stm32::init(config); | 21 | let p = embassy_stm32::init(config); |
| 21 | 22 | ||
| 22 | let mut dac = Dac::new_1ch(p.DAC1, p.PA4); | 23 | let mut dac = DacCh1::new(p.DAC1, NoDma, p.PA4); |
| 23 | 24 | ||
| 24 | loop { | 25 | loop { |
| 25 | for v in 0..=255 { | 26 | for v in 0..=255 { |
| 26 | unwrap!(dac.set(Channel::Ch1, Value::Bit8(to_sine_wave(v)))); | 27 | unwrap!(dac.set(Value::Bit8(to_sine_wave(v)))); |
| 27 | unwrap!(dac.trigger(Channel::Ch1)); | 28 | dac.trigger(); |
| 28 | } | 29 | } |
| 29 | } | 30 | } |
| 30 | } | 31 | } |
diff --git a/examples/stm32l4/src/bin/dac.rs b/examples/stm32l4/src/bin/dac.rs index a36ed5d90..ade43eb35 100644 --- a/examples/stm32l4/src/bin/dac.rs +++ b/examples/stm32l4/src/bin/dac.rs | |||
| @@ -3,26 +3,21 @@ | |||
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_stm32::dac::{Channel, Dac, Value}; | 6 | use embassy_stm32::dac::{DacCh1, DacChannel, Value}; |
| 7 | use embassy_stm32::pac; | 7 | use embassy_stm32::dma::NoDma; |
| 8 | use {defmt_rtt as _, panic_probe as _}; | 8 | use {defmt_rtt as _, panic_probe as _}; |
| 9 | 9 | ||
| 10 | #[cortex_m_rt::entry] | 10 | #[cortex_m_rt::entry] |
| 11 | fn main() -> ! { | 11 | fn main() -> ! { |
| 12 | info!("Hello World!"); | ||
| 13 | |||
| 14 | pac::RCC.apb1enr1().modify(|w| { | ||
| 15 | w.set_dac1en(true); | ||
| 16 | }); | ||
| 17 | |||
| 18 | let p = embassy_stm32::init(Default::default()); | 12 | let p = embassy_stm32::init(Default::default()); |
| 13 | info!("Hello World!"); | ||
| 19 | 14 | ||
| 20 | let mut dac = Dac::new_1ch(p.DAC1, p.PA4); | 15 | let mut dac = DacCh1::new(p.DAC1, NoDma, p.PA4); |
| 21 | 16 | ||
| 22 | loop { | 17 | loop { |
| 23 | for v in 0..=255 { | 18 | for v in 0..=255 { |
| 24 | unwrap!(dac.set(Channel::Ch1, Value::Bit8(to_sine_wave(v)))); | 19 | unwrap!(dac.set(Value::Bit8(to_sine_wave(v)))); |
| 25 | unwrap!(dac.trigger(Channel::Ch1)); | 20 | dac.trigger(); |
| 26 | } | 21 | } |
| 27 | } | 22 | } |
| 28 | } | 23 | } |
diff --git a/examples/stm32l4/src/bin/dac_dma.rs b/examples/stm32l4/src/bin/dac_dma.rs new file mode 100644 index 000000000..c27cc03e1 --- /dev/null +++ b/examples/stm32l4/src/bin/dac_dma.rs | |||
| @@ -0,0 +1,137 @@ | |||
| 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::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 config = embassy_stm32::Config::default(); | ||
| 25 | |||
| 26 | // Initialize the board and obtain a Peripherals instance | ||
| 27 | let p: embassy_stm32::Peripherals = embassy_stm32::init(config); | ||
| 28 | |||
| 29 | // Obtain two independent channels (p.DAC1 can only be consumed once, though!) | ||
| 30 | let (dac_ch1, dac_ch2) = embassy_stm32::dac::Dac::new(p.DAC1, p.DMA1_CH3, p.DMA1_CH4, p.PA4, p.PA5).split(); | ||
| 31 | |||
| 32 | spawner.spawn(dac_task1(dac_ch1)).ok(); | ||
| 33 | spawner.spawn(dac_task2(dac_ch2)).ok(); | ||
| 34 | } | ||
| 35 | |||
| 36 | #[embassy_executor::task] | ||
| 37 | async fn dac_task1(mut dac: Dac1Type) { | ||
| 38 | let data: &[u8; 256] = &calculate_array::<256>(); | ||
| 39 | |||
| 40 | info!("TIM6 frequency is {}", TIM6::frequency()); | ||
| 41 | const FREQUENCY: Hertz = Hertz::hz(200); | ||
| 42 | |||
| 43 | // Compute the reload value such that we obtain the FREQUENCY for the sine | ||
| 44 | let reload: u32 = (TIM6::frequency().0 / FREQUENCY.0) / data.len() as u32; | ||
| 45 | |||
| 46 | // Depends on your clock and on the specific chip used, you may need higher or lower values here | ||
| 47 | if reload < 10 { | ||
| 48 | error!("Reload value {} below threshold!", reload); | ||
| 49 | } | ||
| 50 | |||
| 51 | dac.select_trigger(embassy_stm32::dac::Ch1Trigger::Tim6).unwrap(); | ||
| 52 | dac.enable_channel().unwrap(); | ||
| 53 | |||
| 54 | TIM6::enable(); | ||
| 55 | TIM6::regs().arr().modify(|w| w.set_arr(reload as u16 - 1)); | ||
| 56 | TIM6::regs().cr2().modify(|w| w.set_mms(Mms::UPDATE)); | ||
| 57 | TIM6::regs().cr1().modify(|w| { | ||
| 58 | w.set_opm(Opm::DISABLED); | ||
| 59 | w.set_cen(true); | ||
| 60 | }); | ||
| 61 | |||
| 62 | debug!( | ||
| 63 | "TIM6 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", | ||
| 64 | TIM6::frequency(), | ||
| 65 | FREQUENCY, | ||
| 66 | reload, | ||
| 67 | reload as u16, | ||
| 68 | data.len() | ||
| 69 | ); | ||
| 70 | |||
| 71 | // Loop technically not necessary if DMA circular mode is enabled | ||
| 72 | loop { | ||
| 73 | info!("Loop DAC1"); | ||
| 74 | if let Err(e) = dac.write(ValueArray::Bit8(data), true).await { | ||
| 75 | error!("Could not write to dac: {}", e); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | #[embassy_executor::task] | ||
| 81 | async fn dac_task2(mut dac: Dac2Type) { | ||
| 82 | let data: &[u8; 256] = &calculate_array::<256>(); | ||
| 83 | |||
| 84 | info!("TIM7 frequency is {}", TIM7::frequency()); | ||
| 85 | |||
| 86 | const FREQUENCY: Hertz = Hertz::hz(600); | ||
| 87 | let reload: u32 = (TIM7::frequency().0 / FREQUENCY.0) / data.len() as u32; | ||
| 88 | |||
| 89 | if reload < 10 { | ||
| 90 | error!("Reload value {} below threshold!", reload); | ||
| 91 | } | ||
| 92 | |||
| 93 | TIM7::enable(); | ||
| 94 | TIM7::regs().arr().modify(|w| w.set_arr(reload as u16 - 1)); | ||
| 95 | TIM7::regs().cr2().modify(|w| w.set_mms(Mms::UPDATE)); | ||
| 96 | TIM7::regs().cr1().modify(|w| { | ||
| 97 | w.set_opm(Opm::DISABLED); | ||
| 98 | w.set_cen(true); | ||
| 99 | }); | ||
| 100 | |||
| 101 | dac.select_trigger(embassy_stm32::dac::Ch2Trigger::Tim7).unwrap(); | ||
| 102 | |||
| 103 | debug!( | ||
| 104 | "TIM7 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", | ||
| 105 | TIM7::frequency(), | ||
| 106 | FREQUENCY, | ||
| 107 | reload, | ||
| 108 | reload as u16, | ||
| 109 | data.len() | ||
| 110 | ); | ||
| 111 | |||
| 112 | if let Err(e) = dac.write(ValueArray::Bit8(data), true).await { | ||
| 113 | error!("Could not write to dac: {}", e); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | fn to_sine_wave(v: u8) -> u8 { | ||
| 118 | if v >= 128 { | ||
| 119 | // top half | ||
| 120 | let r = 3.14 * ((v - 128) as f32 / 128.0); | ||
| 121 | (r.sin() * 128.0 + 127.0) as u8 | ||
| 122 | } else { | ||
| 123 | // bottom half | ||
| 124 | let r = 3.14 + 3.14 * (v as f32 / 128.0); | ||
| 125 | (r.sin() * 128.0 + 127.0) as u8 | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | fn calculate_array<const N: usize>() -> [u8; N] { | ||
| 130 | let mut res = [0; N]; | ||
| 131 | let mut i = 0; | ||
| 132 | while i < N { | ||
| 133 | res[i] = to_sine_wave(i as u8); | ||
| 134 | i += 1; | ||
| 135 | } | ||
| 136 | res | ||
| 137 | } | ||
