Skip to content

Commit 94cbc2b

Browse files
committed
Add a convenience wrapper around the sdram slice
1 parent b7d4a36 commit 94cbc2b

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

examples/lcd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ fn main() -> ! {
5555
gpio_a, gpio_b, gpio_c, gpio_d, gpio_e, gpio_f, gpio_g, gpio_h, gpio_i, gpio_j, gpio_k,
5656
);
5757

58-
let sdram = init::init_sdram(&mut rcc, &mut fmc);
59-
let (mut lcd, _sdram) = lcd::init(&mut ltdc, &mut rcc, sdram);
58+
let mut sdram = init::init_sdram(&mut rcc, &mut fmc);
59+
let mut lcd = lcd::init(&mut ltdc, &mut rcc, &mut sdram);
6060
pins.display_enable.set(true);
6161
pins.backlight.set(true);
6262

src/bin/async-await.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ fn run() -> ! {
102102
init::init_systick(Hz(100), &mut systick, &rcc);
103103
systick.enable_interrupt();
104104

105-
let sdram = init::init_sdram(&mut rcc, &mut fmc);
106-
let (mut lcd, _sdram) = lcd::init(&mut ltdc, &mut rcc, sdram);
105+
let mut sdram = init::init_sdram(&mut rcc, &mut fmc);
106+
let mut lcd = lcd::init(&mut ltdc, &mut rcc, &mut sdram);
107107
pins.display_enable.set(true);
108108
pins.backlight.set(true);
109109

src/bin/polling.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ fn main() -> ! {
8787
init::init_systick(Hz(100), &mut systick, &rcc);
8888
systick.enable_interrupt();
8989

90-
let sdram = init::init_sdram(&mut rcc, &mut fmc);
91-
let (mut lcd, _sdram) = lcd::init(&mut ltdc, &mut rcc, sdram);
90+
let mut sdram = init::init_sdram(&mut rcc, &mut fmc);
91+
let mut lcd = lcd::init(&mut ltdc, &mut rcc, &mut sdram);
9292
pins.display_enable.set(true);
9393
pins.backlight.set(true);
9494

src/init/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use stm32f7::stm32f7x6::{self as device, FLASH, FMC, PWR, RCC, SAI2, SYST};
66

77
pub use self::pins::init as pins;
88
pub use self::pins::Pins;
9+
use core::mem;
910

1011
mod pins;
1112

@@ -147,10 +148,30 @@ pub fn enable_syscfg(rcc: &mut RCC) {
147148

148149
static 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

src/lcd/init.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::init::SdRam;
12
use super::Lcd;
23
use stm32f7::stm32f7x6::{LTDC, RCC};
34

@@ -10,8 +11,8 @@ use stm32f7::stm32f7x6::{LTDC, RCC};
1011
pub fn init<'a>(
1112
ltdc: &'a mut LTDC,
1213
rcc: &mut RCC,
13-
mem: &'static mut [volatile::Volatile<u8>],
14-
) -> (Lcd<'a>, &'static mut [volatile::Volatile<u8>]) {
14+
mem: &mut SdRam,
15+
) -> Lcd<'a> {
1516
use crate::lcd;
1617
const HEIGHT: u16 = lcd::HEIGHT as u16;
1718
const WIDTH: u16 = lcd::WIDTH as u16;
@@ -156,8 +157,8 @@ pub fn init<'a>(
156157
w
157158
});
158159

159-
let (layer1, mem) = mem.split_at_mut(lcd::LAYER_1_LENGTH);
160-
let (layer2, mem) = mem.split_at_mut(lcd::LAYER_2_LENGTH);
160+
let layer1 = mem.allocate(lcd::LAYER_1_LENGTH);
161+
let layer2 = mem.allocate(lcd::LAYER_2_LENGTH);
161162

162163
// configure color frame buffer start address
163164
ltdc.l1cfbar
@@ -193,5 +194,5 @@ pub fn init<'a>(
193194

194195
lcd.reload_shadow_registers(); // IMMEDIATE_RELOAD
195196

196-
(lcd, mem)
197+
lcd
197198
}

0 commit comments

Comments
 (0)