Compare commits
3 Commits
c52b21d951
...
0513506b98
| Author | SHA1 | Date | |
|---|---|---|---|
|
0513506b98
|
|||
|
df33b51d52
|
|||
|
6691c2e38c
|
59
Cargo.lock
generated
59
Cargo.lock
generated
@@ -3,10 +3,12 @@
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "as7265x-rust"
|
||||
name = "as7265x"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"embedded-hal",
|
||||
"embedded-hal-async",
|
||||
"maybe-async",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -14,3 +16,58 @@ name = "embedded-hal"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89"
|
||||
|
||||
[[package]]
|
||||
name = "embedded-hal-async"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c4c685bbef7fe13c3c6dd4da26841ed3980ef33e841cddfa15ce8a8fb3f1884"
|
||||
dependencies = [
|
||||
"embedded-hal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "maybe-async"
|
||||
version = "0.2.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5cf92c10c7e361d6b99666ec1c6f9805b0bea2c3bd8c78dc6fe98ac5bd78db11"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.103"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.111"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
[package]
|
||||
name = "as7265x-rust"
|
||||
name = "as7265x"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
embedded-hal = "1.0.0"
|
||||
maybe-async = { version = "0.2.10", features=["is_sync"]}
|
||||
|
||||
embedded-hal-async = { version = "1.0.0", optional = true }
|
||||
|
||||
[features]
|
||||
async = [ "embedded-hal-async" ]
|
||||
|
||||
|
||||
16
examples/esp32c6-async/.cargo/config.toml
Normal file
16
examples/esp32c6-async/.cargo/config.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[target.riscv32imac-unknown-none-elf]
|
||||
runner = "espflash flash --monitor --chip esp32c6"
|
||||
|
||||
[env]
|
||||
|
||||
[build]
|
||||
rustflags = [
|
||||
# Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.)
|
||||
# NOTE: May negatively impact performance of produced code
|
||||
"-C", "force-frame-pointers",
|
||||
]
|
||||
|
||||
target = "riscv32imac-unknown-none-elf"
|
||||
|
||||
[unstable]
|
||||
build-std = ["core"]
|
||||
22
examples/esp32c6-async/.gitignore
vendored
Normal file
22
examples/esp32c6-async/.gitignore
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Editor configuration
|
||||
.vscode/
|
||||
.zed/
|
||||
.helix/
|
||||
.nvim.lua
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
# RustRover
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
1286
examples/esp32c6-async/Cargo.lock
generated
Normal file
1286
examples/esp32c6-async/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
39
examples/esp32c6-async/Cargo.toml
Normal file
39
examples/esp32c6-async/Cargo.toml
Normal file
@@ -0,0 +1,39 @@
|
||||
[package]
|
||||
edition = "2024"
|
||||
name = "esp32c6-async"
|
||||
rust-version = "1.88"
|
||||
version = "0.1.0"
|
||||
|
||||
[[bin]]
|
||||
name = "esp32c6-async"
|
||||
path = "./src/bin/main.rs"
|
||||
|
||||
[dependencies]
|
||||
esp-hal = { version = "1.0.0", features = ["esp32c6", "unstable"] }
|
||||
|
||||
esp-rtos = { version = "0.2.0", features = ["embassy", "esp32c6"] }
|
||||
|
||||
esp-bootloader-esp-idf = { version = "0.4.0", features = ["esp32c6"] }
|
||||
|
||||
embassy-executor = { version = "0.9.1", features = [] }
|
||||
embassy-time = "0.5.0"
|
||||
|
||||
critical-section = "1.2.0"
|
||||
static_cell = "2.1.1"
|
||||
|
||||
as7265x = { path = "../..", features = ["async"] }
|
||||
esp-println = { version = "0.16.1", features = ["esp32c6"] }
|
||||
|
||||
[profile.dev]
|
||||
# Rust debug is too slow.
|
||||
# For debug builds always builds with some optimization
|
||||
opt-level = "s"
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1 # LLVM can perform better optimizations using a single thread
|
||||
debug = 2
|
||||
debug-assertions = false
|
||||
incremental = false
|
||||
lto = 'fat'
|
||||
opt-level = 's'
|
||||
overflow-checks = false
|
||||
56
examples/esp32c6-async/build.rs
Normal file
56
examples/esp32c6-async/build.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
fn main() {
|
||||
linker_be_nice();
|
||||
// make sure linkall.x is the last linker script (otherwise might cause problems with flip-link)
|
||||
println!("cargo:rustc-link-arg=-Tlinkall.x");
|
||||
}
|
||||
|
||||
fn linker_be_nice() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() > 1 {
|
||||
let kind = &args[1];
|
||||
let what = &args[2];
|
||||
|
||||
match kind.as_str() {
|
||||
"undefined-symbol" => match what.as_str() {
|
||||
"_defmt_timestamp" => {
|
||||
eprintln!();
|
||||
eprintln!(
|
||||
"💡 `defmt` not found - make sure `defmt.x` is added as a linker script and you have included `use defmt_rtt as _;`"
|
||||
);
|
||||
eprintln!();
|
||||
}
|
||||
"_stack_start" => {
|
||||
eprintln!();
|
||||
eprintln!("💡 Is the linker script `linkall.x` missing?");
|
||||
eprintln!();
|
||||
}
|
||||
"esp_rtos_initialized" | "esp_rtos_yield_task" | "esp_rtos_task_create" => {
|
||||
eprintln!();
|
||||
eprintln!(
|
||||
"💡 `esp-radio` has no scheduler enabled. Make sure you have initialized `esp-rtos` or provided an external scheduler."
|
||||
);
|
||||
eprintln!();
|
||||
}
|
||||
"embedded_test_linker_file_not_added_to_rustflags" => {
|
||||
eprintln!();
|
||||
eprintln!(
|
||||
"💡 `embedded-test` not found - make sure `embedded-test.x` is added as a linker script for tests"
|
||||
);
|
||||
eprintln!();
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
// we don't have anything helpful for "missing-lib" yet
|
||||
_ => {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
println!(
|
||||
"cargo:rustc-link-arg=--error-handling-script={}",
|
||||
std::env::current_exe().unwrap().display()
|
||||
);
|
||||
}
|
||||
4
examples/esp32c6-async/rust-toolchain.toml
Normal file
4
examples/esp32c6-async/rust-toolchain.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "stable"
|
||||
components = ["rust-src"]
|
||||
targets = ["riscv32imac-unknown-none-elf"]
|
||||
77
examples/esp32c6-async/src/bin/main.rs
Normal file
77
examples/esp32c6-async/src/bin/main.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![deny(
|
||||
clippy::mem_forget,
|
||||
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
|
||||
holding buffers for the duration of a data transfer."
|
||||
)]
|
||||
|
||||
use as7265x::{AS7265X, AS7265XConfig, Channel};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::{Delay, Duration, Timer};
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::i2c::master::I2c;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_println::println;
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
esp_bootloader_esp_idf::esp_app_desc!();
|
||||
|
||||
#[esp_rtos::main]
|
||||
async fn main(_spawner: Spawner) -> ! {
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
||||
let sw_interrupt =
|
||||
esp_hal::interrupt::software::SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
|
||||
esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0);
|
||||
|
||||
let i2c_config = esp_hal::i2c::master::Config::default();
|
||||
let device: I2c<'_, esp_hal::Async> =
|
||||
esp_hal::i2c::master::I2c::new(peripherals.I2C0, i2c_config)
|
||||
.unwrap()
|
||||
.with_sda(peripherals.GPIO22)
|
||||
.with_scl(peripherals.GPIO23)
|
||||
.into_async();
|
||||
|
||||
let mut as7265x_device = AS7265X::new(device, AS7265XConfig::default(), Delay);
|
||||
|
||||
as7265x_device.init().await.expect("Failed to init device");
|
||||
|
||||
loop {
|
||||
println!("Taking messurment...");
|
||||
|
||||
as7265x_device.mesure_with_bulb().await.unwrap();
|
||||
|
||||
println!(
|
||||
"Channel A: {}",
|
||||
as7265x_device
|
||||
.read_calibrated_messurement(Channel::A)
|
||||
.await
|
||||
.expect("Failed to read value")
|
||||
);
|
||||
|
||||
println!(
|
||||
"Channel B: {}",
|
||||
as7265x_device
|
||||
.read_calibrated_messurement(Channel::B)
|
||||
.await
|
||||
.expect("Failed to read value")
|
||||
);
|
||||
|
||||
println!(
|
||||
"Channel F: {}",
|
||||
as7265x_device
|
||||
.read_calibrated_messurement(Channel::F)
|
||||
.await
|
||||
.expect("Failed to read value")
|
||||
);
|
||||
|
||||
Timer::after(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
1
examples/esp32c6-async/src/lib.rs
Normal file
1
examples/esp32c6-async/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
#![no_std]
|
||||
16
examples/esp32c6-sync/.cargo/config.toml
Normal file
16
examples/esp32c6-sync/.cargo/config.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[target.riscv32imac-unknown-none-elf]
|
||||
runner = "espflash flash --monitor --chip esp32c6"
|
||||
|
||||
[env]
|
||||
|
||||
[build]
|
||||
rustflags = [
|
||||
# Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.)
|
||||
# NOTE: May negatively impact performance of produced code
|
||||
"-C", "force-frame-pointers",
|
||||
]
|
||||
|
||||
target = "riscv32imac-unknown-none-elf"
|
||||
|
||||
[unstable]
|
||||
build-std = ["core"]
|
||||
22
examples/esp32c6-sync/.gitignore
vendored
Normal file
22
examples/esp32c6-sync/.gitignore
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Editor configuration
|
||||
.vscode/
|
||||
.zed/
|
||||
.helix/
|
||||
.nvim.lua
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
# RustRover
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
1187
examples/esp32c6-sync/Cargo.lock
generated
Normal file
1187
examples/esp32c6-sync/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
34
examples/esp32c6-sync/Cargo.toml
Normal file
34
examples/esp32c6-sync/Cargo.toml
Normal file
@@ -0,0 +1,34 @@
|
||||
[package]
|
||||
edition = "2024"
|
||||
name = "esp32c6-sync"
|
||||
rust-version = "1.88"
|
||||
version = "0.1.0"
|
||||
|
||||
[[bin]]
|
||||
name = "esp32c6-sync"
|
||||
path = "./src/bin/main.rs"
|
||||
|
||||
[dependencies]
|
||||
esp-hal = { version = "1.0.0", features = ["esp32c6","unstable"] }
|
||||
|
||||
esp-bootloader-esp-idf = { version = "0.4.0", features = ["esp32c6"] }
|
||||
|
||||
critical-section = "1.2.0"
|
||||
|
||||
as7265x = { path="../.." }
|
||||
esp-println = { version = "0.16.1", features = ["esp32c6"] }
|
||||
|
||||
|
||||
[profile.dev]
|
||||
# Rust debug is too slow.
|
||||
# For debug builds always builds with some optimization
|
||||
opt-level = "s"
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1 # LLVM can perform better optimizations using a single thread
|
||||
debug = 2
|
||||
debug-assertions = false
|
||||
incremental = false
|
||||
lto = 'fat'
|
||||
opt-level = 's'
|
||||
overflow-checks = false
|
||||
56
examples/esp32c6-sync/build.rs
Normal file
56
examples/esp32c6-sync/build.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
fn main() {
|
||||
linker_be_nice();
|
||||
// make sure linkall.x is the last linker script (otherwise might cause problems with flip-link)
|
||||
println!("cargo:rustc-link-arg=-Tlinkall.x");
|
||||
}
|
||||
|
||||
fn linker_be_nice() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() > 1 {
|
||||
let kind = &args[1];
|
||||
let what = &args[2];
|
||||
|
||||
match kind.as_str() {
|
||||
"undefined-symbol" => match what.as_str() {
|
||||
"_defmt_timestamp" => {
|
||||
eprintln!();
|
||||
eprintln!(
|
||||
"💡 `defmt` not found - make sure `defmt.x` is added as a linker script and you have included `use defmt_rtt as _;`"
|
||||
);
|
||||
eprintln!();
|
||||
}
|
||||
"_stack_start" => {
|
||||
eprintln!();
|
||||
eprintln!("💡 Is the linker script `linkall.x` missing?");
|
||||
eprintln!();
|
||||
}
|
||||
"esp_rtos_initialized" | "esp_rtos_yield_task" | "esp_rtos_task_create" => {
|
||||
eprintln!();
|
||||
eprintln!(
|
||||
"💡 `esp-radio` has no scheduler enabled. Make sure you have initialized `esp-rtos` or provided an external scheduler."
|
||||
);
|
||||
eprintln!();
|
||||
}
|
||||
"embedded_test_linker_file_not_added_to_rustflags" => {
|
||||
eprintln!();
|
||||
eprintln!(
|
||||
"💡 `embedded-test` not found - make sure `embedded-test.x` is added as a linker script for tests"
|
||||
);
|
||||
eprintln!();
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
// we don't have anything helpful for "missing-lib" yet
|
||||
_ => {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
println!(
|
||||
"cargo:rustc-link-arg=--error-handling-script={}",
|
||||
std::env::current_exe().unwrap().display()
|
||||
);
|
||||
}
|
||||
4
examples/esp32c6-sync/rust-toolchain.toml
Normal file
4
examples/esp32c6-sync/rust-toolchain.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "stable"
|
||||
components = ["rust-src"]
|
||||
targets = ["riscv32imac-unknown-none-elf"]
|
||||
67
examples/esp32c6-sync/src/bin/main.rs
Normal file
67
examples/esp32c6-sync/src/bin/main.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![deny(
|
||||
clippy::mem_forget,
|
||||
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
|
||||
holding buffers for the duration of a data transfer."
|
||||
)]
|
||||
|
||||
use esp_println::println;
|
||||
use as7265x::{AS7265XConfig, Channel, AS7265X};
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::{i2c, main};
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
esp_bootloader_esp_idf::esp_app_desc!();
|
||||
|
||||
#[main]
|
||||
fn main() -> ! {
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
let device = esp_hal::i2c::master::I2c::new(peripherals.I2C0, i2c::master::Config::default())
|
||||
.unwrap()
|
||||
.with_sda(peripherals.GPIO22)
|
||||
.with_scl(peripherals.GPIO23);
|
||||
|
||||
let delay = esp_hal::delay::Delay::new();
|
||||
|
||||
let mut as7265x_device = AS7265X::new(device, AS7265XConfig::default(), delay);
|
||||
|
||||
as7265x_device.init().expect("Failed to init");
|
||||
|
||||
loop {
|
||||
println!("Taking messurment...");
|
||||
|
||||
as7265x_device
|
||||
.mesure_with_bulb()
|
||||
.expect("Failed to run messurment");
|
||||
|
||||
println!(
|
||||
"Channel A: {}",
|
||||
as7265x_device
|
||||
.read_calibrated_messurement(Channel::A)
|
||||
.expect("Failed to read value")
|
||||
);
|
||||
|
||||
println!(
|
||||
"Channel B: {}",
|
||||
as7265x_device
|
||||
.read_calibrated_messurement(Channel::B)
|
||||
.expect("Failed to read value")
|
||||
);
|
||||
|
||||
println!(
|
||||
"Channel F: {}",
|
||||
as7265x_device
|
||||
.read_calibrated_messurement(Channel::F)
|
||||
.expect("Failed to read value")
|
||||
);
|
||||
|
||||
delay.delay_millis(1000);
|
||||
}
|
||||
}
|
||||
1
examples/esp32c6-sync/src/lib.rs
Normal file
1
examples/esp32c6-sync/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
#![no_std]
|
||||
69
src/lib.rs
69
src/lib.rs
@@ -1,10 +1,18 @@
|
||||
#![no_std]
|
||||
|
||||
#[cfg(not(feature = "async"))]
|
||||
use embedded_hal::{
|
||||
delay::DelayNs,
|
||||
i2c::{self},
|
||||
};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use embedded_hal_async::{
|
||||
delay::DelayNs,
|
||||
i2c::{self},
|
||||
};
|
||||
use maybe_async::maybe_async;
|
||||
|
||||
const AS7265X_ADDR: u8 = 0x49;
|
||||
const STATUS_REG: u8 = 0x00;
|
||||
const WRITE_REG: u8 = 0x01;
|
||||
@@ -80,7 +88,7 @@ pub enum Channel {
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
pub fn get_device_for_chanel(&self) -> Device {
|
||||
pub fn get_device_for_channel(&self) -> Device {
|
||||
match self {
|
||||
Channel::A | Channel::B | Channel::C | Channel::D | Channel::E | Channel::F => {
|
||||
Device::Uv
|
||||
@@ -330,6 +338,7 @@ where
|
||||
config: AS7265XConfig,
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
impl<I, D> AS7265X<I, D>
|
||||
where
|
||||
I: i2c::I2c,
|
||||
@@ -343,6 +352,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn init(&mut self) -> Result<(), Error<I::Error>> {
|
||||
self.write_config().await?;
|
||||
|
||||
@@ -376,63 +386,75 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn soft_reset(&mut self) -> Result<(), Error<I::Error>> {
|
||||
self.write_virtual_register(CONFIG_ADDR, SOFT_RESET).await?;
|
||||
self.delay.delay_ms(100);
|
||||
self.delay.delay_ms(100).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
async fn write_config(&mut self) -> Result<(), Error<I::Error>> {
|
||||
let config_byte = self.config.to_config_byte();
|
||||
self.write_virtual_register(CONFIG_ADDR, config_byte).await
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn set_integration_time(&mut self, time: u8) -> Result<(), Error<I::Error>> {
|
||||
self.write_virtual_register(INTEGRATION_ADDR, time).await
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
async fn set_led_config(&mut self, led: Led, config: LedConfig) -> Result<(), Error<I::Error>> {
|
||||
self.select_device(led.get_device()).await?;
|
||||
let config_byte = config.config_byte();
|
||||
self.write_virtual_register(LED_ADDR, config_byte).await
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn get_device_type(&mut self) -> Result<u8, Error<I::Error>> {
|
||||
self.read_virtual_register(HW_VERSION_HIGH_ADDR).await
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn get_hardware_version(&mut self) -> Result<u8, Error<I::Error>> {
|
||||
self.read_virtual_register(HW_VERSION_LOW_ADDR).await
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn get_firmware_version(&mut self) -> Result<(u8, u8), Error<I::Error>> {
|
||||
let high = self.read_virtual_register(FW_VERSION_HIGH_ADDR).await?;
|
||||
let low = self.read_virtual_register(FW_VERSION_LOW_ADDR).await?;
|
||||
Ok((high, low))
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
async fn select_device(&mut self, device: Device) -> Result<(), Error<I::Error>> {
|
||||
self.write_virtual_register(DEV_SELECT_ADDR, device as u8)
|
||||
.await
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn read_temperature(&mut self) -> Result<i8, Error<I::Error>> {
|
||||
let temp = self.read_virtual_register(TEMP_ADDR).await?;
|
||||
Ok(temp as i8)
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn is_data_ready(&mut self) -> Result<bool, Error<I::Error>> {
|
||||
let config = self.read_virtual_register(CONFIG_ADDR).await?;
|
||||
Ok((config & CONFIG_DATA_RDY) != 0)
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn measure(&mut self) -> Result<(), Error<I::Error>> {
|
||||
self.write_config().await?;
|
||||
let wait_time = (self.config.integration_time as u32) * 28 / 10 * 2;
|
||||
self.delay.delay_ms(wait_time);
|
||||
self.delay.delay_ms(wait_time).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn mesure_with_bulb(&mut self) -> Result<(), Error<I::Error>> {
|
||||
self.set_led_config(
|
||||
Led::White,
|
||||
@@ -490,23 +512,26 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn read_raw_measurement(&mut self, channel: Channel) -> Result<u16, Error<I::Error>> {
|
||||
let dev = channel.get_device_for_chanel();
|
||||
let dev = channel.get_device_for_channel();
|
||||
self.select_device(dev).await?;
|
||||
self.read_raw_channel(channel.get_raw_address()).await
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
pub async fn read_calibrated_messurement(
|
||||
&mut self,
|
||||
channel: Channel,
|
||||
) -> Result<f32, Error<I::Error>> {
|
||||
let dev = channel.get_device_for_chanel();
|
||||
let dev = channel.get_device_for_channel();
|
||||
self.select_device(dev).await?;
|
||||
|
||||
self.read_calibrated_channel(channel.get_cal_address())
|
||||
.await
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
async fn write_virtual_register(
|
||||
&mut self,
|
||||
virtual_addr: u8,
|
||||
@@ -517,64 +542,73 @@ where
|
||||
for _ in 0..MAX_RETRIES {
|
||||
let mut status = [0u8; 1];
|
||||
self.device
|
||||
.write_read(AS7265X_ADDR, &[STATUS_REG], &mut status)?;
|
||||
.write_read(AS7265X_ADDR, &[STATUS_REG], &mut status)
|
||||
.await?;
|
||||
if (status[0] & TX_VALID) == 0 {
|
||||
break;
|
||||
}
|
||||
self.delay.delay_ms(5);
|
||||
self.delay.delay_ms(5).await;
|
||||
}
|
||||
|
||||
self.device
|
||||
.write(AS7265X_ADDR, &[WRITE_REG, 0x80 | virtual_addr])?;
|
||||
.write(AS7265X_ADDR, &[WRITE_REG, 0x80 | virtual_addr])
|
||||
.await?;
|
||||
|
||||
for _ in 0..MAX_RETRIES {
|
||||
let mut status = [0u8; 1];
|
||||
self.device
|
||||
.write_read(AS7265X_ADDR, &[STATUS_REG], &mut status)?;
|
||||
.write_read(AS7265X_ADDR, &[STATUS_REG], &mut status)
|
||||
.await?;
|
||||
if (status[0] & TX_VALID) == 0 {
|
||||
break;
|
||||
}
|
||||
self.delay.delay_ms(5);
|
||||
self.delay.delay_ms(5).await;
|
||||
}
|
||||
|
||||
self.device.write(AS7265X_ADDR, &[WRITE_REG, data])?;
|
||||
self.device.write(AS7265X_ADDR, &[WRITE_REG, data]).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
async fn read_virtual_register(&mut self, virtual_addr: u8) -> Result<u8, Error<I::Error>> {
|
||||
const MAX_RETRIES: u8 = 100;
|
||||
|
||||
for _ in 0..MAX_RETRIES {
|
||||
let mut status = [0u8; 1];
|
||||
self.device
|
||||
.write_read(AS7265X_ADDR, &[STATUS_REG], &mut status)?;
|
||||
.write_read(AS7265X_ADDR, &[STATUS_REG], &mut status)
|
||||
.await?;
|
||||
if (status[0] & TX_VALID) == 0 {
|
||||
break;
|
||||
}
|
||||
self.delay.delay_ms(5);
|
||||
self.delay.delay_ms(5).await;
|
||||
}
|
||||
|
||||
self.device
|
||||
.write(AS7265X_ADDR, &[WRITE_REG, virtual_addr])?;
|
||||
.write(AS7265X_ADDR, &[WRITE_REG, virtual_addr])
|
||||
.await?;
|
||||
|
||||
for _ in 0..MAX_RETRIES {
|
||||
let mut status = [0u8; 1];
|
||||
self.device
|
||||
.write_read(AS7265X_ADDR, &[STATUS_REG], &mut status)?;
|
||||
.write_read(AS7265X_ADDR, &[STATUS_REG], &mut status)
|
||||
.await?;
|
||||
if (status[0] & RX_VALID) != 0 {
|
||||
break;
|
||||
}
|
||||
self.delay.delay_ms(5);
|
||||
self.delay.delay_ms(5).await;
|
||||
}
|
||||
|
||||
let mut data = [0u8; 1];
|
||||
self.device
|
||||
.write_read(AS7265X_ADDR, &[READ_REG], &mut data)?;
|
||||
.write_read(AS7265X_ADDR, &[READ_REG], &mut data)
|
||||
.await?;
|
||||
|
||||
Ok(data[0])
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
async fn read_raw_channel(&mut self, high_byte_addr: u8) -> Result<u16, Error<I::Error>> {
|
||||
let high = self.read_virtual_register(high_byte_addr).await?;
|
||||
let low = self.read_virtual_register(high_byte_addr + 1).await?;
|
||||
@@ -582,6 +616,7 @@ where
|
||||
Ok(((high as u16) << 8) | (low as u16))
|
||||
}
|
||||
|
||||
#[maybe_async]
|
||||
async fn read_calibrated_channel(
|
||||
&mut self,
|
||||
high_byte_addr: u8,
|
||||
|
||||
Reference in New Issue
Block a user