aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorGravatar homunkulus <homunkulus@gmx.com> 2017-12-23 10:36:08 +0000
committerGravatar homunkulus <homunkulus@gmx.com> 2017-12-23 10:36:08 +0000
commit8a396c51f2caaeca7ee0f81ef2f3c4f2f73d8df1 (patch)
treefa6538343f2d524be574285c2bb68057edc11420 /examples
parent0f5784c2401d4b12004f34345e721598fa21219a (diff)
parenta238fd5dc783f57f8fa61795690e6069b1becd32 (diff)
downloadrtic-8a396c51f2caaeca7ee0f81ef2f3c4f2f73d8df1.tar.gz
rtic-8a396c51f2caaeca7ee0f81ef2f3c4f2f73d8df1.tar.zst
rtic-8a396c51f2caaeca7ee0f81ef2f3c4f2f73d8df1.zip
Auto merge of #58 - japaric:init-resources, r=japaric
safe `&'static mut` references via init.resources see RFC #59 for details
Diffstat (limited to '')
-rw-r--r--examples/custom-type.rs50
-rw-r--r--examples/full-syntax.rs14
-rw-r--r--examples/generics.rs2
-rw-r--r--examples/late-resources.rs1
-rw-r--r--examples/one-task.rs6
-rw-r--r--examples/preemption.rs6
-rw-r--r--examples/safe-static-mut-ref.rs32
-rw-r--r--examples/two-tasks.rs8
8 files changed, 100 insertions, 19 deletions
diff --git a/examples/custom-type.rs b/examples/custom-type.rs
new file mode 100644
index 00000000..79d6cc46
--- /dev/null
+++ b/examples/custom-type.rs
@@ -0,0 +1,50 @@
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![feature(proc_macro)]
+#![no_std]
+
+extern crate cortex_m_rtfm as rtfm;
+extern crate stm32f103xx;
+
+use rtfm::{app, Threshold};
+
+pub struct Foo;
+
+app! {
+ device: stm32f103xx,
+
+ resources: {
+ static CO_OWNED: Foo = Foo;
+ static ON: Foo = Foo;
+ static OWNED: Foo = Foo;
+ static SHARED: Foo = Foo;
+ },
+
+ idle: {
+ resources: [OWNED, SHARED],
+ },
+
+ tasks: {
+ SYS_TICK: {
+ path: sys_tick,
+ resources: [CO_OWNED, ON, SHARED],
+ },
+
+ TIM2: {
+ enabled: false,
+ path: tim2,
+ priority: 1,
+ resources: [CO_OWNED],
+ },
+ },
+}
+
+fn init(_p: ::init::Peripherals, _r: ::init::Resources) {}
+
+fn idle(_t: &mut Threshold, _r: ::idle::Resources) -> ! {
+ loop {}
+}
+
+fn sys_tick(_t: &mut Threshold, _r: SYS_TICK::Resources) {}
+
+fn tim2(_t: &mut Threshold, _r: TIM2::Resources) {}
diff --git a/examples/full-syntax.rs b/examples/full-syntax.rs
index a8f79a72..5b274122 100644
--- a/examples/full-syntax.rs
+++ b/examples/full-syntax.rs
@@ -63,22 +63,22 @@ mod main {
*r.OWNED != *r.OWNED;
if *r.OWNED {
- if r.SHARED.claim(t, |shared, _| **shared) {
+ if r.SHARED.claim(t, |shared, _| *shared) {
rtfm::wfi();
}
} else {
- r.SHARED.claim_mut(t, |shared, _| **shared = !**shared);
+ r.SHARED.claim_mut(t, |shared, _| *shared = !*shared);
}
}
}
}
-fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) {
- **r.ON = !**r.ON;
+fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
+ *r.ON = !*r.ON;
- **r.CO_OWNED += 1;
+ *r.CO_OWNED += 1;
}
-fn tim2(_t: &mut Threshold, r: TIM2::Resources) {
- **r.CO_OWNED += 1;
+fn tim2(_t: &mut Threshold, mut r: TIM2::Resources) {
+ *r.CO_OWNED += 1;
}
diff --git a/examples/generics.rs b/examples/generics.rs
index 7cf9257b..ca7726d0 100644
--- a/examples/generics.rs
+++ b/examples/generics.rs
@@ -70,5 +70,5 @@ fn exti0(t: &mut Threshold, r: EXTI0::Resources) {
// This task has direct access to the resources
fn exti1(t: &mut Threshold, r: EXTI1::Resources) {
- work(t, r.GPIOA, r.SPI1);
+ work(t, &r.GPIOA, &r.SPI1);
}
diff --git a/examples/late-resources.rs b/examples/late-resources.rs
index d42431c2..07c321f6 100644
--- a/examples/late-resources.rs
+++ b/examples/late-resources.rs
@@ -1,5 +1,4 @@
//! Demonstrates initialization of resources in `init`.
-
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(proc_macro)]
diff --git a/examples/one-task.rs b/examples/one-task.rs
index 2e776768..07def59b 100644
--- a/examples/one-task.rs
+++ b/examples/one-task.rs
@@ -77,11 +77,11 @@ fn idle() -> ! {
// `r` is the set of resources this task has access to. `SYS_TICK::Resources`
// has one field per resource declared in `app!`.
#[allow(unsafe_code)]
-fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) {
+fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
// toggle state
- **r.ON = !**r.ON;
+ *r.ON = !*r.ON;
- if **r.ON {
+ if *r.ON {
// set the pin PC13 high
// NOTE(unsafe) atomic write to a stateless register
unsafe {
diff --git a/examples/preemption.rs b/examples/preemption.rs
index 98dde8d1..8e501887 100644
--- a/examples/preemption.rs
+++ b/examples/preemption.rs
@@ -42,12 +42,12 @@ fn idle() -> ! {
}
}
-fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) {
+fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
// ..
// This task can't be preempted by `tim2` so it has direct access to the
// resource data
- **r.COUNTER += 1;
+ *r.COUNTER += 1;
// ..
}
@@ -61,7 +61,7 @@ fn tim2(t: &mut Threshold, mut r: TIM2::Resources) {
// lead to undefined behavior.
r.COUNTER.claim_mut(t, |counter, _t| {
// `claim_mut` creates a critical section
- **counter += 1;
+ *counter += 1;
});
// ..
diff --git a/examples/safe-static-mut-ref.rs b/examples/safe-static-mut-ref.rs
new file mode 100644
index 00000000..bb872122
--- /dev/null
+++ b/examples/safe-static-mut-ref.rs
@@ -0,0 +1,32 @@
+//! Safe creation of `&'static mut` references
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![feature(proc_macro)]
+#![no_std]
+
+extern crate cortex_m_rtfm as rtfm;
+extern crate stm32f103xx;
+
+use rtfm::app;
+
+app! {
+ device: stm32f103xx,
+
+ resources: {
+ static BUFFER: [u8; 16] = [0; 16];
+ },
+
+ init: {
+ resources: [BUFFER],
+ },
+}
+
+fn init(_p: init::Peripherals, r: init::Resources) {
+ let _buf: &'static mut [u8] = r.BUFFER;
+}
+
+fn idle() -> ! {
+ loop {
+ rtfm::wfi();
+ }
+}
diff --git a/examples/two-tasks.rs b/examples/two-tasks.rs
index df6e784a..4f567f0c 100644
--- a/examples/two-tasks.rs
+++ b/examples/two-tasks.rs
@@ -42,18 +42,18 @@ fn idle() -> ! {
// As both tasks are running at the same priority one can't preempt the other.
// Thus both tasks have direct access to the resource
-fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) {
+fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
// ..
- **r.COUNTER += 1;
+ *r.COUNTER += 1;
// ..
}
-fn tim2(_t: &mut Threshold, r: TIM2::Resources) {
+fn tim2(_t: &mut Threshold, mut r: TIM2::Resources) {
// ..
- **r.COUNTER += 1;
+ *r.COUNTER += 1;
// ..
}