Skip to content

Commit c0299a3

Browse files
committed
Merge branch '✨-bind' into 🦆
2 parents 16791c5 + 028231a commit c0299a3

File tree

46 files changed

+4333
-388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+4333
-388
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ R3 is a proof-of-concept of a static RTOS that utilizes Rust's compile-time func
2727

2828
- The kernel timing mechanism drives **software timers** and a **system-global clock** with microsecond precision. The system clock can be rewound or fast-forwarded for drift compensation. The timing algorithm has a logarithmic time complexity and is therefore scalable. The implementation is robust against a large interrupt processing delay.
2929

30+
- **Bindings** are a statically-defined storage with runtime initialization and configuration-time borrow checking. They can be bound to tasks and other objects to provide safe mutable access.
31+
3032
- The utility library includes safe container types such as **`Mutex`** and **`RecursiveMutex`**, which are built upon low-level synchronization primitives.
3133

3234
[the priority ceiling protocol]: https://en.wikipedia.org/wiki/Priority_ceiling_protocol
@@ -47,6 +49,7 @@ The R3 original kernel is provided as a separate package [`r3_kernel`][].
4749
#![feature(asm)]
4850
#![feature(asm_sym)]
4951
#![feature(const_fn_trait_bound)]
52+
#![feature(const_refs_to_cell)]
5053
#![feature(const_mut_refs)]
5154
#![feature(const_fn_fn_ptr_basics)]
5255
#![feature(const_trait_impl)]
@@ -73,7 +76,7 @@ impl port::SysTickOptions for SystemTraits {
7376

7477
// ----------------------------------------------------------------
7578

76-
use r3::kernel::StaticTask;
79+
use r3::{bind::bind, kernel::StaticTask, prelude::*};
7780

7881
struct Objects {
7982
task: StaticTask<System>,
@@ -85,16 +88,20 @@ const COTTAGE: Objects = r3_kernel::build!(SystemTraits, configure_app => Object
8588
const fn configure_app(b: &mut r3_kernel::Cfg<SystemTraits>) -> Objects {
8689
System::configure_systick(b);
8790

91+
// Runtime-initialized static storage
92+
let count = bind((), || 1u32).finish(b);
93+
8894
Objects {
95+
// Create a task, giving the ownership of `count`
8996
task: StaticTask::define()
90-
.start(task_body)
97+
.start_with_bind((count.borrow_mut(),), task_body)
9198
.priority(2)
9299
.active(true)
93100
.finish(b),
94101
}
95102
}
96103

97-
fn task_body() {
104+
fn task_body(count: &mut u32) {
98105
// ...
99106
}
100107
```

examples/basic/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(const_fn_trait_bound)]
22
#![feature(const_fn_fn_ptr_basics)]
3+
#![feature(const_refs_to_cell)]
34
#![feature(const_mut_refs)]
45
#![feature(const_trait_impl)]
56
#![deny(unsafe_op_in_unsafe_fn)]

examples/basic_gr_peach/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(const_fn_trait_bound)]
22
#![feature(const_fn_fn_ptr_basics)]
3+
#![feature(const_refs_to_cell)]
34
#![feature(const_mut_refs)]
45
#![feature(const_trait_impl)]
56
#![feature(asm_sym)]

examples/basic_nucleo_f401re/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(asm_sym)]
22
#![feature(const_fn_trait_bound)]
33
#![feature(const_fn_fn_ptr_basics)]
4+
#![feature(const_refs_to_cell)]
45
#![feature(const_mut_refs)]
56
#![feature(const_trait_impl)]
67
#![deny(unsafe_op_in_unsafe_fn)]

examples/basic_rp_pico/src/main.rs

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
#![feature(asm_sym)]
22
#![feature(const_fn_trait_bound)]
33
#![feature(const_fn_fn_ptr_basics)]
4+
#![feature(const_refs_to_cell)]
45
#![feature(const_mut_refs)]
56
#![feature(const_trait_impl)]
67
#![deny(unsafe_op_in_unsafe_fn)]
78
#![no_std]
89
#![no_main]
910
#![cfg(target_os = "none")]
1011
use r3::{
11-
kernel::{prelude::*, StartupHook, StaticTask},
12+
bind::bind,
13+
kernel::{prelude::*, StaticTask},
14+
prelude::*,
1215
sync::StaticMutex,
1316
};
1417
use r3_port_arm_m as port;
@@ -72,10 +75,10 @@ const COTTAGE: Objects = r3_kernel::build!(SystemTraits, configure_app => Object
7275
const fn configure_app(b: &mut r3_kernel::Cfg<SystemTraits>) -> Objects {
7376
b.num_task_priority_levels(4);
7477

75-
StartupHook::define()
76-
.start(|| {
78+
let (rp2040_resets, rp2040_usbctrl_regs, rp2040_sio, rp2040_pads_bank0, rp2040_io_bank0) =
79+
bind((), || {
7780
// Configure peripherals
78-
let p = unsafe { rp2040::Peripherals::steal() };
81+
let p = rp2040::Peripherals::take().unwrap();
7982
support_rp2040::clock::init_clock(
8083
&p.CLOCKS,
8184
&p.XOSC,
@@ -105,11 +108,19 @@ const fn configure_app(b: &mut r3_kernel::Cfg<SystemTraits>) -> Objects {
105108

106109
support_rp2040::stdout::set_stdout(uart0.into_nb_writer());
107110
}
111+
112+
(p.RESETS, p.USBCTRL_REGS, p.SIO, p.PADS_BANK0, p.IO_BANK0)
108113
})
109-
.finish(b);
114+
.unpure()
115+
.finish(b)
116+
.unzip();
110117

111118
if USE_USB_UART {
112-
support_rp2040::usbstdio::configure::<_, SystemTraits>(b);
119+
support_rp2040::usbstdio::configure::<_, SystemTraits>(
120+
b,
121+
rp2040_resets,
122+
rp2040_usbctrl_regs,
123+
);
113124
}
114125

115126
SystemTraits::configure_systick(b);
@@ -119,7 +130,17 @@ const fn configure_app(b: &mut r3_kernel::Cfg<SystemTraits>) -> Objects {
119130
.priority(2)
120131
.active(true)
121132
.finish(b);
122-
let task2 = StaticTask::define().start(task2_body).priority(3).finish(b);
133+
let task2 = StaticTask::define()
134+
.start_with_bind(
135+
(
136+
rp2040_sio.borrow_mut(),
137+
rp2040_pads_bank0.borrow_mut(),
138+
rp2040_io_bank0.borrow_mut(),
139+
),
140+
task2_body,
141+
)
142+
.priority(3)
143+
.finish(b);
123144

124145
let mutex1 = StaticMutex::define().finish(b);
125146

@@ -136,30 +157,44 @@ fn task1_body() {
136157
COTTAGE.task2.activate().unwrap();
137158
}
138159

139-
fn task2_body() {
140-
let p = unsafe { rp2040::Peripherals::steal() };
141-
160+
fn task2_body(
161+
rp2040_sio: &mut rp2040::SIO,
162+
rp2040_pads_bank0: &mut rp2040::PADS_BANK0,
163+
rp2040_io_bank0: &mut rp2040::IO_BANK0,
164+
) {
142165
// <https://github.com/jannic/rp-microcontroller-rs/blob/master/boards/rp-pico/examples/blink/main.rs>
143166
// TODO: Documentate what this code does
144167
let pin = 25;
145168

146-
p.SIO.gpio_oe_clr.write(|w| unsafe { w.bits(1 << pin) });
147-
p.SIO.gpio_out_clr.write(|w| unsafe { w.bits(1 << pin) });
169+
rp2040_sio
170+
.gpio_oe_clr
171+
.write(|w| unsafe { w.bits(1 << pin) });
172+
rp2040_sio
173+
.gpio_out_clr
174+
.write(|w| unsafe { w.bits(1 << pin) });
148175

149-
p.PADS_BANK0
176+
rp2040_pads_bank0
150177
.gpio25
151178
.write(|w| w.ie().bit(true).od().bit(false));
152179

153-
p.IO_BANK0.gpio25_ctrl.write(|w| w.funcsel().sio_25());
180+
rp2040_io_bank0.gpio25_ctrl.write(|w| w.funcsel().sio_25());
154181

155-
p.SIO.gpio_oe_set.write(|w| unsafe { w.bits(1 << pin) });
156-
p.SIO.gpio_out_set.write(|w| unsafe { w.bits(1 << pin) });
182+
rp2040_sio
183+
.gpio_oe_set
184+
.write(|w| unsafe { w.bits(1 << pin) });
185+
rp2040_sio
186+
.gpio_out_set
187+
.write(|w| unsafe { w.bits(1 << pin) });
157188

158189
loop {
159190
// Blink the LED
160-
p.SIO.gpio_out_set.write(|w| unsafe { w.bits(1 << pin) });
191+
rp2040_sio
192+
.gpio_out_set
193+
.write(|w| unsafe { w.bits(1 << pin) });
161194
System::sleep(r3::time::Duration::from_millis(100)).unwrap();
162-
p.SIO.gpio_out_clr.write(|w| unsafe { w.bits(1 << pin) });
195+
rp2040_sio
196+
.gpio_out_clr
197+
.write(|w| unsafe { w.bits(1 << pin) });
163198

164199
support_rp2040::sprintln!("time = {:?}", System::time().unwrap());
165200
System::sleep(r3::time::Duration::from_millis(900)).unwrap();

0 commit comments

Comments
 (0)