aboutsummaryrefslogtreecommitdiff
path: root/tests/stm32/src/bin/dac_l1.rs
blob: 2fe0cf1f1b822e9e2136690acd12f331e3ef995c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#![no_std]
#![no_main]

// required-features: stm32l152re

#[path = "../common.rs"]
mod common;
use core::f32::consts::PI;

use common::*;
use defmt::assert;
use embassy_executor::Spawner;
use embassy_stm32::adc::{Adc, SampleTime};
use embassy_stm32::dac::{DacCh1, Value};
use embassy_stm32::{bind_interrupts, peripherals};
use embassy_time::Timer;
use micromath::F32Ext;
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
    ADC1 => embassy_stm32::adc::InterruptHandler<peripherals::ADC1>;
});

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    // Initialize the board and obtain a Peripherals instance
    let p: embassy_stm32::Peripherals = init();

    let adc = peri!(p, ADC);
    let dac = peri!(p, DAC);
    let dac_pin = peri!(p, DAC_PIN);
    let mut adc_pin = unsafe { core::ptr::read(&dac_pin) };

    let mut dac = DacCh1::new_blocking(dac, dac_pin);
    let mut adc = Adc::new(adc, Irqs);

    #[cfg(feature = "stm32h755zi")]
    let normalization_factor = 256;
    #[cfg(any(
        feature = "stm32f429zi",
        feature = "stm32f446re",
        feature = "stm32g071rb",
        feature = "stm32l152re",
    ))]
    let normalization_factor: i32 = 16;

    dac.set(Value::Bit8(0));
    // Now wait a little to obtain a stable value
    Timer::after_millis(30).await;
    let offset = adc.read(&mut adc_pin, SampleTime::from_bits(0)).await;

    for v in 0..=255 {
        // First set the DAC output value
        let dac_output_val = to_sine_wave(v);
        dac.set(Value::Bit8(dac_output_val));

        // Now wait a little to obtain a stable value
        Timer::after_millis(30).await;

        // Need to steal the peripherals here because PA4 is obviously in use already
        let measured = adc
            .read(
                &mut unsafe { embassy_stm32::Peripherals::steal() }.PA4,
                SampleTime::from_bits(0),
            )
            .await;
        // Calibrate and normalize the measurement to get close to the dac_output_val
        let measured_normalized = ((measured as i32 - offset as i32) / normalization_factor) as i16;

        info!("value / measured: {} / {}", dac_output_val, measured_normalized);

        // The deviations are quite enormous but that does not matter since this is only a quick test
        assert!((dac_output_val as i16 - measured_normalized).abs() < 15);
    }

    info!("Test OK");
    cortex_m::asm::bkpt();
}

fn to_sine_wave(v: u8) -> u8 {
    if v >= 128 {
        // top half
        let r = PI * ((v - 128) as f32 / 128.0);
        (r.sin() * 128.0 + 127.0) as u8
    } else {
        // bottom half
        let r = PI + PI * (v as f32 / 128.0);
        (r.sin() * 128.0 + 127.0) as u8
    }
}