Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest embedded-hal, include std example #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 77 additions & 65 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,26 @@ keywords = ["scale", "hx711", "loadcell", "no-std", "driver"]
categories = ["no-std", "embedded", "hardware-support", "science::robotics"]

[dependencies]
esp32-hal = { version = "0.18.0", optional = true, features = ["eh1"] }
critical-section = "1.1.2"
embedded-hal = { version = "0.2.7", features = ["unproven"] }
esp32-hal = { version = "0.17.0", optional = true }
embedded-hal = { version = "1.0.0" }

[features]
default = []
esp32_interrupt = ["esp32-hal"]


[[example]]
name = "interrupt"
required-features = ["esp32_interrupt"]

[[example]]
name = "polling"
required-features = ["esp32_interrupt"]

[dev-dependencies]
esp-backtrace = { version = "0.8.0", features = [
esp-backtrace = { version = "0.11.0", features = [
"esp32",
"panic-handler",
"exception-handler",
"print-uart",
"println",
] }
esp-println = { version = "0.6.0", features = ["esp32", "log"] }
esp-println = { version = "0.9.0", features = ["esp32", "log"] }
log = { version = "0.4.18" }
critical-section = "1.1.2"
2 changes: 1 addition & 1 deletion examples/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static HX711_MUTEX: Mutex<RefCell<Option<HX711<SckPin, DTPin, Delay>>>> =
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.DPORT.split();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let mut delay = Delay::new(&clocks);

Expand Down
2 changes: 1 addition & 1 deletion examples/polling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use loadcell::{hx711, LoadCell};
#[entry]
fn main() -> ! {
let periph = Peripherals::take();
let system = periph.DPORT.split();
let system = periph.SYSTEM.split();

let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

Expand Down
25 changes: 25 additions & 0 deletions examples/polling_idf.rs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The critical section implementation using IsrCriticalSection should be added here

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that implementation may lock this crate into the idf use case, where I would like to keep it no-std. Are you referring to this? https://github.com/beeb/coffee-scale-app/blob/main/rs/src/critical_section.rs. We should look into some way to support both.

Copy link
Collaborator

@beeb beeb Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly, the critical section above is required for the IDF example. It's not required for no-std, as the esp32-rs crates already provide the required critical section implementation. But the provided one for idf is not sufficient to ensure correct reading of the hx711, because it is thread-local only as I understand.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should upstream the change into esp-idf-hal?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.rs/critical-section/latest/critical_section/#usage-in-libraries says not to provide this in libraries, I think we should follow their advice.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course this only applies to the example, we should not implement it for the library. I'm talking about example code specifically, since consumers of the library will need to implement the critical section when using idf

Copy link
Collaborator

@beeb beeb Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DaneSlattery I commented on your PR over at esp-idf-hal

Copy link
Owner Author

@DaneSlattery DaneSlattery Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to think the one provided by default is actually safe across threads and interrupts, see here, they call interrupt::free, which runs the critical section with interrupts disabled. However, whether it is safe across multiple threads or CPUs is another question, and I don't know enough about the details.

https://github.com/esp-rs/esp-idf-hal/blob/e30d7ab708065d48f8a96e32993bde3ff3946370/src/task.rs#L448

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it but had bad readings, the scheduler is free to launch tasks with high priority during the critical section as far as I understood. I discussed this at length on matrix with the team.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// currently does not build in in this no-std environment. copy the code elsewhere
use esp_idf_svc::hal::{delay, gpio::PinDriver, peripherals::Peripherals};
use loadcell::{hx711::HX711, LoadCell};

fn main() {
esp_idf_svc::sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();

let peripherals = Peripherals::take().unwrap();
let dt = PinDriver::input(peripherals.pins.gpio1).unwrap();
let sck = PinDriver::output(peripherals.pins.gpio10).unwrap();
let mut load_sensor = HX711::new(sck, dt, delay::FreeRtos);

load_sensor.tare(16);
load_sensor.set_scale(1.0);

loop {
if load_sensor.is_ready() {
let reading = load_sensor.read_scaled();
log::info!("Last Reading = {:?}", reading);
}

delay::FreeRtos::delay_ms(1000u32);
}
}
13 changes: 6 additions & 7 deletions src/hx711.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ pub mod interrupt;
#[cfg(feature = "esp32_interrupt")]
pub use interrupt::*;

use embedded_hal::blocking::delay::DelayUs;

use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::delay::DelayNs;
use embedded_hal::digital::{InputPin, OutputPin};

#[cfg(feature = "default")]
use crate::LoadCell;
Expand Down Expand Up @@ -46,7 +45,7 @@ pub struct HX711<SckPin, DTPin, Delay>
where
SckPin: OutputPin,
DTPin: InputPin,
Delay: DelayUs<u32>,
Delay: DelayNs,
{
sck_pin: SckPin,
dt_pin: DTPin,
Expand All @@ -72,7 +71,7 @@ impl<SckPin, DTPin, Delay, ESCK, EDT> HX711<SckPin, DTPin, Delay>
where
SckPin: OutputPin<Error = ESCK>,
DTPin: InputPin<Error = EDT>,
Delay: DelayUs<u32>,
Delay: DelayNs,
EDT: fmt::Debug,
ESCK: fmt::Debug,
{
Expand All @@ -91,7 +90,7 @@ where
}

/// Returns true if the load cell amplifier has a value ready to be read.
pub fn is_ready(&self) -> bool {
pub fn is_ready(&mut self) -> bool {
self.dt_pin.is_low().unwrap()
}

Expand Down Expand Up @@ -178,7 +177,7 @@ impl<SckPin, DTPin, Delay, ESCK, EDT> LoadCell for HX711<SckPin, DTPin, Delay>
where
SckPin: OutputPin<Error = ESCK>,
DTPin: InputPin<Error = EDT>,
Delay: DelayUs<u32>,
Delay: DelayNs,
ESCK: fmt::Debug,
EDT: fmt::Debug,
{
Expand Down
6 changes: 3 additions & 3 deletions src/hx711/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub trait Interrupt: LoadCell {
#[cfg(feature = "esp32_interrupt")]
impl<SckPin, DTPin, Delay> Interrupt for HX711<SckPin, DTPin, Delay>
where
SckPin: OutputPin + embedded_hal::digital::v2::OutputPin<Error = Infallible>,
DTPin: InputPin + embedded_hal::digital::v2::InputPin<Error = Infallible>,
Delay: embedded_hal::blocking::delay::DelayUs<u32>,
SckPin: OutputPin + embedded_hal::digital::OutputPin<Error = Infallible>,
DTPin: InputPin + embedded_hal::digital::InputPin<Error = Infallible>,
Delay: embedded_hal::delay::DelayNs,
{
fn tare_sync(&mut self, num_samples: usize) {
let mut was_listening = false;
Expand Down