aboutsummaryrefslogtreecommitdiff
path: root/src/examples/_5_generics.rs
blob: a8f42cdff23a923279a44c4e950c0c69f84ecfaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! 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);
//! }
//! ```
// Auto-generated. Do not modify.