aboutsummaryrefslogtreecommitdiff
path: root/examples/lock-free.rs
blob: db74c7d8b058f95375c73e468e365ef14c24cb92 (plain)
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
//! examples/lock-free.rs

#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]

use panic_semihosting as _;

#[rtic::app(device = lm3s6965)]
mod app {
    use cortex_m_semihosting::{debug, hprintln};
    use lm3s6965::Interrupt;

    #[shared]
    struct Shared {
        #[lock_free] // <- lock-free shared resource
        counter: u64,
    }

    #[local]
    struct Local {}

    #[init]
    fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
        rtic::pend(Interrupt::GPIOA);

        (Shared { counter: 0 }, Local {}, init::Monotonics())
    }

    #[task(binds = GPIOA, shared = [counter])] // <- same priority
    fn gpioa(c: gpioa::Context) {
        hprintln!("GPIOA/start").unwrap();
        rtic::pend(Interrupt::GPIOB);

        *c.shared.counter += 1; // <- no lock API required
        let counter = *c.shared.counter;
        hprintln!("  GPIOA/counter = {}", counter).unwrap();

        if counter == 5 {
            debug::exit(debug::EXIT_SUCCESS);
        }
        hprintln!("GPIOA/end").unwrap();
    }

    #[task(binds = GPIOB, shared = [counter])] // <- same priority
    fn gpiob(c: gpiob::Context) {
        hprintln!("GPIOB/start").unwrap();
        rtic::pend(Interrupt::GPIOA);

        *c.shared.counter += 1; // <- no lock API required
        let counter = *c.shared.counter;
        hprintln!("  GPIOB/counter = {}", counter).unwrap();

        if counter == 5 {
            debug::exit(debug::EXIT_SUCCESS);
        }
        hprintln!("GPIOB/end").unwrap();
    }
}