Skip to content

Instantly share code, notes, and snippets.

@nebelgrau77
Last active September 18, 2020 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebelgrau77/6f158d3ee406925d864673a885023039 to your computer and use it in GitHub Desktop.
Save nebelgrau77/6f158d3ee406925d864673a885023039 to your computer and use it in GitHub Desktop.
use arduino_nano33iot as hal;

use hal::clock::GenericClockController;
use hal::prelude::*;
use rtic::app;
use core::fmt::Write;
use hal::adc::Adc;

#[app(device = crate::hal::pac, peripherals = true)]
const APP: () = {
    struct Resources {
        led: hal::gpio::Pa17<hal::gpio::Output<hal::gpio::OpenDrain>>,
        timer: hal::timer::TimerCounter3,        
        uart: hal::sercom::UART5<hal::sercom::Sercom5Pad3<hal::gpio::Pb23<hal::gpio::PfD>>,hal::sercom::Sercom5Pad2<hal::gpio::Pb22<hal::gpio::PfD>>,(),()>,
        analog: hal::gpio::Pa2<hal::gpio::PfB>,        
        adc: hal::adc::Adc<hal::ADC>, // this does not work
    }


    /// This function is called each time the tc3 interrupt triggers.
    /// We use it to toggle the LED.  The `wait()` call is important
    /// because it checks and resets the counter ready for the next
    /// period.
    
    #[task(binds = TC3, resources = [timer, led, uart, analog])]
    fn tc3(c: tc3::Context) {
        if c.resources.timer.wait().is_ok() {
            c.resources.led.toggle();                
            let sample = adc.read(&mut c.resources.analog).unwrap();
            c.resources.uart.write_fmt(format_args!("Value: {}\n\r", sample)).unwrap();
            
        }
    }

    #[init]
    fn init(c: init::Context) -> init::LateResources {
        let mut device = c.device;

        let mut clocks = GenericClockController::with_internal_32kosc(
            device.GCLK,
            &mut device.PM,
            &mut device.SYSCTRL,
            &mut device.NVMCTRL,
        );
        let gclk0 = clocks.gclk0();
        let mut pins = hal::Pins::new(device.PORT);

        let mut tc3 = hal::timer::TimerCounter::tc3_(
            &clocks.tcc2_tc3(&gclk0).unwrap(),
            device.TC3,
            &mut device.PM,
        );
        
        let mut serial = hal::uart(&mut clocks,
            9600.hz(), 
            device.SERCOM5,
            &mut device.PM,
            pins.rx,
            pins.tx,
            &mut pins.port);

        let mut adc = Adc::adc(
                        device.ADC, 
                        &mut device.PM, 
                        &mut clocks);        

        tc3.start(1.hz());
        tc3.enable_interrupt();

        init::LateResources {
            led: pins.led_sck.into_open_drain_output(&mut pins.port),
            timer: tc3,        
            uart: serial,            
            // not sure if this will work
            analog: pins.a0.into_function_b(&mut pins.port), 
            adc: adc,
        }
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment