File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -111,6 +111,7 @@ pub mod interrupt;
111111pub mod itm;
112112pub mod peripheral;
113113pub mod prelude;
114+ pub mod psp;
114115pub mod register;
115116
116117pub use crate :: peripheral:: Peripherals ;
Original file line number Diff line number Diff line change 1+ //! Process Stack Pointer support
2+
3+ #![ allow( clippy:: missing_inline_in_public_items) ]
4+
5+ use core:: cell:: UnsafeCell ;
6+
7+ /// A stack you can use as your Process Stack (PSP)
8+ ///
9+ /// The const-param N is the size **in 32-bit words**
10+ #[ repr( align( 8 ) , C ) ]
11+ pub struct Stack < const N : usize > {
12+ space : UnsafeCell < [ u32 ; N ] > ,
13+ }
14+
15+ impl < const N : usize > Stack < N > {
16+ /// Const-initialise a Stack
17+ ///
18+ /// Use a turbofish to specify the size, like:
19+ ///
20+ /// ```rust
21+ /// static PSP_STACK: Stack::<4096> = Stack::new();
22+ /// ```
23+ pub const fn new ( ) -> Stack < N > {
24+ Stack {
25+ space : UnsafeCell :: new ( [ 0 ; N ] ) ,
26+ }
27+ }
28+
29+ /// Return the top of the stack
30+ pub fn get_top ( & self ) -> * mut u32 {
31+ let start = self . space . get ( ) as * mut u32 ;
32+ unsafe { start. add ( N ) }
33+ }
34+ }
35+
36+ unsafe impl < const N : usize > Sync for Stack < N > { }
37+
38+ impl < const N : usize > core:: default:: Default for Stack < N > {
39+ fn default ( ) -> Self {
40+ Stack :: new ( )
41+ }
42+ }
43+
44+ /// Switch to running on the PSP
45+ #[ cfg( cortex_m) ]
46+ pub fn switch_to_psp < const N : usize > ( psp_stack : & Stack < N > , function : extern "C" fn ( ) -> !) -> ! {
47+ let stack_top = psp_stack. get_top ( ) ;
48+ unsafe { crate :: asm:: enter_unprivileged ( stack_top, function) }
49+ }
You can’t perform that action at this time.
0 commit comments