-
Notifications
You must be signed in to change notification settings - Fork 173
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
MCPWM 5.0 #132
Open
usbalbin
wants to merge
62
commits into
esp-rs:master
Choose a base branch
from
usbalbin:mcpwm_5.0
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
MCPWM 5.0 #132
Changes from all commits
Commits
Show all changes
62 commits
Select commit
Hold shift + click to select a range
e47f8af
The beginning of a first rough draft...
usbalbin 5a86430
Fix some of the TODOs, disable example for esp variants without mcpwm
usbalbin bef1ea4
Fix clippy lints
usbalbin 208bde0
Update cfg's
usbalbin ad91ea1
use esp_idf_sys::*
usbalbin 4e3b09c
Address some of the comments and add deadtime support
usbalbin 8a1b0fd
Clippy
usbalbin 17a3e63
Fix warnings in example
usbalbin a428ed6
Fix example
usbalbin 8f2f559
Add some docs for deadtime modes
usbalbin d64529a
Added Mcpwm::operator_source_frequency
usbalbin 729cb85
cargo fmt
usbalbin 15610a2
Add more documentation
usbalbin cc8589d
Update example(crashing)
usbalbin 8ca34ba
Work around float printing issue
usbalbin 4ed2696
Clarify deadtime
usbalbin 490a84a
Some cleanup
usbalbin c874e78
Fix some methods missing their self
usbalbin 9ebbeee
Final(?) adjustments to doc
usbalbin d05ae44
Explain example mcpwm-simple
usbalbin f5a13ca
Allow setting initial duty in OperatorConfig
usbalbin 6bc129a
Clamp timer resolution to not be greater than timer group resolution
usbalbin 1d779db
Expose Mcpwm's unit and Operator's timer
usbalbin 8384b06
Very rough, not working draft of MCPWM 5.0 mostly stolen from MCPWM 4.4
usbalbin 5f01835
Refactor into different files and update example(still not compiling …
usbalbin 5acf5fc
A bit less far from compiling...
usbalbin a10f4f3
Almost compiling
usbalbin 12e34db
Compiling!!! However far from working or even testable...
usbalbin 0caf311
...just kidding. However now it is compiling!! But now quite running
usbalbin 451dcd9
Add TODO comment
usbalbin 952a36f
fmt
usbalbin 9ec6458
Fix some, far from all, warnings
usbalbin ca5685e
Unchecked stuff
usbalbin dc05884
Compiling again after refactor, still far from working
usbalbin d932af4
Do all configuration before building the real types(not compiling)
usbalbin 8d80760
Compiling again, still some work left to do before working
usbalbin 1907eef
Clean up, fix some todos, might be testable now/soon!
usbalbin bb8d957
Fix some more warnings, and some cleanup
usbalbin e7e755e
Remove deadtime example
usbalbin d20ae62
Fix example and more cleanup
usbalbin 7b2a929
Dead time should probably wait...
usbalbin 6e8d31e
Operators constructor does not need to be pub, Timer::resolution is a…
usbalbin d3e44c1
Some cleanup, added comments and removed temp changes in .cargo
usbalbin c15c69f
fmt
usbalbin f053be1
Remove some dead code, fix clippy warnings and silent those that cant…
usbalbin 326299f
Update cfgs to require IDF 5.0 for MCPWM (for now)
usbalbin 44cd1d8
fmt
usbalbin 5c4738b
Put example behind cfg
usbalbin 7ab9357
Add extern functions for vararg
usbalbin aa19703
One large variadic call instead of multiple small
usbalbin 55745e6
Avoid std
usbalbin 1c3dae4
fmt
usbalbin 8bca808
Fix typo
usbalbin 7063f05
Fixes after testing on real hardware (ESP32-S3). Flags set to match I…
usbalbin 1bd2dfd
Remove leftover debug code
usbalbin d786f05
Remove patch
usbalbin 4acbca7
Resolve some of the comments
usbalbin 6913374
Make both comparators obligatory
usbalbin cc76cb5
Make Generators mandatory
usbalbin bdcb5e4
Update example
usbalbin 316f685
Remove some unwraps
usbalbin 66d0dde
fmt
usbalbin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/// Simple example showing how MCPWM may be used to generate two synchronized pwm signals with varying duty. | ||
/// The duty on the pin4 will increase from 0% all the way up to 100% and repeat. At the same time the duty | ||
/// on pin 5 will go from 100% down to 0% | ||
/// | ||
/// # duty = 10% | ||
/// | ||
/// . . | ||
/// . . | ||
/// .------. .------. | ||
/// | | | | | ||
/// pin4 | | | | | ||
/// | | | | | ||
/// ----- -------------------------- -------------------------- | ||
/// . . | ||
/// . . | ||
/// .------------------------. .------------------------. | ||
/// | | | | | ||
/// pin5 | | | | | ||
/// | | | | | ||
/// ----- -------- -------- | ||
/// . . | ||
/// | ||
/// | ||
/// # duty = 50% | ||
/// . . | ||
/// . . | ||
/// .---------------. .---------------. | ||
/// | | | | | ||
/// pin4 | | | | | ||
/// | | | | | ||
/// ----- ----------------- ----------------- | ||
/// . . | ||
/// . . | ||
/// .---------------. .---------------. | ||
/// | | | | | ||
/// pin5 | | | | | ||
/// | | | | | ||
/// ----- ----------------- ----------------- | ||
/// . . | ||
/// | ||
/// | ||
/// # duty = 90% | ||
/// . . | ||
/// . . | ||
/// .------------------------. .------------------------. | ||
/// | | | | | ||
/// pin4 | | | | | ||
/// | | | | | ||
/// ----- -------- -------- | ||
/// . . | ||
/// . . | ||
/// .------. .------. | ||
/// | | | | | ||
/// pin5 | | | | | ||
/// | | | | | ||
/// ----- -------------------------- -------------------------- | ||
/// . . | ||
|
||
#[cfg(all(any(esp32, esp32s3), esp_idf_version_major = "5"))] | ||
fn main() -> anyhow::Result<()> { | ||
use embedded_hal::delay::DelayUs; | ||
|
||
use esp_idf_hal::delay::FreeRtos; | ||
use esp_idf_hal::mcpwm::{OperatorConfig, TimerConfig, TimerDriver}; | ||
use esp_idf_hal::prelude::Peripherals; | ||
|
||
esp_idf_sys::link_patches(); | ||
|
||
println!("Configuring MCPWM"); | ||
|
||
let peripherals = Peripherals::take().unwrap(); | ||
let timer_config = TimerConfig::default().period_ticks(8_000); // 10kHz | ||
let operator_config = OperatorConfig::default(peripherals.pins.gpio4, peripherals.pins.gpio5); | ||
let timer = | ||
TimerDriver::new(peripherals.mcpwm0.timer0, timer_config).expect("Failed to set up timer"); | ||
|
||
let mut timer = timer | ||
.into_connection() | ||
.attach_operator0(peripherals.mcpwm0.operator0, operator_config) | ||
.expect("Failed to set up operator and timer connection"); | ||
|
||
// Borrow references to the contained timer and operator | ||
let (timer, operator, _, _) = timer.split(); | ||
|
||
println!("Starting duty-cycle loop"); | ||
|
||
let period_ticks = timer.get_period_peak(); | ||
|
||
// TODO: Will this work as expected in UP_DOWN counter mode? | ||
for duty in (0..period_ticks).step_by(10).cycle() { | ||
if duty % 100 == 0 { | ||
println!( | ||
"cmp: {}, duty {}%", | ||
duty, | ||
100 * u32::from(duty) / u32::from(period_ticks) | ||
); | ||
} | ||
|
||
operator.set_compare_value_x(duty)?; // In this configuration this controls the duty on pin4 | ||
operator.set_compare_value_y(period_ticks - duty)?; // and this controls pin5 | ||
FreeRtos.delay_ms(10)?; | ||
} | ||
|
||
unreachable!() | ||
} | ||
|
||
#[cfg(not(all(any(esp32, esp32s3), esp_idf_version_major = "5")))] | ||
fn main() { | ||
esp_idf_sys::link_patches(); | ||
|
||
println!("Sorry MCPWM is only supported on ESP32 and ESP32-S3"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use core::ptr; | ||
|
||
use esp_idf_sys::{ | ||
esp, mcpwm_cmpr_handle_t, mcpwm_comparator_config_t, mcpwm_comparator_config_t__bindgen_ty_1, | ||
mcpwm_gen_compare_event_action_t, mcpwm_gen_handle_t, mcpwm_gen_t, mcpwm_new_comparator, | ||
mcpwm_oper_handle_t, mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN, | ||
mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP, EspError, | ||
}; | ||
|
||
use super::generator::CountingDirection; | ||
|
||
pub struct Comparator(pub(crate) mcpwm_cmpr_handle_t); | ||
|
||
impl Comparator { | ||
pub(crate) unsafe fn configure( | ||
&mut self, | ||
gen: &mut mcpwm_gen_t, | ||
cfg: CountingDirection, | ||
) -> Result<(), EspError> { | ||
extern "C" { | ||
fn mcpwm_generator_set_actions_on_compare_event( | ||
gen: mcpwm_gen_handle_t, | ||
ev_act0: mcpwm_gen_compare_event_action_t, | ||
ev_act1: mcpwm_gen_compare_event_action_t, | ||
ev_act_end: mcpwm_gen_compare_event_action_t, | ||
) -> esp_idf_sys::esp_err_t; | ||
} | ||
|
||
esp!(mcpwm_generator_set_actions_on_compare_event( | ||
gen, | ||
mcpwm_gen_compare_event_action_t { | ||
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_UP, | ||
comparator: self.0, | ||
action: cfg.counting_up.into(), | ||
}, | ||
mcpwm_gen_compare_event_action_t { | ||
direction: mcpwm_timer_direction_t_MCPWM_TIMER_DIRECTION_DOWN, | ||
comparator: self.0, | ||
action: cfg.counting_down.into(), | ||
}, | ||
mcpwm_gen_compare_event_action_t { | ||
// <-- This marks the last argument in the variadic list | ||
comparator: ptr::null_mut(), | ||
..Default::default() | ||
} | ||
)) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct ComparatorConfig { | ||
flags: mcpwm_comparator_config_t__bindgen_ty_1, | ||
} | ||
|
||
impl ComparatorConfig { | ||
pub(crate) unsafe fn init( | ||
self, | ||
operator_handle: mcpwm_oper_handle_t, | ||
) -> Result<Comparator, EspError> { | ||
let cfg = mcpwm_comparator_config_t { flags: self.flags }; | ||
|
||
let mut cmp = ptr::null_mut(); | ||
unsafe { | ||
esp!(mcpwm_new_comparator(operator_handle, &cfg, &mut cmp))?; | ||
} | ||
|
||
Ok(Comparator(cmp)) | ||
} | ||
} | ||
|
||
impl Default for ComparatorConfig { | ||
fn default() -> Self { | ||
let mut flags: mcpwm_comparator_config_t__bindgen_ty_1 = Default::default(); | ||
// TODO: What should be set here? | ||
flags.set_update_cmp_on_tep(0); | ||
flags.set_update_cmp_on_tez(1); | ||
flags.set_update_cmp_on_sync(0); | ||
Self { flags } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just remembered espressif/esp-idf#9904 . Do you think
tep
should also be set to1
to avoid that? I have not tested this PR for that problem yetThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.