diff options
| author | Adam Greig <[email protected]> | 2023-11-21 03:12:36 +0000 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-11-25 00:29:45 +0100 |
| commit | 09d7950313427d25be255e4a46a29b6c21113826 (patch) | |
| tree | 0273ab82171fff5cb0225ee2dfff7b50cbf12a2f /examples | |
| parent | 267cbaebe6a4a7160c3bc9239fcbd3dfa4a0d9a9 (diff) | |
STM32 DAC: Rework DAC driver, support all families.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f4/src/bin/dac.rs | 5 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/dac.rs | 5 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/dac_dma.rs | 31 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/dac.rs | 5 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/dac_dma.rs | 31 |
5 files changed, 30 insertions, 47 deletions
diff --git a/examples/stm32f4/src/bin/dac.rs b/examples/stm32f4/src/bin/dac.rs index aaedcfecc..8f14d6078 100644 --- a/examples/stm32f4/src/bin/dac.rs +++ b/examples/stm32f4/src/bin/dac.rs | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::dac::{DacCh1, DacChannel, Value}; | 7 | use embassy_stm32::dac::{DacCh1, Value}; |
| 8 | use embassy_stm32::dma::NoDma; | 8 | use embassy_stm32::dma::NoDma; |
| 9 | use {defmt_rtt as _, panic_probe as _}; | 9 | use {defmt_rtt as _, panic_probe as _}; |
| 10 | 10 | ||
| @@ -14,11 +14,10 @@ async fn main(_spawner: Spawner) -> ! { | |||
| 14 | info!("Hello World, dude!"); | 14 | info!("Hello World, dude!"); |
| 15 | 15 | ||
| 16 | let mut dac = DacCh1::new(p.DAC, NoDma, p.PA4); | 16 | let mut dac = DacCh1::new(p.DAC, NoDma, p.PA4); |
| 17 | unwrap!(dac.set_trigger_enable(false)); | ||
| 18 | 17 | ||
| 19 | loop { | 18 | loop { |
| 20 | for v in 0..=255 { | 19 | for v in 0..=255 { |
| 21 | unwrap!(dac.set(Value::Bit8(to_sine_wave(v)))); | 20 | dac.set(Value::Bit8(to_sine_wave(v))); |
| 22 | } | 21 | } |
| 23 | } | 22 | } |
| 24 | } | 23 | } |
diff --git a/examples/stm32h7/src/bin/dac.rs b/examples/stm32h7/src/bin/dac.rs index 35fd6550f..f66268151 100644 --- a/examples/stm32h7/src/bin/dac.rs +++ b/examples/stm32h7/src/bin/dac.rs | |||
| @@ -4,7 +4,7 @@ | |||
| 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::{DacCh1, DacChannel, Value}; | 7 | use embassy_stm32::dac::{DacCh1, Value}; |
| 8 | use embassy_stm32::dma::NoDma; | 8 | use embassy_stm32::dma::NoDma; |
| 9 | use embassy_stm32::Config; | 9 | use embassy_stm32::Config; |
| 10 | use {defmt_rtt as _, panic_probe as _}; | 10 | use {defmt_rtt as _, panic_probe as _}; |
| @@ -46,11 +46,10 @@ fn main() -> ! { | |||
| 46 | let p = embassy_stm32::init(config); | 46 | let p = embassy_stm32::init(config); |
| 47 | 47 | ||
| 48 | let mut dac = DacCh1::new(p.DAC1, NoDma, p.PA4); | 48 | let mut dac = DacCh1::new(p.DAC1, NoDma, p.PA4); |
| 49 | unwrap!(dac.set_trigger_enable(false)); | ||
| 50 | 49 | ||
| 51 | loop { | 50 | loop { |
| 52 | for v in 0..=255 { | 51 | for v in 0..=255 { |
| 53 | unwrap!(dac.set(Value::Bit8(to_sine_wave(v)))); | 52 | dac.set(Value::Bit8(to_sine_wave(v))); |
| 54 | } | 53 | } |
| 55 | } | 54 | } |
| 56 | } | 55 | } |
diff --git a/examples/stm32h7/src/bin/dac_dma.rs b/examples/stm32h7/src/bin/dac_dma.rs index 12783464a..c19fdd623 100644 --- a/examples/stm32h7/src/bin/dac_dma.rs +++ b/examples/stm32h7/src/bin/dac_dma.rs | |||
| @@ -4,21 +4,15 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::dac::{DacChannel, ValueArray}; | 7 | use embassy_stm32::dac::{DacCh1, DacCh2, ValueArray}; |
| 8 | use embassy_stm32::pac::timer::vals::{Mms, Opm}; | 8 | use embassy_stm32::pac::timer::vals::{Mms, Opm}; |
| 9 | use embassy_stm32::peripherals::{TIM6, TIM7}; | 9 | use embassy_stm32::peripherals::{DAC1, DMA1_CH3, DMA1_CH4, TIM6, TIM7}; |
| 10 | use embassy_stm32::rcc::low_level::RccPeripheral; | 10 | use embassy_stm32::rcc::low_level::RccPeripheral; |
| 11 | use embassy_stm32::time::Hertz; | 11 | use embassy_stm32::time::Hertz; |
| 12 | use embassy_stm32::timer::low_level::Basic16bitInstance; | 12 | use embassy_stm32::timer::low_level::Basic16bitInstance; |
| 13 | use micromath::F32Ext; | 13 | use micromath::F32Ext; |
| 14 | use {defmt_rtt as _, panic_probe as _}; | 14 | use {defmt_rtt as _, panic_probe as _}; |
| 15 | 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] | 16 | #[embassy_executor::main] |
| 23 | async fn main(spawner: Spawner) { | 17 | async fn main(spawner: Spawner) { |
| 24 | let mut config = embassy_stm32::Config::default(); | 18 | let mut config = embassy_stm32::Config::default(); |
| @@ -63,7 +57,7 @@ async fn main(spawner: Spawner) { | |||
| 63 | } | 57 | } |
| 64 | 58 | ||
| 65 | #[embassy_executor::task] | 59 | #[embassy_executor::task] |
| 66 | async fn dac_task1(mut dac: Dac1Type) { | 60 | async fn dac_task1(mut dac: DacCh1<'static, DAC1, DMA1_CH3>) { |
| 67 | let data: &[u8; 256] = &calculate_array::<256>(); | 61 | let data: &[u8; 256] = &calculate_array::<256>(); |
| 68 | 62 | ||
| 69 | info!("TIM6 frequency is {}", TIM6::frequency()); | 63 | info!("TIM6 frequency is {}", TIM6::frequency()); |
| @@ -77,8 +71,9 @@ async fn dac_task1(mut dac: Dac1Type) { | |||
| 77 | error!("Reload value {} below threshold!", reload); | 71 | error!("Reload value {} below threshold!", reload); |
| 78 | } | 72 | } |
| 79 | 73 | ||
| 80 | dac.select_trigger(embassy_stm32::dac::TriggerSel::Tim6).unwrap(); | 74 | dac.set_trigger(embassy_stm32::dac::TriggerSel::Tim6); |
| 81 | dac.enable_channel().unwrap(); | 75 | dac.set_triggering(true); |
| 76 | dac.enable(); | ||
| 82 | 77 | ||
| 83 | TIM6::enable_and_reset(); | 78 | TIM6::enable_and_reset(); |
| 84 | TIM6::regs().arr().modify(|w| w.set_arr(reload as u16 - 1)); | 79 | TIM6::regs().arr().modify(|w| w.set_arr(reload as u16 - 1)); |
| @@ -100,14 +95,12 @@ async fn dac_task1(mut dac: Dac1Type) { | |||
| 100 | // Loop technically not necessary if DMA circular mode is enabled | 95 | // Loop technically not necessary if DMA circular mode is enabled |
| 101 | loop { | 96 | loop { |
| 102 | info!("Loop DAC1"); | 97 | info!("Loop DAC1"); |
| 103 | if let Err(e) = dac.write(ValueArray::Bit8(data), true).await { | 98 | dac.write(ValueArray::Bit8(data), true).await; |
| 104 | error!("Could not write to dac: {}", e); | ||
| 105 | } | ||
| 106 | } | 99 | } |
| 107 | } | 100 | } |
| 108 | 101 | ||
| 109 | #[embassy_executor::task] | 102 | #[embassy_executor::task] |
| 110 | async fn dac_task2(mut dac: Dac2Type) { | 103 | async fn dac_task2(mut dac: DacCh2<'static, DAC1, DMA1_CH4>) { |
| 111 | let data: &[u8; 256] = &calculate_array::<256>(); | 104 | let data: &[u8; 256] = &calculate_array::<256>(); |
| 112 | 105 | ||
| 113 | info!("TIM7 frequency is {}", TIM7::frequency()); | 106 | info!("TIM7 frequency is {}", TIM7::frequency()); |
| @@ -127,7 +120,9 @@ async fn dac_task2(mut dac: Dac2Type) { | |||
| 127 | w.set_cen(true); | 120 | w.set_cen(true); |
| 128 | }); | 121 | }); |
| 129 | 122 | ||
| 130 | dac.select_trigger(embassy_stm32::dac::TriggerSel::Tim7).unwrap(); | 123 | dac.set_trigger(embassy_stm32::dac::TriggerSel::Tim7); |
| 124 | dac.set_triggering(true); | ||
| 125 | dac.enable(); | ||
| 131 | 126 | ||
| 132 | debug!( | 127 | debug!( |
| 133 | "TIM7 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", | 128 | "TIM7 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", |
| @@ -138,9 +133,7 @@ async fn dac_task2(mut dac: Dac2Type) { | |||
| 138 | data.len() | 133 | data.len() |
| 139 | ); | 134 | ); |
| 140 | 135 | ||
| 141 | if let Err(e) = dac.write(ValueArray::Bit8(data), true).await { | 136 | dac.write(ValueArray::Bit8(data), true).await; |
| 142 | error!("Could not write to dac: {}", e); | ||
| 143 | } | ||
| 144 | } | 137 | } |
| 145 | 138 | ||
| 146 | fn to_sine_wave(v: u8) -> u8 { | 139 | fn to_sine_wave(v: u8) -> u8 { |
diff --git a/examples/stm32l4/src/bin/dac.rs b/examples/stm32l4/src/bin/dac.rs index 0193a248e..d6a7ff624 100644 --- a/examples/stm32l4/src/bin/dac.rs +++ b/examples/stm32l4/src/bin/dac.rs | |||
| @@ -3,7 +3,7 @@ | |||
| 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::{DacCh1, DacChannel, Value}; | 6 | use embassy_stm32::dac::{DacCh1, Value}; |
| 7 | use embassy_stm32::dma::NoDma; | 7 | use embassy_stm32::dma::NoDma; |
| 8 | use {defmt_rtt as _, panic_probe as _}; | 8 | use {defmt_rtt as _, panic_probe as _}; |
| 9 | 9 | ||
| @@ -13,11 +13,10 @@ fn main() -> ! { | |||
| 13 | info!("Hello World!"); | 13 | info!("Hello World!"); |
| 14 | 14 | ||
| 15 | let mut dac = DacCh1::new(p.DAC1, NoDma, p.PA4); | 15 | let mut dac = DacCh1::new(p.DAC1, NoDma, p.PA4); |
| 16 | unwrap!(dac.set_trigger_enable(false)); | ||
| 17 | 16 | ||
| 18 | loop { | 17 | loop { |
| 19 | for v in 0..=255 { | 18 | for v in 0..=255 { |
| 20 | unwrap!(dac.set(Value::Bit8(to_sine_wave(v)))); | 19 | dac.set(Value::Bit8(to_sine_wave(v))); |
| 21 | } | 20 | } |
| 22 | } | 21 | } |
| 23 | } | 22 | } |
diff --git a/examples/stm32l4/src/bin/dac_dma.rs b/examples/stm32l4/src/bin/dac_dma.rs index c9f0a4cfe..dc86dbf43 100644 --- a/examples/stm32l4/src/bin/dac_dma.rs +++ b/examples/stm32l4/src/bin/dac_dma.rs | |||
| @@ -4,21 +4,15 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::dac::{DacChannel, ValueArray}; | 7 | use embassy_stm32::dac::{DacCh1, DacCh2, ValueArray}; |
| 8 | use embassy_stm32::pac::timer::vals::{Mms, Opm}; | 8 | use embassy_stm32::pac::timer::vals::{Mms, Opm}; |
| 9 | use embassy_stm32::peripherals::{TIM6, TIM7}; | 9 | use embassy_stm32::peripherals::{DAC1, DMA1_CH3, DMA1_CH4, TIM6, TIM7}; |
| 10 | use embassy_stm32::rcc::low_level::RccPeripheral; | 10 | use embassy_stm32::rcc::low_level::RccPeripheral; |
| 11 | use embassy_stm32::time::Hertz; | 11 | use embassy_stm32::time::Hertz; |
| 12 | use embassy_stm32::timer::low_level::Basic16bitInstance; | 12 | use embassy_stm32::timer::low_level::Basic16bitInstance; |
| 13 | use micromath::F32Ext; | 13 | use micromath::F32Ext; |
| 14 | use {defmt_rtt as _, panic_probe as _}; | 14 | use {defmt_rtt as _, panic_probe as _}; |
| 15 | 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] | 16 | #[embassy_executor::main] |
| 23 | async fn main(spawner: Spawner) { | 17 | async fn main(spawner: Spawner) { |
| 24 | let config = embassy_stm32::Config::default(); | 18 | let config = embassy_stm32::Config::default(); |
| @@ -34,7 +28,7 @@ async fn main(spawner: Spawner) { | |||
| 34 | } | 28 | } |
| 35 | 29 | ||
| 36 | #[embassy_executor::task] | 30 | #[embassy_executor::task] |
| 37 | async fn dac_task1(mut dac: Dac1Type) { | 31 | async fn dac_task1(mut dac: DacCh1<'static, DAC1, DMA1_CH3>) { |
| 38 | let data: &[u8; 256] = &calculate_array::<256>(); | 32 | let data: &[u8; 256] = &calculate_array::<256>(); |
| 39 | 33 | ||
| 40 | info!("TIM6 frequency is {}", TIM6::frequency()); | 34 | info!("TIM6 frequency is {}", TIM6::frequency()); |
| @@ -48,8 +42,9 @@ async fn dac_task1(mut dac: Dac1Type) { | |||
| 48 | error!("Reload value {} below threshold!", reload); | 42 | error!("Reload value {} below threshold!", reload); |
| 49 | } | 43 | } |
| 50 | 44 | ||
| 51 | dac.select_trigger(embassy_stm32::dac::TriggerSel::Tim6).unwrap(); | 45 | dac.set_trigger(embassy_stm32::dac::TriggerSel::Tim6); |
| 52 | dac.enable_channel().unwrap(); | 46 | dac.set_triggering(true); |
| 47 | dac.enable(); | ||
| 53 | 48 | ||
| 54 | TIM6::enable_and_reset(); | 49 | TIM6::enable_and_reset(); |
| 55 | TIM6::regs().arr().modify(|w| w.set_arr(reload as u16 - 1)); | 50 | TIM6::regs().arr().modify(|w| w.set_arr(reload as u16 - 1)); |
| @@ -71,14 +66,12 @@ async fn dac_task1(mut dac: Dac1Type) { | |||
| 71 | // Loop technically not necessary if DMA circular mode is enabled | 66 | // Loop technically not necessary if DMA circular mode is enabled |
| 72 | loop { | 67 | loop { |
| 73 | info!("Loop DAC1"); | 68 | info!("Loop DAC1"); |
| 74 | if let Err(e) = dac.write(ValueArray::Bit8(data), true).await { | 69 | dac.write(ValueArray::Bit8(data), true).await; |
| 75 | error!("Could not write to dac: {}", e); | ||
| 76 | } | ||
| 77 | } | 70 | } |
| 78 | } | 71 | } |
| 79 | 72 | ||
| 80 | #[embassy_executor::task] | 73 | #[embassy_executor::task] |
| 81 | async fn dac_task2(mut dac: Dac2Type) { | 74 | async fn dac_task2(mut dac: DacCh2<'static, DAC1, DMA1_CH4>) { |
| 82 | let data: &[u8; 256] = &calculate_array::<256>(); | 75 | let data: &[u8; 256] = &calculate_array::<256>(); |
| 83 | 76 | ||
| 84 | info!("TIM7 frequency is {}", TIM7::frequency()); | 77 | info!("TIM7 frequency is {}", TIM7::frequency()); |
| @@ -98,7 +91,9 @@ async fn dac_task2(mut dac: Dac2Type) { | |||
| 98 | w.set_cen(true); | 91 | w.set_cen(true); |
| 99 | }); | 92 | }); |
| 100 | 93 | ||
| 101 | dac.select_trigger(embassy_stm32::dac::TriggerSel::Tim7).unwrap(); | 94 | dac.set_trigger(embassy_stm32::dac::TriggerSel::Tim7); |
| 95 | dac.set_triggering(true); | ||
| 96 | dac.enable(); | ||
| 102 | 97 | ||
| 103 | debug!( | 98 | debug!( |
| 104 | "TIM7 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", | 99 | "TIM7 Frequency {}, Target Frequency {}, Reload {}, Reload as u16 {}, Samples {}", |
| @@ -109,9 +104,7 @@ async fn dac_task2(mut dac: Dac2Type) { | |||
| 109 | data.len() | 104 | data.len() |
| 110 | ); | 105 | ); |
| 111 | 106 | ||
| 112 | if let Err(e) = dac.write(ValueArray::Bit8(data), true).await { | 107 | dac.write(ValueArray::Bit8(data), true).await; |
| 113 | error!("Could not write to dac: {}", e); | ||
| 114 | } | ||
| 115 | } | 108 | } |
| 116 | 109 | ||
| 117 | fn to_sine_wave(v: u8) -> u8 { | 110 | fn to_sine_wave(v: u8) -> u8 { |
