esp-rs / esp-idf-hal

embedded-hal implementation for Rust on ESP32 and ESP-IDF
https://docs.esp-rs.org/esp-idf-hal/
Apache License 2.0
421 stars 170 forks source link

Motor Control Pulse Width Modulator (MCPWM) #92

Open usbalbin opened 2 years ago

usbalbin commented 2 years ago

Hi!

Is there any way to access the MCPWM peripheral found in ESP32 and ESP32-S3 through this crate?

usbalbin commented 2 years ago

I found the esp32 crate which seems to have this. However I am assuming that should not be used at the same time as this crate due to the crates having their own individual locks?

MabezDev commented 2 years ago

Correct, this crate utilizes esp-idf functions and creates safe Rust wrappers around them. If you would like to try, you can create a wrapper around the MCPWM api and submit a PR (happy to review & mentor if you need some help). If not, someone will implement this eventually :).

usbalbin commented 2 years ago

Maybe I will give it a try if I get time. Thanks! :)

usbalbin commented 2 years ago

Do you think looking at something like ledc.rs and its correlation to the idf would be a good starting point for how to design the api?

MabezDev commented 2 years ago

Do you think looking at something like ledc.rs and its correlation to the idf would be a good starting point for how to design the api?

Yes! I would definitely spend some time looking at LEDC, and some other peripherals just to get a feel of how to use the esp-idf functions within Rust, then you can start to think about how to safely wrap the MCPWM API in Rust :).

Feel free to join the matrix chat if you want: https://matrix.to/#/#esp-rs:matrix.org

usbalbin commented 2 years ago

Am I missing something or are there no mcpwm related functions such as mcpwm_init in esp_idf_sys?

MabezDev commented 2 years ago

So we don't produce bindings for everything in esp-idf, we have a whitelist in this file, inside esp-idf-sys. What you'll need to do is modify esp-idf-sys to include the mcpwm headers.

// binding.h
#include "driver/mcpwm.h"

Hopefully, that should be fairly simple! Let me know if you run into any issues or need some help :).

usbalbin commented 2 years ago

Thanks a lot. I will try to take a look :)

usbalbin commented 2 years ago

As far as I can see #include "driver/mcpwm.h" is already present in that file (just below the inclusion of driver/ledc.h).

When searching for ledc in the repo I can not find any more places than the file mentioned above (exept for what I believe is the corresponding file for the esp8266) where ledc is mentioned.

MabezDev commented 2 years ago

Did you actually try to use the MCPWM functions in the HAL? I think the docs are built for the esp32c3, which doesn't have the MCPWM peripheral so it won't show up there.

usbalbin commented 2 years ago

Oh, just checked locally and yes esp_idf_sys::mcpwm_init and friends are there.

usbalbin commented 2 years ago

@MabezDev do you think an API that would allow you to write something roughly like this would make sense?

let peripherals = Peripherals::take().unwrap();
let config = OperatorConfig::default().frequency(25.kHz().into());
let mcpwm = Mcpwm::new(peripherals.mcpwm0.mcpwm)?;
let mut operator = Operator::new(
    peripherals.mcpwm0.operator0,
    &mcpwm,
    &config,
    peripherals.pins.gpio4,
    peripherals.pins.gpio5,
)?;

operator.set_duty_a(my_duty_percentage_a)?;
operator.set_duty_b(my_duty_percentage_b)?;

see #93, or do you have any other suggestions :)

ilikepi63 commented 1 year ago

I ran into a similar problem and decided to write my own library over esp-idf-sys.

You can find it here: https://crates.io/crates/esp-idf-servo