Skip to content

Commit d7f07d8

Browse files
committed
Remove more static mut referencing.
Also add the Default that clippy wanted.
1 parent a1f5d57 commit d7f07d8

File tree

4 files changed

+52
-37
lines changed

4 files changed

+52
-37
lines changed

src/console.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ impl TextConsole {
304304
}
305305
}
306306

307+
impl Default for TextConsole {
308+
fn default() -> Self {
309+
Self::new()
310+
}
311+
}
312+
307313
unsafe impl Sync for TextConsole {}
308314

309315
impl core::fmt::Write for &TextConsole {

src/main.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ mod vga;
6262
use core::{
6363
convert::{TryFrom, TryInto},
6464
fmt::Write,
65+
ptr::{addr_of, addr_of_mut},
6566
sync::atomic::{AtomicBool, AtomicU32, Ordering},
6667
};
6768

@@ -333,10 +334,10 @@ static API_CALLS: common::Api = common::Api {
333334
const SECONDS_BETWEEN_UNIX_AND_NEOTRON_EPOCH: i64 = 946684800;
334335

335336
extern "C" {
336-
static mut _flash_os_start: u32;
337-
static mut _flash_os_len: u32;
338-
static mut _ram_os_start: u32;
339-
static mut _ram_os_len: u32;
337+
static _flash_os_start: u32;
338+
static _flash_os_len: u32;
339+
static _ram_os_start: u32;
340+
static _ram_os_len: u32;
340341
}
341342

342343
/// What we paint Core 0's stack with
@@ -710,11 +711,10 @@ fn paint_stacks() {
710711
static mut _stack_start: usize;
711712
}
712713
unsafe {
713-
let stack_len =
714-
(&_stack_start as *const usize as usize) - (&__sheap as *const usize as usize);
714+
let stack_len = (addr_of!(_stack_start) as usize) - (addr_of!(__sheap) as usize);
715715
// But not the top 64 words, because we're using the stack right now!
716716
let stack = core::slice::from_raw_parts_mut(
717-
&mut __sheap as *mut usize,
717+
addr_of_mut!(__sheap),
718718
(stack_len / core::mem::size_of::<usize>()) - 256,
719719
);
720720
info!("Painting Core 1 stack: {:?}", stack.as_ptr_range());
@@ -744,10 +744,9 @@ fn check_stacks() {
744744
static mut __sheap: usize;
745745
static mut _stack_start: usize;
746746
}
747-
let stack_len =
748-
unsafe { (&_stack_start as *const usize as usize) - (&__sheap as *const usize as usize) };
747+
let stack_len = unsafe { (addr_of!(_stack_start) as usize) - (addr_of!(__sheap) as usize) };
749748
check_stack(
750-
unsafe { &__sheap as *const usize },
749+
unsafe { addr_of!(__sheap) },
751750
stack_len,
752751
CORE0_STACK_PAINT_WORD,
753752
);
@@ -1896,8 +1895,8 @@ pub extern "C" fn memory_get_region(region: u8) -> FfiOption<common::MemoryRegio
18961895
0 => {
18971896
// Application Region
18981897
FfiOption::Some(MemoryRegion {
1899-
start: unsafe { &mut _ram_os_start as *mut u32 } as *mut u8,
1900-
length: unsafe { &mut _ram_os_len as *const u32 } as usize,
1898+
start: unsafe { addr_of!(_ram_os_start) } as *mut u8,
1899+
length: unsafe { addr_of!(_ram_os_len) } as usize,
19011900
kind: common::MemoryKind::Ram.into(),
19021901
})
19031902
}

src/multicore.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ static CORE1_ENTRY_FUNCTION: [u16; 2] = [
6565
/// Starts core 1 running the given function, with the given stack.
6666
pub fn launch_core1_with_stack(
6767
main_func: unsafe extern "C" fn() -> u32,
68-
stack: &mut [usize],
68+
stack: *mut usize,
69+
stack_len: usize,
6970
ppb: &mut crate::pac::PPB,
7071
fifo: &mut rp_pico::hal::sio::SioFifo,
7172
psm: &mut crate::pac::PSM,
@@ -80,25 +81,30 @@ pub fn launch_core1_with_stack(
8081

8182
defmt::debug!("Setting up stack...");
8283

83-
// Gets popped into `r0` by CORE1_ENTRY_FUNCTION. This is the `main`
84-
// function we want to run. It appears in the call to `core1_wrapper` as
85-
// the first argument.
86-
stack[stack.len() - 3] = main_func as *const () as usize;
87-
// Gets popped into `r1` by CORE1_ENTRY_FUNCTION. This is the top of stack
88-
// for Core 1. It appears in the call to `core1_wrapper` as the second
89-
// argument.
90-
stack[stack.len() - 2] = stack.as_ptr() as *const _ as usize;
91-
// Gets popped into `pc` by CORE1_ENTRY_FUNCTION. This is the function
92-
// `CORE1_ENTRY_FUNCTION` will jump to, passing the above two values as
93-
// arguments.
94-
stack[stack.len() - 1] = core1_wrapper as *const () as usize;
95-
// Point into the top of the stack (so there are three values pushed onto
96-
// it, i.e. at/above it)
97-
let stack_ptr = unsafe { stack.as_mut_ptr().add(stack.len() - 3) };
84+
let stack_ptr = unsafe {
85+
// Gets popped into `r0` by CORE1_ENTRY_FUNCTION. This is the `main`
86+
// function we want to run. It appears in the call to `core1_wrapper` as
87+
// the first argument.
88+
stack
89+
.add(stack_len - 3)
90+
.write_volatile(main_func as *const () as usize);
91+
// Gets popped into `r1` by CORE1_ENTRY_FUNCTION. This is the top of stack
92+
// for Core 1. It appears in the call to `core1_wrapper` as the second
93+
// argument.
94+
stack.add(stack_len - 2).write_volatile(stack as usize);
95+
// Gets popped into `pc` by CORE1_ENTRY_FUNCTION. This is the function
96+
// `CORE1_ENTRY_FUNCTION` will jump to, passing the above two values as
97+
// arguments.
98+
stack
99+
.add(stack_len - 1)
100+
.write_volatile(core1_wrapper as *const () as usize);
101+
// Point into the top of the stack (so there are three values pushed onto
102+
// it, i.e. at/above it)
103+
stack.add(stack_len - 3)
104+
};
98105

99106
defmt::debug!("Stack ptr is 0x{:x}", stack_ptr);
100-
defmt::debug!("Stack bottom is 0x{:x}", stack.as_ptr());
101-
defmt::debug!("Stack top is 0x{:x}", &stack[stack.len() - 4..stack.len()]);
107+
defmt::debug!("Stack bottom is 0x{:x}", stack);
102108

103109
// This is the launch sequence we send to core1, to get it to leave the
104110
// boot ROM and run our code.

src/vga/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod rgb;
4242

4343
use core::{
4444
cell::{RefCell, UnsafeCell},
45+
ptr::addr_of_mut,
4546
sync::atomic::{AtomicBool, AtomicU16, AtomicU32, Ordering},
4647
};
4748
use defmt::{debug, trace};
@@ -1935,13 +1936,16 @@ pub fn init(
19351936
// No-one else is looking at this right now.
19361937
TEXT_COLOUR_LOOKUP.init(&VIDEO_PALETTE);
19371938

1938-
crate::multicore::launch_core1_with_stack(
1939-
core1_main,
1940-
unsafe { &mut super::CORE1_STACK },
1941-
ppb,
1942-
fifo,
1943-
psm,
1944-
);
1939+
unsafe {
1940+
crate::multicore::launch_core1_with_stack(
1941+
core1_main,
1942+
addr_of_mut!(super::CORE1_STACK) as *mut usize,
1943+
super::CORE1_STACK.len(),
1944+
ppb,
1945+
fifo,
1946+
psm,
1947+
);
1948+
}
19451949

19461950
debug!("Core 1 running");
19471951
}

0 commit comments

Comments
 (0)