Skip to content
Snippets Groups Projects
Select Git revision
  • bf2c8430df6ff996181d29fed1ff593dd33a9740
  • master default protected
2 results

rtic_bare6.rs

Blame
  • Forked from Per Lindgren / e7020e_2021
    Source project has a limited visibility.
    rtic_bare6.rs 11.09 KiB
    //! rtic_bare6.rs
    //!
    //! Clocking
    //!
    //! What it covers:
    //! - using svd2rust generated API
    //! - using the stm32f4xx-hal to set clocks
    //! - routing the clock to a PIN for monitoring by an oscilloscope
    
    #![no_main]
    #![no_std]
    
    use panic_rtt_target as _;
    use rtic::cyccnt::{Instant, U32Ext as _};
    use rtt_target::{rprintln, rtt_init_print};
    use stm32f4xx_hal::{
        prelude::*,
        stm32::{self, GPIOC, RCC},
    };
    
    const OFFSET: u32 = 32_000_000;
    
    #[rtic::app(device = stm32f4xx_hal::stm32, monotonic = rtic::cyccnt::CYCCNT, peripherals = true)]
    const APP: () = {
        struct Resources {
            // late resources
            GPIOA: stm32::GPIOA,
        }
        #[init(schedule = [toggle])]
        fn init(cx: init::Context) -> init::LateResources {
            rtt_init_print!();
            rprintln!("init");
    
            let mut core = cx.core;
            let device = cx.device;
    
            // Initialize (enable) the monotonic timer (CYCCNT)
            core.DCB.enable_trace();
            core.DWT.enable_cycle_counter();
    
            // semantically, the monotonic timer is frozen at time "zero" during `init`
            // NOTE do *not* call `Instant::now` in this context; it will return a nonsense value
            let now = cx.start; // the start time of the system
    
            // Schedule `toggle` to run 8e6 cycles (clock cycles) in the future
            cx.schedule.toggle(now + OFFSET.cycles()).unwrap();
    
            // setup LED
            // power on GPIOA, RM0368 6.3.11
            device.RCC.ahb1enr.modify(|_, w| w.gpioaen().set_bit());
            // configure PA5 as output, RM0368 8.4.1
            device.GPIOA.moder.modify(|_, w| w.moder5().output());
    
            clock_out(&device.RCC, &device.GPIOC);
    
            let rcc = device.RCC.constrain();
    
            // let _clocks = rcc.cfgr.freeze();
    
            // Set up the system clock. 48 MHz?
            // let _clocks = rcc
            //     .cfgr
            //     .sysclk(48.mhz())
            //     .pclk1(24.mhz())
            //     .freeze();
            // 
            // let _clocks = rcc
            //     .cfgr
            //     .sysclk(64.mhz())
            //     .pclk1(64.mhz())