aboutsummaryrefslogtreecommitdiff
path: root/tests/stm32/src/bin/dac.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-11-27 00:36:04 +0100
committerDario Nieuwenhuis <[email protected]>2023-11-27 00:38:57 +0100
commitcf13f70ea929ee550e418fe3c954b0415207ca88 (patch)
tree9108acd48114f197b39e126c29c507436f618a5d /tests/stm32/src/bin/dac.rs
parent6bdacb4f69d523ef814e7e3b9c4a878106ca0e62 (diff)
stm32/test: add stm32f446 (board not in HIL rig yet)
Diffstat (limited to 'tests/stm32/src/bin/dac.rs')
-rw-r--r--tests/stm32/src/bin/dac.rs21
1 files changed, 9 insertions, 12 deletions
diff --git a/tests/stm32/src/bin/dac.rs b/tests/stm32/src/bin/dac.rs
index 824eb8803..29d5f866f 100644
--- a/tests/stm32/src/bin/dac.rs
+++ b/tests/stm32/src/bin/dac.rs
@@ -6,6 +6,8 @@
6 6
7#[path = "../common.rs"] 7#[path = "../common.rs"]
8mod common; 8mod common;
9use core::f32::consts::PI;
10
9use common::*; 11use common::*;
10use defmt::assert; 12use defmt::assert;
11use embassy_executor::Spawner; 13use embassy_executor::Spawner;
@@ -13,6 +15,7 @@ use embassy_stm32::adc::Adc;
13use embassy_stm32::dac::{DacCh1, Value}; 15use embassy_stm32::dac::{DacCh1, Value};
14use embassy_stm32::dma::NoDma; 16use embassy_stm32::dma::NoDma;
15use embassy_time::{Delay, Timer}; 17use embassy_time::{Delay, Timer};
18use micromath::F32Ext;
16use {defmt_rtt as _, panic_probe as _}; 19use {defmt_rtt as _, panic_probe as _};
17 20
18#[embassy_executor::main] 21#[embassy_executor::main]
@@ -20,24 +23,22 @@ async fn main(_spawner: Spawner) {
20 // Initialize the board and obtain a Peripherals instance 23 // Initialize the board and obtain a Peripherals instance
21 let p: embassy_stm32::Peripherals = embassy_stm32::init(config()); 24 let p: embassy_stm32::Peripherals = embassy_stm32::init(config());
22 25
23 #[cfg(feature = "stm32f429zi")] 26 let dac = peri!(p, DAC);
24 let dac_peripheral = p.DAC; 27 let dac_pin = peri!(p, DAC_PIN);
25 28 let mut adc_pin = unsafe { core::ptr::read(&dac_pin) };
26 #[cfg(any(feature = "stm32h755zi", feature = "stm32g071rb"))]
27 let dac_peripheral = p.DAC1;
28 29
29 let mut dac = DacCh1::new(dac_peripheral, NoDma, p.PA4); 30 let mut dac = DacCh1::new(dac, NoDma, dac_pin);
30 let mut adc = Adc::new(p.ADC1, &mut Delay); 31 let mut adc = Adc::new(p.ADC1, &mut Delay);
31 32
32 #[cfg(feature = "stm32h755zi")] 33 #[cfg(feature = "stm32h755zi")]
33 let normalization_factor = 256; 34 let normalization_factor = 256;
34 #[cfg(any(feature = "stm32f429zi", feature = "stm32g071rb"))] 35 #[cfg(any(feature = "stm32f429zi", feature = "stm32f446re", feature = "stm32g071rb"))]
35 let normalization_factor: i32 = 16; 36 let normalization_factor: i32 = 16;
36 37
37 dac.set(Value::Bit8(0)); 38 dac.set(Value::Bit8(0));
38 // Now wait a little to obtain a stable value 39 // Now wait a little to obtain a stable value
39 Timer::after_millis(30).await; 40 Timer::after_millis(30).await;
40 let offset = adc.read(&mut unsafe { embassy_stm32::Peripherals::steal() }.PA4); 41 let offset = adc.read(&mut adc_pin);
41 42
42 for v in 0..=255 { 43 for v in 0..=255 {
43 // First set the DAC output value 44 // First set the DAC output value
@@ -62,10 +63,6 @@ async fn main(_spawner: Spawner) {
62 cortex_m::asm::bkpt(); 63 cortex_m::asm::bkpt();
63} 64}
64 65
65use core::f32::consts::PI;
66
67use micromath::F32Ext;
68
69fn to_sine_wave(v: u8) -> u8 { 66fn to_sine_wave(v: u8) -> u8 {
70 if v >= 128 { 67 if v >= 128 {
71 // top half 68 // top half