@@ -6,6 +6,7 @@ use stm32f7::stm32f7x6::{self as device, FLASH, FMC, PWR, RCC, SAI2, SYST};
66
77pub use self :: pins:: init as pins;
88pub use self :: pins:: Pins ;
9+ use core:: mem;
910
1011mod pins;
1112
@@ -147,10 +148,30 @@ pub fn enable_syscfg(rcc: &mut RCC) {
147148
148149static mut SDRAM_INITIALIZED : bool = false ;
149150
151+ /// SdRam allocator helper with some convenience methods
152+ pub struct SdRam ( & ' static mut [ volatile:: Volatile < u8 > ] ) ;
153+
154+ impl SdRam {
155+ /// Allocates `size` bytes or panics if not enough memory available
156+ ///
157+ /// Note: it is not possible to free any memory
158+ pub fn allocate ( & mut self , size : usize ) -> & ' static mut [ volatile:: Volatile < u8 > ] {
159+ let memory = mem:: replace ( & mut self . 0 , & mut [ ] ) ;
160+ let ( ret, rest) = memory. split_at_mut ( size) ;
161+ mem:: replace ( & mut self . 0 , rest) ;
162+ ret
163+ }
164+
165+ /// Yields the rest of the available memory
166+ pub fn all ( self ) -> & ' static mut [ volatile:: Volatile < u8 > ] {
167+ self . 0
168+ }
169+ }
170+
150171/// Initializes the SDRAM, which makes more memory accessible.
151172///
152173/// This is a prerequisite for using the LCD.
153- pub fn init_sdram ( rcc : & mut RCC , fmc : & mut FMC ) -> & ' static mut [ volatile :: Volatile < u8 > ] {
174+ pub fn init_sdram ( rcc : & mut RCC , fmc : & mut FMC ) -> SdRam {
154175
155176 // ensures that we don't do this twice and end up with two `&'static mut` to the same memory
156177 unsafe {
@@ -302,7 +323,7 @@ pub fn init_sdram(rcc: &mut RCC, fmc: &mut FMC) -> &'static mut [volatile::Volat
302323 }
303324 // this block's safety is guaranteed by SDRAM_INITIALIZED check at the start of this function
304325 unsafe {
305- core:: slice:: from_raw_parts_mut ( sdram_start as * mut volatile:: Volatile < u8 > , sdram_len)
326+ SdRam ( core:: slice:: from_raw_parts_mut ( sdram_start as * mut volatile:: Volatile < u8 > , sdram_len) )
306327 }
307328}
308329
0 commit comments