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

bare0.rs

Blame
  • bare0.rs 3.18 KiB
    //! bare0.rs
    //!
    //! Simple bare metal application
    //! What it covers:
    //! - constants
    //! - global (static) variables
    //! - checked vs. wrapping arithmetics
    //! - safe and unsafe code
    //! - making a safe API
    //! - assertions
    //! - panic handling
    
    // build without the Rust standard library
    #![no_std]
    // no standard main, we declare main using [entry]
    #![no_main]
    
    // Minimal runtime / startup for Cortex-M microcontrollers
    //extern crate cortex_m_rt as rt;
    // Panic handler, for textual output using semihosting
    extern crate panic_semihosting;
    // Panic handler, infinite loop on panic
    // extern crate panic_halt;
    
    // import entry point
    use cortex_m_rt::entry;
    
    // a constant (cannot be changed at run-time)
    const X_INIT: u32 = 10;
    //const X_INIT: u32 = core::u32::MAX;
    
    // global mutable variables (changed using unsafe code)
    static mut X: u32 = X_INIT;
    static mut Y: u32 = 0;
    
    #[entry]
    fn main() -> ! {
        // local mutable variable (changed in safe code)
        let mut x = unsafe { X };
    
        loop {
            x += 1; // <- place breakpoint here (3)
            unsafe {
                X += 1;
                Y = X;
                assert!(x == X && X == Y);
            }
        }
    }
    
    // Here we assume you are using `vscode` with `cortex-debug`.
    //
    // 0. Compile/build the example in debug (dev) mode.
    //
    //    > cargo build --example bare0
    //    (or use the vscode build task)
    //
    // 1. Run the program in the debugger, let the program run for a while and
    //    then press pause.
    //
    //    Look under Variables/Local what do you find.
    //
    //    ** your answer here **
    //
    //    In the Expressions (WATCH -vscode) view add X and Y
    //    what do you find
    //
    //    ** your answer here **
    //
    //    Step through one complete iteration of the loop