mirror of
				https://github.com/esp-rs/esp-hal.git
				synced 2025-11-04 06:43:55 +00:00 
			
		
		
		
	* No longer publicly expose the `PeripheralClockControl` struct * Update examples as needed to get things building again * Update CHANGELOG.md * Address review feedback, fix a warning * Use a critical section for all devices other than the ESP32-C6/H2, as they modify multiple registers * Rebase and update `etm` driver to fix build errors
		
			
				
	
	
		
			50 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! Uses timer0 and operator0 of the MCPWM0 peripheral to output a 50% duty
 | 
						|
//! signal at 20 kHz.
 | 
						|
//!
 | 
						|
//! The signal will be output to the pin assigned to `pin`. (GPIO4)
 | 
						|
 | 
						|
#![no_std]
 | 
						|
#![no_main]
 | 
						|
 | 
						|
use esp32s3_hal::{
 | 
						|
    clock::ClockControl,
 | 
						|
    gpio::IO,
 | 
						|
    mcpwm::{operator::PwmPinConfig, timer::PwmWorkingMode, PeripheralClockConfig, MCPWM},
 | 
						|
    peripherals::Peripherals,
 | 
						|
    prelude::*,
 | 
						|
};
 | 
						|
use esp_backtrace as _;
 | 
						|
 | 
						|
#[entry]
 | 
						|
fn main() -> ! {
 | 
						|
    let peripherals = Peripherals::take();
 | 
						|
    let system = peripherals.SYSTEM.split();
 | 
						|
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
 | 
						|
 | 
						|
    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
 | 
						|
    let pin = io.pins.gpio4;
 | 
						|
 | 
						|
    // initialize peripheral
 | 
						|
    let clock_cfg = PeripheralClockConfig::with_frequency(&clocks, 40u32.MHz()).unwrap();
 | 
						|
    let mut mcpwm = MCPWM::new(peripherals.MCPWM0, clock_cfg);
 | 
						|
 | 
						|
    // connect operator0 to timer0
 | 
						|
    mcpwm.operator0.set_timer(&mcpwm.timer0);
 | 
						|
    // connect operator0 to pin
 | 
						|
    let mut pwm_pin = mcpwm
 | 
						|
        .operator0
 | 
						|
        .with_pin_a(pin, PwmPinConfig::UP_ACTIVE_HIGH);
 | 
						|
 | 
						|
    // start timer with timestamp values in the range of 0..=99 and a frequency of
 | 
						|
    // 20 kHz
 | 
						|
    let timer_clock_cfg = clock_cfg
 | 
						|
        .timer_clock_with_frequency(99, PwmWorkingMode::Increase, 20u32.kHz())
 | 
						|
        .unwrap();
 | 
						|
    mcpwm.timer0.start(timer_clock_cfg);
 | 
						|
 | 
						|
    // pin will be high 50% of the time
 | 
						|
    pwm_pin.set_timestamp(50);
 | 
						|
 | 
						|
    loop {}
 | 
						|
}
 |