Skip to content
Snippets Groups Projects
Select Git revision
  • 544b3650df5aa29cacdf91a213b31df55d9a5792
  • master default protected
  • exam
  • exper
  • klee
  • simple
  • v0.3.2
  • v0.3.1
  • v0.3.0
  • v0.2.2
  • v0.2.1
  • v0.2.0
  • v0.1.1
  • v0.1.0
14 results

resource.rs

Blame
  • 2-rtfm.rs 2.36 KiB
    #![deny(unsafe_code)]
    // #![deny(warnings)]
    #![no_main]
    #![no_std]
    
    use async_cortex_m::{task, unsync::Mutex};
    use cortex_m::asm;
    use cortex_m_semihosting::hprintln;
    use pac::Interrupt::TIMER0;
    use panic_udf as _; // panic handler
    use rtfm::app;
    
    #[app(device = pac)]
    const APP: () = {
        struct Resources {
            #[init(9)]
            rtfm_x:u64,
        }
    
        #[idle(resources = [rtfm_x])]
        fn idle(mut cx: idle::Context) -> ! {
            static mut X: Mutex<i64> = Mutex::new(0);
            let c: &'static _ = X;
            // static mut RT_X : impl rtfm::Mutex = &mut cx.resources.x;
    
            // rt_x.lock(|x| {
            //     hprintln!("x {}", x);
            // });
      
            cx.resources.rtfm_x.lock(|x| {
                hprintln!("x {}", x);
            });
    
            // let cc:&'static _ = &cx.resources.x;
       
            task::spawn(async {
                loop {
                    hprintln!("A: before lock").ok();
    
                    // let mut lock = c.lock().await;
    
                    // hprintln!("A: before write {}", *lock).ok();
                    // *lock += 1;
                    // drop(lock);
    
                    hprintln!("A: after releasing the lock").ok();
    
                    hprintln!("A: manual yield").ok();
                    task::r#yield().await;
                    // cc.lock(|x| {
                    //     hprintln!("x {}", x);
                    // });
    
                    cx.resources.rtfm_x.lock(|x| {
                        hprintln!("x {}", x);
                    });
                    
                }
            });
    
            task::block_on(async {
                loop {
                    hprintln!("B: before lock").ok();
    
                    // cannot immediately make progress; context switch to A
                    let mut lock = c.lock().await;
    
                    hprintln!("B: {}", *lock).ok();
                    *lock += 1;
    
    
                    if *lock > 10 {
                        loop {
                            asm::bkpt();
                        }
                    } else {
                        drop(lock);
                        hprintln!("B: yield").ok();
                        task::r#yield().await;
                    }
    
                    cx.resources.rtfm_x.lock(|x| {
                        hprintln!("x {}", x);
                    });
                }
            });
    
            loop {
                continue;
            }
        }
    
        #[task(priority = 2, binds = TIMER0, resources = [rtfm_x])]
        fn rt_task(cx:rt_task::Context) {
            hprintln!("x {}", cx.resources.rtfm_x);
        }
    };