Skip to content
Snippets Groups Projects
Select Git revision
  • 267c8f11bb1dafd76adc4b362132cd9edf89a842
  • main default protected
  • blm_dev_chan
  • release/1.4.0 protected
  • widgets_draw
  • return_of_melodic_demo
  • task_cleanup
  • mixer2
  • dx/fb-save-restore
  • dx/dldldld
  • fpletz/flake
  • dx/jacksense-headset-mic-only
  • release/1.3.0 protected
  • fil3s-limit-filesize
  • allow-reloading-sunmenu
  • wifi-json-error-handling
  • app_text_viewer
  • shoegaze-fps
  • media_has_video_has_audio
  • fil3s-media
  • more-accurate-battery
  • v1.4.0
  • v1.3.0
  • v1.2.0
  • v1.2.0+rc1
  • v1.1.1
  • v1.1.0
  • v1.1.0+rc1
  • v1.0.0
  • v1.0.0+rc6
  • v1.0.0+rc5
  • v1.0.0+rc4
  • v1.0.0+rc3
  • v1.0.0+rc2
  • v1.0.0+rc1
35 results

captouch.c

Blame
  • lib.rs 1.36 KiB
    //! Support for dynamically allocated memory
    //!
    //! Reproduces l0dable hardware.c's `_sbrk()`
    //!
    //! Unfortunately, we cannot link `_sbrk()` directly because it
    //! references the unwieldy `errno`.
    //!
    //! ## Example
    //!
    //! ```rust
    //! #![no_std]
    //! #![no_main]
    //!
    //! extern crate alloc;
    //! use alloc::vec;
    //! use card10_l0dable::*;
    //!
    //! main!(main);
    //! fn main() {
    //!     // Pass stack headroom
    //!     card10_alloc::init(128 * 1024);
    //!     let mut xs = vec![];
    //!     xs.push(23);
    //! }
    //! ```
    #![no_std]
    #![feature(asm)]
    #![feature(alloc_error_handler)]
    
    use core::alloc::Layout;
    use alloc_cortex_m::CortexMHeap;
    use card10_sys as _;
    
    #[global_allocator]
    static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
    
    extern "C" {
        static mut __heap_start: u32;
    }
    
    #[inline(always)]
    fn sp() -> usize {
        let mut value;
        unsafe {
            asm!("mov $0, sp" : "=r" (value) ::: "volatile");
        }
        value
    }
    
    /// Call this before using anything from `alloc`.
    ///
    /// Consider the size of your stack-allocated variables for the
    /// `stack_headroom` parameter.
    ///
    /// Returns heap size
    pub fn init(stack_headroom: usize) -> usize {
        let start = unsafe { &__heap_start } as *const _ as usize;
        let size = sp() - stack_headroom - start;
        unsafe { ALLOCATOR.init(start, size); }
        size
    }
    
    #[alloc_error_handler]
    fn on_oom(_layout: Layout) -> ! {
        panic!("OOM")
    }