mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-27 14:04:56 +01:00
Merge #415
415: Multilock support r=AfoHT a=korken89 Co-authored-by: Emil Fresk <emil.fresk@gmail.com>
This commit is contained in:
commit
9527c92192
11 changed files with 102 additions and 2 deletions
1
.github/workflows/build.yml
vendored
1
.github/workflows/build.yml
vendored
|
@ -243,6 +243,7 @@ jobs:
|
|||
|
||||
resource
|
||||
lock
|
||||
multilock
|
||||
late
|
||||
only-shared-access
|
||||
|
||||
|
|
|
@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- Support for multi-locks, see `examples/multilock.rs` for syntax.
|
||||
|
||||
## [v0.6.0-alpha.0] - 2020-11-14
|
||||
|
||||
### Added
|
||||
|
|
|
@ -57,7 +57,7 @@ required-features = ["__v7"]
|
|||
[dependencies]
|
||||
cortex-m = "0.6.2"
|
||||
cortex-m-rtic-macros = { path = "macros", version = "0.6.0-alpha.0" }
|
||||
rtic-core = "0.3.0"
|
||||
rtic-core = "0.3.1"
|
||||
cortex-m-rt = "0.6.9"
|
||||
heapless = "0.5.0"
|
||||
bare-metal = "1.0.0"
|
||||
|
|
|
@ -64,6 +64,14 @@ $ cargo run --example lock
|
|||
{{#include ../../../../ci/expected/lock.run}}
|
||||
```
|
||||
|
||||
## Multi-lock
|
||||
|
||||
As an extension to `lock`, and to reduce rightward drift, locks can be taken as tuples. The following examples shows this in use:
|
||||
|
||||
``` rust
|
||||
{{#include ../../../../examples/multilock.rs}}
|
||||
```
|
||||
|
||||
## Late resources
|
||||
|
||||
Late resources are resources that are not given an initial value at compile time using the `#[init]` attribute but instead are initialized at runtime using the `init::LateResources` values returned by the `init` function.
|
||||
|
|
4
ci/expected/multilock.run
Normal file
4
ci/expected/multilock.run
Normal file
|
@ -0,0 +1,4 @@
|
|||
Multiple single locks
|
||||
Multiple single locks, s1: 1, s2: 1, s3: 1
|
||||
Multilock!
|
||||
Multiple single locks, s1: 2, s2: 2, s3: 2
|
78
examples/multilock.rs
Normal file
78
examples/multilock.rs
Normal file
|
@ -0,0 +1,78 @@
|
|||
//! examples/mutlilock.rs
|
||||
//!
|
||||
//! The multi-lock feature example.
|
||||
|
||||
#![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;
|
||||
|
||||
#[resources]
|
||||
struct Resources {
|
||||
#[init(0)]
|
||||
shared1: u32,
|
||||
#[init(0)]
|
||||
shared2: u32,
|
||||
#[init(0)]
|
||||
shared3: u32,
|
||||
}
|
||||
|
||||
#[init]
|
||||
fn init(_: init::Context) -> init::LateResources {
|
||||
rtic::pend(Interrupt::GPIOA);
|
||||
|
||||
init::LateResources {}
|
||||
}
|
||||
|
||||
// when omitted priority is assumed to be `1`
|
||||
#[task(binds = GPIOA, resources = [shared1, shared2, shared3])]
|
||||
fn locks(c: locks::Context) {
|
||||
let mut s1 = c.resources.shared1;
|
||||
let mut s2 = c.resources.shared2;
|
||||
let mut s3 = c.resources.shared3;
|
||||
|
||||
hprintln!("Multiple single locks").unwrap();
|
||||
s1.lock(|s1| {
|
||||
s2.lock(|s2| {
|
||||
s3.lock(|s3| {
|
||||
*s1 += 1;
|
||||
*s2 += 1;
|
||||
*s3 += 1;
|
||||
|
||||
hprintln!(
|
||||
"Multiple single locks, s1: {}, s2: {}, s3: {}",
|
||||
*s1,
|
||||
*s2,
|
||||
*s3
|
||||
)
|
||||
.unwrap();
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
hprintln!("Multilock!").unwrap();
|
||||
|
||||
(s1, s2, s3).lock(|s1, s2, s3| {
|
||||
*s1 += 1;
|
||||
*s2 += 1;
|
||||
*s3 += 1;
|
||||
|
||||
hprintln!(
|
||||
"Multiple single locks, s1: {}, s2: {}, s3: {}",
|
||||
*s1,
|
||||
*s2,
|
||||
*s3
|
||||
)
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
debug::exit(debug::EXIT_SUCCESS);
|
||||
}
|
||||
}
|
|
@ -105,6 +105,7 @@ pub fn codegen(
|
|||
#[allow(non_snake_case)]
|
||||
fn #name(#(#locals_pat,)* #context: #name::Context) {
|
||||
use rtic::Mutex as _;
|
||||
use rtic::mutex_prelude::*;
|
||||
|
||||
#(#stmts)*
|
||||
}
|
||||
|
|
|
@ -68,6 +68,7 @@ pub fn codegen(
|
|||
#[allow(non_snake_case)]
|
||||
fn #name(#(#locals_pat,)* #context: #name::Context) -> ! {
|
||||
use rtic::Mutex as _;
|
||||
use rtic::mutex_prelude::*;
|
||||
|
||||
#(#stmts)*
|
||||
}
|
||||
|
|
|
@ -219,6 +219,7 @@ pub fn codegen(
|
|||
pub fn spawn(#(#args,)*) -> Result<(), #ty> {
|
||||
// #let_instant // do we need it?
|
||||
use rtic::Mutex as _;
|
||||
use rtic::mutex_prelude::*;
|
||||
|
||||
let input = #tupled;
|
||||
|
||||
|
@ -258,6 +259,7 @@ pub fn codegen(
|
|||
) -> Result<(), #ty> {
|
||||
unsafe {
|
||||
use rtic::Mutex as _;
|
||||
use rtic::mutex_prelude::*;
|
||||
|
||||
let input = #tupled;
|
||||
if let Some(index) = rtic::export::interrupt::free(|_| #app_path::#fq.dequeue()) {
|
||||
|
|
|
@ -111,6 +111,7 @@ pub fn codegen(
|
|||
#[allow(non_snake_case)]
|
||||
fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) {
|
||||
use rtic::Mutex as _;
|
||||
use rtic::mutex_prelude::*;
|
||||
|
||||
#(#stmts)*
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ use cortex_m::{
|
|||
};
|
||||
use cortex_m_rt as _; // vector table
|
||||
pub use cortex_m_rtic_macros::app;
|
||||
pub use rtic_core::{Exclusive, Mutex};
|
||||
pub use rtic_core::{prelude as mutex_prelude, Exclusive, Mutex};
|
||||
|
||||
#[cfg(armv7m)]
|
||||
pub mod cyccnt;
|
||||
|
|
Loading…
Reference in a new issue