aboutsummaryrefslogtreecommitdiff
path: root/examples/generics.rs
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2017-07-20 22:53:44 -0500
committerGravatar Jorge Aparicio <jorge@japaric.io> 2017-07-20 22:53:44 -0500
commitc7b9507a57f2ba28c18b15dd2719a1c56f74a302 (patch)
tree1fed754475cdbe4baf0d2f1445ce2777b437de31 /examples/generics.rs
parent23425f2f0645cdfbf78135848fe87f733072ade3 (diff)
downloadrtic-c7b9507a57f2ba28c18b15dd2719a1c56f74a302.tar.gz
rtic-c7b9507a57f2ba28c18b15dd2719a1c56f74a302.tar.zst
rtic-c7b9507a57f2ba28c18b15dd2719a1c56f74a302.zip
`Resource` trait, docs, examples and rtfm-syntax related changes
Diffstat (limited to 'examples/generics.rs')
-rw-r--r--examples/generics.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/examples/generics.rs b/examples/generics.rs
new file mode 100644
index 00000000..335d159b
--- /dev/null
+++ b/examples/generics.rs
@@ -0,0 +1,69 @@
+//! Working with resources in a generic fashion
+
+#![deny(unsafe_code)]
+#![feature(proc_macro)]
+#![no_std]
+
+#[macro_use(task)]
+extern crate cortex_m_rtfm as rtfm;
+extern crate stm32f103xx;
+
+use rtfm::{app, Resource, Threshold};
+use stm32f103xx::{SPI1, GPIOA};
+
+app! {
+ device: stm32f103xx,
+
+ tasks: {
+ EXTI0: {
+ enabled: true,
+ priority: 1,
+ resources: [GPIOA, SPI1],
+ },
+
+ EXTI1: {
+ enabled: true,
+ priority: 2,
+ resources: [GPIOA, SPI1],
+ },
+ },
+}
+
+fn init(_p: init::Peripherals) {}
+
+fn idle() -> ! {
+ loop {
+ rtfm::wfi();
+ }
+}
+
+// a generic function to use resources in any task (regardless of its priority)
+fn work<G, S>(t: &mut Threshold, gpioa: &G, spi1: &S)
+where
+ G: Resource<Data = GPIOA>,
+ S: Resource<Data = SPI1>,
+{
+ gpioa.claim(t, |_gpioa, t| {
+ // drive NSS low
+
+ spi1.claim(t, |_spi1, _| {
+ // transfer data
+ });
+
+ // drive NSS high
+ });
+}
+
+task!(EXTI0, exti0);
+
+// this task needs critical sections to access the resources
+fn exti0(t: &mut Threshold, r: EXTI0::Resources) {
+ work(t, &r.GPIOA, &r.SPI1);
+}
+
+task!(EXTI1, exti1);
+
+// this task has direct access to the resources
+fn exti1(t: &mut Threshold, r: EXTI1::Resources) {
+ work(t, r.GPIOA, r.SPI1);
+}