2023-07-27 19:04:43 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2023-12-08 20:24:15 +01:00
|
|
|
// required-features: dac
|
2023-07-27 19:04:43 +02:00
|
|
|
|
|
|
|
#[path = "../common.rs"]
|
|
|
|
mod common;
|
2023-11-27 00:36:04 +01:00
|
|
|
use core::f32::consts::PI;
|
|
|
|
|
2023-07-27 19:04:43 +02:00
|
|
|
use common::*;
|
|
|
|
use defmt::assert;
|
|
|
|
use embassy_executor::Spawner;
|
|
|
|
use embassy_stm32::adc::Adc;
|
2023-11-21 03:12:36 +00:00
|
|
|
use embassy_stm32::dac::{DacCh1, Value};
|
2023-07-27 19:04:43 +02:00
|
|
|
use embassy_stm32::dma::NoDma;
|
2024-04-10 10:49:42 +03:00
|
|
|
use embassy_time::Timer;
|
2023-11-27 00:36:04 +01:00
|
|
|
use micromath::F32Ext;
|
2023-07-27 19:04:43 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
// Initialize the board and obtain a Peripherals instance
|
|
|
|
let p: embassy_stm32::Peripherals = embassy_stm32::init(config());
|
|
|
|
|
2023-12-08 20:24:15 +01:00
|
|
|
let adc = peri!(p, ADC);
|
2023-11-27 00:36:04 +01:00
|
|
|
let dac = peri!(p, DAC);
|
|
|
|
let dac_pin = peri!(p, DAC_PIN);
|
|
|
|
let mut adc_pin = unsafe { core::ptr::read(&dac_pin) };
|
2023-07-27 19:04:43 +02:00
|
|
|
|
2023-11-27 00:36:04 +01:00
|
|
|
let mut dac = DacCh1::new(dac, NoDma, dac_pin);
|
2024-04-10 10:49:42 +03:00
|
|
|
let mut adc = Adc::new(adc);
|
2023-07-27 19:04:43 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "stm32h755zi")]
|
|
|
|
let normalization_factor = 256;
|
2023-11-27 00:36:04 +01:00
|
|
|
#[cfg(any(feature = "stm32f429zi", feature = "stm32f446re", feature = "stm32g071rb"))]
|
2023-07-27 19:04:43 +02:00
|
|
|
let normalization_factor: i32 = 16;
|
|
|
|
|
2023-11-21 03:12:36 +00:00
|
|
|
dac.set(Value::Bit8(0));
|
2023-07-27 19:04:43 +02:00
|
|
|
// Now wait a little to obtain a stable value
|
2023-10-15 00:57:25 +01:00
|
|
|
Timer::after_millis(30).await;
|
2023-11-27 00:36:04 +01:00
|
|
|
let offset = adc.read(&mut adc_pin);
|
2023-07-27 19:04:43 +02:00
|
|
|
|
|
|
|
for v in 0..=255 {
|
|
|
|
// First set the DAC output value
|
|
|
|
let dac_output_val = to_sine_wave(v);
|
2023-11-21 03:12:36 +00:00
|
|
|
dac.set(Value::Bit8(dac_output_val));
|
2023-07-27 19:04:43 +02:00
|
|
|
|
|
|
|
// Now wait a little to obtain a stable value
|
2023-10-15 00:57:25 +01:00
|
|
|
Timer::after_millis(30).await;
|
2023-07-27 19:04:43 +02:00
|
|
|
|
|
|
|
// Need to steal the peripherals here because PA4 is obviously in use already
|
|
|
|
let measured = adc.read(&mut unsafe { embassy_stm32::Peripherals::steal() }.PA4);
|
|
|
|
// 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;
|
|
|
|
|
2023-10-03 22:22:16 +02:00
|
|
|
//info!("value / measured: {} / {}", dac_output_val, measured_normalized);
|
2023-07-27 19:04:43 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|