examples builded
This commit is contained in:
93
hal_adc/src/bin/main.rs
Normal file
93
hal_adc/src/bin/main.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
// src/bin/main.rs
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_stm32::adc;
|
||||
use embassy_stm32::adc::{AdcChannel, adc4};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: embassy_executor::Spawner) {
|
||||
let config = embassy_stm32::Config::default();
|
||||
|
||||
let mut p = embassy_stm32::init(config);
|
||||
|
||||
// **** ADC1 init ****
|
||||
let mut adc1 = adc::Adc::new(p.ADC1);
|
||||
let mut adc1_pin1 = p.PA3; // A0 on nucleo u5a5
|
||||
let mut adc1_pin2 = p.PA2; // A1
|
||||
adc1.set_resolution(adc::Resolution::BITS14);
|
||||
adc1.set_averaging(adc::Averaging::Samples1024);
|
||||
adc1.set_sample_time(adc::SampleTime::CYCLES160_5);
|
||||
let max1 = adc::resolution_to_max_count(adc::Resolution::BITS14);
|
||||
|
||||
// **** ADC4 init ****
|
||||
let mut adc4 = adc4::Adc4::new(p.ADC4);
|
||||
let mut adc4_pin1 = p.PC1; // A4
|
||||
let mut adc4_pin2 = p.PC0; // A5
|
||||
adc4.set_resolution(adc4::Resolution::BITS12);
|
||||
adc4.set_averaging(adc4::Averaging::Samples256);
|
||||
adc4.set_sample_time(adc4::SampleTime::CYCLES1_5);
|
||||
let max4 = adc4::resolution_to_max_count(adc4::Resolution::BITS12);
|
||||
|
||||
// **** ADC1 blocking read ****
|
||||
let raw: u16 = adc1.blocking_read(&mut adc1_pin1);
|
||||
let volt: f32 = 3.3 * raw as f32 / max1 as f32;
|
||||
info!("Read adc1 pin 1 {}", volt);
|
||||
|
||||
let raw: u16 = adc1.blocking_read(&mut adc1_pin2);
|
||||
let volt: f32 = 3.3 * raw as f32 / max1 as f32;
|
||||
info!("Read adc1 pin 2 {}", volt);
|
||||
|
||||
// **** ADC4 blocking read ****
|
||||
let raw: u16 = adc4.blocking_read(&mut adc4_pin1);
|
||||
let volt: f32 = 3.3 * raw as f32 / max4 as f32;
|
||||
info!("Read adc4 pin 1 {}", volt);
|
||||
|
||||
let raw: u16 = adc4.blocking_read(&mut adc4_pin2);
|
||||
let volt: f32 = 3.3 * raw as f32 / max4 as f32;
|
||||
info!("Read adc4 pin 2 {}", volt);
|
||||
|
||||
// **** ADC1 async read ****
|
||||
let mut degraded11 = adc1_pin1.degrade_adc();
|
||||
let mut degraded12 = adc1_pin2.degrade_adc();
|
||||
let mut measurements = [0u16; 2];
|
||||
|
||||
adc1.read(
|
||||
p.GPDMA1_CH0.reborrow(),
|
||||
[
|
||||
(&mut degraded11, adc::SampleTime::CYCLES160_5),
|
||||
(&mut degraded12, adc::SampleTime::CYCLES160_5),
|
||||
]
|
||||
.into_iter(),
|
||||
&mut measurements,
|
||||
)
|
||||
.await;
|
||||
let volt1: f32 = 3.3 * measurements[0] as f32 / max1 as f32;
|
||||
let volt2: f32 = 3.3 * measurements[1] as f32 / max1 as f32;
|
||||
|
||||
info!("Async read 1 pin 1 {}", volt1);
|
||||
info!("Async read 1 pin 2 {}", volt2);
|
||||
|
||||
// **** ADC2 does not support async read ****
|
||||
|
||||
// **** ADC4 async read ****
|
||||
let mut degraded41 = adc4_pin1.degrade_adc();
|
||||
let mut degraded42 = adc4_pin2.degrade_adc();
|
||||
let mut measurements = [0u16; 2];
|
||||
|
||||
// The channels must be in ascending order and can't repeat for ADC4
|
||||
adc4.read(
|
||||
p.GPDMA1_CH1.reborrow(),
|
||||
[&mut degraded42, &mut degraded41].into_iter(),
|
||||
&mut measurements,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let volt2: f32 = 3.3 * measurements[0] as f32 / max4 as f32;
|
||||
let volt1: f32 = 3.3 * measurements[1] as f32 / max4 as f32;
|
||||
info!("Async read 4 pin 1 {}", volt1);
|
||||
info!("Async read 4 pin 2 {}", volt2);
|
||||
}
|
||||
46
hal_adc/src/lib.rs
Normal file
46
hal_adc/src/lib.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use defmt_rtt as _; // global logger
|
||||
|
||||
// TODO(5) adjust HAL import
|
||||
// use some_hal as _; // memory layout
|
||||
|
||||
use panic_probe as _;
|
||||
|
||||
// same panicking *behavior* as `panic-probe` but doesn't print a panic message
|
||||
// this prevents the panic message being printed *twice* when `defmt::panic` is invoked
|
||||
#[defmt::panic_handler]
|
||||
fn panic() -> ! {
|
||||
cortex_m::asm::udf()
|
||||
}
|
||||
|
||||
/// Terminates the application and makes a semihosting-capable debug tool exit
|
||||
/// with status code 0.
|
||||
pub fn exit() -> ! {
|
||||
semihosting::process::exit(0);
|
||||
}
|
||||
|
||||
/// Hardfault handler.
|
||||
///
|
||||
/// Terminates the application and makes a semihosting-capable debug tool exit
|
||||
/// with an error. This seems better than the default, which is to spin in a
|
||||
/// loop.
|
||||
#[cortex_m_rt::exception]
|
||||
unsafe fn HardFault(_frame: &cortex_m_rt::ExceptionFrame) -> ! {
|
||||
semihosting::process::exit(1);
|
||||
}
|
||||
|
||||
// defmt-test 0.3.0 has the limitation that this `#[tests]` attribute can only be used
|
||||
// once within a crate. the module can be in any file but there can only be at most
|
||||
// one `#[tests]` module in this library crate
|
||||
#[cfg(test)]
|
||||
#[defmt_test::tests]
|
||||
mod unit_tests {
|
||||
use defmt::assert;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
assert!(true)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user