//! Blinks an LED
//!
//! This assumes that a LED is connected to the pin assigned to `led`. (GPIO4)
#![no_std]
#![no_main]

use core::{pin::Pin, borrow::BorrowMut};
use hal::{clock::{ClockControl, Clocks}, peripherals::Peripherals, prelude::*, timer::{TimerGroup, Timer0, Timer1}, Rtc, system::{PeripheralClockControl, SystemClockControl, }, mcpwm::PeripheralClockConfig, delay, IO, xtensa_lx::timer::delay, Delay, Timer, dma::SpiPeripheral,};

use backtrace as _;


#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = peripherals.DPORT.split();
    let system_cocks: Clocks<'_> = ClockControl::boot_defaults(system.clock_control).freeze();
    let mut peripheral_cock_control: PeripheralClockControl = system.peripheral_clock_control;
    
    let mut tg0 = TimerGroup::new(peripherals.TIMG0, &system_cocks, &mut peripheral_cock_control);
    let mut tg0_timer0 = Timer0::from(tg0.timer0.free());
    
    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

    let mut gpio2_output = io.pins.gpio2.into_push_pull_output();
    gpio2_output.set_high().unwrap();
    {
        let mut rtc = Rtc::new(peripherals.RTC_CNTL);
        rtc.rwdt.disable();    
    }
    
    let mut next = 0;
    loop {
        let now = tg0_timer0.now();
        if now >= next{
            gpio2_output.toggle().unwrap();
            next = now + 100;
        }

        // delay.delay_ms(500_u32);
        
    }
}
A4988