diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-11-27 00:36:04 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-11-27 00:38:57 +0100 |
| commit | cf13f70ea929ee550e418fe3c954b0415207ca88 (patch) | |
| tree | 9108acd48114f197b39e126c29c507436f618a5d /tests/stm32/src/bin/dac.rs | |
| parent | 6bdacb4f69d523ef814e7e3b9c4a878106ca0e62 (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.rs | 21 |
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"] |
| 8 | mod common; | 8 | mod common; |
| 9 | use core::f32::consts::PI; | ||
| 10 | |||
| 9 | use common::*; | 11 | use common::*; |
| 10 | use defmt::assert; | 12 | use defmt::assert; |
| 11 | use embassy_executor::Spawner; | 13 | use embassy_executor::Spawner; |
| @@ -13,6 +15,7 @@ use embassy_stm32::adc::Adc; | |||
| 13 | use embassy_stm32::dac::{DacCh1, Value}; | 15 | use embassy_stm32::dac::{DacCh1, Value}; |
| 14 | use embassy_stm32::dma::NoDma; | 16 | use embassy_stm32::dma::NoDma; |
| 15 | use embassy_time::{Delay, Timer}; | 17 | use embassy_time::{Delay, Timer}; |
| 18 | use micromath::F32Ext; | ||
| 16 | use {defmt_rtt as _, panic_probe as _}; | 19 | use {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 | ||
| 65 | use core::f32::consts::PI; | ||
| 66 | |||
| 67 | use micromath::F32Ext; | ||
| 68 | |||
| 69 | fn to_sine_wave(v: u8) -> u8 { | 66 | fn to_sine_wave(v: u8) -> u8 { |
| 70 | if v >= 128 { | 67 | if v >= 128 { |
| 71 | // top half | 68 | // top half |
