tests/stm32: add L1 DAC/ADC test.

This commit is contained in:
Dario Nieuwenhuis 2023-12-08 20:24:15 +01:00
parent c27459c052
commit fe218ed978
4 changed files with 105 additions and 12 deletions

View File

@ -10,14 +10,14 @@ stm32c031c6 = ["embassy-stm32/stm32c031c6", "cm0", "not-gpdma"]
stm32f103c8 = ["embassy-stm32/stm32f103c8", "not-gpdma"]
stm32f207zg = ["embassy-stm32/stm32f207zg", "chrono", "not-gpdma", "eth", "rng"]
stm32f303ze = ["embassy-stm32/stm32f303ze", "chrono", "not-gpdma"]
stm32f429zi = ["embassy-stm32/stm32f429zi", "chrono", "eth", "stop", "can", "not-gpdma", "dac-adc-pin", "rng"]
stm32f446re = ["embassy-stm32/stm32f446re", "chrono", "stop", "can", "not-gpdma", "dac-adc-pin", "sdmmc"]
stm32f429zi = ["embassy-stm32/stm32f429zi", "chrono", "eth", "stop", "can", "not-gpdma", "dac", "rng"]
stm32f446re = ["embassy-stm32/stm32f446re", "chrono", "stop", "can", "not-gpdma", "dac", "sdmmc"]
stm32f767zi = ["embassy-stm32/stm32f767zi", "chrono", "not-gpdma", "eth", "rng"]
stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac-adc-pin"]
stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac"]
stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "stop", "not-gpdma", "rng"]
stm32h563zi = ["embassy-stm32/stm32h563zi", "chrono", "eth", "rng"]
stm32h753zi = ["embassy-stm32/stm32h753zi", "chrono", "not-gpdma", "eth", "rng"]
stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac-adc-pin", "rng"]
stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac", "rng"]
stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "not-gpdma", "rng"]
stm32l073rz = ["embassy-stm32/stm32l073rz", "cm0", "not-gpdma", "rng"]
stm32l152re = ["embassy-stm32/stm32l152re", "chrono", "not-gpdma"]
@ -41,7 +41,7 @@ ble = ["dep:embassy-stm32-wpan", "embassy-stm32-wpan/ble"]
mac = ["dep:embassy-stm32-wpan", "embassy-stm32-wpan/mac"]
embassy-stm32-wpan = []
not-gpdma = []
dac-adc-pin = []
dac = []
cm0 = ["portable-atomic/unsafe-assume-single-core"]
@ -84,7 +84,12 @@ required-features = [ "can",]
[[bin]]
name = "dac"
path = "src/bin/dac.rs"
required-features = [ "dac-adc-pin",]
required-features = [ "dac",]
[[bin]]
name = "dac_l1"
path = "src/bin/dac_l1.rs"
required-features = [ "stm32l152re",]
[[bin]]
name = "eth"

View File

@ -1,7 +1,7 @@
#![no_std]
#![no_main]
// required-features: dac-adc-pin
// required-features: dac
#[path = "../common.rs"]
mod common;
@ -22,12 +22,13 @@ async fn main(_spawner: Spawner) {
// Initialize the board and obtain a Peripherals instance
let p: embassy_stm32::Peripherals = embassy_stm32::init(config());
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(dac, NoDma, dac_pin);
let mut adc = Adc::new(p.ADC1, &mut Delay);
let mut adc = Adc::new(adc, &mut Delay);
#[cfg(feature = "stm32h755zi")]
let normalization_factor = 256;

View File

@ -0,0 +1,86 @@
#![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;
use embassy_stm32::dac::{DacCh1, Value};
use embassy_stm32::dma::NoDma;
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::ADC>;
});
#[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());
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(dac, NoDma, 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).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).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
}
}

View File

@ -101,14 +101,14 @@ define_peris!(
define_peris!(
UART = USART1, UART_TX = PC4, UART_RX = PC5, UART_TX_DMA = DMA1_CH1, UART_RX_DMA = DMA1_CH2,
SPI = SPI1, SPI_SCK = PA5, SPI_MOSI = PA7, SPI_MISO = PA6, SPI_TX_DMA = DMA1_CH1, SPI_RX_DMA = DMA1_CH2,
DAC = DAC1, DAC_PIN = PA4,
ADC = ADC1, DAC = DAC1, DAC_PIN = PA4,
@irq UART = {USART1 => embassy_stm32::usart::InterruptHandler<embassy_stm32::peripherals::USART1>;},
);
#[cfg(feature = "stm32f429zi")]
define_peris!(
UART = USART6, UART_TX = PG14, UART_RX = PG9, UART_TX_DMA = DMA2_CH6, UART_RX_DMA = DMA2_CH1,
SPI = SPI1, SPI_SCK = PA5, SPI_MOSI = PA7, SPI_MISO = PA6, SPI_TX_DMA = DMA2_CH3, SPI_RX_DMA = DMA2_CH2,
DAC = DAC, DAC_PIN = PA4,
ADC = ADC1, DAC = DAC, DAC_PIN = PA4,
CAN = CAN1, CAN_RX = PD0, CAN_TX = PD1,
@irq UART = {USART6 => embassy_stm32::usart::InterruptHandler<embassy_stm32::peripherals::USART6>;},
);
@ -116,7 +116,7 @@ define_peris!(
define_peris!(
UART = USART1, UART_TX = PA9, UART_RX = PA10, UART_TX_DMA = DMA2_CH7, UART_RX_DMA = DMA2_CH5,
SPI = SPI1, SPI_SCK = PA5, SPI_MOSI = PA7, SPI_MISO = PA6, SPI_TX_DMA = DMA2_CH3, SPI_RX_DMA = DMA2_CH2,
DAC = DAC, DAC_PIN = PA4,
ADC = ADC1, DAC = DAC, DAC_PIN = PA4,
CAN = CAN1, CAN_RX = PA11, CAN_TX = PA12,
@irq UART = {USART1 => embassy_stm32::usart::InterruptHandler<embassy_stm32::peripherals::USART1>;},
);
@ -130,7 +130,7 @@ define_peris!(
define_peris!(
UART = USART1, UART_TX = PB6, UART_RX = PB7, UART_TX_DMA = DMA1_CH0, UART_RX_DMA = DMA1_CH1,
SPI = SPI1, SPI_SCK = PA5, SPI_MOSI = PB5, SPI_MISO = PA6, SPI_TX_DMA = DMA1_CH0, SPI_RX_DMA = DMA1_CH1,
DAC = DAC1, DAC_PIN = PA4,
ADC = ADC1, DAC = DAC1, DAC_PIN = PA4,
@irq UART = {USART1 => embassy_stm32::usart::InterruptHandler<embassy_stm32::peripherals::USART1>;},
);
#[cfg(feature = "stm32h7a3zi")]
@ -191,6 +191,7 @@ define_peris!(
define_peris!(
UART = USART3, UART_TX = PB10, UART_RX = PB11, UART_TX_DMA = DMA1_CH2, UART_RX_DMA = DMA1_CH3,
SPI = SPI1, SPI_SCK = PA5, SPI_MOSI = PA7, SPI_MISO = PA6, SPI_TX_DMA = DMA1_CH3, SPI_RX_DMA = DMA1_CH2,
ADC = ADC, DAC = DAC, DAC_PIN = PA4,
@irq UART = {USART3 => embassy_stm32::usart::InterruptHandler<embassy_stm32::peripherals::USART3>;},
);
#[cfg(feature = "stm32l552ze")]